colorwidget.cpp 28.3 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
**
** 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 "colorwidget.h"
#include <modelnode.h>
#include <abstractview.h>
#include <nodeproperty.h>
#include <nodelistproperty.h>
#include <variantproperty.h>
#include <qmlobjectnode.h>
37
38
39
#include <qmlitemnode.h>
#include <QGradient>
#include <metainfo.h>
40

41
42
43
44
45
#include <QHBoxLayout>
#include <QLabel>
#include <QToolButton>
#include <QGradient>
#include <QPainter>
46
47
48
49
50
#include <QDoubleSpinBox>
#include <QGridLayout>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QGraphicsEffect>
51

52
53
54
55
56
57
58
59
60
static inline int clamp(int x, int lower, int upper)
{
    if (x < lower)
        x = lower;
    if (x > upper)
        x = upper;
    return x;
}

61
static inline QString properName(const QColor &color)
62
63
64
65
66
67
68
69
70
{
    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;
}

71
static inline QColor properColor(const QString &str)
72
{
73
    if (str.isEmpty())
74
        return QColor();
75
76
77
78
79
80
81
82
83
84
85
86
    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;
87
    }
88
    QColor lcolor(lcolorStr);
89
90
    if (lcolorStr.contains('#'))
        lcolor.setAlpha(lalpha);
91
92
    return lcolor;
}
93

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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;
}

115
116
117
118
119
120
121
122
123
124
125
126
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;
}

127
namespace QmlDesigner {
128

129
130
131
132
133
134
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");
}
135

136
137
138
139
void ColorButton::setColor(const QString &colorStr)
{
    if (m_colorString == colorStr)
        return;
140

141
142
143

    setEnabled(isColorString(colorStr));

144
145
146
147
148
    m_colorString = colorStr;
    update();
    emit colorChanged();
}

149
150
151
152
153
QColor ColorButton::convertedColor() const
{
    return properColor(m_colorString);
}

154
155
156
157
158
159
void ColorButton::paintEvent(QPaintEvent *event)
{
    QToolButton::paintEvent(event);
    if (!isEnabled())
        return;

160
    QColor color = properColor(m_colorString);
161
162
163

    QPainter p(this);

Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
164

165
166
    QRect r(0, 0, width() - 2, height() - 2);    
    p.drawTiledPixmap(r.adjusted(1, 1, -1, -1), tilePixMap(9));
167
168
169
170
    if (isEnabled())
        p.setBrush(color);
    else
        p.setBrush(Qt::transparent);
171

172
173
174
175
176
    if (color.value() > 80)
        p.setPen(QColor(0x444444));
    else
        p.setPen(QColor(0x9e9e9e));
    p.drawRect(r.translated(1, 1));   
177

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    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);
194
    }
195
}
196
197


198
199
200
201
202
203
204
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
205
    emit hueChanged(m_color.hsvHue());
206
}
207

208
209
210
211
212
213
void HueControl::setHue(int newHue)
{
    if (m_color.hsvHue() == newHue)
        return;
    m_color.setHsv(newHue, m_color.hsvSaturation(), m_color.value());
    update();
214
    emit hueChanged(m_color.hsvHue());
215
}
216

217
218
219
void HueControl::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
220

221
    QPainter p(this);
222

223
224
225
226
227
228
229
230
231
232
233
234
    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);
235
        }
236
    }
237

238
239
    //p.setPen(QColor(0x404040));
    //p.drawRect(QRect(1, 1, width() - 1, height() - 1).adjusted(10, 5, -20, -5));
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
240

241
    p.drawPixmap(0, 5, m_cache);
242

243
    QVector<QPointF> points;
244

245
    int y = m_color.hueF() * 120 + 5;
246

247
248
249
    points.append(QPointF(5, y));
    points.append(QPointF(15, y + 5));
    points.append(QPointF(15, y - 5));
250

251

Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
252
253
254
255
256
257
258
    p.setRenderHint(QPainter::Antialiasing, true);
    p.translate(0.5, 1.5);
    p.setPen(QColor(0, 0, 0, 120));
    p.drawPolygon(points);
    p.translate(0, -1);
    p.setPen(0x222222);
    p.setBrush(QColor(0x707070));
259
260
    p.drawPolygon(points);
}
261

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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;
308

309
310
311
312
313
    m_color.setAlpha(newAlpha);
    update();
    emit alphaChanged();
    emit colorChanged();
}
314

315
316
317
318
QString ColorBox::strColor() const
{
    return properName(m_color);
}
319

320
321
322
323
void ColorBox::setStrColor(const QString &colorStr)
{
    if (properName(m_color) == colorStr)
        return;
324

325
326
    setColor(properColor(colorStr));
}
327

328
329
330
331
void ColorBox::setColor(const QColor &color)
{
    if (m_color == color)
        return;
332

333
334
335
336
337
338
339
340
341
342
343
    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();
}
344

345
346
347
348
349
350
351
352
353
354
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();
}
355

356
357
358
359
360
361
362
363
364
365
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);
}
366

367
368
369
370
371
372
373
374
375
376
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();
}
377

378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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++)
            {
402
                QColor c;
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
403
                c.setHsv(fixedHue, (x*255) / width, 255 - (y*255) / height);
404
405
406
                chacheP.setPen(c);
                chacheP.drawPoint(x ,y);
            }
407
408
    }
    p.drawPixmap(5, 5, m_cache);
409

Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
410
411
    int x = clamp(m_color.hsvSaturationF() * 120, 0, 119) + 5;
    int y = clamp(120 - m_color.valueF() * 120, 0, 119) + 5;
412

Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
413
414
415
416
417
    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);
418

419
}
420

421
422
423
424
425
426
427
428
429
430
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 */)
{
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
431
432
    if (m_mousePressed)
        emit colorChanged();
433
434
435
436
437
438
439
440
441
442
    m_mousePressed = false;
}

void ColorBox::mouseMoveEvent(QMouseEvent *e)
{
    if (!m_mousePressed)
        return;
    QPoint pos = e->pos();
    setCurrent(pos.x() - 5, pos.y() - 5);
}
443

444
445
446
void GradientLine::setItemNode(const QVariant &itemNode)
{

447
    if (!itemNode.value<ModelNode>().isValid())
448
449
        return;
    m_itemNode = itemNode.value<ModelNode>();
450
    setupGradient();
451
452
453
    emit itemNodeChanged();
}

454
455
456
457
458
459
460
461
462
void GradientLine::setGradient(const QLinearGradient &gradient)
{
    m_gradient = gradient;
    m_useGradient = true;
    setupGradient();
    emit gradientChanged();

}

463
464
465
466
467
468
469
static inline QColor invertColor(const QColor color)
{
    QColor c = color.toHsv();
    c.setHsv(c.hue(), c.saturation(), 255 - c.value());
    return c;
}

470
471
472
473
474
475
476
477
478
GradientLine::GradientLine(QWidget *parent) :
        QWidget(parent),
        m_activeColor(Qt::black),
        m_gradientName("gradient"),
        m_colorIndex(0),
        m_dragActive(false),
        m_yOffset(0),
        m_create(false),
        m_active(false),
479
480
        m_dragOff(false),
        m_useGradient(true)
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
{
    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();
}

514
515
void GradientLine::setupGradient()
{
516
517
518
519
520
521
522
523
524
525
526
527
528
    if (m_useGradient) {
        m_colorList.clear();
        m_stops.clear();
        foreach (const QGradientStop &stop, m_gradient.stops()) {
            m_stops << stop.first;
            m_colorList << stop.second;
        }
    } else {
        ModelNode modelNode = m_itemNode.modelNode();
        if (!modelNode.isValid())
            return;
        m_colorList.clear();
        m_stops.clear();
529

530
        if (modelNode.hasProperty(m_gradientName)) { //gradient exists
531

532
533
534
535
536
537
538
539
540
            ModelNode gradientNode = modelNode.nodeProperty(m_gradientName).modelNode();
            QList<ModelNode> stopList = gradientNode.nodeListProperty("stops").toModelNodeList();

            foreach (const ModelNode &stopNode, stopList) {
                QmlObjectNode stopObjectNode = stopNode;
                if (stopObjectNode.isValid()) {
                    m_stops << stopObjectNode.instanceValue("position").toReal();
                    m_colorList << stopObjectNode.instanceValue("color").value<QColor>();
                }
541
            }
542
543
544
        } else {
            m_colorList << m_activeColor << QColor(Qt::black);
            m_stops << 0 << 1;
545
546
547
548
549
550
        }
    }

    updateGradient();
}

551
552
553
554
555
556
557
558
559
560
561
562
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)) {
563
564
            RewriterTransaction transaction = m_itemNode.modelNode().view()->beginRewriterTransaction();
            ModelNode gradientNode = modelNode.nodeProperty(m_gradientName).modelNode();
565
            if (QmlObjectNode(gradientNode).isValid())
566
                QmlObjectNode(gradientNode).destroy();
567
568
569
570
        }
    }
}

571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
bool GradientLine::event(QEvent *event)
{
    if (event->type() == QEvent::ShortcutOverride)
        if (static_cast<QKeyEvent*>(event)->matches(QKeySequence::Delete)) {
            event->accept();
            return true;
    }

    return QWidget::event(event);
}

void GradientLine::keyPressEvent(QKeyEvent * event)
{
    if (event->matches(QKeySequence::Delete)) {
        if ((currentColorIndex()) != 0 && (currentColorIndex() < m_stops.size() - 1)) {
            m_dragActive = false;
            m_stops.removeAt(currentColorIndex());
            m_colorList.removeAt(currentColorIndex());
            updateGradient();
            setCurrentIndex(0);
            //delete item
        }
    } else {
       QWidget::keyPressEvent(event);
    }
}

void GradientLine::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
601

602
603
    QPainter p(this);

604
605
606
607
    if (!isEnabled()) {
        p.setBrush(Qt::NoBrush);
        p.setPen(QColor(0x444444));
        p.drawRect(9, 31, width() - 14, height() - 32);
608

609
610
        p.drawTiledPixmap(10, 32, width() - 16, height() - 34, tilePixMap(8));
    } else {
611

612
        QLinearGradient linearGradient(QPointF(0, 0), QPointF(width(), 0));
613

614
615
        for (int i =0; i < m_stops.size(); i++)
            linearGradient.setColorAt(m_stops.at(i), m_colorList.at(i));
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
616

617
618
619
        p.setBrush(Qt::NoBrush);
        p.setPen(QColor(0x444444));
        p.drawRect(9, 31, width() - 14, height() - 32);
620
621


622
        p.drawTiledPixmap(9, 31, width() - 16, height() - 34, tilePixMap(8));
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
623

624
625
626
627
628
        p.setBrush(linearGradient);
        p.setPen(QColor(0x222222));
        p.drawRect(8, 30, width() - 14, height() - 32);
        p.setPen(QColor(255, 255, 255, 40));
        p.drawRect(9, 31, width() - 16, height() - 34);
629

630
        p.setPen(Qt::black);
631

632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
        for (int i =0; i < m_colorList.size(); i++) {
            int localYOffset = 0;
            QColor arrowColor(Qt::black);
            if (i == currentColorIndex()) {
                localYOffset = m_yOffset;
                arrowColor = QColor(0x909090);
            }
            p.setPen(arrowColor);
            if (i == 0 || i == (m_colorList.size() - 1))
                localYOffset = 0;

            int pos = qreal((width() - 16)) * m_stops.at(i) + 9;
            p.setBrush(arrowColor);
            QVector<QPointF> points;
            points.append(QPointF(pos + 0.5, 28.5 + localYOffset)); //triangle
            points.append(QPointF(pos - 3.5, 22.5 + localYOffset));
            points.append(QPointF(pos + 4.5, 22.5 + localYOffset));
            p.setRenderHint(QPainter::Antialiasing, true);
            p.drawPolygon(points);
            p.setRenderHint(QPainter::Antialiasing, false);
            p.setBrush(Qt::NoBrush);
            p.setPen(QColor(0x424242));
            p.drawRect(pos - 4, 9 + localYOffset, 10, 11);

            p.drawTiledPixmap(pos - 4, 9 + localYOffset, 9, 10, tilePixMap(5));
            p.setPen(QColor(0x424242));
            p.setBrush(m_colorList.at(i));
            p.drawRect(pos - 5, 8 + localYOffset, 10, 11);
            p.setBrush(Qt::NoBrush);
            p.setPen(QColor(255, 255, 255, 30));
            p.drawRect(pos - 4, 9 + localYOffset, 8, 9);
        }
664
665
666
667
668
669
    }
}

void GradientLine::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
670
        event->accept();
671
672
673
674
675
676
677
678
        int xPos = event->pos().x();
        int yPos = event->pos().y();

        int draggedIndex = -1;
        m_create = false;
        m_dragActive = false;
        if ((yPos > 10) && (yPos < 30))
            for (int i =0; i < m_stops.size(); i++) {
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
679
            int pos = qreal((width() - 16)) * m_stops.at(i) + 9;
680
681
682
683
684
685
686
687
688
689
690
691
            if (((xPos + 5) > pos) && ((xPos - 5) < pos)) {
                draggedIndex = i;
                m_dragActive = true;
                m_dragStart = event->pos();
                setCurrentIndex(draggedIndex);
                update();
            }
        }
        if (draggedIndex == -1)
            m_create = true;
    }
    setFocus(Qt::MouseFocusReason);
692
693
694
695
}

void GradientLine::mouseDoubleClickEvent(QMouseEvent *event)
{
696
    event->accept();
697
698
699
    m_dragActive = false;
    m_create = false;
    emit openColorDialog(event->pos());
700
701
702
703
704
}

void GradientLine::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
705
        event->accept();
706
        if (m_dragActive == false && m_create) {
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
707
            qreal stopPos = qreal(event->pos().x() - 9) / qreal((width() - 15));
708
709
710
711
712
            int index = -1;
            for (int i =0; i < m_stops.size() - 1; i++) {
                if ((stopPos > m_stops.at(i)) && (index == -1))
                    index = i +1;
            }
713
            if (index != -1 && (m_useGradient || m_itemNode.isInBaseState())) { //creating of items only in base state
714
715
716
717
718
719
720
721
                m_stops.insert(index, stopPos);
                m_colorList.insert(index, QColor(Qt::white));
                setCurrentIndex(index);
            }
        }
    }
    m_dragActive = false;
    m_yOffset = 0;
722
    updateGradient();
723
    update();
724
    setFocus(Qt::MouseFocusReason);
725
726
727
728
729
}

void GradientLine::mouseMoveEvent(QMouseEvent *event)
{
    if (m_dragActive) {
730
        event->accept();
731
        int xPos = event->pos().x();
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
732
        int pos = qreal((width() - 20)) * m_stops.at(currentColorIndex()) + 8;
733
734
735
736
737
738
739
740
741
        int offset = m_dragOff ? 2 : 20;
        if (xPos < pos + offset && xPos > pos - offset) {
            m_dragOff = false;
            int xDistance = event->pos().x() - m_dragStart.x();
            qreal distance = qreal(xDistance) / qreal((width() - 20));
            qreal newStop  = m_stops.at(currentColorIndex()) + distance;
            if ((newStop >=0) && (newStop <= 1))
                m_stops[currentColorIndex()] = newStop;
            m_yOffset += event->pos().y() - m_dragStart.y();
742
            if (m_yOffset > 0 || !(m_itemNode.isInBaseState() || m_useGradient)) { //deleting only in base state
743
744
745
746
747
748
749
750
751
752
753
754
                m_yOffset = 0;
            } else if ((m_yOffset < - 12) && (currentColorIndex()) != 0 && (currentColorIndex() < m_stops.size() - 1)) {
                m_yOffset = 0;
                m_dragActive = false;
                m_stops.removeAt(currentColorIndex());
                m_colorList.removeAt(currentColorIndex());
                updateGradient();
                setCurrentIndex(0);
                //delete item
            }
        } else {
            m_dragOff = true;
755
756
757
758
759
760
761
        }
        m_dragStart = event->pos();
        update();
    }
}

void GradientLine::setup()
762
{
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780

}

static inline QColor normalizeColor(const QColor &color)
{
    QColor newColor = QColor(color.name());
    newColor.setAlpha(color.alpha());
    return newColor;
}

static inline qreal roundReal(qreal real)
{
    int i = real * 100;
    return qreal(i) / 100;
}

void GradientLine::updateGradient()
{
781
782
783
784
785
786
787
788
789
790
791
792
793
    if (m_useGradient) {
        QGradientStops stops;
        for (int i = 0;i < m_stops.size(); i++) {
            stops.append(QPair<qreal, QColor>(m_stops.at(i), m_colorList.at(i)));
        }
        m_gradient.setStops(stops);
        emit gradientChanged();
    } else {
        if (!active())
            return;
        RewriterTransaction transaction = m_itemNode.modelNode().view()->beginRewriterTransaction();
        if (!m_itemNode.isValid())
            return;
794

795
796
        if (!m_itemNode.modelNode().metaInfo().hasProperty(m_gradientName))
            return;
797

798
        ModelNode modelNode = m_itemNode.modelNode();
799

800
801
802
803
        if (m_itemNode.isInBaseState()) {
            if (modelNode.hasProperty(m_gradientName)) {
                modelNode.removeProperty(m_gradientName);
            }
804

805
            ModelNode gradientNode = modelNode.view()->createModelNode("Qt/Gradient", 4, 7);
806
807


808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
            for (int i = 0;i < m_stops.size(); i++) {
                ModelNode gradientStopNode = modelNode.view()->createModelNode("Qt/GradientStop", 4, 7);
                gradientStopNode.variantProperty("position") = roundReal(m_stops.at(i));
                gradientStopNode.variantProperty("color") = normalizeColor(m_colorList.at(i));
                gradientNode.nodeListProperty("stops").reparentHere(gradientStopNode);
            }
            modelNode.nodeProperty(m_gradientName).reparentHere(gradientNode);
        } else { //state
            if  (!modelNode.hasProperty(m_gradientName)) {
                qWarning(" GradientLine::updateGradient: no gradient in state");
                return;
            }
            ModelNode gradientNode = modelNode.nodeProperty(m_gradientName).modelNode();
            QList<ModelNode> stopList = gradientNode.nodeListProperty("stops").toModelNodeList();
            for (int i = 0;i < m_stops.size(); i++) {
                QmlObjectNode stopObjectNode = stopList.at(i);
                stopObjectNode.setVariantProperty("position", roundReal(m_stops.at(i)));
                stopObjectNode.setVariantProperty("color", normalizeColor(m_colorList.at(i)));
            }
827
828
829
830
        }
    }
}

831
832
833
834
835
void GradientLine::setCurrentIndex(int i)
{
    if (i == m_colorIndex)
        return;
    m_colorIndex = i;
836
    m_activeColor = m_colorList.at(i); 
837
838
839
840
    emit activeColorChanged();
    update();
}

841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
BauhausColorDialog::BauhausColorDialog(QWidget *parent) : QFrame(parent )
{

    setFrameStyle(QFrame::NoFrame);
    setFrameShape(QFrame::StyledPanel);
    setFrameShadow(QFrame::Sunken);

    QGraphicsDropShadowEffect *dropShadowEffect = new QGraphicsDropShadowEffect;
    dropShadowEffect->setBlurRadius(6);
    dropShadowEffect->setOffset(2, 2);
    setGraphicsEffect(dropShadowEffect);
    setAutoFillBackground(true);

    m_hueControl = new HueControl(this);
    m_colorBox = new ColorBox(this);

857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
    QWidget *colorFrameWidget = new QWidget(this);
    QVBoxLayout* vBox = new QVBoxLayout(colorFrameWidget);
    colorFrameWidget->setLayout(vBox);
    vBox->setSpacing(0);
    vBox->setMargin(0);
    vBox->setContentsMargins(0,5,0,28);

    m_beforeColorWidget = new QFrame(colorFrameWidget);
    m_beforeColorWidget->setFixedSize(30, 18);
    m_beforeColorWidget->setAutoFillBackground(true);
    //m_beforeColorWidget->setFrameShape(QFrame::StyledPanel);
    m_currentColorWidget = new QFrame(colorFrameWidget);
    m_currentColorWidget->setFixedSize(30, 18);
    m_currentColorWidget->setAutoFillBackground(true);
    //m_currentColorWidget->setFrameShape(QFrame::StyledPanel);

    vBox->addWidget(m_beforeColorWidget);
    vBox->addWidget(m_currentColorWidget);


877
878
879
880
881
882
    m_rSpinBox = new QDoubleSpinBox(this);
    m_gSpinBox = new QDoubleSpinBox(this);
    m_bSpinBox = new QDoubleSpinBox(this);
    m_alphaSpinBox = new QDoubleSpinBox(this);

    QGridLayout *gridLayout = new QGridLayout(this);
883
884
885
    gridLayout->setSpacing(4);
    gridLayout->setVerticalSpacing(4);
    gridLayout->setMargin(4);
886
887
888
889
890
    setLayout(gridLayout);

    gridLayout->addWidget(m_colorBox, 0, 0, 4, 1);
    gridLayout->addWidget(m_hueControl, 0, 1, 4, 1);

891
892
893
894
895
896
897
    gridLayout->addWidget(colorFrameWidget, 0, 2, 2, 1);


    gridLayout->addWidget(new QLabel("R", this), 0, 3, 1, 1);
    gridLayout->addWidget(new QLabel("G", this), 1, 3, 1, 1);
    gridLayout->addWidget(new QLabel("B", this), 2, 3, 1, 1);
    gridLayout->addWidget(new QLabel("A", this), 3, 3, 1, 1);
898

899
900
901
902
    gridLayout->addWidget(m_rSpinBox, 0, 4, 1, 1);
    gridLayout->addWidget(m_gSpinBox, 1, 4, 1, 1);
    gridLayout->addWidget(m_bSpinBox, 2, 4, 1, 1);
    gridLayout->addWidget(m_alphaSpinBox, 3, 4, 1, 1);
903
904
905
906
907
908

    QDialogButtonBox *buttonBox = new QDialogButtonBox(this);

    QPushButton *cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
    QPushButton *applyButton = buttonBox->addButton(QDialogButtonBox::Apply);

909
    gridLayout->addWidget(buttonBox, 4, 0, 1, 2);
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935

    resize(sizeHint());

    connect(m_colorBox, SIGNAL(colorChanged()), this, SLOT(onColorBoxChanged()));
    connect(m_alphaSpinBox, SIGNAL(valueChanged(double)), this, SLOT(spinBoxChanged()));
    connect(m_rSpinBox, SIGNAL(valueChanged(double)), this, SLOT(spinBoxChanged()));
    connect(m_gSpinBox, SIGNAL(valueChanged(double)), this, SLOT(spinBoxChanged()));
    connect(m_bSpinBox, SIGNAL(valueChanged(double)), this, SLOT(spinBoxChanged()));
    connect(m_hueControl, SIGNAL(hueChanged(int)), this, SLOT(onHueChanged(int)));

    connect(applyButton, SIGNAL(pressed()), this, SLOT(onAccept()));
    connect(cancelButton, SIGNAL(pressed()), this, SIGNAL(rejected()));

    m_alphaSpinBox->setMaximum(1);
    m_rSpinBox->setMaximum(1);
    m_gSpinBox->setMaximum(1);
    m_bSpinBox->setMaximum(1);
    m_alphaSpinBox->setSingleStep(0.1);
    m_rSpinBox->setSingleStep(0.1);
    m_gSpinBox->setSingleStep(0.1);
    m_bSpinBox->setSingleStep(0.1);

    m_blockUpdate = false;

}

936
937
938
939
940
941
942
943
void BauhausColorDialog::setupColor(const QColor &color)
{
    QPalette pal = m_beforeColorWidget->palette();
    pal.setColor(QPalette::Background, color);
    m_beforeColorWidget->setPalette(pal);
    setColor(color);
}

944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
void BauhausColorDialog::spinBoxChanged()
{
    if (m_blockUpdate)
        return;
    QColor newColor;
    newColor.setAlphaF(m_alphaSpinBox->value());
    newColor.setRedF(m_rSpinBox->value());
    newColor.setGreenF(m_gSpinBox->value());
    newColor.setBlueF(m_bSpinBox->value());
    setColor(newColor);
}

void BauhausColorDialog::onColorBoxChanged()
{
    if (m_blockUpdate)
        return;

    setColor(m_colorBox->color());
}

void BauhausColorDialog::setupWidgets()
{
    m_blockUpdate = true;
    m_hueControl->setHue(m_color.hsvHue());
    m_alphaSpinBox->setValue(m_color.alphaF());
    m_rSpinBox->setValue(m_color.redF());
    m_gSpinBox->setValue(m_color.greenF());
    m_bSpinBox->setValue(m_color.blueF());
    m_colorBox->setColor(m_color);
973
974
975
    QPalette pal = m_currentColorWidget->palette();
    pal.setColor(QPalette::Background, m_color);
    m_currentColorWidget->setPalette(pal);
976
977
978
    m_blockUpdate = false;
}

979
}