ctkColorPickerButton.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // Qt includes
00022 #include <QColorDialog>
00023 #include <QIcon>
00024 #include <QPainter>
00025 #include <QPixmap>
00026 #include <QStyle>
00027 
00028 // CTK includes
00029 #include "ctkColorPickerButton.h"
00030 
00031 //-----------------------------------------------------------------------------
00032 ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
00033   :QPushButton(_parent)
00034 {
00035   connect(this, SIGNAL(toggled(bool)), this, SLOT(changeColor(bool)));
00036   this->setColor(Qt::black);
00037 
00038   this->setCheckable(true);
00039 }
00040 
00041 //-----------------------------------------------------------------------------
00042 ctkColorPickerButton::ctkColorPickerButton(const QString& _text, QWidget* _parent)
00043   :QPushButton(_text, _parent)
00044 {
00045   connect(this, SIGNAL(clicked), this, SLOT(changeColor));
00046   this->setColor(Qt::black);
00047 
00048   // Customize
00049   this->setCheckable(true);
00050 }
00051 
00052 //-----------------------------------------------------------------------------
00053 ctkColorPickerButton::ctkColorPickerButton(const QColor& _color,
00054                                              const QString& _text,
00055                                              QWidget* _parent)
00056   :QPushButton(_text, _parent)
00057 {
00058   connect(this, SIGNAL(clicked), this, SLOT(changeColor));
00059   this->setColor(_color);
00060 
00061   // Customize
00062   this->setCheckable(true);
00063 }
00064 
00065 //-----------------------------------------------------------------------------
00066 void ctkColorPickerButton::changeColor(bool change)
00067 {
00068   if (change)
00069     {
00070     this->setColor(QColorDialog::getColor(this->Color));
00071 
00072     this->setChecked(false);
00073     }
00074 }
00075 
00076 //-----------------------------------------------------------------------------
00077 void ctkColorPickerButton::setColor(const QColor& _color)
00078 {
00079   if (_color == this->Color)
00080     {
00081     return;
00082     }
00083 
00084   int _iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
00085   QPixmap pix(_iconSize, _iconSize);
00086   pix.fill(palette().button().color());
00087   QPainter p(&pix);
00088   p.setPen(QPen(Qt::gray));
00089   p.setBrush(_color);
00090   p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
00091 
00092   this->setIcon(QIcon(pix));
00093   this->setText(_color.name());
00094   
00095   this->Color = _color;
00096   emit colorChanged(this->Color);
00097 }
00098 
00099 //-----------------------------------------------------------------------------
00100 QColor ctkColorPickerButton::color()const
00101 {
00102   return this->Color;
00103 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines