diff --git a/src/document/UBDocumentController.cpp b/src/document/UBDocumentController.cpp index 19fc3e77..523b87da 100644 --- a/src/document/UBDocumentController.cpp +++ b/src/document/UBDocumentController.cpp @@ -1645,10 +1645,15 @@ UBDocumentTreeItemDelegate::UBDocumentTreeItemDelegate(QObject *parent) void UBDocumentTreeItemDelegate::commitAndCloseEditor() { - QLineEdit *lineEditor = qobject_cast(sender()); - if (lineEditor) { - emit commitData(lineEditor); + QLineEdit *lineEditor = dynamic_cast(sender()); + if (lineEditor) + { + if (lineEditor->hasAcceptableInput()) + { + emit commitData(lineEditor); //emit closeEditor(lineEditor); + } + emit UBApplication::documentController->reorderDocumentsRequested(); } } @@ -1678,13 +1683,26 @@ QWidget *UBDocumentTreeItemDelegate::createEditor(QWidget *parent, const QStyleO //N/C - NNE - 20140407 : Add the test for the index column. if(index.column() == 0){ mExistingFileNames.clear(); - const UBDocumentTreeModel *indexModel = qobject_cast(index.model()); - if (indexModel) { - mExistingFileNames = indexModel->nodeNameList(index.parent()); - mExistingFileNames.removeOne(index.data().toString()); + const UBDocumentTreeModel *docModel = 0; + + const UBSortFilterProxyModel *proxy = dynamic_cast(index.model()); + + if(proxy){ + docModel = dynamic_cast(proxy->sourceModel()); + }else{ + docModel = dynamic_cast(index.model()); + } + + QModelIndex sourceIndex = proxy->mapToSource(index); + + if (docModel) { + mExistingFileNames = docModel->nodeNameList(sourceIndex.parent()); + mExistingFileNames.removeOne(sourceIndex.data().toString()); } 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; @@ -1760,7 +1778,12 @@ void UBDocumentController::createNewDocument() ? docModel->virtualPathForIndex(selectedIndex) : docModel->virtualDirForIndex(selectedIndex); - UBDocumentProxy *document = pManager->createDocument(groupName); + + QDateTime now = QDateTime::currentDateTime(); + QString documentName = docModel->adjustNameForParentIndex(now.toString(Qt::SystemLocaleShortDate), selectedIndex.parent()); + + UBDocumentProxy *document = pManager->createDocument(groupName, documentName); + selectDocument(document, true, false, true); if (document) diff --git a/src/document/UBDocumentController.h b/src/document/UBDocumentController.h index 9999579d..5ee68d59 100644 --- a/src/document/UBDocumentController.h +++ b/src/document/UBDocumentController.h @@ -321,6 +321,27 @@ private: void updateIndexEnvirons(const QModelIndex &index); }; +class UBValidator : public QValidator +{ + const QStringList mExistingFileNames; + + public: + UBValidator(const QStringList existingFileNames, QObject *parent = nullptr) + : QValidator(parent) + , mExistingFileNames(existingFileNames) + { + + } + + QValidator::State validate(QString &input, int &pos) const + { + if (mExistingFileNames.contains(input)) + return QValidator::Intermediate; + else + return QValidator::Acceptable; + } +}; + class UBDocumentTreeItemDelegate : public QStyledItemDelegate { Q_OBJECT