iVS3D v2.0.0
Loading...
Searching...
No Matches
adaptivetoolbutton.h
1#include <QToolButton>
2#include <QResizeEvent>
3#include <QIcon>
4#include <QMap>
5#include <QFontMetrics>
6#include "cvmat_qmetadata.h"
7
13class AdaptiveToolButton : public QToolButton
14{
15public:
22 AdaptiveToolButton(const QString &fullText, const QString &shortText = "", QWidget *parent = nullptr)
23 : QToolButton(parent), mFullText(fullText), mShortText(shortText), mColorTheme(ColorTheme::LIGHT)
24 {
25 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
26 setMinimumSize(30, 30);
27 setIconSize(QSize(25, 25));
28 setToolButtonStyle(Qt::ToolButtonStyle::ToolButtonTextBesideIcon);
29 setText(fullText);
30 }
31
37 void setIconForTheme(const QIcon &icon, ColorTheme theme)
38 {
39 mIcons[theme] = icon;
40 if (mColorTheme == theme) {
41 setIcon(icon);
42 adjustLabel();
43 }
44 }
45
50 void setColorTheme(ColorTheme theme)
51 {
52 mColorTheme = theme;
53 if(mIcons.contains(theme)) {
54 setIcon(mIcons.value(theme));
55 adjustLabel();
56 }
57 }
58
59protected:
60 void resizeEvent(QResizeEvent *event) override
61 {
62 QToolButton::resizeEvent(event);
63 adjustLabel();
64 }
65
66private:
67 void adjustLabel()
68 {
69 QFontMetrics fm(font());
70 int textWidth = fm.horizontalAdvance(mFullText);
71 int availableWidth = width() - iconSize().width() - 10; // margin/padding
72
73 if (textWidth > availableWidth) {
74 if (!mShortText.isEmpty() && fm.horizontalAdvance(mShortText) <= availableWidth) {
75 setText(mShortText);
76 setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
77 } else {
78 setToolButtonStyle(Qt::ToolButtonIconOnly);
79 }
80 } else {
81 setText(mFullText);
82 setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
83 }
84 }
85
86 QString mFullText;
87 QString mShortText;
88 ColorTheme mColorTheme;
89 QMap<ColorTheme, QIcon> mIcons;
90};
Definition adaptivetoolbutton.h:14
void setIconForTheme(const QIcon &icon, ColorTheme theme)
setIconForTheme sets the icon for a specific color theme
Definition adaptivetoolbutton.h:37
void setColorTheme(ColorTheme theme)
setColorTheme sets the current color theme and updates the icon accordingly
Definition adaptivetoolbutton.h:50
AdaptiveToolButton(const QString &fullText, const QString &shortText="", QWidget *parent=nullptr)
AdaptiveToolButton constructor.
Definition adaptivetoolbutton.h:22