Files
qt-creator/src/plugins/welcome/welcomeplugin.cpp

349 lines
11 KiB
C++
Raw Normal View History

/****************************************************************************
2009-07-20 19:08:09 +02:00
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
2009-07-20 19:08:09 +02:00
**
** This file is part of Qt Creator.
2009-07-20 19:08:09 +02:00
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
2009-07-20 19:08:09 +02:00
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
2009-07-20 19:08:09 +02:00
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
2009-07-20 19:08:09 +02:00
#include "welcomeplugin.h"
2011-04-13 17:09:44 +02:00
#include <extensionsystem/pluginmanager.h>
2011-04-13 17:09:44 +02:00
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/imode.h>
#include <coreplugin/iwelcomepage.h>
2009-07-20 19:08:09 +02:00
#include <coreplugin/modemanager.h>
#include <coreplugin/iwizardfactory.h>
2009-07-20 19:08:09 +02:00
#include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
2011-04-13 17:09:44 +02:00
#include <utils/styledbar.h>
Implement theming for QtCreator Adds a 'Theme' tab to the environment settings and a '-theme' command line option. A theme is a combination of colors, gradients, flags and style information. There are two themes: - 'default': preserves the current default look - 'dark': uses a more flat for many widgets, dark color theme for everything This does not use a stylesheet (too limited), but rather sets the palette via C++ and modifies drawing behavior. Overall, the look is more flat (removed some gradients and bevels). Tested on Ubuntu 14.04 using Qt 5.4 and running on a KDE Desktop (Oxygen base style). For a screenshot, see https://gist.github.com/thorbenk/5ab06bea726de0aa7473 Changes: - Introduce class Theme, defining the interface how to access theme specific settings. The class reads a .creatortheme file (INI file, via QSettings) - Define named colors in the [Palette] section (see dark.creatortheme for example usage) - Use either named colors of AARRGGBB (hex) in the [Colors] section - A file ending with .creatortheme may be supplied to the '-theme' command line option - A global Theme instance can be accessed via creatorTheme() - Query colors, gradients, icons and flags from the theme were possible (TODO: use this in more places...) - There are very many color roles. It seems better to me to describe the role clearly, and then to consolidate later in the actual theme by assigning the same color. For example, one can set the text color of the output pane button individualy. - Many elements are also drawn differently. For the dark theme, I wanted to have a flatter look. - Introduce Theme::WidgetStyle enum, for now {Original, Flat}. - The theme specifies which kind of widget style it wants. - The drawing code queries the theme's style flag and switches between the original, gradient based look and the new, flat look. - Create some custom icons which look better on dark background (wip, currently folder/file icons) - Let ManhattanStyle draw some elements for non-panelwidgets, too (open/close arrows in QTreeView, custom folder/file icons) - For the welcomescreen, pass the WelcomeTheme class. WelcomeTheme exposes theme colors as Q_PROPERTY accessible from .qml - Themes can be modified via the 'Themes' tab in the environment settings. TODO: * Unify image handling * Avoid style name references * Fix gradients Change-Id: I92c2050ab0fb327649ea1eff4adec973d2073944 Reviewed-by: Thomas Hartmann <Thomas.Hartmann@digia.com> Reviewed-by: hjk <hjk121@nokiamail.com>
2014-10-14 19:09:48 +02:00
#include <utils/theme/theme.h>
#include <QVBoxLayout>
#include <QMessageBox>
2011-04-13 17:09:44 +02:00
#include <QDir>
#include <QQmlPropertyMap>
2009-07-20 19:08:09 +02:00
#ifdef USE_QUICK_WIDGET
#include <QtQuickWidgets/QQuickWidget>
typedef QQuickWidget QuickContainer;
#else
#include <QtQuick/QQuickView>
typedef QQuickView QuickContainer;
#endif
#include <QtQml/QQmlContext>
#include <QtQml/QQmlEngine>
enum { debug = 0 };
2009-07-20 19:08:09 +02:00
2011-04-13 17:09:44 +02:00
using namespace ExtensionSystem;
using namespace Utils;
2011-04-13 17:09:44 +02:00
static const char currentPageSettingsKeyC[] = "WelcomeTab";
2011-04-13 17:09:44 +02:00
namespace Welcome {
namespace Internal {
class WelcomeMode : public Core::IMode
{
Q_OBJECT
Q_PROPERTY(int activePlugin READ activePlugin WRITE setActivePlugin NOTIFY activePluginChanged)
2011-04-13 17:09:44 +02:00
public:
WelcomeMode();
~WelcomeMode();
void activated();
void initPlugins();
int activePlugin() const { return m_activePlugin; }
// bool eventFilter(QObject *, QEvent *);
public slots:
Implement theming for QtCreator Adds a 'Theme' tab to the environment settings and a '-theme' command line option. A theme is a combination of colors, gradients, flags and style information. There are two themes: - 'default': preserves the current default look - 'dark': uses a more flat for many widgets, dark color theme for everything This does not use a stylesheet (too limited), but rather sets the palette via C++ and modifies drawing behavior. Overall, the look is more flat (removed some gradients and bevels). Tested on Ubuntu 14.04 using Qt 5.4 and running on a KDE Desktop (Oxygen base style). For a screenshot, see https://gist.github.com/thorbenk/5ab06bea726de0aa7473 Changes: - Introduce class Theme, defining the interface how to access theme specific settings. The class reads a .creatortheme file (INI file, via QSettings) - Define named colors in the [Palette] section (see dark.creatortheme for example usage) - Use either named colors of AARRGGBB (hex) in the [Colors] section - A file ending with .creatortheme may be supplied to the '-theme' command line option - A global Theme instance can be accessed via creatorTheme() - Query colors, gradients, icons and flags from the theme were possible (TODO: use this in more places...) - There are very many color roles. It seems better to me to describe the role clearly, and then to consolidate later in the actual theme by assigning the same color. For example, one can set the text color of the output pane button individualy. - Many elements are also drawn differently. For the dark theme, I wanted to have a flatter look. - Introduce Theme::WidgetStyle enum, for now {Original, Flat}. - The theme specifies which kind of widget style it wants. - The drawing code queries the theme's style flag and switches between the original, gradient based look and the new, flat look. - Create some custom icons which look better on dark background (wip, currently folder/file icons) - Let ManhattanStyle draw some elements for non-panelwidgets, too (open/close arrows in QTreeView, custom folder/file icons) - For the welcomescreen, pass the WelcomeTheme class. WelcomeTheme exposes theme colors as Q_PROPERTY accessible from .qml - Themes can be modified via the 'Themes' tab in the environment settings. TODO: * Unify image handling * Avoid style name references * Fix gradients Change-Id: I92c2050ab0fb327649ea1eff4adec973d2073944 Reviewed-by: Thomas Hartmann <Thomas.Hartmann@digia.com> Reviewed-by: hjk <hjk121@nokiamail.com>
2014-10-14 19:09:48 +02:00
void onThemeChanged();
void setActivePlugin(int pos)
{
if (m_activePlugin != pos) {
m_activePlugin = pos;
emit activePluginChanged(pos);
}
}
signals:
void activePluginChanged(int pos);
2011-04-13 17:09:44 +02:00
private slots:
void welcomePluginAdded(QObject*);
void sceneGraphError(QQuickWindow::SceneGraphError, const QString &message);
2011-04-13 17:09:44 +02:00
private:
void facilitateQml(QQmlEngine *engine);
QWidget *m_modeWidget;
QuickContainer *m_welcomePage;
QList<QObject*> m_pluginList;
int m_activePlugin;
QQmlPropertyMap m_themeProperties;
2011-04-13 17:09:44 +02:00
};
// --- WelcomeMode
WelcomeMode::WelcomeMode()
: m_activePlugin(0)
2011-04-13 17:09:44 +02:00
{
setDisplayName(tr("Welcome"));
setIcon(QIcon(QLatin1String(":/welcome/images/mode_welcome.png")));
2011-04-13 17:09:44 +02:00
setPriority(Core::Constants::P_MODE_WELCOME);
setId(Core::Constants::MODE_WELCOME);
2011-04-13 17:09:44 +02:00
setContextHelpId(QLatin1String("Qt Creator Manual"));
2011-04-13 17:19:55 +02:00
setContext(Core::Context(Core::Constants::C_WELCOME_MODE));
m_modeWidget = new QWidget;
m_modeWidget->setObjectName(QLatin1String("WelcomePageModeWidget"));
QVBoxLayout *layout = new QVBoxLayout(m_modeWidget);
layout->setMargin(0);
layout->setSpacing(0);
2011-04-13 17:09:44 +02:00
m_welcomePage = new QuickContainer();
onThemeChanged(); // initialize background color and theme properties
m_welcomePage->setResizeMode(QuickContainer::SizeRootObjectToView);
m_welcomePage->setObjectName(QLatin1String("WelcomePage"));
connect(m_welcomePage, SIGNAL(sceneGraphError(QQuickWindow::SceneGraphError,QString)),
this, SLOT(sceneGraphError(QQuickWindow::SceneGraphError,QString)));
Utils::StyledBar* styledBar = new Utils::StyledBar(m_modeWidget);
styledBar->setObjectName(QLatin1String("WelcomePageStyledBar"));
layout->addWidget(styledBar);
#ifdef USE_QUICK_WIDGET
m_welcomePage->setParent(m_modeWidget);
layout->addWidget(m_welcomePage);
#else
QWidget *container = QWidget::createWindowContainer(m_welcomePage, m_modeWidget);
container->setProperty("nativeAncestors", true);
m_modeWidget->setLayout(layout);
layout->addWidget(container);
#endif // USE_QUICK_WIDGET
connect(PluginManager::instance(), SIGNAL(objectAdded(QObject*)), SLOT(welcomePluginAdded(QObject*)));
setWidget(m_modeWidget);
2011-04-13 17:09:44 +02:00
}
Implement theming for QtCreator Adds a 'Theme' tab to the environment settings and a '-theme' command line option. A theme is a combination of colors, gradients, flags and style information. There are two themes: - 'default': preserves the current default look - 'dark': uses a more flat for many widgets, dark color theme for everything This does not use a stylesheet (too limited), but rather sets the palette via C++ and modifies drawing behavior. Overall, the look is more flat (removed some gradients and bevels). Tested on Ubuntu 14.04 using Qt 5.4 and running on a KDE Desktop (Oxygen base style). For a screenshot, see https://gist.github.com/thorbenk/5ab06bea726de0aa7473 Changes: - Introduce class Theme, defining the interface how to access theme specific settings. The class reads a .creatortheme file (INI file, via QSettings) - Define named colors in the [Palette] section (see dark.creatortheme for example usage) - Use either named colors of AARRGGBB (hex) in the [Colors] section - A file ending with .creatortheme may be supplied to the '-theme' command line option - A global Theme instance can be accessed via creatorTheme() - Query colors, gradients, icons and flags from the theme were possible (TODO: use this in more places...) - There are very many color roles. It seems better to me to describe the role clearly, and then to consolidate later in the actual theme by assigning the same color. For example, one can set the text color of the output pane button individualy. - Many elements are also drawn differently. For the dark theme, I wanted to have a flatter look. - Introduce Theme::WidgetStyle enum, for now {Original, Flat}. - The theme specifies which kind of widget style it wants. - The drawing code queries the theme's style flag and switches between the original, gradient based look and the new, flat look. - Create some custom icons which look better on dark background (wip, currently folder/file icons) - Let ManhattanStyle draw some elements for non-panelwidgets, too (open/close arrows in QTreeView, custom folder/file icons) - For the welcomescreen, pass the WelcomeTheme class. WelcomeTheme exposes theme colors as Q_PROPERTY accessible from .qml - Themes can be modified via the 'Themes' tab in the environment settings. TODO: * Unify image handling * Avoid style name references * Fix gradients Change-Id: I92c2050ab0fb327649ea1eff4adec973d2073944 Reviewed-by: Thomas Hartmann <Thomas.Hartmann@digia.com> Reviewed-by: hjk <hjk121@nokiamail.com>
2014-10-14 19:09:48 +02:00
void WelcomeMode::onThemeChanged()
{
const QVariantHash creatorTheme = Utils::creatorTheme()->values();
QVariantHash::const_iterator it;
for (it = creatorTheme.constBegin(); it != creatorTheme.constEnd(); ++it)
m_themeProperties.insert(it.key(), it.value());
Implement theming for QtCreator Adds a 'Theme' tab to the environment settings and a '-theme' command line option. A theme is a combination of colors, gradients, flags and style information. There are two themes: - 'default': preserves the current default look - 'dark': uses a more flat for many widgets, dark color theme for everything This does not use a stylesheet (too limited), but rather sets the palette via C++ and modifies drawing behavior. Overall, the look is more flat (removed some gradients and bevels). Tested on Ubuntu 14.04 using Qt 5.4 and running on a KDE Desktop (Oxygen base style). For a screenshot, see https://gist.github.com/thorbenk/5ab06bea726de0aa7473 Changes: - Introduce class Theme, defining the interface how to access theme specific settings. The class reads a .creatortheme file (INI file, via QSettings) - Define named colors in the [Palette] section (see dark.creatortheme for example usage) - Use either named colors of AARRGGBB (hex) in the [Colors] section - A file ending with .creatortheme may be supplied to the '-theme' command line option - A global Theme instance can be accessed via creatorTheme() - Query colors, gradients, icons and flags from the theme were possible (TODO: use this in more places...) - There are very many color roles. It seems better to me to describe the role clearly, and then to consolidate later in the actual theme by assigning the same color. For example, one can set the text color of the output pane button individualy. - Many elements are also drawn differently. For the dark theme, I wanted to have a flatter look. - Introduce Theme::WidgetStyle enum, for now {Original, Flat}. - The theme specifies which kind of widget style it wants. - The drawing code queries the theme's style flag and switches between the original, gradient based look and the new, flat look. - Create some custom icons which look better on dark background (wip, currently folder/file icons) - Let ManhattanStyle draw some elements for non-panelwidgets, too (open/close arrows in QTreeView, custom folder/file icons) - For the welcomescreen, pass the WelcomeTheme class. WelcomeTheme exposes theme colors as Q_PROPERTY accessible from .qml - Themes can be modified via the 'Themes' tab in the environment settings. TODO: * Unify image handling * Avoid style name references * Fix gradients Change-Id: I92c2050ab0fb327649ea1eff4adec973d2073944 Reviewed-by: Thomas Hartmann <Thomas.Hartmann@digia.com> Reviewed-by: hjk <hjk121@nokiamail.com>
2014-10-14 19:09:48 +02:00
}
2011-04-13 17:09:44 +02:00
WelcomeMode::~WelcomeMode()
2009-07-20 19:08:09 +02:00
{
QSettings *settings = Core::ICore::settings();
settings->setValue(QLatin1String(currentPageSettingsKeyC), activePlugin());
delete m_modeWidget;
2009-07-20 19:08:09 +02:00
}
void WelcomeMode::sceneGraphError(QQuickWindow::SceneGraphError, const QString &message)
{
QMessageBox *messageBox =
new QMessageBox(QMessageBox::Warning,
tr("Welcome Mode Load Error"), message,
QMessageBox::Close, m_modeWidget);
messageBox->setModal(false);
messageBox->setAttribute(Qt::WA_DeleteOnClose);
messageBox->show();
}
void WelcomeMode::facilitateQml(QQmlEngine * /*engine*/)
{
}
static QString applicationDirPath()
{
// normalize paths so QML doesn't freak out if it's wrongly capitalized on Windows
return Utils::FileUtils::normalizePathName(QCoreApplication::applicationDirPath());
}
static QString resourcePath()
{
// normalize paths so QML doesn't freak out if it's wrongly capitalized on Windows
return Utils::FileUtils::normalizePathName(Core::ICore::resourcePath());
}
2011-04-13 17:09:44 +02:00
void WelcomeMode::initPlugins()
{
QSettings *settings = Core::ICore::settings();
setActivePlugin(settings->value(QLatin1String(currentPageSettingsKeyC)).toInt());
QQmlContext *ctx = m_welcomePage->rootContext();
ctx->setContextProperty(QLatin1String("welcomeMode"), this);
QList<Core::IWelcomePage*> duplicatePlugins = PluginManager::getObjects<Core::IWelcomePage>();
Utils::sort(duplicatePlugins, [](const Core::IWelcomePage *l, const Core::IWelcomePage *r) {
return l->priority() < r->priority();
});
QList<Core::IWelcomePage*> plugins;
QHash<Core::Id, Core::IWelcomePage*> pluginHash;
//avoid duplicate ids - choose by priority
foreach (Core::IWelcomePage* plugin, duplicatePlugins) {
if (pluginHash.contains(plugin->id())) {
Core::IWelcomePage* pluginOther = pluginHash.value(plugin->id());
if (pluginOther->priority() > plugin->priority()) {
plugins.removeAll(pluginOther);
pluginHash.remove(pluginOther->id());
plugins << plugin;
pluginHash.insert(plugin->id(), plugin);
}
} else {
plugins << plugin;
pluginHash.insert(plugin->id(), plugin);
}
}
QQmlEngine *engine = m_welcomePage->engine();
QStringList importPathList = engine->importPathList();
importPathList << resourcePath() + QLatin1String("/welcomescreen");
engine->setImportPathList(importPathList);
if (!debug)
engine->setOutputWarningsToStandardError(false);
QString pluginPath = applicationDirPath();
if (HostOsInfo::isMacHost())
pluginPath += QLatin1String("/../PlugIns");
else
pluginPath += QLatin1String("/../" IDE_LIBRARY_BASENAME "/qtcreator");
engine->addImportPath(QDir::cleanPath(pluginPath));
facilitateQml(engine);
foreach (Core::IWelcomePage *plugin, plugins) {
plugin->facilitateQml(engine);
m_pluginList.append(plugin);
2011-04-13 17:09:44 +02:00
}
ctx->setContextProperty(QLatin1String("pagesModel"), QVariant::fromValue(m_pluginList));
onThemeChanged();
connect(Core::ICore::instance(), &Core::ICore::themeChanged, this, &WelcomeMode::onThemeChanged);
ctx->setContextProperty(QLatin1String("creatorTheme"), &m_themeProperties);
Implement theming for QtCreator Adds a 'Theme' tab to the environment settings and a '-theme' command line option. A theme is a combination of colors, gradients, flags and style information. There are two themes: - 'default': preserves the current default look - 'dark': uses a more flat for many widgets, dark color theme for everything This does not use a stylesheet (too limited), but rather sets the palette via C++ and modifies drawing behavior. Overall, the look is more flat (removed some gradients and bevels). Tested on Ubuntu 14.04 using Qt 5.4 and running on a KDE Desktop (Oxygen base style). For a screenshot, see https://gist.github.com/thorbenk/5ab06bea726de0aa7473 Changes: - Introduce class Theme, defining the interface how to access theme specific settings. The class reads a .creatortheme file (INI file, via QSettings) - Define named colors in the [Palette] section (see dark.creatortheme for example usage) - Use either named colors of AARRGGBB (hex) in the [Colors] section - A file ending with .creatortheme may be supplied to the '-theme' command line option - A global Theme instance can be accessed via creatorTheme() - Query colors, gradients, icons and flags from the theme were possible (TODO: use this in more places...) - There are very many color roles. It seems better to me to describe the role clearly, and then to consolidate later in the actual theme by assigning the same color. For example, one can set the text color of the output pane button individualy. - Many elements are also drawn differently. For the dark theme, I wanted to have a flatter look. - Introduce Theme::WidgetStyle enum, for now {Original, Flat}. - The theme specifies which kind of widget style it wants. - The drawing code queries the theme's style flag and switches between the original, gradient based look and the new, flat look. - Create some custom icons which look better on dark background (wip, currently folder/file icons) - Let ManhattanStyle draw some elements for non-panelwidgets, too (open/close arrows in QTreeView, custom folder/file icons) - For the welcomescreen, pass the WelcomeTheme class. WelcomeTheme exposes theme colors as Q_PROPERTY accessible from .qml - Themes can be modified via the 'Themes' tab in the environment settings. TODO: * Unify image handling * Avoid style name references * Fix gradients Change-Id: I92c2050ab0fb327649ea1eff4adec973d2073944 Reviewed-by: Thomas Hartmann <Thomas.Hartmann@digia.com> Reviewed-by: hjk <hjk121@nokiamail.com>
2014-10-14 19:09:48 +02:00
#if defined(USE_QUICK_WIDGET)
bool useNativeText = !Utils::HostOsInfo::isMacHost();
#else
bool useNativeText = true;
#endif
ctx->setContextProperty(QLatin1String("useNativeText"), useNativeText);
QString path = resourcePath() + QLatin1String("/welcomescreen/welcomescreen.qml");
// finally, load the root page
m_welcomePage->setSource(
QUrl::fromLocalFile(path));
2011-04-13 17:09:44 +02:00
}
void WelcomeMode::welcomePluginAdded(QObject *obj)
{
QHash<Core::Id, Core::IWelcomePage*> pluginHash;
foreach (QObject *obj, m_pluginList) {
Core::IWelcomePage *plugin = qobject_cast<Core::IWelcomePage*>(obj);
pluginHash.insert(plugin->id(), plugin);
}
if (Core::IWelcomePage *plugin = qobject_cast<Core::IWelcomePage*>(obj)) {
//check for duplicated id
if (pluginHash.contains(plugin->id())) {
Core::IWelcomePage* pluginOther = pluginHash.value(plugin->id());
if (pluginOther->priority() > plugin->priority())
m_pluginList.removeAll(pluginOther);
else
return;
}
int insertPos = 0;
foreach (Core::IWelcomePage* p, PluginManager::getObjects<Core::IWelcomePage>()) {
if (plugin->priority() < p->priority())
insertPos++;
else
break;
}
m_pluginList.insert(insertPos, plugin);
// update model through reset
QQmlContext *ctx = m_welcomePage->rootContext();
ctx->setContextProperty(QLatin1String("pagesModel"), QVariant::fromValue(m_pluginList));
2011-04-13 17:09:44 +02:00
}
}
WelcomePlugin::WelcomePlugin()
: m_welcomeMode(0)
2009-07-20 19:08:09 +02:00
{
}
/*! Initializes the plugin. Returns true on success.
Plugins want to register objects with the plugin manager here.
\a errorMessage can be used to pass an error message to the plugin system,
2009-07-20 19:08:09 +02:00
if there was any.
*/
bool WelcomePlugin::initialize(const QStringList & /* arguments */, QString * /* errorMessage */)
2009-07-20 19:08:09 +02:00
{
m_welcomeMode = new WelcomeMode;
addAutoReleasedObject(m_welcomeMode);
2009-07-20 19:08:09 +02:00
return true;
}
/*! Notification that all extensions that this plugin depends on have been
initialized. The dependencies are defined in the plugins .qwp file.
Normally this function is used for things that rely on other plugins to have
2009-07-20 19:08:09 +02:00
added objects to the plugin manager, that implement interfaces that we're
interested in. These objects can now be requested through the
PluginManagerInterface.
The WelcomePlugin doesn't need things from other plugins, so it does
nothing here.
*/
void WelcomePlugin::extensionsInitialized()
{
m_welcomeMode->initPlugins();
Core::ModeManager::activateMode(m_welcomeMode->id());
2009-07-20 19:08:09 +02:00
}
2011-04-13 17:09:44 +02:00
} // namespace Internal
} // namespace Welcome
2011-04-13 17:09:44 +02:00
#include "welcomeplugin.moc"