Display a warning when the same key is used multiple times
authorWuzzy <Wuzzy2@mail.ru>
Tue, 07 May 2019 18:45:14 +0200
changeset 14920 4d5df8d35a95
parent 14919 4596357d002d
child 14921 505c8d101be3
Display a warning when the same key is used multiple times
ChangeLog.txt
QTfrontend/ui/page/pageeditteam.cpp
QTfrontend/ui/page/pageoptions.cpp
QTfrontend/ui/widget/keybinder.cpp
QTfrontend/ui/widget/keybinder.h
--- a/ChangeLog.txt	Tue May 07 16:08:27 2019 +0200
+++ b/ChangeLog.txt	Tue May 07 18:45:14 2019 +0200
@@ -97,6 +97,7 @@
  + Restructure credits page
  + More intelligent automatic mission selection in campaign screen
  + New data directory for video thumbnails: Data/VideoThumbnails
+ + Display a warning when the same key is used multiple times
  * Fix renaming a video leading to loss of thumbnail after restart
  * Fix controls list failing to display correct key names with regards to keyboard layout
  * Fix force-locked schemes getting unlocked when changing map types
--- a/QTfrontend/ui/page/pageeditteam.cpp	Tue May 07 16:08:27 2019 +0200
+++ b/QTfrontend/ui/page/pageeditteam.cpp	Tue May 07 18:45:14 2019 +0200
@@ -605,6 +605,7 @@
         else
             qDebug() << "Binds: cannot find" << team.keyBind(i);
     }
+    binder->checkConflicts();
 }
 
 HWTeam PageEditTeam::data()
@@ -678,4 +679,5 @@
 {
     for (int i = 0; i < BINDS_NUMBER; i++)
         binder->setBindIndex(i, 0);
+    binder->checkConflicts();
 }
--- a/QTfrontend/ui/page/pageoptions.cpp	Tue May 07 16:08:27 2019 +0200
+++ b/QTfrontend/ui/page/pageoptions.cpp	Tue May 07 18:45:14 2019 +0200
@@ -1218,6 +1218,7 @@
             QModelIndexList mdl = binds->match(binds->index(0, 0), Qt::UserRole + 1, value, 1, Qt::MatchExactly);
             if(mdl.size() == 1) binder->setBindIndex(i, mdl[0].row());
         }
+        binder->checkConflicts();
     }
 
     currentTab = index;
@@ -1258,4 +1259,5 @@
         if(ret != -1)
             bindUpdated(i);
     }
+    binder->checkConflicts();
 }
--- a/QTfrontend/ui/widget/keybinder.cpp	Tue May 07 16:08:27 2019 +0200
+++ b/QTfrontend/ui/widget/keybinder.cpp	Tue May 07 18:45:14 2019 +0200
@@ -38,6 +38,7 @@
 {
     this->defaultText = defaultText;
     enableSignal = false;
+    p_hasConflicts = false;
 
     // Two-column tab layout
     QHBoxLayout * pageKeysLayout = new QHBoxLayout(this);
@@ -90,6 +91,12 @@
     helpLabel->setStyleSheet("color: #130F2A; background: #F6CB1C; border: solid 4px #F6CB1C; border-radius: 10px; padding: auto 20px;");
     helpLabel->setFixedHeight(24);
     rightLayout->addWidget(helpLabel, 0, Qt::AlignCenter);
+    conflictLabel = new QLabel();
+    conflictLabel->setText(tr("Warning: The same key is assigned multiple times!"));
+    conflictLabel->setStyleSheet("color: #FFFFFF; background: #E31A1A; border: solid 4px #E31A1A; border-radius: 10px; padding: auto 20px;");
+    conflictLabel->setFixedHeight(24);
+    conflictLabel->setHidden(true);
+    rightLayout->addWidget(conflictLabel, 0, Qt::AlignCenter);
 
     // Category list and bind table row heights
     const int rowHeight = 20;
@@ -265,6 +272,7 @@
             if (CBBind[i] == sender())
             {
                 emit bindUpdate(i);
+                checkConflicts();
                 break;
             }
         }
@@ -298,6 +306,63 @@
     }
 }
 
+// check if the given key is bound multiple times
+bool KeyBinder::checkConflictsWith(int compareTo, bool updateState)
+{
+    // TODO: Make conflict check more efficient
+    for(int i=0; i<BINDS_NUMBER; i++)
+    {
+        if(i == compareTo)
+            continue;
+        if(CBBind[i] == NULL || CBBind[compareTo] == NULL)
+            continue;
+        QString bind1 = CBBind[i]->currentData(Qt::UserRole + 1).toString();
+        QString bind2 = CBBind[compareTo]->currentData(Qt::UserRole + 1).toString();
+        if(bind1 == "none" || bind2 == "none" || bind1 == "default" || bind2 == "default")
+            continue;
+        // TODO: For team key binds, also check collisions with global key binds
+        if(bind1 == bind2)
+        {
+            if(updateState)
+            {
+                p_hasConflicts = true;
+                conflictLabel->setHidden(false);
+            }
+            return true;
+        }
+    }
+    if(updateState)
+    {
+        p_hasConflicts = false;
+        conflictLabel->setHidden(true);
+    }
+    return false;
+}
+
+// check if any key is bound multiple times and causing a conflict
+bool KeyBinder::checkConflicts()
+{
+    bool conflict = false;
+    for(int i=0; i<BINDS_NUMBER; i++)
+    {
+        conflict = checkConflictsWith(i, false);
+        if(conflict)
+        {
+            p_hasConflicts = true;
+            conflictLabel->setHidden(false);
+            return true;
+        }
+    }
+    p_hasConflicts = false;
+    conflictLabel->setHidden(true);
+    return false;
+}
+
+bool KeyBinder::hasConflicts()
+{
+    return p_hasConflicts;
+}
+
 // Set a combobox's index
 void KeyBinder::setBindIndex(int keyIndex, int bindIndex)
 {
--- a/QTfrontend/ui/widget/keybinder.h	Tue May 07 16:08:27 2019 +0200
+++ b/QTfrontend/ui/widget/keybinder.h	Tue May 07 18:45:14 2019 +0200
@@ -29,6 +29,7 @@
 class QTableWidget;
 class QBoxLayout;
 class QComboBox;
+class QLabel;
 
 // USAGE NOTE: Every time the widget comes into view, you must call resetInterface()
 
@@ -43,6 +44,9 @@
         void setBindIndex(int keyIndex, int bindIndex);
         int bindIndex(int keyIndex);
         void resetInterface();
+        bool hasConflicts();
+        bool checkConflicts();
+        bool checkConflictsWith(int bind, bool updateState);
 
     private:
         QHash<QObject *, QTableWidgetItem *> * bindComboBoxCellMappings;
@@ -51,8 +55,10 @@
         QListWidget * catList;
         QBoxLayout *bindingsPages;
         QComboBox * CBBind[BINDS_NUMBER];
+        QLabel * conflictLabel;
         QString defaultText;
         bool enableSignal;
+        bool p_hasConflicts;
 
     signals:
         void bindUpdate(int bindID);