00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef WW_NO_COLORBUTTON
00013 #include "qwwcolorbutton.h"
00014 #include <QStandardItemModel>
00015 #include <QAbstractItemDelegate>
00016 #include <QPainter>
00017 #include <QListView>
00018 #include <QLayout>
00019 #include <cmath>
00020 #include <QScrollBar>
00021 #include <QColorDialog>
00022 #include "wwglobal_p.h"
00023 #include <QtDebug>
00024 #include <QEvent>
00025
00026
00027
00032 class ColorModel : public QStandardItemModel {
00033 public:
00034 ColorModel(QObject *parent=0) : QStandardItemModel(parent) {
00035 setColumnCount(1);
00036 }
00037 };
00038
00043 class ColorDelegate : public QAbstractItemDelegate {
00044 public:
00045 ColorDelegate(QObject *parent = 0) : QAbstractItemDelegate(parent) {}
00046 void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
00047 if (!index.isValid()) return;
00048 QColor c = qvariant_cast<QColor>(index.data(Qt::DecorationRole));
00049
00050 painter->save();
00051 painter->setBrush(c);
00052 painter->drawRect(option.rect);
00053 if(option.state & QStyle::State_Selected){
00054 QPen p = painter->pen();
00055 p.setWidth(2);
00056 painter->setPen(p);
00057 painter->drawRect(option.rect.adjusted(1,1,0,0));
00058 }
00059
00060
00061 painter->restore();
00062
00063 }
00064 QSize sizeHint ( const QStyleOptionViewItem & , const QModelIndex & ) const {
00065 return QSize(16,16);
00066 }
00067 };
00068
00072 class QwwColorPopup : public QWidget {
00073 public:
00074 QwwColorPopup(ColorModel *model, QWidget *parent = 0) : QWidget(parent, Qt::Popup) {
00075 QVBoxLayout *l = new QVBoxLayout(this);
00076 l->setSpacing(1);
00077 l->setMargin(0);
00078 view = new QListView;
00079 view->setFlow(QListView::LeftToRight);
00080 view->setResizeMode(QListView::Adjust);
00081 view->setWrapping(true);
00082 view->setUniformItemSizes(true);
00083 view->setMouseTracking(true);
00084
00085
00086
00087 l->addWidget(view);
00088 button = new QPushButton;
00089 button->setText(tr("Other"));
00090 button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
00091 l->addWidget(button);
00092 view->setItemDelegate(new ColorDelegate(this));
00093 view->setModel(model);
00094 if(parent)
00095 connect(button, SIGNAL(clicked()), parent, SLOT(_q_colorDialogRequested()));
00096 connect(view, SIGNAL(entered(const QModelIndex &)), parent, SLOT(_q_setCurrentIndex(const QModelIndex &)));
00097 }
00098 QListView *colorView() const {
00099 return view;
00100 }
00101
00102 private:
00103 QListView *view;
00104 QPushButton *button;
00105 };
00106
00107 class QwwColorButtonPrivate : public QwwPrivate {
00108 public:
00109 QwwColorButtonPrivate(QwwColorButton *q) : QwwPrivate(q) {
00110 popup = 0;
00111 model = 0;
00112 }
00113 QwwColorPopup *popup;
00114 ColorModel *model;
00115 QColor curCol;
00116 QString curNam;
00117 };
00118
00139 QwwColorButton::QwwColorButton(QWidget *parent)
00140 : QPushButton(parent), QwwPrivatable(new QwwColorButtonPrivate(this)) {
00141 Q_D(QwwColorButton);
00142 d->model = new ColorModel(this);
00143 connect(this, SIGNAL(clicked()), SLOT(_q_clicked()));
00144 setStandardColors();
00145 }
00146
00147
00153 QwwColorButton::~QwwColorButton() {}
00154
00158 void QwwColorButton::_q_clicked() {
00159 Q_D(QwwColorButton);
00160 if (!d->popup) {
00161 d->popup = new QwwColorPopup(d->model, this);
00162
00163 connect(d->popup->colorView(), SIGNAL(clicked(const QModelIndex&)), SLOT(_q_activated(const QModelIndex&)));
00164 }
00165 QPoint p = rect().bottomLeft();
00166 p = mapToGlobal(p);
00167 d->popup->move(p);
00168
00169
00170
00171 d->popup->show();
00172
00173 }
00174
00179 void QwwColorButton::_q_activated(const QModelIndex &ind) {
00180 Q_D(QwwColorButton);
00181 d->popup->hide();
00182 QColor c = qvariant_cast<QColor>(ind.data(Qt::DecorationRole));
00183 d->curCol = c;
00184 d->curNam = ind.data(Qt::ToolTipRole).toString();
00185 QPixmap px(12,12);
00186 QPainter pt(&px);
00187 pt.setBrush(c);
00188 pt.drawRect(0, 0, 11, 11);
00189 setIcon(px);
00190 setText(ind.data(Qt::ToolTipRole).toString());
00191 emit colorPicked(c);
00192 }
00193
00197 QColor QwwColorButton::currentColor() const {
00198 Q_D(const QwwColorButton);
00199 return d->curCol;
00200 }
00201
00207 void QwwColorButton::addColor(const QColor & c, const QString & n) {
00208 Q_D(QwwColorButton);
00209 QStandardItem *item = new QStandardItem;
00210 item->setData(c, Qt::DecorationRole);
00211 item->setData(n, Qt::ToolTipRole);
00212 d->model->appendRow(item);
00213 }
00214
00218 void QwwColorButton::clear() {
00219 Q_D(QwwColorButton);
00220 d->model->clear();
00221 }
00222
00226 void QwwColorButton::setStandardColors() {
00227 QStringList clist = QColor::colorNames();
00228 clear();
00229 foreach(QString col, clist) {
00230 QColor c;
00231 c.setNamedColor(col);
00232 addColor(c, col);
00233 }
00234 }
00235
00240 QList< QColor > QwwColorButton::colors() const {
00241 return QList<QColor>();
00242
00243 }
00244
00249 void QwwColorButton::setColors(const QList< QColor > &list) {
00250 clear();
00251 foreach(QColor col, list) {
00252 addColor(col, QString());
00253 }
00254 }
00255
00259 void QwwColorButton::_q_colorDialogRequested()
00260 {
00261 Q_D(QwwColorButton);
00262 QColor c = QColorDialog::getColor(Qt::white, this);
00263 if(c.isValid()){
00264 addColor(c, tr("Custom color"));
00265 _q_activated(d->model->index(d->model->rowCount()-1, 0));
00266 }
00267 }
00268
00273 void QwwColorButton::setCurrentColor(const QColor & c)
00274 {
00275 Q_D(QwwColorButton);
00276 if(c==d->curCol) return;
00277
00278 d->curCol = c;
00279 emit colorPicked(c);
00280 }
00281
00282 void QwwColorButton::_q_setCurrentIndex(const QModelIndex &index)
00283 {
00284 Q_D(QwwColorButton);
00285 d->popup->colorView()->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
00286 }
00287
00288
00289 #endif