OGS
QGraphicsGrid Class Reference

Detailed Description

A 2D cartesian grid as a QGraphicsItem.

A 2D cartesian grid as a QGraphicsItem. The size of the grid cells is constant but can be anisotroph.

Definition at line 26 of file QGraphicsGrid.h.

#include <QGraphicsGrid.h>

Inheritance diagram for QGraphicsGrid:
[legend]
Collaboration diagram for QGraphicsGrid:
[legend]

Public Member Functions

 QGraphicsGrid (QRectF rect, int xCells, int yCells, QGraphicsItem *parent=nullptr)
 
 QGraphicsGrid (int x, int y, int width, int height, int xCells, int yCells, QGraphicsItem *parent=nullptr)
 
 QGraphicsGrid (QRectF rect, int xCells, int yCells, bool ticks, QPen pen, QGraphicsItem *parent=nullptr)
 
 QGraphicsGrid (int x, int y, int width, int height, int xCells, int yCells, bool ticks, QPen pen, QGraphicsItem *parent=nullptr)
 
 ~QGraphicsGrid () override
 
QRectF boundingRect () const override
 Returns the bounding rectangle of the grid.
 
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 setNumberOfYCells (int yCells)
 Sets the number of cells in y direction.
 
void setRect (QRectF rect)
 
void setRect (int x, int y, int width, int height)
 Sets the bounding rectangle of the grid.
 

Private Member Functions

void initDefaultPens ()
 Defines the default pens.
 

Private Attributes

QPen _inside
 
QPen _outside
 
QRectF _bounds
 
int _numberOfXCells
 
int _numberOfYCells
 
bool _showTicks
 

Constructor & Destructor Documentation

◆ QGraphicsGrid() [1/4]

QGraphicsGrid::QGraphicsGrid ( QRectF rect,
int xCells,
int yCells,
QGraphicsItem * parent = nullptr )

Creates a grid using a QRectF object and a default pen and no ticks.

Parameters
rectA rectangle specifying the size of the grid.
xCellsThe number of grid cells in x-direction.
yCellsThe number of grid cells in y-direction.
parentThe parent QGraphicsItem.

Definition at line 26 of file QGraphicsGrid.cpp.

28 : QGraphicsItem(parent)
29{
30 _numberOfXCells = xCells;
31 _numberOfYCells = yCells;
32 _bounds = rect;
33 _showTicks = false;
34
36}
void initDefaultPens()
Defines the default pens.

References _bounds, _numberOfXCells, _numberOfYCells, _showTicks, and initDefaultPens().

◆ QGraphicsGrid() [2/4]

QGraphicsGrid::QGraphicsGrid ( int x,
int y,
int width,
int height,
int xCells,
int yCells,
QGraphicsItem * parent = nullptr )

Creates a grid by specifying its bounding rectangle using a default pen and no ticks.

Parameters
xX-coordinate for the lower left corner of the bounding rectangle for the grid.
yY-coordinate for the lower left corner of the bounding rectangle for the grid.
widthWidth of the bounding rectangle.
heightHeight of the bounding rectangle.
xCellsThe number of grid cells in x-direction.
yCellsThe number of grid cells in y-direction.
parentThe parent QGraphicsItem.

Definition at line 47 of file QGraphicsGrid.cpp.

54 : QGraphicsItem(parent)
55{
56 _numberOfXCells = xCells;
57 _numberOfYCells = yCells;
58 _bounds = QRectF(x, y, width, height);
59 _showTicks = false;
60
62}

References _bounds, _numberOfXCells, _numberOfYCells, _showTicks, and initDefaultPens().

◆ QGraphicsGrid() [3/4]

QGraphicsGrid::QGraphicsGrid ( QRectF rect,
int xCells,
int yCells,
bool ticks,
QPen pen,
QGraphicsItem * parent = nullptr )

Creates a grid using a QRectF object and a default pen and no ticks.

Parameters
rectA rectangle specifying the size of the grid.
xCellsThe number of grid cells in x-direction.
yCellsThe number of grid cells in y-direction.
ticksSpecifies if ticks are displayed for the grid.
penThe pen for drawing the grid.
parentThe parent QGraphicsItem.

Definition at line 73 of file QGraphicsGrid.cpp.

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}

References _bounds, _inside, _numberOfXCells, _numberOfYCells, _outside, and _showTicks.

◆ QGraphicsGrid() [4/4]

QGraphicsGrid::QGraphicsGrid ( int x,
int y,
int width,
int height,
int xCells,
int yCells,
bool ticks,
QPen pen,
QGraphicsItem * parent = nullptr )

Creates a grid by specifying its bounding rectangle using a default pen and no ticks.

Parameters
xX-coordinate for the lower left corner of the bounding rectangle for the grid.
yY-coordinate for the lower left corner of the bounding rectangle for the grid.
widthWidth of the bounding rectangle.
heightHeight of the bounding rectangle.
xCellsThe number of grid cells in x-direction.
yCellsThe number of grid cells in y-direction.
ticksSpecifies if ticks are displayed for the grid.
penThe pen for drawing the grid.
parentThe parent QGraphicsItem.

Definition at line 108 of file QGraphicsGrid.cpp.

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}

References _bounds, _inside, _numberOfXCells, _numberOfYCells, _outside, and _showTicks.

◆ ~QGraphicsGrid()

QGraphicsGrid::~QGraphicsGrid ( )
overridedefault

Member Function Documentation

◆ boundingRect()

QRectF QGraphicsGrid::boundingRect ( ) const
override

Returns the bounding rectangle of the grid.

Definition at line 138 of file QGraphicsGrid.cpp.

139{
140 return _bounds;
141}

References _bounds.

◆ initDefaultPens()

void QGraphicsGrid::initDefaultPens ( )
private

Defines the default pens.

Definition at line 144 of file QGraphicsGrid.cpp.

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}

References _inside, and _outside.

Referenced by QGraphicsGrid(), and QGraphicsGrid().

◆ paint()

void QGraphicsGrid::paint ( QPainter * painter,
const QStyleOptionGraphicsItem * option,
QWidget * widget )
override

Paints the grid.

Definition at line 155 of file QGraphicsGrid.cpp.

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}

References _bounds, _inside, _numberOfXCells, _numberOfYCells, _outside, and _showTicks.

◆ setNumberOfXCells()

void QGraphicsGrid::setNumberOfXCells ( int xCells)

Sets the number of cells in x direction.

Definition at line 228 of file QGraphicsGrid.cpp.

229{
230 _numberOfXCells = xCells;
231}

References _numberOfXCells.

◆ setNumberOfYCells()

void QGraphicsGrid::setNumberOfYCells ( int yCells)

Sets the number of cells in y direction.

Definition at line 234 of file QGraphicsGrid.cpp.

235{
236 _numberOfYCells = yCells;
237}

References _numberOfYCells.

◆ setRect() [1/2]

void QGraphicsGrid::setRect ( int x,
int y,
int width,
int height )

Sets the bounding rectangle of the grid.

Definition at line 240 of file QGraphicsGrid.cpp.

241{
242 _bounds = QRectF(x, y, width, height);
243}

References _bounds.

◆ setRect() [2/2]

void QGraphicsGrid::setRect ( QRectF rect)

Member Data Documentation

◆ _bounds

QRectF QGraphicsGrid::_bounds
private

◆ _inside

QPen QGraphicsGrid::_inside
private

Definition at line 69 of file QGraphicsGrid.h.

Referenced by QGraphicsGrid(), QGraphicsGrid(), initDefaultPens(), and paint().

◆ _numberOfXCells

int QGraphicsGrid::_numberOfXCells
private

◆ _numberOfYCells

int QGraphicsGrid::_numberOfYCells
private

◆ _outside

QPen QGraphicsGrid::_outside
private

Definition at line 70 of file QGraphicsGrid.h.

Referenced by QGraphicsGrid(), QGraphicsGrid(), initDefaultPens(), and paint().

◆ _showTicks

bool QGraphicsGrid::_showTicks
private

Definition at line 74 of file QGraphicsGrid.h.

Referenced by QGraphicsGrid(), QGraphicsGrid(), QGraphicsGrid(), QGraphicsGrid(), and paint().


The documentation for this class was generated from the following files: