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)
{

View File

@@ -38,24 +38,29 @@ class FancyToolButton : public QToolButton
{
Q_OBJECT
Q_PROPERTY(float fader READ fader WRITE setFader)
Q_PROPERTY(qreal fader READ fader WRITE setFader CONSTANT)
public:
FancyToolButton(QAction *action, QWidget *parent = 0);
FancyToolButton(QAction *action, QWidget *parent = nullptr);
void paintEvent(QPaintEvent *event);
bool event(QEvent *e);
QSize sizeHint() const;
QSize minimumSizeHint() const;
void paintEvent(QPaintEvent *event) override;
bool event(QEvent *e) override;
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
float m_fader;
float fader() { return m_fader; }
void setFader(float value) { m_fader = value; update(); }
qreal fader() const { return m_fader; }
void setFader(qreal value)
{
m_fader = value;
update();
}
static void hoverOverlay(QPainter *painter, const QRect &spanRect);
private:
void actionChanged();
qreal m_fader = 0;
};
class FancyActionBar : public QWidget
@@ -63,13 +68,13 @@ class FancyActionBar : public QWidget
Q_OBJECT
public:
FancyActionBar(QWidget *parent = 0);
FancyActionBar(QWidget *parent = nullptr);
void paintEvent(QPaintEvent *event);
void paintEvent(QPaintEvent *event) override;
void insertAction(int index, QAction *action);
void addProjectSelector(QAction *action);
QLayout *actionsLayout() const;
QSize minimumSizeHint() const;
QSize minimumSizeHint() const override;
private:
QVBoxLayout *m_actionsLayout;

View File

@@ -24,26 +24,27 @@
****************************************************************************/
#include "fancytabwidget.h"
#include "fancyactionbar.h"
#include "coreconstants.h"
#include "coreconstants.h"
#include "fancyactionbar.h"
#include <utils/asconst.h>
#include <utils/hostosinfo.h>
#include <utils/stylehelper.h>
#include <utils/styledbar.h>
#include <utils/stylehelper.h>
#include <utils/theme/theme.h>
#include <QDebug>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QStyleFactory>
#include <QPainter>
#include <QPixmapCache>
#include <QStackedLayout>
#include <QStatusBar>
#include <QStyleFactory>
#include <QStyleOption>
#include <QToolTip>
#include <QVBoxLayout>
using namespace Core;
using namespace Internal;
@@ -53,31 +54,29 @@ static const int kMenuButtonWidth = 16;
void FancyTab::fadeIn()
{
animator.stop();
animator.setDuration(80);
animator.setEndValue(1);
animator.start();
m_animator.stop();
m_animator.setDuration(80);
m_animator.setEndValue(1);
m_animator.start();
}
void FancyTab::fadeOut()
{
animator.stop();
animator.setDuration(160);
animator.setEndValue(0);
animator.start();
m_animator.stop();
m_animator.setDuration(160);
m_animator.setEndValue(0);
m_animator.start();
}
void FancyTab::setFader(float value)
void FancyTab::setFader(qreal value)
{
m_fader = value;
tabbar->update();
m_tabbar->update();
}
FancyTabBar::FancyTabBar(QWidget *parent)
: QWidget(parent)
{
m_hoverIndex = -1;
m_currentIndex = -1;
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
setMinimumWidth(44);
setAttribute(Qt::WA_Hover, true);
@@ -90,17 +89,17 @@ QSize FancyTabBar::tabSizeHint(bool minimum) const
QFont boldFont(font());
boldFont.setPointSizeF(StyleHelper::sidebarFontSize());
boldFont.setBold(true);
QFontMetrics fm(boldFont);
int spacing = 8;
int width = 60 + spacing + 2;
const QFontMetrics fm(boldFont);
const int spacing = 8;
const int width = 60 + spacing + 2;
int maxLabelwidth = 0;
for (int tab=0 ; tab<count() ;++tab) {
int width = fm.width(m_tabs.at(tab)->text);
for (auto tab : asConst(m_tabs)) {
const int width = fm.width(tab->text);
if (width > maxLabelwidth)
maxLabelwidth = width;
}
int iconHeight = minimum ? 0 : 32;
return QSize(qMax(width, maxLabelwidth + 4), iconHeight + spacing + fm.height());
const int iconHeight = minimum ? 0 : 32;
return {qMax(width, maxLabelwidth + 4), iconHeight + spacing + fm.height()};
}
void FancyTabBar::paintEvent(QPaintEvent *event)
@@ -122,12 +121,12 @@ void FancyTabBar::paintEvent(QPaintEvent *event)
}
// Handle hover events for mouse fade ins
void FancyTabBar::mouseMoveEvent(QMouseEvent *e)
void FancyTabBar::mouseMoveEvent(QMouseEvent *event)
{
int newHover = -1;
for (int i = 0; i < count(); ++i) {
QRect area = tabRect(i);
if (area.contains(e->pos())) {
const QRect area = tabRect(i);
if (area.contains(event->pos())) {
newHover = i;
break;
}
@@ -150,9 +149,9 @@ bool FancyTabBar::event(QEvent *event)
{
if (event->type() == QEvent::ToolTip) {
if (validIndex(m_hoverIndex)) {
QString tt = tabToolTip(m_hoverIndex);
const QString tt = tabToolTip(m_hoverIndex);
if (!tt.isEmpty()) {
QToolTip::showText(static_cast<QHelpEvent*>(event)->globalPos(), tt, this);
QToolTip::showText(static_cast<QHelpEvent *>(event)->globalPos(), tt, this);
return true;
}
}
@@ -161,34 +160,33 @@ bool FancyTabBar::event(QEvent *event)
}
// Resets hover animation on mouse enter
void FancyTabBar::enterEvent(QEvent *e)
void FancyTabBar::enterEvent(QEvent *event)
{
Q_UNUSED(e)
Q_UNUSED(event)
m_hoverRect = QRect();
m_hoverIndex = -1;
}
// Resets hover animation on mouse enter
void FancyTabBar::leaveEvent(QEvent *e)
void FancyTabBar::leaveEvent(QEvent *event)
{
Q_UNUSED(e)
Q_UNUSED(event)
m_hoverIndex = -1;
m_hoverRect = QRect();
for (int i = 0 ; i < m_tabs.count() ; ++i) {
m_tabs[i]->fadeOut();
}
for (auto tab : asConst(m_tabs))
tab->fadeOut();
}
QSize FancyTabBar::sizeHint() const
{
QSize sh = tabSizeHint();
return QSize(sh.width(), sh.height() * m_tabs.count());
const QSize sh = tabSizeHint();
return {sh.width(), sh.height() * m_tabs.count()};
}
QSize FancyTabBar::minimumSizeHint() const
{
QSize sh = tabSizeHint(true);
return QSize(sh.width(), sh.height() * m_tabs.count());
const QSize sh = tabSizeHint(true);
return {sh.width(), sh.height() * m_tabs.count()};
}
QRect FancyTabBar::tabRect(int index) const
@@ -198,20 +196,20 @@ QRect FancyTabBar::tabRect(int index) const
if (sh.height() * m_tabs.count() > height())
sh.setHeight(height() / m_tabs.count());
return QRect(0, index * sh.height(), sh.width(), sh.height());
return {0, index * sh.height(), sh.width(), sh.height()};
}
void FancyTabBar::mousePressEvent(QMouseEvent *e)
void FancyTabBar::mousePressEvent(QMouseEvent *event)
{
e->accept();
event->accept();
for (int index = 0; index < m_tabs.count(); ++index) {
const QRect rect = tabRect(index);
if (rect.contains(e->pos())) {
if (rect.contains(event->pos())) {
if (isTabEnabled(index)) {
if (m_tabs.at(index)->hasMenu && rect.right() - e->pos().x() <= kMenuButtonWidth) {
if (m_tabs.at(index)->hasMenu
&& rect.right() - event->pos().x() <= kMenuButtonWidth) {
// menu arrow clicked
emit menuTriggered(index, e);
emit menuTriggered(index, event);
} else {
m_currentIndex = index;
update();
@@ -229,8 +227,8 @@ static void paintSelectedTabBackground(QPainter *painter, const QRect &spanRect)
const int verticalOverlap = 2; // Grows up and down for the overlaps
const int dpr = painter->device()->devicePixelRatio();
const QString cacheKey = QLatin1String(Q_FUNC_INFO) + QString::number(spanRect.width())
+ QLatin1Char('x') + QString::number(spanRect.height())
+ QLatin1Char('@') + QString::number(dpr);
+ QLatin1Char('x') + QString::number(spanRect.height())
+ QLatin1Char('@') + QString::number(dpr);
QPixmap selection;
if (!QPixmapCache::find(cacheKey, &selection)) {
selection = QPixmap(QSize(spanRect.width(), spanRect.height() + 2 * verticalOverlap) * dpr);
@@ -260,11 +258,13 @@ static void paintSelectedTabBackground(QPainter *painter, const QRect &spanRect)
//highlights
p.setPen(QColor(255, 255, 255, 50));
p.drawLine(borderRect.topLeft() + QPointF(0, -2), borderRect.topRight() - QPointF(0, 2));
p.drawLine(borderRect.bottomLeft() + QPointF(0, 1), borderRect.bottomRight() + QPointF(0, 1));
p.drawLine(borderRect.bottomLeft() + QPointF(0, 1),
borderRect.bottomRight() + QPointF(0, 1));
p.setPen(QColor(255, 255, 255, 40));
p.drawLine(borderRect.topLeft() + QPointF(0, 0), borderRect.topRight());
p.drawLine(borderRect.topRight() + QPointF(0, 1), borderRect.bottomRight() - QPointF(0, 1));
p.drawLine(borderRect.bottomLeft() + QPointF(0, -1), borderRect.bottomRight() - QPointF(0, 1));
p.drawLine(borderRect.bottomLeft() + QPointF(0, -1),
borderRect.bottomRight() - QPointF(0, 1));
p.end();
QPixmapCache::insert(cacheKey, selection);
@@ -280,21 +280,21 @@ void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const
}
painter->save();
FancyTab *tab = m_tabs.at(tabIndex);
QRect rect = tabRect(tabIndex);
bool selected = (tabIndex == m_currentIndex);
bool enabled = isTabEnabled(tabIndex);
const FancyTab *tab = m_tabs.at(tabIndex);
const QRect rect = tabRect(tabIndex);
const bool selected = (tabIndex == m_currentIndex);
const bool enabled = isTabEnabled(tabIndex);
if (selected) {
if (creatorTheme()->flag(Theme::FlatToolBars)) {
// background color of a fancy tab that is active
painter->fillRect(rect, creatorTheme()->color(Theme::FancyToolButtonSelectedColor));
// background color of a fancy tab that is active
painter->fillRect(rect, creatorTheme()->color(Theme::FancyToolButtonSelectedColor));
} else {
paintSelectedTabBackground(painter, rect);
}
}
QString tabText(tab->text);
const QString tabText(tab->text);
QRect tabTextRect(rect);
const bool drawIcon = rect.height() > 36;
QRect tabIconRect(tabTextRect);
@@ -304,9 +304,10 @@ void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const
boldFont.setBold(true);
painter->setFont(boldFont);
painter->setPen(selected ? QColor(255, 255, 255, 160) : QColor(0, 0, 0, 110));
const int textFlags = Qt::AlignCenter | (drawIcon ? Qt::AlignBottom : Qt::AlignVCenter) | Qt::TextWordWrap;
const int textFlags = Qt::AlignCenter | (drawIcon ? Qt::AlignBottom : Qt::AlignVCenter)
| Qt::TextWordWrap;
const float fader = m_tabs[tabIndex]->fader();
const qreal fader = tab->fader();
if (fader > 0 && !HostOsInfo::isMacHost() && !selected && enabled) {
painter->save();
painter->setOpacity(fader);
@@ -321,7 +322,11 @@ void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const
painter->setOpacity(0.7);
if (drawIcon) {
int textHeight = painter->fontMetrics().boundingRect(QRect(0, 0, width(), height()), Qt::TextWordWrap, tabText).height();
const int textHeight = painter->fontMetrics()
.boundingRect(QRect(0, 0, width(), height()),
Qt::TextWordWrap,
tabText)
.height();
tabIconRect.adjust(0, 4, 0, -textHeight);
const QIcon::Mode iconMode = enabled ? (selected ? QIcon::Active : QIcon::Normal)
: QIcon::Disabled;
@@ -333,13 +338,13 @@ void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const
painter->setOpacity(1.0); //FIXME: was 0.7 before?
if (enabled) {
painter->setPen(selected
? creatorTheme()->color(Theme::FancyTabWidgetEnabledSelectedTextColor)
: creatorTheme()->color(Theme::FancyTabWidgetEnabledUnselectedTextColor));
painter->setPen(
selected ? creatorTheme()->color(Theme::FancyTabWidgetEnabledSelectedTextColor)
: creatorTheme()->color(Theme::FancyTabWidgetEnabledUnselectedTextColor));
} else {
painter->setPen(selected
? creatorTheme()->color(Theme::FancyTabWidgetDisabledSelectedTextColor)
: creatorTheme()->color(Theme::FancyTabWidgetDisabledUnselectedTextColor));
painter->setPen(
selected ? creatorTheme()->color(Theme::FancyTabWidgetDisabledSelectedTextColor)
: creatorTheme()->color(Theme::FancyTabWidgetDisabledUnselectedTextColor));
}
painter->translate(0, -1);
painter->drawText(tabTextRect, textFlags, tabText);
@@ -354,7 +359,8 @@ void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const
painter->restore();
}
void FancyTabBar::setCurrentIndex(int index) {
void FancyTabBar::setCurrentIndex(int index)
{
if (isTabEnabled(index) && index != m_currentIndex) {
m_currentIndex = index;
update();
@@ -384,35 +390,27 @@ bool FancyTabBar::isTabEnabled(int index) const
return false;
}
//////
// FancyColorButton
//////
class FancyColorButton : public QWidget
{
Q_OBJECT
public:
explicit FancyColorButton(QWidget *parent = 0)
: QWidget(parent)
explicit FancyColorButton(QWidget *parent = nullptr)
: QWidget(parent)
{
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
}
void mousePressEvent(QMouseEvent *ev)
{
emit clicked(ev->button(), ev->modifiers());
}
void mousePressEvent(QMouseEvent *ev) override { emit clicked(ev->button(), ev->modifiers()); }
void paintEvent(QPaintEvent *event)
void paintEvent(QPaintEvent *event) override
{
QWidget::paintEvent(event);
// Some Themes do not want highlights, shadows and borders in the toolbars.
// But we definitely want a separator between FancyColorButton and FancyTabBar
if (!creatorTheme()->flag(Theme::DrawToolBarHighlights)
&& !creatorTheme()->flag(Theme::DrawToolBarBorders)) {
&& !creatorTheme()->flag(Theme::DrawToolBarBorders)) {
QPainter p(this);
p.setPen(StyleHelper::toolBarBorderColor());
const QRectF innerRect = QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5);
@@ -434,12 +432,12 @@ FancyTabWidget::FancyTabWidget(QWidget *parent)
m_tabBar = new FancyTabBar(this);
m_selectionWidget = new QWidget(this);
QVBoxLayout *selectionLayout = new QVBoxLayout;
auto selectionLayout = new QVBoxLayout;
selectionLayout->setSpacing(0);
selectionLayout->setMargin(0);
StyledBar *bar = new StyledBar;
QHBoxLayout *layout = new QHBoxLayout(bar);
auto bar = new StyledBar;
auto layout = new QHBoxLayout(bar);
layout->setMargin(0);
layout->setSpacing(0);
auto fancyButton = new FancyColorButton(this);
@@ -455,7 +453,7 @@ FancyTabWidget::FancyTabWidget(QWidget *parent)
m_cornerWidgetContainer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
m_cornerWidgetContainer->setAutoFillBackground(false);
QVBoxLayout *cornerWidgetLayout = new QVBoxLayout;
auto cornerWidgetLayout = new QVBoxLayout;
cornerWidgetLayout->setSpacing(0);
cornerWidgetLayout->setMargin(0);
cornerWidgetLayout->addStretch();
@@ -467,13 +465,13 @@ FancyTabWidget::FancyTabWidget(QWidget *parent)
m_statusBar = new QStatusBar;
m_statusBar->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
QVBoxLayout *vlayout = new QVBoxLayout;
auto vlayout = new QVBoxLayout;
vlayout->setMargin(0);
vlayout->setSpacing(0);
vlayout->addLayout(m_modesStack);
vlayout->addWidget(m_statusBar);
QHBoxLayout *mainLayout = new QHBoxLayout;
auto mainLayout = new QHBoxLayout;
mainLayout->setMargin(0);
mainLayout->setSpacing(1);
mainLayout->addWidget(m_selectionWidget);
@@ -494,8 +492,7 @@ bool FancyTabWidget::isSelectionWidgetVisible() const
return m_selectionWidget->isVisible();
}
void FancyTabWidget::insertTab(int index, QWidget *tab, const QIcon &icon, const QString &label,
bool hasMenu)
void FancyTabWidget::insertTab(int index, QWidget *tab, const QIcon &icon, const QString &label, bool hasMenu)
{
m_modesStack->insertWidget(index, tab);
m_tabBar->insertTab(index, icon, label, hasMenu);
@@ -533,7 +530,7 @@ void FancyTabWidget::paintEvent(QPaintEvent *event)
painter.setPen(StyleHelper::borderColor());
painter.drawLine(boderRect.topRight(), boderRect.bottomRight());
QColor light = StyleHelper::sidebarHighlight();
const QColor light = StyleHelper::sidebarHighlight();
painter.setPen(light);
painter.drawLine(boderRect.bottomLeft(), boderRect.bottomRight());
}
@@ -542,7 +539,7 @@ void FancyTabWidget::paintEvent(QPaintEvent *event)
void FancyTabWidget::insertCornerWidget(int pos, QWidget *widget)
{
QVBoxLayout *layout = static_cast<QVBoxLayout *>(m_cornerWidgetContainer->layout());
auto layout = static_cast<QVBoxLayout *>(m_cornerWidgetContainer->layout());
layout->insertWidget(pos, widget);
}

View File

@@ -28,8 +28,8 @@
#include <QIcon>
#include <QWidget>
#include <QTimer>
#include <QPropertyAnimation>
#include <QTimer>
QT_BEGIN_NAMESPACE
class QPainter;
@@ -44,14 +44,18 @@ class FancyTab : public QObject
{
Q_OBJECT
Q_PROPERTY(float fader READ fader WRITE setFader)
Q_PROPERTY(qreal fader READ fader WRITE setFader CONSTANT)
public:
FancyTab(QWidget *tabbar) : tabbar(tabbar){
animator.setPropertyName("fader");
animator.setTargetObject(this);
FancyTab(QWidget *tabbar)
: m_tabbar(tabbar)
{
m_animator.setPropertyName("fader");
m_animator.setTargetObject(this);
}
float fader() { return m_fader; }
void setFader(float value);
qreal fader() const { return m_fader; }
void setFader(qreal qreal);
void fadeIn();
void fadeOut();
@@ -63,9 +67,9 @@ public:
bool hasMenu = false;
private:
QPropertyAnimation animator;
QWidget *tabbar;
float m_fader = 0;
QPropertyAnimation m_animator;
QWidget *m_tabbar;
qreal m_fader = 0;
};
class FancyTabBar : public QWidget
@@ -73,26 +77,27 @@ class FancyTabBar : public QWidget
Q_OBJECT
public:
FancyTabBar(QWidget *parent = 0);
FancyTabBar(QWidget *parent = nullptr);
bool event(QEvent *event);
bool event(QEvent *event) override;
void paintEvent(QPaintEvent *event);
void paintEvent(QPaintEvent *event) override;
void paintTab(QPainter *painter, int tabIndex) const;
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void enterEvent(QEvent *);
void leaveEvent(QEvent *);
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void enterEvent(QEvent *event) override;
void leaveEvent(QEvent *event) override;
bool validIndex(int index) const { return index >= 0 && index < m_tabs.count(); }
QSize sizeHint() const;
QSize minimumSizeHint() const;
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
void setTabEnabled(int index, bool enable);
bool isTabEnabled(int index) const;
void insertTab(int index, const QIcon &icon, const QString &label, bool hasMenu) {
FancyTab *tab = new FancyTab(this);
void insertTab(int index, const QIcon &icon, const QString &label, bool hasMenu)
{
auto tab = new FancyTab(this);
tab->icon = icon;
tab->text = label;
tab->hasMenu = hasMenu;
@@ -102,7 +107,8 @@ public:
updateGeometry();
}
void setEnabled(int index, bool enabled);
void removeTab(int index) {
void removeTab(int index)
{
FancyTab *tab = m_tabs.takeAt(index);
delete tab;
updateGeometry();
@@ -110,10 +116,10 @@ public:
void setCurrentIndex(int index);
int currentIndex() const { return m_currentIndex; }
void setTabToolTip(int index, QString toolTip) { m_tabs[index]->toolTip = toolTip; }
void setTabToolTip(int index, const QString &toolTip) { m_tabs[index]->toolTip = toolTip; }
QString tabToolTip(int index) const { return m_tabs.at(index)->toolTip; }
int count() const {return m_tabs.count(); }
int count() const { return m_tabs.count(); }
QRect tabRect(int index) const;
signals:
@@ -122,11 +128,10 @@ signals:
private:
QRect m_hoverRect;
int m_hoverIndex;
int m_currentIndex;
QList<FancyTab*> m_tabs;
int m_hoverIndex = -1;
int m_currentIndex = -1;
QList<FancyTab *> m_tabs;
QSize tabSizeHint(bool minimum = false) const;
};
class FancyTabWidget : public QWidget
@@ -134,7 +139,7 @@ class FancyTabWidget : public QWidget
Q_OBJECT
public:
FancyTabWidget(QWidget *parent = 0);
FancyTabWidget(QWidget *parent = nullptr);
void insertTab(int index, QWidget *tab, const QIcon &icon, const QString &label, bool hasMenu);
void removeTab(int index);
@@ -144,7 +149,7 @@ public:
int cornerWidgetCount() const;
void setTabToolTip(int index, const QString &toolTip);
void paintEvent(QPaintEvent *event);
void paintEvent(QPaintEvent *event) override;
int currentIndex() const;
QStatusBar *statusBar() const;