Refactoring.

This commit is contained in:
Roman Telezhynskyi 2024-04-27 11:44:30 +03:00
parent 5adcdb9b67
commit 4b985b4ecc
6 changed files with 60 additions and 77 deletions

View File

@ -33,20 +33,14 @@
//---------------------------------------------------------------------------------------------------------------------
FancyTab::FancyTab(QWidget *tabbar)
: m_icon(),
m_text(),
m_toolTip(),
m_enabled(false),
m_Animator(),
m_TabBar(tabbar),
m_Fader(0)
: m_TabBar(tabbar)
{
m_Animator.setPropertyName("fader");
m_Animator.setTargetObject(this);
}
//---------------------------------------------------------------------------------------------------------------------
auto FancyTab::fader() -> double
auto FancyTab::fader() const -> double
{
return m_Fader;
}

View File

@ -42,8 +42,9 @@ class FancyTab final : public QObject
public:
explicit FancyTab(QWidget *tabbar);
~FancyTab() override = default;
auto fader() -> double;
auto fader() const -> double;
void setFader(double value);
void fadeIn();
@ -52,13 +53,13 @@ public:
private:
// cppcheck-suppress unknownMacro
Q_DISABLE_COPY_MOVE(FancyTab) // NOLINT
QIcon m_icon;
QString m_text;
QString m_toolTip;
bool m_enabled;
QPropertyAnimation m_Animator;
QIcon m_icon{};
QString m_text{};
QString m_toolTip{};
bool m_enabled{false};
QPropertyAnimation m_Animator{};
QWidget *m_TabBar;
double m_Fader;
double m_Fader{0};
};
#endif // FANCYTAB_H

View File

@ -42,14 +42,9 @@ using namespace Qt::Literals::StringLiterals;
const int FancyTabBar::m_rounding = 22;
//---------------------------------------------------------------------------------------------------------------------
FancyTabBar::FancyTabBar(const TabBarPosition position, QWidget *parent)
FancyTabBar::FancyTabBar(TabBarPosition position, QWidget *parent)
: QWidget(parent),
m_position(position),
m_hoverRect(),
m_hoverIndex(-1),
m_currentIndex(-1),
m_attachedTabs(),
m_timerTriggerChangedSignal()
m_position(position)
{
if (m_position == FancyTabBar::Above || m_position == FancyTabBar::Below)
{
@ -115,11 +110,11 @@ auto FancyTabBar::TabSizeHint(bool minimum) const -> QSize
int const width = 60 + spacing + 2;
int const iconHeight = minimum ? 0 : 32;
return QSize(qMax(width, maxLabelwidth + 4), iconHeight + spacing + fm.height());
return {qMax(width, maxLabelwidth + 4), iconHeight + spacing + fm.height()};
}
//---------------------------------------------------------------------------------------------------------------------
auto FancyTabBar::GetCorner(const QRect &rect, const Corner corner) const -> QPoint
auto FancyTabBar::GetCorner(const QRect &rect, Corner corner) const -> QPoint
{
switch (m_position)
{
@ -189,7 +184,7 @@ auto FancyTabBar::GetCorner(const QRect &rect, const Corner corner) const -> QPo
qFatal("that's impossible!");
return QPoint();
return {};
}
//---------------------------------------------------------------------------------------------------------------------
@ -201,8 +196,8 @@ auto FancyTabBar::GetCorner(const QRect &rect, const Corner corner) const -> QPo
// gives
//
// QRect(-3, -1, 9, 13) // 9 by 13 rect, starting at -3/-1.
auto FancyTabBar::AdjustRect(const QRect &rect, const qint8 offsetOutside, const qint8 offsetInside,
const qint8 offsetBeginning, const qint8 offsetEnd) const -> QRect
auto FancyTabBar::AdjustRect(const QRect &rect, qint8 offsetOutside, qint8 offsetInside, qint8 offsetBeginning,
qint8 offsetEnd) const -> QRect
{
switch (m_position)
{
@ -219,13 +214,12 @@ auto FancyTabBar::AdjustRect(const QRect &rect, const qint8 offsetOutside, const
}
qFatal("that's impossible!");
return QRect();
return {};
}
//---------------------------------------------------------------------------------------------------------------------
// Same with a point: + means towards Outside/End, - means towards Inside/Beginning
auto FancyTabBar::AdjustPoint(const QPoint &point, const qint8 offsetInsideOutside,
const qint8 offsetBeginningEnd) const -> QPoint
auto FancyTabBar::AdjustPoint(const QPoint &point, qint8 offsetInsideOutside, qint8 offsetBeginningEnd) const -> QPoint
{
switch (m_position)
{
@ -242,7 +236,7 @@ auto FancyTabBar::AdjustPoint(const QPoint &point, const qint8 offsetInsideOutsi
}
qFatal("that's impossible!");
return QPoint();
return {};
}
//---------------------------------------------------------------------------------------------------------------------
@ -370,7 +364,7 @@ auto FancyTabBar::ValidIndex(int index) const -> bool
}
//---------------------------------------------------------------------------------------------------------------------
void FancyTabBar::SetOrientation(const FancyTabBar::TabBarPosition p)
void FancyTabBar::SetOrientation(TabBarPosition p)
{
m_position = p;
}
@ -383,10 +377,10 @@ auto FancyTabBar::sizeHint() const -> QSize
if (m_position == Above || m_position == Below)
{
return QSize(sh.width() * static_cast<int>(m_attachedTabs.count()), sh.height());
return {sh.width() * static_cast<int>(m_attachedTabs.count()), sh.height()};
}
return QSize(sh.width(), sh.height() * static_cast<int>(m_attachedTabs.count()));
return {sh.width(), sh.height() * static_cast<int>(m_attachedTabs.count())};
}
//---------------------------------------------------------------------------------------------------------------------
@ -397,10 +391,10 @@ auto FancyTabBar::minimumSizeHint() const -> QSize
if (m_position == Above || m_position == Below)
{
return QSize(sh.width() * static_cast<int>(m_attachedTabs.count()), sh.height());
return {sh.width() * static_cast<int>(m_attachedTabs.count()), sh.height()};
}
return QSize(sh.width(), sh.height() * static_cast<int>(m_attachedTabs.count()));
return {sh.width(), sh.height() * static_cast<int>(m_attachedTabs.count())};
}
//---------------------------------------------------------------------------------------------------------------------
@ -415,17 +409,15 @@ auto FancyTabBar::TabRect(int index) const -> QRect
sh.setWidth(width() / static_cast<int>(m_attachedTabs.count()));
}
return QRect(index * sh.width(), 0, sh.width(), sh.height());
return {index * sh.width(), 0, sh.width(), sh.height()};
}
else
{
if (sh.height() * m_attachedTabs.count() > height())
{
sh.setHeight(height() / static_cast<int>(m_attachedTabs.count()));
}
return QRect(0, index * sh.height(), sh.width(), sh.height());
if (sh.height() * m_attachedTabs.count() > height())
{
sh.setHeight(height() / static_cast<int>(m_attachedTabs.count()));
}
return {0, index * sh.height(), sh.width(), sh.height()};
}
//---------------------------------------------------------------------------------------------------------------------
@ -536,7 +528,7 @@ void FancyTabBar::PaintTab(QPainter *painter, int tabIndex) const
if (!selected && enabled)
{
painter->save();
int const fader = int(m_attachedTabs[tabIndex]->fader());
int const fader = static_cast<int>(m_attachedTabs[tabIndex]->fader());
QLinearGradient grad(GetCorner(rect, OutsideBeginning), GetCorner(rect, InsideBeginning));
grad.setColorAt(0, Qt::transparent);

View File

@ -53,13 +53,13 @@ public:
Right
};
explicit FancyTabBar(const TabBarPosition position, QWidget *parent = nullptr);
virtual ~FancyTabBar() = default;
explicit FancyTabBar(TabBarPosition position, QWidget *parent = nullptr);
~FancyTabBar() override = default;
virtual auto sizeHint() const -> QSize override;
virtual auto minimumSizeHint() const -> QSize override;
auto sizeHint() const -> QSize override;
auto minimumSizeHint() const -> QSize override;
void SetOrientation(const TabBarPosition p);
void SetOrientation(TabBarPosition p);
void SetTabEnabled(int index, bool enable);
auto IsTabEnabled(int index) const -> bool;
@ -85,16 +85,16 @@ signals:
void CurrentChanged(int);
protected:
virtual auto event(QEvent *event) -> bool override;
virtual void paintEvent(QPaintEvent *event) override;
virtual void mousePressEvent(QMouseEvent *) override;
virtual void mouseMoveEvent(QMouseEvent *) override;
auto event(QEvent *event) -> bool override;
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseMoveEvent(QMouseEvent *e) override;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
virtual void enterEvent(QEnterEvent *) override;
void enterEvent(QEnterEvent *e) override;
#else
virtual void enterEvent(QEvent *) override;
void enterEvent(QEvent *e) override;
#endif
virtual void leaveEvent(QEvent *) override;
void leaveEvent(QEvent *e) override;
private slots:
void EmitCurrentIndex();
@ -113,20 +113,19 @@ private:
static const int m_rounding;
TabBarPosition m_position;
QRect m_hoverRect;
int m_hoverIndex;
int m_currentIndex;
QList<FancyTab *> m_attachedTabs;
QTimer m_timerTriggerChangedSignal;
QRect m_hoverRect{};
int m_hoverIndex{-1};
int m_currentIndex{-1};
QList<FancyTab *> m_attachedTabs{};
QTimer m_timerTriggerChangedSignal{};
auto GetCorner(const QRect &rect, const Corner corner) const -> QPoint;
auto GetCorner(const QRect &rect, Corner corner) const -> QPoint;
auto AdjustRect(const QRect &rect, const qint8 offsetOutside, const qint8 offsetInside, const qint8 offsetBeginning,
const qint8 offsetEnd) const -> QRect;
auto AdjustRect(const QRect &rect, qint8 offsetOutside, qint8 offsetInside, qint8 offsetBeginning,
qint8 offsetEnd) const -> QRect;
// Same with a point. + means towards Outside/End, - means towards Inside/Beginning
auto AdjustPoint(const QPoint &point, const qint8 offsetInsideOutside, const qint8 offsetBeginningEnd) const
-> QPoint;
auto AdjustPoint(const QPoint &point, qint8 offsetInsideOutside, qint8 offsetBeginningEnd) const -> QPoint;
auto TabSizeHint(bool minimum = false) const -> QSize;
void PaintTab(QPainter *painter, int tabIndex) const;

View File

@ -39,6 +39,12 @@
#include <QWidget>
#include <QtMath>
namespace
{
QColor m_baseColor;
QColor m_requestedBaseColor;
} // namespace
//---------------------------------------------------------------------------------------------------------------------
auto StyleHelper::sidebarFontSize() -> qreal
{
@ -61,11 +67,6 @@ auto StyleHelper::panelTextColor(bool lightColored) -> QColor
return Qt::black;
}
//---------------------------------------------------------------------------------------------------------------------
// Invalid by default, setBaseColor needs to be called at least once
QColor StyleHelper::m_baseColor;
QColor StyleHelper::m_requestedBaseColor;
//---------------------------------------------------------------------------------------------------------------------
auto StyleHelper::baseColor(bool lightColored) -> QColor
{
@ -88,7 +89,7 @@ auto StyleHelper::borderColor(bool lightColored) -> QColor
//---------------------------------------------------------------------------------------------------------------------
auto StyleHelper::sidebarHighlight() -> QColor
{
return QColor(255, 255, 255, 40);
return {255, 255, 255, 40};
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -59,10 +59,6 @@ public:
static void drawIconWithShadow(const QIcon &icon, const QRect &rect, QPainter *p, QIcon::Mode iconMode,
int dipRadius = 3, const QColor &color = QColor(0, 0, 0, 130),
const QPoint &dipOffset = QPoint(1, -2));
private:
static QColor m_baseColor;
static QColor m_requestedBaseColor;
};
#endif // STYLEHELPER_H