-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhpglgraphicsview.cpp
More file actions
251 lines (186 loc) · 5.57 KB
/
hpglgraphicsview.cpp
File metadata and controls
251 lines (186 loc) · 5.57 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "hpglgraphicsview.h"
void hpglGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
// qDebug() << "HGV button: " << event->button();
if (event->button() == Qt::LeftButton)
{
// qDebug() << "HGV: Emitting mouseReleased.";
emit mouseReleased();
}
// Carry on with default action
QGraphicsView::mouseReleaseEvent(event);
}
void hpglGraphicsView::wheelEvent(QWheelEvent *event)
{
if (QApplication::keyboardModifiers() == Qt::ControlModifier)
{
int delta = event->delta();
zoomDelta(delta);
}
else
{
event->ignore();
}
QGraphicsView::wheelEvent(event);
}
void hpglGraphicsView::statusUpdate(QString _consoleStatus)
{
emit statusUpdate(_consoleStatus, Qt::black);
}
void hpglGraphicsView::setGrid()
{
return;
QTransform _transform = transform();
// physicalDpi is the number of pixels in an inch
int xDpi = physicalDpiX();
int yDpi = physicalDpiY();
QSettings settings;
if (!settings.value("mainwindow/grid", SETDEF_MAINWINDOW_GRID).toBool())
{
setBackgroundBrush(Qt::NoBrush);
return;
}
int size = settings.value("mainwindow/grid/size", SETDEF_MAINWINDOW_GRID_SIZE).toInt();
QTransform hpglToPx;
hpglToPx.scale(xDpi/1016.0, yDpi/1016.0);
// m11 and m22 are horizontal and vertical scaling
qreal mx, my;
mx = hpglToPx.m11() / qAbs(_transform.m11());
my = hpglToPx.m22() / qAbs(_transform.m22());
int gridX, gridY;
if (settings.value("device/width/type", SETDEF_DEVICE_WDITH_TYPE).toInt() == deviceWidth_t::CM)
{
gridX = (((xDpi*size)/2.54) / mx);
gridY = (((yDpi*size)/2.54) / my);
}
else
{
gridX = ((xDpi*size) / mx);
gridY = ((yDpi*size) / my);
}
QImage grid(gridX, gridY, QImage::Format_RGB32);
QRgb value;
value = qRgb(100, 240, 100);
for (int x = 0; x < gridX; ++x)
{
for (int y = 0; y < gridY; ++y)
{
grid.setPixelColor(QPoint(x, y), QColor(40, 40, 40));
}
}
for (int i = 0; i < gridX; ++i)
{
grid.setPixel(i, 0, value);
}
for (int i = 0; i < gridY; ++i)
{
grid.setPixel(0, i, value);
}
QBrush gridBrush(grid);
gridBrush.setTransform((_transform.inverted()));
setBackgroundBrush(gridBrush);
// plotScene.setBackgroundBrush(gridBrush);
}
void hpglGraphicsView::zoomActual()
{
// physicalDpi is the number of pixels in an inch
int xDpi = physicalDpiX();
int yDpi = physicalDpiY();
// Transforms
QTransform hpglToPx, viewFlip;
hpglToPx.scale(xDpi/1016.0, yDpi/1016.0);
viewFlip.scale(1, -1);
setTransform(hpglToPx * viewFlip);
setGrid();
emit statusUpdate("Scene scale set to 1:1", Qt::darkGreen);
emit zoomUpdate("Actual size");
}
void hpglGraphicsView::zoomSceneRect()
{
fitInView(
scene()->sceneRect(),
Qt::KeepAspectRatio);
setGrid();
emit statusUpdate("Scene scale set to view all", Qt::darkGreen);
emit zoomUpdate("Vinyl width");
}
void hpglGraphicsView::zoomGraphicsItems()
{
QRectF newrect;
newrect.setX(0);
newrect.setY(0);
newrect.setWidth(0);
newrect.setHeight(0);
QList<QGraphicsItem*> itemList = items();
// type() = 10 -> QGraphicsItemGroup
for (int i = 0; i < itemList.length(); ++i)
{
QGraphicsItem * item = itemList.at(i);
if (item->type() == 10) // item is a qgraphicsitemgroup
{
QRectF compRect = item->boundingRect();
QPointF compPoint = item->pos();
if ((compPoint.x()+compRect.width()) > newrect.width())
{
newrect.setWidth(compPoint.x()+compRect.width());
}
if ((compPoint.y()+compRect.height()) > newrect.height())
{
newrect.setHeight(compPoint.y()+compRect.height());
}
}
}
fitInView(newrect, Qt::KeepAspectRatio);
setGrid();
emit statusUpdate("Scene scale set to contain items", Qt::darkGreen);
emit zoomUpdate("Show all items");
}
void hpglGraphicsView::zoomSelectedItems()
{
QRectF newrect;
newrect.setX(0);
newrect.setY(0);
newrect.setWidth(0);
newrect.setHeight(0);
QList<QGraphicsItem*> itemList = items();
// type() = 10 -> QGraphicsItemGroup
for (int i = 0; i < itemList.length(); ++i)
{
QGraphicsItem * item = itemList.at(i);
if (item->type() == 10 && item->isSelected()) // item is a qgraphicsitemgroup
{
QRectF compRect = item->boundingRect();
QPointF compPoint = item->pos();
if ((compPoint.x()+compRect.width()) > newrect.width())
{
newrect.setWidth(compPoint.x()+compRect.width());
}
if ((compPoint.y()+compRect.height()) > newrect.height())
{
newrect.setHeight(compPoint.y()+compRect.height());
}
}
}
fitInView(newrect, Qt::KeepAspectRatio);
setGrid();
emit statusUpdate("Scene scale set to contain items", Qt::darkGreen);
emit zoomUpdate("Show all items");
}
void hpglGraphicsView::zoomDelta(int delta)
{
QTransform oldtransform = transform();
QTransform scaleTransform;
scaleTransform.scale((1.0+(5.0/delta)), (1.0+(5.0/delta)));
setTransform(oldtransform * scaleTransform);
setGrid();
emit statusUpdate("Scene scale set to scroll wheel zoom.", Qt::darkGreen);
emit zoomUpdate("Custom");
}
void hpglGraphicsView::zoomOut()
{
zoomDelta(-30);
}
void hpglGraphicsView::zoomIn()
{
zoomDelta(30);
}