File indexing completed on 2024-05-12 17:12:40
0001 /*************************************************************************** 0002 * Copyright (C) 2009 by Renaud Guezennec * 0003 * https://rolisteam.org/contact * 0004 * * 0005 * rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 0021 #include <QButtonGroup> 0022 #include <QtWidgets> 0023 0024 #include "vcolorselector.h" 0025 0026 #include "preferences/preferencesmanager.h" 0027 0028 VColorLabel::VColorLabel(QWidget* parent) : QAbstractButton(parent) 0029 { 0030 // setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding); 0031 QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred); 0032 policy.setHeightForWidth(true); 0033 setSizePolicy(policy); 0034 setMinimumSize(40, 40); 0035 setMaximumSize(100, 100); 0036 } 0037 void VColorLabel::mousePressEvent(QMouseEvent* ev) 0038 { 0039 Q_UNUSED(ev); 0040 emit clickedColor(this->palette().color(QPalette::Window)); 0041 QAbstractButton::mousePressEvent(ev); 0042 } 0043 int VColorLabel::heightForWidth(int width) const 0044 { 0045 return width; 0046 } 0047 void VColorLabel::resizeEvent(QResizeEvent* event) 0048 { 0049 /*int length = width()>height() ? height() : width(); 0050 setFixedSize(length,length);*/ 0051 QWidget::resizeEvent(event); 0052 } 0053 void VColorLabel::mouseDoubleClickEvent(QMouseEvent* event) 0054 { 0055 Q_UNUSED(event); 0056 emit doubledclicked(); 0057 QAbstractButton::mouseDoubleClickEvent(event); 0058 } 0059 void VColorLabel::paintEvent(QPaintEvent* event) 0060 { 0061 /* QPainter painter(this); 0062 painter.fillRect(rect(),this->palette().color(QPalette::Window));*/ 0063 QWidget::paintEvent(event); 0064 } 0065 // end of VColorLabel 0066 0067 BackgroundButton::BackgroundButton(QPixmap* p, QWidget* parent) : QPushButton(parent), m_background(p) 0068 { 0069 /* setIcon(QIcon(*m_background)); 0070 setIconSize(QSize(rect().width()-2,rect().height()-2));*/ 0071 setCheckable(true); 0072 } 0073 0074 void BackgroundButton::paintEvent(QPaintEvent* event) 0075 { 0076 Q_UNUSED(event) 0077 QPainter painter(this); 0078 painter.setRenderHint(QPainter::Antialiasing, true); 0079 QRect r= rect(); 0080 r.setBottom(r.bottom()); 0081 r.setTop(r.top()); 0082 r.setLeft(r.left()); 0083 r.setRight(r.right()); 0084 painter.drawImage(rect(), m_background->toImage(), m_background->rect()); 0085 if(isChecked()) 0086 painter.drawRect(rect().adjusted(0, 0, -1, -1)); 0087 // QPushButton::paintEvent(event); 0088 } 0089 0090 VColorSelector::VColorSelector(QWidget* parent) : QWidget(parent) 0091 { 0092 m_options= PreferencesManager::getInstance(); 0093 0094 // Creation du layout principale 0095 QVBoxLayout* selecteurLayout= new QVBoxLayout(this); 0096 0097 m_currentColorLabel= new VColorLabel(this); 0098 // m_currentColorLabel->setMaximumSize(200,200); 0099 m_currentColorLabel->setPalette(QPalette(QColor(0, 0, 0))); 0100 m_currentColorLabel->setToolTip(tr("Predefine Color 1")); 0101 m_currentColorLabel->setAutoFillBackground(true); 0102 connect(m_currentColorLabel, SIGNAL(doubledclicked()), this, SLOT(VColorSelectorDialog())); 0103 0104 m_currentColor= QColor(0, 0, 0); 0105 0106 m_colorTableChooser= new ColorTableChooser(this); 0107 // m_colorTableChooser->setSizePolicy(QSizePolicy::Fixed); 0108 // m_colorTableChooser->setMaximumSize(200,200); 0109 0110 selecteurLayout->addWidget(m_currentColorLabel, 1); 0111 // selecteurLayout->setAlignment(m_currentColorLabel, Qt::AlignHCenter | Qt::AlignTop); 0112 selecteurLayout->addWidget(m_colorTableChooser, 1); 0113 connect(m_colorTableChooser, SIGNAL(currentColorChanged(QColor)), this, SLOT(selectColor(QColor))); 0114 0115 setLayout(selecteurLayout); 0116 } 0117 0118 void VColorSelector::selectColor(const QColor& color) 0119 { 0120 m_currentColorLabel->setPalette(QPalette(color)); 0121 m_currentColor= color; 0122 0123 emit currentColorChanged(m_currentColor); 0124 } 0125 void VColorSelector::setCurrentColor(const QColor& color) 0126 { 0127 // m_currentColorLabel->clear(); 0128 m_currentColorLabel->setPalette(QPalette(color)); 0129 0130 m_currentColorLabel->setToolTip( 0131 tr("Red: %1, Green: %2, Blue: %3").arg(color.red()).arg(color.green()).arg(color.blue())); 0132 0133 m_currentColor= color; 0134 emit currentColorChanged(m_currentColor); 0135 } 0136 QColor& VColorSelector::currentColor() 0137 { 0138 return m_currentColor; 0139 } 0140 0141 void VColorSelector::VColorSelectorDialog() 0142 { 0143 QColor color= QColorDialog::getColor(m_currentColor); 0144 if(color.isValid()) 0145 { 0146 // m_currentColorLabel->setPalette(QPalette(color)); 0147 setCurrentColor(color); 0148 // m_currentColorLabel->setToolTip(tr("Red: %1, Green: %2, Blue: 0149 // %3").arg(color.red()).arg(color.green()).arg(color.blue())); 0150 } 0151 }