Core: Modernize FancyToolButton and FancyTabWidget

I got much help from clang-tidy, clazy and clang-format.

In preparation of QTCREATORBUG-18845

Change-Id: Ie881efd5093d86dc8a345e0a5badd93a6ccecbb9
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Alessandro Portale
2018-02-20 18:11:23 +01:00
parent 0172631b48
commit 84e17dd0f8
4 changed files with 224 additions and 217 deletions

View File

@@ -24,24 +24,25 @@
****************************************************************************/
#include "fancyactionbar.h"
#include "coreconstants.h"
#include <utils/hostosinfo.h>
#include <utils/stylehelper.h>
#include <utils/stringutils.h>
#include <utils/tooltip/tooltip.h>
#include <utils/stylehelper.h>
#include <utils/theme/theme.h>
#include <utils/tooltip/tooltip.h>
#include <QPainter>
#include <QVBoxLayout>
#include <QAction>
#include <QStyle>
#include <QStyleOption>
#include <QMouseEvent>
#include <QDebug>
#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QPixmapCache>
#include <QPropertyAnimation>
#include <QDebug>
#include <QStyle>
#include <QStyleOption>
#include <QVBoxLayout>
using namespace Utils;
@@ -49,7 +50,7 @@ namespace Core {
namespace Internal {
FancyToolButton::FancyToolButton(QAction *action, QWidget *parent)
: QToolButton(parent), m_fader(0)
: QToolButton(parent)
{
setDefaultAction(action);
connect(action, &QAction::changed, this, &FancyToolButton::actionChanged);
@@ -62,35 +63,31 @@ FancyToolButton::FancyToolButton(QAction *action, QWidget *parent)
bool FancyToolButton::event(QEvent *e)
{
switch (e->type()) {
case QEvent::Enter:
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "fader");
animation->setDuration(125);
animation->setEndValue(1.0);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
break;
case QEvent::Leave:
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "fader");
animation->setDuration(125);
animation->setEndValue(0.0);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
break;
case QEvent::ToolTip:
{
QHelpEvent *he = static_cast<QHelpEvent *>(e);
ToolTip::show(mapToGlobal(he->pos()), toolTip(), this);
return true;
}
default:
return QToolButton::event(e);
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;
}
return false;
default:
break;
}
return QToolButton::event(e);
}
static QVector<QString> splitInTwoLines(const QString &text, const QFontMetrics &fontMetrics,
static QVector<QString> splitInTwoLines(const QString &text,
const QFontMetrics &fontMetrics,
qreal availableWidth)
{
// split in two lines.
@@ -98,12 +95,11 @@ static QVector<QString> splitInTwoLines(const QString &text, const QFontMetrics
// 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);
QRegExp rx(QLatin1String("\\s+"));
const QRegExp rx(QLatin1String("\\s+"));
int splitPos = -1;
int nextSplitPos = text.length();
do {
nextSplitPos = rx.lastIndexIn(text,
nextSplitPos - text.length() - 1);
nextSplitPos = rx.lastIndexIn(text, nextSplitPos - text.length() - 1);
if (nextSplitPos != -1) {
int splitCandidate = nextSplitPos + rx.matchedLength();
if (fontMetrics.width(text.mid(splitCandidate)) <= availableWidth)
@@ -114,8 +110,7 @@ static QVector<QString> splitInTwoLines(const QString &text, const QFontMetrics
} 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,
availableWidth);
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
@@ -126,7 +121,9 @@ static QVector<QString> splitInTwoLines(const QString &text, const QFontMetrics
}
splitLines[1] = QChar(0x2026) /*'...'*/ + splitLines[1];
} else {
splitLines[0] = fontMetrics.elidedText(text.left(splitPos).trimmed(), Qt::ElideRight, availableWidth);
splitLines[0] = fontMetrics.elidedText(text.left(splitPos).trimmed(),
Qt::ElideRight,
int(availableWidth));
splitLines[1] = text.mid(splitPos);
}
return splitLines;
@@ -138,11 +135,10 @@ void FancyToolButton::paintEvent(QPaintEvent *event)
QPainter painter(this);
// draw borders
bool isTitledAction = defaultAction()->property("titledAction").toBool();
const bool isTitledAction = defaultAction()->property("titledAction").toBool();
if (!HostOsInfo::isMacHost() // Mac UIs usually don't hover
&& m_fader > 0 && isEnabled() && !isDown() && !isChecked()) {
&& m_fader > 0 && isEnabled() && !isDown() && !isChecked()) {
painter.save();
if (creatorTheme()->flag(Theme::FlatToolBars)) {
const QColor hoverColor = creatorTheme()->color(Theme::FancyToolButtonHoverColor);
@@ -169,79 +165,83 @@ void FancyToolButton::paintEvent(QPaintEvent *event)
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.topLeft() + QPointF(0, 1),
borderRectF.topRight() + QPointF(0, 1));
painter.drawLine(borderRectF.bottomLeft(), borderRectF.bottomRight());
painter.drawLine(borderRectF.bottomLeft(), borderRectF.bottomRight());
}
painter.restore();
}
const QIcon::Mode iconMode = isEnabled() ? ((isDown() || isChecked()) ? QIcon::Active : QIcon::Normal)
: QIcon::Disabled;
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);
// draw popup texts
if (isTitledAction) {
QFont normalFont(painter.font());
QRect centerRect = rect();
normalFont.setPointSizeF(StyleHelper::sidebarFontSize());
QFont boldFont(normalFont);
boldFont.setBold(true);
QFontMetrics fm(normalFont);
QFontMetrics boldFm(boldFont);
int lineHeight = boldFm.height();
int textFlags = Qt::AlignVCenter|Qt::AlignHCenter;
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);
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);
QPoint textOffset = centerRect.center()
- QPoint(iconRect.width() / 2, iconRect.height() / 2);
textOffset = textOffset - QPoint(0, lineHeight + 3);
QRectF r(0, textOffset.y(), rect().width(), lineHeight);
painter.setPen(creatorTheme()->color(isEnabled()
? Theme::PanelTextColorLight
: Theme::IconsDisabledColor));
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;
QString ellidedProjectName = fm.elidedText(projectName, Qt::ElideMiddle, availableWidth);
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);
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) {
if (boldFm.width(buildConfiguration) <= availableWidth)
// text fits in one line
splitBuildConfiguration[0] = buildConfiguration;
} else {
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));
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) {
if (splitBuildConfiguration[i].isEmpty())
const QString &buildConfigText = splitBuildConfiguration[i];
if (buildConfigText.isEmpty())
continue;
painter.drawText(buildConfigRect[i], textFlags, splitBuildConfiguration[i]);
painter.drawText(buildConfigRect[i], textFlags, buildConfigText);
}
// pop up arrow next to icon
@@ -271,7 +271,8 @@ void FancyActionBar::paintEvent(QPaintEvent *event)
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));
painter.drawLine(borderRect.topLeft() + QPointF(1, 1),
borderRect.topRight() + QPointF(0, 1));
}
}
@@ -282,28 +283,28 @@ QSize FancyToolButton::sizeHint() const
QFont boldFont(font());
boldFont.setPointSizeF(StyleHelper::sidebarFontSize());
boldFont.setBold(true);
QFontMetrics fm(boldFont);
qreal lineHeight = fm.height();
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);
buttonSize += QSizeF(0, lineHeight * 2 + 2);
}
return buttonSize.toSize();
}
QSize FancyToolButton::minimumSizeHint() const
{
return QSize(8, 8);
return {8, 8};
}
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());
+ QLatin1Char('x') + QString::number(logicalSize.height());
QPixmap overlay;
if (!QPixmapCache::find(cacheKey, &overlay)) {
const int dpr = painter->device()->devicePixelRatio();
@@ -336,29 +337,28 @@ 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())
if (QAction *action = defaultAction())
setVisible(action->isVisible());
}
FancyActionBar::FancyActionBar(QWidget *parent)
: QWidget(parent)
{
setObjectName(QLatin1String("actionbar"));
setObjectName("actionbar");
m_actionsLayout = new QVBoxLayout;
QVBoxLayout *spacerLayout = new QVBoxLayout;
auto spacerLayout = new QVBoxLayout;
spacerLayout->addLayout(m_actionsLayout);
int sbh = 8;
const int sbh = 8;
spacerLayout->addSpacing(sbh);
spacerLayout->setMargin(0);
spacerLayout->setSpacing(0);
setLayout(spacerLayout);
setContentsMargins(0,2,0,0);
setContentsMargins(0, 2, 0, 0);
}
void FancyActionBar::addProjectSelector(QAction *action)
{
m_actionsLayout->insertWidget(0, new FancyToolButton(action, this));
}
void FancyActionBar::insertAction(int index, QAction *action)
{