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

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 16.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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::appendCursor(Qt::CursorShape shape, const QString &name, const QIcon &icon)
97{
98 if (m_cursorShapeToValue.contains(shape))
99 return;
100 int value = m_cursorNames.count();
101 m_cursorNames.append(name);
102 m_cursorIcons[value] = icon;
103 m_valueToCursorShape[value] = shape;
104 m_cursorShapeToValue[shape] = value;
105}
106
107QStringList QtCursorDatabase::cursorShapeNames() const
108{
109 return m_cursorNames;
110}
111
112QMap<int, QIcon> QtCursorDatabase::cursorShapeIcons() const
113{
114 return m_cursorIcons;
115}
116
117QString QtCursorDatabase::cursorToShapeName(const QCursor &cursor) const
118{
119 int val = cursorToValue(cursor);
120 if (val >= 0)
121 return m_cursorNames.at(val);
122 return QString();
123}
124
125QIcon QtCursorDatabase::cursorToShapeIcon(const QCursor &cursor) const
126{
127 int val = cursorToValue(cursor);
128 return m_cursorIcons.value(val);
129}
130
131int QtCursorDatabase::cursorToValue(const QCursor &cursor) const
132{
133#ifndef QT_NO_CURSOR
134 Qt::CursorShape shape = cursor.shape();
135 if (m_cursorShapeToValue.contains(shape))
136 return m_cursorShapeToValue[shape];
137#endif
138 return -1;
139}
140
141#ifndef QT_NO_CURSOR
142QCursor QtCursorDatabase::valueToCursor(int value) const
143{
144 if (m_valueToCursorShape.contains(value))
145 return QCursor(m_valueToCursorShape[value]);
146 return QCursor();
147}
148#endif
149
150QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b)
151{
152 QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
153 img.fill(0);
154
155 QPainter painter(&img);
156 painter.setCompositionMode(QPainter::CompositionMode_Source);
157 painter.fillRect(0, 0, img.width(), img.height(), b);
158 QColor color = b.color();
159 if (color.alpha() != 255) { // indicate alpha by an inset
160 QBrush opaqueBrush = b;
161 color.setAlpha(255);
162 opaqueBrush.setColor(color);
163 painter.fillRect(img.width() / 4, img.height() / 4,
164 img.width() / 2, img.height() / 2, opaqueBrush);
165 }
166 painter.end();
167 return QPixmap::fromImage(img);
168}
169
170QIcon QtPropertyBrowserUtils::brushValueIcon(const QBrush &b)
171{
172 return QIcon(brushValuePixmap(b));
173}
174
175QString QtPropertyBrowserUtils::colorValueText(const QColor &c)
176{
177 return QApplication::translate("QtPropertyBrowserUtils", "[%1, %2, %3] (%4)", 0, QApplication::UnicodeUTF8)
178 .arg(QString::number(c.red()))
179 .arg(QString::number(c.green()))
180 .arg(QString::number(c.blue()))
181 .arg(QString::number(c.alpha()));
182}
183
184QPixmap QtPropertyBrowserUtils::fontValuePixmap(const QFont &font)
185{
186 QFont f = font;
187 QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
188 img.fill(0);
189 QPainter p(&img);
190 p.setRenderHint(QPainter::TextAntialiasing, true);
191 p.setRenderHint(QPainter::Antialiasing, true);
192 f.setPointSize(13);
193 p.setFont(f);
194 QTextOption t;
195 t.setAlignment(Qt::AlignCenter);
196 p.drawText(QRect(0, 0, 16, 16), QString(QLatin1Char('A')), t);
197 return QPixmap::fromImage(img);
198}
199
200QIcon QtPropertyBrowserUtils::fontValueIcon(const QFont &f)
201{
202 return QIcon(fontValuePixmap(f));
203}
204
205QString QtPropertyBrowserUtils::fontValueText(const QFont &f)
206{
207 return QApplication::translate("QtPropertyBrowserUtils", "[%1, %2]", 0, QApplication::UnicodeUTF8)
208 .arg(f.family())
209 .arg(f.pointSize());
210}
211
212QString QtPropertyBrowserUtils::dateFormat()
213{
214 QLocale loc;
215 return loc.dateFormat(QLocale::ShortFormat);
216}
217
218QString QtPropertyBrowserUtils::timeFormat()
219{
220 QLocale loc;
221 // ShortFormat is missing seconds on UNIX.
222 return loc.timeFormat(QLocale::LongFormat);
223}
224
225QString QtPropertyBrowserUtils::dateTimeFormat()
226{
227 QString format = dateFormat();
228 format += QLatin1Char(' ');
229 format += timeFormat();
230 return format;
231}
232
233QtBoolEdit::QtBoolEdit(QWidget *parent) :
234 QWidget(parent),
235 m_checkBox(new QCheckBox(this)),
236 m_textVisible(true)
237{
238 QHBoxLayout *lt = new QHBoxLayout;
239 if (QApplication::layoutDirection() == Qt::LeftToRight)
240 lt->setContentsMargins(4, 0, 0, 0);
241 else
242 lt->setContentsMargins(0, 0, 4, 0);
243 lt->addWidget(m_checkBox);
244 setLayout(lt);
245 connect(m_checkBox, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
246 setFocusProxy(m_checkBox);
247 m_checkBox->setText(tr("True"));
248}
249
250void QtBoolEdit::setTextVisible(bool textVisible)
251{
252 if (m_textVisible == textVisible)
253 return;
254
255 m_textVisible = textVisible;
256 if (m_textVisible)
257 m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
258 else
259 m_checkBox->setText(QString());
260}
261
262Qt::CheckState QtBoolEdit::checkState() const
263{
264 return m_checkBox->checkState();
265}
266
267void QtBoolEdit::setCheckState(Qt::CheckState state)
268{
269 m_checkBox->setCheckState(state);
270}
271
272bool QtBoolEdit::isChecked() const
273{
274 return m_checkBox->isChecked();
275}
276
277void QtBoolEdit::setChecked(bool c)
278{
279 m_checkBox->setChecked(c);
280 if (!m_textVisible)
281 return;
282 m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
283}
284
285bool QtBoolEdit::blockCheckBoxSignals(bool block)
286{
287 return m_checkBox->blockSignals(block);
288}
289
290void QtBoolEdit::mousePressEvent(QMouseEvent *event)
291{
292 if (event->buttons() == Qt::LeftButton) {
293 m_checkBox->click();
294 event->accept();
295 } else {
296 QWidget::mousePressEvent(event);
297 }
298}
299
300
301QtKeySequenceEdit::QtKeySequenceEdit(QWidget *parent)
302 : QWidget(parent), m_num(0), m_lineEdit(new QLineEdit(this))
303{
304 QHBoxLayout *layout = new QHBoxLayout(this);
305 layout->addWidget(m_lineEdit);
306 layout->setMargin(0);
307 m_lineEdit->installEventFilter(this);
308 m_lineEdit->setReadOnly(true);
309 m_lineEdit->setFocusProxy(this);
310 setFocusPolicy(m_lineEdit->focusPolicy());
311 setAttribute(Qt::WA_InputMethodEnabled);
312}
313
314bool QtKeySequenceEdit::eventFilter(QObject *o, QEvent *e)
315{
316 if (o == m_lineEdit && e->type() == QEvent::ContextMenu) {
317 QContextMenuEvent *c = static_cast<QContextMenuEvent *>(e);
318 QMenu *menu = m_lineEdit->createStandardContextMenu();
319 const QList<QAction *> actions = menu->actions();
320 QListIterator<QAction *> itAction(actions);
321 while (itAction.hasNext()) {
322 QAction *action = itAction.next();
323 action->setShortcut(QKeySequence());
324 QString actionString = action->text();
325 const int pos = actionString.lastIndexOf(QLatin1Char('\t'));
326 if (pos > 0)
327 actionString.remove(pos, actionString.length() - pos);
328 action->setText(actionString);
329 }
330 QAction *actionBefore = 0;
331 if (actions.count() > 0)
332 actionBefore = actions[0];
333 QAction *clearAction = new QAction(tr("Clear Shortcut"), menu);
334 menu->insertAction(actionBefore, clearAction);
335 menu->insertSeparator(actionBefore);
336 clearAction->setEnabled(!m_keySequence.isEmpty());
337 connect(clearAction, SIGNAL(triggered()), this, SLOT(slotClearShortcut()));
338 menu->exec(c->globalPos());
339 delete menu;
340 e->accept();
341 return true;
342 }
343
344 return QWidget::eventFilter(o, e);
345}
346
347void QtKeySequenceEdit::slotClearShortcut()
348{
349 if (m_keySequence.isEmpty())
350 return;
351 setKeySequence(QKeySequence());
352 emit keySequenceChanged(m_keySequence);
353}
354
355void QtKeySequenceEdit::handleKeyEvent(QKeyEvent *e)
356{
357 int nextKey = e->key();
358 if (nextKey == Qt::Key_Control || nextKey == Qt::Key_Shift ||
359 nextKey == Qt::Key_Meta || nextKey == Qt::Key_Alt ||
360 nextKey == Qt::Key_Super_L || nextKey == Qt::Key_AltGr)
361 return;
362
363 nextKey |= translateModifiers(e->modifiers(), e->text());
364 int k0 = m_keySequence[0];
365 int k1 = m_keySequence[1];
366 int k2 = m_keySequence[2];
367 int k3 = m_keySequence[3];
368 switch (m_num) {
369 case 0: k0 = nextKey; k1 = 0; k2 = 0; k3 = 0; break;
370 case 1: k1 = nextKey; k2 = 0; k3 = 0; break;
371 case 2: k2 = nextKey; k3 = 0; break;
372 case 3: k3 = nextKey; break;
373 default: break;
374 }
375 ++m_num;
376 if (m_num > 3)
377 m_num = 0;
378 m_keySequence = QKeySequence(k0, k1, k2, k3);
379 m_lineEdit->setText(m_keySequence.toString(QKeySequence::NativeText));
380 e->accept();
381 emit keySequenceChanged(m_keySequence);
382}
383
384void QtKeySequenceEdit::setKeySequence(const QKeySequence &sequence)
385{
386 if (sequence == m_keySequence)
387 return;
388 m_num = 0;
389 m_keySequence = sequence;
390 m_lineEdit->setText(m_keySequence.toString(QKeySequence::NativeText));
391}
392
393QKeySequence QtKeySequenceEdit::keySequence() const
394{
395 return m_keySequence;
396}
397
398int QtKeySequenceEdit::translateModifiers(Qt::KeyboardModifiers state, const QString &text) const
399{
400 int result = 0;
401 if ((state & Qt::ShiftModifier) && (text.size() == 0 || !text.at(0).isPrint() || text.at(0).isLetter() || text.at(0).isSpace()))
402 result |= Qt::SHIFT;
403 if (state & Qt::ControlModifier)
404 result |= Qt::CTRL;
405 if (state & Qt::MetaModifier)
406 result |= Qt::META;
407 if (state & Qt::AltModifier)
408 result |= Qt::ALT;
409 return result;
410}
411
412void QtKeySequenceEdit::focusInEvent(QFocusEvent *e)
413{
414 m_lineEdit->event(e);
415 m_lineEdit->selectAll();
416 QWidget::focusInEvent(e);
417}
418
419void QtKeySequenceEdit::focusOutEvent(QFocusEvent *e)
420{
421 m_num = 0;
422 m_lineEdit->event(e);
423 QWidget::focusOutEvent(e);
424}
425
426void QtKeySequenceEdit::keyPressEvent(QKeyEvent *e)
427{
428 handleKeyEvent(e);
429 e->accept();
430}
431
432void QtKeySequenceEdit::keyReleaseEvent(QKeyEvent *e)
433{
434 m_lineEdit->event(e);
435}
436
437bool QtKeySequenceEdit::event(QEvent *e)
438{
439 if (e->type() == QEvent::Shortcut ||
440 e->type() == QEvent::ShortcutOverride ||
441 e->type() == QEvent::KeyRelease) {
442 e->accept();
443 return true;
444 }
445 return QWidget::event(e);
446}
447
448QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.