-
Notifications
You must be signed in to change notification settings - Fork 0
/
axis.cpp
397 lines (357 loc) · 8.99 KB
/
axis.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include "axis.h"
/**
* @brief Axis::Axis
*tworzy oś z paramterami:
*czcionka: Verdana, 8pt
*wartość minimalna: 0
*wartość maksymalna: 100
*odstęp wartości: 10
*podziałka: 4px skierowana do wewnątrz
*pusta etykieta
*bez jednostki
*domyślny styl linii
*/
Axis::Axis()
{
//this->autoscale = false;
this->font = QFont("verdana", 8);
this->geometry = Geometry();
this->max = 100;
this->min = 0;
this->tick = 10;
this->tickDirection = inside;
this->tickSize = 4;
this->position = left;
this->showUnit = false;
this->label = Label();
this->unit = "";
this->lineStyle = LineStyle();
}
/**
* @brief Axis::setLabel
*ustawia etykietę
* @param label etykieta
*/
void Axis::setLabel(Label label)
{
this->label = label;
}
/**
* @brief Axis::getLabel
* @return referencję do etykiety
*/
Label &Axis::getLabel()
{
return this->label;
}
/**
* @brief Axis::setLineStyle
*ustawia styl linii
* @param style styl linii
*/
void Axis::setLineStyle(LineStyle style)
{
this->lineStyle = style;
}
/**
* @brief Axis::getLineStyle
* @return referencję do stylu linii
*/
LineStyle &Axis::getLineStyle()
{
return this->lineStyle;
}
/**
* @brief Axis::setTick
*ustawia odstęp między podziałkami
* @param tick odstęp wartości na osi
*/
void Axis::setTick(double tick)
{
this->tick = tick;
}
/**
* @brief Axis::getTick
* @return odstęp wartości na osi
*/
double Axis::getTick()
{
return this->tick;
}
/*void Axis::setAutoscale(bool autoscale)
{
this->autoscale = autoscale;
}
bool Axis::getAutoscale()
{
return this->autoscale;
}*/
/**
* @brief Axis::setMin
*ustawia minimalną wartość osi
* @param min wartość minimalna na osi
*/
void Axis::setMin(double min)
{
this->min = min;
}
/**
* @brief Axis::getMin
* @return wartość minimalną na osi
*/
double Axis::getMin()
{
return this->min;
}
/**
* @brief Axis::setMax
*ustawia maksymalną wartość osi
* @param max wartość maksymalna na osi
*/
void Axis::setMax(double max)
{
this->max = max;
}
/**
* @brief Axis::getMax
* @return wartość maksymalną na osi
*/
double Axis::getMax()
{
return this->max;
}
/**
* @brief Axis::setTickSize
*ustawia długość podziałki
* @param size szerokość podziałki
*/
void Axis::setTickSize(int size)
{
this->tickSize = size;
}
/**
* @brief Axis::getTickSize
* @return szerokość podziałki
*/
int Axis::getTickSize()
{
return this->tickSize;
}
/**
* @brief Axis::setTickDirection
*ustawia kierunek podziałki
* @param direction kierunek podziałki
*/
void Axis::setTickDirection(Direction direction)
{
this->tickDirection = direction;
}
/**
* @brief Axis::getTickDirection
* @return kierunek podziałki
*/
Direction Axis::getTickDirection()
{
return this->tickDirection;
}
/**
* @brief Axis::setUnitVisibility
*ustawia widoczność jednostek przy wartościach
* @param visible widoczność jednostki
*/
void Axis::setUnitVisibility(bool visible)
{
this->showUnit = visible;
}
/**
* @brief Axis::getUnitVisibility
* @return widoczność jednostki
*/
bool Axis::getUnitVisibility()
{
return this->showUnit;
}
/**
* @brief Axis::setFont
*ustawia czcionkę
* @param font czcionka
*/
void Axis::setFont(QFont font)
{
this->font = font;
}
/**
* @brief Axis::getFont
* @return czcionkę
*/
QFont Axis::getFont()
{
return this->font;
}
/**
* @brief Axis::setPosition
*ustawia pozycję osi
* @param position pozycja osi (lewa/prawa/góra/dół)
*/
void Axis::setPosition(Position position)
{
this->position = position;
}
/**
* @brief Axis::getPosition
* @return pozycję osi
*/
Position Axis::getPosition()
{
return this->position;
}
/**
* @brief Axis::setUnit
*ustawia jednostkę pokazywaną przy wartościach
* @param unit jednostka
*/
void Axis::setUnit(QString unit)
{
this->unit = unit;
}
/**
* @brief Axis::getUnit
* @return jednostkę
*/
QString Axis::getUnit()
{
return this->unit;
}
/**
* @brief Axis::setGeometry
*ustawia rozmiar osi
* @param geometry rozmiar
*/
void Axis::setGeometry(Geometry geometry)
{
this->geometry = geometry;
}
/**
* @brief Axis::getGeometry
* @return referencję do rozmiaru
*/
Geometry &Axis::getGeometry()
{
return this->geometry;
}
/**
* @brief Axis::draw
*rysuje oś
* @return narysowaną właśnie oś
*/
QPixmap Axis::draw()
{
QFontMetrics fm(this->getFont());
const int tickCount = (this->max-this->min)/this->tick;
const QString unit = this->showUnit?" "+this->unit:"";
int textWidth = fm.width(QString::number(this->max)+unit);
int textHeight = fm.height();
QPixmap temp;
float space;
//najdluzszy tekst
for(int i=0; i<=tickCount; i++)
{
if(fm.width(QString::number(this->min+tick*i)+unit) > textWidth)
textWidth = fm.width(QString::number(this->min+tick*i)+unit);
}
if(position==left || position==right)
{
//this->setGeometry(Geometry(0,0,textWidth+6+this->tickSize, this->geometry.getHeight()));
int dodatek=0;
if(this->label.getGeometry().getWidth()>0)
dodatek = this->label.getGeometry().getWidth()+2;
temp = QPixmap(textWidth+6+this->tickSize+dodatek, this->geometry.getHeight());
space = (temp.height()-20)/(max-min)*tick;
if(position == left)
textWidth += dodatek;
}
else
{
//this->setGeometry(Geometry(0, 0, this->geometry.getWidth(), textHeight+6+tickSize));
temp = QPixmap(this->geometry.getWidth(), textHeight+6+this->tickSize);
space = (temp.width())/(max-min)*tick;
}
QPainter p;
p.begin(&temp);
p.fillRect(0, 0, temp.width(), temp.height(), Qt::white);
p.setFont(this->getFont());
if(position==left)
{
if(this->label.getGeometry().getWidth() > 0)
{
//textWidth += this->label.getGeometry().getWidth()+2;
p.drawPixmap(0, (temp.height()-this->label.getGeometry().getHeight())/2, this->label.draw());
}
if(tickDirection==inside)
p.drawLine(textWidth+5, 10, textWidth+5, temp.height()-10);
else if(tickDirection == middle)
p.drawLine(textWidth+5+tickSize/2, 10, textWidth+5+tickSize/2, temp.height()-10);
else
p.drawLine(textWidth+5+tickSize, 10, textWidth+5+tickSize, temp.height()-10);
for(int i=tickCount; i>=0; i--)
{
p.drawLine(textWidth+5, temp.height()-10-i*space, textWidth+5+tickSize, temp.height()-10-i*space);
p.drawText(0, temp.height()-10-i*space-textHeight/2, textWidth, textHeight, Qt::AlignRight, QString::number(min+tick*i)+unit);
}
p.drawLine(textWidth+5, 10, textWidth+5+tickSize, 10);
}
else if(position == right)
{
if(tickDirection==inside)
p.drawLine(1+tickSize, 10, 1+tickSize, temp.height()-10);
else if(tickDirection == middle)
p.drawLine(1+tickSize/2, 10, 1+tickSize/2, temp.height()-10);
else
p.drawLine(1, 10, 1, temp.height()-10);
for(int i=tickCount; i>=0; i--)
{
p.drawLine(1, temp.height()-10-i*space, 1+tickSize, temp.height()-10-i*space);
p.drawText(6+tickSize, temp.height()-10-i*space-textHeight/2, textWidth, textHeight, Qt::AlignLeft, QString::number(min+tick*i)+unit);
}
p.drawLine(1, 10, 1+tickSize, 10);
if(this->label.getGeometry().getWidth() > 0)
{
//textWidth += this->label.getGeometry().getWidth()+2;
p.drawPixmap(6+tickSize+textWidth+2, (temp.height()-this->label.getGeometry().getHeight())/2, this->label.draw());
}
}
else if(position == bottom)
{
if(tickDirection==outside)
p.drawLine(0, 1, temp.width(), 1);
else if(tickDirection == middle)
p.drawLine(0, 1+tickSize/2, temp.width(), 1+tickSize/2);
else
p.drawLine(0, 1+tickSize, temp.width(), 1+tickSize);
for(int i=tickCount; i>=0; i--)
{
p.drawLine(i*space, 1, i*space, 1+tickSize);
//p.drawText(6+tickSize, temp.height()-10-i*space+textHeight/2, QString::number(min+tick*i)+unit);
}
p.drawLine(temp.width()-1, 1, temp.width()-1, 1+tickSize);
}
else //top
{
if(tickDirection==outside)
p.drawLine(10, 5+tickSize+textHeight, temp.width()-10, 5+tickSize+textHeight);
else if(tickDirection == middle)
p.drawLine(10, 5+tickSize/2+textHeight, temp.width()-10, 5+tickSize/2+textHeight);
else
p.drawLine(10, 5+textHeight, temp.width()-10, 5+textHeight);
for(int i=tickCount; i>=0; i--)
{
p.drawLine(10+i*space, 6+textHeight, 10+i*space, 6+tickSize+textHeight);
//p.drawText(6+tickSize, temp.height()-10-i*space+textHeight/2, QString::number(min+tick*i)+unit);
}
p.drawLine(temp.width()-10, 6+textHeight, temp.width()-10, 6+tickSize+textHeight);
}
p.end();
this->geometry.setWidth(temp.width());
this->geometry.setHeight(temp.height());
return temp;
}