File indexing completed on 2022-09-27 12:31:08

0001 /*
0002     SPDX-FileCopyrightText: 2003-2008 Cies Breijs <cies AT kde DOT nl>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "colorpicker.h"
0008 #include "interpreter/translator.h"  // for getting the translated ArgumentSeparator
0009 
0010 #include <QApplication>
0011 #include <QBoxLayout>
0012 #include <QClipboard>
0013 #include <QDialogButtonBox>
0014 #include <QFontDatabase>
0015 #include <QGridLayout>
0016 #include <QLabel>
0017 #include <QLineEdit>
0018 #include <QPushButton>
0019 #include <QSlider>
0020 #include <QSpinBox>
0021 
0022 #include <KLocalizedString>
0023 #include <KStandardGuiItem>
0024 #include <KGuiItem>
0025 
0026 
0027 ColorPicker::ColorPicker(QWidget* parent)
0028     : QDialog(parent)
0029 {
0030     setWindowTitle(i18nc("@title:window", "Color Picker"));
0031     setModal(false);
0032 
0033     QVBoxLayout *mainLayout = new QVBoxLayout;
0034     setLayout(mainLayout);
0035 
0036     QWidget* baseWidget = new QWidget(this);
0037     mainLayout->addWidget(baseWidget);
0038 
0039     QVBoxLayout* baseLayout = new QVBoxLayout;
0040     baseLayout->setContentsMargins(0, 0, 0, 0);
0041     baseWidget->setLayout( baseLayout );
0042     QGridLayout* gridLayout = new QGridLayout;
0043     baseLayout->addLayout(gridLayout);
0044 
0045     redSlider = new QSlider(Qt::Horizontal, this);
0046     redSlider->setMaximum(255);
0047     redSpin = new QSpinBox(this);
0048     redSpin->setMaximum(255);
0049     QLabel* redLabel = new QLabel(i18n("Amount red:"));
0050     redLabel->setBuddy(redSpin);
0051     gridLayout->addWidget(redLabel, 0, 0);
0052     gridLayout->addWidget(redSlider, 0, 1);
0053     gridLayout->addWidget(redSpin, 0, 2);
0054     connect(redSlider, &QSlider::valueChanged, redSpin, &QSpinBox::setValue);
0055     connect(redSpin, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), redSlider, &QSlider::setValue);
0056     connect(redSpin, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &ColorPicker::redChanged);
0057 
0058 
0059     greenSlider = new QSlider(Qt::Horizontal, this);
0060     greenSlider->setMaximum(255);
0061     greenSpin = new QSpinBox(this);
0062     greenSpin->setMaximum(255);
0063     QLabel* greenLabel = new QLabel(i18n("Amount green:"));
0064     greenLabel->setBuddy(greenSpin);
0065     gridLayout->addWidget(greenLabel, 1, 0);
0066     gridLayout->addWidget(greenSlider, 1, 1);
0067     gridLayout->addWidget(greenSpin, 1, 2);
0068     connect(greenSlider, &QSlider::valueChanged, greenSpin, &QSpinBox::setValue);
0069     connect(greenSpin, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), greenSlider, &QSlider::setValue);
0070     connect(greenSpin, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &ColorPicker::greenChanged);
0071 
0072     blueSlider = new QSlider(Qt::Horizontal, this);
0073     blueSlider->setMaximum(255);
0074     blueSpin = new QSpinBox(this);
0075     blueSpin->setMaximum(255);
0076     QLabel* blueLabel = new QLabel(i18n("Amount blue:"));
0077     blueLabel->setBuddy(blueSpin);
0078     gridLayout->addWidget(blueLabel, 2, 0);
0079     gridLayout->addWidget(blueSlider, 2, 1);
0080     gridLayout->addWidget(blueSpin, 2, 2);
0081     connect(blueSlider, &QSlider::valueChanged, blueSpin, &QSpinBox::setValue);
0082     connect(blueSpin, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), blueSlider, &QSlider::setValue);
0083     connect(blueSpin, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &ColorPicker::blueChanged);
0084 
0085     baseLayout->addSpacing(20);
0086 
0087     colorPatch = new ColorPatch(this);
0088     colorPatch->setMinimumWidth(100);
0089     colorPatch->setMinimumHeight(60);
0090     baseLayout->addWidget(colorPatch);
0091 
0092     baseLayout->addSpacing(20);
0093 
0094     QHBoxLayout* resultLayout = new QHBoxLayout;
0095     resultLayout->addStretch();
0096 
0097     baseLayout->addLayout(resultLayout);
0098     resultBox = new QLineEdit(this);
0099     resultBox->setReadOnly(true);
0100     resultBox->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
0101     int width = QFontMetrics(QFontDatabase::systemFont(QFontDatabase::FixedFont)).boundingRect(QStringLiteral("255, 255, 255_000")).width();
0102     resultBox->setMinimumWidth(width);
0103     resultBox->setMaximumWidth(width);
0104     resultLayout->addWidget(resultBox);
0105 
0106     QPushButton* copyButton = new QPushButton(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("&Copy to clipboard"), baseWidget);
0107     mainLayout->addWidget(copyButton);
0108     resultLayout->addWidget(copyButton);
0109     connect(copyButton, &QPushButton::clicked, this, &ColorPicker::copyProxy);
0110 
0111     QPushButton* pasteButton = new QPushButton(QIcon::fromTheme(QStringLiteral("edit-paste")), i18n("&Paste to editor"), baseWidget);
0112     mainLayout->addWidget(pasteButton);
0113     resultLayout->addWidget(pasteButton);
0114     connect(pasteButton, &QPushButton::clicked, this, &ColorPicker::pasteProxy);
0115 
0116     QDialogButtonBox *buttonBox = new QDialogButtonBox();
0117     QWidget *mainWidget = new QWidget(this);
0118     mainLayout->addWidget(mainWidget);
0119 
0120     QPushButton *user1Button = new QPushButton;
0121     buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
0122     connect(buttonBox, &QDialogButtonBox::accepted, this, &ColorPicker::accept);
0123     connect(buttonBox, &QDialogButtonBox::rejected, this, &ColorPicker::reject);
0124 
0125     mainLayout->addWidget(buttonBox);
0126     user1Button->setDefault(true);
0127     KGuiItem::assign(user1Button, KStandardGuiItem::close());
0128     connect(user1Button, &QPushButton::clicked, this, &ColorPicker::hide);
0129 
0130     resultLayout->addStretch();
0131 
0132     updateResult(0, 0, 0);
0133 }
0134 
0135 void ColorPicker::redChanged(int value)
0136 {
0137     updateResult(value, greenSlider->value(), blueSlider->value());
0138 }
0139 
0140 void ColorPicker::greenChanged(int value)
0141 {
0142     updateResult(redSlider->value(), value, blueSlider->value());
0143 }
0144 
0145 void ColorPicker::blueChanged(int value)
0146 {
0147     updateResult(redSlider->value(), greenSlider->value(), value);
0148 }
0149 
0150 void ColorPicker::updateResult(int r, int g, int b)
0151 {
0152     QString separator(Translator::instance()->default2localized(QStringLiteral(",")));
0153     resultBox->setText(QStringLiteral("%2%1 %3%1 %4 \n").arg(separator).arg(r).arg(g).arg(b));
0154     colorPatch->setColor(QColor(r, g, b));
0155     colorPatch->repaint();
0156 }
0157 
0158 void ColorPicker::copyProxy()
0159 {
0160     QApplication::clipboard()->setText(resultBox->text());
0161 }
0162 
0163 void ColorPicker::pasteProxy()
0164 {
0165     Q_EMIT pasteText(resultBox->text());
0166 }