KoEditColorSetDialog.cpp 7.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* This file is part of the KDE project
 * Copyright (C) 2007 Fredy Yanardi <fyanardi@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "KoEditColorSetDialog.h"

Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
22 23
#include <KoIcon.h>

24 25 26 27
#include <QScrollArea>
#include <QHBoxLayout>
#include <QFileInfo>

Halla Rempt's avatar
Halla Rempt committed
28 29 30 31
#include <klocale.h>
#include <kcolordialog.h>
#include <kinputdialog.h>
#include <kmessagebox.h>
32 33 34 35

#include <KoColorSet.h>
#include <KoColorPatch.h>
#include <KoColorSpaceRegistry.h>
Yue Liu's avatar
Yue Liu committed
36
#include <KoFileDialog.h>
37 38

// debug
Halla Rempt's avatar
Halla Rempt committed
39
#include <kdebug.h>
40

41
KoEditColorSetWidget::KoEditColorSetWidget(const QList<KoColorSet *> &palettes, const QString &activePalette, QWidget *parent)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    : QWidget(parent),
    m_colorSets(palettes),
    m_gridLayout(0),
    m_activeColorSet(0),
    m_activePatch(0),
    m_initialColorSetCount(palettes.count()),
    m_activeColorSetRequested(false)
{
    widget.setupUi(this);
    foreach (KoColorSet *colorSet, m_colorSets) {
        colorSet->load();
        widget.selector->addItem(colorSet->name());
    }
    connect(widget.selector, SIGNAL(currentIndexChanged(int)), this, SLOT(setActiveColorSet(int)));

    // A widget that shows all colors from active palette
    // FIXME no need to handcode the QScrollArea if designer can add QScrollArea (Qt 4.4?)
    m_scrollArea = new QScrollArea(widget.patchesFrame);

    int index = 0;
    foreach (KoColorSet *set, m_colorSets) {
        if (set->name() == activePalette) {
            m_activeColorSet = set;
            index = widget.selector->findText(set->name());
            widget.selector->setCurrentIndex(index);
        }
    }
    if (!m_activeColorSet && !palettes.isEmpty()) {
        m_activeColorSet = palettes.first();
        index = widget.selector->findText(m_activeColorSet->name());
    }

    m_scrollArea->setMinimumWidth(16*(12+2));

    QHBoxLayout *layout = new QHBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(m_scrollArea);
    widget.patchesFrame->setLayout(layout);

Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
81 82 83 84
    widget.add->setIcon(koIcon("list-add"));
    widget.remove->setIcon(koIcon("list-remove"));
    widget.open->setIcon(koIcon("document-open"));
    widget.save->setIcon(koIcon("document-save"));
85 86 87 88 89 90 91 92 93 94 95

    setEnabled(m_activeColorSet != 0);
    setActiveColorSet(index);
    widget.remove->setEnabled(false); // initially no color selected

    connect(widget.add, SIGNAL(clicked()), this, SLOT(addColor()));
    connect(widget.remove, SIGNAL(clicked()), this, SLOT(removeColor()));
    connect(widget.open, SIGNAL(clicked()), this, SLOT(open()));
    connect(widget.save, SIGNAL(clicked()), this, SLOT(save()));
}

96
KoEditColorSetWidget::~KoEditColorSetWidget()
97 98 99 100 101 102 103 104 105 106 107 108 109
{
    // only delete new color sets
    uint colorSetCount = m_colorSets.count();
    for( uint i = m_initialColorSetCount; i < colorSetCount; ++i ) {
        KoColorSet * cs = m_colorSets[i];
        // if the active color set was requested by activeColorSet()
        // the caller takes ownership and then we do not delete it here
        if( cs == m_activeColorSet && m_activeColorSetRequested )
            continue;
        delete cs;
    }
}

110
void KoEditColorSetWidget::setActiveColorSet(int index)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
{
    if (m_gridLayout) {
        delete m_gridLayout;
        m_activePatch = 0;
    }

    QWidget *wdg = new QWidget(m_scrollArea);
    m_gridLayout = new QGridLayout();
    m_gridLayout->setMargin(0);
    m_gridLayout->setSpacing(2);

    m_activeColorSet = m_colorSets.value(index);
    setEnabled(m_activeColorSet != 0);
    if (m_activeColorSet) {
        widget.remove->setEnabled(false);
        for (int i = 0; i < m_activeColorSet->nColors(); i++) {
            KoColorPatch *patch = new KoColorPatch(widget.patchesFrame);
            patch->setColor(m_activeColorSet->getColor(i).color);
            connect(patch, SIGNAL(triggered(KoColorPatch *)), this, SLOT(setTextLabel(KoColorPatch *)));
            m_gridLayout->addWidget(patch, i/16, i%16);
        }
    }

    wdg->setLayout(m_gridLayout);
    m_scrollArea->setWidget(wdg);
}

138
void KoEditColorSetWidget::setTextLabel(KoColorPatch *patch)
139 140 141 142 143 144 145 146 147 148 149 150
{
    widget.colorName->setText(patch->color().toQColor().name());
    if (m_activePatch) {
        m_activePatch->setFrameShape(QFrame::NoFrame);
        m_activePatch->setFrameShadow(QFrame::Plain);
    }
    m_activePatch = patch;
    m_activePatch->setFrameShape(QFrame::Panel);
    m_activePatch->setFrameShadow(QFrame::Raised);
    widget.remove->setEnabled(true);
}

151
void KoEditColorSetWidget::addColor()
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
{
    QColor color;
    int result = KColorDialog::getColor(color);
    if (result == KColorDialog::Accepted) {
        KoColorSetEntry newEntry;
        newEntry.color = KoColor(color, KoColorSpaceRegistry::instance()->rgb8());
        newEntry.name = KInputDialog::getText(i18n("Add Color To Palette"), i18n("Color name:"));
        KoColorPatch *patch = new KoColorPatch(widget.patchesFrame);
        patch->setColor(newEntry.color);
        connect(patch, SIGNAL(triggered(KoColorPatch *)), this, SLOT(setTextLabel(KoColorPatch *)));
        Q_ASSERT(m_gridLayout);
        Q_ASSERT(m_activeColorSet);
        m_gridLayout->addWidget(patch, m_activeColorSet->nColors()/16, m_activeColorSet->nColors()%16);
        m_activeColorSet->add(newEntry);
    }
}

169
void KoEditColorSetWidget::removeColor()
170 171 172 173 174 175 176 177 178 179 180
{
    Q_ASSERT(m_activeColorSet);
    for (int i = 0; i < m_activeColorSet->nColors(); i++) {
        if (m_activePatch->color() == m_activeColorSet->getColor(i).color) {
            m_activeColorSet->remove(m_activeColorSet->getColor(i));
            setActiveColorSet(widget.selector->currentIndex());
            break;
        }
    }
}

181
void KoEditColorSetWidget::open()
182 183
{
    Q_ASSERT(m_activeColorSet);
184
    KoFileDialog dialog(this, KoFileDialog::OpenFile, "OpenColorSet");
185
    dialog.setDefaultDir(m_activeColorSet->filename());
186
    dialog.setNameFilter(i18n("Gimp Color Palette (*.gpl)"));
187
    QString fileName = dialog.url();
188 189 190 191 192 193 194
    KoColorSet *colorSet = new KoColorSet(fileName);
    colorSet->load();
    m_colorSets.append(colorSet);
    widget.selector->addItem(colorSet->name());
    widget.selector->setCurrentIndex(widget.selector->count() - 1);
}

195
void KoEditColorSetWidget::save()
196 197 198 199 200 201
{
    Q_ASSERT(m_activeColorSet);
    if (!m_activeColorSet->save())
        KMessageBox::error(0, i18n("Cannot write to palette file %1. Maybe it is read-only. ", m_activeColorSet->filename()), i18n("Palette"));
}

202
KoColorSet *KoEditColorSetWidget::activeColorSet()
203 204 205 206 207 208 209 210
{
    m_activeColorSetRequested = true;
    return m_activeColorSet;
}

KoEditColorSetDialog::KoEditColorSetDialog(const QList<KoColorSet *> &palettes, const QString &activePalette, QWidget *parent)
    : KDialog(parent)
{
211
    ui = new KoEditColorSetWidget(palettes, activePalette, this);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    setMainWidget(ui);
    setCaption(i18n("Add/Remove Colors"));
    enableButton(KDialog::Ok, ui->isEnabled());
}

KoEditColorSetDialog::~KoEditColorSetDialog()
{
    delete ui;
}

KoColorSet *KoEditColorSetDialog::activeColorSet()
{
    return ui->activeColorSet();
}

Thomas Zander's avatar
Thomas Zander committed
227
#include <KoEditColorSetDialog.moc>