-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruudukko.cpp
123 lines (94 loc) · 3.04 KB
/
ruudukko.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
/**************************************************************************
** (c) Arto Hyvättinen 2011
** Gnu Public Licence 2 - No Warranty !!!
** 28.12.2011
**
** Ratapiha - kauko
**************************************************************************/
#include "ruudukko.h"
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QDebug>
Ruudukko::Ruudukko(QGraphicsScene *sceene) :
QGraphicsObject()
{
// Huolehditaan riittävästä ruudusta
sceene->addRect(-0.0, -50.0, 200, 100, Qt::NoPen, Qt::NoBrush);
setZValue(-125);
sceene->addItem(this);
koko_ = scene()->sceneRect();
connect( scene(), SIGNAL(sceneRectChanged(QRectF)), this, SLOT(kokoMuuttunut(QRectF)));
}
Ruudukko::~Ruudukko()
{
}
QRectF Ruudukko::boundingRect() const
{
return koko_;
}
void Ruudukko::kokoMuuttunut(QRectF uusikoko)
{
koko_ = uusikoko;
}
void Ruudukko::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
// Piirretään ruudukko
qreal detail = QStyleOptionGraphicsItem::levelOfDetailFromTransform(painter->worldTransform());
int tarkkuus = 10;
QPen kyna10( QColor(200,200,200));
QPen kyna100( QColor(225,225,225), 2.0 / detail);
QPen kyna1000( QColor(225,225,225), 4.0 / detail);
QPen kyna10000( QColor(225,225,225), 6.0 / detail);
// Piirretään ensin nollaympyrä
painter->setBrush( Qt::NoBrush);
painter->setPen( kyna10000);
painter->drawEllipse( QPointF(0.0,0.0),5.0,5.0);
if( detail > 0.25)
tarkkuus = 10;
else if( detail > 0.05 )
tarkkuus = 100;
else if( detail > 0.02)
{
tarkkuus = 1000;
kyna1000 = QPen( QColor(175,175,175), 1.5 / detail);
kyna10000 = QPen( QColor(175,175,175), 3.0 / detail);
}
else
{
tarkkuus = 10000;
kyna10000 = QPen( QColor(175,175,175), 1.5 / detail);
}
int x = (int)koko_.x() - ((int)koko_.x()) % 10000 - 10000 ;
while( x < koko_.x() + koko_.width() )
{
if( x % 10000 == 0)
painter->setPen( kyna10000);
else if( x % 1000 == 0)
painter->setPen(kyna1000);
else if(x % 100 == 0)
painter->setPen(kyna100);
else
painter->setPen(kyna10);
if( x >= (int) koko_.x())
painter->drawLine( QLineF(x, scene()->sceneRect().y(),
x, scene()->sceneRect().y()+scene()->sceneRect().height()));
x+=tarkkuus;
}
int y = (int)koko_.y() - ( (int)koko_.y()) % 10000 - 10000;
while( y < koko_.y() + koko_.height() )
{
if( y % 10000 == 0)
painter->setPen( kyna10000);
else if( y % 1000 == 0)
painter->setPen(kyna1000);
else if(y % 100 == 0)
painter->setPen(kyna100);
else
painter->setPen(kyna10);
if( y>= (int) koko_.y())
painter->drawLine( QLineF(scene()->sceneRect().x(),y,
scene()->sceneRect().x()+scene()->sceneRect().width(),y));
y+=tarkkuus;
}
}