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 <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2016-10-31 17:27:24 +01:00
parent d93963296a
commit 05bf915c03
5 changed files with 48 additions and 40 deletions

View File

@@ -58,6 +58,8 @@ void setCreatorTheme(Theme *theme)
return; return;
delete m_creatorTheme; delete m_creatorTheme;
m_creatorTheme = theme; m_creatorTheme = theme;
if (theme && theme->flag(Theme::ApplyThemePaletteGlobally))
QApplication::setPalette(theme->palette());
} }
Theme::Theme(const QString &id, QObject *parent) Theme::Theme(const QString &id, QObject *parent)

View File

@@ -93,53 +93,29 @@ CorePlugin::~CorePlugin()
setCreatorTheme(0); setCreatorTheme(0);
} }
void CorePlugin::parseArguments(const QStringList &arguments) struct CoreArguments {
{
const Id settingsThemeId = Id::fromSetting(ICore::settings()->value(
QLatin1String(Constants::SETTINGS_THEME), QLatin1String(Constants::DEFAULT_THEME)));
Id themeId = settingsThemeId;
QColor overrideColor; QColor overrideColor;
Id themeId;
bool presentationMode = false; bool presentationMode = false;
};
CoreArguments parseArguments(const QStringList &arguments)
{
CoreArguments args;
for (int i = 0; i < arguments.size(); ++i) { for (int i = 0; i < arguments.size(); ++i) {
if (arguments.at(i) == QLatin1String("-color")) { if (arguments.at(i) == QLatin1String("-color")) {
const QString colorcode(arguments.at(i + 1)); const QString colorcode(arguments.at(i + 1));
overrideColor = QColor(colorcode); args.overrideColor = QColor(colorcode);
i++; // skip the argument i++; // skip the argument
} }
if (arguments.at(i) == QLatin1String("-presentationMode")) if (arguments.at(i) == QLatin1String("-presentationMode"))
presentationMode = true; args.presentationMode = true;
if (arguments.at(i) == QLatin1String("-theme")) { if (arguments.at(i) == QLatin1String("-theme")) {
themeId = Id::fromString(arguments.at(i + 1)); args.themeId = Id::fromString(arguments.at(i + 1));
i++; i++; // skip the argument
} }
} }
const QList<ThemeEntry> availableThemes = ThemeEntry::availableThemes(); return args;
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);
} }
bool CorePlugin::initialize(const QStringList &arguments, QString *errorMessage) 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."); *errorMessage = tr("No themes found in installation.");
return false; return false;
} }
new ActionManager(this); const CoreArguments args = parseArguments(arguments);
Theme::initialPalette(); // Initialize palette before setting it 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()); qsrand(QDateTime::currentDateTime().toTime_t());
parseArguments(arguments);
const bool success = m_mainWindow->init(errorMessage); const bool success = m_mainWindow->init(errorMessage);
if (success) { if (success) {
m_editMode = new EditMode; m_editMode = new EditMode;

View File

@@ -78,7 +78,6 @@ private slots:
#endif #endif
private: private:
void parseArguments(const QStringList & arguments);
static void addToPathChooserContextMenu(Utils::PathChooser *pathChooser, QMenu *menu); static void addToPathChooserContextMenu(Utils::PathChooser *pathChooser, QMenu *menu);
MainWindow *m_mainWindow; MainWindow *m_mainWindow;

View File

@@ -171,8 +171,7 @@ void ThemeChooser::apply()
return; return;
const QString themeId = d->m_themeListModel->themeAt(index).id().toString(); const QString themeId = d->m_themeListModel->themeAt(index).id().toString();
QSettings *settings = ICore::settings(); QSettings *settings = ICore::settings();
const QString currentThemeId = settings->value(QLatin1String(Constants::SETTINGS_THEME), const QString currentThemeId = ThemeEntry::themeSetting().toString();
QLatin1String(Constants::DEFAULT_THEME)).toString();
if (currentThemeId != themeId) { if (currentThemeId != themeId) {
QMessageBox::information(ICore::mainWindow(), tr("Restart Required"), QMessageBox::information(ICore::mainWindow(), tr("Restart Required"),
tr("The theme change will take effect after a restart of Qt Creator.")); tr("The theme change will take effect after a restart of Qt Creator."));
@@ -215,5 +214,25 @@ QList<ThemeEntry> ThemeEntry::availableThemes()
return themes; 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 Internal
} // namespace Core } // namespace Core

View File

@@ -31,6 +31,8 @@
#include <QWidget> #include <QWidget>
namespace Utils { class Theme; }
namespace Core { namespace Core {
namespace Internal { namespace Internal {
@@ -46,6 +48,8 @@ public:
QString displayName() const; QString displayName() const;
QString filePath() const; QString filePath() const;
static QList<ThemeEntry> availableThemes(); static QList<ThemeEntry> availableThemes();
static Id themeSetting();
static Utils::Theme *createTheme(Id id);
private: private:
Id m_id; Id m_id;