color_preview.cpp
Go to the documentation of this file.
1 
23 #include "color_preview.hpp"
24 
25 #include <QStylePainter>
26 #include <QStyleOptionFrame>
27 #include <QMouseEvent>
28 #include <QDrag>
29 #include <QMimeData>
30 
31 namespace color_widgets {
32 
34 {
35 public:
36  QColor col;
37  QColor comparison;
38  QBrush back;
40 
41  Private() : col(Qt::red), back(Qt::darkGray, Qt::DiagCrossPattern), display_mode(NoAlpha)
42  {}
43 };
44 
45 ColorPreview::ColorPreview(QWidget *parent) :
46  QWidget(parent), p(new Private)
47 {
48  p->back.setTexture(QPixmap(QLatin1String(":/color_widgets/alphaback.png")));
49 }
50 
52 {
53  delete p;
54 }
55 
56 void ColorPreview::setBackground(const QBrush &bk)
57 {
58  p->back = bk;
59  update();
60 }
61 
62 QBrush ColorPreview::background() const
63 {
64  return p->back;
65 }
66 
68 {
69  return p->display_mode;
70 }
71 
73 {
74  p->display_mode = m;
75  update();
76 }
77 
78 QColor ColorPreview::color() const
79 {
80  return p->col;
81 }
82 
83 QColor ColorPreview::comparisonColor() const
84 {
85  return p->comparison;
86 }
87 
89 {
90  return QSize(24,24);
91 }
92 
93 void ColorPreview::paint(QPainter &painter, QRect rect) const
94 {
95  QColor c1, c2;
96  switch(p->display_mode) {
97  case NoAlpha:
98  c1 = c2 = p->col.rgb();
99  break;
100  case AllAlpha:
101  c1 = c2 = p->col;
102  break;
103  case SplitAlpha:
104  c1 = p->col.rgb();
105  c2 = p->col;
106  break;
107  case SplitColor:
108  c1 = p->comparison;
109  c2 = p->col;
110  break;
111  }
112 
113  QStyleOptionFrame panel;
114  panel.initFrom(this);
115  panel.lineWidth = 2;
116  panel.midLineWidth = 0;
117  panel.state |= QStyle::State_Sunken;
118  style()->drawPrimitive(QStyle::PE_Frame, &panel, &painter, this);
119  QRect r = style()->subElementRect(QStyle::SE_FrameContents, &panel, this);
120  painter.setClipRect(r);
121 
122  if ( c1.alpha() < 255 || c2.alpha() < 255 )
123  painter.fillRect(0, 0, rect.width(), rect.height(), p->back);
124 
125  int w = rect.width() / 2;
126  int h = rect.height();
127  painter.fillRect(0, 0, w, h, c1);
128  painter.fillRect(w, 0, w, h, c2);
129 }
130 
131 void ColorPreview::setColor(const QColor &c)
132 {
133  p->col = c;
134  update();
135  emit colorChanged(c);
136 }
137 
138 void ColorPreview::setComparisonColor(const QColor &c)
139 {
140  p->comparison = c;
141  update();
142 }
143 
144 void ColorPreview::paintEvent(QPaintEvent *)
145 {
146  QStylePainter painter(this);
147 
148  paint(painter, geometry());
149 }
150 
151 void ColorPreview::resizeEvent(QResizeEvent *)
152 {
153  update();
154 }
155 
156 void ColorPreview::mouseReleaseEvent(QMouseEvent * ev)
157 {
158  if ( QRect(QPoint(0,0),size()).contains(ev->pos()) )
159  emit clicked();
160 }
161 
162 void ColorPreview::mouseMoveEvent(QMouseEvent *ev)
163 {
164 
165  if ( ev->buttons() &Qt::LeftButton && !QRect(QPoint(0,0),size()).contains(ev->pos()) )
166  {
167  QMimeData *data = new QMimeData;
168 
169  data->setColorData(p->col);
170 
171  QDrag* drag = new QDrag(this);
172  drag->setMimeData(data);
173 
174  QPixmap preview(24,24);
175  preview.fill(p->col);
176  drag->setPixmap(preview);
177 
178  drag->exec();
179  }
180 }
181 
182 } // namespace color_widgets
color_widgets::ColorPreview::setColor
void setColor(const QColor &c)
Set current color.
Definition: color_preview.cpp:131
color_widgets::ColorPreview::AllAlpha
@ AllAlpha
show current color with transparency
Definition: color_preview.hpp:45
color_widgets::ColorPreview::setComparisonColor
void setComparisonColor(const QColor &c)
Set the comparison color.
Definition: color_preview.cpp:138
color_widgets::ColorPreview::background
QBrush background
Definition: color_preview.hpp:39
color_widgets::ColorPreview::SplitColor
@ SplitColor
Show current and comparison colors side by side.
Definition: color_preview.hpp:47
color_widgets::ColorPreview::resizeEvent
void resizeEvent(QResizeEvent *)
Definition: color_preview.cpp:151
color_preview.hpp
color_widgets::ColorPreview::paintEvent
void paintEvent(QPaintEvent *)
Definition: color_preview.cpp:144
color_widgets::ColorPreview::setDisplayMode
void setDisplayMode(DisplayMode dm)
Set how transparent colors are handled.
Definition: color_preview.cpp:72
color_widgets::ColorPreview::NoAlpha
@ NoAlpha
Show current color with no transparency.
Definition: color_preview.hpp:44
color_widgets::ColorPreview::SplitAlpha
@ SplitAlpha
Show both solid and transparent side by side.
Definition: color_preview.hpp:46
color_widgets::ColorPreview::setBackground
void setBackground(const QBrush &bk)
Change the background visible under transparent colors.
Definition: color_preview.cpp:56
color::red
@ red
nonstd::span_lite::size
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1554
color_widgets::ColorPreview::Private::display_mode
DisplayMode display_mode
How the color(s) are to be shown.
Definition: color_preview.cpp:39
color_widgets::ColorPreview::Private::Private
Private()
Definition: color_preview.cpp:41
color_widgets
Definition: color_dialog.hpp:33
update
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
color_widgets::ColorPreview::Private::back
QBrush back
Background brush, visible on a transparent color.
Definition: color_preview.cpp:38
color_widgets::ColorPreview::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *ev)
Definition: color_preview.cpp:156
color_widgets::ColorPreview::comparisonColor
QColor comparisonColor
Definition: color_preview.hpp:37
color_widgets::ColorPreview::clicked
void clicked()
Emitted when the user clicks on the widget.
color_widgets::ColorPreview::Private::comparison
QColor comparison
comparison color
Definition: color_preview.cpp:37
color_widgets::ColorPreview::Private
Definition: color_preview.cpp:33
color_widgets::ColorPreview::color
QColor color
Definition: color_preview.hpp:36
color_widgets::ColorPreview::colorChanged
void colorChanged(QColor)
Emitted on setColor.
color_widgets::ColorPreview::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *ev)
Definition: color_preview.cpp:162
color_widgets::ColorPreview::paint
void paint(QPainter &painter, QRect rect) const
Definition: color_preview.cpp:93
color_widgets::ColorPreview::p
Private *const p
Definition: color_preview.hpp:96
color_widgets::ColorPreview::DisplayMode
DisplayMode
Definition: color_preview.hpp:42
color_widgets::ColorPreview::Private::col
QColor col
color to be viewed
Definition: color_preview.cpp:36
mqtt_test.data
dictionary data
Definition: mqtt_test.py:22
color_widgets::ColorPreview::sizeHint
QSize sizeHint() const
Definition: color_preview.cpp:88
color_widgets::ColorPreview::displayMode
DisplayMode displayMode() const
Get color display mode.
Definition: color_preview.cpp:67
color_widgets::ColorPreview::ColorPreview
ColorPreview(QWidget *parent=0)
Definition: color_preview.cpp:45
color_widgets::ColorPreview::~ColorPreview
~ColorPreview()
Definition: color_preview.cpp:51


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:43