From e1c8d00979793dd00a90d312ccdd275cf48dc4ad Mon Sep 17 00:00:00 2001 From: Tom Tanner Date: Sat, 18 Jul 2015 13:34:10 +0100 Subject: Reworked executable lists to not use 3 bools in addExecutable Think it's more readable --- src/editexecutablesdialog.cpp | 15 ++++++----- src/executableslist.cpp | 61 +++++++++---------------------------------- src/executableslist.h | 49 ++++++++++++++++++++++++---------- src/mainwindow.cpp | 25 +++++++++++------- src/organizercore.cpp | 20 +++++++++----- 5 files changed, 83 insertions(+), 87 deletions(-) diff --git a/src/editexecutablesdialog.cpp b/src/editexecutablesdialog.cpp index 4ae0b351..97154789 100644 --- a/src/editexecutablesdialog.cpp +++ b/src/editexecutablesdialog.cpp @@ -60,7 +60,7 @@ void EditExecutablesDialog::refreshExecutablesWidget() for(; current != end; ++current) { QListWidgetItem *newItem = new QListWidgetItem(current->m_Title); - newItem->setTextColor(current->m_Custom ? QColor(Qt::black) : QColor(Qt::darkGray)); + newItem->setTextColor(current->m_Type == Executable::Type::Custom ? QColor(Qt::black) : QColor(Qt::darkGray)); ui->executablesListBox->addItem(newItem); } @@ -100,9 +100,10 @@ void EditExecutablesDialog::saveExecutable() : ExecutableInfo::CloseMOStyle::DEFAULT_STAY, ui->overwriteAppIDBox->isChecked() ? ui->appIDOverwriteEdit->text() : "", - true, - false, - ui->useAppIconCheckBox->isChecked()); + Executable::Type::Custom, + Executable::Toolbar::Disabled, + ui->useAppIconCheckBox->isChecked() ? + Executable::Icon::Application : Executable::Icon::MO); } @@ -215,7 +216,7 @@ bool EditExecutablesDialog::executableChanged() || selectedExecutable.m_WorkingDirectory != QDir::fromNativeSeparators(ui->workingDirEdit->text()) || selectedExecutable.m_BinaryInfo.absoluteFilePath() != QDir::fromNativeSeparators(ui->binaryEdit->text()) || (selectedExecutable.m_CloseMO == ExecutableInfo::CloseMOStyle::DEFAULT_CLOSE) != ui->closeCheckBox->isChecked() - || selectedExecutable.m_UseOwnIcon != ui->useAppIconCheckBox->isChecked(); + || selectedExecutable.usesOwnIcon() != ui->useAppIconCheckBox->isChecked(); } else { return false; } @@ -289,13 +290,13 @@ void EditExecutablesDialog::on_executablesListBox_clicked(const QModelIndex &cur ui->closeCheckBox->setEnabled(true); ui->closeCheckBox->setToolTip(tr("If checked, MO will be closed once the specified executable is run.")); } - ui->removeButton->setEnabled(selectedExecutable.m_Custom); + ui->removeButton->setEnabled(selectedExecutable.isCustom()); ui->overwriteAppIDBox->setChecked(!selectedExecutable.m_SteamAppID.isEmpty()); if (!selectedExecutable.m_SteamAppID.isEmpty()) { ui->appIDOverwriteEdit->setText(selectedExecutable.m_SteamAppID); } else { ui->appIDOverwriteEdit->clear(); } - ui->useAppIconCheckBox->setChecked(selectedExecutable.m_UseOwnIcon); + ui->useAppIconCheckBox->setChecked(selectedExecutable.usesOwnIcon()); } } diff --git a/src/executableslist.cpp b/src/executableslist.cpp index 1f5fee51..ba2b5616 100644 --- a/src/executableslist.cpp +++ b/src/executableslist.cpp @@ -30,31 +30,6 @@ using namespace MOBase; using namespace MOShared; -static QDataStream &operator<<(QDataStream &out, const Executable &obj) -{ - out << obj.m_Title - << obj.m_BinaryInfo.absoluteFilePath() - << obj.m_Arguments - << static_cast::type>(obj.m_CloseMO) - << obj.m_SteamAppID - << obj.m_WorkingDirectory - << obj.m_Custom - << obj.m_Toolbar << obj.m_UseOwnIcon; - return out; -} - -static QDataStream &operator>>(QDataStream &in, Executable &obj) -{ - QString binaryTemp; - int closeStyleTemp; - in >> obj.m_Title >> binaryTemp >> obj.m_Arguments >> closeStyleTemp - >> obj.m_SteamAppID >> obj.m_WorkingDirectory >> obj.m_Custom >> obj.m_Toolbar >> obj.m_UseOwnIcon; - - obj.m_CloseMO = static_cast(closeStyleTemp); - obj.m_BinaryInfo.setFile(binaryTemp); - return in; -} - ExecutablesList::ExecutablesList() { } @@ -154,43 +129,31 @@ void ExecutablesList::addExecutable(const Executable &executable) } -void ExecutablesList::position(const QString &title, bool toolbar, int pos) -{ - auto existingExe = findExe(title); - if (existingExe != m_Executables.end()) { - Executable temp = *existingExe; - temp.m_Toolbar = toolbar; - m_Executables.erase(existingExe); - m_Executables.insert(m_Executables.begin() + pos, temp); - } -} - - void ExecutablesList::addExecutable(const QString &title, const QString &executableName, const QString &arguments, const QString &workingDirectory, ExecutableInfo::CloseMOStyle closeMO, const QString &steamAppID, - bool custom, - bool toolbar, - bool ownicon) + Executable::Type custom, + Executable::Toolbar toolbar, + Executable::Icon ownicon) { QFileInfo file(executableName); auto existingExe = findExe(title); if (existingExe != m_Executables.end()) { existingExe->m_Title = title; - existingExe->m_Custom = custom; + existingExe->m_Type = custom; existingExe->m_CloseMO = closeMO; existingExe->m_Toolbar = toolbar; - if (custom) { + if (custom == Executable::Type::Custom) { // for pre-configured executables don't overwrite settings we didn't store existingExe->m_BinaryInfo = file; existingExe->m_Arguments = arguments; existingExe->m_WorkingDirectory = workingDirectory; existingExe->m_SteamAppID = steamAppID; - existingExe->m_UseOwnIcon = ownicon; + existingExe->m_Icon = ownicon; } } else { Executable newExe; @@ -200,9 +163,9 @@ void ExecutablesList::addExecutable(const QString &title, newExe.m_Arguments = arguments; newExe.m_WorkingDirectory = workingDirectory; newExe.m_SteamAppID = steamAppID; - newExe.m_Custom = true; + newExe.m_Type = Executable::Type::Custom; newExe.m_Toolbar = toolbar; - newExe.m_UseOwnIcon = ownicon; + newExe.m_Icon = ownicon; m_Executables.push_back(newExe); } } @@ -211,7 +174,7 @@ void ExecutablesList::addExecutable(const QString &title, void ExecutablesList::remove(const QString &title) { for (std::vector::iterator iter = m_Executables.begin(); iter != m_Executables.end(); ++iter) { - if (iter->m_Custom && iter->m_Title == title) { + if (iter->isCustom() && iter->m_Title == title) { m_Executables.erase(iter); break; } @@ -232,9 +195,9 @@ void ExecutablesList::addExecutableInternal(const QString &title, const QString newExe.m_Arguments = arguments; newExe.m_WorkingDirectory = workingDirectory; newExe.m_SteamAppID = steamAppID; - newExe.m_Custom = false; - newExe.m_Toolbar = false; - newExe.m_UseOwnIcon = false; + newExe.m_Type = Executable::Type::Game; + newExe.m_Toolbar = Executable::Toolbar::Disabled; + newExe.m_Icon = Executable::Icon::MO; m_Executables.push_back(newExe); } } diff --git a/src/executableslist.h b/src/executableslist.h index 8eda38f5..4e1ad644 100644 --- a/src/executableslist.h +++ b/src/executableslist.h @@ -39,9 +39,32 @@ struct Executable { QString m_SteamAppID; QString m_WorkingDirectory; - bool m_Custom; - bool m_Toolbar; - bool m_UseOwnIcon; + enum class Type { + Game, + Custom + }; + + enum class Toolbar { + Disabled, + Enabled + }; + + enum class Icon { + MO, + Application + }; + + Type m_Type; + Toolbar m_Toolbar; + Icon m_Icon; + + bool isCustom() const { return m_Type == Type::Custom; } + + bool shownOnToolbar() const { return m_Toolbar == Toolbar::Enabled; } + + void showOnToolbar(bool state) { m_Toolbar = state ? Toolbar::Enabled : Toolbar::Disabled; } + + bool usesOwnIcon() const { return m_Icon == Icon::Application; } }; @@ -120,17 +143,15 @@ public: * @param arguments arguments to pass to the executable * @param closeMO if true, MO will be closed when the binary is started **/ - void addExecutable(const QString &title, const QString &executableName, const QString &arguments, - const QString &workingDirectory, MOBase::ExecutableInfo::CloseMOStyle closeMO, const QString &steamAppID, - bool custom, bool toolbar, bool ownicon); - - /** - * @brief change position of an executable which is expected to exist - * @param title title of the executable - * @param toolbar enable/disable placement on the toolbar - * @param pos new position for the executable - */ - void position(const QString &title, bool toolbar, int pos); + void addExecutable(const QString &title, + const QString &executableName, + const QString &arguments, + const QString &workingDirectory, + MOBase::ExecutableInfo::CloseMOStyle closeMO, + const QString &steamAppID, + Executable::Type custom, + Executable::Toolbar toolbar, + Executable::Icon ownicon); /** * @brief remove the executable with the specified file name. This needs to be an absolute file path diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index bd880dac..2e7797d2 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -486,7 +486,7 @@ void MainWindow::updateToolBar() std::vector::iterator begin, end; m_OrganizerCore.executablesList()->getExecutables(begin, end); for (auto iter = begin; iter != end; ++iter) { - if (iter->m_Toolbar) { + if (iter->shownOnToolbar()) { QAction *exeAction = new QAction(iconForExecutable(iter->m_BinaryInfo.filePath()), iter->m_Title, ui->toolBar); @@ -3256,8 +3256,8 @@ void MainWindow::on_savegameList_customContextMenuRequested(const QPoint &pos) void MainWindow::linkToolbar() { Executable &exe(getSelectedExecutable()); - exe.m_Toolbar = !exe.m_Toolbar; - ui->linkButton->menu()->actions().at(static_cast(Shortcut_Type::Toolbar))->setIcon(exe.m_Toolbar ? QIcon(":/MO/gui/remove") : QIcon(":/MO/gui/link")); + exe.showOnToolbar(!exe.shownOnToolbar()); + ui->linkButton->menu()->actions().at(static_cast(Shortcut_Type::Toolbar))->setIcon(exe.shownOnToolbar() ? QIcon(":/MO/gui/remove") : QIcon(":/MO/gui/link")); updateToolBar(); } @@ -3306,7 +3306,7 @@ void MainWindow::addWindowsLink(Shortcut_Type const mapping) , parameter.c_str() , QDir::toNativeSeparators(linkName).toUtf8().constData() , description.c_str() - , (selectedExecutable.m_UseOwnIcon ? iconFile.c_str() : nullptr), 0 + , (selectedExecutable.usesOwnIcon() ? iconFile.c_str() : nullptr), 0 , currentDirectory.c_str()) == 0) { ui->linkButton->menu()->actions().at(static_cast(mapping))->setIcon(QIcon(":/MO/gui/remove")); } else { @@ -3541,10 +3541,15 @@ void MainWindow::addAsExecutable() tr("Please enter a name for the executable"), QLineEdit::Normal, targetInfo.baseName()); if (!name.isEmpty()) { - m_OrganizerCore.executablesList()->addExecutable(name, binaryInfo.absoluteFilePath(), - arguments, targetInfo.absolutePath(), - ExecutableInfo::CloseMOStyle::DEFAULT_STAY, QString(), - true, false, false); + m_OrganizerCore.executablesList()->addExecutable(name, + binaryInfo.absoluteFilePath(), + arguments, + targetInfo.absolutePath(), + ExecutableInfo::CloseMOStyle::DEFAULT_STAY, + QString(), + Executable::Type::Custom, + Executable::Toolbar::Disabled, + Executable::Icon::MO); refreshExecutablesList(); } } break; @@ -4121,7 +4126,7 @@ void MainWindow::removeFromToolbar() { try { Executable &exe = m_OrganizerCore.executablesList()->find(m_ContextAction->text()); - exe.m_Toolbar = false; + exe.showOnToolbar(false); } catch (const std::runtime_error&) { qDebug("executable doesn't exist any more"); } @@ -4240,7 +4245,7 @@ void MainWindow::on_linkButton_pressed() QFileInfo const linkDesktopFile(getDesktopLinkfile(selectedExecutable)); QFileInfo const linkMenuFile(getStartMenuLinkfile(selectedExecutable)); - ui->linkButton->menu()->actions().at(static_cast(Shortcut_Type::Toolbar))->setIcon(selectedExecutable.m_Toolbar ? removeIcon : addIcon); + ui->linkButton->menu()->actions().at(static_cast(Shortcut_Type::Toolbar))->setIcon(selectedExecutable.shownOnToolbar() ? removeIcon : addIcon); ui->linkButton->menu()->actions().at(static_cast(Shortcut_Type::Windows_Desktop))->setIcon(linkDesktopFile.exists() ? removeIcon : addIcon); ui->linkButton->menu()->actions().at(static_cast(Shortcut_Type::Windows_StartMenu))->setIcon(linkMenuFile.exists() ? removeIcon : addIcon); } diff --git a/src/organizercore.cpp b/src/organizercore.cpp index 01141dd7..589a1595 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -222,15 +222,15 @@ QSettings::Status OrganizerCore::storeSettings(const QString &fileName) const Executable &item = *current; settings.setArrayIndex(count++); settings.setValue("title", item.m_Title); - settings.setValue("custom", item.m_Custom); - settings.setValue("toolbar", item.m_Toolbar); - if (item.m_Custom) { + settings.setValue("custom", item.isCustom()); + settings.setValue("toolbar", item.shownOnToolbar()); + settings.setValue("ownicon", item.usesOwnIcon()); + if (item.isCustom()) { settings.setValue("binary", item.m_BinaryInfo.absoluteFilePath()); settings.setValue("arguments", item.m_Arguments); settings.setValue("workingDirectory", item.m_WorkingDirectory); settings.setValue("closeOnStart", item.m_CloseMO == ExecutableInfo::CloseMOStyle::DEFAULT_CLOSE); settings.setValue("steamAppID", item.m_SteamAppID); - settings.setValue("ownicon", item.m_UseOwnIcon); } } settings.endArray(); @@ -330,15 +330,21 @@ void OrganizerCore::updateExecutablesList(QSettings &settings) ExecutableInfo::CloseMOStyle closeMO = settings.value("closeOnStart").toBool() ? ExecutableInfo::CloseMOStyle::DEFAULT_CLOSE : ExecutableInfo::CloseMOStyle::DEFAULT_STAY; + Executable::Type type = + settings.value("custom", true).toBool() ? Executable::Type::Custom : Executable::Type::Game; + Executable::Toolbar toolbar = + settings.value("toolbar", false).toBool() ? Executable::Toolbar::Enabled : Executable::Toolbar::Disabled; + Executable::Icon icon = + settings.value("ownicon", false).toBool() ? Executable::Icon::Application : Executable::Icon::MO; m_ExecutablesList.addExecutable(settings.value("title").toString(), settings.value("binary").toString(), settings.value("arguments").toString(), settings.value("workingDirectory", "").toString(), closeMO, settings.value("steamAppID", "").toString(), - settings.value("custom", true).toBool(), - settings.value("toolbar", false).toBool(), - settings.value("ownicon", false).toBool()); + type, + toolbar, + icon); } settings.endArray(); -- cgit v1.3.1