Commit 12916161 authored by Thomas Hartmann's avatar Thomas Hartmann

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
parent 2effcaeb
......@@ -10,5 +10,7 @@ SUBDIRS = \
utils/process_stub.pro \
cplusplus \
qmljs \
qmljsdebugger \
qmleditorwidgets \
symbianutils \
3rdparty
/**************************************************************************
**
** 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 "colorbox.h"
#include <QPainter>
#include <QMouseEvent>
static inline QString properName(const QColor &color)
{
QString s;
if (color.alpha() == 255)
s.sprintf("#%02x%02x%02x", color.red(), color.green(), color.blue());
else
s.sprintf("#%02x%02x%02x%02x", color.alpha(), color.red(), color.green(), color.blue());
return s;
}
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;
}
static inline int clamp(int x, int lower, int upper)
{
if (x < lower)
x = lower;
if (x > upper)
x = upper;
return x;
}
namespace QmlEditorWidgets {
void ColorBox::setHue(int newHue)
{
if (m_color.hsvHue() == newHue)
return;
int oldAlpha = m_color.alpha();
m_color.setHsv(newHue,m_color.hsvSaturation(),m_color.value());
m_color.setAlpha(oldAlpha);
update();
emit hueChanged();
emit colorChanged();
}
int ColorBox::hue() const
{
int retval = m_color.hsvHue();
if (retval<0) retval=0;
if (retval>359) retval=359;
return retval;
}
void ColorBox::setAlpha(int newAlpha)
{
if (m_color.alpha() == newAlpha)
return;
m_color.setAlpha(newAlpha);
update();
emit alphaChanged();
emit colorChanged();
}
QString ColorBox::strColor() const
{
return properName(m_color);
}
void ColorBox::setStrColor(const QString &colorStr)
{
if (properName(m_color) == colorStr)
return;
setColor(properColor(colorStr));
}
void ColorBox::setColor(const QColor &color)
{
if (m_color == color)
return;
int oldsaturation = m_color.hsvSaturation();
int oldvalue = m_color.value();
int oldhue = m_color.hsvHue();
int oldAlpha = m_color.alpha();
m_color=color;
update();
if (oldhue != m_color.hsvHue()) emit hueChanged();
if (oldsaturation != saturation()) emit saturationChanged();
if (oldvalue != value()) emit valueChanged();
if (oldAlpha != alpha()) emit alphaChanged();
}
void ColorBox::setSaturation(int newsaturation)
{
if (m_color.hsvSaturation()==newsaturation) return;
int oldAlpha = m_color.alpha();
m_color.setHsv(m_color.hsvHue(),newsaturation,m_color.value());
m_color.setAlpha(oldAlpha);
update();
emit saturationChanged();
emit colorChanged();
}
void ColorBox::setCurrent(int x, int y)
{
QColor newColor;
x = clamp(x, 0, 120);
y = clamp(y, 0, 120);
int oldAlpha = m_color.alpha();
newColor.setHsv(hue(), (x*255) / 120, 255 - (y*255) / 120);
newColor.setAlpha(oldAlpha);
setColor(newColor);
}
void ColorBox::setValue(int newvalue)
{
if (m_color.value()==newvalue) return;
int oldAlpha = m_color.alpha();
m_color.setHsv(m_color.hsvHue(),m_color.hsvSaturation(),newvalue);
m_color.setAlpha(oldAlpha);
update();
emit valueChanged();
emit colorChanged();
}
void ColorBox::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter p(this);
if ((m_color.saturation()>1) && (m_color.value()>1))
m_saturatedColor.setHsv(m_color.hsvHue(),255,255);
if ((hue() != m_lastHue) || (m_cache.isNull())) {
m_lastHue = hue();
int fixedHue = clamp(m_lastHue, 0, 359);
m_cache = QPixmap(120, 120);
int height = 120;
int width = 120;
QPainter chacheP(&m_cache);
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
QColor c;
c.setHsv(fixedHue, (x*255) / width, 255 - (y*255) / height);
chacheP.setPen(c);
chacheP.drawPoint(x ,y);
}
}
p.drawPixmap(5, 5, m_cache);
int x = clamp(m_color.hsvSaturationF() * 120, 0, 119) + 5;
int y = clamp(120 - m_color.valueF() * 120, 0, 119) + 5;
p.setPen(QColor(255, 255, 255, 50));
p.drawLine(5, y, x-1, y);
p.drawLine(x+1, y, width()-7, y);
p.drawLine(x, 5, x, y-1);
p.drawLine(x, y+1, x, height()-7);
}
void ColorBox::mousePressEvent(QMouseEvent *e)
{
// The current cell marker is set to the cell the mouse is pressed in
QPoint pos = e->pos();
m_mousePressed = true;
setCurrent(pos.x() - 5, pos.y() - 5);
}
void ColorBox::mouseReleaseEvent(QMouseEvent * /* event */)
{
if (m_mousePressed)
emit colorChanged();
m_mousePressed = false;
}
void ColorBox::mouseMoveEvent(QMouseEvent *e)
{
if (!m_mousePressed)
return;
QPoint pos = e->pos();
setCurrent(pos.x() - 5, pos.y() - 5);
}
} //QmlEditorWidgets
/**************************************************************************
**
** 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.
**
**************************************************************************/
#ifndef COLORBOX_H
#define COLORBOX_H
#include <qmleditorwidgets_global.h>
#include <QtGui/QWidget>
#include <QToolButton>
#include <qdeclarative.h>
namespace QmlEditorWidgets {
class QMLEDITORWIDGETS_EXPORT ColorBox : public QWidget
{
Q_OBJECT
Q_PROPERTY(QString strColor READ strColor WRITE setStrColor NOTIFY colorChanged)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(int hue READ hue WRITE setHue NOTIFY hueChanged)
Q_PROPERTY(int saturation READ saturation WRITE setSaturation NOTIFY saturationChanged)
Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
Q_PROPERTY(int alpha READ alpha WRITE setAlpha NOTIFY alphaChanged)
public:
ColorBox(QWidget *parent = 0) : QWidget(parent), m_color(Qt::white), m_saturatedColor(Qt::white), m_lastHue(0)
{
setFixedWidth(130);
setFixedHeight(130);
}
void setHue(int newHue);
int hue() const;
void setAlpha(int newAlpha);
int alpha() const { return m_color.alpha(); }
void setStrColor(const QString &colorStr);
void setColor(const QColor &color);
QString strColor() const;
QColor color() const { return m_color; }
int saturation() const { return m_color.hsvSaturation(); }
void setSaturation(int newsaturation);
int value() const { return m_color.value(); }
void setValue(int newvalue);
signals:
void colorChanged();
void hueChanged();
void saturationChanged();
void valueChanged();
void alphaChanged();
protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void setCurrent(int x, int y);
private:
QColor m_color;
QColor m_saturatedColor;
bool m_mousePressed;
int m_lastHue;
QPixmap m_cache;
};
} //QmlEditorWidgets
QML_DECLARE_TYPE(QmlEditorWidgets::ColorBox);
#endif //COLORBOX_H
/**************************************************************************
**
** 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
/**************************************************************************
**
** 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.
**
**************************************************************************/