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

Last change on this file since 235 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 15.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at qt-sales@nokia.com.
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
51#if QT_VERSION >= 0x040400
52QT_BEGIN_NAMESPACE
53#endif
54
55QtCursorDatabase::QtCursorDatabase()
56{
57 appendCursor(Qt::ArrowCursor, QApplication::translate("QtCursorDatabase", "Arrow", 0,
58 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-arrow.png")));
59 appendCursor(Qt::UpArrowCursor, QApplication::translate("QtCursorDatabase", "Up Arrow", 0,
60 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-uparrow.png")));
61 appendCursor(Qt::CrossCursor, QApplication::translate("QtCursorDatabase", "Cross", 0,
62 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-cross.png")));
63 appendCursor(Qt::WaitCursor, QApplication::translate("QtCursorDatabase", "Wait", 0,
64 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-wait.png")));
65 appendCursor(Qt::IBeamCursor, QApplication::translate("QtCursorDatabase", "IBeam", 0,
66 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-ibeam.png")));
67 appendCursor(Qt::SizeVerCursor, QApplication::translate("QtCursorDatabase", "Size Vertical", 0,
68 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-sizev.png")));
69 appendCursor(Qt::SizeHorCursor, QApplication::translate("QtCursorDatabase", "Size Horizontal", 0,
70 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-sizeh.png")));
71 appendCursor(Qt::SizeFDiagCursor, QApplication::translate("QtCursorDatabase", "Size Backslash", 0,
72 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-sizef.png")));
73 appendCursor(Qt::SizeBDiagCursor, QApplication::translate("QtCursorDatabase", "Size Slash", 0,
74 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-sizeb.png")));
75 appendCursor(Qt::SizeAllCursor, QApplication::translate("QtCursorDatabase", "Size All", 0,
76 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-sizeall.png")));
77 appendCursor(Qt::BlankCursor, QApplication::translate("QtCursorDatabase", "Blank", 0,
78 QApplication::UnicodeUTF8), QIcon());
79 appendCursor(Qt::SplitVCursor, QApplication::translate("QtCursorDatabase", "Split Vertical", 0,
80 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-vsplit.png")));
81 appendCursor(Qt::SplitHCursor, QApplication::translate("QtCursorDatabase", "Split Horizontal", 0,
82 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-hsplit.png")));
83 appendCursor(Qt::PointingHandCursor, QApplication::translate("QtCursorDatabase", "Pointing Hand", 0,
84 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-hand.png")));
85 appendCursor(Qt::ForbiddenCursor, QApplication::translate("QtCursorDatabase", "Forbidden", 0,
86 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-forbidden.png")));
87 appendCursor(Qt::OpenHandCursor, QApplication::translate("QtCursorDatabase", "Open Hand", 0,
88 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-openhand.png")));
89 appendCursor(Qt::ClosedHandCursor, QApplication::translate("QtCursorDatabase", "Closed Hand", 0,
90 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-closedhand.png")));
91 appendCursor(Qt::WhatsThisCursor, QApplication::translate("QtCursorDatabase", "What's This", 0,
92 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-whatsthis.png")));
93 appendCursor(Qt::BusyCursor, QApplication::translate("QtCursorDatabase", "Busy", 0,
94 QApplication::UnicodeUTF8), QIcon(QLatin1String(":/trolltech/qtpropertybrowser/images/cursor-busy.png")));
95}
96
97void QtCursorDatabase::appendCursor(Qt::CursorShape shape, const QString &name, const QIcon &icon)
98{
99 if (m_cursorShapeToValue.contains(shape))
100 return;
101 int value = m_cursorNames.count();
102 m_cursorNames.append(name);
103 m_cursorIcons[value] = icon;
104 m_valueToCursorShape[value] = shape;
105 m_cursorShapeToValue[shape] = value;
106}
107
108QStringList QtCursorDatabase::cursorShapeNames() const
109{
110 return m_cursorNames;
111}
112
113QMap<int, QIcon> QtCursorDatabase::cursorShapeIcons() const
114{
115 return m_cursorIcons;
116}
117
118QString QtCursorDatabase::cursorToShapeName(const QCursor &cursor) const
119{
120 int val = cursorToValue(cursor);
121 if (val >= 0)
122 return m_cursorNames.at(val);
123 return QString();
124}
125
126QIcon QtCursorDatabase::cursorToShapeIcon(const QCursor &cursor) const
127{
128 int val = cursorToValue(cursor);
129 return m_cursorIcons.value(val);
130}
131
132int QtCursorDatabase::cursorToValue(const QCursor &cursor) const
133{
134#ifndef QT_NO_CURSOR
135 Qt::CursorShape shape = cursor.shape();
136 if (m_cursorShapeToValue.contains(shape))
137 return m_cursorShapeToValue[shape];
138#endif
139 return -1;
140}
141
142#ifndef QT_NO_CURSOR
143QCursor QtCursorDatabase::valueToCursor(int value) const
144{
145 if (m_valueToCursorShape.contains(value))
146 return QCursor(m_valueToCursorShape[value]);
147 return QCursor();
148}
149#endif
150
151QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b)
152{
153 QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
154 img.fill(0);
155
156 QPainter painter(&img);
157 painter.setCompositionMode(QPainter::CompositionMode_Source);
158 painter.fillRect(0, 0, img.width(), img.height(), b);
159 QColor color = b.color();
160 if (color.alpha() != 255) { // indicate alpha by an inset
161 QBrush opaqueBrush = b;
162 color.setAlpha(255);
163 opaqueBrush.setColor(color);
164 painter.fillRect(img.width() / 4, img.height() / 4,
165 img.width() / 2, img.height() / 2, opaqueBrush);
166 }
167 painter.end();
168 return QPixmap::fromImage(img);
169}
170
171QIcon QtPropertyBrowserUtils::brushValueIcon(const QBrush &b)
172{
173 return QIcon(brushValuePixmap(b));
174}
175
176QString QtPropertyBrowserUtils::colorValueText(const QColor &c)
177{
178 return QApplication::translate("QtPropertyBrowserUtils", "[%1, %2, %3] (%4)", 0, QApplication::UnicodeUTF8)
179 .arg(QString::number(c.red()))
180 .arg(QString::number(c.green()))
181 .arg(QString::number(c.blue()))
182 .arg(QString::number(c.alpha()));
183}
184
185QPixmap QtPropertyBrowserUtils::fontValuePixmap(const QFont &font)
186{
187 QFont f = font;
188 QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
189 img.fill(0);
190 QPainter p(&img);
191 p.setRenderHint(QPainter::TextAntialiasing, true);
192 p.setRenderHint(QPainter::Antialiasing, true);
193 f.setPointSize(13);
194 p.setFont(f);
195 QTextOption t;
196 t.setAlignment(Qt::AlignCenter);
197 p.drawText(QRect(0, 0, 16, 16), QString(QLatin1Char('A')), t);
198 return QPixmap::fromImage(img);
199}
200
201QIcon QtPropertyBrowserUtils::fontValueIcon(const QFont &f)
202{
203 return QIcon(fontValuePixmap(f));
204}
205
206QString QtPropertyBrowserUtils::fontValueText(const QFont &f)
207{
208 return QApplication::translate("QtPropertyBrowserUtils", "[%1, %2]", 0, QApplication::UnicodeUTF8)
209 .arg(f.family())
210 .arg(f.pointSize());
211}
212
213
214QtBoolEdit::QtBoolEdit(QWidget *parent) :
215 QWidget(parent),
216 m_checkBox(new QCheckBox(this)),
217 m_textVisible(true)
218{
219 QHBoxLayout *lt = new QHBoxLayout;
220 if (QApplication::layoutDirection() == Qt::LeftToRight)
221 lt->setContentsMargins(4, 0, 0, 0);
222 else
223 lt->setContentsMargins(0, 0, 4, 0);
224 lt->addWidget(m_checkBox);
225 setLayout(lt);
226 connect(m_checkBox, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
227 setFocusProxy(m_checkBox);
228 m_checkBox->setText(tr("True"));
229}
230
231void QtBoolEdit::setTextVisible(bool textVisible)
232{
233 if (m_textVisible == textVisible)
234 return;
235
236 m_textVisible = textVisible;
237 if (m_textVisible)
238 m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
239 else
240 m_checkBox->setText(QString());
241}
242
243Qt::CheckState QtBoolEdit::checkState() const
244{
245 return m_checkBox->checkState();
246}
247
248void QtBoolEdit::setCheckState(Qt::CheckState state)
249{
250 m_checkBox->setCheckState(state);
251}
252
253bool QtBoolEdit::isChecked() const
254{
255 return m_checkBox->isChecked();
256}
257
258void QtBoolEdit::setChecked(bool c)
259{
260 m_checkBox->setChecked(c);
261 if (!m_textVisible)
262 return;
263 m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
264}
265
266bool QtBoolEdit::blockCheckBoxSignals(bool block)
267{
268 return m_checkBox->blockSignals(block);
269}
270
271void QtBoolEdit::mousePressEvent(QMouseEvent *event)
272{
273 if (event->buttons() == Qt::LeftButton) {
274 m_checkBox->click();
275 event->accept();
276 } else {
277 QWidget::mousePressEvent(event);
278 }
279}
280
281
282QtKeySequenceEdit::QtKeySequenceEdit(QWidget *parent)
283 : QWidget(parent), m_num(0), m_lineEdit(new QLineEdit(this))
284{
285 QHBoxLayout *layout = new QHBoxLayout(this);
286 layout->addWidget(m_lineEdit);
287 layout->setMargin(0);
288 m_lineEdit->installEventFilter(this);
289 m_lineEdit->setReadOnly(true);
290 m_lineEdit->setFocusProxy(this);
291 setFocusPolicy(m_lineEdit->focusPolicy());
292 setAttribute(Qt::WA_InputMethodEnabled);
293}
294
295bool QtKeySequenceEdit::eventFilter(QObject *o, QEvent *e)
296{
297 if (o == m_lineEdit && e->type() == QEvent::ContextMenu) {
298 QContextMenuEvent *c = static_cast<QContextMenuEvent *>(e);
299 QMenu *menu = m_lineEdit->createStandardContextMenu();
300 const QList<QAction *> actions = menu->actions();
301 QListIterator<QAction *> itAction(actions);
302 while (itAction.hasNext()) {
303 QAction *action = itAction.next();
304 action->setShortcut(QKeySequence());
305 QString actionString = action->text();
306 const int pos = actionString.lastIndexOf(QLatin1Char('\t'));
307 if (pos > 0)
308 actionString.remove(pos, actionString.length() - pos);
309 action->setText(actionString);
310 }
311 QAction *actionBefore = 0;
312 if (actions.count() > 0)
313 actionBefore = actions[0];
314 QAction *clearAction = new QAction(tr("Clear Shortcut"), menu);
315 menu->insertAction(actionBefore, clearAction);
316 menu->insertSeparator(actionBefore);
317 clearAction->setEnabled(!m_keySequence.isEmpty());
318 connect(clearAction, SIGNAL(triggered()), this, SLOT(slotClearShortcut()));
319 menu->exec(c->globalPos());
320 delete menu;
321 e->accept();
322 return true;
323 }
324
325 return QWidget::eventFilter(o, e);
326}
327
328void QtKeySequenceEdit::slotClearShortcut()
329{
330 if (m_keySequence.isEmpty())
331 return;
332 setKeySequence(QKeySequence());
333 emit keySequenceChanged(m_keySequence);
334}
335
336void QtKeySequenceEdit::handleKeyEvent(QKeyEvent *e)
337{
338 int nextKey = e->key();
339 if (nextKey == Qt::Key_Control || nextKey == Qt::Key_Shift ||
340 nextKey == Qt::Key_Meta || nextKey == Qt::Key_Alt ||
341 nextKey == Qt::Key_Super_L || nextKey == Qt::Key_AltGr)
342 return;
343
344 nextKey |= translateModifiers(e->modifiers(), e->text());
345 int k0 = m_keySequence[0];
346 int k1 = m_keySequence[1];
347 int k2 = m_keySequence[2];
348 int k3 = m_keySequence[3];
349 switch (m_num) {
350 case 0: k0 = nextKey; k1 = 0; k2 = 0; k3 = 0; break;
351 case 1: k1 = nextKey; k2 = 0; k3 = 0; break;
352 case 2: k2 = nextKey; k3 = 0; break;
353 case 3: k3 = nextKey; break;
354 default: break;
355 }
356 ++m_num;
357 if (m_num > 3)
358 m_num = 0;
359 m_keySequence = QKeySequence(k0, k1, k2, k3);
360 m_lineEdit->setText(m_keySequence.toString(QKeySequence::NativeText));
361 e->accept();
362 emit keySequenceChanged(m_keySequence);
363}
364
365void QtKeySequenceEdit::setKeySequence(const QKeySequence &sequence)
366{
367 if (sequence == m_keySequence)
368 return;
369 m_num = 0;
370 m_keySequence = sequence;
371 m_lineEdit->setText(m_keySequence.toString(QKeySequence::NativeText));
372}
373
374QKeySequence QtKeySequenceEdit::keySequence() const
375{
376 return m_keySequence;
377}
378
379int QtKeySequenceEdit::translateModifiers(Qt::KeyboardModifiers state, const QString &text) const
380{
381 int result = 0;
382 if ((state & Qt::ShiftModifier) && (text.size() == 0 || !text.at(0).isPrint() || text.at(0).isLetter() || text.at(0).isSpace()))
383 result |= Qt::SHIFT;
384 if (state & Qt::ControlModifier)
385 result |= Qt::CTRL;
386 if (state & Qt::MetaModifier)
387 result |= Qt::META;
388 if (state & Qt::AltModifier)
389 result |= Qt::ALT;
390 return result;
391}
392
393void QtKeySequenceEdit::focusInEvent(QFocusEvent *e)
394{
395 m_lineEdit->event(e);
396 m_lineEdit->selectAll();
397 QWidget::focusInEvent(e);
398}
399
400void QtKeySequenceEdit::focusOutEvent(QFocusEvent *e)
401{
402 m_num = 0;
403 m_lineEdit->event(e);
404 QWidget::focusOutEvent(e);
405}
406
407void QtKeySequenceEdit::keyPressEvent(QKeyEvent *e)
408{
409 handleKeyEvent(e);
410 e->accept();
411}
412
413void QtKeySequenceEdit::keyReleaseEvent(QKeyEvent *e)
414{
415 m_lineEdit->event(e);
416}
417
418bool QtKeySequenceEdit::event(QEvent *e)
419{
420 if (e->type() == QEvent::Shortcut ||
421 e->type() == QEvent::ShortcutOverride ||
422 e->type() == QEvent::KeyRelease) {
423 e->accept();
424 return true;
425 }
426 return QWidget::event(e);
427}
428
429
430
431
432#if QT_VERSION >= 0x040400
433QT_END_NAMESPACE
434#endif
Note: See TracBrowser for help on using the repository browser.