| 1 | /* |
| 2 | * File: PreferencesWin.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "PreferencesWin.h" |
| 26 | #include "HerculesStudio.h" |
| 27 | #include "Fonts.h" |
| 28 | |
| 29 | #include <QFileDialog> |
| 30 | |
| 31 | #include <iostream> |
| 32 | |
| 33 | PreferencesWin::PreferencesWin( const std::string& currentPath, QWidget *parent) |
| 34 | : QDialog(parent), mCurrentPath(currentPath), mPreferences(&Preferences::getInstance()) |
| 35 | { |
| 36 | ui.setupUi(this); |
| 37 | |
| 38 | #ifdef Q_WS_MAC |
| 39 | ui.AnimateCheckBox->setVisible(false); |
| 40 | #endif |
| 41 | |
| 42 | connect(ui.okButton, SIGNAL(pressed()), this, SLOT(okPressed())); |
| 43 | connect(ui.cancelButton, SIGNAL(pressed()), this, SLOT(cancelPressed())); |
| 44 | connect(ui.hercDirButton, SIGNAL(pressed()), this, SLOT(hercDirPressed())); |
| 45 | connect(ui.configDirButton, SIGNAL(pressed()), this, SLOT(configDirPressed())); |
| 46 | connect(ui.logsDirButton, SIGNAL(pressed()), this, SLOT(logsDirPressed())); |
| 47 | connect(ui.mipsGaugeButton, SIGNAL(toggled(bool)), this, SLOT(mipsToggled())); |
| 48 | |
| 49 | ui.hercDirLineEdit->setText(mPreferences->hercDir().c_str()); |
| 50 | ui.configDirLineEdit->setText(mPreferences->configDir().c_str()); |
| 51 | ui.logsDirLineEdit->setText(mPreferences->logsDir().c_str()); |
| 52 | |
| 53 | QStringList& list = Fonts::getInstance().getList(); |
| 54 | |
| 55 | ui.fontNameLog->addItems(list); |
| 56 | ui.fontNameRegs->addItems(list); |
| 57 | ui.fontNamePsw->addItems(list); |
| 58 | ui.fontNameCommand->addItems(list); |
| 59 | ui.fontNameConfiguration->addItems(list); |
| 60 | |
| 61 | int fontIndex = Fonts::getInstance().indexOf(mPreferences->fontName(Preferences::LogFontObject).c_str()); |
| 62 | if (fontIndex == -1) fontIndex = 0; |
| 63 | ui.fontNameLog->setCurrentIndex(fontIndex); |
| 64 | ui.fontSizeLog->setValue(mPreferences->fontSize(Preferences::LogFontObject)); |
| 65 | ui.boldLog->setChecked(mPreferences->fontIsBold(Preferences::LogFontObject)); |
| 66 | ui.italicLog->setChecked(mPreferences->fontIsItalic(Preferences::LogFontObject)); |
| 67 | |
| 68 | fontIndex = Fonts::getInstance().indexOf(mPreferences->fontName(Preferences::RegsFontObject).c_str()); |
| 69 | if (fontIndex == -1) fontIndex = 0; |
| 70 | ui.fontNameRegs->setCurrentIndex(fontIndex); |
| 71 | ui.fontSizeRegs->setValue(mPreferences->fontSize(Preferences::RegsFontObject)); |
| 72 | ui.boldRegs->setChecked(mPreferences->fontIsBold(Preferences::RegsFontObject)); |
| 73 | ui.italicRegs->setChecked(mPreferences->fontIsItalic(Preferences::RegsFontObject)); |
| 74 | |
| 75 | fontIndex = Fonts::getInstance().indexOf(mPreferences->fontName(Preferences::PswFontObject).c_str()); |
| 76 | if (fontIndex == -1) fontIndex = 0; |
| 77 | ui.fontNamePsw->setCurrentIndex(fontIndex); |
| 78 | ui.fontSizePsw->setValue(mPreferences->fontSize(Preferences::PswFontObject)); |
| 79 | ui.boldPsw->setChecked(mPreferences->fontIsBold(Preferences::PswFontObject)); |
| 80 | ui.italicPsw->setChecked(mPreferences->fontIsItalic(Preferences::PswFontObject)); |
| 81 | |
| 82 | fontIndex = Fonts::getInstance().indexOf(mPreferences->fontName(Preferences::CommandFontObject).c_str()); |
| 83 | if (fontIndex == -1) fontIndex = 0; |
| 84 | ui.fontNameCommand->setCurrentIndex(fontIndex); |
| 85 | ui.fontSizeCommand->setValue(mPreferences->fontSize(Preferences::CommandFontObject)); |
| 86 | ui.boldCommand->setChecked(mPreferences->fontIsBold(Preferences::CommandFontObject)); |
| 87 | ui.italicCommand->setChecked(mPreferences->fontIsItalic(Preferences::CommandFontObject)); |
| 88 | |
| 89 | fontIndex = Fonts::getInstance().indexOf(mPreferences->fontName(Preferences::ConfigurationFontObject).c_str()); |
| 90 | if (fontIndex == -1) fontIndex = 0; |
| 91 | ui.fontNameConfiguration->setCurrentIndex(fontIndex); |
| 92 | ui.fontSizeConfiguration->setValue(mPreferences->fontSize(Preferences::ConfigurationFontObject)); |
| 93 | ui.boldConfiguration->setChecked(mPreferences->fontIsBold(Preferences::ConfigurationFontObject)); |
| 94 | ui.italicConfiguration->setChecked(mPreferences->fontIsItalic(Preferences::ConfigurationFontObject)); |
| 95 | |
| 96 | ui.logTimestampCheckbox->setChecked(mPreferences->logTimestamp()); |
| 97 | |
| 98 | if (mPreferences->mipsAsGauge()) |
| 99 | ui.mipsGaugeButton->setChecked(true); |
| 100 | else |
| 101 | ui.mipsLedButton->setChecked(true); |
| 102 | |
| 103 | if (mPreferences->pswMode() == Psw::Docked) |
| 104 | ui.pswDock->setChecked(true); |
| 105 | else |
| 106 | ui.pswDock->setChecked(false); |
| 107 | |
| 108 | ui.splitLogCheckbox->setChecked(mPreferences->splitLog()); |
| 109 | ui.autosaveLogCheckbox->setChecked(mPreferences->autosaveLog()); |
| 110 | |
| 111 | if (mPreferences->theme() == Preferences::Classic) |
| 112 | ui.classicRadioButton->setChecked(true); |
| 113 | else |
| 114 | ui.modernRadioButton->setChecked(true); |
| 115 | |
| 116 | if (mPreferences->logFileLines() > 0) |
| 117 | ui.memLines->setValue( mPreferences->logFileLines() ); |
| 118 | else |
| 119 | ui.memLines->setValue(5000); |
| 120 | |
| 121 | if (mPreferences->greenLed()) |
| 122 | ui.GreenCheckBox->setChecked(true); |
| 123 | else |
| 124 | ui.GreenCheckBox->setChecked(false); |
| 125 | |
| 126 | if (mPreferences->animate()) |
| 127 | ui.AnimateCheckBox->setChecked(true); |
| 128 | else |
| 129 | ui.AnimateCheckBox->setChecked(false); |
| 130 | |
| 131 | if (mPreferences->darkBackground()) |
| 132 | ui.ThemeComboBox->setCurrentIndex(1); |
| 133 | else |
| 134 | ui.ThemeComboBox->setCurrentIndex(0); |
| 135 | } |
| 136 | |
| 137 | PreferencesWin::~PreferencesWin() |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | void PreferencesWin::okPressed() |
| 142 | { |
| 143 | mPreferences->setHercDir(ui.hercDirLineEdit->text().toStdString()); |
| 144 | mPreferences->setConfigDir(ui.configDirLineEdit->text().toStdString()); |
| 145 | mPreferences->setLogsDir(ui.logsDirLineEdit->text().toStdString()); |
| 146 | |
| 147 | |
| 148 | mPreferences->setFontName(Preferences::LogFontObject, ui.fontNameLog->currentText().toStdString()); |
| 149 | mPreferences->setFontSize(Preferences::LogFontObject, ui.fontSizeLog->value()); |
| 150 | mPreferences->setBold(Preferences::LogFontObject, ui.boldLog->isChecked()); |
| 151 | mPreferences->setItalic(Preferences::LogFontObject, ui.italicLog->isChecked()); |
| 152 | |
| 153 | mPreferences->setFontName(Preferences::RegsFontObject, ui.fontNameRegs->currentText().toStdString()); |
| 154 | mPreferences->setFontSize(Preferences::RegsFontObject, ui.fontSizeRegs->value()); |
| 155 | mPreferences->setBold(Preferences::RegsFontObject, ui.boldRegs->isChecked()); |
| 156 | mPreferences->setItalic(Preferences::RegsFontObject, ui.italicRegs->isChecked()); |
| 157 | |
| 158 | mPreferences->setFontName(Preferences::PswFontObject, ui.fontNamePsw->currentText().toStdString()); |
| 159 | mPreferences->setFontSize(Preferences::PswFontObject, ui.fontSizePsw->value()); |
| 160 | mPreferences->setBold(Preferences::PswFontObject, ui.boldPsw->isChecked()); |
| 161 | mPreferences->setItalic(Preferences::PswFontObject, ui.italicPsw->isChecked()); |
| 162 | |
| 163 | mPreferences->setFontName(Preferences::CommandFontObject, ui.fontNameCommand->currentText().toStdString()); |
| 164 | mPreferences->setFontSize(Preferences::CommandFontObject, ui.fontSizeCommand->value()); |
| 165 | mPreferences->setBold(Preferences::CommandFontObject, ui.boldCommand->isChecked()); |
| 166 | mPreferences->setItalic(Preferences::CommandFontObject, ui.italicCommand->isChecked()); |
| 167 | |
| 168 | mPreferences->setFontName(Preferences::ConfigurationFontObject, ui.fontNameConfiguration->currentText().toStdString()); |
| 169 | mPreferences->setFontSize(Preferences::ConfigurationFontObject, ui.fontSizeConfiguration->value()); |
| 170 | mPreferences->setBold(Preferences::ConfigurationFontObject, ui.boldConfiguration->isChecked()); |
| 171 | mPreferences->setItalic(Preferences::ConfigurationFontObject, ui.italicConfiguration->isChecked()); |
| 172 | |
| 173 | mPreferences->setLogTimestamp(ui.logTimestampCheckbox->isChecked()); |
| 174 | |
| 175 | mPreferences->setMipsAsGauge(ui.mipsGaugeButton->isChecked()); |
| 176 | |
| 177 | mPreferences->setPswMode( ui.pswDock->isChecked() ? Psw::Docked : Psw::StatusBar); |
| 178 | |
| 179 | mPreferences->setSplitLog( ui.splitLogCheckbox->isChecked()); |
| 180 | |
| 181 | mPreferences->setAutosaveLog( ui.autosaveLogCheckbox->isChecked()); |
| 182 | |
| 183 | mPreferences->setTheme( ui.classicRadioButton->isChecked() ? Preferences::Classic : Preferences::Modern); |
| 184 | |
| 185 | mPreferences->setLogFileLines( ui.memLines->value() ); |
| 186 | |
| 187 | mPreferences->setGreenLed(ui.GreenCheckBox->isChecked()); |
| 188 | ui.GreenCheckBox->setEnabled(!ui.mipsGaugeButton->isChecked()); |
| 189 | |
| 190 | mPreferences->setAnimate(ui.AnimateCheckBox->isChecked()); |
| 191 | |
| 192 | mPreferences->setDarkBackground(ui.ThemeComboBox->currentIndex() == 1); |
| 193 | |
| 194 | mPreferences->write(); |
| 195 | setVisible(false); // do not hide animation |
| 196 | emit preferencesChanged(); |
| 197 | close(); |
| 198 | } |
| 199 | |
| 200 | void PreferencesWin::cancelPressed() |
| 201 | { |
| 202 | close(); |
| 203 | } |
| 204 | |
| 205 | void PreferencesWin::hercDirPressed() |
| 206 | { |
| 207 | QString s = QFileDialog::getExistingDirectory(this, |
| 208 | tr("Find Hercules directory"), |
| 209 | ui.hercDirLineEdit->text(), |
| 210 | QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 211 | if (s.length() > 0) |
| 212 | ui.hercDirLineEdit->setText(s); |
| 213 | } |
| 214 | void PreferencesWin::configDirPressed() |
| 215 | { |
| 216 | QString s = QFileDialog::getExistingDirectory(this, |
| 217 | tr("Hercules configurations directory"), |
| 218 | ui.configDirLineEdit->text(), |
| 219 | QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 220 | if (s.length() > 0) |
| 221 | ui.configDirLineEdit->setText(s); |
| 222 | } |
| 223 | void PreferencesWin::logsDirPressed() |
| 224 | { |
| 225 | QString s = QFileDialog::getExistingDirectory(this, |
| 226 | tr("Logs directory"), |
| 227 | ui.logsDirLineEdit->text(), |
| 228 | QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 229 | if (s.length() > 0) |
| 230 | ui.logsDirLineEdit->setText(s); |
| 231 | } |
| 232 | |
| 233 | void PreferencesWin::mipsToggled() |
| 234 | { |
| 235 | ui.GreenCheckBox->setEnabled(!ui.mipsGaugeButton->isChecked()); |
| 236 | } |