-
Notifications
You must be signed in to change notification settings - Fork 0
/
QTopMenuButtonWidget.hpp
145 lines (113 loc) · 4.45 KB
/
QTopMenuButtonWidget.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* This file is part of Escain QTopMenu library
*
* QTopMenu library is free software: you can redistribute it and/or modify
* it under ther terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* Escain Documentor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
*
* Author: Adrian Maire escain (at) gmail.com
*/
#ifndef QTOPMENUBUTTONWIDGET_HPP
#define QTOPMENUBUTTONWIDGET_HPP
#include <memory>
#include <QStaticText>
#include "QTopMenuWidget.hpp"
#include <QClickManager.hpp>
#include <QSvgIcon.hpp>
#include "QTopMenuButton.hpp"
class QWidget;
namespace Escain
{
enum class QTopMenuBuggonWidgetLayout
{
Small,
Big,
Horizontal
};
struct QTopMenuButtonWidgetStyleOptions
{
bool isHover = false;
bool isEnabled = true;
bool isPressed = false;
bool isFocussed = false;
QPaletteExt palette;
const QSvgIcon* icon=nullptr;
const QStaticText* staticText=nullptr;
QRect widgetRect;
QTopMenuBuggonWidgetLayout layout;
DisplaySide direction;
size_t id=0;
qreal margin = 0.0;
};
class QSvgIcon;
class QTopMenuButtonWidget: public QTopMenuWidget
{
Q_OBJECT
public:
explicit QTopMenuButtonWidget( const std::string& name, size_t id, QWidget* parent=nullptr);
virtual ~QTopMenuButtonWidget() override;
// See QTopMenuWidget for more details
std::optional<QSizeF> bestSize(DisplaySide dir, const CellInfo& cellInfo,
const QSizeF& sizeHint, const QSizeF& maxSize) const override;
/// Icon accessors
virtual void icon(QSvgIcon* ic);
virtual const QSvgIcon* icon() const;
/// Label accessors
virtual void label( const std::string& label);
virtual const std::string& label() const;
/// Space around content
qreal margin() const;
virtual void margin( qreal margin );
using QTopMenuWidget::direction;
void direction( const DisplaySide d ) override;
signals:
void clicked(const QPointF& cursorPos, const std::unordered_set<size_t>& clickableRectangleIds, QTopMenuButtonWidget* me); //TODO remove me argument
protected:
/// overridable call to the static equivalent, so it can be override by child classes.
virtual void drawControl( const QTopMenuButtonWidgetStyleOptions& opt, QPainter& p) const { staticDrawControl(opt, p); }
/// Draw a QTopMenuButtonWidget widget. Static so it can be reused externally or overrided.
static void staticDrawControl( const QTopMenuButtonWidgetStyleOptions&, QPainter& p);
/// Intialize QTopMenuButtonWidgetStyleOptions with current widget
virtual void initStyleOption(QTopMenuButtonWidgetStyleOptions&) const;
//TODO document: static call allows non-childs to draw similar content (and eventually move it to a QSyle class)
// virtual method allows child class to call another implementation of that static call
// (because otherwise, internal implementation only call this static methdo, and never the override one.)
virtual QRect textRect() const;
static QRect textRect(const QRect& widgetRect, qreal margin, const QFontMetrics& metrics,
const QString& label, const QTopMenuBuggonWidgetLayout layout);
virtual QRectF iconRect() const;
static QRectF iconRect(const QRect& widgetRect, const QTopMenuBuggonWidgetLayout layout, const DisplaySide);
virtual void setupFontForLabel( QFont& f) const { staticSetupFontForLabel(f); }
static void staticSetupFontForLabel( QFont& );
virtual QString elidedText( const QFontMetrics& metrics, const QTopMenuBuggonWidgetLayout layout,
const QString& str, const QSize& widgetSize, const qreal margin) const;
virtual void recomputeSize();
virtual QTopMenuBuggonWidgetLayout detectLayout(const QSize& size) const;
QClickManager m_clickManager;
bool m_clickPressed=false;
bool m_hovered = false;
std::string m_label;
QSvgIcon* m_icon=nullptr;
qreal m_margin = 2.0;
QTopMenuBuggonWidgetLayout m_cachedLayout = QTopMenuBuggonWidgetLayout::Big;
QStaticText m_staticText;
QRect m_textRect;
QRect m_iconRect;
bool m_recomputeSizeNeeded = true;
constexpr static qreal cornerRadius= 0.0;
constexpr static int borderWidth = 0.5;
bool event(QEvent* e) override;
void paintEvent(QPaintEvent *e) override;
void resizeEvent(QResizeEvent *) override;
void keyPressEvent(QKeyEvent *e) override;
};
}
#endif