1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the tools applications of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qtpropertybrowserutils_p.h" |
41 | #include <QtWidgets/QApplication> |
42 | #include <QtGui/QPainter> |
43 | #include <QtWidgets/QHBoxLayout> |
44 | #include <QtGui/QMouseEvent> |
45 | #include <QtWidgets/QCheckBox> |
46 | #include <QtWidgets/QLineEdit> |
47 | #include <QtWidgets/QMenu> |
48 | #include <QtCore/QLocale> |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | QtCursorDatabase::QtCursorDatabase() |
53 | { |
54 | appendCursor(shape: Qt::ArrowCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Arrow" ), |
55 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-arrow.png" ))); |
56 | appendCursor(shape: Qt::UpArrowCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Up Arrow" ), |
57 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-uparrow.png" ))); |
58 | appendCursor(shape: Qt::CrossCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Cross" ), |
59 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-cross.png" ))); |
60 | appendCursor(shape: Qt::WaitCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Wait" ), |
61 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-wait.png" ))); |
62 | appendCursor(shape: Qt::IBeamCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "IBeam" ), |
63 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-ibeam.png" ))); |
64 | appendCursor(shape: Qt::SizeVerCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Size Vertical" ), |
65 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizev.png" ))); |
66 | appendCursor(shape: Qt::SizeHorCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Size Horizontal" ), |
67 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeh.png" ))); |
68 | appendCursor(shape: Qt::SizeFDiagCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Size Backslash" ), |
69 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizef.png" ))); |
70 | appendCursor(shape: Qt::SizeBDiagCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Size Slash" ), |
71 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeb.png" ))); |
72 | appendCursor(shape: Qt::SizeAllCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Size All" ), |
73 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeall.png" ))); |
74 | appendCursor(shape: Qt::BlankCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Blank" ), |
75 | icon: QIcon()); |
76 | appendCursor(shape: Qt::SplitVCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Split Vertical" ), |
77 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-vsplit.png" ))); |
78 | appendCursor(shape: Qt::SplitHCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Split Horizontal" ), |
79 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-hsplit.png" ))); |
80 | appendCursor(shape: Qt::PointingHandCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Pointing Hand" ), |
81 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-hand.png" ))); |
82 | appendCursor(shape: Qt::ForbiddenCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Forbidden" ), |
83 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-forbidden.png" ))); |
84 | appendCursor(shape: Qt::OpenHandCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Open Hand" ), |
85 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-openhand.png" ))); |
86 | appendCursor(shape: Qt::ClosedHandCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Closed Hand" ), |
87 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-closedhand.png" ))); |
88 | appendCursor(shape: Qt::WhatsThisCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "What's This" ), |
89 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-whatsthis.png" ))); |
90 | appendCursor(shape: Qt::BusyCursor, name: QCoreApplication::translate(context: "QtCursorDatabase" , key: "Busy" ), |
91 | icon: QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-busy.png" ))); |
92 | } |
93 | |
94 | void QtCursorDatabase::clear() |
95 | { |
96 | m_cursorNames.clear(); |
97 | m_cursorIcons.clear(); |
98 | m_valueToCursorShape.clear(); |
99 | m_cursorShapeToValue.clear(); |
100 | } |
101 | |
102 | void QtCursorDatabase::appendCursor(Qt::CursorShape shape, const QString &name, const QIcon &icon) |
103 | { |
104 | if (m_cursorShapeToValue.contains(akey: shape)) |
105 | return; |
106 | const int value = m_cursorNames.count(); |
107 | m_cursorNames.append(t: name); |
108 | m_cursorIcons.insert(akey: value, avalue: icon); |
109 | m_valueToCursorShape.insert(akey: value, avalue: shape); |
110 | m_cursorShapeToValue.insert(akey: shape, avalue: value); |
111 | } |
112 | |
113 | QStringList QtCursorDatabase::cursorShapeNames() const |
114 | { |
115 | return m_cursorNames; |
116 | } |
117 | |
118 | QMap<int, QIcon> QtCursorDatabase::cursorShapeIcons() const |
119 | { |
120 | return m_cursorIcons; |
121 | } |
122 | |
123 | QString QtCursorDatabase::cursorToShapeName(const QCursor &cursor) const |
124 | { |
125 | int val = cursorToValue(cursor); |
126 | if (val >= 0) |
127 | return m_cursorNames.at(i: val); |
128 | return QString(); |
129 | } |
130 | |
131 | QIcon QtCursorDatabase::cursorToShapeIcon(const QCursor &cursor) const |
132 | { |
133 | int val = cursorToValue(cursor); |
134 | return m_cursorIcons.value(akey: val); |
135 | } |
136 | |
137 | int QtCursorDatabase::cursorToValue(const QCursor &cursor) const |
138 | { |
139 | #ifndef QT_NO_CURSOR |
140 | Qt::CursorShape shape = cursor.shape(); |
141 | if (m_cursorShapeToValue.contains(akey: shape)) |
142 | return m_cursorShapeToValue[shape]; |
143 | #endif |
144 | return -1; |
145 | } |
146 | |
147 | #ifndef QT_NO_CURSOR |
148 | QCursor QtCursorDatabase::valueToCursor(int value) const |
149 | { |
150 | if (m_valueToCursorShape.contains(akey: value)) |
151 | return QCursor(m_valueToCursorShape[value]); |
152 | return QCursor(); |
153 | } |
154 | #endif |
155 | |
156 | QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b) |
157 | { |
158 | QImage img(16, 16, QImage::Format_ARGB32_Premultiplied); |
159 | img.fill(pixel: 0); |
160 | |
161 | QPainter painter(&img); |
162 | painter.setCompositionMode(QPainter::CompositionMode_Source); |
163 | painter.fillRect(x: 0, y: 0, w: img.width(), h: img.height(), b); |
164 | QColor color = b.color(); |
165 | if (color.alpha() != 255) { // indicate alpha by an inset |
166 | QBrush opaqueBrush = b; |
167 | color.setAlpha(255); |
168 | opaqueBrush.setColor(color); |
169 | painter.fillRect(x: img.width() / 4, y: img.height() / 4, |
170 | w: img.width() / 2, h: img.height() / 2, b: opaqueBrush); |
171 | } |
172 | painter.end(); |
173 | return QPixmap::fromImage(image: img); |
174 | } |
175 | |
176 | QIcon QtPropertyBrowserUtils::brushValueIcon(const QBrush &b) |
177 | { |
178 | return QIcon(brushValuePixmap(b)); |
179 | } |
180 | |
181 | QString QtPropertyBrowserUtils::colorValueText(const QColor &c) |
182 | { |
183 | return QCoreApplication::translate(context: "QtPropertyBrowserUtils" , key: "[%1, %2, %3] (%4)" ) |
184 | .arg(a: c.red()).arg(a: c.green()).arg(a: c.blue()).arg(a: c.alpha()); |
185 | } |
186 | |
187 | QPixmap QtPropertyBrowserUtils::fontValuePixmap(const QFont &font) |
188 | { |
189 | QFont f = font; |
190 | QImage img(16, 16, QImage::Format_ARGB32_Premultiplied); |
191 | img.fill(pixel: 0); |
192 | QPainter p(&img); |
193 | p.setRenderHint(hint: QPainter::TextAntialiasing, on: true); |
194 | p.setRenderHint(hint: QPainter::Antialiasing, on: true); |
195 | f.setPointSize(13); |
196 | p.setFont(f); |
197 | QTextOption t; |
198 | t.setAlignment(Qt::AlignCenter); |
199 | p.drawText(r: QRect(0, 0, 16, 16), text: QString(QLatin1Char('A')), o: t); |
200 | return QPixmap::fromImage(image: img); |
201 | } |
202 | |
203 | QIcon QtPropertyBrowserUtils::fontValueIcon(const QFont &f) |
204 | { |
205 | return QIcon(fontValuePixmap(font: f)); |
206 | } |
207 | |
208 | QString QtPropertyBrowserUtils::fontValueText(const QFont &f) |
209 | { |
210 | return QCoreApplication::translate(context: "QtPropertyBrowserUtils" , key: "[%1, %2]" ) |
211 | .arg(a: f.family()).arg(a: f.pointSize()); |
212 | } |
213 | |
214 | QString QtPropertyBrowserUtils::dateFormat() |
215 | { |
216 | QLocale loc; |
217 | QString format = loc.dateFormat(format: QLocale::ShortFormat); |
218 | // Change dd.MM.yy, MM/dd/yy to 4 digit years |
219 | if (format.count(c: QLatin1Char('y')) == 2) |
220 | format.insert(i: format.indexOf(c: QLatin1Char('y')), s: QLatin1String("yy" )); |
221 | return format; |
222 | } |
223 | |
224 | QString QtPropertyBrowserUtils::timeFormat() |
225 | { |
226 | QLocale loc; |
227 | // ShortFormat is missing seconds on UNIX. |
228 | return loc.timeFormat(format: QLocale::LongFormat); |
229 | } |
230 | |
231 | QString QtPropertyBrowserUtils::dateTimeFormat() |
232 | { |
233 | QString format = dateFormat(); |
234 | format += QLatin1Char(' '); |
235 | format += timeFormat(); |
236 | return format; |
237 | } |
238 | |
239 | QtBoolEdit::QtBoolEdit(QWidget *parent) : |
240 | QWidget(parent), |
241 | m_checkBox(new QCheckBox(this)), |
242 | m_textVisible(true) |
243 | { |
244 | QHBoxLayout *lt = new QHBoxLayout; |
245 | if (QApplication::layoutDirection() == Qt::LeftToRight) |
246 | lt->setContentsMargins(left: 4, top: 0, right: 0, bottom: 0); |
247 | else |
248 | lt->setContentsMargins(left: 0, top: 0, right: 4, bottom: 0); |
249 | lt->addWidget(m_checkBox); |
250 | setLayout(lt); |
251 | connect(sender: m_checkBox, SIGNAL(toggled(bool)), receiver: this, SIGNAL(toggled(bool))); |
252 | setFocusProxy(m_checkBox); |
253 | m_checkBox->setText(tr(s: "True" )); |
254 | } |
255 | |
256 | void QtBoolEdit::setTextVisible(bool textVisible) |
257 | { |
258 | if (m_textVisible == textVisible) |
259 | return; |
260 | |
261 | m_textVisible = textVisible; |
262 | if (m_textVisible) |
263 | m_checkBox->setText(isChecked() ? tr(s: "True" ) : tr(s: "False" )); |
264 | else |
265 | m_checkBox->setText(QString()); |
266 | } |
267 | |
268 | Qt::CheckState QtBoolEdit::checkState() const |
269 | { |
270 | return m_checkBox->checkState(); |
271 | } |
272 | |
273 | void QtBoolEdit::setCheckState(Qt::CheckState state) |
274 | { |
275 | m_checkBox->setCheckState(state); |
276 | } |
277 | |
278 | bool QtBoolEdit::isChecked() const |
279 | { |
280 | return m_checkBox->isChecked(); |
281 | } |
282 | |
283 | void QtBoolEdit::setChecked(bool c) |
284 | { |
285 | m_checkBox->setChecked(c); |
286 | if (!m_textVisible) |
287 | return; |
288 | m_checkBox->setText(isChecked() ? tr(s: "True" ) : tr(s: "False" )); |
289 | } |
290 | |
291 | bool QtBoolEdit::blockCheckBoxSignals(bool block) |
292 | { |
293 | return m_checkBox->blockSignals(b: block); |
294 | } |
295 | |
296 | void QtBoolEdit::mousePressEvent(QMouseEvent *event) |
297 | { |
298 | if (event->buttons() == Qt::LeftButton) { |
299 | m_checkBox->click(); |
300 | event->accept(); |
301 | } else { |
302 | QWidget::mousePressEvent(event); |
303 | } |
304 | } |
305 | |
306 | QT_END_NAMESPACE |
307 | |