-
Notifications
You must be signed in to change notification settings - Fork 2
/
gridwidget.cpp
135 lines (122 loc) · 3.4 KB
/
gridwidget.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
/*
* Copyright (C) Kreogist Dev Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <QPainter>
#include <QStaticText>
#include "groundglobal.h"
#include "gridwidget.h"
GridWidget::GridWidget(QWidget *parent) :
QWidget(parent),
m_widget(nullptr),
m_coordinate(false)
{
setAutoFillBackground(true);
}
void GridWidget::paintEvent(QPaintEvent *event)
{
//Paint the widget.
QWidget::paintEvent(event);
//Paint the reference line.
QPainter painter(this);
painter.setPen(GroundGlobal::instance()->referenceLineColor());
painter.setBrush(QColor(0,0,0,0));
painter.setOpacity(0.3);
for(int i=0; i<width(); i+=m_gridStep)
{
painter.drawLine(i, 0, i, height());
}
for(int i=0; i<height(); i+=m_gridStep)
{
painter.drawLine(0, i, width(), i);
}
painter.setOpacity(1.0);
QFont textFont=font();
textFont.setPixelSize(4);
painter.setFont(textFont);
int stopWidth=width()+height()+m_gridStep;
for(int i=0; i<stopWidth; i+=m_gridStep)
{
//Draw right top to left bottom lines.
painter.drawLine(i, 0, i-height(), height());
//Draw left top to right bottom lines.
painter.drawLine(i, 0, i+height(), height());
}
stopWidth=-height();
for(int i=0; i>=stopWidth; i-=m_gridStep)
{
painter.drawLine(i, 0, i+height(), height());
}
//Draw captions.
if(m_coordinate)
{
for(CoordinateItem item : m_captions)
{
painter.drawText(item.x, item.y, item.text);
}
}
}
bool GridWidget::coordinate() const
{
return m_coordinate;
}
void GridWidget::setCoordinate(bool coordinate)
{
m_coordinate = coordinate;
update();
}
int GridWidget::m_gridStep=100;
int GridWidget::m_gridSize=50;
void GridWidget::setGridStep(int gridStep)
{
//Update the parameter.
m_gridStep = gridStep;
m_gridSize = m_gridStep>>1;
}
QWidget *GridWidget::widget() const
{
return m_widget;
}
void GridWidget::setWidget(QWidget *widget)
{
m_widget = widget;
m_widget->setParent(this);
}
void GridWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
//If the widget is not null, resize it.
if(m_widget)
{
m_widget->resize(size());
}
//Update the items.
m_captions.clear();
int stopWidth=width()+height()+m_gridStep;
for(int i=0; i<stopWidth; i+=m_gridStep)
{
int horizon=i-m_gridSize+8;
for(int j=-m_gridSize-2; j<height()+m_gridStep; j+=m_gridSize)
{
CoordinateItem item;
item.x=horizon;
item.y=j;
item.text="("+QString::number(horizon)+", "+QString::number(j)+")";
m_captions.append(item);
horizon-=m_gridSize;
}
}
}