File indexing completed on 2024-07-14 05:27:12

0001 /***************************************************************************
0002  *     Copyright (C) 2010 by Joseph Boudou                                 *
0003  *     Copyright (C) 2014 by Renaud Guezennec                              *
0004  *     https://rolisteam.org/                                           *
0005  *                                                                         *
0006  *   rolisteam 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  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, write to the                         *
0018  *   Free Software Foundation, Inc.,                                       *
0019  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0020  ***************************************************************************/
0021 
0022 #include <common_widgets/colorbutton.h>
0023 
0024 #include <QColorDialog>
0025 #include <QDebug>
0026 #include <QPaintEvent>
0027 #include <QStyleFactory>
0028 #include <QStyleOptionFocusRect>
0029 
0030 ColorButton::ColorButton(QWidget* parent, bool transparency)
0031     : QPushButton(parent), m_color(QColor("tan")), m_hasTransparency(transparency)
0032 {
0033     setStyle(QStyleFactory::create("fusion"));
0034     //}
0035     setColor(m_color);
0036 
0037     setMaximumSize(sizeHint());
0038 
0039     // should never be the default button
0040     setDefault(false);
0041     setAutoDefault(false);
0042     //  m_dialog.setCurrentColor(m_color);
0043 
0044     connect(this, SIGNAL(pressed()), this, SLOT(openDialog()));
0045     // connect(&m_dialog, SIGNAL(colorSelected(const QColor &)), this, SLOT(setColor(const QColor &)));
0046 }
0047 
0048 ColorButton::ColorButton(const QColor& color, QWidget* parent)
0049     : QPushButton(parent), m_color(color) //, m_dialog(color, this)
0050 {
0051     setStyle(QStyleFactory::create("fusion"));
0052 
0053     setColor(m_color);
0054 
0055     setMaximumSize(sizeHint());
0056 
0057     setDefault(false);
0058     setAutoDefault(false);
0059     // m_dialog.setCurrentColor(m_color);
0060     connect(this, SIGNAL(clicked()), this, SLOT(openDialog()));
0061     // connect(&m_dialog, SIGNAL(colorSelected(const QColor &)), this, SLOT(setColor(const QColor &)));
0062 }
0063 ColorButton::~ColorButton() {}
0064 void ColorButton::openDialog()
0065 {
0066     QColorDialog dialog(this);
0067     dialog.setCurrentColor(m_color);
0068     dialog.setOption(QColorDialog::ShowAlphaChannel, m_hasTransparency);
0069     if(QDialog::Accepted == dialog.exec())
0070     {
0071         setColor(dialog.currentColor());
0072     }
0073 }
0074 
0075 QColor ColorButton::color() const
0076 {
0077     return m_color;
0078 }
0079 void ColorButton::setTransparency(bool state)
0080 {
0081     m_hasTransparency= state;
0082 }
0083 
0084 QSize ColorButton::sizeHint() const
0085 {
0086     return QSize(28, 20);
0087 }
0088 
0089 void ColorButton::setColor(const QColor& color)
0090 {
0091     if(m_color != color)
0092     {
0093         m_color= color;
0094         QPalette tmp= palette();
0095         tmp.setColor(QPalette::Button, m_color);
0096         tmp.setColor(QPalette::Window, m_color);
0097         setPalette(tmp);
0098         setAutoFillBackground(true);
0099         setStyleSheet(QString("ColorButton { background-color: rgb(%1,%2,%3);}")
0100                           .arg(color.red())
0101                           .arg(color.green())
0102                           .arg(color.blue()));
0103         emit colorChanged(m_color);
0104     }
0105 }