Engauge Digitizer  2
ViewProfileScale.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 "EngaugeAssert.h"
8 #include "ViewProfileScale.h"
9 #include <QPainter>
10 
12  QWidget *parent) :
13  QLabel (parent),
14  m_colorFilterMode (COLOR_FILTER_MODE_FOREGROUND)
15 {
16  setMinimumWidth(minimumWidth);
17 }
18 
19 void ViewProfileScale::paintEvent (QPaintEvent *event)
20 {
21  switch (m_colorFilterMode) {
22  case COLOR_FILTER_MODE_FOREGROUND:
23  paintForeground ();
24  break;
25 
26  case COLOR_FILTER_MODE_HUE:
27  paintHue ();
28  break;
29 
30  case COLOR_FILTER_MODE_INTENSITY:
31  paintIntensity ();
32  break;
33 
34  case COLOR_FILTER_MODE_SATURATION:
35  paintSaturation ();
36  break;
37 
38  case COLOR_FILTER_MODE_VALUE:
39  paintValue ();
40  break;
41 
42  default:
43  ENGAUGE_ASSERT (false);
44  }
45 
46  QLabel::paintEvent (event);
47 }
48 
49 void ViewProfileScale::paintForeground ()
50 {
51  if (qGray (m_rgbBackground) < 127) {
52  // Go from blackish to white
53  paintOneSpectrum (QColor (m_rgbBackground), QColor (Qt::white));
54  } else {
55  // Go from whitish to black
56  paintOneSpectrum (QColor (m_rgbBackground), QColor (Qt::black));
57  }
58 }
59 
60 void ViewProfileScale::paintHue ()
61 {
62  // Create two spectrums:
63  // 1) one spectrum from red to green
64  // 2) another from green to blue
65  QLinearGradient gradient (QPointF (0.0,
66  height() / 2.0),
67  QPointF (width (),
68  height () / 2.0));
69  gradient.setColorAt (0.0000, Qt::red);
70  gradient.setColorAt (0.3333, Qt::green);
71  gradient.setColorAt (0.6666, Qt::blue);
72  gradient.setColorAt (1.0000, Qt::red);
73 
74  QPainter painter (this);
75  painter.setPen (Qt::NoPen);
76 
77  QBrush brush (gradient);
78 
79  painter.setBrush (brush);
80  painter.drawRect (0,
81  0,
82  rect().width (),
83  rect().height ());
84 }
85 
86 void ViewProfileScale::paintIntensity ()
87 {
88  paintOneSpectrum (QColor (Qt::black), QColor (Qt::white));
89 }
90 
91 void ViewProfileScale::paintOneSpectrum (const QColor &colorStart,
92  const QColor &colorStop)
93 {
94  QLinearGradient gradient (QPointF (0.0,
95  height() / 2.0),
96  QPointF (width (),
97  height () / 2.0));
98  gradient.setColorAt (0, colorStart);
99  gradient.setColorAt (1, colorStop);
100 
101  QPainter painter (this);
102  painter.setPen (Qt::NoPen);
103 
104  QBrush brush (gradient);
105 
106  painter.setBrush (brush);
107  painter.drawRect (0,
108  0,
109  rect().width (),
110  rect().height ());
111 }
112 
113 void ViewProfileScale::paintSaturation ()
114 {
115  paintOneSpectrum (QColor (Qt::white), QColor (Qt::red));
116 }
117 
118 void ViewProfileScale::paintValue ()
119 {
120  paintOneSpectrum (QColor (Qt::black), QColor (Qt::red));
121 }
122 
123 void ViewProfileScale::setBackgroundColor (QRgb rgbBackground)
124 {
125  m_rgbBackground = rgbBackground;
126 }
127 
128 void ViewProfileScale::setColorFilterMode (ColorFilterMode colorFilterMode)
129 {
130  m_colorFilterMode = colorFilterMode;
131  update ();
132 }
void setBackgroundColor(QRgb rgbBackground)
Save the background color for foreground calculations.
ViewProfileScale(int minimumWidth, QWidget *parent=0)
Single constructor.
void setColorFilterMode(ColorFilterMode colorFilterMode)
Change the gradient type.
virtual void paintEvent(QPaintEvent *)
Draw the gradient.