same behavior for document naming as for folder naming

preferencesAboutTextFull
Clément Fauconnier 3 years ago
parent 05b6bf87b3
commit fe06bdf1df
  1. 39
      src/document/UBDocumentController.cpp
  2. 21
      src/document/UBDocumentController.h

@ -1645,10 +1645,15 @@ UBDocumentTreeItemDelegate::UBDocumentTreeItemDelegate(QObject *parent)
void UBDocumentTreeItemDelegate::commitAndCloseEditor() void UBDocumentTreeItemDelegate::commitAndCloseEditor()
{ {
QLineEdit *lineEditor = qobject_cast<QLineEdit*>(sender()); QLineEdit *lineEditor = dynamic_cast<QLineEdit*>(sender());
if (lineEditor) { if (lineEditor)
emit commitData(lineEditor); {
if (lineEditor->hasAcceptableInput())
{
emit commitData(lineEditor);
//emit closeEditor(lineEditor); //emit closeEditor(lineEditor);
}
emit UBApplication::documentController->reorderDocumentsRequested(); 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. //N/C - NNE - 20140407 : Add the test for the index column.
if(index.column() == 0){ if(index.column() == 0){
mExistingFileNames.clear(); mExistingFileNames.clear();
const UBDocumentTreeModel *indexModel = qobject_cast<const UBDocumentTreeModel*>(index.model()); const UBDocumentTreeModel *docModel = 0;
if (indexModel) {
mExistingFileNames = indexModel->nodeNameList(index.parent()); const UBSortFilterProxyModel *proxy = dynamic_cast<const UBSortFilterProxyModel*>(index.model());
mExistingFileNames.removeOne(index.data().toString());
if(proxy){
docModel = dynamic_cast<UBDocumentTreeModel*>(proxy->sourceModel());
}else{
docModel = dynamic_cast<const UBDocumentTreeModel*>(index.model());
}
QModelIndex sourceIndex = proxy->mapToSource(index);
if (docModel) {
mExistingFileNames = docModel->nodeNameList(sourceIndex.parent());
mExistingFileNames.removeOne(sourceIndex.data().toString());
} }
QLineEdit *nameEditor = new QLineEdit(parent); QLineEdit *nameEditor = new QLineEdit(parent);
UBValidator* validator = new UBValidator(mExistingFileNames);
nameEditor->setValidator(validator);
connect(nameEditor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor())); connect(nameEditor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
connect(nameEditor, SIGNAL(textChanged(QString)), this, SLOT(processChangedText(QString))); connect(nameEditor, SIGNAL(textChanged(QString)), this, SLOT(processChangedText(QString)));
return nameEditor; return nameEditor;
@ -1760,7 +1778,12 @@ void UBDocumentController::createNewDocument()
? docModel->virtualPathForIndex(selectedIndex) ? docModel->virtualPathForIndex(selectedIndex)
: docModel->virtualDirForIndex(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); selectDocument(document, true, false, true);
if (document) if (document)

@ -321,6 +321,27 @@ private:
void updateIndexEnvirons(const QModelIndex &index); 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 class UBDocumentTreeItemDelegate : public QStyledItemDelegate
{ {
Q_OBJECT Q_OBJECT

Loading…
Cancel
Save