File indexing completed on 2024-05-12 17:12:40
0001 /*************************************************************************** 0002 * Copyright (C) 2011 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 #include <QDebug> 0021 #include <QPainter> 0022 #include <QResizeEvent> 0023 #include <QVBoxLayout> 0024 #include <QWheelEvent> 0025 0026 #include "vcolortablechooser.h" 0027 #define MAX_COLOR 359 0028 #define MAX_SATURATION 255 0029 //////////////////////////////////////// 0030 // Implementation of ValueChooser 0031 //////////////////////////////////////// 0032 SaturationChooser::SaturationChooser() : m_polygon(QPolygon(3)) 0033 { 0034 m_currentValue= MAX_SATURATION; 0035 setStyleSheet("background-color: rgb(0,0,0)"); 0036 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); 0037 setMinimumSize(40, 10); 0038 setMaximumSize(maximumWidth(), 10); 0039 m_gradianRect= rect(); 0040 0041 m_polygon.setPoint(0, 0, 5); 0042 m_polygon.setPoint(1, 5, 9); 0043 m_polygon.setPoint(2, -5, 9); 0044 } 0045 int SaturationChooser::getValue() const 0046 { 0047 return m_currentValue; 0048 } 0049 void SaturationChooser::mousePressEvent(QMouseEvent* e) 0050 { 0051 if((e->pos().x() <= width()) && (e->pos().x() >= 0)) 0052 { 0053 m_currentValue= MAX_SATURATION * e->pos().x() / width(); 0054 emit valueChanged(m_currentValue); 0055 update(); 0056 } 0057 } 0058 void SaturationChooser::mouseMoveEvent(QMouseEvent* e) 0059 { 0060 if((e->pos().x() <= width()) && (e->pos().x() >= 0)) 0061 { 0062 m_currentValue= MAX_SATURATION * e->pos().x() / width(); 0063 emit valueChanged(m_currentValue); 0064 update(); 0065 } 0066 } 0067 0068 void SaturationChooser::wheelEvent(QWheelEvent* event) 0069 { 0070 int step= event->angleDelta().x() / 8; 0071 0072 if(step + m_currentValue > MAX_SATURATION) 0073 m_currentValue= MAX_SATURATION; 0074 else if(step + m_currentValue < 0) 0075 m_currentValue= 0; 0076 else 0077 m_currentValue+= step; 0078 emit valueChanged(m_currentValue); 0079 update(); 0080 } 0081 0082 void SaturationChooser::setColor(QColor& color) 0083 { 0084 m_color= color; 0085 } 0086 0087 void SaturationChooser::paintEvent(QPaintEvent* event) 0088 { 0089 QPainter painter(this); 0090 QLinearGradient linearGrad(QPointF(0, event->rect().height() / 2), 0091 QPointF(event->rect().width(), event->rect().height() / 2)); 0092 linearGrad.setColorAt(0, Qt::black); 0093 linearGrad.setColorAt(1, m_color); 0094 0095 QBrush brush(linearGrad); 0096 m_gradianRect= event->rect(); 0097 m_gradianRect.setHeight(m_gradianRect.height() / 2); 0098 painter.fillRect(m_gradianRect, brush); 0099 painter.setPen(Qt::black); 0100 0101 int pos= m_currentValue * width() / MAX_SATURATION; 0102 0103 painter.drawPolygon(m_polygon.translated(pos, 0)); 0104 } 0105 void SaturationChooser::colorHasChanged(int h, int s) 0106 { 0107 m_color.setHsv(h, s, MAX_SATURATION); 0108 update(); 0109 } 0110 0111 //////////////////////////////////////// 0112 // Implementation of ColorTable 0113 //////////////////////////////////////// 0114 ColorTable::ColorTable() 0115 { 0116 setStyleSheet("background-color: rgb(0,0,0)"); 0117 // setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred); 0118 QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred); 0119 policy.setHeightForWidth(true); 0120 setSizePolicy(policy); 0121 setMinimumSize(40, 40); 0122 setMaximumSize(100, 100); 0123 } 0124 0125 int ColorTable::heightForWidth(int width) const 0126 { 0127 return width; 0128 } 0129 QSize ColorTable::sizeHint() const 0130 { 0131 // return QSize(size().width(),heightForWidth(size().width())); 0132 return QWidget::sizeHint(); 0133 } 0134 void ColorTable::paintEvent(QPaintEvent* event) 0135 { 0136 QWidget::paintEvent(event); 0137 QPainter painter(this); 0138 0139 int colorv; 0140 int saturation; 0141 QColor color; 0142 0143 // int min = qMin(,); 0144 0145 for(int i= 0; i < width(); i++) 0146 { 0147 for(int j= 0; j < height(); j++) 0148 { 0149 colorv= MAX_COLOR * j / height(); 0150 saturation= MAX_SATURATION * i / width(); 0151 color.setHsv(colorv, saturation, MAX_SATURATION); 0152 painter.setPen(color); 0153 painter.drawPoint(i, j); 0154 } 0155 } 0156 } 0157 void ColorTable::mousePressEvent(QMouseEvent* e) 0158 { 0159 if(rect().contains(e->pos())) 0160 { 0161 int color= MAX_COLOR * e->pos().y() / height(); 0162 int saturation= MAX_SATURATION * e->pos().x() / width(); 0163 emit dataChanged(color, saturation); 0164 } 0165 } 0166 0167 void ColorTable::resizeEvent(QResizeEvent* event) 0168 { 0169 QWidget::resizeEvent(event); 0170 } 0171 0172 //////////////////////////////////////// 0173 // Implementation of ColorTableChooser 0174 //////////////////////////////////////// 0175 ColorTableChooser::ColorTableChooser(QWidget* parent) : QWidget(parent) 0176 { 0177 m_layout= new QVBoxLayout(); 0178 m_layout->setContentsMargins(QMargins()); 0179 m_colorTable= new ColorTable(); 0180 m_valueChooser= new SaturationChooser(); 0181 m_layout->addWidget(m_colorTable); 0182 m_layout->addWidget(m_valueChooser); 0183 setLayout(m_layout); 0184 setStyleSheet("background-color: rgb(0,0,0)"); 0185 0186 connect(m_colorTable, SIGNAL(dataChanged(int, int)), this, SLOT(colorHasChanged(int, int))); 0187 connect(m_colorTable, SIGNAL(dataChanged(int, int)), m_valueChooser, SLOT(colorHasChanged(int, int))); 0188 connect(m_valueChooser, SIGNAL(valueChanged(int)), this, SLOT(valueHasChanged(int))); 0189 setMinimumSize(40, 45); 0190 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); 0191 0192 m_h= 0; 0193 m_s= MAX_SATURATION; 0194 } 0195 ColorTableChooser::~ColorTableChooser() 0196 { 0197 qDebug() << "destructor Table Chooser"; 0198 } 0199 void ColorTableChooser::resizeEvent(QResizeEvent* event) 0200 { 0201 QWidget::resizeEvent(event); 0202 } 0203 void ColorTableChooser::valueHasChanged(int v) 0204 { 0205 m_color.setHsv(m_h, m_s, v); 0206 emit currentColorChanged(m_color); 0207 } 0208 0209 void ColorTableChooser::colorHasChanged(int h, int s) 0210 { 0211 m_h= h; 0212 m_s= s; 0213 m_color.setHsv(h, s, m_valueChooser->getValue()); 0214 emit currentColorChanged(m_color); 0215 }