forked from yvt/terravox
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcolorview.cpp
More file actions
115 lines (92 loc) · 2.94 KB
/
Copy pathcolorview.cpp
File metadata and controls
115 lines (92 loc) · 2.94 KB
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
#include "colorview.h"
#include <QPainter>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QPoint>
#include <QMouseEvent>
#include <QApplication>
#include <QDrag>
#include <QPixmap>
ColorView::ColorView(QWidget *parent) : QAbstractButton(parent)
{
}
ColorView::~ColorView()
{
}
void ColorView::setValue(const QColor &col)
{
if (col.rgb() == value_.rgb()) {
return;
}
value_ = col;
update();
emit valueChanged(col);
}
void ColorView::paintEvent(QPaintEvent *)
{
QPainter p(this);
QSize size = this->size();
for (int x = 0; x < size.width(); x += 10) {
for (int y = 0; y < size.height(); y += 10) {
p.fillRect(x, y, 10, 10, ((x + y) / 10 & 1) ? QColor(255, 255, 255) : QColor(192, 192, 192));
}
}
p.fillRect(0, 0, size.width(), size.height(), value_);
auto v2 = value_; v2.setAlphaF(1.f);
p.fillRect(0, 0, size.width() / 2, size.height(), v2);
p.setPen(QColor(0, 0, 0));
p.drawRect(0, 0, size.width() - 1, size.height() - 1);
p.setPen(QColor(255, 255, 255));
p.drawRect(1, 1, size.width() - 3, size.height() - 3);
}
void ColorView::dragEnterEvent(QDragEnterEvent *e)
{
if (e->mimeData()->hasColor())
e->acceptProposedAction();
}
void ColorView::dropEvent(QDropEvent *e)
{
if (e->mimeData()->hasColor()) {
auto color = qvariant_cast<QColor>(e->mimeData()->colorData());
setValue(color);
e->acceptProposedAction();
}
}
void ColorView::mousePressEvent(QMouseEvent *e)
{
dragStartPos_ = e->pos();
QAbstractButton::mousePressEvent(e);
}
void ColorView::mouseMoveEvent(QMouseEvent *e)
{
if (e->buttons() & Qt::LeftButton) {
if ((e->pos() - dragStartPos_).manhattanLength() >= QApplication::startDragDistance()) {
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
mimeData->setColorData(value_);
drag->setMimeData(mimeData);
QPixmap pix(20, 20);
auto size = pix.size();
{
QPainter p(&pix);
for (int x = 0; x < size.width(); x += 5) {
for (int y = 0; y < size.height(); y += 5) {
p.fillRect(x, y, 5, 5, ((x + y) / 5 & 1) ? QColor(255, 255, 255) : QColor(192, 192, 192));
}
}
p.fillRect(0, 0, size.width(), size.height(), value_);
auto v2 = value_; v2.setAlphaF(1.f);
p.fillRect(0, 0, size.width() / 2, size.height(), v2);
p.setPen(QColor(0, 0, 0));
p.drawRect(0, 0, size.width() - 1, size.height() - 1);
p.setPen(QColor(255, 255, 255));
p.drawRect(1, 1, size.width() - 3, size.height() - 3);
}
drag->setPixmap(pix);
drag->exec(Qt::CopyAction | Qt::MoveAction);
return;
}
}
QAbstractButton::mouseMoveEvent(e);
}