QTfrontend/ui/page/pagescheme.cpp
changeset 13554 a5a8488e2485
parent 13272 5984e8c6cbeb
child 13612 7b4a56157bc5
equal deleted inserted replaced
13553:e1a3994558e1 13554:a5a8488e2485
   592     mapper->addMapping(SB_GetAwayTime, 41);
   592     mapper->addMapping(SB_GetAwayTime, 41);
   593     mapper->addMapping(CB_WorldEdge, 42, "currentIndex");
   593     mapper->addMapping(CB_WorldEdge, 42, "currentIndex");
   594     mapper->addMapping(LE_ScriptParam, 43);
   594     mapper->addMapping(LE_ScriptParam, 43);
   595 
   595 
   596     mapper->toFirst();
   596     mapper->toFirst();
       
   597 
       
   598     connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(dataChanged(QModelIndex, QModelIndex)));
   597 }
   599 }
   598 
   600 
   599 void PageScheme::newRow()
   601 void PageScheme::newRow()
   600 {
   602 {
       
   603     changingSchemes = true;
   601     QAbstractItemModel * model = mapper->model();
   604     QAbstractItemModel * model = mapper->model();
   602     model->insertRow(-1);
   605     model->insertRow(-1);
   603     selectScheme->setCurrentIndex(model->rowCount() - 1);
   606     selectScheme->setCurrentIndex(model->rowCount() - 1);
       
   607     changingSchemes = false;
       
   608     checkDupe();
   604 }
   609 }
   605 
   610 
   606 void PageScheme::copyRow()
   611 void PageScheme::copyRow()
   607 {
   612 {
       
   613     changingSchemes = true;
   608     QAbstractItemModel * model = mapper->model();
   614     QAbstractItemModel * model = mapper->model();
   609     model->insertRow(selectScheme->currentIndex());
   615     model->insertRow(selectScheme->currentIndex());
   610     selectScheme->setCurrentIndex(model->rowCount() - 1);
   616     selectScheme->setCurrentIndex(model->rowCount() - 1);
       
   617     changingSchemes = false;
       
   618     checkDupe();
   611 }
   619 }
   612 
   620 
   613 void PageScheme::deleteRow()
   621 void PageScheme::deleteRow()
   614 {
   622 {
       
   623     changingSchemes = true;
   615     int numberOfDefaultSchemes = ((GameSchemeModel*)mapper->model())->numberOfDefaultSchemes;
   624     int numberOfDefaultSchemes = ((GameSchemeModel*)mapper->model())->numberOfDefaultSchemes;
   616     if (selectScheme->currentIndex() < numberOfDefaultSchemes)
   625     if (selectScheme->currentIndex() < numberOfDefaultSchemes)
   617     {
   626     {
   618         QMessageBox deniedMsg(this);
   627         QMessageBox deniedMsg(this);
   619         deniedMsg.setIcon(QMessageBox::Warning);
   628         deniedMsg.setIcon(QMessageBox::Warning);
   635         {
   644         {
   636             QAbstractItemModel * model = mapper->model();
   645             QAbstractItemModel * model = mapper->model();
   637             model->removeRow(selectScheme->currentIndex());
   646             model->removeRow(selectScheme->currentIndex());
   638         }
   647         }
   639     }
   648     }
       
   649     changingSchemes = false;
       
   650 }
       
   651 
       
   652 void PageScheme::dataChanged(QModelIndex topLeft, QModelIndex bottomRight)
       
   653 {
       
   654     Q_UNUSED(bottomRight)
       
   655     if(topLeft.column() == 0) {
       
   656         checkDupe();
       
   657     };
   640 }
   658 }
   641 
   659 
   642 void PageScheme::schemeSelected(int n)
   660 void PageScheme::schemeSelected(int n)
   643 {
   661 {
   644     int c = ((GameSchemeModel*)mapper->model())->numberOfDefaultSchemes;
   662     int c = ((GameSchemeModel*)mapper->model())->numberOfDefaultSchemes;
   645     gbGameModes->setEnabled(n >= c);
   663     gbGameModes->setEnabled(n >= c);
   646     gbBasicSettings->setEnabled(n >= c);
   664     gbBasicSettings->setEnabled(n >= c);
   647     LE_name->setEnabled(n >= c);
   665     LE_name->setEnabled(n >= c);
   648     L_name->setEnabled(n >= c);
   666     L_name->setEnabled(n >= c);
   649 }
   667     checkDupe();
   650 
   668 }
   651 
   669 
       
   670 // Check for duplicates and rename scheme if duplicate found
       
   671 void PageScheme::checkDupe()
       
   672 {
       
   673     if (changingSchemes)
       
   674     {
       
   675         return;
       
   676     }
       
   677     int except = selectScheme->currentIndex();
       
   678     QString name = selectScheme->currentText();
       
   679     GameSchemeModel* model = (GameSchemeModel*)mapper->model();
       
   680     bool dupe = model->hasScheme(name, except);
       
   681     if (dupe)
       
   682     {
       
   683         QString newName;
       
   684         //name already used -> look for an appropriate name
       
   685         int i=2;
       
   686         while(model->hasScheme(newName = tr("%1 (%2)").arg(name).arg(i++), except))
       
   687         {
       
   688             if(i > 1000)
       
   689             {
       
   690                 return;
       
   691             }
       
   692         }
       
   693         LE_name->setText(newName);
       
   694         selectScheme->setCurrentText(newName);
       
   695         model->renameScheme(except, newName);
       
   696 
       
   697         QMessageBox dupeMsg(this);
       
   698         dupeMsg.setIcon(QMessageBox::Warning);
       
   699         dupeMsg.setWindowTitle(QMessageBox::tr("Schemes - Name already taken"));
       
   700         dupeMsg.setText(QMessageBox::tr("A scheme with the name '%1' already exists. Your scheme has been renamed to '%2'.").arg(name).arg(newName));
       
   701         dupeMsg.setWindowModality(Qt::WindowModal);
       
   702         dupeMsg.exec();
       
   703     }
       
   704 }
       
   705