Prevent saving game schemes if name was already been taken
authorWuzzy <Wuzzy2@mail.ru>
Thu, 26 Jul 2018 00:18:59 +0200
changeset 13554 a5a8488e2485
parent 13553 e1a3994558e1
child 13555 43b72629d453
Prevent saving game schemes if name was already been taken
QTfrontend/model/gameSchemeModel.cpp
QTfrontend/model/gameSchemeModel.h
QTfrontend/ui/page/pagescheme.cpp
QTfrontend/ui/page/pagescheme.h
--- a/QTfrontend/model/gameSchemeModel.cpp	Tue Jul 24 20:46:22 2018 +0200
+++ b/QTfrontend/model/gameSchemeModel.cpp	Thu Jul 26 00:18:59 2018 +0200
@@ -856,9 +856,15 @@
 
 bool GameSchemeModel::hasScheme(QString name)
 {
+    return hasScheme(name, -1);
+}
+
+bool GameSchemeModel::hasScheme(QString name, int ignoreID)
+{
+    QString nameLower = name.toLower();
     for(int i=0; i<schemes.size(); i++)
     {
-        if(schemes[i][0] == name)
+        if(((ignoreID == -1) || (i != ignoreID)) && (schemes[i][0].toString().toLower() == nameLower))
         {
             return true;
         }
@@ -866,6 +872,11 @@
     return false;
 }
 
+bool GameSchemeModel::renameScheme(int index, QString newName)
+{
+    return setData(QAbstractItemModel::createIndex(index, 0), QVariant(newName));
+}
+
 Qt::ItemFlags GameSchemeModel::flags(const QModelIndex & index) const
 {
     Q_UNUSED(index);
--- a/QTfrontend/model/gameSchemeModel.h	Tue Jul 24 20:46:22 2018 +0200
+++ b/QTfrontend/model/gameSchemeModel.h	Thu Jul 26 00:18:59 2018 +0200
@@ -34,6 +34,8 @@
         int rowCount(const QModelIndex & parent) const;
         int columnCount(const QModelIndex & parent) const;
         bool hasScheme(QString name);
+        bool hasScheme(QString name, int ignoreID);
+        bool renameScheme(int index, QString newName);
         Qt::ItemFlags flags(const QModelIndex & index) const;
         bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
         bool insertRows(int row, int count, const QModelIndex & parent = QModelIndex());
@@ -47,6 +49,9 @@
     public slots:
         void Save();
 
+    signals:
+        void dataChanged(const QModelIndex &topLeft, const QModelIndex& bottomRight);
+
     protected:
         QList< QList<QVariant> > schemes;
 };
--- a/QTfrontend/ui/page/pagescheme.cpp	Tue Jul 24 20:46:22 2018 +0200
+++ b/QTfrontend/ui/page/pagescheme.cpp	Thu Jul 26 00:18:59 2018 +0200
@@ -594,24 +594,33 @@
     mapper->addMapping(LE_ScriptParam, 43);
 
     mapper->toFirst();
+
+    connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(dataChanged(QModelIndex, QModelIndex)));
 }
 
 void PageScheme::newRow()
 {
+    changingSchemes = true;
     QAbstractItemModel * model = mapper->model();
     model->insertRow(-1);
     selectScheme->setCurrentIndex(model->rowCount() - 1);
+    changingSchemes = false;
+    checkDupe();
 }
 
 void PageScheme::copyRow()
 {
+    changingSchemes = true;
     QAbstractItemModel * model = mapper->model();
     model->insertRow(selectScheme->currentIndex());
     selectScheme->setCurrentIndex(model->rowCount() - 1);
+    changingSchemes = false;
+    checkDupe();
 }
 
 void PageScheme::deleteRow()
 {
+    changingSchemes = true;
     int numberOfDefaultSchemes = ((GameSchemeModel*)mapper->model())->numberOfDefaultSchemes;
     if (selectScheme->currentIndex() < numberOfDefaultSchemes)
     {
@@ -637,6 +646,15 @@
             model->removeRow(selectScheme->currentIndex());
         }
     }
+    changingSchemes = false;
+}
+
+void PageScheme::dataChanged(QModelIndex topLeft, QModelIndex bottomRight)
+{
+    Q_UNUSED(bottomRight)
+    if(topLeft.column() == 0) {
+        checkDupe();
+    };
 }
 
 void PageScheme::schemeSelected(int n)
@@ -646,6 +664,42 @@
     gbBasicSettings->setEnabled(n >= c);
     LE_name->setEnabled(n >= c);
     L_name->setEnabled(n >= c);
+    checkDupe();
 }
 
+// Check for duplicates and rename scheme if duplicate found
+void PageScheme::checkDupe()
+{
+    if (changingSchemes)
+    {
+        return;
+    }
+    int except = selectScheme->currentIndex();
+    QString name = selectScheme->currentText();
+    GameSchemeModel* model = (GameSchemeModel*)mapper->model();
+    bool dupe = model->hasScheme(name, except);
+    if (dupe)
+    {
+        QString newName;
+        //name already used -> look for an appropriate name
+        int i=2;
+        while(model->hasScheme(newName = tr("%1 (%2)").arg(name).arg(i++), except))
+        {
+            if(i > 1000)
+            {
+                return;
+            }
+        }
+        LE_name->setText(newName);
+        selectScheme->setCurrentText(newName);
+        model->renameScheme(except, newName);
 
+        QMessageBox dupeMsg(this);
+        dupeMsg.setIcon(QMessageBox::Warning);
+        dupeMsg.setWindowTitle(QMessageBox::tr("Schemes - Name already taken"));
+        dupeMsg.setText(QMessageBox::tr("A scheme with the name '%1' already exists. Your scheme has been renamed to '%2'.").arg(name).arg(newName));
+        dupeMsg.setWindowModality(Qt::WindowModal);
+        dupeMsg.exec();
+    }
+}
+
--- a/QTfrontend/ui/page/pagescheme.h	Tue Jul 24 20:46:22 2018 +0200
+++ b/QTfrontend/ui/page/pagescheme.h	Thu Jul 26 00:18:59 2018 +0200
@@ -101,8 +101,13 @@
         QGroupBox * gbGameModes;
         QGroupBox * gbBasicSettings;
 
+        bool changingSchemes = false;
+
+        void checkDupe();
+
     private slots:
         void schemeSelected(int);
+        void dataChanged(QModelIndex topLeft, QModelIndex bottomRight);
 };
 
 #endif