File indexing completed on 2024-12-22 04:18:17

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "colorbutton.h"
0014 
0015 #include <QPainter>
0016 #include <QColorDialog>
0017 #include <QDebug>
0018 #include <qdrawutil.h>
0019 
0020 namespace Kst {
0021 
0022 ColorButton::ColorButton(QWidget *parent)
0023   : QToolButton(parent), _color(Qt::black) {
0024   setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
0025   connect (this, SIGNAL(clicked()), this, SLOT(chooseColor()));
0026 }
0027 
0028 
0029 ColorButton::ColorButton(const QColor &color, QWidget *parent)
0030   : QToolButton(parent), _color(color) {
0031   setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
0032   connect (this, SIGNAL(clicked()), this, SLOT(chooseColor()));
0033 }
0034 
0035 
0036 ColorButton::~ColorButton() {
0037 }
0038 
0039 
0040 QColor ColorButton::color() const {
0041   return _color;
0042 }
0043 
0044 
0045 void ColorButton::clearSelection() {
0046   setColor(QColor(Qt::transparent));
0047 }
0048 
0049 
0050 bool ColorButton::colorDirty() const {
0051   return _color != QColor(Qt::transparent);
0052 }
0053 
0054 
0055 void ColorButton::setColor(const QColor &color) {
0056   _color = color;
0057   update();
0058   emit changed(color);
0059 }
0060 
0061 
0062 void ColorButton::paintEvent(QPaintEvent *event)
0063 {
0064   QToolButton::paintEvent(event);
0065   if (!isEnabled())
0066       return;
0067 
0068   QPainter painter(this);
0069   QBrush brush(_color);
0070   int m = (rect().height())/6;
0071 
0072   qDrawShadePanel(&painter, rect().x() + m, rect().y() + m, rect().width() - 2*m, rect().height() - 2*m,
0073                   palette(), /*sunken*/ isDown(), /*lineWidth*/ 1, /*fill*/ &brush);
0074 
0075 }
0076 
0077 
0078 void ColorButton::chooseColor() {
0079 
0080   if (_color == Qt::transparent) {
0081     _color = Qt::black;
0082   }
0083 
0084   QColor color = QColorDialog::getColor(_color, parentWidget(), "Choose Color", QColorDialog::ShowAlphaChannel);
0085 
0086   if (color.isValid()) {
0087     setColor(color);
0088   }
0089 }
0090 
0091 }
0092 
0093 // vim: ts=2 sw=2 et