Warning, file /office/calligra/libs/widgets/KoColorSetWidget.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, 2012 C. Boemann <cbo@boemann.dk>
0003    Copyright (c) 2007-2008 Fredy Yanardi <fyanardi@gmail.com>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 #include "KoColorSetWidget.h"
0021 #include "KoColorSetWidget_p.h"
0022 
0023 #include <QTimer>
0024 #include <QApplication>
0025 #include <QSize>
0026 #include <QToolButton>
0027 #include <QHBoxLayout>
0028 #include <QCheckBox>
0029 #include <QFrame>
0030 #include <QLabel>
0031 #include <QMouseEvent>
0032 #include <QMenu>
0033 #include <QWidgetAction>
0034 #include <QDir>
0035 #include <QPointer>
0036 #include <QScrollArea>
0037 
0038 #include <klocalizedstring.h>
0039 #include <ksharedconfig.h>
0040 
0041 #include <KoColorSet.h>
0042 #include <KoColorPatch.h>
0043 #include <KoEditColorSetDialog.h>
0044 #include <KoColorSpaceRegistry.h>
0045 #include <KoResourceServer.h>
0046 #include <KoResourceServerProvider.h>
0047 
0048 void KoColorSetWidget::KoColorSetWidgetPrivate::fillColors()
0049 {
0050     delete colorSetContainer;
0051     colorSetContainer = new QWidget();
0052     colorSetLayout = new QGridLayout();
0053     colorSetLayout->setMargin(3);
0054     colorSetLayout->setSpacing(0); // otherwise the use can click where there is none
0055     colorSetContainer->setBackgroundRole(QPalette::Dark);
0056     for(int i = 0; i<16; i++) {
0057         colorSetLayout->setColumnMinimumWidth(i, 12);
0058     }
0059     colorSetContainer->setLayout(colorSetLayout);
0060 
0061     if (colorSet) {
0062         for( int i = 0, p= 0; i < colorSet->nColors(); i++) {
0063             KoColorPatch *patch = new KoColorPatch(colorSetContainer);
0064             patch->setFrameStyle(QFrame::Plain | QFrame::Box);
0065             patch->setLineWidth(1);
0066             patch->setColor(colorSet->getColor(i).color);
0067             connect(patch, SIGNAL(triggered(KoColorPatch*)), thePublic, SLOT(colorTriggered(KoColorPatch*)));
0068             colorSetLayout->addWidget(patch, p/16, p%16);
0069             ++p;
0070         }
0071     }
0072 
0073     scrollArea->setWidget(colorSetContainer);
0074 }
0075 
0076 void KoColorSetWidget::KoColorSetWidgetPrivate::addRemoveColors()
0077 {
0078     KoResourceServer<KoColorSet>* srv = KoResourceServerProvider::instance()->paletteServer();
0079     QList<KoColorSet*> palettes = srv->resources();
0080 
0081     Q_ASSERT(colorSet);
0082     KoEditColorSetDialog *dlg = new KoEditColorSetDialog(palettes, colorSet->name(), thePublic);
0083     if (dlg->exec() == KoDialog::Accepted ) { // always reload the color set
0084         KoColorSet * cs = dlg->activeColorSet();
0085         // check if the selected colorset is predefined
0086         if( cs && !palettes.contains( cs ) ) {
0087             int i = 1;
0088             QFileInfo fileInfo;
0089             QString savePath = srv->saveLocation();
0090 
0091             do {
0092                 fileInfo.setFile( savePath + QString("%1.gpl").arg( i++, 4, 10, QChar('0') ) );
0093             }
0094             while (fileInfo.exists());
0095 
0096             cs->setFilename( fileInfo.filePath() );
0097             cs->setValid( true );
0098 
0099             // add new colorset to predefined colorsets
0100             if (!srv->addResource(cs)) {
0101 
0102                 delete cs;
0103                 cs = 0;
0104             }
0105         }
0106         if (cs) {
0107             thePublic->setColorSet(cs);
0108         }
0109     }
0110     delete dlg;
0111 }
0112 
0113 void KoColorSetWidget::KoColorSetWidgetPrivate::addRecent(const KoColor &color)
0114 {
0115     if(numRecents<6) {
0116         recentPatches[numRecents] = new KoColorPatch(thePublic);
0117         recentPatches[numRecents]->setFrameShape(QFrame::Box);
0118         recentsLayout->insertWidget(numRecents+1, recentPatches[numRecents]);
0119         connect(recentPatches[numRecents], SIGNAL(triggered(KoColorPatch*)), thePublic, SLOT(colorTriggered(KoColorPatch*)));
0120         numRecents++;
0121     }
0122     // shift colors to the right
0123     for (int i = numRecents- 1; i >0; i--) {
0124         recentPatches[i]->setColor(recentPatches[i-1]->color());
0125     }
0126 
0127     //Finally set the recent color
0128     recentPatches[0]->setColor(color);
0129 }
0130 
0131 void KoColorSetWidget::KoColorSetWidgetPrivate::activateRecent(int i)
0132 {
0133     KoColor color = recentPatches[i]->color();
0134 
0135     while (i >0) {
0136         recentPatches[i]->setColor(recentPatches[i-1]->color());
0137         i--;
0138     }
0139     recentPatches[0]->setColor(color);
0140 }
0141 
0142 KoColorSetWidget::KoColorSetWidget(QWidget *parent)
0143    : QFrame(parent)
0144     ,d(new KoColorSetWidgetPrivate())
0145 {
0146     d->thePublic = this;
0147     d->colorSet = 0;
0148 
0149     d->firstShowOfContainer = true;
0150 
0151     d->mainLayout = new QVBoxLayout();
0152     d->mainLayout->setMargin(4);
0153     d->mainLayout->setSpacing(2);
0154 
0155     d->colorSetContainer = 0;
0156 
0157     d->numRecents = 0;
0158     d->recentsLayout = new QHBoxLayout();
0159     d->mainLayout->addLayout(d->recentsLayout);
0160     d->recentsLayout->setMargin(0);
0161     d->recentsLayout->addWidget(new QLabel(i18n("Recent:")));
0162     d->recentsLayout->addStretch(1);
0163 
0164     KoColor color(KoColorSpaceRegistry::instance()->rgb8());
0165     color.fromQColor(QColor(128,0,0));
0166     d->addRecent(color);
0167 
0168     d->scrollArea = new QScrollArea();
0169     d->scrollArea->setBackgroundRole(QPalette::Dark);
0170     d->mainLayout->addWidget(d->scrollArea);
0171     d->fillColors();
0172 
0173     d->addRemoveButton = new QToolButton(this);
0174     d->addRemoveButton->setText(i18n("Add / Remove Colors..."));
0175     d->addRemoveButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
0176     connect(d->addRemoveButton, SIGNAL(clicked()), SLOT(addRemoveColors()));
0177     d->mainLayout->addWidget(d->addRemoveButton);
0178 
0179     setLayout(d->mainLayout);
0180 
0181     KoColorSet *colorSet = new KoColorSet();
0182     d->colorSet = colorSet;
0183     d->fillColors();
0184 }
0185 
0186 KoColorSetWidget::~KoColorSetWidget()
0187 {
0188     KoResourceServer<KoColorSet>* srv = KoResourceServerProvider::instance()->paletteServer();
0189     QList<KoColorSet*> palettes = srv->resources();
0190     if (!palettes.contains(d->colorSet)) {
0191         delete d->colorSet;
0192     }
0193     delete d;
0194 }
0195 
0196 void KoColorSetWidget::KoColorSetWidgetPrivate::colorTriggered(KoColorPatch *patch)
0197 {
0198     int i;
0199 
0200     emit thePublic->colorChanged(patch->color(), true);
0201 
0202     for(i = 0; i <numRecents; i++)
0203         if(patch == recentPatches[i]) {
0204             activateRecent(i);
0205             break;
0206         }
0207 
0208     if(i == numRecents) // we didn't find it above
0209         addRecent(patch->color());
0210 }
0211 
0212 void KoColorSetWidget::setColorSet(KoColorSet *colorSet)
0213 {
0214     if (colorSet == d->colorSet) return;
0215 
0216     KoResourceServer<KoColorSet>* srv = KoResourceServerProvider::instance()->paletteServer();
0217     QList<KoColorSet*> palettes = srv->resources();
0218     if (!palettes.contains(d->colorSet)) {
0219         delete d->colorSet;
0220     }
0221 
0222     d->colorSet = colorSet;
0223     d->fillColors();
0224 }
0225 
0226 KoColorSet* KoColorSetWidget::colorSet()
0227 {
0228     return d->colorSet;
0229 }
0230 
0231 void KoColorSetWidget::resizeEvent(QResizeEvent *event)
0232 {
0233     emit widgetSizeChanged(event->size());
0234     QFrame::resizeEvent(event);
0235 }
0236 
0237 //have to include this because of Q_PRIVATE_SLOT
0238 #include "moc_KoColorSetWidget.cpp"
0239