KoColorSetWidget.cpp 6.66 KB
Newer Older
1
/* This file is part of the KDE project
2
   Copyright (c) 2007, 2012 C. Boemann <cbo@boemann.dk>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   Copyright (c) 2007-2008 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 "KoColorSetWidget.h"
21
#include "KoColorSetWidget_p.h"
22 23 24 25 26 27 28 29 30 31 32 33

#include <QApplication>
#include <QSize>
#include <QToolButton>
#include <QHBoxLayout>
#include <QCheckBox>
#include <QFrame>
#include <QLabel>
#include <QMouseEvent>
#include <QMenu>
#include <QWidgetAction>
#include <QDir>
34
#include <QPointer>
35
#include <QScrollArea>
36 37
#include <QGroupBox>
#include <QVBoxLayout>
38

39
#include <klocalizedstring.h>
40
#include <ksharedconfig.h>
41

42
#include <resources/KoColorSet.h>
43 44 45 46
#include <KoColorPatch.h>
#include <KoColorSpaceRegistry.h>
#include <KoResourceServer.h>
#include <KoResourceServerProvider.h>
47
#include <KoResourceServerAdapter.h>
48

49
#include <kis_palette_view.h>
50
#include <KisPaletteDelegate.h>
51
#include <KisPaletteModel.h>
52
#include <kis_icon_utils.h>
53 54 55

void KoColorSetWidget::KoColorSetWidgetPrivate::addRecent(const KoColor &color)
{
56
    if(numRecents < 6) {
57
        recentPatches[numRecents] = new KoColorPatch(thePublic);
58 59
        recentPatches[numRecents]->setFrameShape(QFrame::StyledPanel);
        recentPatches[numRecents]->setDisplayRenderer(displayRenderer);
60
        recentsLayout->insertWidget(numRecents + 1, recentPatches[numRecents]);
61
        connect(recentPatches[numRecents], SIGNAL(triggered(KoColorPatch*)), thePublic, SLOT(slotPatchTriggered(KoColorPatch*)));
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        numRecents++;
    }
    // shift colors to the right
    for (int i = numRecents- 1; i >0; i--) {
        recentPatches[i]->setColor(recentPatches[i-1]->color());
    }

    //Finally set the recent color
    recentPatches[0]->setColor(color);
}

void KoColorSetWidget::KoColorSetWidgetPrivate::activateRecent(int i)
{
    KoColor color = recentPatches[i]->color();

    while (i >0) {
        recentPatches[i]->setColor(recentPatches[i-1]->color());
        i--;
    }
    recentPatches[0]->setColor(color);
}

KoColorSetWidget::KoColorSetWidget(QWidget *parent)
   : QFrame(parent)
86
   , d(new KoColorSetWidgetPrivate())
87 88 89 90
{
    d->thePublic = this;

    d->numRecents = 0;
91
    d->recentsLayout = new QHBoxLayout;
92 93 94 95 96 97 98 99
    d->recentsLayout->setMargin(0);
    d->recentsLayout->addWidget(new QLabel(i18n("Recent:")));
    d->recentsLayout->addStretch(1);

    KoColor color(KoColorSpaceRegistry::instance()->rgb8());
    color.fromQColor(QColor(128,0,0));
    d->addRecent(color);

100 101 102
    d->paletteView = new KisPaletteView(this);
    KisPaletteModel *paletteModel = new KisPaletteModel(d->paletteView);
    d->paletteView->setPaletteModel(paletteModel);
103
    d->paletteView->setDisplayRenderer(d->displayRenderer);
104

105 106 107 108 109
    d->paletteChooser = new KisPaletteListWidget(this);
    d->paletteChooserButton = new KisPopupButton(this);
    d->paletteChooserButton->setPopupWidget(d->paletteChooser);
    d->paletteChooserButton->setIcon(KisIconUtils::loadIcon("hi16-palette_library"));
    d->paletteChooserButton->setToolTip(i18n("Choose palette"));
110

111
    d->colorNameCmb = new KisPaletteComboBox(this);
112
    d->colorNameCmb->setCompanionView(d->paletteView);
113 114 115 116 117 118

    d->bottomLayout = new QHBoxLayout;
    d->bottomLayout->addWidget(d->paletteChooserButton);
    d->bottomLayout->addWidget(d->colorNameCmb);
    d->bottomLayout->setStretch(0, 0); // minimize chooser button
    d->bottomLayout->setStretch(1, 1); // maximize color name cmb
119

120 121 122 123 124 125
    d->mainLayout = new QVBoxLayout(this);
    d->mainLayout->setMargin(4);
    d->mainLayout->setSpacing(2);
    d->mainLayout->addLayout(d->recentsLayout);
    d->mainLayout->addWidget(d->paletteView);
    d->mainLayout->addLayout(d->bottomLayout);
126 127

    setLayout(d->mainLayout);
128

129
    connect(d->paletteChooser, SIGNAL(sigPaletteSelected(KoColorSet*)),
130
            SLOT(slotPaletteChoosen(KoColorSet*)));
131 132
    connect(d->paletteView, SIGNAL(sigColorSelected(KoColor)),
            SLOT(slotColorSelectedByPalette(KoColor)));
133 134
    connect(d->colorNameCmb, SIGNAL(sigColorSelected(KoColor)),
            SLOT(slotNameListSelection(KoColor)));
135 136 137 138 139 140 141

    d->rServer = KoResourceServerProvider::instance()->paletteServer();
    QPointer<KoColorSet> defaultColorSet = d->rServer->resourceByName("Default");
    if (!defaultColorSet && d->rServer->resources().count() > 0) {
        defaultColorSet = d->rServer->resources().first();
    }
    setColorSet(defaultColorSet);
142 143 144 145 146 147 148
}

KoColorSetWidget::~KoColorSetWidget()
{
    delete d;
}

149
void KoColorSetWidget::setColorSet(QPointer<KoColorSet> colorSet)
150
{
151
    if (!colorSet) return;
152 153
    if (colorSet == d->colorSet) return;

154
    d->paletteView->paletteModel()->setPalette(colorSet.data());
155 156 157
    d->colorSet = colorSet;
}

158 159 160 161 162
KoColorSet* KoColorSetWidget::colorSet()
{
    return d->colorSet;
}

163 164 165 166 167 168 169 170 171 172 173 174
void KoColorSetWidget::setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer)
{
    if (displayRenderer) {
        d->displayRenderer = displayRenderer;
        for (int i=0; i<6; i++) {
            if (d->recentPatches[i]) {
                d->recentPatches[i]->setDisplayRenderer(displayRenderer);
            }
        }
    }
}

175 176 177 178 179 180
void KoColorSetWidget::resizeEvent(QResizeEvent *event)
{
    emit widgetSizeChanged(event->size());
    QFrame::resizeEvent(event);
}

181
void KoColorSetWidget::slotColorSelectedByPalette(const KoColor &color)
182
{
183 184
    emit colorChanged(color, true);
    d->addRecent(color);
185 186 187 188
}

void KoColorSetWidget::slotPatchTriggered(KoColorPatch *patch)
{
189
    emit colorChanged(patch->color(), true);
190 191 192 193 194 195 196 197 198 199 200 201 202

    int i;

    for (i = 0; i < d->numRecents; i++) {
        if(patch == d->recentPatches[i]) {
            d->activateRecent(i);
            break;
        }
    }

    if (i == d->numRecents) { // we didn't find it above
        d->addRecent(patch->color());
    }
203 204 205 206 207
}

void KoColorSetWidget::slotPaletteChoosen(KoColorSet *colorSet)
{
    d->colorSet = colorSet;
208
    d->paletteView->paletteModel()->setPalette(colorSet);
209 210
}

211 212 213 214 215
void KoColorSetWidget::slotNameListSelection(const KoColor &color)
{
    emit colorChanged(color, true);
}

Boudewijn Rempt's avatar
Boudewijn Rempt committed
216 217
//have to include this because of Q_PRIVATE_SLOT
#include "moc_KoColorSetWidget.cpp"