diff options
author | Thomas Hartmann <Thomas.Hartmann@nokia.com> | 2010-08-12 15:43:13 +0200 |
---|---|---|
committer | Thomas Hartmann <Thomas.Hartmann@nokia.com> | 2010-08-12 15:50:17 +0200 |
commit | 12916161abf29fd5f703c5a950bb3e6fd7b9ba10 (patch) | |
tree | 36bf64ef4fb085cd70990a86e3df1a2e41a66de5 /src/libs/qmleditorwidgets/colorbutton.cpp | |
parent | 2effcaeb2ef9369d01b874e6098bcb709fce7f72 (diff) | |
download | qt-creator-12916161abf29fd5f703c5a950bb3e6fd7b9ba10.tar.gz |
Creating lib: qmleditorwidgets
The library qmleditorwidgets contains widgets and image resources
what are shared between QuickToolBar and QmlDesigner
This library is created to make QuickToolBar independent from
QmlDesigner plugin.
All the widgets I moved to qmleditorwidgets are removed from
QmlDesigner in this patch
Diffstat (limited to 'src/libs/qmleditorwidgets/colorbutton.cpp')
-rw-r--r-- | src/libs/qmleditorwidgets/colorbutton.cpp | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/src/libs/qmleditorwidgets/colorbutton.cpp b/src/libs/qmleditorwidgets/colorbutton.cpp new file mode 100644 index 0000000000..645f380e7f --- /dev/null +++ b/src/libs/qmleditorwidgets/colorbutton.cpp @@ -0,0 +1,152 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include "colorbutton.h" +#include <QPainter> + +static inline QPixmap tilePixMap(int size) +{ + const int checkerbordSize= size; + QPixmap tilePixmap(checkerbordSize * 2, checkerbordSize * 2); + tilePixmap.fill(Qt::white); + QPainter tilePainter(&tilePixmap); + QColor color(220, 220, 220); + tilePainter.fillRect(0, 0, checkerbordSize, checkerbordSize, color); + tilePainter.fillRect(checkerbordSize, checkerbordSize, checkerbordSize, checkerbordSize, color); + return tilePixmap; +} + +static inline bool isColorString(const QString &colorString) +{ + bool ok = true; + if (colorString.size() == 9 && colorString.at(0) == QLatin1Char('#')) { + // #rgba + for (int i = 1; i < 9; ++i) { + const QChar c = colorString.at(i); + if ((c >= QLatin1Char('0') && c <= QLatin1Char('9')) + || (c >= QLatin1Char('a') && c <= QLatin1Char('f')) + || (c >= QLatin1Char('A') && c <= QLatin1Char('F'))) + continue; + ok = false; + break; + } + } else { + ok = QColor::isValidColor(colorString); + } + + return ok; +} + +static inline QColor properColor(const QString &str) +{ + if (str.isEmpty()) + return QColor(); + int lalpha = 255; + QString lcolorStr = str; + if (lcolorStr.at(0) == '#' && lcolorStr.length() == 9) { + QString alphaStr = lcolorStr; + alphaStr.truncate(3); + lcolorStr.remove(0, 3); + lcolorStr = "#" + lcolorStr; + alphaStr.remove(0,1); + bool v; + lalpha = alphaStr.toInt(&v, 16); + if (!v) + lalpha = 255; + } + QColor lcolor(lcolorStr); + if (lcolorStr.contains('#')) + lcolor.setAlpha(lalpha); + return lcolor; +} + +namespace QmlEditorWidgets { + +void ColorButton::setColor(const QString &colorStr) +{ + if (m_colorString == colorStr) + return; + + + setEnabled(isColorString(colorStr)); + + m_colorString = colorStr; + update(); + emit colorChanged(); +} + +QColor ColorButton::convertedColor() const +{ + return properColor(m_colorString); +} + +void ColorButton::paintEvent(QPaintEvent *event) +{ + QToolButton::paintEvent(event); + if (!isEnabled()) + return; + + QColor color = properColor(m_colorString); + + QPainter p(this); + + + QRect r(0, 0, width() - 2, height() - 2); + p.drawTiledPixmap(r.adjusted(1, 1, -1, -1), tilePixMap(9)); + if (isEnabled()) + p.setBrush(color); + else + p.setBrush(Qt::transparent); + + if (color.value() > 80) + p.setPen(QColor(0x444444)); + else + p.setPen(QColor(0x9e9e9e)); + p.drawRect(r.translated(1, 1)); + + if (m_showArrow) { + p.setRenderHint(QPainter::Antialiasing, true); + QVector<QPointF> points; + if (isChecked()) { + points.append(QPointF(2, 3)); + points.append(QPointF(8, 3)); + points.append(QPointF(5, 9)); + } else { + points.append(QPointF(8, 6)); + points.append(QPointF(2, 9)); + points.append(QPointF(2, 3)); + } + p.translate(0.5, 0.5); + p.setBrush(QColor(0xaaaaaa)); + p.setPen(QColor(0x444444)); + p.drawPolygon(points); + } +} + +} //QmlEditorWidgets |