Commit 0b470d1c authored by Christiaan Janssen's avatar Christiaan Janssen
Browse files

QmlDesigner.PropertyEditor: color widget improvements

parent d87d7694
......@@ -74,15 +74,12 @@ QExtGroupBox {
onColorChanged: {
colorGroupBox.backendColor.value = color;
}
hue: hueControl.hue;
}
HueControl {
id: hueControl;
color: colorGroupBox.backendColor.value;
onColorChanged: {
colorGroupBox.backendColor.value = color;
}
hue: colorControl.hue;
onHueChanged: colorControl.hue=hue;
}
QWidget {
......@@ -100,10 +97,10 @@ QExtGroupBox {
}
QSpinBox {
maximum: 360
value: colorControl.hue*360;
onValueChanged: if (colorControl.hue !=value/360.0)
colorControl.hue=value/360.0;
maximum: 359
value: colorControl.hue;
onValueChanged: if (colorControl.hue != value)
colorControl.hue=value;
}
}
......@@ -117,9 +114,9 @@ QExtGroupBox {
}
QSpinBox {
maximum: 255
value: colorControl.saturation*255;
onValueChanged: if (colorControl.saturation !=value/255.0)
colorControl.saturation=value/255.0;
value: colorControl.saturation;
onValueChanged: if (colorControl.saturation !=value)
colorControl.saturation=value;
}
}
......@@ -132,16 +129,13 @@ QExtGroupBox {
}
QSpinBox {
maximum: 255
value: colorControl.value*255;
onValueChanged: if (colorControl.value!=value/255.0)
colorControl.value=value/255.0;
value: colorControl.value;
onValueChanged: if (Math.floor(colorControl.value)!=value)
colorControl.value=value;
}
}
}
// QWidget {
// }
}
}
}
......
......@@ -84,12 +84,11 @@ namespace QmlDesigner {
void HueControl::setCurrent(int y)
{
QColor color(m_colorString);
if (y<0) y=0;
if (y>119) y=119;
color.setHsvF(qreal(y) / 120.0, color.hsvSaturationF(), color.valueF());
setColor(color.name());
setHue(qreal(y) / 120.0);
if (y>120) y=120;
m_color.setHsv((y * 359)/120, m_color.hsvSaturation(), m_color.value());
update(); // redraw pointer
emit hueChanged();
}
void HueControl::paintEvent(QPaintEvent *event)
......@@ -108,7 +107,7 @@ namespace QmlDesigner {
for (int i = 0; i < height; i++)
{
QColor c;
c.setHsvF(qreal(i) / 120.0, 1,1.0);
c.setHsv( (i*359) / 120.0, 255,255);
cacheP.fillRect(0, i, 10, i + 1, c);
}
}
......@@ -117,7 +116,7 @@ namespace QmlDesigner {
QVector<QPointF> points;
int y = hue() * 120 + 5;
int y = m_color.hueF() * 120 + 5;
points.append(QPointF(15, y));
points.append(QPointF(25, y + 5));
......@@ -135,10 +134,8 @@ namespace QmlDesigner {
if (x>120) x=120;
if (y<0) y=0;
if (y>120) y=120;
newColor.setHsvF(hue(), qreal(x) / 120, 1.0 - qreal(y) / 120);
QString newColorStr = QVariant(newColor).toString();
setColor(newColorStr);
newColor.setHsv(m_saturatedColor.hsvHue(), (x*255) / 120, 255 - (y*255) / 120);
setColor(newColor);
}
void ColorBox::paintEvent(QPaintEvent *event)
......@@ -146,10 +143,12 @@ namespace QmlDesigner {
QWidget::paintEvent(event);
QPainter p(this);
QColor color(m_colorString);
if (m_hue != (m_lastHue) || (m_cache.isNull())) {
m_lastHue = m_hue;
if ((m_color.saturation()>1) && (m_color.value()>1))
m_saturatedColor.setHsv(m_color.hsvHue(),255,255);
if ((m_saturatedColor.hsvHue() != m_lastHue) || (m_cache.isNull())) {
m_lastHue = m_saturatedColor.hsvHue();
m_cache = QPixmap(120, 120);
int height = 120;
......@@ -157,14 +156,11 @@ namespace QmlDesigner {
QPainter chacheP(&m_cache);
if (color.hsvHueF() < 0)
color.setHsvF(hue(), color.hsvSaturationF(), color.valueF());
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
QColor c;
c.setHsvF(color.hsvHueF(), qreal(x) / 120, 1.0 - qreal(y) / 120);
c.setHsv(m_saturatedColor.hsvHue(), (x*255) / 120, 255 - (y*255) / 120);
chacheP.setPen(c);
chacheP.drawPoint(x ,y);
}
......@@ -172,8 +168,8 @@ namespace QmlDesigner {
p.drawPixmap(5, 5, m_cache);
int x = color.hsvSaturationF() * 120 + 5;
int y = 120 - color.valueF() * 120 + 5;
int x = m_color.hsvSaturationF() * 120 + 5;
int y = 120 - m_color.valueF() * 120 + 5;
p.setPen(Qt::white);
p.drawEllipse(x - 2, y - 2, 4, 4);
......
......@@ -87,46 +87,53 @@ class ColorBox : public QWidget
Q_OBJECT
Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(qreal hue READ hue WRITE setHue NOTIFY hueChanged)
Q_PROPERTY(qreal saturation READ saturation WRITE setSaturation NOTIFY saturationChanged)
Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
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)
public:
ColorBox(QWidget *parent = 0) : QWidget(parent), m_colorString("#ffffff"), m_hue(0), m_lastHue(0)
ColorBox(QWidget *parent = 0) : QWidget(parent), m_color(Qt::white), m_saturatedColor(Qt::white), m_lastHue(0)
{
setFixedWidth(130);
setFixedHeight(130);
}
void setHue(qreal newHue)
void setHue(int newHue)
{
if (m_hue == newHue)
if (m_color.hsvHue() == newHue)
return;
m_hue = newHue;
m_color.setHsv(newHue,m_color.hsvSaturation(),m_color.value());
update();
emit hueChanged();
emit colorChanged();
}
qreal hue() const
int hue() const
{
return m_hue;
return m_color.hue();
}
void setColor(const QString &colorStr)
{
if (m_colorString == colorStr)
if (m_color.name() == colorStr)
return;
setColor(QColor(colorStr));
}
void setColor(const QColor &color)
{
if (m_color == color)
return;
qreal oldsaturation = saturation();
qreal oldvalue = value();
m_colorString = colorStr;
int oldsaturation = m_color.hsvSaturation();
int oldvalue = m_color.value();
int oldhue = m_color.hsvHue();
m_color=color;
update();
qreal newHue = QColor(m_colorString).hsvHueF();
if (newHue >= 0)
setHue(newHue);
if (oldhue != m_color.hsvHue()) emit hueChanged();
if (oldsaturation != saturation()) emit saturationChanged();
if (oldvalue != value()) emit valueChanged();
emit colorChanged();
......@@ -134,37 +141,33 @@ void setColor(const QString &colorStr)
QString color() const
{
return m_colorString;
return m_color.name();
}
qreal saturation() const
int saturation() const
{
return QColor(m_colorString).hsvSaturationF();
return m_color.hsvSaturation();
}
void setSaturation(qreal newsaturation)
void setSaturation(int newsaturation)
{
QColor color(m_colorString);
if (color.hsvSaturationF()==newsaturation) return;
color.setHsvF(color.hsvHueF(),newsaturation,color.valueF());
m_colorString=color.name();
if (m_color.hsvSaturation()==newsaturation) return;
m_color.setHsv(m_color.hsvHue(),newsaturation,m_color.value());
update();
emit saturationChanged();
emit colorChanged();
}
qreal value() const
int value() const
{
return QColor(m_colorString).valueF();
return m_color.value();
}
void setValue(qreal newvalue)
void setValue(int newvalue)
{
QColor color(m_colorString);
if (color.valueF()==newvalue) return;
color.setHsvF(color.hsvHueF(),color.hsvSaturationF(),newvalue);
m_colorString=color.name();
if (m_color.value()==newvalue) return;
m_color.setHsv(m_color.hsvHue(),m_color.hsvSaturation(),newvalue);
update();
emit valueChanged();
emit colorChanged();
......@@ -204,10 +207,10 @@ void setCurrent(int x, int y);
private:
QString m_colorString;
QColor m_color;
QColor m_saturatedColor;
bool m_mousePressed;
qreal m_hue;
qreal m_lastHue;
int m_lastHue;
QPixmap m_cache;
};
......@@ -215,53 +218,31 @@ class HueControl : public QWidget
{
Q_OBJECT
Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(qreal hue READ hue WRITE setHue NOTIFY hueChanged)
public:
HueControl(QWidget *parent = 0) : QWidget(parent), m_colorString("#ffffff"), m_mousePressed(false), m_hue(0)
HueControl(QWidget *parent = 0) : QWidget(parent), m_color(Qt::white), m_mousePressed(false)
{
setFixedWidth(40);
setFixedHeight(130);
}
void setHue(qreal newHue)
void setHue(int newHue)
{
if (m_hue == newHue)
if (m_color.hsvHue() == newHue)
return;
m_hue = newHue;
QColor color(m_colorString);
color.setHsvF(newHue, color.hsvSaturationF(), color.valueF());
m_colorString = color.name();
m_color.setHsv(newHue, m_color.hsvSaturation(), m_color.value());
update();
emit hueChanged();
}
qreal hue() const
int hue() const
{
return m_hue;
}
void setColor(const QString &colorStr)
{
if (m_colorString == colorStr)
return;
m_colorString = colorStr;
m_hue = QColor(m_colorString).hsvHueF();
update();
emit colorChanged();
}
QString color() const
{
return m_colorString;
return m_color.hsvHue();
}
signals:
void colorChanged();
void hueChanged();
protected:
......@@ -292,9 +273,8 @@ void setCurrent(int y);
private:
QString m_colorString;
QColor m_color;
bool m_mousePressed;
qreal m_hue;
QPixmap m_cache;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment