OGS
QGraphicsGrid.cpp
Go to the documentation of this file.
1
15#include "QGraphicsGrid.h"
16
17#include <QPainter>
18
26QGraphicsGrid::QGraphicsGrid(QRectF rect, int xCells, int yCells,
27 QGraphicsItem* parent)
28 : QGraphicsItem(parent)
29{
30 _numberOfXCells = xCells;
31 _numberOfYCells = yCells;
32 _bounds = rect;
33 _showTicks = false;
34
36}
37
48 int y,
49 int width,
50 int height,
51 int xCells,
52 int yCells,
53 QGraphicsItem* parent)
54 : QGraphicsItem(parent)
55{
56 _numberOfXCells = xCells;
57 _numberOfYCells = yCells;
58 _bounds = QRectF(x, y, width, height);
59 _showTicks = false;
60
62}
63
74 int xCells,
75 int yCells,
76 bool ticks,
77 QPen pen,
78 QGraphicsItem* parent)
79 : QGraphicsItem(parent)
80{
81 _numberOfXCells = xCells;
82 _numberOfYCells = yCells;
83 _bounds = rect;
84 _showTicks = ticks;
85
86 _outside = pen;
87 _outside.setCosmetic(true);
88
89 _inside = pen;
90 QColor iColour = pen.color();
91 iColour.setAlpha(125);
92 _inside.setColor(iColour);
93 _inside.setStyle(Qt::DotLine);
94 _inside.setCosmetic(true);
95}
96
109 int y,
110 int width,
111 int height,
112 int xCells,
113 int yCells,
114 bool ticks,
115 QPen pen,
116 QGraphicsItem* parent)
117 : QGraphicsItem(parent)
118{
119 _numberOfXCells = xCells;
120 _numberOfYCells = yCells;
121 _bounds = QRectF(x, y, width, height);
122 _showTicks = ticks;
123
124 _outside = pen;
125 _outside.setCosmetic(true);
126
127 _inside = pen;
128 QColor iColour = pen.color();
129 iColour.setAlpha(125);
130 _inside.setColor(iColour);
131 _inside.setStyle(Qt::DotLine);
132 _inside.setCosmetic(true);
133}
134
136
139{
140 return _bounds;
141}
142
145{
146 QPen in(Qt::gray, 1, Qt::DotLine, Qt::SquareCap, Qt::RoundJoin);
147 QPen out(Qt::black, 1, Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin);
148 _inside = in;
149 _outside = out;
150 _inside.setCosmetic(true);
151 _outside.setCosmetic(true);
152}
153
155void QGraphicsGrid::paint(QPainter* painter,
156 const QStyleOptionGraphicsItem* option,
157 QWidget* widget)
158{
159 Q_UNUSED(option)
160 Q_UNUSED(widget)
161
162 if (!_bounds.isValid())
163 {
164 return;
165 }
166
167 /* draw outside rectangle */
168 QBrush brush(Qt::NoBrush);
169 painter->setPen(_outside);
170 painter->drawRect(_bounds);
171
172 /* draw horizontal lines */
173 for (int i = 0; i <= _numberOfXCells; ++i)
174 {
175 auto x = static_cast<int>(
176 _bounds.left() + (i * (_bounds.width() - 1) / _numberOfXCells));
177
178 if (i > 0 && i < _numberOfXCells)
179 {
180 painter->setPen(_inside);
181 painter->drawLine(x, static_cast<int>(_bounds.top()), x,
182 static_cast<int>(_bounds.bottom()));
183 }
184
185 /* draw ticks on x-axis */
186 if (_showTicks)
187 {
188 // double label = bounds.left() + (i * bounds.width() /
189 // numberOfXCells);
190 painter->setPen(_outside);
191 painter->drawLine(x, static_cast<int>(_bounds.bottom()), x,
192 static_cast<int>(_bounds.bottom()) + 5);
193 // painter->drawText(x - margin, bounds.bottom() + 5, 2*margin, 20,
194 // Qt::AlignHCenter | Qt::AlignTop,
195 // QString::number(label));
196 }
197 }
198
199 /* draw vertical lines */
200 for (int j = 0; j <= _numberOfYCells; ++j)
201 {
202 auto y = static_cast<int>(
203 _bounds.bottom() - (j * (_bounds.height() - 1) / _numberOfYCells));
204
205 if (j > 0 && j < _numberOfYCells)
206 {
207 painter->setPen(_inside);
208 painter->drawLine(static_cast<int>(_bounds.left()), y,
209 static_cast<int>(_bounds.right()), y);
210 }
211
212 /* draw ticks on y-axis */
213 if (_showTicks)
214 {
215 // double label = bounds.top() + (j * bounds.height() /
216 // numberOfYCells);
217 painter->setPen(_outside);
218 painter->drawLine(static_cast<int>(_bounds.left()) - 5, y,
219 static_cast<int>(_bounds.left()), y);
220 // painter->drawText(bounds.left() - margin, y - 10, margin - 5, 20,
221 // Qt::AlignRight | Qt::AlignVCenter,
222 // QString::number(label));
223 }
224 }
225}
226
229{
230 _numberOfXCells = xCells;
231}
232
235{
236 _numberOfYCells = yCells;
237}
238
240void QGraphicsGrid::setRect(int x, int y, int width, int height)
241{
242 _bounds = QRectF(x, y, width, height);
243}
Definition of the QGraphicsGrid class.
void setRect(QRectF rect)
void setNumberOfYCells(int yCells)
Sets the number of cells in y direction.
~QGraphicsGrid() override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Paints the grid.
void setNumberOfXCells(int xCells)
Sets the number of cells in x direction.
void initDefaultPens()
Defines the default pens.
QRectF boundingRect() const override
Returns the bounding rectangle of the grid.
QGraphicsGrid(QRectF rect, int xCells, int yCells, QGraphicsItem *parent=nullptr)