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 15 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 15 of file QGraphicsGrid.cpp.

17 : QGraphicsItem(parent)
18{
19 _numberOfXCells = xCells;
20 _numberOfYCells = yCells;
21 _bounds = rect;
22 _showTicks = false;
23
25}
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 36 of file QGraphicsGrid.cpp.

43 : QGraphicsItem(parent)
44{
45 _numberOfXCells = xCells;
46 _numberOfYCells = yCells;
47 _bounds = QRectF(x, y, width, height);
48 _showTicks = false;
49
51}

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 62 of file QGraphicsGrid.cpp.

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}

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 97 of file QGraphicsGrid.cpp.

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}

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 127 of file QGraphicsGrid.cpp.

128{
129 return _bounds;
130}

References _bounds.

◆ initDefaultPens()

void QGraphicsGrid::initDefaultPens ( )
private

Defines the default pens.

Definition at line 133 of file QGraphicsGrid.cpp.

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}

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 144 of file QGraphicsGrid.cpp.

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}

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 217 of file QGraphicsGrid.cpp.

218{
219 _numberOfXCells = xCells;
220}

References _numberOfXCells.

◆ setNumberOfYCells()

void QGraphicsGrid::setNumberOfYCells ( int yCells)

Sets the number of cells in y direction.

Definition at line 223 of file QGraphicsGrid.cpp.

224{
225 _numberOfYCells = yCells;
226}

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 229 of file QGraphicsGrid.cpp.

230{
231 _bounds = QRectF(x, y, width, height);
232}

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 58 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 59 of file QGraphicsGrid.h.

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

◆ _showTicks

bool QGraphicsGrid::_showTicks
private

Definition at line 63 of file QGraphicsGrid.h.

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


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