-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrulebar.cpp
261 lines (237 loc) · 9.14 KB
/
rulebar.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "rulebar.h"
#include <QGraphicsView>
#define MINIMUM_INCR 5
typedef struct
{
double ruler_scale[16];
int subdivide[5];
}SPRulerMetric;
// Ruler metric for general use.
static SPRulerMetric const ruler_metric_general = {
{ 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 25000, 50000, 100000 },
{ 1, 5, 10, 50, 100 }
};
// Ruler metric for inch scales.
static SPRulerMetric const ruler_metric_inches = {
{ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 },
{ 1, 2, 4, 8, 16 }
};
QtRuleBar::QtRuleBar(Qt::Orientation direction, QGraphicsView * view, QWidget *parent)
:QWidget(parent),
m_view(view)
{
m_lower = m_upper = m_maxsize = 0;
m_lastPos = QPoint(0,0);
m_direction = direction;
QFont f(font());
f.setBold(false);
f.setPixelSize(10);
setFont(f);
}
void QtRuleBar::setRange(double lower, double upper, double max_size)
{
m_lower = lower;
m_upper = upper;
m_maxsize = max_size;
}
void QtRuleBar::updatePosition(const QPoint &pos)
{
m_lastPos = pos;
update();
}
void QtRuleBar::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPalette palette = qApp->palette();
QPainter painter(this);
QRect rulerRect = rect();
painter.fillRect(rulerRect,palette.window().color());
QPen pen(palette.windowText().color());
painter.setPen(pen);
if ( m_direction == Qt::Horizontal ){
painter.drawLine(rulerRect.bottomLeft(),rulerRect.bottomRight());
}
else{
painter.drawLine(rulerRect.topRight(),rulerRect.bottomRight());
}
drawTicker(&painter);
drawPos(&painter);
}
void QtRuleBar::drawTicker(QPainter *painter)
{
int i;
int width, height;
int length, ideal_length;
double lower = m_lower , upper = m_upper; /* Upper and lower limits, in ruler units */
double increment; /* Number of pixels per unit */
uint scale; /* Number of units per major unit */
double start, end, cur;
char unit_str[32];
char digit_str[2] = { '\0', '\0' };
int digit_height;
int text_size;
int pos;
double max_size = m_maxsize;
SPRulerMetric ruler_metric = ruler_metric_general; /* The metric to use for this unit system */
QRect allocation = this->rect();
QFontMetrics fm(font());
digit_height = fm.height() ;
if (m_direction==Qt::Horizontal){
width = allocation.width();
height = allocation.height();
}else{
width = allocation.height();
height = allocation.width();
}
if ( (upper - lower) == 0 ) return ;
increment = (double) width / (upper - lower);
scale = ceil (max_size);
sprintf (unit_str, "%d", scale );
text_size = strlen (unit_str) * digit_height + 1;
for (scale = 0; scale < sizeof (ruler_metric.ruler_scale) /
sizeof(ruler_metric.ruler_scale[0]); scale++)
if (ruler_metric.ruler_scale[scale] * fabs (increment) > 2 * text_size)
break;
if (scale == sizeof (ruler_metric.ruler_scale))
scale = sizeof (ruler_metric.ruler_scale) - 1;
length = 0;
for (i = sizeof (ruler_metric.subdivide) /
sizeof( ruler_metric.subdivide[0] ) - 1; i >= 0; i--){
double subd_incr;
if (scale == 1 && i == 1 )
subd_incr = 1.0 ;
else
subd_incr = ((double)ruler_metric.ruler_scale[scale] /
(double)ruler_metric.subdivide[i]);
if (subd_incr * fabs (increment) <= MINIMUM_INCR)
continue;
ideal_length = height / (i + 1) - 1;
if (ideal_length > ++length)
length = ideal_length;
if (lower < upper){
start = floor (lower / subd_incr) * subd_incr;
end = ceil (upper / subd_incr) * subd_incr;
}else
{
start = floor (upper / subd_incr) * subd_incr;
end = ceil (lower / subd_incr) * subd_incr;
}
int tick_index = 0;
for (cur = start; cur <= end; cur += subd_incr){
pos = int(qRound((cur - lower) * increment + 1e-12));
if (m_direction==Qt::Horizontal){
QRect rt(pos,height-length,1,length);
painter->drawLine(rt.topLeft(),rt.bottomLeft());
}else{
QRect rt(height-length,pos,length,1);
painter->drawLine(rt.topLeft(),rt.topRight());
}
double label_spacing_px = fabs(increment*(double)ruler_metric.ruler_scale[scale]/ruler_metric.subdivide[i]);
if (i == 0 &&
(label_spacing_px > 6*digit_height || tick_index%2 == 0 || cur == 0) &&
(label_spacing_px > 3*digit_height || tick_index%4 == 0 || cur == 0))
{
if (qAbs((int)cur) >= 2000 && (((int) cur)/1000)*1000 == ((int) cur))
sprintf (unit_str, "%dk", ((int) cur)/1000);
else
sprintf (unit_str, "%d", (int) cur);
if (m_direction==Qt::Horizontal){
int w = fm.width(unit_str);
painter->drawText(pos + 2,
allocation.top(),
w,
RULER_SIZE,
Qt::AlignLeft|Qt::AlignTop,unit_str);
} else{
#if 0
int w = fm.width("u") + 2;
int start = cur < 0 ? 1 : 0 ;
if ( start == 1 ){
QRect textRect(-w/2,-digit_height/2,w,digit_height);
painter->save();
painter->translate(4, pos + w/2 + 2 );
painter->rotate(90);
painter->drawText(textRect,
Qt::AlignRight,QString(unit_str[0]));
painter->restore();
}
for (int j = start; j < (int) strlen (unit_str); j++){
digit_str[0] = unit_str[j];
painter->drawText(1,
pos + digit_height * j,
w,
digit_height,
Qt::AlignLeft|Qt::AlignTop,QString(digit_str[0]));
}
#else
int w = fm.width(unit_str);
QRect textRect(-w/2,-RULER_SIZE/2,w,RULER_SIZE);
painter->save();
painter->translate(4, pos + w/2+2);
painter->rotate(90);
painter->drawText(textRect,Qt::AlignRight,unit_str);
painter->restore();
#endif
}
}
tick_index++;
}
}
}
void QtRuleBar::drawPos(QPainter *painter)
{
int x, y;
int width, height;
int bs_width, bs_height;
QRect allocation = this->rect();
double position;
double lower = m_lower;
double upper = m_upper;
if (m_direction==Qt::Horizontal){
width = allocation.width();
height = allocation.height();
bs_width = height / 2 + 2 ;
bs_width |= 1; /* make sure it's odd */
bs_height = bs_width / 2 + 1;
position = lower + (upper - lower) * m_lastPos.x() / allocation.width();
}else{
width = allocation.height();
height = allocation.width();
bs_height = width / 2 + 2 ;
bs_height |= 1; /* make sure it's odd */
bs_width = bs_height / 2 + 1;
position = lower + (upper - lower) * m_lastPos.y() / allocation.height() ;
}
if ((bs_width > 0) && (bs_height > 0)){
double increment;
if (m_direction==Qt::Horizontal){
increment = (double) width / (upper - lower);
x = qRound ((position - lower) * increment) + bs_width / 2 - 1;
y = (height + bs_height) / 2 ;
painter->drawLine(m_lastPos.x(),0, m_lastPos.x() , height);
}else{
increment = (double) height / (upper - lower);
x = (width + bs_width) / 2 ;
y = qRound ((position - lower) * increment) + (bs_height) / 2 - 1;
painter->drawLine(0 , m_lastPos.y() , width , m_lastPos.y());
}
}
}
QtCornerBox::QtCornerBox(QWidget *parent)
:QWidget(parent)
{
}
void QtCornerBox::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
QPainter painter(this);
QPalette palette = qApp->palette();
painter.fillRect(rect(),palette.window().color());
QPen pen(palette.windowText().color());
pen.setStyle(Qt::DashLine);
painter.setPen(pen);
painter.drawLine(rect().center().x(),rect().top(),rect().center().x(),rect().bottom());
painter.drawLine(rect().left(),rect().center().y(),rect().right(),rect().center().y());
painter.drawLine(rect().topRight(),rect().bottomRight());
painter.drawLine(rect().bottomLeft(),rect().bottomRight());
}