Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtpropertybrowserutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtWidgets/QApplication>
6#include <QtGui/QPainter>
7#include <QtWidgets/QHBoxLayout>
8#include <QtGui/QMouseEvent>
9#include <QtWidgets/QCheckBox>
10#include <QtWidgets/QLineEdit>
11#include <QtWidgets/QMenu>
12#include <QtCore/QLocale>
13
15
16using namespace Qt::StringLiterals;
17
18// Make sure icons are removed as soon as QApplication is destroyed, otherwise,
19// handles are leaked on X11.
24
26{
27 qAddPostRoutine(clearCursorDatabase);
28
29 appendCursor(Qt::ArrowCursor, QCoreApplication::translate("QtCursorDatabase", "Arrow"),
30 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-arrow.png"_L1));
31 appendCursor(Qt::UpArrowCursor, QCoreApplication::translate("QtCursorDatabase", "Up Arrow"),
32 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-uparrow.png"_L1));
33 appendCursor(Qt::CrossCursor, QCoreApplication::translate("QtCursorDatabase", "Cross"),
34 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-cross.png"_L1));
35 appendCursor(Qt::WaitCursor, QCoreApplication::translate("QtCursorDatabase", "Wait"),
36 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-wait.png"_L1));
37 appendCursor(Qt::IBeamCursor, QCoreApplication::translate("QtCursorDatabase", "IBeam"),
38 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-ibeam.png"_L1));
39 appendCursor(Qt::SizeVerCursor, QCoreApplication::translate("QtCursorDatabase", "Size Vertical"),
40 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-sizev.png"_L1));
41 appendCursor(Qt::SizeHorCursor, QCoreApplication::translate("QtCursorDatabase", "Size Horizontal"),
42 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-sizeh.png"_L1));
43 appendCursor(Qt::SizeFDiagCursor, QCoreApplication::translate("QtCursorDatabase", "Size Backslash"),
44 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-sizef.png"_L1));
45 appendCursor(Qt::SizeBDiagCursor, QCoreApplication::translate("QtCursorDatabase", "Size Slash"),
46 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-sizeb.png"_L1));
47 appendCursor(Qt::SizeAllCursor, QCoreApplication::translate("QtCursorDatabase", "Size All"),
48 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-sizeall.png"_L1));
49 appendCursor(Qt::BlankCursor, QCoreApplication::translate("QtCursorDatabase", "Blank"),
50 QIcon());
51 appendCursor(Qt::SplitVCursor, QCoreApplication::translate("QtCursorDatabase", "Split Vertical"),
52 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-vsplit.png"_L1));
53 appendCursor(Qt::SplitHCursor, QCoreApplication::translate("QtCursorDatabase", "Split Horizontal"),
54 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-hsplit.png"_L1));
55 appendCursor(Qt::PointingHandCursor, QCoreApplication::translate("QtCursorDatabase", "Pointing Hand"),
56 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-hand.png"_L1));
57 appendCursor(Qt::ForbiddenCursor, QCoreApplication::translate("QtCursorDatabase", "Forbidden"),
58 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-forbidden.png"_L1));
59 appendCursor(Qt::OpenHandCursor, QCoreApplication::translate("QtCursorDatabase", "Open Hand"),
60 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-openhand.png"_L1));
61 appendCursor(Qt::ClosedHandCursor, QCoreApplication::translate("QtCursorDatabase", "Closed Hand"),
62 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-closedhand.png"_L1));
63 appendCursor(Qt::WhatsThisCursor, QCoreApplication::translate("QtCursorDatabase", "What's This"),
64 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-whatsthis.png"_L1));
65 appendCursor(Qt::BusyCursor, QCoreApplication::translate("QtCursorDatabase", "Busy"),
66 QIcon(":/qt-project.org/qtpropertybrowser/images/cursor-busy.png"_L1));
67}
68
70{
71 m_cursorNames.clear();
72 m_cursorIcons.clear();
73 m_valueToCursorShape.clear();
74 m_cursorShapeToValue.clear();
75}
76
77void QtCursorDatabase::appendCursor(Qt::CursorShape shape, const QString &name, const QIcon &icon)
78{
79 if (m_cursorShapeToValue.contains(shape))
80 return;
81 const int value = m_cursorNames.size();
82 m_cursorNames.append(name);
83 m_cursorIcons.insert(value, icon);
84 m_valueToCursorShape.insert(value, shape);
85 m_cursorShapeToValue.insert(shape, value);
86}
87
89{
90 return m_cursorNames;
91}
92
94{
95 return m_cursorIcons;
96}
97
98QString QtCursorDatabase::cursorToShapeName(const QCursor &cursor) const
99{
100 int val = cursorToValue(cursor);
101 if (val >= 0)
102 return m_cursorNames.at(val);
103 return {};
104}
105
106QIcon QtCursorDatabase::cursorToShapeIcon(const QCursor &cursor) const
107{
108 int val = cursorToValue(cursor);
109 return m_cursorIcons.value(val);
110}
111
112int QtCursorDatabase::cursorToValue(const QCursor &cursor) const
113{
114#ifndef QT_NO_CURSOR
115 Qt::CursorShape shape = cursor.shape();
116 if (m_cursorShapeToValue.contains(shape))
117 return m_cursorShapeToValue[shape];
118#endif
119 return -1;
120}
121
122#ifndef QT_NO_CURSOR
124{
125 if (m_valueToCursorShape.contains(value))
126 return QCursor(m_valueToCursorShape[value]);
127 return {};
128}
129#endif
130
131Q_GLOBAL_STATIC(QtCursorDatabase, cursorDatabase)
132
134{
135 return cursorDatabase();
136}
137
138QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b)
139{
140 QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
141 img.fill(0);
142
143 QPainter painter(&img);
144 painter.setCompositionMode(QPainter::CompositionMode_Source);
145 painter.fillRect(0, 0, img.width(), img.height(), b);
146 QColor color = b.color();
147 if (color.alpha() != 255) { // indicate alpha by an inset
148 QBrush opaqueBrush = b;
149 color.setAlpha(255);
150 opaqueBrush.setColor(color);
151 painter.fillRect(img.width() / 4, img.height() / 4,
152 img.width() / 2, img.height() / 2, opaqueBrush);
153 }
154 painter.end();
155 return QPixmap::fromImage(img);
156}
157
158QIcon QtPropertyBrowserUtils::brushValueIcon(const QBrush &b)
159{
160 return QIcon(brushValuePixmap(b));
161}
162
163QString QtPropertyBrowserUtils::colorValueText(QColor c)
164{
165 return QCoreApplication::translate("QtPropertyBrowserUtils", "[%1, %2, %3] (%4)")
166 .arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
167}
168
169QPixmap QtPropertyBrowserUtils::fontValuePixmap(const QFont &font)
170{
171 QFont f = font;
172 QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
173 img.fill(0);
174 QPainter p(&img);
175 p.setRenderHint(QPainter::TextAntialiasing, true);
176 p.setRenderHint(QPainter::Antialiasing, true);
177 f.setPointSize(13);
178 p.setFont(f);
179 QTextOption t;
180 t.setAlignment(Qt::AlignCenter);
181 p.drawText(QRect(0, 0, 16, 16), QString(QLatin1Char('A')), t);
182 return QPixmap::fromImage(img);
183}
184
185QIcon QtPropertyBrowserUtils::fontValueIcon(const QFont &f)
186{
187 return QIcon(fontValuePixmap(f));
188}
189
190QString QtPropertyBrowserUtils::fontValueText(const QFont &f)
191{
192 return QCoreApplication::translate("QtPropertyBrowserUtils", "[%1, %2]")
193 .arg(f.family()).arg(f.pointSize());
194}
195
196QString QtPropertyBrowserUtils::dateFormat()
197{
198 QLocale loc;
199 QString format = loc.dateFormat(QLocale::ShortFormat);
200 // Change dd.MM.yy, MM/dd/yy to 4 digit years
201 if (format.count(QLatin1Char('y')) == 2)
202 format.insert(format.indexOf(QLatin1Char('y')), "yy"_L1);
203 return format;
204}
205
206QString QtPropertyBrowserUtils::timeFormat()
207{
208 QLocale loc;
209 // ShortFormat is missing seconds on UNIX.
210 return loc.timeFormat(QLocale::LongFormat);
211}
212
213QString QtPropertyBrowserUtils::dateTimeFormat()
214{
215 QString format = dateFormat();
216 format += QLatin1Char(' ');
217 format += timeFormat();
218 return format;
219}
220
221QtBoolEdit::QtBoolEdit(QWidget *parent) :
222 QWidget(parent),
223 m_checkBox(new QCheckBox(this)),
224 m_textVisible(true)
225{
226 auto *lt = new QHBoxLayout;
227 if (QApplication::layoutDirection() == Qt::LeftToRight)
228 lt->setContentsMargins(4, 0, 0, 0);
229 else
230 lt->setContentsMargins(0, 0, 4, 0);
231 lt->addWidget(m_checkBox);
232 setLayout(lt);
233 connect(m_checkBox, &QAbstractButton::toggled, this, &QtBoolEdit::toggled);
234 setFocusProxy(m_checkBox);
235 m_checkBox->setText(tr("True"));
236}
237
238void QtBoolEdit::setTextVisible(bool textVisible)
239{
240 if (m_textVisible == textVisible)
241 return;
242
243 m_textVisible = textVisible;
244 if (m_textVisible)
245 m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
246 else
247 m_checkBox->setText(QString());
248}
249
251{
252 return m_checkBox->checkState();
253}
254
255void QtBoolEdit::setCheckState(Qt::CheckState state)
256{
257 m_checkBox->setCheckState(state);
258}
259
261{
262 return m_checkBox->isChecked();
263}
264
266{
267 m_checkBox->setChecked(c);
268 if (!m_textVisible)
269 return;
270 m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
271}
272
274{
275 return m_checkBox->blockSignals(block);
276}
277
278void QtBoolEdit::mousePressEvent(QMouseEvent *event)
279{
280 if (event->buttons() == Qt::LeftButton) {
281 m_checkBox->click();
282 event->accept();
283 } else {
284 QWidget::mousePressEvent(event);
285 }
286}
287
288QT_END_NAMESPACE
Qt::CheckState checkState() const
void setTextVisible(bool textVisible)
bool blockCheckBoxSignals(bool block)
void setCheckState(Qt::CheckState state)
QCursor valueToCursor(int value) const
QStringList cursorShapeNames() const
int cursorToValue(const QCursor &cursor) const
QString cursorToShapeName(const QCursor &cursor) const
QIcon cursorToShapeIcon(const QCursor &cursor) const
static QtCursorDatabase * instance()
QMap< int, QIcon > cursorShapeIcons() const
Combined button and popup list for selecting options.
static void clearCursorDatabase()