mirror of
				https://github.com/OpenMW/openmw.git
				synced 2025-10-25 07:56:37 +00:00 
			
		
		
		
	integrated namespace widget into input field (GenericCreator)
This commit is contained in:
		
							parent
							
								
									fa9c6b19c2
								
							
						
					
					
						commit
						c66dfe67b1
					
				
					 4 changed files with 111 additions and 24 deletions
				
			
		|  | @ -10,6 +10,8 @@ | ||||||
| #include <QLabel> | #include <QLabel> | ||||||
| #include <QComboBox> | #include <QComboBox> | ||||||
| 
 | 
 | ||||||
|  | #include <components/misc/stringops.hpp> | ||||||
|  | 
 | ||||||
| #include "../../model/world/commands.hpp" | #include "../../model/world/commands.hpp" | ||||||
| #include "../../model/world/data.hpp" | #include "../../model/world/data.hpp" | ||||||
| #include "../../model/world/idtable.hpp" | #include "../../model/world/idtable.hpp" | ||||||
|  | @ -24,9 +26,6 @@ void CSVWorld::GenericCreator::update() | ||||||
|     mId->setToolTip (QString::fromUtf8 (mErrors.c_str())); |     mId->setToolTip (QString::fromUtf8 (mErrors.c_str())); | ||||||
| 
 | 
 | ||||||
|     mCreate->setEnabled (mErrors.empty() && !mLocked); |     mCreate->setEnabled (mErrors.empty() && !mLocked); | ||||||
| 
 |  | ||||||
|     if (mNamespace) |  | ||||||
|         mNamespace->setText (QString::fromUtf8 (getNamespace().c_str())); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void CSVWorld::GenericCreator::setManualEditing (bool enabled) | void CSVWorld::GenericCreator::setManualEditing (bool enabled) | ||||||
|  | @ -87,17 +86,40 @@ std::string CSVWorld::GenericCreator::getNamespace() const | ||||||
|     return ""; |     return ""; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | void CSVWorld::GenericCreator::updateNamespace() | ||||||
|  | { | ||||||
|  |     std::string namespace_ = getNamespace(); | ||||||
|  | 
 | ||||||
|  |     mValidator->setNamespace (namespace_); | ||||||
|  | 
 | ||||||
|  |     int index = mId->text().indexOf ("::"); | ||||||
|  | 
 | ||||||
|  |     if (index==-1) | ||||||
|  |     { | ||||||
|  |         // no namespace in old text
 | ||||||
|  |         mId->setText (QString::fromUtf8 (namespace_.c_str()) + mId->text()); | ||||||
|  |     } | ||||||
|  |     else | ||||||
|  |     { | ||||||
|  |         std::string oldNamespace = | ||||||
|  |             Misc::StringUtils::lowerCase (mId->text().left (index).toUtf8().constData()); | ||||||
|  | 
 | ||||||
|  |         if (oldNamespace=="project" || oldNamespace=="session") | ||||||
|  |             mId->setText (QString::fromUtf8 (namespace_.c_str()) + mId->text().mid (index+2)); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack, | CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack, | ||||||
|     const CSMWorld::UniversalId& id, bool relaxedIdRules) |     const CSMWorld::UniversalId& id, bool relaxedIdRules) | ||||||
| : mData (data), mUndoStack (undoStack), mListId (id), mLocked (false), mCloneMode (false), | : mData (data), mUndoStack (undoStack), mListId (id), mLocked (false), mCloneMode (false), | ||||||
|   mClonedType (CSMWorld::UniversalId::Type_None), mScopes (CSMWorld::Scope_Content), mScope (0), |   mClonedType (CSMWorld::UniversalId::Type_None), mScopes (CSMWorld::Scope_Content), mScope (0), | ||||||
|   mScopeLabel (0), mNamespace (0) |   mScopeLabel (0) | ||||||
| { | { | ||||||
|     mLayout = new QHBoxLayout; |     mLayout = new QHBoxLayout; | ||||||
|     mLayout->setContentsMargins (0, 0, 0, 0); |     mLayout->setContentsMargins (0, 0, 0, 0); | ||||||
| 
 | 
 | ||||||
|     mId = new QLineEdit; |     mId = new QLineEdit; | ||||||
|     mId->setValidator (new IdValidator (relaxedIdRules, this)); |     mId->setValidator (mValidator = new IdValidator (relaxedIdRules, this)); | ||||||
|     mLayout->addWidget (mId, 1); |     mLayout->addWidget (mId, 1); | ||||||
| 
 | 
 | ||||||
|     mCreate = new QPushButton ("Create"); |     mCreate = new QPushButton ("Create"); | ||||||
|  | @ -125,6 +147,7 @@ void CSVWorld::GenericCreator::reset() | ||||||
|     mCloneMode = false; |     mCloneMode = false; | ||||||
|     mId->setText (""); |     mId->setText (""); | ||||||
|     update(); |     update(); | ||||||
|  |     updateNamespace(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| std::string CSVWorld::GenericCreator::getErrors() const | std::string CSVWorld::GenericCreator::getErrors() const | ||||||
|  | @ -197,21 +220,6 @@ void CSVWorld::GenericCreator::setScope (unsigned int scope) | ||||||
|     int count = (mScopes & CSMWorld::Scope_Content) + (mScopes & CSMWorld::Scope_Project) + |     int count = (mScopes & CSMWorld::Scope_Content) + (mScopes & CSMWorld::Scope_Project) + | ||||||
|         (mScopes & CSMWorld::Scope_Session); |         (mScopes & CSMWorld::Scope_Session); | ||||||
| 
 | 
 | ||||||
|     // namespace widget
 |  | ||||||
|     if (count>1 || (count>0 && !(mScopes & CSMWorld::Scope_Content))) |  | ||||||
|     { |  | ||||||
|         if (!mNamespace) |  | ||||||
|         { |  | ||||||
|             mNamespace = new QLabel ("::", this); |  | ||||||
|             insertAtBeginning (mNamespace, false); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|     else |  | ||||||
|     { |  | ||||||
|         delete mNamespace; |  | ||||||
|         mNamespace = 0; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     // scope selector widget
 |     // scope selector widget
 | ||||||
|     if (count>1) |     if (count>1) | ||||||
|     { |     { | ||||||
|  | @ -242,9 +250,12 @@ void CSVWorld::GenericCreator::setScope (unsigned int scope) | ||||||
|         delete mScopeLabel; |         delete mScopeLabel; | ||||||
|         mScopeLabel = 0; |         mScopeLabel = 0; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     updateNamespace(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void CSVWorld::GenericCreator::scopeChanged (int index) | void CSVWorld::GenericCreator::scopeChanged (int index) | ||||||
| { | { | ||||||
|     update(); |     update(); | ||||||
|  |     updateNamespace(); | ||||||
| } | } | ||||||
|  | @ -19,6 +19,8 @@ namespace CSMWorld | ||||||
| 
 | 
 | ||||||
| namespace CSVWorld | namespace CSVWorld | ||||||
| { | { | ||||||
|  |     class IdValidator; | ||||||
|  | 
 | ||||||
|     class GenericCreator : public Creator |     class GenericCreator : public Creator | ||||||
|     { |     { | ||||||
|             Q_OBJECT |             Q_OBJECT | ||||||
|  | @ -36,7 +38,7 @@ namespace CSVWorld | ||||||
|             unsigned int mScopes; |             unsigned int mScopes; | ||||||
|             QComboBox *mScope; |             QComboBox *mScope; | ||||||
|             QLabel *mScopeLabel; |             QLabel *mScopeLabel; | ||||||
|             QLabel *mNamespace; |             IdValidator *mValidator; | ||||||
| 
 | 
 | ||||||
|         protected: |         protected: | ||||||
|             bool mCloneMode; |             bool mCloneMode; | ||||||
|  | @ -62,6 +64,10 @@ namespace CSVWorld | ||||||
| 
 | 
 | ||||||
|             std::string getNamespace() const; |             std::string getNamespace() const; | ||||||
| 
 | 
 | ||||||
|  |         private: | ||||||
|  | 
 | ||||||
|  |             void updateNamespace(); | ||||||
|  | 
 | ||||||
|         public: |         public: | ||||||
| 
 | 
 | ||||||
|             GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack, |             GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack, | ||||||
|  |  | ||||||
|  | @ -1,6 +1,8 @@ | ||||||
| 
 | 
 | ||||||
| #include "idvalidator.hpp" | #include "idvalidator.hpp" | ||||||
| 
 | 
 | ||||||
|  | #include <components/misc/stringops.hpp> | ||||||
|  | 
 | ||||||
| bool CSVWorld::IdValidator::isValid (const QChar& c, bool first) const | bool CSVWorld::IdValidator::isValid (const QChar& c, bool first) const | ||||||
| { | { | ||||||
|     if (c.isLetter() || c=='_') |     if (c.isLetter() || c=='_') | ||||||
|  | @ -26,11 +28,74 @@ QValidator::State CSVWorld::IdValidator::validate (QString& input, int& pos) con | ||||||
|     else |     else | ||||||
|     { |     { | ||||||
|         bool first = true; |         bool first = true; | ||||||
|  |         bool scope = false; | ||||||
|  |         bool prevScope = false; | ||||||
| 
 | 
 | ||||||
|         for (QString::const_iterator iter (input.begin()); iter!=input.end(); ++iter, first = false) |         QString::const_iterator iter = input.begin(); | ||||||
|             if (!isValid (*iter, first)) | 
 | ||||||
|                 return QValidator::Invalid; |         if (!mNamespace.empty()) | ||||||
|  |         { | ||||||
|  |             std::string namespace_ = input.left (mNamespace.size()).toUtf8().constData(); | ||||||
|  | 
 | ||||||
|  |             if (Misc::StringUtils::lowerCase (namespace_)!=mNamespace) | ||||||
|  |                 return QValidator::Invalid; // incorrect namespace
 | ||||||
|  | 
 | ||||||
|  |             iter += namespace_.size(); | ||||||
|  |             first = false; | ||||||
|  |             prevScope = true; | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             int index = input.indexOf (":"); | ||||||
|  | 
 | ||||||
|  |             if (index!=-1) | ||||||
|  |             { | ||||||
|  |                 QString namespace_ = input.left (index); | ||||||
|  | 
 | ||||||
|  |                 if (namespace_=="project" || namespace_=="session") | ||||||
|  |                     return QValidator::Invalid; // reserved namespace
 | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         for (; iter!=input.end(); ++iter, first = false) | ||||||
|  |         { | ||||||
|  |             if (*iter==':') | ||||||
|  |             { | ||||||
|  |                 if (first) | ||||||
|  |                     return QValidator::Invalid; // scope operator at the beginning
 | ||||||
|  | 
 | ||||||
|  |                 if (scope) | ||||||
|  |                 { | ||||||
|  |                     scope = false; | ||||||
|  |                     prevScope = true; | ||||||
|  |                 } | ||||||
|  |                 else | ||||||
|  |                 { | ||||||
|  |                     if (prevScope) | ||||||
|  |                         return QValidator::Invalid; // sequence of two scope operators
 | ||||||
|  | 
 | ||||||
|  |                     scope = true; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             else if (scope) | ||||||
|  |                 return QValidator::Invalid; // incomplete scope operator
 | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 prevScope = false; | ||||||
|  | 
 | ||||||
|  |                 if (!isValid (*iter, first)) | ||||||
|  |                     return QValidator::Invalid; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (prevScope) | ||||||
|  |             return QValidator::Intermediate; // ending with scope operator
 | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return QValidator::Acceptable; |     return QValidator::Acceptable; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void CSVWorld::IdValidator::setNamespace (const std::string& namespace_) | ||||||
|  | { | ||||||
|  |     mNamespace = Misc::StringUtils::lowerCase (namespace_); | ||||||
| } | } | ||||||
|  | @ -1,6 +1,8 @@ | ||||||
| #ifndef CSV_WORLD_IDVALIDATOR_H | #ifndef CSV_WORLD_IDVALIDATOR_H | ||||||
| #define CSV_WORLD_IDVALIDATOR_H | #define CSV_WORLD_IDVALIDATOR_H | ||||||
| 
 | 
 | ||||||
|  | #include <string> | ||||||
|  | 
 | ||||||
| #include <QValidator> | #include <QValidator> | ||||||
| 
 | 
 | ||||||
| namespace CSVWorld | namespace CSVWorld | ||||||
|  | @ -8,6 +10,7 @@ namespace CSVWorld | ||||||
|     class IdValidator : public QValidator |     class IdValidator : public QValidator | ||||||
|     { |     { | ||||||
|             bool mRelaxed; |             bool mRelaxed; | ||||||
|  |             std::string mNamespace; | ||||||
| 
 | 
 | ||||||
|         private: |         private: | ||||||
| 
 | 
 | ||||||
|  | @ -20,6 +23,8 @@ namespace CSVWorld | ||||||
| 
 | 
 | ||||||
|             virtual State validate (QString& input, int& pos) const; |             virtual State validate (QString& input, int& pos) const; | ||||||
| 
 | 
 | ||||||
|  |             void setNamespace (const std::string& namespace_); | ||||||
|  | 
 | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue