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
|
#include "AlphaDialog.h"
#include "ui_AlphaDialog.h"
#include <QtGui/QPaintEvent>
#include <QtGui/QPainter>
#include <QtGui/QPainterPath>
namespace nvidia {
namespace CurveEditor {
const int MARGIN_X = 8;
const int MARGIN_Y = 8;
const int ALPHA_W = 255;
const int ALPHA_H = 30;
const int CURSOR_W = 12;
const int CURSOR_H = 12;
static QImage s_triangleUp;
void InitTriangleResources(int w, int h)
{
s_triangleUp = QImage(w, h, QImage::Format_ARGB32);
s_triangleUp.fill(QColor(0, 0, 0, 0));
// a painter cannot switch device?
QPainterPath path;
QPainter painter(&s_triangleUp);
painter.setRenderHint(QPainter::Antialiasing,true);
path = QPainterPath(); // trick to clear up a path
path.moveTo(w>>1, 0);
path.lineTo(0, h);
path.lineTo(w, h);
path.lineTo(w>>1, 0);
painter.setPen(Qt::NoPen);
painter.fillPath(path, QBrush(QColor(50, 50, 50)));
}
//////////////////////////////////////////////////////////////////////////////
bool isClickedInCursor(const QPoint& cursorPos, const QPoint& p)
{
QVector<QPoint> points;
points.push_back(cursorPos);
points.push_back(QPoint(cursorPos.x() - (CURSOR_W>>1), cursorPos.y() + CURSOR_H));
points.push_back(QPoint(cursorPos.x() + (CURSOR_W>>1), cursorPos.y() + CURSOR_H));
points.push_back(cursorPos);
QPolygon polygon(points);
return polygon.containsPoint(p, Qt::OddEvenFill);
}
//////////////////////////////////////////////////////////////////////////////
int AlphaDialog::getAlpha(int alpha, QWidget *parent)
{
AlphaDialog dlg(parent, alpha);
dlg.exec();
return dlg._alpha;
}
//////////////////////////////////////////////////////////////////////////////
AlphaDialog::AlphaDialog(QWidget *parent, int alpha) :
QDialog(parent),
ui(new Ui::AlphaDialog),
_drag(false),
_alpha(alpha),
_xOffset(0)
{
ui->setupUi(this);
setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
setFixedWidth(271);
setFixedHeight(120);
ui->spinBoxAlpha->setValue(_alpha);
InitTriangleResources(12, 12);
}
//////////////////////////////////////////////////////////////////////////////
AlphaDialog::~AlphaDialog()
{
delete ui;
}
//////////////////////////////////////////////////////////////////////////////
void AlphaDialog::paintEvent(QPaintEvent * e)
{
QDialog::paintEvent(e);
QPainter painter;
painter.begin(this);
drawAlphaRectangle(painter);
drawCursor(painter, _alpha + MARGIN_X);
painter.end();
}
//////////////////////////////////////////////////////////////////////////////
void AlphaDialog::mousePressEvent( QMouseEvent* e )
{
if(e->button() & Qt::LeftButton)
{
QPoint mousePos = e->pos();
if (isClickedInCursor(QPoint(_alpha + MARGIN_X, MARGIN_Y + ALPHA_H), mousePos))
{
_xOffset = _alpha + MARGIN_X - mousePos.x();
_drag = true;
}
}
}
//////////////////////////////////////////////////////////////////////////////
void AlphaDialog::mouseReleaseEvent( QMouseEvent* e )
{
_drag = false;
_xOffset = -1;
update();
}
//////////////////////////////////////////////////////////////////////////////
void AlphaDialog::mouseMoveEvent( QMouseEvent* e )
{
Qt::MouseButtons buttons = e->buttons();
if(buttons & Qt::LeftButton)
{
if (_drag)
{
QPoint mousePos = e->pos();
_alpha = mousePos.x() + _xOffset - MARGIN_X;
if (_alpha < 0)
{
_alpha = 0;
}
else if(_alpha > ALPHA_W)
{
_alpha = ALPHA_W;
}
ui->spinBoxAlpha->setValue(_alpha);
update();
}
}
}
void AlphaDialog::drawAlphaRectangle(QPainter& painter)
{
QPointF topLeftPnt(MARGIN_X, MARGIN_Y);
QPointF topRightPnt(MARGIN_X + ALPHA_W, MARGIN_Y);
QPointF bottomRightPnt(MARGIN_X + ALPHA_W, MARGIN_Y + ALPHA_H);
QPointF bottomLeftPnt(MARGIN_X, MARGIN_Y + ALPHA_H);
QPainterPath path;
path.moveTo(topLeftPnt);
path.lineTo(topRightPnt);
path.lineTo(bottomRightPnt);
path.lineTo(bottomLeftPnt);
QColor colorLeft(0, 0, 0, 255);
QColor colorRight(255, 255, 255, 255);
colorLeft.setAlpha(255);
colorRight.setAlpha(255);
QLinearGradient indicatorGradient(topLeftPnt,topRightPnt);
indicatorGradient.setColorAt(0.0, colorLeft);
indicatorGradient.setColorAt(1.0, colorRight);
painter.fillPath(path, indicatorGradient);
}
void AlphaDialog::drawCursor(QPainter& painter, int xPos)
{
QRect rect(xPos - (CURSOR_W>>1), MARGIN_Y + ALPHA_H, CURSOR_W, CURSOR_H);
painter.drawImage(rect, s_triangleUp);
}
void AlphaDialog::on_spinBoxAlpha_valueChanged(int arg1)
{
_alpha = arg1;
}
} // namespace CurveEditor
} // namespace nvidia
|