QTfrontend/ui/widget/keybinder.cpp
changeset 14899 4d5df8d35a95
parent 14846 75b515a64202
child 14900 505c8d101be3
equal deleted inserted replaced
14898:4596357d002d 14899:4d5df8d35a95
    36 
    36 
    37 KeyBinder::KeyBinder(QWidget * parent, const QString & helpText, const QString & defaultText, const QString & resetButtonText) : QWidget(parent)
    37 KeyBinder::KeyBinder(QWidget * parent, const QString & helpText, const QString & defaultText, const QString & resetButtonText) : QWidget(parent)
    38 {
    38 {
    39     this->defaultText = defaultText;
    39     this->defaultText = defaultText;
    40     enableSignal = false;
    40     enableSignal = false;
       
    41     p_hasConflicts = false;
    41 
    42 
    42     // Two-column tab layout
    43     // Two-column tab layout
    43     QHBoxLayout * pageKeysLayout = new QHBoxLayout(this);
    44     QHBoxLayout * pageKeysLayout = new QHBoxLayout(this);
    44     pageKeysLayout->setSpacing(0);
    45     pageKeysLayout->setSpacing(0);
    45     pageKeysLayout->setContentsMargins(0, 0, 0, 0);
    46     pageKeysLayout->setContentsMargins(0, 0, 0, 0);
    88     QLabel * helpLabel = new QLabel();
    89     QLabel * helpLabel = new QLabel();
    89     helpLabel->setText(helpText);
    90     helpLabel->setText(helpText);
    90     helpLabel->setStyleSheet("color: #130F2A; background: #F6CB1C; border: solid 4px #F6CB1C; border-radius: 10px; padding: auto 20px;");
    91     helpLabel->setStyleSheet("color: #130F2A; background: #F6CB1C; border: solid 4px #F6CB1C; border-radius: 10px; padding: auto 20px;");
    91     helpLabel->setFixedHeight(24);
    92     helpLabel->setFixedHeight(24);
    92     rightLayout->addWidget(helpLabel, 0, Qt::AlignCenter);
    93     rightLayout->addWidget(helpLabel, 0, Qt::AlignCenter);
       
    94     conflictLabel = new QLabel();
       
    95     conflictLabel->setText(tr("Warning: The same key is assigned multiple times!"));
       
    96     conflictLabel->setStyleSheet("color: #FFFFFF; background: #E31A1A; border: solid 4px #E31A1A; border-radius: 10px; padding: auto 20px;");
       
    97     conflictLabel->setFixedHeight(24);
       
    98     conflictLabel->setHidden(true);
       
    99     rightLayout->addWidget(conflictLabel, 0, Qt::AlignCenter);
    93 
   100 
    94     // Category list and bind table row heights
   101     // Category list and bind table row heights
    95     const int rowHeight = 20;
   102     const int rowHeight = 20;
    96     QSize catSize, headerSize;
   103     QSize catSize, headerSize;
    97     catSize.setHeight(36);
   104     catSize.setHeight(36);
   263         for (int i = 0; i < BINDS_NUMBER; i++)
   270         for (int i = 0; i < BINDS_NUMBER; i++)
   264         {
   271         {
   265             if (CBBind[i] == sender())
   272             if (CBBind[i] == sender())
   266             {
   273             {
   267                 emit bindUpdate(i);
   274                 emit bindUpdate(i);
       
   275                 checkConflicts();
   268                 break;
   276                 break;
   269             }
   277             }
   270         }
   278         }
   271     }
   279     }
   272 }
   280 }
   296             selectedBindTable->clearSelection();
   304             selectedBindTable->clearSelection();
   297         selectedBindTable = theSender;
   305         selectedBindTable = theSender;
   298     }
   306     }
   299 }
   307 }
   300 
   308 
       
   309 // check if the given key is bound multiple times
       
   310 bool KeyBinder::checkConflictsWith(int compareTo, bool updateState)
       
   311 {
       
   312     // TODO: Make conflict check more efficient
       
   313     for(int i=0; i<BINDS_NUMBER; i++)
       
   314     {
       
   315         if(i == compareTo)
       
   316             continue;
       
   317         if(CBBind[i] == NULL || CBBind[compareTo] == NULL)
       
   318             continue;
       
   319         QString bind1 = CBBind[i]->currentData(Qt::UserRole + 1).toString();
       
   320         QString bind2 = CBBind[compareTo]->currentData(Qt::UserRole + 1).toString();
       
   321         if(bind1 == "none" || bind2 == "none" || bind1 == "default" || bind2 == "default")
       
   322             continue;
       
   323         // TODO: For team key binds, also check collisions with global key binds
       
   324         if(bind1 == bind2)
       
   325         {
       
   326             if(updateState)
       
   327             {
       
   328                 p_hasConflicts = true;
       
   329                 conflictLabel->setHidden(false);
       
   330             }
       
   331             return true;
       
   332         }
       
   333     }
       
   334     if(updateState)
       
   335     {
       
   336         p_hasConflicts = false;
       
   337         conflictLabel->setHidden(true);
       
   338     }
       
   339     return false;
       
   340 }
       
   341 
       
   342 // check if any key is bound multiple times and causing a conflict
       
   343 bool KeyBinder::checkConflicts()
       
   344 {
       
   345     bool conflict = false;
       
   346     for(int i=0; i<BINDS_NUMBER; i++)
       
   347     {
       
   348         conflict = checkConflictsWith(i, false);
       
   349         if(conflict)
       
   350         {
       
   351             p_hasConflicts = true;
       
   352             conflictLabel->setHidden(false);
       
   353             return true;
       
   354         }
       
   355     }
       
   356     p_hasConflicts = false;
       
   357     conflictLabel->setHidden(true);
       
   358     return false;
       
   359 }
       
   360 
       
   361 bool KeyBinder::hasConflicts()
       
   362 {
       
   363     return p_hasConflicts;
       
   364 }
       
   365 
   301 // Set a combobox's index
   366 // Set a combobox's index
   302 void KeyBinder::setBindIndex(int keyIndex, int bindIndex)
   367 void KeyBinder::setBindIndex(int keyIndex, int bindIndex)
   303 {
   368 {
   304     enableSignal = false;
   369     enableSignal = false;
   305     if(CBBind[keyIndex] != NULL)
   370     if(CBBind[keyIndex] != NULL)