OGS
QGraphicsGrid.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "QGraphicsGrid.h"
5
6#include <QPainter>
7
15QGraphicsGrid::QGraphicsGrid(QRectF rect, int xCells, int yCells,
16 QGraphicsItem* parent)
17 : QGraphicsItem(parent)
18{
19 _numberOfXCells = xCells;
20 _numberOfYCells = yCells;
21 _bounds = rect;
22 _showTicks = false;
23
25}
26
37 int y,
38 int width,
39 int height,
40 int xCells,
41 int yCells,
42 QGraphicsItem* parent)
43 : QGraphicsItem(parent)
44{
45 _numberOfXCells = xCells;
46 _numberOfYCells = yCells;
47 _bounds = QRectF(x, y, width, height);
48 _showTicks = false;
49
51}
52
63 int xCells,
64 int yCells,
65 bool ticks,
66 QPen pen,
67 QGraphicsItem* parent)
68 : QGraphicsItem(parent)
69{
70 _numberOfXCells = xCells;
71 _numberOfYCells = yCells;
72 _bounds = rect;
73 _showTicks = ticks;
74
75 _outside = pen;
76 _outside.setCosmetic(true);
77
78 _inside = pen;
79 QColor iColour = pen.color();
80 iColour.setAlpha(125);
81 _inside.setColor(iColour);
82 _inside.setStyle(Qt::DotLine);
83 _inside.setCosmetic(true);
84}
85
98 int y,
99 int width,
100 int height,
101 int xCells,
102 int yCells,
103 bool ticks,
104 QPen pen,
105 QGraphicsItem* parent)
106 : QGraphicsItem(parent)
107{
108 _numberOfXCells = xCells;
109 _numberOfYCells = yCells;
110 _bounds = QRectF(x, y, width, height);
111 _showTicks = ticks;
112
113 _outside = pen;
114 _outside.setCosmetic(true);
115
116 _inside = pen;
117 QColor iColour = pen.color();
118 iColour.setAlpha(125);
119 _inside.setColor(iColour);
120 _inside.setStyle(Qt::DotLine);
121 _inside.setCosmetic(true);
122}
123
125
128{
129 return _bounds;
130}
131
134{
135 QPen in(Qt::gray, 1, Qt::DotLine, Qt::SquareCap, Qt::RoundJoin);
136 QPen out(Qt::black, 1, Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin);
137 _inside = in;
138 _outside = out;
139 _inside.setCosmetic(true);
140 _outside.setCosmetic(true);
141}
142
144void QGraphicsGrid::paint(QPainter* painter,
145 const QStyleOptionGraphicsItem* option,
146 QWidget* widget)
147{
148 Q_UNUSED(option)
149 Q_UNUSED(widget)
150
151 if (!_bounds.isValid())
152 {
153 return;
154 }
155
156 /* draw outside rectangle */
157 QBrush brush(Qt::NoBrush);
158 painter->setPen(_outside);
159 painter->drawRect(_bounds);
160
161 /* draw horizontal lines */
162 for (int i = 0; i <= _numberOfXCells; ++i)
163 {
164 auto x = static_cast<int>(
165 _bounds.left() + (i * (_bounds.width() - 1) / _numberOfXCells));
166
167 if (i > 0 && i < _numberOfXCells)
168 {
169 painter->setPen(_inside);
170 painter->drawLine(x, static_cast<int>(_bounds.top()), x,
171 static_cast<int>(_bounds.bottom()));
172 }
173
174 /* draw ticks on x-axis */
175 if (_showTicks)
176 {
177 // double label = bounds.left() + (i * bounds.width() /
178 // numberOfXCells);
179 painter->setPen(_outside);
180 painter->drawLine(x, static_cast<int>(_bounds.bottom()), x,
181 static_cast<int>(_bounds.bottom()) + 5);
182 // painter->drawText(x - margin, bounds.bottom() + 5, 2*margin, 20,
183 // Qt::AlignHCenter | Qt::AlignTop,
184 // QString::number(label));
185 }
186 }
187
188 /* draw vertical lines */
189 for (int j = 0; j <= _numberOfYCells; ++j)
190 {
191 auto y = static_cast<int>(
192 _bounds.bottom() - (j * (_bounds.height() - 1) / _numberOfYCells));
193
194 if (j > 0 && j < _numberOfYCells)
195 {
196 painter->setPen(_inside);
197 painter->drawLine(static_cast<int>(_bounds.left()), y,
198 static_cast<int>(_bounds.right()), y);
199 }
200
201 /* draw ticks on y-axis */
202 if (_showTicks)
203 {
204 // double label = bounds.top() + (j * bounds.height() /
205 // numberOfYCells);
206 painter->setPen(_outside);
207 painter->drawLine(static_cast<int>(_bounds.left()) - 5, y,
208 static_cast<int>(_bounds.left()), y);
209 // painter->drawText(bounds.left() - margin, y - 10, margin - 5, 20,
210 // Qt::AlignRight | Qt::AlignVCenter,
211 // QString::number(label));
212 }
213 }
214}
215
218{
219 _numberOfXCells = xCells;
220}
221
224{
225 _numberOfYCells = yCells;
226}
227
229void QGraphicsGrid::setRect(int x, int y, int width, int height)
230{
231 _bounds = QRectF(x, y, width, height);
232}
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)