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
QColor comparisonColor() const
Get the comparison color.
DisplayMode displayMode() const
Get color display mode.
void setColor(const QColor &c)
Set current color.
void colorChanged(QColor)
Emitted on setColor.
void setBackground(const QBrush &bk)
Change the background visible under transparent colors.
void paint(QPainter &painter, QRect rect) const
Show both solid and transparent side by side.
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
void resizeEvent(QResizeEvent *)
QBrush back
Background brush, visible on a transparent color.
QColor comparison
comparison color
void paintEvent(QPaintEvent *)
show current color with transparency
Show current and comparison colors side by side.
void clicked()
Emitted when the user clicks on the widget.
ColorPreview(QWidget *parent=0)
uintptr_t size
QColor color() const
Get current color.
void mouseReleaseEvent(QMouseEvent *ev)
TFSIMD_FORCE_INLINE const tfScalar & w() const
QColor col
color to be viewed
DisplayMode display_mode
How the color(s) are to be shown.
Show current color with no transparency.
void setDisplayMode(DisplayMode dm)
Set how transparent colors are handled.
QBrush background() const
Get the background visible under transparent colors.
empty_struct data[sizeof(T)/sizeof(empty_struct)]
void setComparisonColor(const QColor &c)
Set the comparison color.
void mouseMoveEvent(QMouseEvent *ev)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17