File indexing completed on 2024-05-12 17:12:38

0001 /***************************************************************************
0002  *  Copyright (C) 2007 by Romain Campioni                  *
0003  *  Copyright (C) 2009 by Renaud Guezennec                             *
0004  *   https://rolisteam.org/contact                   *
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 <QColorDialog>
0023 #include <QDebug>
0024 #include <QMouseEvent>
0025 #include <QPainter>
0026 #include <QVBoxLayout>
0027 
0028 #include "rwidgets/customs/colorselector.h"
0029 
0030 ///////////////////////////////////
0031 // ColorSelector
0032 ///////////////////////////////////
0033 ColorWidget::ColorWidget(QWidget* parent) : QWidget(parent) {}
0034 
0035 void ColorWidget::setColor(QColor color)
0036 {
0037     m_color= color;
0038 }
0039 
0040 QColor ColorWidget::getColor()
0041 {
0042     return m_color;
0043 }
0044 
0045 void ColorWidget::mousePressEvent(QMouseEvent* event)
0046 {
0047     if(event->button() == Qt::LeftButton)
0048     {
0049         emit clicked(m_color);
0050         event->accept();
0051     }
0052     QWidget::mousePressEvent(event);
0053 }
0054 void ColorWidget::paintEvent(QPaintEvent* event)
0055 {
0056     Q_UNUSED(event)
0057     QPainter painter(this);
0058     painter.fillRect(rect(), m_color);
0059 }
0060 
0061 ///////////////////////////////////
0062 // ColorSelector
0063 ///////////////////////////////////
0064 SelectedColor ColorSelector::s_selectedColor;
0065 
0066 ColorSelector::ColorSelector(QWidget* parent) : QWidget(parent)
0067 {
0068     m_isGM= false;
0069     m_pressedButton= false;
0070 
0071     m_preferences= PreferencesManager::getInstance();
0072 
0073     int rouge[48]
0074         = {255, 255, 128, 0, 128, 0, 255, 255, 255, 255, 128, 0, 0, 0, 128, 255, 128, 255, 0,   0,   0,  128, 128, 255,
0075            128, 255, 0,   0, 0,   0, 128, 128, 64,  128, 0,   0, 0, 0, 64,  64,  0,   128, 128, 128, 64, 192, 64,  255};
0076     int vert[48]= {128, 255, 255, 255, 255, 128, 128, 128, 0, 255, 255, 255, 255, 128, 128, 0,
0077                    64,  128, 255, 128, 64,  128, 0,   0,   0, 128, 128, 128, 0,   0,   0,   0,
0078                    0,   64,  64,  64,  0,   0,   0,   0,   0, 128, 128, 128, 128, 192, 64,  255};
0079     int bleu[48]
0080         = {128, 128, 128, 128, 255, 255, 192, 255, 0, 0, 0, 64, 255, 192, 192, 255, 64, 64, 0,  128, 128, 255, 64, 128,
0081            0,   0,   0,   64,  255, 160, 128, 255, 0, 0, 0, 64, 128, 64,  64,  128, 0,  0,  64, 128, 128, 192, 64, 255};
0082 
0083     // Creation du layout principale
0084     // m_layoutSelector = new QVBoxLayout(this);
0085     m_layoutSelector= new FlowLayout(this);
0086     m_layoutSelector->setContentsMargins(QMargins(2, 2, 2, 2));
0087     m_layoutSelector->setSpacing(1);
0088 
0089     s_selectedColor.type= ColorType;
0090     s_selectedColor.color= QColor(rouge[40], vert[40], bleu[40]);
0091 
0092     m_currentColor= new QLabel(this);
0093     m_currentColor->setFrameStyle(QFrame::Panel | QFrame::Raised);
0094     m_currentColor->setLineWidth(1);
0095     m_currentColor->setMidLineWidth(1);
0096 
0097     m_currentColor->setFixedSize(45, 40);
0098     setBackgroundColorToWidget(m_currentColor, s_selectedColor.color);
0099     m_currentColor->setToolTip(QStringLiteral("%1 1").arg(m_predefineColor));
0100     m_currentColor->setAutoFillBackground(true);
0101     m_currentColor->setScaledContents(true);
0102 
0103     // Ajout de la couleur actuelle au layout principal
0104     m_layoutSelector->addWidget(m_currentColor);
0105     m_layoutSelector->setAlignment(m_currentColor, Qt::AlignHCenter | Qt::AlignTop);
0106 
0107     // Création du layout de la grille de couleurs predefinies
0108     m_predefinedGrid= new QGridLayout();
0109     m_predefinedGrid->setSpacing(1);
0110     m_predefinedGrid->setContentsMargins(QMargins(1, 1, 1, 1));
0111 
0112     QWidget* wid= new QWidget(this);
0113 
0114     // Creation des widgets pour les couleurs predefinies
0115     int i= 0, x= 0, y= 0;
0116     for(; i < 48; i++)
0117     {
0118         QColor couleur(rouge[i], vert[i], bleu[i]);
0119 
0120         m_predefinedColor.append(new ColorWidget(wid));
0121         m_predefinedColor[i]->setColor(couleur);
0122         m_predefinedColor[i]->setAutoFillBackground(true);
0123         m_predefinedColor[i]->setFixedHeight(5);
0124         m_predefinedColor[i]->setFixedWidth(5);
0125         m_predefinedColor[i]->setToolTip(QStringLiteral("%2 %1 ").arg(i + 1).arg(m_predefineColor));
0126         connect(m_predefinedColor[i], SIGNAL(clicked(QColor)), this, SLOT(changeCurrentColor(QColor)));
0127 
0128         QColorDialog::setStandardColor(x * 6 + y, couleur.rgb());
0129         m_predefinedGrid->addWidget(m_predefinedColor[i], y, x);
0130 
0131         x++;
0132         y= x >= 8 ? y + 1 : y;
0133         x= x >= 8 ? 0 : x;
0134     }
0135     wid->setLayout(m_predefinedGrid);
0136     m_layoutSelector->addWidget(wid);
0137 
0138     // Création du layout de la grille de couleurs personnelles
0139     m_characterGrid= new QGridLayout();
0140     m_characterGrid->setSpacing(1);
0141     m_characterGrid->setContentsMargins(QMargins(1, 1, 1, 1));
0142     // Ajout de la grille de couleurs personnelles dans le layout principal
0143     wid= new QWidget(this);
0144 
0145     // Creation des widgets pour les couleurs personnelles
0146     for(i= 0, x= 0, y= 7; i < 16; i++)
0147     {
0148         // Creation d'un widget de couleur blanche
0149         m_personalColor.append(new ColorWidget(wid));
0150         connect(m_personalColor[i], SIGNAL(clicked(QColor)), this, SLOT(changeCurrentColor(QColor)));
0151         m_personalColor[i]->setAutoFillBackground(true);
0152         m_personalColor[i]->setFixedHeight(5);
0153         m_personalColor[i]->setFixedWidth(5);
0154         m_personalColor[i]->setToolTip(tr("Custom Color %1 ").arg(i + 1));
0155 
0156         // Mise a jour des couleurs personnelles de QColorDialog
0157         QColorDialog::setCustomColor(
0158             i, m_preferences->value(QString("customcolors%1").arg(i), QColor(Qt::white)).value<QColor>().rgb());
0159 
0160         // Ajout du widget au layout
0161         m_characterGrid->addWidget(m_personalColor[i], y, x);
0162 
0163         x++;
0164         y= x >= 8 ? y + 1 : y;
0165         x= x >= 8 ? 0 : x;
0166     }
0167     wid->setLayout(m_characterGrid);
0168     m_layoutSelector->addWidget(wid);
0169 
0170     updatePersonalColor();
0171 
0172     // Création du layout des couleurs speciales
0173     m_specialColor= new QHBoxLayout();
0174     m_specialColor->setSpacing(1);
0175     m_specialColor->setContentsMargins(QMargins());
0176     // Ajout du layout des couleurs specuales dans le layout principal
0177     wid= new QWidget(this);
0178     wid->setLayout(m_specialColor);
0179     m_layoutSelector->addWidget(wid);
0180 
0181     // Ajout de la couleur speciale qui efface
0182     m_eraseColor= new QLabel(this);
0183     m_eraseColor->setFrameStyle(QFrame::Box | QFrame::Raised);
0184     m_eraseColor->setLineWidth(0);
0185     m_eraseColor->setMidLineWidth(1);
0186     m_eraseColor->setFixedHeight(15);
0187     m_eraseColor->setFixedWidth(15);
0188     m_pixelErase= new QPixmap(":/resources/images/erase.png");
0189     m_eraseColor->setPixmap(*m_pixelErase);
0190     setBackgroundColorToWidget(m_eraseColor, QColor(Qt::white));
0191     m_eraseColor->setScaledContents(true);
0192     m_eraseColor->setToolTip(tr("Erase"));
0193     m_eraseColor->setAutoFillBackground(true);
0194     m_specialColor->addWidget(m_eraseColor);
0195 
0196     // Ajout de la couleur speciale qui masque
0197     m_maskColor= new QLabel(this);
0198     m_maskColor->setFrameStyle(QFrame::Box | QFrame::Raised);
0199     m_maskColor->setLineWidth(0);
0200     m_maskColor->setMidLineWidth(1);
0201     m_maskColor->setFixedHeight(15);
0202     m_maskColor->setFixedWidth(15);
0203     setBackgroundColorToWidget(m_maskColor, QColor(Qt::white));
0204     m_maskPixel= new QPixmap(":/resources/images/hide.png");
0205     m_maskColor->setPixmap(*m_maskPixel);
0206     m_maskColor->setScaledContents(true);
0207     m_maskColor->setAutoFillBackground(true);
0208     m_specialColor->addWidget(m_maskColor);
0209 
0210     // Ajout de la couleur speciale qui demasque
0211     m_unveilColor= new QLabel(this);
0212     m_unveilColor->setFrameStyle(QFrame::Box | QFrame::Raised);
0213     m_unveilColor->setLineWidth(0);
0214     m_unveilColor->setMidLineWidth(1);
0215     m_unveilColor->setFixedHeight(15);
0216     m_unveilColor->setFixedWidth(15);
0217     setBackgroundColorToWidget(m_unveilColor, QColor(Qt::white));
0218     m_unveilPixel= new QPixmap(":/resources/images/showMap.png");
0219     m_unveilColor->setPixmap(*m_unveilPixel);
0220     m_unveilColor->setScaledContents(true);
0221     m_unveilColor->setAutoFillBackground(true);
0222     m_specialColor->addWidget(m_unveilColor);
0223 
0224     // Taille de la palette
0225     // setFixedHeight(126);
0226 
0227     m_maskColor->installEventFilter(this);
0228     m_unveilColor->installEventFilter(this);
0229     m_eraseColor->installEventFilter(this);
0230     m_currentColor->installEventFilter(this);
0231 }
0232 ColorSelector::~ColorSelector()
0233 {
0234     delete m_currentColor;
0235     delete m_eraseColor;
0236     delete m_maskColor;
0237     delete m_unveilColor;
0238     delete m_pixelErase;
0239     delete m_maskPixel;
0240     delete m_unveilPixel;
0241     delete m_specialColor;
0242     delete m_characterGrid;
0243     delete m_layoutSelector;
0244 }
0245 void ColorSelector::updateUi(bool isGM)
0246 {
0247     m_isGM= isGM;
0248     checkPermissionColor();
0249 }
0250 
0251 void ColorSelector::checkPermissionColor()
0252 {
0253     // player
0254     if(!m_isGM)
0255     {
0256         PreferencesManager::getInstance()->registerValue("Fog_color", QColor(0, 0, 0), false);
0257         m_maskColor->setToolTip(tr("Hide (GM only)"));
0258         m_unveilColor->setToolTip(tr("Unveil (GM only)"));
0259         m_maskColor->setEnabled(false);
0260         m_unveilColor->setEnabled(false);
0261     }
0262     else // GM
0263     {
0264         PreferencesManager::getInstance()->registerValue("Fog_color", QColor(50, 50, 50), false);
0265         m_maskColor->setToolTip(tr("Hide"));
0266         m_unveilColor->setToolTip(tr("Unveil"));
0267         m_maskColor->setEnabled(true);
0268         m_unveilColor->setEnabled(true);
0269     }
0270 }
0271 bool ColorSelector::eventFilter(QObject* obj, QEvent* event)
0272 {
0273     if(event->type() == QEvent::MouseButtonPress)
0274     {
0275         if(obj == m_maskColor)
0276         {
0277             if(!m_isGM)
0278                 return false;
0279 
0280             setBackgroundColorToWidget(m_currentColor, QColor(Qt::white));
0281             m_currentColor->setPixmap(*m_maskPixel);
0282 
0283             s_selectedColor.type= Veil;
0284             m_currentColor->setToolTip(m_maskColor->toolTip());
0285         }
0286         else if(obj == m_unveilColor)
0287         {
0288             if(!m_isGM)
0289                 return false;
0290 
0291             setBackgroundColorToWidget(m_currentColor, QColor(Qt::white));
0292             m_currentColor->setPixmap(*m_unveilPixel);
0293             s_selectedColor.type= Unveil;
0294             m_currentColor->setToolTip(m_unveilColor->toolTip());
0295         }
0296         else if(obj == m_eraseColor)
0297         {
0298             setBackgroundColorToWidget(m_currentColor, QColor(Qt::white));
0299             m_currentColor->setPixmap(*m_pixelErase);
0300             s_selectedColor.type= Erase;
0301             m_currentColor->setToolTip(m_eraseColor->toolTip());
0302         }
0303     }
0304     else if(event->type() == QEvent::MouseButtonDblClick)
0305     {
0306         if(obj == m_currentColor)
0307         {
0308             QColor couleur= QColorDialog::getColor(s_selectedColor.color, this);
0309 
0310             if(couleur.isValid())
0311             {
0312                 m_currentColor->clear();
0313                 setBackgroundColorToWidget(m_currentColor, couleur);
0314                 s_selectedColor.type= ColorType;
0315                 s_selectedColor.color= couleur;
0316                 m_currentColor->setToolTip(
0317                     tr("Red: %1, Green: %2, Blue: %3").arg(couleur.red()).arg(couleur.green()).arg(couleur.blue()));
0318             }
0319         }
0320     }
0321     return QWidget::eventFilter(obj, event);
0322 }
0323 
0324 SelectedColor& ColorSelector::getSelectedColor()
0325 {
0326     return s_selectedColor;
0327 }
0328 void ColorSelector::changeCurrentColor(QColor color)
0329 {
0330     m_currentColor->clear();
0331     setBackgroundColorToWidget(m_currentColor, color);
0332     m_currentColor->setToolTip(
0333         tr("Red: %1, Green: %2, Blue: %3").arg(color.red()).arg(color.green()).arg(color.blue()));
0334     s_selectedColor.type= ColorType;
0335     s_selectedColor.color= color;
0336 }
0337 
0338 void ColorSelector::updatePersonalColor()
0339 {
0340     for(int i= 0, j= 0; i < 16; i++)
0341     {
0342         setBackgroundColorToWidget(m_personalColor[i], QColor(QColorDialog::customColor(j)));
0343         j+= 2;
0344         j= j <= 15 ? j : 1;
0345     }
0346 }
0347 
0348 QColor ColorSelector::getPersonColor(int numero)
0349 {
0350     int numCouleur;
0351 
0352     if(numero % 2)
0353         numCouleur= (numero - 1) / 2 + 8;
0354     else
0355         numCouleur= numero / 2;
0356 
0357     return (m_personalColor[numCouleur]->palette()).color(QPalette::Window);
0358 }
0359 
0360 void ColorSelector::setBackgroundColorToWidget(QWidget* wid, QColor color)
0361 {
0362     if(color.isValid())
0363     {
0364         wid->setStyleSheet(QString("background: rgb(%1,%2,%3)").arg(color.red()).arg(color.green()).arg(color.blue()));
0365     }
0366     else
0367     {
0368         wid->setStyleSheet("");
0369     }
0370 }