diff --git a/src/document/UBDocumentController.cpp b/src/document/UBDocumentController.cpp index 22eda173..cc9c1412 100644 --- a/src/document/UBDocumentController.cpp +++ b/src/document/UBDocumentController.cpp @@ -1034,6 +1034,16 @@ QString UBDocumentTreeModel::virtualPathForIndex(const QModelIndex &pIndex) cons return virtualDirForIndex(pIndex) + "/" + curNode->nodeName(); } +QList UBDocumentTreeModel::nodeChildrenFromIndex(const QModelIndex &pIndex) const +{ + UBDocumentTreeNode *node = nodeFromIndex(pIndex); + + if (node) + return node->children(); + else + return QList(); +} + QStringList UBDocumentTreeModel::nodeNameList(const QModelIndex &pIndex, bool distinctNodeType) const { QStringList result; @@ -1668,14 +1678,20 @@ void UBDocumentTreeItemDelegate::commitAndCloseEditor() void UBDocumentTreeItemDelegate::processChangedText(const QString &str) const { QLineEdit *editor = qobject_cast(sender()); - if (!editor) { - return; - } - - if (!validateString(str)) { - editor->setStyleSheet("background-color: #FFB3C8;"); - } else { - editor->setStyleSheet("background-color: #FFFFFF;"); + if (editor) + { + if (editor->validator()) + { + int pos = 0; + if (editor->validator()->validate(const_cast(str), pos) != QValidator::Acceptable) + { + editor->setStyleSheet("background-color: #FFB3C8;"); + } + else + { + editor->setStyleSheet("background-color: #FFFFFF;"); + } + } } } @@ -1702,17 +1718,29 @@ QWidget *UBDocumentTreeItemDelegate::createEditor(QWidget *parent, const QStyleO QModelIndex sourceIndex = proxy->mapToSource(index); - if (docModel) { + if (docModel) + { mExistingFileNames = docModel->nodeNameList(sourceIndex.parent()); mExistingFileNames.removeOne(sourceIndex.data().toString()); + + UBDocumentTreeNode* sourceNode = docModel->nodeFromIndex(sourceIndex); + + if (sourceNode) + { + QLineEdit *nameEditor = new QLineEdit(parent); + QList nodeChildren = docModel->nodeChildrenFromIndex(sourceIndex.parent()); + nodeChildren.removeOne(sourceNode); + + UBValidator* validator = new UBValidator(nodeChildren, sourceNode->nodeType()); + nameEditor->setValidator(validator); + connect(nameEditor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor())); + connect(nameEditor, SIGNAL(textChanged(QString)), this, SLOT(processChangedText(QString))); + + return nameEditor; + } } - QLineEdit *nameEditor = new QLineEdit(parent); - UBValidator* validator = new UBValidator(mExistingFileNames); - nameEditor->setValidator(validator); - connect(nameEditor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor())); - connect(nameEditor, SIGNAL(textChanged(QString)), this, SLOT(processChangedText(QString))); - return nameEditor; + return nullptr; } //N/C - NNe - 20140407 : the other column are not editable. @@ -1731,8 +1759,17 @@ void UBDocumentTreeItemDelegate::setEditorData(QWidget *editor, const QModelInde void UBDocumentTreeItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QLineEdit *lineEditor = qobject_cast(editor); - if (validateString(lineEditor->text())) { - model->setData(index, lineEditor->text()); + if (lineEditor) + { + int pos; + QString input = lineEditor->text(); + if (lineEditor->validator()) + { + if (lineEditor->validator()->validate(input, pos) == QValidator::Acceptable) + { + model->setData(index, input); + } + } } } diff --git a/src/document/UBDocumentController.h b/src/document/UBDocumentController.h index fa613d04..6c673097 100644 --- a/src/document/UBDocumentController.h +++ b/src/document/UBDocumentController.h @@ -210,6 +210,7 @@ public: QString virtualDirForIndex(const QModelIndex &pIndex) const; QString virtualPathForIndex(const QModelIndex &pIndex) const; QStringList nodeNameList(const QModelIndex &pIndex, bool distinctNodeType = false) const; + QList nodeChildrenFromIndex(const QModelIndex &pIndex) const; bool newNodeAllowed(const QModelIndex &pSelectedIndex) const; QModelIndex goTo(const QString &dir); bool inTrash(const QModelIndex &index) const; @@ -324,22 +325,27 @@ private: class UBValidator : public QValidator { - const QStringList mExistingFileNames; + const QList mExistingNodes; + UBDocumentTreeNode::Type mEditedNodeType; public: - UBValidator(const QStringList existingFileNames, QObject *parent = nullptr) + UBValidator(const QList existingNodes, UBDocumentTreeNode::Type editedNodeType, QObject *parent = nullptr) : QValidator(parent) - , mExistingFileNames(existingFileNames) + , mExistingNodes(existingNodes) + , mEditedNodeType(editedNodeType) { } QValidator::State validate(QString &input, int &pos) const { - if (mExistingFileNames.contains(input)) - return QValidator::Intermediate; - else - return QValidator::Acceptable; + for (auto node : mExistingNodes) + { + if (node->nodeName() == input && node->nodeType() == mEditedNodeType) + return QValidator::Intermediate; + } + + return QValidator::Acceptable; } };