00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef WW_NO_TWOCOLORINDICATOR
00013
00014 #include "qwwtwocolorindicator.h"
00015 #include <QStylePainter>
00016 #include <QStyleOptionFrame>
00017 #include <QMouseEvent>
00018 #include <QColorDialog>
00019 #include "wwglobal_p.h"
00020
00067 class QwwTwoColorIndicatorPrivate : public QwwPrivate{
00068 public:
00069 QwwTwoColorIndicatorPrivate(QwwPrivatable *p) : QwwPrivate(p),
00070 fg(Qt::black), bg(Qt::white){
00071 fgP = false;
00072 bgP = false;
00073 active = true;
00074 }
00075 QColor fg;
00076 QColor bg;
00077 bool fgP;
00078 bool bgP;
00079 bool active;
00080 };
00081
00086 QwwTwoColorIndicator::QwwTwoColorIndicator(QWidget *parent)
00087 : QWidget(parent), QwwPrivatable(new QwwTwoColorIndicatorPrivate(this)){
00088 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00089 }
00090
00091
00095 QwwTwoColorIndicator::~QwwTwoColorIndicator() {
00096 }
00097
00103 void QwwTwoColorIndicator::paintEvent(QPaintEvent *) {
00104 Q_D(QwwTwoColorIndicator);
00105 QPainter painter(this);
00106 paintSection(&painter, QRect(width()/3, height()/3, 2*width()/3, 2*height()/3), d->bg);
00107 paintSection(&painter, QRect(0, 0, 2*width()/3, 2*height()/3), d->fg);
00108 }
00109
00110
00117 void QwwTwoColorIndicator::paintSection(QPainter * painter, const QRect & rect, const QColor & color) {
00118 QStyleOptionFrame opt;
00119 opt.initFrom(this);
00120 opt.rect = rect;
00121 painter->fillRect(opt.rect.adjusted(1, 1, -1, -1), opt.state & QStyle::State_Enabled ? color : palette().color(QPalette::Disabled, QPalette::Window ));
00122 style()->drawPrimitive(QStyle::PE_Frame, &opt, painter, this);
00123 }
00124
00125
00129 void QwwTwoColorIndicator::switchColors() {
00130 Q_D(QwwTwoColorIndicator);
00131 if (d->fg==d->bg) return;
00132 qSwap(d->fg, d->bg);
00133 emit bgChanged(d->bg);
00134 emit fgChanged(d->fg);
00135 update();
00136 }
00137
00142 void QwwTwoColorIndicator::setBgColor(const QColor &b) {
00143 Q_D(QwwTwoColorIndicator);
00144 if (b==d->bg) return;
00145 d->bg = b;
00146 emit bgChanged(b);
00147 update();
00148 }
00149
00154 void QwwTwoColorIndicator::setFgColor(const QColor &f) {
00155 Q_D(QwwTwoColorIndicator);
00156 if (f==d->fg) return;
00157 d->fg = f;
00158 emit fgChanged(f);
00159 update();
00160 }
00161
00162
00163
00164 const QColor & QwwTwoColorIndicator::bgColor() const {
00165 Q_D(const QwwTwoColorIndicator);
00166 return d->bg;
00167 }
00168
00169 const QColor & QwwTwoColorIndicator::fgColor() const {
00170 Q_D(const QwwTwoColorIndicator);
00171 return d->fg;
00172 }
00173
00179 bool QwwTwoColorIndicator::isActive() const {
00180 Q_D(const QwwTwoColorIndicator);
00181 return d->active;
00182 }
00183
00189 void QwwTwoColorIndicator::setActive(bool a) {
00190 Q_D(QwwTwoColorIndicator);
00191 d->active = a;
00192 }
00193
00194
00198 QSize QwwTwoColorIndicator::sizeHint() const {
00199 return QSize(40,40);
00200 }
00201
00202
00206 QSize QwwTwoColorIndicator::minimumSizeHint() const {
00207 return QSize(20,20);
00208 }
00209
00213 void QwwTwoColorIndicator::mousePressEvent(QMouseEvent *ev) {
00214 Q_D(QwwTwoColorIndicator);
00215 int w = width()/3;
00216 int h = height()/3;
00217 if (ev->button() == Qt::LeftButton) {
00218 if (QRect(0, 0, 2*w, 2*h).contains(ev->pos())) {
00219 d->fgP = true;
00220 d->bgP = false;
00221 emit fgPressed();
00222 } else if (QRect(w, h, 2*w, 2*h).contains(ev->pos())) {
00223 d->bgP = true;
00224 d->fgP = false;
00225 emit bgPressed();
00226 }
00227 }
00228 }
00229
00233 void QwwTwoColorIndicator::mouseReleaseEvent(QMouseEvent *ev) {
00234 Q_D(QwwTwoColorIndicator);
00235 int w = width()/3;
00236 int h = height()/3;
00237 if (ev->button() == Qt::LeftButton) {
00238 if (d->fgP && QRect(0, 0, 2*w, 2*h).contains(ev->pos())) {
00239 emit fgClicked();
00240 if (d->active) {
00241 QColor c = QColorDialog::getColor(d->fg, this);
00242 if (c.isValid()) {
00243 setFgColor(c);
00244 }
00245 }
00246 } else if (d->bgP && QRect(w, h, 2*w, 2*h).contains(ev->pos())) {
00247 emit bgClicked();
00248 if (d->active) {
00249 QColor c = QColorDialog::getColor(d->bg, this);
00250 if (c.isValid()) {
00251 setBgColor(c);
00252 }
00253 }
00254 }
00255 d->fgP = d->bgP = false;
00256 }
00257 }
00258
00259 #endif