File indexing completed on 2024-04-28 07:33:17

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