aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/timelineeditor/timelinecontrols.cpp
blob: bb0500763d9b426a13563cb91b6ef02bb3ef1423 (plain)
1
2
3
4
5
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "timelinecontrols.h"
#include "timelinepropertyitem.h"

#include <coreplugin/icore.h>

#include <QColorDialog>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QToolTip>

#include <theme.h>

#include <limits>

namespace QmlDesigner {

TimelineControl *createTimelineControl(const NodeMetaInfo &metaInfo)
{
    if (metaInfo.isFloat())
        return new FloatControl;
    if (metaInfo.isColor())
        return new ColorControl;

    return nullptr;
}

FloatControl::FloatControl()
    : QDoubleSpinBox(nullptr)
{
    setValue(0.0);
    setButtonSymbols(QAbstractSpinBox::NoButtons);
    setFrame(false);
    setStepType(QAbstractSpinBox::AdaptiveDecimalStepType);

    setMinimum(std::numeric_limits<float>::lowest());
    setMaximum(std::numeric_limits<float>::max());

    QColor bg = Theme::getColor(Theme::DScontrolBackground);

    auto p = palette();
    p.setColor(QPalette::Text, Theme::instance()->color(Utils::Theme::PanelTextColorLight));
    p.setColor(QPalette::Base, bg.darker(110));
    setPalette(p);

    m_timer.setInterval(100);
    m_timer.setSingleShot(true);

    auto startTimer = [this]( ) { m_timer.start(); };
    auto deferredSlot = [this]( ) { emit controlValueChanged(QVariant(this->value())); };

    QObject::connect(this, &QDoubleSpinBox::editingFinished, &m_timer, startTimer);
    QObject::connect(&m_timer, &QTimer::timeout, deferredSlot);
}

FloatControl::~FloatControl() = default;

QWidget *FloatControl::widget()
{
    return this;
}

void FloatControl::connect(TimelinePropertyItem *item)
{
    QObject::connect(this,
                     &FloatControl::controlValueChanged,
                     item,
                     &TimelinePropertyItem::changePropertyValue);
}

QVariant FloatControl::controlValue() const
{
    return QVariant(value());
}

void FloatControl::setControlValue(const QVariant &value)
{
    if (value.userType() != QMetaType::Float && value.userType() != QMetaType::Double)
        return;

    QSignalBlocker blocker(this);
    setValue(value.toDouble());
}

void FloatControl::setSize(int width, int height)
{
    setFixedWidth(width);
    setFixedHeight(height);
}

ColorControl::ColorControl()
    : QWidget(nullptr)
    , m_color(Qt::black)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    setFixedHeight(20);
}

ColorControl::ColorControl(const QColor &color, QWidget *parent)
    : QWidget(parent)
    , m_color(color)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    setFixedHeight(20);
}

ColorControl::~ColorControl() = default;

QWidget *ColorControl::widget()
{
    return this;
}

void ColorControl::connect(TimelinePropertyItem *item)
{
    QObject::connect(this,
                     &ColorControl::controlValueChanged,
                     item,
                     &TimelinePropertyItem::changePropertyValue);
}

QVariant ColorControl::controlValue() const
{
    return QVariant(value());
}

void ColorControl::setControlValue(const QVariant &value)
{
    if (value.userType() != QMetaType::QColor)
        return;

    m_color = qvariant_cast<QColor>(value);
}

void ColorControl::setSize(int width, int height)
{
    setFixedWidth(width);
    setFixedHeight(height);
}

QColor ColorControl::value() const
{
    return m_color;
}

bool ColorControl::event(QEvent *event)
{
    if (event->type() == QEvent::ToolTip) {
        if (auto helpEvent = static_cast<const QHelpEvent *>(event)) {
            QToolTip::showText(helpEvent->globalPos(), m_color.name());
            return true;
        }
    }
    return QWidget::event(event);
}

void ColorControl::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.fillRect(event->rect(), m_color);
}

void ColorControl::mouseReleaseEvent(QMouseEvent *event)
{
    QColor color = QColorDialog::getColor(m_color, Core::ICore::dialogParent());

    event->accept();

    if (color.isValid() && color != m_color) {
        m_color = color;
        update();
        emit valueChanged();
        emit controlValueChanged(QVariant(m_color));
    }
}

void ColorControl::mousePressEvent(QMouseEvent *event)
{
    // Needed to make the mouseRelease Event work if this
    // widget is embedded inside a QGraphicsProxyWidget.
    QWidget::mousePressEvent(event);
    event->accept();
}

} // End namespace QmlDesigner.