Go to the documentation of this file.00001
00023 #include "color_preview.hpp"
00024
00025 #include <QStylePainter>
00026 #include <QStyleOptionFrame>
00027 #include <QMouseEvent>
00028 #include <QDrag>
00029 #include <QMimeData>
00030
00031 namespace color_widgets {
00032
00033 class ColorPreview::Private
00034 {
00035 public:
00036 QColor col;
00037 QColor comparison;
00038 QBrush back;
00039 DisplayMode display_mode;
00040
00041 Private() : col(Qt::red), back(Qt::darkGray, Qt::DiagCrossPattern), display_mode(NoAlpha)
00042 {}
00043 };
00044
00045 ColorPreview::ColorPreview(QWidget *parent) :
00046 QWidget(parent), p(new Private)
00047 {
00048 p->back.setTexture(QPixmap(QLatin1String(":/color_widgets/alphaback.png")));
00049 }
00050
00051 ColorPreview::~ColorPreview()
00052 {
00053 delete p;
00054 }
00055
00056 void ColorPreview::setBackground(const QBrush &bk)
00057 {
00058 p->back = bk;
00059 update();
00060 }
00061
00062 QBrush ColorPreview::background() const
00063 {
00064 return p->back;
00065 }
00066
00067 ColorPreview::DisplayMode ColorPreview::displayMode() const
00068 {
00069 return p->display_mode;
00070 }
00071
00072 void ColorPreview::setDisplayMode(DisplayMode m)
00073 {
00074 p->display_mode = m;
00075 update();
00076 }
00077
00078 QColor ColorPreview::color() const
00079 {
00080 return p->col;
00081 }
00082
00083 QColor ColorPreview::comparisonColor() const
00084 {
00085 return p->comparison;
00086 }
00087
00088 QSize ColorPreview::sizeHint() const
00089 {
00090 return QSize(24,24);
00091 }
00092
00093 void ColorPreview::paint(QPainter &painter, QRect rect) const
00094 {
00095 QColor c1, c2;
00096 switch(p->display_mode) {
00097 case NoAlpha:
00098 c1 = c2 = p->col.rgb();
00099 break;
00100 case AllAlpha:
00101 c1 = c2 = p->col;
00102 break;
00103 case SplitAlpha:
00104 c1 = p->col.rgb();
00105 c2 = p->col;
00106 break;
00107 case SplitColor:
00108 c1 = p->comparison;
00109 c2 = p->col;
00110 break;
00111 }
00112
00113 QStyleOptionFrame panel;
00114 panel.initFrom(this);
00115 panel.lineWidth = 2;
00116 panel.midLineWidth = 0;
00117 panel.state |= QStyle::State_Sunken;
00118 style()->drawPrimitive(QStyle::PE_Frame, &panel, &painter, this);
00119 QRect r = style()->subElementRect(QStyle::SE_FrameContents, &panel, this);
00120 painter.setClipRect(r);
00121
00122 if ( c1.alpha() < 255 || c2.alpha() < 255 )
00123 painter.fillRect(0, 0, rect.width(), rect.height(), p->back);
00124
00125 int w = rect.width() / 2;
00126 int h = rect.height();
00127 painter.fillRect(0, 0, w, h, c1);
00128 painter.fillRect(w, 0, w, h, c2);
00129 }
00130
00131 void ColorPreview::setColor(const QColor &c)
00132 {
00133 p->col = c;
00134 update();
00135 emit colorChanged(c);
00136 }
00137
00138 void ColorPreview::setComparisonColor(const QColor &c)
00139 {
00140 p->comparison = c;
00141 update();
00142 }
00143
00144 void ColorPreview::paintEvent(QPaintEvent *)
00145 {
00146 QStylePainter painter(this);
00147
00148 paint(painter, geometry());
00149 }
00150
00151 void ColorPreview::resizeEvent(QResizeEvent *)
00152 {
00153 update();
00154 }
00155
00156 void ColorPreview::mouseReleaseEvent(QMouseEvent * ev)
00157 {
00158 if ( QRect(QPoint(0,0),size()).contains(ev->pos()) )
00159 emit clicked();
00160 }
00161
00162 void ColorPreview::mouseMoveEvent(QMouseEvent *ev)
00163 {
00164
00165 if ( ev->buttons() &Qt::LeftButton && !QRect(QPoint(0,0),size()).contains(ev->pos()) )
00166 {
00167 QMimeData *data = new QMimeData;
00168
00169 data->setColorData(p->col);
00170
00171 QDrag* drag = new QDrag(this);
00172 drag->setMimeData(data);
00173
00174 QPixmap preview(24,24);
00175 preview.fill(p->col);
00176 drag->setPixmap(preview);
00177
00178 drag->exec();
00179 }
00180 }
00181
00182 }