File indexing completed on 2024-05-19 04:36:34
0001 /* This file is part of the TikZKit project. 0002 * 0003 * Copyright (C) 2014 Dominik Haumann <dhaumann@kde.org> 0004 * 0005 * This library is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU Library General Public License as published 0007 * by the Free Software Foundation, either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This library 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 Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, see 0017 * <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "ColorWidget.h" 0021 #include "ColorPalette.h" 0022 #include "SliderSpinBox.h" 0023 0024 #include <tikz/core/Style.h> 0025 #include <tikz/core/tikz.h> 0026 0027 #include <QDebug> 0028 #include <QAbstractButton> 0029 #include <QButtonGroup> 0030 #include <QComboBox> 0031 #include <QDir> 0032 #include <QLabel> 0033 #include <QGridLayout> 0034 #include <QVBoxLayout> 0035 #include <QPainter> 0036 #include <QPaintEvent> 0037 #include <QVector> 0038 0039 namespace tikz { 0040 namespace ui { 0041 0042 class ColorButton : public QAbstractButton 0043 { 0044 Q_OBJECT 0045 0046 public: 0047 ColorButton(QRgb color, QWidget * parent = nullptr) 0048 : QAbstractButton(parent) 0049 , m_color(color) 0050 { 0051 setCheckable(true); 0052 } 0053 0054 void paintEvent(QPaintEvent * ) override 0055 { 0056 QPainter p(this); 0057 p.fillRect(QRect(1, 1, 16, 16), m_color); 0058 0059 if (isChecked() || underMouse()) { 0060 p.drawRect(QRect(0, 0, 17, 17)); 0061 } 0062 } 0063 0064 QRgb color() const noexcept 0065 { 0066 return m_color; 0067 } 0068 0069 QSize sizeHint() const override 0070 { 0071 return QSize(18, 18); 0072 } 0073 0074 QSize minimumSizeHint() const override 0075 { 0076 return sizeHint(); 0077 } 0078 0079 private: 0080 QRgb m_color; 0081 }; 0082 0083 class ColorWidgetPrivate 0084 { 0085 public: 0086 QComboBox * cmbPalette = nullptr; 0087 QVector<ColorButton*> buttons; 0088 QButtonGroup * group = nullptr; 0089 QVBoxLayout * vLayout = nullptr; 0090 QGridLayout * gridLayout = nullptr; 0091 SliderSpinBox * sbOpacity = nullptr; 0092 0093 }; 0094 0095 ColorWidget::ColorWidget(QWidget * parent) 0096 : QFrame(parent) 0097 , d(new ColorWidgetPrivate()) 0098 { 0099 d->group = new QButtonGroup(this); 0100 d->vLayout = new QVBoxLayout(this); 0101 0102 // 0103 // 1. add palette chooser 0104 // 0105 { 0106 auto hbox = new QHBoxLayout(); 0107 d->vLayout->addLayout(hbox); 0108 0109 auto lblPalette = new QLabel("&Palette:", this); 0110 hbox->addWidget(lblPalette); 0111 0112 d->cmbPalette = new QComboBox(this); 0113 lblPalette->setBuddy(d->cmbPalette); 0114 hbox->addWidget(d->cmbPalette); 0115 hbox->addStretch(); 0116 0117 QDir dir("../data/palettes/"); 0118 const QStringList palettes = dir.entryList(QStringList() << "*.gpl"); 0119 0120 ColorPalette cp; 0121 for (const QString & palette : palettes) { 0122 cp.load("../data/palettes/" + palette); 0123 if (! cp.name().isEmpty()) { 0124 const QString path = QStringLiteral("../data/palettes/") + palette; 0125 d->cmbPalette->addItem(cp.name(), path); 0126 } 0127 } 0128 0129 connect(d->cmbPalette, SIGNAL(currentIndexChanged(int)), 0130 this, SLOT(setPaletteFromIndex(int))); 0131 0132 } 0133 0134 // 0135 // 2. add color buttons 0136 // 0137 d->gridLayout = new QGridLayout(); 0138 d->gridLayout->setSpacing(0); 0139 d->vLayout->addLayout(d->gridLayout); 0140 0141 // 0142 // 3. add custom color button 0143 // 0144 { 0145 auto hbox = new QHBoxLayout(); 0146 d->vLayout->addLayout(hbox); 0147 0148 auto lblOpacity = new QLabel("&Opacity:", this); 0149 hbox->addWidget(lblOpacity); 0150 0151 d->sbOpacity = new SliderSpinBox(this); 0152 d->sbOpacity->setRange(0, 100); 0153 d->sbOpacity->setSuffix("%"); 0154 d->sbOpacity->setSingleStep(10); 0155 d->sbOpacity->setMinimumWidth(100); 0156 lblOpacity->setBuddy(d->sbOpacity); 0157 hbox->addWidget(d->sbOpacity); 0158 hbox->addStretch(); 0159 } 0160 0161 // finally, select some palette 0162 setPaletteFromIndex(0); 0163 0164 connect(d->group, SIGNAL(buttonClicked(QAbstractButton*)), 0165 this, SLOT(setColor(QAbstractButton*))); 0166 } 0167 0168 ColorWidget::~ColorWidget() 0169 { 0170 delete d; 0171 } 0172 0173 void ColorWidget::setPalette(const QString & palette) 0174 { 0175 if (d->buttons.size()) { 0176 qDeleteAll(d->buttons); 0177 d->buttons.clear(); 0178 } 0179 0180 const int index = d->cmbPalette->findData(palette); 0181 Q_ASSERT(index >= 0); 0182 d->cmbPalette->setCurrentIndex(index); 0183 0184 ColorPalette cp; 0185 cp.load(palette); 0186 0187 d->buttons.reserve(cp.rows() * cp.columns()); 0188 0189 for (int row = 0, offset = 0; row < cp.rows(); ++row) { 0190 for (int col = 0; col < cp.columns(); ++col) { 0191 auto button = new ColorButton(cp.color(row, col), this); 0192 d->group->addButton(button); 0193 d->gridLayout->addWidget(button, row + offset, col); 0194 button->show(); 0195 d->buttons.append(button); 0196 } 0197 if (cp.spacingAfterRow(row)) { 0198 offset++; 0199 d->gridLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Fixed, QSizePolicy::Fixed), row + offset, 0); 0200 } 0201 } 0202 0203 resize(minimumSizeHint()); 0204 } 0205 0206 void ColorWidget::setColor(QAbstractButton * button) 0207 { 0208 auto colorButton = qobject_cast<ColorButton *>(button); 0209 Q_ASSERT(colorButton); 0210 0211 qDebug() << colorButton->color(); 0212 } 0213 0214 void ColorWidget::setPaletteFromIndex(int index) 0215 { 0216 setPalette(d->cmbPalette->itemData(index).toString()); 0217 } 0218 0219 } 0220 } 0221 0222 #include "ColorWidget.moc" 0223 0224 // kate: indent-width 4; replace-tabs on;