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