| 1 | #include "preferences.h" |
| 2 | #include "ui_preferences.h" |
| 3 | #include "defines.h" |
| 4 | #include <QMessageBox> |
| 5 | #include <QFileDialog> |
| 6 | |
| 7 | Preferences::Preferences(QWidget *parent) : |
| 8 | QDialog(parent), |
| 9 | ui(new Ui::Preferences) |
| 10 | { |
| 11 | ui->setupUi(this); |
| 12 | loadSettings(); |
| 13 | |
| 14 | QPushButton * button; |
| 15 | button = ui->buttonBox->button(QDialogButtonBox::Reset); |
| 16 | if (button) |
| 17 | connect(button, &QPushButton::clicked, this, &Preferences::loadSettings); |
| 18 | button = ui->buttonBox->button(QDialogButtonBox::Ok); |
| 19 | if (button) |
| 20 | connect(button, &QPushButton::clicked, this, &Preferences::saveSettings); |
| 21 | |
| 22 | connect( ui->gobanPathButton, &QPushButton::clicked, this, &Preferences::slot_getGobanPath); |
| 23 | connect( ui->tablePathButton, &QPushButton::clicked, this, &Preferences::slot_getTablePath); |
| 24 | |
| 25 | engineTableModel = new EngineTableModel(this); |
| 26 | ui->engineTableView->setModel(engineTableModel); |
| 27 | ui->engineTableView->setColumnWidth ( EngineTableModel::ENGINE_DEFAULT, 60 ); |
| 28 | ui->engineTableView->setColumnWidth ( EngineTableModel::ENGINE_PATH, 300 ); |
| 29 | connect(ui->engineAddButton, &QPushButton::pressed, this, &Preferences::addEngine); |
| 30 | connect(ui->engineRemoveButton, &QPushButton::pressed, this, &Preferences::removeEngine); |
| 31 | } |
| 32 | |
| 33 | Preferences::~Preferences() |
| 34 | { |
| 35 | delete ui; |
| 36 | } |
| 37 | |
| 38 | |
| 39 | EngineTableModel::EngineTableModel(QObject *parent) |
| 40 | : QAbstractTableModel(parent) |
| 41 | { |
| 42 | loadEngines(); |
| 43 | } |
| 44 | |
| 45 | EngineTableModel::~EngineTableModel() |
| 46 | { |
| 47 | saveEngines(); |
| 48 | } |
| 49 | |
| 50 | int EngineTableModel::rowCount(const QModelIndex &) const |
| 51 | { |
| 52 | return engines.size(); |
| 53 | } |
| 54 | |
| 55 | int EngineTableModel::columnCount(const QModelIndex &) const |
| 56 | { |
| 57 | return ENGINE_NUMITEMS; |
| 58 | } |
| 59 | |
| 60 | QModelIndex EngineTableModel::index ( int row, int column, const QModelIndex &) const |
| 61 | { |
| 62 | return createIndex(row,column); |
| 63 | } |
| 64 | |
| 65 | QVariant EngineTableModel::data(const QModelIndex &index, int role) const |
| 66 | { |
| 67 | if(!index.isValid()) |
| 68 | return QVariant(); |
| 69 | |
| 70 | switch (index.column()) |
| 71 | { |
| 72 | case ENGINE_DEFAULT: |
| 73 | if (role == Qt::CheckStateRole) |
| 74 | return (index.row() == selected_engine ? Qt::Checked : Qt::Unchecked); |
| 75 | break; |
| 76 | case ENGINE_PATH: |
| 77 | if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) |
| 78 | return engines[index.row()].path; |
| 79 | break; |
| 80 | case ENGINE_ARGUMENTS: |
| 81 | if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) |
| 82 | return engines[index.row()].arguments; |
| 83 | break; |
| 84 | } |
| 85 | return QVariant(); |
| 86 | } |
| 87 | |
| 88 | QVariant EngineTableModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 89 | { |
| 90 | if(orientation == Qt::Horizontal && role == Qt::DisplayRole) |
| 91 | switch(section) |
| 92 | { |
| 93 | case ENGINE_DEFAULT: |
| 94 | return QVariant(tr("Default")); |
| 95 | case ENGINE_PATH: |
| 96 | return QVariant(tr("Path")); |
| 97 | case ENGINE_ARGUMENTS: |
| 98 | return QVariant(tr("Arguments")); |
| 99 | } |
| 100 | return QVariant(); |
| 101 | } |
| 102 | |
| 103 | Qt::ItemFlags EngineTableModel::flags(const QModelIndex & index) const |
| 104 | { |
| 105 | if (index.column() == ENGINE_DEFAULT) |
| 106 | return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable; |
| 107 | else |
| 108 | return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable; |
| 109 | } |
| 110 | |
| 111 | bool EngineTableModel::setData(const QModelIndex & index, const QVariant & value, int role) |
| 112 | { |
| 113 | int row = index.row(); |
| 114 | int column = index.column(); |
| 115 | if ((row < 0) || (row >= engines.size()) || ((role != Qt::EditRole) && (role != Qt::CheckStateRole))) |
| 116 | return false; |
| 117 | |
| 118 | switch (column) |
| 119 | { |
| 120 | case ENGINE_DEFAULT: |
| 121 | if (role == Qt::CheckStateRole) |
| 122 | { |
| 123 | selected_engine = (value.toBool() ? row : 0); |
| 124 | saveEngines(); |
| 125 | emit dataChanged(QAbstractTableModel::index(0,(int)ENGINE_DEFAULT), |
| 126 | QAbstractTableModel::index(engines.size()-1,(int)ENGINE_DEFAULT)); |
| 127 | return true; |
| 128 | } |
| 129 | else |
| 130 | return false; |
| 131 | case ENGINE_PATH: |
| 132 | engines[row].path = value.toString(); |
| 133 | break; |
| 134 | case ENGINE_ARGUMENTS: |
| 135 | engines[row].arguments = value.toString(); |
| 136 | break; |
| 137 | default: |
| 138 | return false; |
| 139 | } |
| 140 | saveEngines(); |
| 141 | emit dataChanged(index,index); |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | bool EngineTableModel::removeRows(int row, int count, const QModelIndex &) |
| 146 | { |
| 147 | beginRemoveRows(QModelIndex(),row,row+count-1); |
| 148 | for(;count>0;count--) |
| 149 | engines.removeAt(row); |
| 150 | endRemoveRows(); |
| 151 | if (selected_engine >= engines.size()) |
| 152 | selected_engine = 0; |
| 153 | saveEngines(); |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | void EngineTableModel::addEngine(Engine e) |
| 159 | { |
| 160 | int row = engines.size(); |
| 161 | beginInsertRows(QModelIndex(),row,row); |
| 162 | engines.append(e); |
| 163 | saveEngines(); |
| 164 | endInsertRows(); |
| 165 | } |
| 166 | |
| 167 | void EngineTableModel::loadEngines(void) |
| 168 | { |
| 169 | QSettings settings; |
| 170 | int size = settings.beginReadArray("ENGINES"); |
| 171 | for (int i = 0; i < size; ++i) |
| 172 | { |
| 173 | settings.setArrayIndex(i); |
| 174 | engines.append(Engine(settings.value("path").toString(),settings.value("args").toString())); |
| 175 | } |
| 176 | settings.endArray(); |
| 177 | if (engines.isEmpty()) |
| 178 | engines.append(Engine(QString(DEFAULT_ENGINE_PATH)+QString(DEFAULT_ENGINE),QString(DEFAULT_ENGINE_OPTIONS))); |
| 179 | selected_engine = settings.value("DEFAULT_ENGINE").toInt(); |
| 180 | if ((selected_engine < 0) || (selected_engine >= engines.size())) |
| 181 | selected_engine = 0; |
| 182 | } |
| 183 | |
| 184 | void EngineTableModel::saveEngines(void) |
| 185 | { |
| 186 | QSettings settings; |
| 187 | settings.beginWriteArray("ENGINES"); |
| 188 | for (int i = 0; i < engines.size(); ++i) |
| 189 | { |
| 190 | settings.setArrayIndex(i); |
| 191 | settings.setValue("path", engines.at(i).path); |
| 192 | settings.setValue("args", engines.at(i).arguments); |
| 193 | } |
| 194 | settings.endArray(); |
| 195 | settings.setValue("DEFAULT_ENGINE", selected_engine); |
| 196 | } |
| 197 | |
| 198 | void Preferences::addEngine() |
| 199 | { |
| 200 | QString fileName(QFileDialog::getOpenFileName(this, tr("Go engine"), |
| 201 | QString(DEFAULT_ENGINE_PATH)+QString(DEFAULT_ENGINE))); |
| 202 | if (fileName.isEmpty()) |
| 203 | return; |
| 204 | if (fileName.contains(QRegExp(QString(DEFAULT_ENGINE)+QString("$")))) |
| 205 | engineTableModel->addEngine(Engine(fileName,DEFAULT_ENGINE_OPTIONS)); |
| 206 | else |
| 207 | engineTableModel->addEngine(Engine(fileName,"")); |
| 208 | } |
| 209 | |
| 210 | void Preferences::removeEngine() |
| 211 | { |
| 212 | QModelIndex index = ui->engineTableView->selectionModel()->currentIndex(); |
| 213 | if (index.isValid()) |
| 214 | engineTableModel->removeRow(index.row()); |
| 215 | } |
| 216 | |
| 217 | void Preferences::saveSettings() |
| 218 | { |
| 219 | settings.setValue("MAIN_WINDOW_SIZE_X", width()); |
| 220 | settings.setValue("MAIN_WINDOW_SIZE_Y", height()); |
| 221 | settings.setValue("MAIN_WINDOW_POS_X", pos().x()); |
| 222 | settings.setValue("MAIN_WINDOW_POS_Y", pos().y()); |
| 223 | |
| 224 | settings.setValue("LAST_PATH", currentWorkingDir); |
| 225 | // settings.setValue("COMPUTER_PATH", ui->LineEdit_computer->text()); |
| 226 | settings.setValue("SKIN", ui->LineEdit_goban->text()); |
| 227 | settings.setValue("SKIN_TABLE", ui->LineEdit_table->text()); |
| 228 | |
| 229 | settings.setValue("TIMER_INTERVAL", ui->timerComboBox->currentIndex()); |
| 230 | |
| 231 | int i = 0; |
| 232 | if ( ui->radioButtonStones_2D->isChecked()) |
| 233 | i=1; |
| 234 | else if ( ui->radioButtonStones_3D->isChecked()) |
| 235 | i=2; |
| 236 | settings.setValue("STONES_LOOK", i); |
| 237 | |
| 238 | if ( ui->terrCrossRB->isChecked()) |
| 239 | i=0; |
| 240 | else |
| 241 | i=1; |
| 242 | settings.setValue("TERR_STONE_MARK", i); |
| 243 | |
| 244 | settings.setValue("SOUND_MYGAMES", ui->checkBox_myGamesSound->isChecked()); |
| 245 | settings.setValue("SOUND_OTHERGAMES", ui->checkBox_otherGamesSound->isChecked()); |
| 246 | |
| 247 | if ( ui->komarkerCB->isChecked()) |
| 248 | i=1; |
| 249 | else |
| 250 | i=0; |
| 251 | settings.setValue("KOMARKER", i); |
| 252 | if ( ui->numberCurrentMoveCB->isChecked()) |
| 253 | i=1; |
| 254 | else |
| 255 | i=0; |
| 256 | settings.setValue("NUMBER_CURRENT_MOVE", i); |
| 257 | #ifdef UNNECESSARY |
| 258 | if ( ui->warnOnCloseEditedCB->isChecked()) |
| 259 | i=1; |
| 260 | else |
| 261 | i=0; |
| 262 | |
| 263 | settings.setValue("WARNONCLOSEEDITED", i); |
| 264 | if ( ui->warnOnCloseEngineCB->isChecked()) |
| 265 | i=1; |
| 266 | else |
| 267 | i=0; |
| 268 | settings.setValue("WARNONCLOSEENGINE", i); |
| 269 | #endif //UNNECESSARY |
| 270 | if ( ui->simplePlayerNamesCB->isChecked()) |
| 271 | i=1; |
| 272 | else |
| 273 | i=0; |
| 274 | settings.setValue("SIMPLEPLAYERNAMES", i); |
| 275 | if ( ui->observeOutsideCB->isChecked()) |
| 276 | i=1; |
| 277 | else |
| 278 | i=0; |
| 279 | settings.setValue("OBSERVEOUTSIDE", i); |
| 280 | if ( ui->alternateListColorsCB->isChecked()) |
| 281 | i=1; |
| 282 | else |
| 283 | i=0; |
| 284 | settings.setValue("ALTERNATELISTCOLORS", i); |
| 285 | |
| 286 | //settings.setValue("ACCOUNT", ui->connectionWidget->ui->serverComboBox->currentIndex()); |
| 287 | |
| 288 | //server games default values |
| 289 | settings.setValue("DEFAULT_KOMI",ui->komiSpinDefault->value() ); |
| 290 | settings.setValue("DEFAULT_SIZE",ui->boardSizeSpin->value() ); |
| 291 | settings.setValue("DEFAULT_TIME",ui->timeSpin->value() ); |
| 292 | settings.setValue("DEFAULT_BY",ui->BYSpin->value() ); |
| 293 | |
| 294 | settings.setValue("NMATCH_BLACK", ui->checkBox_Nmatch_Black->isChecked()); |
| 295 | settings.setValue("NMATCH_WHITE", ui->checkBox_Nmatch_White->isChecked()); |
| 296 | settings.setValue("NMATCH_NIGIRI",ui->checkBox_Nmatch_Nigiri->isChecked()); |
| 297 | settings.setValue("NMATCH_MAIN_TIME", ui->timeSpin_Nmatch->value()); |
| 298 | settings.setValue("NMATCH_BYO_TIME", ui->BYSpin_Nmatch->value()); |
| 299 | settings.setValue("NMATCH_HANDICAP", ui->HandicapSpin_Nmatch->value()); |
| 300 | |
| 301 | settings.setValue("AUTOSAVE", ui->CheckBox_autoSave->isChecked()); |
| 302 | settings.setValue("AUTOSAVE_PLAYED", ui->CheckBox_autoSave_Played->isChecked()); |
| 303 | |
| 304 | |
| 305 | //server byo yomi warning |
| 306 | settings.setValue("BYO_SOUND_WARNING", ui->ByoSoundWarning->isChecked()); |
| 307 | settings.setValue("BYO_SEC_WARNING",ui->ByoSecWarning->value()); |
| 308 | |
| 309 | //qDebug("password: %s\n", hostlist.at(0)->password().toLatin1().constData()); |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * loads the parameters from the QSettings into the 2 lats tabs |
| 314 | */ |
| 315 | void Preferences::loadSettings() |
| 316 | { |
| 317 | QSettings settings; |
| 318 | QVariant var; |
| 319 | |
| 320 | if((var = settings.value("LAST_PATH")) == QVariant()) |
| 321 | currentWorkingDir = QString(); |
| 322 | else |
| 323 | currentWorkingDir = var.toString(); |
| 324 | |
| 325 | ui->radioButtonStones_real->setChecked(true); |
| 326 | ui->radioButtonStones_2D->setChecked((settings.value("STONES_LOOK")==1)); |
| 327 | ui->radioButtonStones_3D->setChecked((settings.value("STONES_LOOK")==2)); |
| 328 | |
| 329 | ui->checkBox_myGamesSound->setChecked(settings.value("SOUND_MYGAMES", QVariant(true)).toBool()); |
| 330 | ui->checkBox_otherGamesSound->setChecked(settings.value("SOUND_OTHERGAMES", QVariant(true)).toBool()); |
| 331 | |
| 332 | ui->LineEdit_goban->setText(settings.value("SKIN").toString()); |
| 333 | ui->LineEdit_table->setText(settings.value("SKIN_TABLE").toString()); |
| 334 | |
| 335 | ui->timerComboBox->setCurrentIndex(settings.value("TIMER_INTERVAL").toInt()); |
| 336 | ui->komarkerCB->setChecked(settings.value("KOMARKER", QVariant(true)).toBool()); |
| 337 | ui->numberCurrentMoveCB->setChecked(settings.value("NUMBER_CURRENT_MOVE", QVariant(false)).toBool()); |
| 338 | if(settings.value("TERR_STONE_MARK").toBool()) |
| 339 | ui->terrStoneRB->setChecked(true); |
| 340 | else |
| 341 | ui->terrCrossRB->setChecked(true); |
| 342 | |
| 343 | ui->simplePlayerNamesCB->setChecked((settings.value("SIMPLEPLAYERNAMES") == 1)); |
| 344 | ui->observeOutsideCB->setChecked((settings.value("OBSERVEOUTSIDE") == 1)); |
| 345 | ui->alternateListColorsCB->setChecked(settings.value("ALTERNATELISTCOLORS",QVariant(false)).toBool()); |
| 346 | |
| 347 | ui->komiSpinDefault->setValue(settings.value("DEFAULT_KOMI", QVariant(5.5)).toInt()); |
| 348 | ui->boardSizeSpin->setValue(settings.value("DEFAULT_SIZE", QVariant(19)).toInt()); |
| 349 | ui->timeSpin->setValue(settings.value("DEFAULT_TIME").toInt()); |
| 350 | ui->BYSpin->setValue(settings.value("DEFAULT_BY").toInt()); |
| 351 | |
| 352 | ui->checkBox_Nmatch_Black->setChecked(settings.value("NMATCH_BLACK", QVariant(true)).toBool()); |
| 353 | ui->checkBox_Nmatch_White->setChecked(settings.value("NMATCH_WHITE", QVariant(true)).toBool()); |
| 354 | ui->checkBox_Nmatch_Nigiri->setChecked(settings.value("NMATCH_NIGIRI", QVariant(true)).toBool()); |
| 355 | ui->HandicapSpin_Nmatch->setValue(settings.value("NMATCH_HANDICAP", QVariant(8)).toInt()); |
| 356 | ui->timeSpin_Nmatch->setValue(settings.value("NMATCH_MAIN_TIME", QVariant(99)).toInt()); |
| 357 | ui->BYSpin_Nmatch->setValue(settings.value("NMATCH_BYO_TIME", QVariant(60)).toInt()); |
| 358 | |
| 359 | ui->CheckBox_autoSave->setChecked(settings.value("AUTOSAVE").toBool()); |
| 360 | ui->CheckBox_autoSave_Played->setChecked(settings.value("AUTOSAVE_PLAYED").toBool()); |
| 361 | |
| 362 | //server byo yomi warning |
| 363 | ui->ByoSoundWarning->setChecked(settings.value("BYO_SOUND_WARNING").toBool()); |
| 364 | ui->ByoSecWarning->setValue(settings.value("BYO_SEC_WARNING").toInt()); |
| 365 | |
| 366 | preferences.fill(); |
| 367 | } |
| 368 | |
| 369 | void Preferences::slot_getGobanPath() |
| 370 | { |
| 371 | QString fileName(QFileDialog::getOpenFileName(this, tr("Goban picture"), "", |
| 372 | tr("All Files (*)"), new QString(""), QFileDialog::DontUseNativeDialog)); |
| 373 | if (fileName.isEmpty()) |
| 374 | return; |
| 375 | |
| 376 | ui->LineEdit_goban->setText(fileName); |
| 377 | } |
| 378 | |
| 379 | void Preferences::slot_getTablePath() |
| 380 | { |
| 381 | QString fileName(QFileDialog::getOpenFileName(this, tr("Table picture"), "", |
| 382 | tr("All Files (*)"), new QString(""), QFileDialog::DontUseNativeDialog)); |
| 383 | if (fileName.isEmpty()) |
| 384 | return; |
| 385 | |
| 386 | ui->LineEdit_table->setText(fileName); |
| 387 | } |