Files
qt-creator/src/plugins/coreplugin/fancyactionbar.cpp

404 lines
16 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator.
2008-12-02 12:01:29 +01: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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
2010-12-17 16:01:08 +01:00
**
****************************************************************************/
2008-12-02 14:09:21 +01:00
2008-12-02 12:01:29 +01:00
#include "fancyactionbar.h"
#include "coreconstants.h"
2008-12-02 12:01:29 +01:00
#include <utils/hostosinfo.h>
#include <utils/stringutils.h>
#include <utils/stylehelper.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 <utils/tooltip/tooltip.h>
#include <QAction>
#include <QDebug>
#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QPixmapCache>
#include <QPropertyAnimation>
#include <QStyle>
#include <QStyleOption>
#include <QVBoxLayout>
2008-12-02 12:01:29 +01:00
using namespace Utils;
namespace Core {
namespace Internal {
2008-12-02 12:01:29 +01:00
FancyToolButton::FancyToolButton(QAction *action, QWidget *parent)
: QToolButton(parent)
2008-12-02 12:01:29 +01:00
{
setDefaultAction(action);
connect(action, &QAction::changed, this, &FancyToolButton::actionChanged);
actionChanged();
2008-12-02 12:01:29 +01:00
setAttribute(Qt::WA_Hover, true);
2009-07-13 18:04:08 +02:00
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
2008-12-02 12:01:29 +01:00
}
bool FancyToolButton::event(QEvent *e)
{
switch (e->type()) {
case QEvent::Enter: {
auto animation = new QPropertyAnimation(this, "fader");
animation->setDuration(125);
animation->setEndValue(1.0);
animation->start(QAbstractAnimation::DeleteWhenStopped);
} break;
case QEvent::Leave: {
auto animation = new QPropertyAnimation(this, "fader");
animation->setDuration(125);
animation->setEndValue(0.0);
animation->start(QAbstractAnimation::DeleteWhenStopped);
} break;
case QEvent::ToolTip: {
auto he = static_cast<QHelpEvent *>(e);
ToolTip::show(mapToGlobal(he->pos()), toolTip(), this);
return true;
}
default:
break;
}
return QToolButton::event(e);
}
static QVector<QString> splitInTwoLines(const QString &text,
const QFontMetrics &fontMetrics,
qreal availableWidth)
{
// split in two lines.
// this looks if full words can be split off at the end of the string,
// to put them in the second line. First line is drawn with ellipsis,
// second line gets ellipsis if it couldn't split off full words.
QVector<QString> splitLines(2);
const QRegExp rx(QLatin1String("\\s+"));
int splitPos = -1;
int nextSplitPos = text.length();
do {
nextSplitPos = rx.lastIndexIn(text, nextSplitPos - text.length() - 1);
if (nextSplitPos != -1) {
int splitCandidate = nextSplitPos + rx.matchedLength();
if (fontMetrics.width(text.mid(splitCandidate)) <= availableWidth)
splitPos = splitCandidate;
else
break;
}
} while (nextSplitPos > 0 && fontMetrics.width(text.left(nextSplitPos)) > availableWidth);
// check if we could split at white space at all
if (splitPos < 0) {
splitLines[0] = fontMetrics.elidedText(text, Qt::ElideRight, int(availableWidth));
QString common = Utils::commonPrefix(QStringList({splitLines[0], text}));
splitLines[1] = text.mid(common.length());
// elide the second line even if it fits, since it is cut off in mid-word
while (fontMetrics.width(QChar(0x2026) /*'...'*/ + splitLines[1]) > availableWidth
&& splitLines[1].length() > 3
/*keep at least three original characters (should not happen)*/) {
splitLines[1].remove(0, 1);
}
splitLines[1] = QChar(0x2026) /*'...'*/ + splitLines[1];
} else {
splitLines[0] = fontMetrics.elidedText(text.left(splitPos).trimmed(),
Qt::ElideRight,
int(availableWidth));
splitLines[1] = text.mid(splitPos);
}
return splitLines;
}
2008-12-02 12:01:29 +01:00
void FancyToolButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
// draw borders
if (!HostOsInfo::isMacHost() // Mac UIs usually don't hover
&& m_fader > 0 && isEnabled() && !isDown() && !isChecked()) {
painter.save();
if (creatorTheme()->flag(Theme::FlatToolBars)) {
const QColor hoverColor = creatorTheme()->color(Theme::FancyToolButtonHoverColor);
QColor fadedHoverColor = hoverColor;
fadedHoverColor.setAlpha(int(m_fader * hoverColor.alpha()));
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
painter.fillRect(rect(), fadedHoverColor);
} else {
painter.setOpacity(m_fader);
FancyToolButton::hoverOverlay(&painter, rect());
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
}
painter.restore();
} else if (isDown() || isChecked()) {
painter.save();
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
const QColor selectedColor = creatorTheme()->color(Theme::FancyToolButtonSelectedColor);
if (creatorTheme()->flag(Theme::FlatToolBars)) {
painter.fillRect(rect(), selectedColor);
} else {
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
QLinearGradient grad(rect().topLeft(), rect().topRight());
grad.setColorAt(0, Qt::transparent);
grad.setColorAt(0.5, selectedColor);
grad.setColorAt(1, Qt::transparent);
painter.fillRect(rect(), grad);
painter.setPen(QPen(grad, 1.0));
const QRectF borderRectF(QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5));
painter.drawLine(borderRectF.topLeft(), borderRectF.topRight());
painter.drawLine(borderRectF.topLeft(), borderRectF.topRight());
painter.drawLine(borderRectF.topLeft() + QPointF(0, 1),
borderRectF.topRight() + QPointF(0, 1));
painter.drawLine(borderRectF.bottomLeft(), borderRectF.bottomRight());
painter.drawLine(borderRectF.bottomLeft(), borderRectF.bottomRight());
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
}
painter.restore();
}
const QIcon::Mode iconMode = isEnabled()
? ((isDown() || isChecked()) ? QIcon::Active : QIcon::Normal)
: QIcon::Disabled;
QRect iconRect(0, 0, Constants::MODEBAR_ICON_SIZE, Constants::MODEBAR_ICON_SIZE);
const bool isTitledAction = defaultAction()->property("titledAction").toBool();
// draw popup texts
if (isTitledAction && !m_iconsOnly) {
QFont normalFont(painter.font());
QRect centerRect = rect();
normalFont.setPointSizeF(StyleHelper::sidebarFontSize());
QFont boldFont(normalFont);
boldFont.setBold(true);
const QFontMetrics fm(normalFont);
const QFontMetrics boldFm(boldFont);
const int lineHeight = boldFm.height();
const int textFlags = Qt::AlignVCenter | Qt::AlignHCenter;
const QString projectName = defaultAction()->property("heading").toString();
if (!projectName.isNull())
centerRect.adjust(0, lineHeight + 4, 0, 0);
centerRect.adjust(0, 0, 0, -lineHeight * 2 - 4);
iconRect.moveCenter(centerRect.center());
StyleHelper::drawIconWithShadow(icon(), iconRect, &painter, iconMode);
painter.setFont(normalFont);
QPoint textOffset = centerRect.center()
- QPoint(iconRect.width() / 2, iconRect.height() / 2);
textOffset = textOffset - QPoint(0, lineHeight + 3);
const QRectF r(0, textOffset.y(), rect().width(), lineHeight);
painter.setPen(creatorTheme()->color(isEnabled() ? Theme::PanelTextColorLight
: Theme::IconsDisabledColor));
// draw project name
const int margin = 6;
const qreal availableWidth = r.width() - margin;
const QString ellidedProjectName = fm.elidedText(projectName,
Qt::ElideMiddle,
int(availableWidth));
painter.drawText(r, textFlags, ellidedProjectName);
// draw build configuration name
textOffset = iconRect.center() + QPoint(iconRect.width() / 2, iconRect.height() / 2);
QRectF buildConfigRect[2];
buildConfigRect[0] = QRectF(0, textOffset.y() + 4, rect().width(), lineHeight);
buildConfigRect[1] = QRectF(0, textOffset.y() + 4 + lineHeight, rect().width(), lineHeight);
painter.setFont(boldFont);
QVector<QString> splitBuildConfiguration(2);
const QString buildConfiguration = defaultAction()->property("subtitle").toString();
if (boldFm.width(buildConfiguration) <= availableWidth)
// text fits in one line
splitBuildConfiguration[0] = buildConfiguration;
else
splitBuildConfiguration = splitInTwoLines(buildConfiguration, boldFm, availableWidth);
// draw the two text lines for the build configuration
painter.setPen(
creatorTheme()->color(isEnabled()
// Intentionally using the "Unselected" colors,
// because the text color won't change in the pressed
// state as they would do on the mode buttons.
? Theme::FancyTabWidgetEnabledUnselectedTextColor
: Theme::FancyTabWidgetDisabledUnselectedTextColor));
for (int i = 0; i < 2; ++i) {
const QString &buildConfigText = splitBuildConfiguration[i];
if (buildConfigText.isEmpty())
continue;
painter.drawText(buildConfigRect[i], textFlags, buildConfigText);
}
} else {
iconRect.moveCenter(rect().center());
StyleHelper::drawIconWithShadow(icon(), iconRect, &painter, iconMode);
2008-12-02 12:01:29 +01:00
}
// pop up arrow next to icon
if (isTitledAction && isEnabled() && !icon().isNull()) {
QStyleOption opt;
opt.initFrom(this);
opt.rect = rect().adjusted(rect().width() -
(m_iconsOnly ? 6 : 16), 0, -(m_iconsOnly ? 0 : 8), 0);
StyleHelper::drawArrow(QStyle::PE_IndicatorArrowRight, &painter, &opt);
}
2008-12-02 12:01:29 +01:00
}
void FancyActionBar::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
const QRectF borderRect = QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5);
if (creatorTheme()->flag(Theme::FlatToolBars)) {
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
// this paints the background of the bottom portion of the
// left tab bar
painter.fillRect(event->rect(), StyleHelper::baseColor());
painter.setPen(creatorTheme()->color(Theme::FancyToolBarSeparatorColor));
painter.drawLine(borderRect.topLeft(), borderRect.topRight());
} else {
painter.setPen(StyleHelper::sidebarShadow());
painter.drawLine(borderRect.topLeft(), borderRect.topRight());
painter.setPen(StyleHelper::sidebarHighlight());
painter.drawLine(borderRect.topLeft() + QPointF(1, 1),
borderRect.topRight() + QPointF(0, 1));
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
}
2008-12-02 12:01:29 +01:00
}
2008-12-02 12:01:29 +01:00
QSize FancyToolButton::sizeHint() const
{
if (m_iconsOnly) {
return {Core::Constants::MODEBAR_ICONSONLY_BUTTON_SIZE,
Core::Constants::MODEBAR_ICONSONLY_BUTTON_SIZE};
}
QSizeF buttonSize = iconSize().expandedTo(QSize(64, 38));
if (defaultAction()->property("titledAction").toBool()) {
QFont boldFont(font());
boldFont.setPointSizeF(StyleHelper::sidebarFontSize());
boldFont.setBold(true);
const QFontMetrics fm(boldFont);
const qreal lineHeight = fm.height();
const QString projectName = defaultAction()->property("heading").toString();
buttonSize += QSizeF(0, 10);
if (!projectName.isEmpty())
buttonSize += QSizeF(0, lineHeight + 2);
buttonSize += QSizeF(0, lineHeight * 2 + 2);
}
return buttonSize.toSize();
2008-12-02 12:01:29 +01:00
}
2009-07-13 18:04:08 +02:00
QSize FancyToolButton::minimumSizeHint() const
{
return {8, 8};
2009-07-13 18:04:08 +02:00
}
void FancyToolButton::setIconsOnly(bool iconsOnly)
{
m_iconsOnly = iconsOnly;
updateGeometry();
}
void FancyToolButton::hoverOverlay(QPainter *painter, const QRect &spanRect)
{
const QSize logicalSize = spanRect.size();
const QString cacheKey = QLatin1String(Q_FUNC_INFO) + QString::number(logicalSize.width())
+ QLatin1Char('x') + QString::number(logicalSize.height());
QPixmap overlay;
if (!QPixmapCache::find(cacheKey, &overlay)) {
const int dpr = painter->device()->devicePixelRatio();
overlay = QPixmap(logicalSize * dpr);
overlay.fill(Qt::transparent);
overlay.setDevicePixelRatio(dpr);
const QColor hoverColor = creatorTheme()->color(Theme::FancyToolButtonHoverColor);
const QRect rect(QPoint(), logicalSize);
const QRectF borderRect = QRectF(rect).adjusted(0.5, 0.5, -0.5, -0.5);
QLinearGradient grad(rect.topLeft(), rect.topRight());
grad.setColorAt(0, Qt::transparent);
grad.setColorAt(0.5, hoverColor);
grad.setColorAt(1, Qt::transparent);
QPainter p(&overlay);
p.fillRect(rect, grad);
p.setPen(QPen(grad, 1.0));
p.drawLine(borderRect.topLeft(), borderRect.topRight());
p.drawLine(borderRect.bottomLeft(), borderRect.bottomRight());
p.end();
QPixmapCache::insert(cacheKey, overlay);
}
painter->drawPixmap(spanRect.topLeft(), overlay);
}
void FancyToolButton::actionChanged()
{
// the default action changed in some way, e.g. it might got hidden
// since we inherit a tool button we won't get invisible, so do this here
if (QAction *action = defaultAction())
setVisible(action->isVisible());
}
2008-12-02 12:01:29 +01:00
FancyActionBar::FancyActionBar(QWidget *parent)
: QWidget(parent)
{
setObjectName("actionbar");
2008-12-02 12:01:29 +01:00
m_actionsLayout = new QVBoxLayout;
m_actionsLayout->setMargin(0);
m_actionsLayout->setSpacing(0);
setLayout(m_actionsLayout);
setContentsMargins(0, 2, 0, 8);
2008-12-02 12:01:29 +01:00
}
void FancyActionBar::addProjectSelector(QAction *action)
{
insertAction(0, action);
}
void FancyActionBar::insertAction(int index, QAction *action)
2008-12-02 12:01:29 +01:00
{
auto *button = new FancyToolButton(action, this);
if (!action->objectName().isEmpty())
button->setObjectName(action->objectName() + ".Button"); // used for UI introduction
button->setIconsOnly(m_iconsOnly);
m_actionsLayout->insertWidget(index, button);
2008-12-02 12:01:29 +01:00
}
QLayout *FancyActionBar::actionsLayout() const
{
return m_actionsLayout;
}
QSize FancyActionBar::minimumSizeHint() const
{
return sizeHint();
}
void FancyActionBar::setIconsOnly(bool iconsOnly)
{
m_iconsOnly = iconsOnly;
for (int i = 0, c = m_actionsLayout->count(); i < c; ++i) {
if (auto *button = qobject_cast<FancyToolButton*>(m_actionsLayout->itemAt(i)->widget()))
button->setIconsOnly(iconsOnly);
}
setContentsMargins(0, iconsOnly ? 7 : 2, 0, iconsOnly ? 2 : 8);
}
} // namespace Internal
} // namespace Core