Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
graphicsviewadapterforqt.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
21 
22 #include "graphicsitemforqt.h"
23 #include "../core/drawers/drawerbase.h"
24 #include "../core/system/eptexception.h"
25 #include "../core/math/mathtools.h"
26 
27 //-----------------------------------------------------------------------------
28 // Constructor
29 //-----------------------------------------------------------------------------
30 
38 
40  DrawerBase *drawer,
41  QRectF sceneRect)
42  : QGraphicsView(parent),
44  mDrawer(drawer),
45  mScene(sceneRect),
46  mSceneRect(sceneRect)
47 {
48  setScene(&mScene);
49 
50  // antialiasing for nice lines
51  setRenderHints(QPainter::Antialiasing |
52  QPainter::SmoothPixmapTransform |
53  QPainter::HighQualityAntialiasing);
54 }
55 
56 
57 //-----------------------------------------------------------------------------
58 // Destructor
59 //-----------------------------------------------------------------------------
60 
64 
66 {
67  // upon delete the QGraphicItems may not be deleted,
68  // this was already done by the QGraphicsView
69  for (GraphicsItem *item : getGraphicItems())
70  {
71  // so set them to 0
72  static_cast<GraphicsItemForQt*>(item)->setItem(nullptr);
73  }
74 }
75 
76 
77 //-----------------------------------------------------------------------------
78 // Modify the scene rectangle
79 //-----------------------------------------------------------------------------
80 
87 
89 {
90  mSceneRect = rect;
91  mDrawer->redraw(true); // force redraw
92 }
93 
94 
95 //-----------------------------------------------------------------------------
96 // Show the scene
97 //-----------------------------------------------------------------------------
98 
105 
106 void GraphicsViewAdapterForQt::showEvent(QShowEvent *event)
107 {
108  fitInView(mScene.sceneRect());
109  QGraphicsView::showEvent(event);
110 }
111 
112 
113 //-----------------------------------------------------------------------------
114 // Resize the scene
115 //-----------------------------------------------------------------------------
116 
123 
124 void GraphicsViewAdapterForQt::resizeEvent(QResizeEvent *event)
125 {
126  fitInView(mScene.sceneRect());
127  QGraphicsView::resizeEvent(event);
128 }
129 
130 
131 //-----------------------------------------------------------------------------
132 // Clear the scene
133 //-----------------------------------------------------------------------------
134 
141 
143 {
145  mScene.clear();
146 }
147 
148 
149 //-----------------------------------------------------------------------------
150 //
151 //-----------------------------------------------------------------------------
152 
153 GraphicsItem *GraphicsViewAdapterForQt::drawLine(double x1, double y1, double x2, double y2, PenType pen) {
154  if (x1 < 0 || x1 > 1) {return nullptr;}
155  if (y1 < 0 || y1 > 1) {return nullptr;}
156  if (x2 < 0 || x2 > 1) {return nullptr;}
157  if (y2 < 0 || y2 > 1) {return nullptr;}
158  QGraphicsLineItem *line(mScene.addLine(convertRelToAbsLine(0, 0, x2 - x1, y2 - y1), getPen(pen)));
159  line->setPos(convertRelToAbs(QPointF(x1, y1)));
160  return new GraphicsItemForQt(this, line);
161 }
162 
163 //-----------------------------------------------------------------------------
164 //
165 //-----------------------------------------------------------------------------
166 
167 GraphicsItem *GraphicsViewAdapterForQt::drawChart(const std::vector<Point> &points, PenType pen) {
168  if (points.size() <= 1) {
169  return nullptr;
170  }
171 
172  QPainterPath path;
173  size_t firstValidPointIndex = 0;
174  for (; firstValidPointIndex < points.size(); firstValidPointIndex++) {
175  if (points[firstValidPointIndex].x < 0 || points[firstValidPointIndex].x > 1) {continue;}
176  if (points[firstValidPointIndex].y < 0 || points[firstValidPointIndex].y > 1) {continue;}
177  path.moveTo(convertRelToAbs(QPointF(points[firstValidPointIndex].x, points[firstValidPointIndex].y)));
178  break;
179  }
180  for (size_t i = firstValidPointIndex; i < points.size(); i++) {
181  if (points[i].x < 0 || points[i].x > 1) {continue;}
182  if (points[i].y < 0 || points[i].y > 1) {continue;}
183  path.lineTo(convertRelToAbs(QPointF(points[i].x, points[i].y)));
184  }
185 
186  return new GraphicsItemForQt(this, mScene.addPath(path, getPen(pen)));
187 }
188 
189 //-----------------------------------------------------------------------------
190 //
191 //-----------------------------------------------------------------------------
192 
193 GraphicsItem* GraphicsViewAdapterForQt::drawFilledRect(double x, double y, double w, double h, PenType pen, FillTypes fill)
194 {
195  if (x < 0 || x > 1) {return nullptr;}
196  if (y < 0 || y > 1) {return nullptr;}
197  if (x + w > 1) {return nullptr;}
198  if (y + h > 1) {return nullptr;}
199  return new GraphicsItemForQt(this,
200  mScene.addRect(QRectF(convertRelToAbs(QPointF(x, y)),
201  convertRelToAbs(QSizeF(w, h))),
202  getPen(pen),
203  getFill(fill)));
204 }
205 
206 
207 //-----------------------------------------------------------------------------
208 //
209 //-----------------------------------------------------------------------------
210 
211 QPen GraphicsViewAdapterForQt::getPen(PenType penType, bool cosmetic) const {
212  QPen pen;
213 
214  switch (penType) {
215  case PEN_THIN_BLACK:
216  pen = QPen(QBrush(Qt::black), 1);
217  break;
218  case PEN_MEDIUM_BLACK:
219  pen = QPen(QBrush(Qt::black), 2);
220  break;
222  pen = QPen(QBrush(QColor(220,220,220)), 1);
223  break;
224  case PEN_THIN_LIGHT_GRAY:
225  pen = QPen(QBrush(Qt::lightGray), 1);
226  break;
228  pen = QPen(QBrush(Qt::lightGray), 2);
229  break;
230  case PEN_THIN_DARK_GRAY:
231  pen = QPen(QBrush(Qt::darkGray), 1);
232  break;
234  pen = QPen(QBrush(Qt::darkGray), 2);
235  break;
236  case PEN_MEDIUM_GRAY:
237  pen = QPen(QBrush(Qt::gray), 2);
238  break;
240  pen = QPen(QBrush(Qt::darkGreen), 2);
241  break;
242  case PEN_THIN_CYAN:
243  pen = QPen(QBrush(Qt::cyan), 1);
244  break;
245  case PEN_MEDIUM_CYAN:
246  pen = QPen(QBrush(Qt::cyan), 2);
247  break;
248  case PEN_MEDIUM_ORANGE:
249  pen = QPen(QBrush(QColor(0xff,0x66,0)), 2);
250  break;
251  case PEN_MEDIUM_MAGENTA:
252  pen = QPen(QBrush(QColor(0xff,0,0xff)), 2);
253  break;
254  case PEN_THIN_MAGENTA:
255  pen = QPen(QBrush(QColor(0xff,0,0xff)), 1);
256  break;
257  case PEN_THIN_RED:
258  pen = QPen(QBrush(Qt::red), 1);
259  break;
260  case PEN_MEDIUM_RED:
261  pen = QPen(QBrush(Qt::red), 2);
262  break;
263  case PEN_THIN_BLUE:
264  pen = QPen(QBrush(Qt::blue), 1);
265  break;
267  pen = QPen(QBrush(Qt::transparent), 1);
268  break;
269  }
270 
271  pen.setCosmetic(cosmetic);
272 
273  return pen;
274 }
275 
276 //-----------------------------------------------------------------------------
277 //
278 //-----------------------------------------------------------------------------
279 
281  switch (fill) {
282  case FILL_TRANSPARENT:
283  return QBrush(Qt::transparent);
284  case FILL_LIGHT_GRAY:
285  return QBrush(Qt::lightGray);
286  case FILL_RED:
287  return QBrush(Qt::red);
288  case FILL_ORANGE:
289  return QBrush(QColor(255,165,0));
290  case FILL_GREEN:
291  return QBrush(Qt::green);
292  case FILL_LIGHT_GREEN:
293  return QBrush(QColor(200,255,200));
294  case FILL_BLUE:
295  return QBrush(Qt::blue);
296  }
297 
298  return QBrush();
299 }
300 
301 //-----------------------------------------------------------------------------
302 //
303 //-----------------------------------------------------------------------------
304 
305 QPointF GraphicsViewAdapterForQt::convertRelToAbs(const QPointF &p) const {
306  return QPointF(p.x() * mSceneRect.width(), p.y() * mSceneRect.height());
307 }
308 
309 //-----------------------------------------------------------------------------
310 //
311 //-----------------------------------------------------------------------------
312 
313 QSizeF GraphicsViewAdapterForQt::convertRelToAbs(const QSizeF &s) const {
314  return QSizeF(s.width() * mSceneRect.width(), s.height() * mSceneRect.height());
315 }
316 
317 //-----------------------------------------------------------------------------
318 //
319 //-----------------------------------------------------------------------------
320 
321 QLineF GraphicsViewAdapterForQt::convertRelToAbsLine(qreal x1, qreal y1, qreal x2, qreal y2) const {
322  return QLineF(x1 * mSceneRect.width(),
323  y1 * mSceneRect.height(),
324  x2 * mSceneRect.width(),
325  y2 * mSceneRect.height());
326 }
327 
328 //-----------------------------------------------------------------------------
329 //
330 //-----------------------------------------------------------------------------
331 
332 QPointF GraphicsViewAdapterForQt::convertAbsToRel(const QPointF &p) const {
333  return QPointF(p.x() / mSceneRect.width(), p.y() / mSceneRect.height());
334 }
Implementation class for the GraphicsItem in Qt.
QBrush getFill(FillTypes fill) const
Create a QBrush for the given value of FillTypes.
QGraphicsScene mScene
The QGraphicsScene.
virtual GraphicsItem * drawFilledRect(double x, double y, double w, double h, PenType pen, FillTypes fill) override
Abstract function: Draw a filled rectangle.
virtual void clear() override
Clear the scene.
QPen getPen(PenType penType, bool cosmetic=true) const
Get a QPen for the given value of PenTypes.
QPointF convertRelToAbs(const QPointF &p) const
Converting from relative to absolute coordinates.
void redraw(bool force=false)
Function to completely redraw the scene.
Definition: drawerbase.cpp:57
virtual GraphicsItem * drawChart(const std::vector< Point > &points, PenType pen) override
Abstract function: Draw a chart (polygon).
FillTypes
Available filling colors (e.g. to fill rectangles)
GraphicsViewAdapterForQt(QWidget *parent, DrawerBase *drawer, QRectF sceneRect)
Constructor.
GraphicItemsList & getGraphicItems()
Get the list of the graphic items.
virtual GraphicsItem * drawLine(double x1, double y1, double x2, double y2, PenType pen) override
Abstract function: Draw a line.
void setSceneRect(const QRectF &rect)
Setter function for the scene rectangle.
void resizeEvent(QResizeEvent *event) override
Resize the scene (reimplemented resize event).
QPointF convertAbsToRel(const QPointF &p) const
Converting from absolute to relative coordinates.
PenType
Available pen types for drawing.
void showEvent(QShowEvent *event) override
Show the scene (reimplemented show event).
Abstract base class for implementations rendering graphics.
Class for a single item in a graphics view.
Definition: graphicsitem.h:55
QLineF convertRelToAbsLine(qreal x1, qreal y1, qreal x2, qreal y2) const
Converting of a line from relative to absolute coordinates.
QRectF mSceneRect
The scene rect.
virtual void clear()
Clear the graphics panel.
DrawerBase * mDrawer
The drawer of this GraphicsViewAdapterForQt.
Abstract base class for drawing 2d graphics.
Definition: drawerbase.h:40
~GraphicsViewAdapterForQt()
Destructor, removing all graphics items from the list.