Engauge Digitizer  2
ViewPointStyle.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "EnumsToQt.h"
8 #include "Logger.h"
9 #include <QPainter>
10 #include "ViewPointStyle.h"
11 
12 // Use solid background since transparency approach never worked, even with an alpha channel
13 const QColor COLOR_FOR_BRUSH_ENABLED (Qt::white);
14 const QColor COLOR_FOR_BRUSH_DISABLED (Qt::gray);
15 
17  QLabel (parent),
18  m_enabled (false)
19 {
20  // Note the size is set externally by the layout engine
21 }
22 
23 QPixmap ViewPointStyle::pixmapForCurrentSettings () const
24 {
25  LOG4CPP_INFO_S ((*mainCat)) << "ViewPointStyle::pixmapForCurrentSettings";
26 
27  // Polygon that is sized for the main drawing window.
28  QPolygonF polygonUnscaled = m_pointStyle.polygon();
29 
30  // Resize polygon to fit icon, by builiding a new scaled polygon from the unscaled polygon
31  double xMinGot = polygonUnscaled.boundingRect().left();
32  double xMaxGot = polygonUnscaled.boundingRect().right();
33  double yMinGot = polygonUnscaled.boundingRect().top();
34  double yMaxGot = polygonUnscaled.boundingRect().bottom();
35 
36  QPolygonF polygonScaled;
37  for (int i = 0; i < polygonUnscaled.length(); i++) {
38  QPointF pOld = polygonUnscaled.at(i);
39  polygonScaled.append (QPointF ((width () - 1) * (pOld.x() - xMinGot) / (xMaxGot - xMinGot),
40  (height () - 1) * (pOld.y() - yMinGot) / (yMaxGot - yMinGot)));
41  }
42 
43  // Color
44  QColor color = ColorPaletteToQColor(m_pointStyle.paletteColor());
45  if (!m_enabled) {
46  color = QColor (Qt::black);
47  }
48 
49  // Image for drawing
50  QImage img (width (),
51  height (),
52  QImage::Format_RGB32);
53  QPainter painter (&img);
54 
55  painter.fillRect (0,
56  0,
57  width (),
58  height (),
59  QBrush (m_enabled ? COLOR_FOR_BRUSH_ENABLED : COLOR_FOR_BRUSH_DISABLED));
60 
61  if (m_enabled) {
62  painter.setPen (QPen (color, m_pointStyle.lineWidth()));
63  painter.drawPolygon (polygonScaled);
64  }
65 
66  // Create pixmap from image
67  QPixmap pixmap = QPixmap::fromImage (img);
68 
69  return pixmap;
70 }
71 
72 void ViewPointStyle::setEnabled (bool enabled)
73 {
74  LOG4CPP_INFO_S ((*mainCat)) << "ViewPointStyle::setEnabled"
75  << " enabled=" << (enabled ? "true" : "false");
76 
77  m_enabled = enabled;
78  setPixmap (pixmapForCurrentSettings ());
79 }
80 
81 void ViewPointStyle::setPointStyle (const PointStyle &pointStyle)
82 {
83  LOG4CPP_INFO_S ((*mainCat)) << "ViewPointStyle::setPointStyle";
84 
85  m_pointStyle = pointStyle;
86  setPixmap (pixmapForCurrentSettings ());
87 }
88 
90 {
91  LOG4CPP_INFO_S ((*mainCat)) << "ViewPointStyle::unsetPointStyle";
92 
93  QPixmap pEmpty (width (),
94  height ());
95  pEmpty.fill (COLOR_FOR_BRUSH_DISABLED);
96 
97  setPixmap (pEmpty);
98 }
void unsetPointStyle()
Apply no PointStyle.
ViewPointStyle(QWidget *parent=0)
Single constructor.
QPolygonF polygon() const
Return the polygon for creating a QGraphicsPolygonItem. The size is determined by the radius...
Definition: PointStyle.cpp:155
Details for a specific Point.
Definition: PointStyle.h:20
void setEnabled(bool enabled)
Show the style with semi-transparency or full-transparency to indicate if associated Curve is active ...
void setPointStyle(const PointStyle &pointStyle)
Apply the PointStyle of the currently selected curve.
ColorPalette paletteColor() const
Get method for point color.
Definition: PointStyle.cpp:150
int lineWidth() const
Get method for line width.
Definition: PointStyle.cpp:119