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 
QtCursorDatabase()52 QtCursorDatabase::QtCursorDatabase()
53 {
54     appendCursor(Qt::ArrowCursor, QCoreApplication::translate("QtCursorDatabase", "Arrow"),
55                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-arrow.png")));
56     appendCursor(Qt::UpArrowCursor, QCoreApplication::translate("QtCursorDatabase", "Up Arrow"),
57                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-uparrow.png")));
58     appendCursor(Qt::CrossCursor, QCoreApplication::translate("QtCursorDatabase", "Cross"),
59                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-cross.png")));
60     appendCursor(Qt::WaitCursor, QCoreApplication::translate("QtCursorDatabase", "Wait"),
61                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-wait.png")));
62     appendCursor(Qt::IBeamCursor, QCoreApplication::translate("QtCursorDatabase", "IBeam"),
63                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-ibeam.png")));
64     appendCursor(Qt::SizeVerCursor, QCoreApplication::translate("QtCursorDatabase", "Size Vertical"),
65                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizev.png")));
66     appendCursor(Qt::SizeHorCursor, QCoreApplication::translate("QtCursorDatabase", "Size Horizontal"),
67                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeh.png")));
68     appendCursor(Qt::SizeFDiagCursor, QCoreApplication::translate("QtCursorDatabase", "Size Backslash"),
69                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizef.png")));
70     appendCursor(Qt::SizeBDiagCursor, QCoreApplication::translate("QtCursorDatabase", "Size Slash"),
71                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeb.png")));
72     appendCursor(Qt::SizeAllCursor, QCoreApplication::translate("QtCursorDatabase", "Size All"),
73                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeall.png")));
74     appendCursor(Qt::BlankCursor, QCoreApplication::translate("QtCursorDatabase", "Blank"),
75                  QIcon());
76     appendCursor(Qt::SplitVCursor, QCoreApplication::translate("QtCursorDatabase", "Split Vertical"),
77                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-vsplit.png")));
78     appendCursor(Qt::SplitHCursor, QCoreApplication::translate("QtCursorDatabase", "Split Horizontal"),
79                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-hsplit.png")));
80     appendCursor(Qt::PointingHandCursor, QCoreApplication::translate("QtCursorDatabase", "Pointing Hand"),
81                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-hand.png")));
82     appendCursor(Qt::ForbiddenCursor, QCoreApplication::translate("QtCursorDatabase", "Forbidden"),
83                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-forbidden.png")));
84     appendCursor(Qt::OpenHandCursor, QCoreApplication::translate("QtCursorDatabase", "Open Hand"),
85                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-openhand.png")));
86     appendCursor(Qt::ClosedHandCursor, QCoreApplication::translate("QtCursorDatabase", "Closed Hand"),
87                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-closedhand.png")));
88     appendCursor(Qt::WhatsThisCursor, QCoreApplication::translate("QtCursorDatabase", "What's This"),
89                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-whatsthis.png")));
90     appendCursor(Qt::BusyCursor, QCoreApplication::translate("QtCursorDatabase", "Busy"),
91                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-busy.png")));
92 }
93 
clear()94 void QtCursorDatabase::clear()
95 {
96     m_cursorNames.clear();
97     m_cursorIcons.clear();
98     m_valueToCursorShape.clear();
99     m_cursorShapeToValue.clear();
100 }
101 
appendCursor(Qt::CursorShape shape,const QString & name,const QIcon & icon)102 void QtCursorDatabase::appendCursor(Qt::CursorShape shape, const QString &name, const QIcon &icon)
103 {
104     if (m_cursorShapeToValue.contains(shape))
105         return;
106     const int value = m_cursorNames.count();
107     m_cursorNames.append(name);
108     m_cursorIcons.insert(value, icon);
109     m_valueToCursorShape.insert(value, shape);
110     m_cursorShapeToValue.insert(shape, value);
111 }
112 
cursorShapeNames() const113 QStringList QtCursorDatabase::cursorShapeNames() const
114 {
115     return m_cursorNames;
116 }
117 
cursorShapeIcons() const118 QMap<int, QIcon> QtCursorDatabase::cursorShapeIcons() const
119 {
120     return m_cursorIcons;
121 }
122 
cursorToShapeName(const QCursor & cursor) const123 QString QtCursorDatabase::cursorToShapeName(const QCursor &cursor) const
124 {
125     int val = cursorToValue(cursor);
126     if (val >= 0)
127         return m_cursorNames.at(val);
128     return QString();
129 }
130 
cursorToShapeIcon(const QCursor & cursor) const131 QIcon QtCursorDatabase::cursorToShapeIcon(const QCursor &cursor) const
132 {
133     int val = cursorToValue(cursor);
134     return m_cursorIcons.value(val);
135 }
136 
cursorToValue(const QCursor & cursor) const137 int QtCursorDatabase::cursorToValue(const QCursor &cursor) const
138 {
139 #ifndef QT_NO_CURSOR
140     Qt::CursorShape shape = cursor.shape();
141     if (m_cursorShapeToValue.contains(shape))
142         return m_cursorShapeToValue[shape];
143 #endif
144     return -1;
145 }
146 
147 #ifndef QT_NO_CURSOR
valueToCursor(int value) const148 QCursor QtCursorDatabase::valueToCursor(int value) const
149 {
150     if (m_valueToCursorShape.contains(value))
151         return QCursor(m_valueToCursorShape[value]);
152     return QCursor();
153 }
154 #endif
155 
brushValuePixmap(const QBrush & b)156 QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b)
157 {
158     QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
159     img.fill(0);
160 
161     QPainter painter(&img);
162     painter.setCompositionMode(QPainter::CompositionMode_Source);
163     painter.fillRect(0, 0, img.width(), 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(img.width() / 4, img.height() / 4,
170                          img.width() / 2, img.height() / 2, opaqueBrush);
171     }
172     painter.end();
173     return QPixmap::fromImage(img);
174 }
175 
brushValueIcon(const QBrush & b)176 QIcon QtPropertyBrowserUtils::brushValueIcon(const QBrush &b)
177 {
178     return QIcon(brushValuePixmap(b));
179 }
180 
colorValueText(const QColor & c)181 QString QtPropertyBrowserUtils::colorValueText(const QColor &c)
182 {
183     return QCoreApplication::translate("QtPropertyBrowserUtils", "[%1, %2, %3] (%4)")
184            .arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
185 }
186 
fontValuePixmap(const QFont & font)187 QPixmap QtPropertyBrowserUtils::fontValuePixmap(const QFont &font)
188 {
189     QFont f = font;
190     QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
191     img.fill(0);
192     QPainter p(&img);
193     p.setRenderHint(QPainter::TextAntialiasing, true);
194     p.setRenderHint(QPainter::Antialiasing, true);
195     f.setPointSize(13);
196     p.setFont(f);
197     QTextOption t;
198     t.setAlignment(Qt::AlignCenter);
199     p.drawText(QRect(0, 0, 16, 16), QString(QLatin1Char('A')), t);
200     return QPixmap::fromImage(img);
201 }
202 
fontValueIcon(const QFont & f)203 QIcon QtPropertyBrowserUtils::fontValueIcon(const QFont &f)
204 {
205     return QIcon(fontValuePixmap(f));
206 }
207 
fontValueText(const QFont & f)208 QString QtPropertyBrowserUtils::fontValueText(const QFont &f)
209 {
210     return QCoreApplication::translate("QtPropertyBrowserUtils", "[%1, %2]")
211            .arg(f.family()).arg(f.pointSize());
212 }
213 
dateFormat()214 QString QtPropertyBrowserUtils::dateFormat()
215 {
216     QLocale loc;
217     QString format = loc.dateFormat(QLocale::ShortFormat);
218     // Change dd.MM.yy, MM/dd/yy to 4 digit years
219     if (format.count(QLatin1Char('y')) == 2)
220         format.insert(format.indexOf(QLatin1Char('y')), QLatin1String("yy"));
221     return format;
222 }
223 
timeFormat()224 QString QtPropertyBrowserUtils::timeFormat()
225 {
226     QLocale loc;
227     // ShortFormat is missing seconds on UNIX.
228     return loc.timeFormat(QLocale::LongFormat);
229 }
230 
dateTimeFormat()231 QString QtPropertyBrowserUtils::dateTimeFormat()
232 {
233     QString format = dateFormat();
234     format += QLatin1Char(' ');
235     format += timeFormat();
236     return format;
237 }
238 
QtBoolEdit(QWidget * parent)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(4, 0, 0, 0);
247     else
248         lt->setContentsMargins(0, 0, 4, 0);
249     lt->addWidget(m_checkBox);
250     setLayout(lt);
251     connect(m_checkBox, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
252     setFocusProxy(m_checkBox);
253     m_checkBox->setText(tr("True"));
254 }
255 
setTextVisible(bool textVisible)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("True") : tr("False"));
264     else
265         m_checkBox->setText(QString());
266 }
267 
checkState() const268 Qt::CheckState QtBoolEdit::checkState() const
269 {
270     return m_checkBox->checkState();
271 }
272 
setCheckState(Qt::CheckState state)273 void QtBoolEdit::setCheckState(Qt::CheckState state)
274 {
275     m_checkBox->setCheckState(state);
276 }
277 
isChecked() const278 bool QtBoolEdit::isChecked() const
279 {
280     return m_checkBox->isChecked();
281 }
282 
setChecked(bool c)283 void QtBoolEdit::setChecked(bool c)
284 {
285     m_checkBox->setChecked(c);
286     if (!m_textVisible)
287         return;
288     m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
289 }
290 
blockCheckBoxSignals(bool block)291 bool QtBoolEdit::blockCheckBoxSignals(bool block)
292 {
293     return m_checkBox->blockSignals(block);
294 }
295 
mousePressEvent(QMouseEvent * event)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