util/tools/shared/qtpropertybrowser/qtpropertybrowserutils.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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 
       
    52 QT_BEGIN_NAMESPACE
       
    53 
       
    54 QtCursorDatabase::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 
       
    96 void QtCursorDatabase::clear()
       
    97 {
       
    98     m_cursorNames.clear();
       
    99     m_cursorIcons.clear();
       
   100     m_valueToCursorShape.clear();
       
   101     m_cursorShapeToValue.clear();
       
   102 }
       
   103 
       
   104 void 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 
       
   115 QStringList QtCursorDatabase::cursorShapeNames() const
       
   116 {
       
   117     return m_cursorNames;
       
   118 }
       
   119 
       
   120 QMap<int, QIcon> QtCursorDatabase::cursorShapeIcons() const
       
   121 {
       
   122     return m_cursorIcons;
       
   123 }
       
   124 
       
   125 QString 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 
       
   133 QIcon QtCursorDatabase::cursorToShapeIcon(const QCursor &cursor) const
       
   134 {
       
   135     int val = cursorToValue(cursor);
       
   136     return m_cursorIcons.value(val);
       
   137 }
       
   138 
       
   139 int 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
       
   150 QCursor 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 
       
   158 QPixmap 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 
       
   178 QIcon QtPropertyBrowserUtils::brushValueIcon(const QBrush &b)
       
   179 {
       
   180     return QIcon(brushValuePixmap(b));
       
   181 }
       
   182 
       
   183 QString 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 
       
   192 QPixmap 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 
       
   208 QIcon QtPropertyBrowserUtils::fontValueIcon(const QFont &f)
       
   209 {
       
   210     return QIcon(fontValuePixmap(f));
       
   211 }
       
   212 
       
   213 QString 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 
       
   220 QString QtPropertyBrowserUtils::dateFormat()
       
   221 {
       
   222     QLocale loc;
       
   223     return loc.dateFormat(QLocale::ShortFormat);
       
   224 }
       
   225 
       
   226 QString QtPropertyBrowserUtils::timeFormat()
       
   227 {
       
   228     QLocale loc;
       
   229     // ShortFormat is missing seconds on UNIX.
       
   230     return loc.timeFormat(QLocale::LongFormat);    
       
   231 }
       
   232 
       
   233 QString QtPropertyBrowserUtils::dateTimeFormat()
       
   234 {
       
   235     QString format = dateFormat();
       
   236     format += QLatin1Char(' ');
       
   237     format += timeFormat();
       
   238     return format;
       
   239 }
       
   240 
       
   241 QtBoolEdit::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 
       
   258 void 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 
       
   270 Qt::CheckState QtBoolEdit::checkState() const
       
   271 {
       
   272     return m_checkBox->checkState();
       
   273 }
       
   274 
       
   275 void QtBoolEdit::setCheckState(Qt::CheckState state)
       
   276 {
       
   277     m_checkBox->setCheckState(state);
       
   278 }
       
   279 
       
   280 bool QtBoolEdit::isChecked() const
       
   281 {
       
   282     return m_checkBox->isChecked();
       
   283 }
       
   284 
       
   285 void 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 
       
   293 bool QtBoolEdit::blockCheckBoxSignals(bool block)
       
   294 {
       
   295     return m_checkBox->blockSignals(block);
       
   296 }
       
   297 
       
   298 void QtBoolEdit::mousePressEvent(QMouseEvent *event)
       
   299 {
       
   300     if (event->buttons() == Qt::LeftButton) {
       
   301         m_checkBox->click();
       
   302         event->accept();
       
   303     } else {
       
   304         QWidget::mousePressEvent(event);
       
   305     }
       
   306 }
       
   307 
       
   308 
       
   309 QtKeySequenceEdit::QtKeySequenceEdit(QWidget *parent)
       
   310     : QWidget(parent), m_num(0), m_lineEdit(new QLineEdit(this))
       
   311 {
       
   312     QHBoxLayout *layout = new QHBoxLayout(this);
       
   313     layout->addWidget(m_lineEdit);
       
   314     layout->setMargin(0);
       
   315     m_lineEdit->installEventFilter(this);
       
   316     m_lineEdit->setReadOnly(true);
       
   317     m_lineEdit->setFocusProxy(this);
       
   318     setFocusPolicy(m_lineEdit->focusPolicy());
       
   319     setAttribute(Qt::WA_InputMethodEnabled);
       
   320 }
       
   321 
       
   322 bool QtKeySequenceEdit::eventFilter(QObject *o, QEvent *e)
       
   323 {
       
   324     if (o == m_lineEdit && e->type() == QEvent::ContextMenu) {
       
   325         QContextMenuEvent *c = static_cast<QContextMenuEvent *>(e);
       
   326         QMenu *menu = m_lineEdit->createStandardContextMenu();
       
   327         const QList<QAction *> actions = menu->actions();
       
   328         QListIterator<QAction *> itAction(actions);
       
   329         while (itAction.hasNext()) {
       
   330             QAction *action = itAction.next();
       
   331             action->setShortcut(QKeySequence());
       
   332             QString actionString = action->text();
       
   333             const int pos = actionString.lastIndexOf(QLatin1Char('\t'));
       
   334             if (pos > 0)
       
   335                 actionString.remove(pos, actionString.length() - pos);
       
   336             action->setText(actionString);
       
   337         }
       
   338         QAction *actionBefore = 0;
       
   339         if (actions.count() > 0)
       
   340             actionBefore = actions[0];
       
   341         QAction *clearAction = new QAction(tr("Clear Shortcut"), menu);
       
   342         menu->insertAction(actionBefore, clearAction);
       
   343         menu->insertSeparator(actionBefore);
       
   344         clearAction->setEnabled(!m_keySequence.isEmpty());
       
   345         connect(clearAction, SIGNAL(triggered()), this, SLOT(slotClearShortcut()));
       
   346         menu->exec(c->globalPos());
       
   347         delete menu;
       
   348         e->accept();
       
   349         return true;
       
   350     }
       
   351 
       
   352     return QWidget::eventFilter(o, e);
       
   353 }
       
   354 
       
   355 void QtKeySequenceEdit::slotClearShortcut()
       
   356 {
       
   357     if (m_keySequence.isEmpty())
       
   358         return;
       
   359     setKeySequence(QKeySequence());
       
   360     emit keySequenceChanged(m_keySequence);
       
   361 }
       
   362 
       
   363 void QtKeySequenceEdit::handleKeyEvent(QKeyEvent *e)
       
   364 {
       
   365     int nextKey = e->key();
       
   366     if (nextKey == Qt::Key_Control || nextKey == Qt::Key_Shift ||
       
   367             nextKey == Qt::Key_Meta || nextKey == Qt::Key_Alt ||
       
   368             nextKey == Qt::Key_Super_L || nextKey == Qt::Key_AltGr)
       
   369         return;
       
   370 
       
   371     nextKey |= translateModifiers(e->modifiers(), e->text());
       
   372     int k0 = m_keySequence[0];
       
   373     int k1 = m_keySequence[1];
       
   374     int k2 = m_keySequence[2];
       
   375     int k3 = m_keySequence[3];
       
   376     switch (m_num) {
       
   377         case 0: k0 = nextKey; k1 = 0; k2 = 0; k3 = 0; break;
       
   378         case 1: k1 = nextKey; k2 = 0; k3 = 0; break;
       
   379         case 2: k2 = nextKey; k3 = 0; break;
       
   380         case 3: k3 = nextKey; break;
       
   381         default: break;
       
   382     }
       
   383     ++m_num;
       
   384     if (m_num > 3)
       
   385         m_num = 0;
       
   386     m_keySequence = QKeySequence(k0, k1, k2, k3);
       
   387     m_lineEdit->setText(m_keySequence.toString(QKeySequence::NativeText));
       
   388     e->accept();
       
   389     emit keySequenceChanged(m_keySequence);
       
   390 }
       
   391 
       
   392 void QtKeySequenceEdit::setKeySequence(const QKeySequence &sequence)
       
   393 {
       
   394     if (sequence == m_keySequence)
       
   395         return;
       
   396     m_num = 0;
       
   397     m_keySequence = sequence;
       
   398     m_lineEdit->setText(m_keySequence.toString(QKeySequence::NativeText));
       
   399 }
       
   400 
       
   401 QKeySequence QtKeySequenceEdit::keySequence() const
       
   402 {
       
   403     return m_keySequence;
       
   404 }
       
   405 
       
   406 int QtKeySequenceEdit::translateModifiers(Qt::KeyboardModifiers state, const QString &text) const
       
   407 {
       
   408     int result = 0;
       
   409     if ((state & Qt::ShiftModifier) && (text.size() == 0 || !text.at(0).isPrint() || text.at(0).isLetter() || text.at(0).isSpace()))
       
   410         result |= Qt::SHIFT;
       
   411     if (state & Qt::ControlModifier)
       
   412         result |= Qt::CTRL;
       
   413     if (state & Qt::MetaModifier)
       
   414         result |= Qt::META;
       
   415     if (state & Qt::AltModifier)
       
   416         result |= Qt::ALT;
       
   417     return result;
       
   418 }
       
   419 
       
   420 void QtKeySequenceEdit::focusInEvent(QFocusEvent *e)
       
   421 {
       
   422     m_lineEdit->event(e);
       
   423     m_lineEdit->selectAll();
       
   424     QWidget::focusInEvent(e);
       
   425 }
       
   426 
       
   427 void QtKeySequenceEdit::focusOutEvent(QFocusEvent *e)
       
   428 {
       
   429     m_num = 0;
       
   430     m_lineEdit->event(e);
       
   431     QWidget::focusOutEvent(e);
       
   432 }
       
   433 
       
   434 void QtKeySequenceEdit::keyPressEvent(QKeyEvent *e)
       
   435 {
       
   436     handleKeyEvent(e);
       
   437     e->accept();
       
   438 }
       
   439 
       
   440 void QtKeySequenceEdit::keyReleaseEvent(QKeyEvent *e)
       
   441 {
       
   442     m_lineEdit->event(e);
       
   443 }
       
   444 
       
   445 bool QtKeySequenceEdit::event(QEvent *e)
       
   446 {
       
   447     if (e->type() == QEvent::Shortcut ||
       
   448             e->type() == QEvent::ShortcutOverride  ||
       
   449             e->type() == QEvent::KeyRelease) {
       
   450         e->accept();
       
   451         return true;
       
   452     }
       
   453     return QWidget::event(e);
       
   454 }
       
   455 
       
   456 QT_END_NAMESPACE