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

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

trunk: Merged in qt 4.6.1 sources.

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