diff --git a/src/plugins/qmldesigner/components/propertyeditor/colorwidget.cpp b/src/plugins/qmldesigner/components/propertyeditor/colorwidget.cpp index 6db5a6e28b6a722f7165fd7ee8ebbd40eab941ea..a35f7523fcbd6f760531af693d5f0a2479f703c4 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/colorwidget.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/colorwidget.cpp @@ -28,12 +28,6 @@ **************************************************************************/ #include "colorwidget.h" -#include <QHBoxLayout> -#include <QLabel> -#include <QToolButton> -#include <QGradient> -#include <QPainter> -#include <QtDebug> #include <modelnode.h> #include <abstractview.h> #include <nodeproperty.h> @@ -44,173 +38,353 @@ #include <QGradient> #include <metainfo.h> -namespace QmlDesigner { +#include <QHBoxLayout> +#include <QLabel> +#include <QToolButton> +#include <QGradient> +#include <QPainter> - void ColorWidget::registerDeclarativeTypes() { - qmlRegisterType<QmlDesigner::ColorButton>("Bauhaus",1,0,"ColorButton"); - qmlRegisterType<QmlDesigner::HueControl>("Bauhaus",1,0,"HueControl"); - qmlRegisterType<QmlDesigner::ColorBox>("Bauhaus",1,0,"ColorBox"); - qmlRegisterType<QmlDesigner::GradientLine>("Bauhaus",1,0,"GradientLine"); +static inline int clamp(int x, int lower, int upper) +{ + if (x < lower) + x = lower; + if (x > upper) + x = upper; + return x; +} + +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; +} + +inline QColor properColor(const QString &str) +{ + 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); + lcolor.setAlpha(lalpha); + return lcolor; +} - void ColorButton::paintEvent(QPaintEvent *event) - { - QToolButton::paintEvent(event); - if (!isEnabled()) - return; +namespace QmlDesigner { - QColor color(m_colorString); - - QPainter p(this); - - QRect r(0, 0, width(), height()); - if (isEnabled()) - p.setBrush(color); - else - p.setBrush(Qt::transparent); - p.setPen(Qt::black); - - if (!m_noColor) { - p.drawRect(r); - } else { - p.fillRect(r, Qt::white); - p.fillRect(0, 0, width() /2, height() /2, QColor(Qt::gray)); - p.fillRect(width() /2, height() /2, width() /2, height() /2, QColor(Qt::gray)); - p.setBrush(Qt::transparent); - p.drawRect(r); - } +void ColorWidget::registerDeclarativeTypes() { + qmlRegisterType<QmlDesigner::ColorButton>("Bauhaus",1,0,"ColorButton"); + qmlRegisterType<QmlDesigner::HueControl>("Bauhaus",1,0,"HueControl"); + qmlRegisterType<QmlDesigner::ColorBox>("Bauhaus",1,0,"ColorBox"); + qmlRegisterType<QmlDesigner::GradientLine>("Bauhaus",1,0,"GradientLine"); +} +void ColorButton::setColor(const QString &colorStr) +{ + if (m_colorString == colorStr) + return; - 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.setPen("#707070"); - p.setBrush(Qt::white); - p.drawPolygon(points); + m_colorString = colorStr; + update(); + emit colorChanged(); +} + +void ColorButton::paintEvent(QPaintEvent *event) +{ + QToolButton::paintEvent(event); + if (!isEnabled()) + return; + + QColor color(m_colorString); + + QPainter p(this); + + QRect r(0, 0, width(), height()); + if (isEnabled()) + p.setBrush(color); + else + p.setBrush(Qt::transparent); + p.setPen(Qt::black); + + if (!m_noColor) { + p.drawRect(r); + } else { + p.fillRect(r, Qt::white); + p.fillRect(0, 0, width() /2, height() /2, QColor(Qt::gray)); + p.fillRect(width() /2, height() /2, width() /2, height() /2, QColor(Qt::gray)); + p.setBrush(Qt::transparent); + p.drawRect(r); } - void HueControl::setCurrent(int y) - { - if (y<0) y=0; - if (y>120) y=120; - int oldAlpha = m_color.alpha(); - m_color.setHsv((y * 359)/120, m_color.hsvSaturation(), m_color.value()); - m_color.setAlpha(oldAlpha); - update(); // redraw pointer - emit hueChanged(); + 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.setPen("#707070"); + p.setBrush(Qt::white); + p.drawPolygon(points); +} - void HueControl::paintEvent(QPaintEvent *event) - { - QWidget::paintEvent(event); - QPainter p(this); +void HueControl::setCurrent(int y) +{ + y = clamp(y, 0, 120); + int oldAlpha = m_color.alpha(); + m_color.setHsv((y * 359)/120, m_color.hsvSaturation(), m_color.value()); + m_color.setAlpha(oldAlpha); + update(); // redraw pointer + emit hueChanged(); +} - int localHeight = 120; +void HueControl::setHue(int newHue) +{ + if (m_color.hsvHue() == newHue) + return; + m_color.setHsv(newHue, m_color.hsvSaturation(), m_color.value()); + update(); + emit hueChanged(); +} - if (m_cache.isNull()) { - m_cache = QPixmap(10, localHeight); +void HueControl::paintEvent(QPaintEvent *event) +{ + QWidget::paintEvent(event); - QPainter cacheP(&m_cache); + QPainter p(this); - for (int i = 0; i < localHeight; i++) - { - QColor c; - c.setHsv( (i*359) / 120.0, 255,255); - cacheP.fillRect(0, i, 10, i + 1, c); - } + int localHeight = 120; + + if (m_cache.isNull()) { + m_cache = QPixmap(10, localHeight); + + QPainter cacheP(&m_cache); + + for (int i = 0; i < localHeight; i++) + { + QColor c; + c.setHsv( (i*359) / 120.0, 255,255); + cacheP.fillRect(0, i, 10, i + 1, c); } + } - p.drawPixmap(10, 5, m_cache); + p.drawPixmap(10, 5, m_cache); - QVector<QPointF> points; + QVector<QPointF> points; - int y = m_color.hueF() * 120 + 5; + int y = m_color.hueF() * 120 + 5; - points.append(QPointF(15, y)); - points.append(QPointF(25, y + 5)); - points.append(QPointF(25, y - 5)); + points.append(QPointF(15, y)); + points.append(QPointF(25, y + 5)); + points.append(QPointF(25, y - 5)); - p.setPen(Qt::black); - p.setBrush(Qt::NoBrush); - p.drawRect(QRect(0, 0, width() - 1, height() - 1).adjusted(10, 5, -20, -5)); + p.setPen(Qt::black); + p.setBrush(Qt::NoBrush); + p.drawRect(QRect(0, 0, width() - 1, height() - 1).adjusted(10, 5, -20, -5)); - p.setPen(Qt::black); - p.setBrush(QColor("#707070")); - p.drawPolygon(points); - } + p.setPen(Qt::black); + p.setBrush(QColor("#707070")); + p.drawPolygon(points); +} - void ColorBox::setCurrent(int x, int y) - { - QColor newColor; - if (x<0) x=0; - if (x>120) x=120; - if (y<0) y=0; - if (y>120) y=120; - int oldAlpha = m_color.alpha(); - newColor.setHsv(hue(), (x*255) / 120, 255 - (y*255) / 120); - newColor.setAlpha(oldAlpha); - setColor(newColor); - } +void HueControl::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.y() - 5); +} + +void HueControl::mouseReleaseEvent(QMouseEvent * /* event */) +{ + m_mousePressed = false; +} + +void HueControl::mouseMoveEvent(QMouseEvent *e) +{ + if (!m_mousePressed) + return; + QPoint pos = e->pos(); + setCurrent(pos.y() - 5); +} + +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; - void ColorBox::paintEvent(QPaintEvent *event) - { - QWidget::paintEvent(event); + m_color.setAlpha(newAlpha); + update(); + emit alphaChanged(); + emit colorChanged(); +} - QPainter p(this); +QString ColorBox::strColor() const +{ + return properName(m_color); +} - if ((m_color.saturation()>1) && (m_color.value()>1)) - m_saturatedColor.setHsv(m_color.hsvHue(),255,255); +void ColorBox::setStrColor(const QString &colorStr) +{ + if (properName(m_color) == colorStr) + return; - if ((hue() != m_lastHue) || (m_cache.isNull())) { - m_lastHue = hue(); + setColor(properColor(colorStr)); +} - int fixedHue = m_lastHue; +void ColorBox::setColor(const QColor &color) +{ + if (m_color == color) + return; - if (fixedHue<0) fixedHue=0; - if (fixedHue>359) fixedHue=359; + 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(); + emit colorChanged(); +} - m_cache = QPixmap(120, 120); +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(); +} - int height = 120; - int width = 120; +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); +} - QPainter chacheP(&m_cache); +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(); +} - for (int y = 0; y < height; y++) - for (int x = 0; x < width; x++) - { +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) / 120, 255 - (y*255) / 120); chacheP.setPen(c); chacheP.drawPoint(x ,y); } - } + } - p.drawPixmap(5, 5, m_cache); + p.drawPixmap(5, 5, m_cache); - int x = m_color.hsvSaturationF() * 120 + 5; - int y = 120 - m_color.valueF() * 120 + 5; + int x = clamp(m_color.hsvSaturationF() * 120, 0, 120) + 5; + int y = clamp(120 - m_color.valueF() * 120, 0, 120) + 5; - if (x<5) x=5; - if (x>125) x=125; - if (y<5) y=5; - if (y>125) y=125; + p.setPen(Qt::white); + p.drawEllipse(x - 2, y - 2, 4, 4); - p.setPen(Qt::white); - p.drawEllipse(x - 2, y - 2, 4, 4); + p.setPen(Qt::black); + p.drawRect(QRect(0, 0, width() - 1, height() -1).adjusted(4, 4, -4, -4)); +} - p.setPen(Qt::black); - p.drawRect(QRect(0, 0, width() - 1, height() -1).adjusted(4, 4, -4, -4)); - } +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 */) +{ + m_mousePressed = false; +} + +void ColorBox::mouseMoveEvent(QMouseEvent *e) +{ + if (!m_mousePressed) + return; + QPoint pos = e->pos(); + setCurrent(pos.x() - 5, pos.y() - 5); +} void GradientLine::setItemNode(const QVariant &itemNode) { @@ -229,6 +403,40 @@ static inline QColor invertColor(const QColor color) return c; } +GradientLine::GradientLine(QWidget *parent) : QWidget(parent), m_activeColor(Qt::black), m_gradientName("gradient"), m_dragActive(false), m_yOffset(0), m_create(false), m_active(false) +{ + setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); + setFocusPolicy(Qt::StrongFocus); + setFixedHeight(50); + setMinimumWidth(160); + resize(160, 50); + m_colorList << m_activeColor << QColor(Qt::white); + m_stops << 0 << 1; + updateGradient(); + setCurrentIndex(0); +} + +void GradientLine::setGradientName(const QString &newName) +{ + if (newName == m_gradientName) + return; + m_gradientName = newName; + setup(); + emit gradientNameChanged(); +} + +void GradientLine::setActiveColor(const QColor &newColor) +{ + if (newColor.name() == m_activeColor.name() && newColor.alpha() == m_activeColor.alpha()) + return; + + m_activeColor = newColor; + m_colorList.removeAt(currentColorIndex()); + m_colorList.insert(currentColorIndex(), m_activeColor); + updateGradient(); + update(); +} + void GradientLine::setupGradient() { ModelNode modelNode = m_itemNode.modelNode(); @@ -255,6 +463,27 @@ void GradientLine::setupGradient() updateGradient(); } +void GradientLine::deleteGradient() +{ + if (!m_itemNode.isValid()) + return; + + if (!m_itemNode.modelNode().metaInfo().hasProperty(m_gradientName)) + return; + + ModelNode modelNode = m_itemNode.modelNode(); + + if (m_itemNode.isInBaseState()) { + if (modelNode.hasProperty(m_gradientName)) { + RewriterTransaction transaction; + m_itemNode.modelNode().removeProperty(m_gradientName); //### there is atm a bug in the node instances which lead to a crash if using destroy() + /*ModelNode gradientNode = modelNode.nodeProperty(m_gradientName).modelNode(); + if (QmlObjectNode(gradientNode).isValid()) + QmlObjectNode(gradientNode).destroy();*/ + } + } +} + bool GradientLine::event(QEvent *event) { if (event->type() == QEvent::ShortcutOverride) @@ -350,7 +579,6 @@ void GradientLine::mousePressEvent(QMouseEvent *event) } setFocus(Qt::MouseFocusReason); event->accept(); - //QWidget::mousePressEvent(event); } void GradientLine::mouseReleaseEvent(QMouseEvent *event) @@ -376,7 +604,6 @@ void GradientLine::mouseReleaseEvent(QMouseEvent *event) update(); updateGradient(); event->accept(); - //QWidget::mouseReleaseEvent(event); } void GradientLine::mouseMoveEvent(QMouseEvent *event) @@ -414,7 +641,7 @@ void GradientLine::mouseMoveEvent(QMouseEvent *event) } void GradientLine::setup() -{ +{ } @@ -445,8 +672,9 @@ void GradientLine::updateGradient() ModelNode modelNode = m_itemNode.modelNode(); if (m_itemNode.isInBaseState()) { - if (modelNode.hasProperty(m_gradientName)) + if (modelNode.hasProperty(m_gradientName)) { modelNode.removeProperty(m_gradientName); + } ModelNode gradientNode = modelNode.view()->createModelNode("Qt/Gradient", 4, 6); @@ -473,4 +701,14 @@ void GradientLine::updateGradient() } } +void GradientLine::setCurrentIndex(int i) +{ + if (i == m_colorIndex) + return; + m_colorIndex = i; + setActiveColor(m_colorList.at(i)); + emit activeColorChanged(); + update(); +} + } diff --git a/src/plugins/qmldesigner/components/propertyeditor/colorwidget.h b/src/plugins/qmldesigner/components/propertyeditor/colorwidget.h index f03b8d297a6bcad0a4ea68250a9808ca11ca0658..3168618b1562ed4aeb04e7a60f414d29ac406c9b 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/colorwidget.h +++ b/src/plugins/qmldesigner/components/propertyeditor/colorwidget.h @@ -54,28 +54,11 @@ Q_OBJECT Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(bool noColor READ noColor WRITE setNoColor) - public: + ColorButton(QWidget *parent = 0) : QToolButton (parent), m_colorString("#ffffff"), m_noColor(false) {} - ColorButton(QWidget *parent = 0) : QToolButton (parent), m_colorString("#ffffff"), m_noColor(false) - { - } - - void setColor(const QString &colorStr) - { - if (m_colorString == colorStr) - return; - - m_colorString = colorStr; - update(); - emit colorChanged(); - } - - QString color() const - { - return m_colorString; - } - + void setColor(const QString &colorStr); + QString color() const { return m_colorString; } bool noColor() const { return m_noColor; } void setNoColor(bool f) { m_noColor = f; update(); } @@ -89,36 +72,6 @@ private: bool m_noColor; }; -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; -} - -inline QColor properColor(const QString &str) -{ - 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); - lcolor.setAlpha(lalpha); - return lcolor; -} - class ColorBox : public QWidget { Q_OBJECT @@ -131,117 +84,24 @@ 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); + } -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) -{ - 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 hue() const -{ - int retval = m_color.hsvHue(); - if (retval<0) retval=0; - if (retval>359) retval=359; - return retval; -} - -void setAlpha(int newAlpha) -{ - if (m_color.alpha() == newAlpha) - return; - - m_color.setAlpha(newAlpha); - update(); - emit alphaChanged(); - emit colorChanged(); -} - -int alpha() const -{ - return m_color.alpha(); -} - -void setStrColor(const QString &colorStr) -{ - if (properName(m_color) == colorStr) - return; - - setColor(properColor(colorStr)); -} - -void 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(); - emit colorChanged(); -} - -QString strColor() const -{ - return properName(m_color); -} - -QColor color() const -{ - return m_color; -} - -int saturation() const -{ - return m_color.hsvSaturation(); -} - -void 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(); -} - -int value() const -{ - return m_color.value(); -} - -void 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 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(); @@ -253,29 +113,10 @@ signals: protected: void paintEvent(QPaintEvent *event); - void 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 mouseReleaseEvent(QMouseEvent * /* event */) - { - m_mousePressed = false; - } - -void mouseMoveEvent(QMouseEvent *e) -{ - if (!m_mousePressed) - return; - QPoint pos = e->pos(); - setCurrent(pos.x() - 5, pos.y() - 5); -} - -void setCurrent(int x, int y); - + void mousePressEvent(QMouseEvent *); + void mouseReleaseEvent(QMouseEvent *); + void mouseMoveEvent(QMouseEvent *); + void setCurrent(int x, int y); private: QColor m_color; @@ -293,55 +134,24 @@ Q_PROPERTY(qreal hue READ hue WRITE setHue NOTIFY hueChanged) public: -HueControl(QWidget *parent = 0) : QWidget(parent), m_color(Qt::white), m_mousePressed(false) -{ - setFixedWidth(40); - setFixedHeight(130); -} + HueControl(QWidget *parent = 0) : QWidget(parent), m_color(Qt::white), m_mousePressed(false) + { + setFixedWidth(40); + setFixedHeight(130); + } -void setHue(int newHue) -{ - if (m_color.hsvHue() == newHue) - return; - m_color.setHsv(newHue, m_color.hsvSaturation(), m_color.value()); - update(); - emit hueChanged(); -} - -int hue() const -{ - return m_color.hsvHue(); -} + void setHue(int newHue); + int hue() const { return m_color.hsvHue(); } signals: void hueChanged(); protected: - void paintEvent(QPaintEvent *event); - - void 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.y() - 5); - } - - void mouseReleaseEvent(QMouseEvent * /* event */) - { - m_mousePressed = false; - } - -void mouseMoveEvent(QMouseEvent *e) -{ - if (!m_mousePressed) - return; - QPoint pos = e->pos(); - setCurrent(pos.y() - 5); -} - -void setCurrent(int y); - + void paintEvent(QPaintEvent *); + void mousePressEvent(QMouseEvent *); + void mouseReleaseEvent(QMouseEvent *); + void mouseMoveEvent(QMouseEvent *); + void setCurrent(int y); private: QColor m_color; @@ -358,50 +168,20 @@ class GradientLine : public QWidget { Q_PROPERTY(bool active READ active WRITE setActive) public: - GradientLine(QWidget *parent = 0) : QWidget(parent), m_activeColor(Qt::black), m_gradientName("gradient"), m_dragActive(false), m_yOffset(0), m_create(false), m_active(false) - { - setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); - setFocusPolicy(Qt::StrongFocus); - setFixedHeight(50); - setMinimumWidth(160); - resize(160, 50); - m_colorList << m_activeColor << QColor(Qt::white); - m_stops << 0 << 1; - updateGradient(); - setCurrentIndex(0); - } - + GradientLine(QWidget *parent = 0); QVariant itemNode() const { return QVariant::fromValue(m_itemNode.modelNode()); } void setItemNode(const QVariant &itemNode); QString gradientName() const { return m_gradientName; } - void setGradientName(const QString &newName) - { - if (newName == m_gradientName) - return; - m_gradientName = newName; - setup(); - emit gradientNameChanged(); - } - + void setGradientName(const QString &newName); QColor activeColor() const { return m_activeColor; } - void setActiveColor(const QColor &newColor) - { - if (newColor.name() == m_activeColor.name() && newColor.alpha() == m_activeColor.alpha()) - return; - - m_activeColor = newColor; - m_colorList.removeAt(currentColorIndex()); - m_colorList.insert(currentColorIndex(), m_activeColor); - updateGradient(); - update(); - } - + void setActiveColor(const QColor &newColor); bool active() const { return m_active; } void setActive(bool a) { m_active = a; } public slots: void setupGradient(); + void deleteGradient(); signals: void activeColorChanged(); @@ -419,15 +199,7 @@ private: void setup(); void updateGradient(); int currentColorIndex() const { return m_colorIndex; } - void setCurrentIndex(int i) - { - if (i == m_colorIndex) - return; - m_colorIndex = i; - setActiveColor(m_colorList.at(i)); - emit activeColorChanged(); - update(); - } + void setCurrentIndex(int i); QColor m_activeColor; QmlItemNode m_itemNode;