From 643a50ad5a6f6851db63ece41a399d9783dc5acd Mon Sep 17 00:00:00 2001 From: Tannin Date: Sun, 2 Aug 2015 20:09:56 +0200 Subject: - changed the way flags on executables are stored in memory - some coding style fixes --- src/editexecutablesdialog.cpp | 7 +++--- src/executableslist.cpp | 33 ++++++++++++++-------------- src/executableslist.h | 35 +++++++++++------------------ src/mainwindow.cpp | 51 ++++++++++++++++++++----------------------- src/mainwindow.h | 8 +++---- src/organizercore.cpp | 16 ++++++-------- 6 files changed, 68 insertions(+), 82 deletions(-) (limited to 'src') diff --git a/src/editexecutablesdialog.cpp b/src/editexecutablesdialog.cpp index 97154789..47f9262d 100644 --- a/src/editexecutablesdialog.cpp +++ b/src/editexecutablesdialog.cpp @@ -60,7 +60,8 @@ void EditExecutablesDialog::refreshExecutablesWidget() for(; current != end; ++current) { QListWidgetItem *newItem = new QListWidgetItem(current->m_Title); - newItem->setTextColor(current->m_Type == Executable::Type::Custom ? QColor(Qt::black) : QColor(Qt::darkGray)); + newItem->setTextColor((current->m_Flags & Executable::CustomExecutable) ? QColor(Qt::black) + : QColor(Qt::darkGray)); ui->executablesListBox->addItem(newItem); } @@ -100,10 +101,8 @@ void EditExecutablesDialog::saveExecutable() : ExecutableInfo::CloseMOStyle::DEFAULT_STAY, ui->overwriteAppIDBox->isChecked() ? ui->appIDOverwriteEdit->text() : "", - Executable::Type::Custom, - Executable::Toolbar::Disabled, ui->useAppIconCheckBox->isChecked() ? - Executable::Icon::Application : Executable::Icon::MO); + Executable::UseApplicationIcon : Executable::Flags()); } diff --git a/src/executableslist.cpp b/src/executableslist.cpp index ba2b5616..5a870009 100644 --- a/src/executableslist.cpp +++ b/src/executableslist.cpp @@ -74,7 +74,7 @@ const Executable &ExecutablesList::find(const QString &title) const return exe; } } - throw std::runtime_error("invalid name " + title.toStdString()); + throw std::runtime_error(QString("invalid name %1").arg(title).toLocal8Bit().constData()); } @@ -85,7 +85,7 @@ Executable &ExecutablesList::find(const QString &title) return exe; } } - throw std::runtime_error("invalid name " + title.toStdString()); + throw std::runtime_error(QString("invalid name %1").arg(title).toLocal8Bit().constData()); } @@ -135,25 +135,21 @@ void ExecutablesList::addExecutable(const QString &title, const QString &workingDirectory, ExecutableInfo::CloseMOStyle closeMO, const QString &steamAppID, - Executable::Type custom, - Executable::Toolbar toolbar, - Executable::Icon ownicon) + Executable::Flags flags) { QFileInfo file(executableName); auto existingExe = findExe(title); if (existingExe != m_Executables.end()) { existingExe->m_Title = title; - existingExe->m_Type = custom; existingExe->m_CloseMO = closeMO; - existingExe->m_Toolbar = toolbar; - if (custom == Executable::Type::Custom) { + existingExe->m_Flags = flags; + if (flags & Executable::CustomExecutable) { // 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_Icon = ownicon; } } else { Executable newExe; @@ -163,9 +159,7 @@ void ExecutablesList::addExecutable(const QString &title, newExe.m_Arguments = arguments; newExe.m_WorkingDirectory = workingDirectory; newExe.m_SteamAppID = steamAppID; - newExe.m_Type = Executable::Type::Custom; - newExe.m_Toolbar = toolbar; - newExe.m_Icon = ownicon; + newExe.m_Flags = Executable::CustomExecutable | flags; m_Executables.push_back(newExe); } } @@ -174,7 +168,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->isCustom() && iter->m_Title == title) { + if (iter->isCustom() && (iter->m_Title == title)) { m_Executables.erase(iter); break; } @@ -195,9 +189,16 @@ void ExecutablesList::addExecutableInternal(const QString &title, const QString newExe.m_Arguments = arguments; newExe.m_WorkingDirectory = workingDirectory; newExe.m_SteamAppID = steamAppID; - newExe.m_Type = Executable::Type::Game; - newExe.m_Toolbar = Executable::Toolbar::Disabled; - newExe.m_Icon = Executable::Icon::MO; + newExe.m_Flags = 0; m_Executables.push_back(newExe); } } + + +void Executable::showOnToolbar(bool state) +{ + if (state) + m_Flags |= ShowInToolbar; + else + m_Flags ^= ShowInToolbar; +} diff --git a/src/executableslist.h b/src/executableslist.h index 4e1ad644..907b37aa 100644 --- a/src/executableslist.h +++ b/src/executableslist.h @@ -39,32 +39,23 @@ struct Executable { QString m_SteamAppID; QString m_WorkingDirectory; - enum class Type { - Game, - Custom + enum Flag { + CustomExecutable = 0x01, + ShowInToolbar = 0x02, + UseApplicationIcon = 0x04 }; - enum class Toolbar { - Disabled, - Enabled - }; - - enum class Icon { - MO, - Application - }; + Q_DECLARE_FLAGS(Flags, Flag) - Type m_Type; - Toolbar m_Toolbar; - Icon m_Icon; + Flags m_Flags; - bool isCustom() const { return m_Type == Type::Custom; } + bool isCustom() const { return m_Flags.testFlag(CustomExecutable); } - bool shownOnToolbar() const { return m_Toolbar == Toolbar::Enabled; } + bool shownOnToolbar() const { return m_Flags.testFlag(ShowInToolbar); } - void showOnToolbar(bool state) { m_Toolbar = state ? Toolbar::Enabled : Toolbar::Disabled; } + void showOnToolbar(bool state); - bool usesOwnIcon() const { return m_Icon == Icon::Application; } + bool usesOwnIcon() const { return m_Flags.testFlag(UseApplicationIcon); } }; @@ -149,9 +140,7 @@ public: const QString &workingDirectory, MOBase::ExecutableInfo::CloseMOStyle closeMO, const QString &steamAppID, - Executable::Type custom, - Executable::Toolbar toolbar, - Executable::Icon ownicon); + Executable::Flags flags); /** * @brief remove the executable with the specified file name. This needs to be an absolute file path @@ -198,4 +187,6 @@ private: }; +Q_DECLARE_OPERATORS_FOR_FLAGS(Executable::Flags) + #endif // EXECUTABLESLIST_H diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 2e7797d2..dda0470e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -459,7 +459,7 @@ void MainWindow::actionToToolButton(QAction *&sourceAction) void MainWindow::updateToolBar() { - foreach (QAction *action, ui->toolBar->actions()) { + for (QAction *action : ui->toolBar->actions()) { if (action->objectName().startsWith("custom__")) { ui->toolBar->removeAction(action); } @@ -478,7 +478,7 @@ void MainWindow::updateToolBar() actionToToolButton(ui->actionHelp); createHelpWidget(); - foreach (QAction *action, ui->toolBar->actions()) { + for (QAction *action : ui->toolBar->actions()) { if (action->isSeparator()) { // insert spacers ui->toolBar->insertWidget(action, spacer); @@ -953,7 +953,7 @@ void MainWindow::startExeAction() { QAction *action = qobject_cast(sender()); if (action != nullptr) { - Executable const &selectedExecutable(m_OrganizerCore.executablesList()->find(action->text())); + const Executable &selectedExecutable(m_OrganizerCore.executablesList()->find(action->text())); m_OrganizerCore.spawnBinary( selectedExecutable.m_BinaryInfo, selectedExecutable.m_Arguments, @@ -1612,7 +1612,7 @@ void MainWindow::installMod(QString fileName) void MainWindow::on_startButton_clicked() { - Executable const &selectedExecutable(getSelectedExecutable()); + const Executable &selectedExecutable(getSelectedExecutable()); m_OrganizerCore.spawnBinary( selectedExecutable.m_BinaryInfo, @@ -1731,7 +1731,7 @@ void MainWindow::on_executablesListBox_currentIndexChanged(int index) if (executablesList->isEnabled()) { //I think the 2nd test is impossible - if (index == 0 || index > static_cast(m_OrganizerCore.executablesList()->size())) { + if ((index == 0) || (index >= static_cast(m_OrganizerCore.executablesList()->size()))) { if (modifyExecutablesDialog()) { setExecutableIndex(previousIndex); } @@ -3257,31 +3257,31 @@ void MainWindow::linkToolbar() { Executable &exe(getSelectedExecutable()); 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")); + ui->linkButton->menu()->actions().at(static_cast(ShortcutType::Toolbar))->setIcon(exe.shownOnToolbar() ? QIcon(":/MO/gui/remove") : QIcon(":/MO/gui/link")); updateToolBar(); } namespace { -QString getLinkfile(QString const &dir, Executable const &exec) +QString getLinkfile(const QString &dir, const Executable &exec) { return QDir::fromNativeSeparators(dir) + "/" + exec.m_Title + ".lnk"; } -QString getDesktopLinkfile(Executable const &exec) +QString getDesktopLinkfile(const Executable &exec) { return getLinkfile(getDesktopDirectory(), exec); } -QString getStartMenuLinkfile(Executable const &exec) +QString getStartMenuLinkfile(const Executable &exec) { return getLinkfile(getStartMenuDirectory(), exec); } } -void MainWindow::addWindowsLink(Shortcut_Type const mapping) +void MainWindow::addWindowsLink(const ShortcutType mapping) { Executable const &selectedExecutable(getSelectedExecutable()); - QString const linkName = getLinkfile(mapping == Shortcut_Type::Windows_Desktop ? getDesktopDirectory() : getStartMenuDirectory(), + QString const linkName = getLinkfile(mapping == ShortcutType::Desktop ? getDesktopDirectory() : getStartMenuDirectory(), selectedExecutable); if (QFile::exists(linkName)) { @@ -3317,12 +3317,12 @@ void MainWindow::addWindowsLink(Shortcut_Type const mapping) void MainWindow::linkDesktop() { - addWindowsLink(Shortcut_Type::Windows_Desktop); + addWindowsLink(ShortcutType::Desktop); } void MainWindow::linkMenu() { - addWindowsLink(Shortcut_Type::Windows_StartMenu); + addWindowsLink(ShortcutType::StartMenu); } void MainWindow::on_actionSettings_triggered() @@ -3547,9 +3547,7 @@ void MainWindow::addAsExecutable() targetInfo.absolutePath(), ExecutableInfo::CloseMOStyle::DEFAULT_STAY, QString(), - Executable::Type::Custom, - Executable::Toolbar::Disabled, - Executable::Icon::MO); + Executable::CustomExecutable); refreshExecutablesList(); } } break; @@ -3691,8 +3689,7 @@ void MainWindow::openDataFile() void MainWindow::updateAvailable() { - QToolBar *toolBar = findChild("toolBar"); - foreach (QAction *action, toolBar->actions()) { + for (QAction *action : ui->toolBar->actions()) { if (action->text() == tr("Update")) { action->setEnabled(true); action->setToolTip(tr("Update available")); @@ -4159,7 +4156,7 @@ void MainWindow::on_espList_customContextMenuRequested(const QPoint &pos) QItemSelection currentSelection = ui->espList->selectionModel()->selection(); bool hasLocked = false; bool hasUnlocked = false; - Q_FOREACH (const QModelIndex &idx, currentSelection.indexes()) { + for (const QModelIndex &idx : currentSelection.indexes()) { int row = m_PluginListSortProxy->mapToSource(idx).row(); if (m_OrganizerCore.pluginList()->isEnabled(row)) { if (m_OrganizerCore.pluginList()->isESPLocked(row)) { @@ -4237,17 +4234,17 @@ Executable &MainWindow::getSelectedExecutable() void MainWindow::on_linkButton_pressed() { - Executable const &selectedExecutable(getSelectedExecutable()); + const Executable &selectedExecutable(getSelectedExecutable()); - QIcon const addIcon(":/MO/gui/link"); - QIcon const removeIcon(":/MO/gui/remove"); + const QIcon addIcon(":/MO/gui/link"); + const QIcon removeIcon(":/MO/gui/remove"); - QFileInfo const linkDesktopFile(getDesktopLinkfile(selectedExecutable)); - QFileInfo const linkMenuFile(getStartMenuLinkfile(selectedExecutable)); + const QFileInfo linkDesktopFile(getDesktopLinkfile(selectedExecutable)); + const QFileInfo linkMenuFile(getStartMenuLinkfile(selectedExecutable)); - 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); + ui->linkButton->menu()->actions().at(static_cast(ShortcutType::Toolbar))->setIcon(selectedExecutable.shownOnToolbar() ? removeIcon : addIcon); + ui->linkButton->menu()->actions().at(static_cast(ShortcutType::Desktop))->setIcon(linkDesktopFile.exists() ? removeIcon : addIcon); + ui->linkButton->menu()->actions().at(static_cast(ShortcutType::StartMenu))->setIcon(linkMenuFile.exists() ? removeIcon : addIcon); } void MainWindow::on_showHiddenBox_toggled(bool checked) diff --git a/src/mainwindow.h b/src/mainwindow.h index d60de869..d16719e3 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -326,13 +326,13 @@ private: MOBase::DelayedFileWriter m_ArchiveListWriter; - enum class Shortcut_Type { + enum class ShortcutType { Toolbar, - Windows_Desktop, - Windows_StartMenu + Desktop, + StartMenu }; - void addWindowsLink(Shortcut_Type const); + void addWindowsLink(ShortcutType const); Executable const &getSelectedExecutable() const; Executable &getSelectedExecutable(); diff --git a/src/organizercore.cpp b/src/organizercore.cpp index 852abba3..1a81d33b 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -330,21 +330,19 @@ 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; + + Executable::Flags flags; + if (settings.value("custom", true).toBool()) flags |= Executable::CustomExecutable; + if (settings.value("toolbar", false).toBool()) flags |= Executable::ShowInToolbar; + if (settings.value("ownicon", false).toBool()) flags |= Executable::UseApplicationIcon; + m_ExecutablesList.addExecutable(settings.value("title").toString(), settings.value("binary").toString(), settings.value("arguments").toString(), settings.value("workingDirectory", "").toString(), closeMO, settings.value("steamAppID", "").toString(), - type, - toolbar, - icon); + flags); } settings.endArray(); -- cgit v1.3.1