Warning, file /office/calligra/libs/widgets/KoEditColorSetDialog.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Fredy Yanardi <fyanardi@gmail.com>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KoEditColorSetDialog.h"
0021 
0022 #include <KoIcon.h>
0023 
0024 #include <QScrollArea>
0025 #include <QHBoxLayout>
0026 #include <QFileInfo>
0027 #include <QColorDialog>
0028 #include <QInputDialog>
0029 
0030 #include <klocalizedstring.h>
0031 #include <kmessagebox.h>
0032 
0033 #include <KoColorSet.h>
0034 #include <KoColorPatch.h>
0035 #include <KoColorSpaceRegistry.h>
0036 #include <KoFileDialog.h>
0037 
0038 // debug
0039 #include <WidgetsDebug.h>
0040 
0041 KoEditColorSetWidget::KoEditColorSetWidget(const QList<KoColorSet *> &palettes, const QString &activePalette, QWidget *parent)
0042     : QWidget(parent),
0043     m_colorSets(palettes),
0044     m_gridLayout(nullptr),
0045     m_activeColorSet(nullptr),
0046     m_activePatch(nullptr),
0047     m_initialColorSetCount(palettes.count()),
0048     m_activeColorSetRequested(false)
0049 {
0050     widget.setupUi(this);
0051     foreach (KoColorSet *colorSet, m_colorSets) {
0052         colorSet->load();
0053         widget.selector->addItem(colorSet->name());
0054     }
0055     connect(widget.selector, SIGNAL(currentIndexChanged(int)), this, SLOT(setActiveColorSet(int)));
0056 
0057     // A widget that shows all colors from active palette
0058     // FIXME no need to handcode the QScrollArea if designer can add QScrollArea (Qt 4.4?)
0059     m_scrollArea = new QScrollArea(widget.patchesFrame);
0060 
0061     int index = 0;
0062     foreach (KoColorSet *set, m_colorSets) {
0063         if (set->name() == activePalette) {
0064             m_activeColorSet = set;
0065             index = widget.selector->findText(set->name());
0066             widget.selector->setCurrentIndex(index);
0067         }
0068     }
0069     if (!m_activeColorSet && !palettes.isEmpty()) {
0070         m_activeColorSet = palettes.first();
0071         index = widget.selector->findText(m_activeColorSet->name());
0072     }
0073 
0074     m_scrollArea->setMinimumWidth(16*(12+2));
0075 
0076     QHBoxLayout *layout = new QHBoxLayout();
0077     layout->setContentsMargins(0, 0, 0, 0);
0078     layout->addWidget(m_scrollArea);
0079     widget.patchesFrame->setLayout(layout);
0080 
0081     widget.add->setIcon(koIcon("list-add"));
0082     widget.remove->setIcon(koIcon("list-remove"));
0083     widget.open->setIcon(koIcon("document-open"));
0084     widget.save->setIcon(koIcon("document-save"));
0085 
0086     setEnabled(m_activeColorSet != 0);
0087     setActiveColorSet(index);
0088     widget.remove->setEnabled(false); // initially no color selected
0089 
0090     connect(widget.add, SIGNAL(clicked()), this, SLOT(addColor()));
0091     connect(widget.remove, SIGNAL(clicked()), this, SLOT(removeColor()));
0092     connect(widget.open, SIGNAL(clicked()), this, SLOT(open()));
0093     connect(widget.save, SIGNAL(clicked()), this, SLOT(save()));
0094 }
0095 
0096 KoEditColorSetWidget::~KoEditColorSetWidget()
0097 {
0098     // only delete new color sets
0099     uint colorSetCount = m_colorSets.count();
0100     for( uint i = m_initialColorSetCount; i < colorSetCount; ++i ) {
0101         KoColorSet * cs = m_colorSets[i];
0102         // if the active color set was requested by activeColorSet()
0103         // the caller takes ownership and then we do not delete it here
0104         if( cs == m_activeColorSet && m_activeColorSetRequested )
0105             continue;
0106         delete cs;
0107     }
0108 }
0109 
0110 void KoEditColorSetWidget::setActiveColorSet(int index)
0111 {
0112     if (m_gridLayout) {
0113         delete m_gridLayout;
0114         m_activePatch = nullptr;
0115     }
0116 
0117     QWidget *wdg = new QWidget(m_scrollArea);
0118     m_gridLayout = new QGridLayout();
0119     m_gridLayout->setMargin(0);
0120     m_gridLayout->setSpacing(2);
0121 
0122     m_activeColorSet = m_colorSets.value(index);
0123     setEnabled(m_activeColorSet != 0);
0124     if (m_activeColorSet) {
0125         widget.remove->setEnabled(false);
0126         for (int i = 0; i < m_activeColorSet->nColors(); i++) {
0127             KoColorPatch *patch = new KoColorPatch(widget.patchesFrame);
0128             patch->setColor(m_activeColorSet->getColor(i).color);
0129             connect(patch, SIGNAL(triggered(KoColorPatch*)), this, SLOT(setTextLabel(KoColorPatch*)));
0130             m_gridLayout->addWidget(patch, i/16, i%16);
0131         }
0132     }
0133 
0134     wdg->setLayout(m_gridLayout);
0135     m_scrollArea->setWidget(wdg);
0136 }
0137 
0138 void KoEditColorSetWidget::setTextLabel(KoColorPatch *patch)
0139 {
0140     widget.colorName->setText(patch->color().toQColor().name());
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         newEntry.color = KoColor(color, KoColorSpaceRegistry::instance()->rgb8());
0159         newEntry.name = QInputDialog::getText(this, i18n("Add Color To Palette"), i18n("Color name:"));
0160         KoColorPatch *patch = new KoColorPatch(widget.patchesFrame);
0161         patch->setColor(newEntry.color);
0162         connect(patch, SIGNAL(triggered(KoColorPatch*)), this, SLOT(setTextLabel(KoColorPatch*)));
0163         Q_ASSERT(m_gridLayout);
0164         Q_ASSERT(m_activeColorSet);
0165         m_gridLayout->addWidget(patch, m_activeColorSet->nColors()/16, m_activeColorSet->nColors()%16);
0166         m_activeColorSet->add(newEntry);
0167     }
0168 }
0169 
0170 void KoEditColorSetWidget::removeColor()
0171 {
0172     Q_ASSERT(m_activeColorSet);
0173     for (int i = 0; i < m_activeColorSet->nColors(); i++) {
0174         if (m_activePatch->color() == m_activeColorSet->getColor(i).color) {
0175             m_activeColorSet->remove(m_activeColorSet->getColor(i));
0176             setActiveColorSet(widget.selector->currentIndex());
0177             break;
0178         }
0179     }
0180 }
0181 
0182 void KoEditColorSetWidget::open()
0183 {
0184     Q_ASSERT(m_activeColorSet);
0185     KoFileDialog dialog(this, KoFileDialog::OpenFile, "OpenColorSet");
0186     dialog.setDefaultDir(m_activeColorSet->filename());
0187     dialog.setNameFilter(i18n("Gimp Color Palette (*.gpl)"));
0188     QString fileName = dialog.filename();
0189     KoColorSet *colorSet = new KoColorSet(fileName);
0190     colorSet->load();
0191     m_colorSets.append(colorSet);
0192     widget.selector->addItem(colorSet->name());
0193     widget.selector->setCurrentIndex(widget.selector->count() - 1);
0194 }
0195 
0196 void KoEditColorSetWidget::save()
0197 {
0198     Q_ASSERT(m_activeColorSet);
0199     if (!m_activeColorSet->save())
0200         KMessageBox::error(nullptr, i18n("Cannot write to palette file %1. Maybe it is read-only. ", m_activeColorSet->filename()), i18n("Palette"));
0201 }
0202 
0203 KoColorSet *KoEditColorSetWidget::activeColorSet()
0204 {
0205     m_activeColorSetRequested = true;
0206     return m_activeColorSet;
0207 }
0208 
0209 KoEditColorSetDialog::KoEditColorSetDialog(const QList<KoColorSet *> &palettes, const QString &activePalette, QWidget *parent)
0210     : KoDialog(parent)
0211 {
0212     ui = new KoEditColorSetWidget(palettes, activePalette, this);
0213     setMainWidget(ui);
0214     setCaption(i18n("Add/Remove Colors"));
0215     enableButton(KoDialog::Ok, ui->isEnabled());
0216 }
0217 
0218 KoEditColorSetDialog::~KoEditColorSetDialog()
0219 {
0220     delete ui;
0221 }
0222 
0223 KoColorSet *KoEditColorSetDialog::activeColorSet()
0224 {
0225     return ui->activeColorSet();
0226 }