source: trunk/tools/shared/qtpropertybrowser/qtpropertybrowserutils.cpp@ 890

Last change on this file since 890 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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