From 05bf915c031a1b1d0331fe6209e1eddaee5f7f07 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 31 Oct 2016 17:27:24 +0100 Subject: [PATCH] Core: Refactor argument handling Split up getting the data from the arguments list and actually applying the values, and move the details of theme creation and application to better places. This gets rid of ugly control flow details like that CorePlugin::initialize created the action manager before calling parseArguments, because that is needed to apply the presentation mode argument setting, and parseArguments created the main window because that needs to be created _after_ setting the theme (which can be overridden by command line argument), but _before_ applying the override color argument setting. Change-Id: I9c99305b6efbfcc4b37cea9e5c70d816a621963b Reviewed-by: David Schulz --- src/libs/utils/theme/theme.cpp | 2 + src/plugins/coreplugin/coreplugin.cpp | 58 +++++++++---------------- src/plugins/coreplugin/coreplugin.h | 1 - src/plugins/coreplugin/themechooser.cpp | 23 +++++++++- src/plugins/coreplugin/themechooser.h | 4 ++ 5 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/libs/utils/theme/theme.cpp b/src/libs/utils/theme/theme.cpp index 90037dee7c8..10ee20217d4 100644 --- a/src/libs/utils/theme/theme.cpp +++ b/src/libs/utils/theme/theme.cpp @@ -58,6 +58,8 @@ void setCreatorTheme(Theme *theme) return; delete m_creatorTheme; m_creatorTheme = theme; + if (theme && theme->flag(Theme::ApplyThemePaletteGlobally)) + QApplication::setPalette(theme->palette()); } Theme::Theme(const QString &id, QObject *parent) diff --git a/src/plugins/coreplugin/coreplugin.cpp b/src/plugins/coreplugin/coreplugin.cpp index 75df4228015..888abfbb5c2 100644 --- a/src/plugins/coreplugin/coreplugin.cpp +++ b/src/plugins/coreplugin/coreplugin.cpp @@ -93,53 +93,29 @@ CorePlugin::~CorePlugin() setCreatorTheme(0); } -void CorePlugin::parseArguments(const QStringList &arguments) -{ - const Id settingsThemeId = Id::fromSetting(ICore::settings()->value( - QLatin1String(Constants::SETTINGS_THEME), QLatin1String(Constants::DEFAULT_THEME))); - Id themeId = settingsThemeId; +struct CoreArguments { QColor overrideColor; + Id themeId; bool presentationMode = false; +}; +CoreArguments parseArguments(const QStringList &arguments) +{ + CoreArguments args; for (int i = 0; i < arguments.size(); ++i) { if (arguments.at(i) == QLatin1String("-color")) { const QString colorcode(arguments.at(i + 1)); - overrideColor = QColor(colorcode); + args.overrideColor = QColor(colorcode); i++; // skip the argument } if (arguments.at(i) == QLatin1String("-presentationMode")) - presentationMode = true; + args.presentationMode = true; if (arguments.at(i) == QLatin1String("-theme")) { - themeId = Id::fromString(arguments.at(i + 1)); - i++; + args.themeId = Id::fromString(arguments.at(i + 1)); + i++; // skip the argument } } - const QList availableThemes = ThemeEntry::availableThemes(); - int themeIndex = Utils::indexOf(availableThemes, Utils::equal(&ThemeEntry::id, themeId)); - if (themeIndex < 0) { - themeIndex = Utils::indexOf(availableThemes, - Utils::equal(&ThemeEntry::id, settingsThemeId)); - } - if (themeIndex < 0) - themeIndex = 0; - if (themeIndex < availableThemes.size()) { - const ThemeEntry themeEntry = availableThemes.at(themeIndex); - QSettings themeSettings(themeEntry.filePath(), QSettings::IniFormat); - Theme *theme = new Theme(themeEntry.id().toString(), qApp); - theme->readSettings(themeSettings); - if (theme->flag(Theme::ApplyThemePaletteGlobally)) - QApplication::setPalette(theme->palette()); - setCreatorTheme(theme); - } - - // defer creation of these widgets until here, - // because they need a valid theme set - m_mainWindow = new MainWindow; - ActionManager::setPresentationModeEnabled(presentationMode); - m_locator = new Locator; - - if (overrideColor.isValid()) - m_mainWindow->setOverrideColor(overrideColor); + return args; } bool CorePlugin::initialize(const QStringList &arguments, QString *errorMessage) @@ -148,10 +124,18 @@ bool CorePlugin::initialize(const QStringList &arguments, QString *errorMessage) *errorMessage = tr("No themes found in installation."); return false; } - new ActionManager(this); + const CoreArguments args = parseArguments(arguments); Theme::initialPalette(); // Initialize palette before setting it + Theme *themeFromArg = ThemeEntry::createTheme(args.themeId); + setCreatorTheme(themeFromArg ? themeFromArg + : ThemeEntry::createTheme(ThemeEntry::themeSetting())); + new ActionManager(this); + ActionManager::setPresentationModeEnabled(args.presentationMode); + m_mainWindow = new MainWindow; + if (args.overrideColor.isValid()) + m_mainWindow->setOverrideColor(args.overrideColor); + m_locator = new Locator; qsrand(QDateTime::currentDateTime().toTime_t()); - parseArguments(arguments); const bool success = m_mainWindow->init(errorMessage); if (success) { m_editMode = new EditMode; diff --git a/src/plugins/coreplugin/coreplugin.h b/src/plugins/coreplugin/coreplugin.h index c6135ed85c2..62a11fb2e95 100644 --- a/src/plugins/coreplugin/coreplugin.h +++ b/src/plugins/coreplugin/coreplugin.h @@ -78,7 +78,6 @@ private slots: #endif private: - void parseArguments(const QStringList & arguments); static void addToPathChooserContextMenu(Utils::PathChooser *pathChooser, QMenu *menu); MainWindow *m_mainWindow; diff --git a/src/plugins/coreplugin/themechooser.cpp b/src/plugins/coreplugin/themechooser.cpp index 6e18697aa26..ab695aeea82 100644 --- a/src/plugins/coreplugin/themechooser.cpp +++ b/src/plugins/coreplugin/themechooser.cpp @@ -171,8 +171,7 @@ void ThemeChooser::apply() return; const QString themeId = d->m_themeListModel->themeAt(index).id().toString(); QSettings *settings = ICore::settings(); - const QString currentThemeId = settings->value(QLatin1String(Constants::SETTINGS_THEME), - QLatin1String(Constants::DEFAULT_THEME)).toString(); + const QString currentThemeId = ThemeEntry::themeSetting().toString(); if (currentThemeId != themeId) { QMessageBox::information(ICore::mainWindow(), tr("Restart Required"), tr("The theme change will take effect after a restart of Qt Creator.")); @@ -215,5 +214,25 @@ QList ThemeEntry::availableThemes() return themes; } +Id ThemeEntry::themeSetting() +{ + return Id::fromSetting(ICore::settings()->value(QLatin1String(Constants::SETTINGS_THEME), + QLatin1String(Constants::DEFAULT_THEME))); +} + +Theme *ThemeEntry::createTheme(Id id) +{ + if (!id.isValid()) + return nullptr; + const ThemeEntry entry = Utils::findOrDefault(availableThemes(), + Utils::equal(&ThemeEntry::id, id)); + if (!entry.id().isValid()) + return nullptr; + QSettings themeSettings(entry.filePath(), QSettings::IniFormat); + Theme *theme = new Theme(entry.id().toString()); + theme->readSettings(themeSettings); + return theme; +} + } // namespace Internal } // namespace Core diff --git a/src/plugins/coreplugin/themechooser.h b/src/plugins/coreplugin/themechooser.h index e95c4802690..51930347601 100644 --- a/src/plugins/coreplugin/themechooser.h +++ b/src/plugins/coreplugin/themechooser.h @@ -31,6 +31,8 @@ #include +namespace Utils { class Theme; } + namespace Core { namespace Internal { @@ -46,6 +48,8 @@ public: QString displayName() const; QString filePath() const; static QList availableThemes(); + static Id themeSetting(); + static Utils::Theme *createTheme(Id id); private: Id m_id;