File indexing completed on 2024-05-12 16:02:09

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2007 Fredy Yanardi <fyanardi@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "KoEditColorSetDialog.h"
0008 
0009 #include <KoIcon.h>
0010 
0011 #include <QScrollArea>
0012 #include <QHBoxLayout>
0013 #include <QFileInfo>
0014 #include <QColorDialog>
0015 #include <QInputDialog>
0016 
0017 #include <klocalizedstring.h>
0018 #include <kmessagebox.h>
0019 
0020 #include <resources/KoColorSet.h>
0021 #include <KoColorPatch.h>
0022 #include <KoColorSpaceRegistry.h>
0023 #include <KoFileDialog.h>
0024 
0025 // debug
0026 #include <WidgetsDebug.h>
0027 
0028 KoEditColorSetWidget::KoEditColorSetWidget(const QList<KoColorSet *> &palettes, const QString &activePalette, QWidget *parent)
0029     : QWidget(parent),
0030     m_colorSets(palettes),
0031     m_gridLayout(0),
0032     m_activeColorSet(0),
0033     m_activePatch(0),
0034     m_initialColorSetCount(palettes.count()),
0035     m_activeColorSetRequested(false)
0036 {
0037     widget.setupUi(this);
0038     foreach (KoColorSet *colorSet, m_colorSets) {
0039         //colorSet->load(); resources are loaded at startup...
0040         widget.selector->addItem(colorSet->name());
0041     }
0042     connect(widget.selector, SIGNAL(currentIndexChanged(int)), this, SLOT(setActiveColorSet(int)));
0043 
0044     // A widget that shows all colors from active palette
0045     // FIXME no need to handcode the QScrollArea if designer can add QScrollArea (Qt 4.4?)
0046     m_scrollArea = new QScrollArea(widget.patchesFrame);
0047 
0048     int index = 0;
0049     foreach (KoColorSet *set, m_colorSets) {
0050         if (set->name() == activePalette) {
0051             m_activeColorSet = set;
0052             index = widget.selector->findText(set->name());
0053             widget.selector->setCurrentIndex(index);
0054         }
0055     }
0056     if (!m_activeColorSet && !palettes.isEmpty()) {
0057         m_activeColorSet = palettes.first();
0058         index = widget.selector->findText(m_activeColorSet->name());
0059     }
0060 
0061     int columns = 16;
0062     if(m_activeColorSet) {
0063         columns = m_activeColorSet->columnCount();
0064         if (columns==0){
0065             columns = 16;
0066         }
0067     }
0068     m_scrollArea->setMinimumWidth(columns*(12+2));
0069 
0070     QHBoxLayout *layout = new QHBoxLayout(widget.patchesFrame);
0071     layout->setContentsMargins(0, 0, 0, 0);
0072     layout->addWidget(m_scrollArea);
0073 
0074     widget.add->setIcon(koIcon("list-add"));
0075     widget.remove->setIcon(koIcon("list-remove"));
0076     widget.open->setIcon(koIcon("document-open"));
0077     widget.save->setIcon(koIcon("document-save"));
0078 
0079     setEnabled(m_activeColorSet != 0);
0080     setActiveColorSet(index);
0081     widget.remove->setEnabled(false); // initially no color selected
0082 
0083     connect(widget.add, SIGNAL(clicked()), this, SLOT(addColor()));
0084     connect(widget.remove, SIGNAL(clicked()), this, SLOT(removeColor()));
0085     connect(widget.open, SIGNAL(clicked()), this, SLOT(open()));
0086     connect(widget.save, SIGNAL(clicked()), this, SLOT(save()));
0087 }
0088 
0089 KoEditColorSetWidget::~KoEditColorSetWidget()
0090 {
0091     // only delete new color sets
0092     uint colorSetCount = m_colorSets.count();
0093     for( uint i = m_initialColorSetCount; i < colorSetCount; ++i ) {
0094         KoColorSet * cs = m_colorSets[i];
0095         // if the active color set was requested by activeColorSet()
0096         // the caller takes ownership and then we do not delete it here
0097         if( cs == m_activeColorSet && m_activeColorSetRequested )
0098             continue;
0099         delete cs;
0100     }
0101 }
0102 
0103 void KoEditColorSetWidget::setActiveColorSet(int index)
0104 {
0105     if (m_gridLayout) {
0106         qDeleteAll(m_gridLayout->children());
0107         delete m_gridLayout;
0108         m_activePatch = 0;
0109     }
0110 
0111     QWidget *wdg = new QWidget(m_scrollArea);
0112     m_gridLayout = new QGridLayout(wdg);
0113     m_gridLayout->setMargin(0);
0114     m_gridLayout->setSpacing(2);
0115 
0116     m_activeColorSet = m_colorSets.value(index);
0117     setEnabled(m_activeColorSet != 0);
0118     int columns = 16;
0119 
0120     if (m_activeColorSet) {
0121         columns = m_activeColorSet->columnCount();
0122         if (columns==0){columns=16;}
0123         widget.remove->setEnabled(false);
0124         for (quint32 i = 0; i < m_activeColorSet->nColors(); i++) {
0125             KoColorPatch *patch = new KoColorPatch(widget.patchesFrame);
0126             KoColorSetEntry c = m_activeColorSet->getColorGlobal(i);
0127             patch->setColor(c.color());
0128             patch->setToolTip(c.name());
0129             connect(patch, SIGNAL(triggered(KoColorPatch*)), this, SLOT(setTextLabel(KoColorPatch*)));
0130             m_gridLayout->addWidget(patch, i/columns, i%columns);
0131         }
0132     }
0133 
0134     m_scrollArea->setMinimumWidth(columns*(12+2));
0135     m_scrollArea->setWidget(wdg);
0136 }
0137 
0138 void KoEditColorSetWidget::setTextLabel(KoColorPatch *patch)
0139 {
0140     widget.colorName->setText(patch->toolTip());
0141     if (m_activePatch) {
0142         m_activePatch->setFrameShape(QFrame::NoFrame);
0143         m_activePatch->setFrameShadow(QFrame::Plain);
0144     }
0145     m_activePatch = patch;
0146     m_activePatch->setFrameShape(QFrame::Panel);
0147     m_activePatch->setFrameShadow(QFrame::Raised);
0148     widget.remove->setEnabled(true);
0149 }
0150 
0151 void KoEditColorSetWidget::addColor()
0152 {
0153     QColor color;
0154 
0155     color = QColorDialog::getColor(color);
0156     if (color.isValid()) {
0157         KoColorSetEntry newEntry(
0158                     KoColor(color, KoColorSpaceRegistry::instance()->rgb8()),
0159                     QInputDialog::getText(this, i18n("Add Color To Palette"), i18n("Color name:")));
0160         KoColorPatch *patch = new KoColorPatch(widget.patchesFrame);
0161         patch->setColor(newEntry.color());
0162         patch->setToolTip(newEntry.name());
0163         connect(patch, SIGNAL(triggered(KoColorPatch*)), this, SLOT(setTextLabel(KoColorPatch*)));
0164         Q_ASSERT(m_gridLayout);
0165         Q_ASSERT(m_activeColorSet);
0166         m_gridLayout->addWidget(patch, m_activeColorSet->nColors()/m_activeColorSet->columnCount(), m_activeColorSet->nColors()%m_activeColorSet->columnCount());
0167         m_activeColorSet->add(newEntry);
0168     }
0169 }
0170 
0171 void KoEditColorSetWidget::removeColor()
0172 {
0173     Q_ASSERT(m_activeColorSet);
0174     for (quint32 i = 0; i < m_activeColorSet->nColors(); i++) {
0175         KoColorSetEntry c = m_activeColorSet->getColorGlobal(i);
0176         if (m_activePatch->color() == c.color()) {
0177             m_activeColorSet->removeAt(i);
0178             setActiveColorSet(widget.selector->currentIndex());
0179             break;
0180         }
0181     }
0182 }
0183 
0184 void KoEditColorSetWidget::open()
0185 {
0186     Q_ASSERT(m_activeColorSet);
0187     KoFileDialog dialog(this, KoFileDialog::OpenFile, "OpenColorSet");
0188     dialog.setDefaultDir(m_activeColorSet->filename());
0189     dialog.setMimeTypeFilters(QStringList() << "application/x-gimp-color-palette");
0190     QString fileName = dialog.filename();
0191     KoColorSet *colorSet = new KoColorSet(fileName);
0192     colorSet->load();
0193     m_colorSets.append(colorSet);
0194     widget.selector->addItem(colorSet->name());
0195     widget.selector->setCurrentIndex(widget.selector->count() - 1);
0196 }
0197 
0198 void KoEditColorSetWidget::save()
0199 {
0200     Q_ASSERT(m_activeColorSet);
0201     if (!m_activeColorSet->save())
0202         KMessageBox::error(0, i18n("Cannot write to palette file %1. Maybe it is read-only. ", m_activeColorSet->filename()), i18n("Palette"));
0203 }
0204 
0205 KoColorSet *KoEditColorSetWidget::activeColorSet()
0206 {
0207     m_activeColorSetRequested = true;
0208     return m_activeColorSet;
0209 }
0210 
0211 KoEditColorSetDialog::KoEditColorSetDialog(const QList<KoColorSet *> &palettes, const QString &activePalette, QWidget *parent)
0212     : KoDialog(parent)
0213 {
0214     ui = new KoEditColorSetWidget(palettes, activePalette, this);
0215     setMainWidget(ui);
0216     setCaption(i18n("Add/Remove Colors"));
0217     enableButton(KoDialog::Ok, ui->isEnabled());
0218 }
0219 
0220 KoEditColorSetDialog::~KoEditColorSetDialog()
0221 {
0222     delete ui;
0223 }
0224 
0225 KoColorSet *KoEditColorSetDialog::activeColorSet()
0226 {
0227     return ui->activeColorSet();
0228 }