File indexing completed on 2024-04-28 04:31:55

0001 /*
0002  * Copyright (C) 2010-2015 by Stephen Allewell
0003  * steve.allewell@gmail.com
0004  *
0005  * This program 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 
0011 #include "CalibrateFlossDlg.h"
0012 
0013 #include <QHideEvent>
0014 #include <QShowEvent>
0015 
0016 #include <KConfigGroup>
0017 #include <KHelpClient>
0018 #include <KLocalizedString>
0019 #include <KSharedConfig>
0020 
0021 #include "Floss.h"
0022 #include "FlossScheme.h"
0023 #include "SchemeManager.h"
0024 
0025 CalibrateFlossDlg::CalibrateFlossDlg(QWidget *parent, const QString &schemeName)
0026     : QDialog(parent)
0027     , m_schemeName(schemeName)
0028     , m_item(nullptr)
0029     , m_sample(nullptr)
0030 {
0031     setWindowTitle(i18n("Calibrate Floss"));
0032     ui.setupUi(this);
0033 
0034     fillSchemeList();
0035     ui.SchemeList->setCurrentIndex(ui.SchemeList->findText(schemeName));
0036 }
0037 
0038 CalibrateFlossDlg::~CalibrateFlossDlg()
0039 {
0040     delete m_sample;
0041 }
0042 
0043 void CalibrateFlossDlg::hideEvent(QHideEvent *event)
0044 {
0045     KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).writeEntry(QStringLiteral("CalibrateFlossDlg"), size());
0046 
0047     QDialog::hideEvent(event);
0048 }
0049 
0050 void CalibrateFlossDlg::showEvent(QShowEvent *event)
0051 {
0052     QDialog::showEvent(event);
0053 
0054     if (KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).hasKey(QStringLiteral("CalibrateFlossDlg"))) {
0055         resize(KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).readEntry(QStringLiteral("CalibrateFlossDlg"), QSize()));
0056     }
0057 }
0058 
0059 void CalibrateFlossDlg::fillSchemeList()
0060 {
0061     ui.SchemeList->insertItems(0, SchemeManager::schemes());
0062 }
0063 
0064 void CalibrateFlossDlg::fillColorList()
0065 {
0066     m_item = nullptr;
0067     ui.ColorList->clear();
0068     m_schemeName = ui.SchemeList->currentText();
0069     FlossScheme *scheme = SchemeManager::scheme(m_schemeName);
0070     QListIterator<Floss *> flossIterator(scheme->flosses());
0071 
0072     while (flossIterator.hasNext()) {
0073         Floss *floss = flossIterator.next();
0074         QListWidgetItem *listWidgetItem = new QListWidgetItem;
0075         listWidgetItem->setText(QString::fromLatin1("%1 %2").arg(floss->name(), floss->description()));
0076         listWidgetItem->setData(Qt::DecorationRole, QColor(floss->color()));
0077         listWidgetItem->setData(Qt::UserRole, QString(floss->name()));
0078         listWidgetItem->setData(Qt::UserRole + 1, QString(floss->description()));
0079         listWidgetItem->setData(Qt::CheckStateRole, Qt::Unchecked);
0080         listWidgetItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
0081         ui.ColorList->addItem(listWidgetItem);
0082     }
0083 
0084     if (ui.ColorList->count()) {
0085         ui.ColorList->setCurrentItem(ui.ColorList->item(0));
0086     }
0087 }
0088 
0089 void CalibrateFlossDlg::updateSample()
0090 {
0091     if (!m_sample) {
0092         m_sample = new QPixmap(ui.ExampleColor->size());
0093     }
0094 
0095     m_sample->fill(m_sampleColor);
0096     ui.ExampleColor->setPixmap(*m_sample);
0097     ui.RedSlider->setValue(m_sampleColor.red());
0098     ui.GreenSlider->setValue(m_sampleColor.green());
0099     ui.BlueSlider->setValue(m_sampleColor.blue());
0100 }
0101 
0102 void CalibrateFlossDlg::updateName(bool modified)
0103 {
0104     KLocalizedString str =
0105         modified ? ki18nc("%1 is floss name, %2 is floss description", "%1-%2 (Modified)") : ki18nc("%1 is floss name, %2 is floss description", "%1-%2");
0106     ui.SelectedColorName->setText(str.subs(m_item->data(Qt::UserRole).toString()).subs(m_item->data(Qt::UserRole + 1).toString()).toString());
0107 }
0108 
0109 void CalibrateFlossDlg::commitColor()
0110 {
0111     if (m_item && (m_item->data(Qt::DecorationRole).value<QColor>() != m_sampleColor)) {
0112         m_calibratedColors[m_schemeName][m_item->data(Qt::UserRole).toString()] = m_sampleColor;
0113         m_item->setData(Qt::CheckStateRole, Qt::Checked);
0114     }
0115 }
0116 
0117 void CalibrateFlossDlg::on_SchemeList_currentIndexChanged(const QString &)
0118 {
0119     commitColor();
0120     fillColorList();
0121 }
0122 
0123 void CalibrateFlossDlg::on_ColorList_currentItemChanged(QListWidgetItem *item)
0124 {
0125     if (m_item) {
0126         commitColor();
0127     }
0128 
0129     m_item = item;
0130 
0131     if (item) {
0132         if (m_calibratedColors[m_schemeName].contains(m_item->data(Qt::UserRole).toString())) {
0133             m_sampleColor = m_calibratedColors[m_schemeName][m_item->data(Qt::UserRole).toString()];
0134         } else {
0135             m_sampleColor = m_item->data(Qt::DecorationRole).value<QColor>();
0136         }
0137 
0138         updateSample();
0139         updateName(m_calibratedColors[m_schemeName].contains(m_item->data(Qt::UserRole).toString()));
0140     }
0141 }
0142 
0143 void CalibrateFlossDlg::on_RedSlider_valueChanged(int red)
0144 {
0145     m_sampleColor.setRgb(red, m_sampleColor.green(), m_sampleColor.blue());
0146     updateSample();
0147     updateName(true);
0148 }
0149 
0150 void CalibrateFlossDlg::on_GreenSlider_valueChanged(int green)
0151 {
0152     m_sampleColor.setRgb(m_sampleColor.red(), green, m_sampleColor.blue());
0153     updateSample();
0154     updateName(true);
0155 }
0156 
0157 void CalibrateFlossDlg::on_BlueSlider_valueChanged(int blue)
0158 {
0159     m_sampleColor.setRgb(m_sampleColor.red(), m_sampleColor.green(), blue);
0160     updateSample();
0161     updateName(true);
0162 }
0163 
0164 void CalibrateFlossDlg::on_ResetColor_clicked()
0165 {
0166     m_sampleColor = m_item->data(Qt::DecorationRole).value<QColor>();
0167 
0168     if (m_calibratedColors[m_schemeName].contains(m_item->data(Qt::UserRole).toString())) {
0169         m_calibratedColors[m_schemeName].remove(m_item->data(Qt::UserRole).toString());
0170     }
0171 
0172     updateSample();
0173     updateName(false);
0174     m_item->setData(Qt::CheckStateRole, Qt::Unchecked);
0175 }
0176 
0177 void CalibrateFlossDlg::on_DialogButtonBox_accepted()
0178 {
0179     commitColor();
0180 
0181     QMapIterator<QString, ChangedColors> mapIterator(m_calibratedColors);
0182 
0183     while (mapIterator.hasNext()) {
0184         mapIterator.next();
0185 
0186         if (mapIterator.value().count()) {
0187             FlossScheme *scheme = SchemeManager::scheme(mapIterator.key());
0188             QListIterator<Floss *> flossIterator(scheme->flosses());
0189 
0190             while (flossIterator.hasNext()) {
0191                 Floss *f = flossIterator.next();
0192 
0193                 if (mapIterator.value().contains(f->name())) {
0194                     f->setColor(mapIterator.value()[f->name()]);
0195                 }
0196             }
0197 
0198             SchemeManager::writeScheme(mapIterator.key());
0199         }
0200     }
0201 
0202     accept();
0203 }
0204 
0205 void CalibrateFlossDlg::on_DialogButtonBox_rejected()
0206 {
0207     reject();
0208 }
0209 
0210 void CalibrateFlossDlg::on_DialogButtonBox_helpRequested()
0211 {
0212     KHelpClient::invokeHelp(QStringLiteral("CalibrateDialog"), QStringLiteral("kxstitch"));
0213 }
0214 
0215 #include "moc_CalibrateFlossDlg.cpp"