aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/qtcwidgets.h
blob: 7195f5e9f4c4d25e41089f4ee71b0791c54e899a (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright (C) 2023 Tasuku Suzuki <tasuku.suzuki@signal-slot.co.jp>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "utils_global.h"

#include "fancylineedit.h"
#include "layoutbuilder.h"
#include "stylehelper.h"
#include "theme/theme.h"

#include <QAbstractButton>
#include <QComboBox>
#include <QLabel>

namespace Utils {

class QTCREATOR_UTILS_EXPORT TextFormat
{
public:
    QColor color() const;
    QFont font(bool underlined = false) const;
    int lineHeight() const;

    const Theme::Color themeColor;
    const StyleHelper::UiElement uiElement;
    const int drawTextFlags = Qt::AlignLeft | Qt::AlignBottom | Qt::TextDontClip
                              | Qt::TextShowMnemonic;
};

QTCREATOR_UTILS_EXPORT void applyTf(QLabel *label, const TextFormat &tf, bool singleLine = true);

class QTCREATOR_UTILS_EXPORT QtcButton : public QAbstractButton
{
    Q_OBJECT // Needed for the Q_ENUM(Role) to work

public:
    enum Role {
        LargePrimary,
        LargeSecondary,
        LargeTertiary,
        SmallPrimary,
        SmallSecondary,
        SmallTertiary,
        SmallList,
        SmallLink,
        Tag,
    };
    Q_ENUM(Role)

    explicit QtcButton(const QString &text, Role role, QWidget *parent = nullptr);
    QSize minimumSizeHint() const override;
    void setPixmap(const QPixmap &newPixmap);
    void setRole(Role role);

protected:
    void paintEvent(QPaintEvent *event) override;

private:
    void updateMargins();

    Role m_role = LargePrimary;
    QPixmap m_pixmap;
};

class QTCREATOR_UTILS_EXPORT QtcLabel : public QLabel
{
    Q_OBJECT // Needed for the Q_ENUM(Role) to work
public:
    enum Role {
        Primary,
        Secondary,
    };
    Q_ENUM(Role)

    explicit QtcLabel(const QString &text, Role role, QWidget *parent = nullptr);

    void setRole(Role role);
};

class QTCREATOR_UTILS_EXPORT QtcSearchBox : public Utils::FancyLineEdit
{
public:
    explicit QtcSearchBox(QWidget *parent = nullptr);
    QSize minimumSizeHint() const override;

protected:
    void paintEvent(QPaintEvent *event) override;
    void enterEvent(QEnterEvent *event) override;
    void leaveEvent(QEvent *event) override;
};

class QTCREATOR_UTILS_EXPORT QtcComboBox : public QComboBox
{
public:
    explicit QtcComboBox(QWidget *parent = nullptr);
    QSize sizeHint() const override;

protected:
    void paintEvent(QPaintEvent *event) override;

protected:
    void enterEvent(QEnterEvent *event) override;
    void leaveEvent(QEvent *event) override;
};

class QTCREATOR_UTILS_EXPORT QtcSwitch : public QAbstractButton
{
public:
    explicit QtcSwitch(const QString &text, QWidget *parent = nullptr);
    QSize sizeHint() const override;
    QSize minimumSizeHint() const override;

protected:
    void paintEvent(QPaintEvent *event) override;
};

class QTCREATOR_UTILS_EXPORT QtcIconButton : public QAbstractButton
{
public:
    QtcIconButton(QWidget *parent = nullptr);

    void paintEvent(QPaintEvent *e) override;
    void enterEvent(QEnterEvent *e) override;
    void leaveEvent(QEvent *e) override;

    QSize sizeHint() const override;

private:
    bool m_containsMouse{false};
};

class QTCREATOR_UTILS_EXPORT QtcRectangleWidget : public QWidget
{
public:
    QtcRectangleWidget(QWidget *parent = nullptr);

    QSize sizeHint() const override;

    int radius() const;
    void setRadius(int radius);

    QBrush fillBrush() const;
    void setFillBrush(const QBrush &brush);

    void setStrokePen(QPen pen);
    QPen strokePen() const;

protected:
    void paintEvent(QPaintEvent *event) override;

private:
    int m_radius{10};
    QBrush m_fillBrush{Qt::black};
    QPen m_strokePen{Qt::NoPen};
};

class CachedImage;

class QTCREATOR_UTILS_EXPORT QtcImage : public QWidget
{
public:
    QtcImage(QWidget *parent = nullptr);

    void setUrl(const QString &url);

    void setRadius(int radius);
    int radius() const;

    void paintEvent(QPaintEvent *event) override;

    QSize sizeForWidth(int width) const;

    QSize sizeHint() const override;

    int heightForWidth(int width) const override;

private:
    void setPixmap(const QPixmap &px);

private:
    CachedImage *m_cachedImage = nullptr;
    QPixmap m_pixmap;
    int m_radius = 0;
};

class QTCREATOR_UTILS_EXPORT QtcTabBar : public QTabBar
{
public:
    QtcTabBar(QWidget *parent = nullptr);

protected:
    void paintEvent(QPaintEvent *event) override;
    bool event(QEvent *event) override;
};


namespace QtcWidgets {

class QTCREATOR_UTILS_EXPORT Label : public Layouting::Widget
{
public:
    using Implementation = QtcLabel;
    using I = Building::BuilderItem<Label>;
    Label();
    Label(std::initializer_list<I> ps);
    void setText(const QString &text);
    void setRole(QtcLabel::Role role);
};

class QTCREATOR_UTILS_EXPORT Button : public Layouting::Widget
{
public:
    using Implementation = QtcButton;
    using I = Building::BuilderItem<Button>;
    Button();
    Button(std::initializer_list<I> ps);
    void setText(const QString &text);
    void setIcon(const Utils::Icon &icon);
    void setRole(QtcButton::Role role);
    void onClicked(QObject *guard, const std::function<void()> &);
};

class QTCREATOR_UTILS_EXPORT IconButton : public Layouting::Widget
{
public:
    using Implementation = QtcIconButton;
    using I = Building::BuilderItem<IconButton>;
    IconButton();
    IconButton(std::initializer_list<I> ps);
    void setIcon(const Utils::Icon &icon);
    void onClicked(QObject *guard, const std::function<void()> &);
};

class QTCREATOR_UTILS_EXPORT Switch : public Layouting::Widget
{
public:
    using Implementation = QtcSwitch;
    using I = Building::BuilderItem<Switch>;
    Switch();
    Switch(std::initializer_list<I> ps);
    void setText(const QString &text);
    void setChecked(bool checked);
    void onClicked(QObject *guard, const std::function<void()> &);
};

class QTCREATOR_UTILS_EXPORT SearchBox : public Layouting::Widget
{
public:
    using Implementation = QtcSearchBox;
    using I = Building::BuilderItem<SearchBox>;

    SearchBox();
    SearchBox(std::initializer_list<I> ps);

    void setPlaceholderText(const QString &text);
    void setText(const QString &text);
    void onTextChanged(QObject *guard, const std::function<void(QString)> &);
};

class QTCREATOR_UTILS_EXPORT Rectangle : public Layouting::Widget
{
public:
    using Implementation = QtcRectangleWidget;
    using I = Building::BuilderItem<Rectangle>;

    Rectangle(std::initializer_list<I> ps);

    void setRadius(int radius);
    void setFillBrush(const QBrush &brush);
    void setStrokePen(const QPen &pen);
};

class QTCREATOR_UTILS_EXPORT Image : public Layouting::Widget
{
public:
    using Implementation = QtcImage;
    using I = Building::BuilderItem<Image>;

    Image(std::initializer_list<I> ps);

    void setUrl(const QString &url);
    void setRadius(int radius);
};

} // namespace QtcWidgets

QTC_DEFINE_BUILDER_SETTER(role, setRole);
QTC_DEFINE_BUILDER_SETTER(fillBrush, setFillBrush);
QTC_DEFINE_BUILDER_SETTER(strokePen, setStrokePen);
QTC_DEFINE_BUILDER_SETTER(radius, setRadius);
QTC_DEFINE_BUILDER_SETTER(url, setUrl)


} // namespace Utils