diff --git a/libs/widgets/KoColorPopupAction.cpp b/libs/widgets/KoColorPopupAction.cpp index 04cc65b684..91f0c3ce18 100644 --- a/libs/widgets/KoColorPopupAction.cpp +++ b/libs/widgets/KoColorPopupAction.cpp @@ -1,256 +1,256 @@ /* This file is part of the KDE project * Copyright (c) 2007 C. Boemann * Copyright (C) 2007 Fredy Yanardi * * 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 "KoColorPopupAction.h" #include "KoColorSetWidget.h" #include "KoTriangleColorSelector.h" #include "KoColorSlider.h" #include "KoCheckerBoardPainter.h" #include "KoResourceServer.h" #include "KoResourceServerProvider.h" #include #include #include #include #include #include #include #include #include #include - +#include class KoColorPopupAction::KoColorPopupActionPrivate { public: KoColorPopupActionPrivate() : colorSetWidget(0), colorChooser(0), opacitySlider(0), menu(0), checkerPainter(4) , showFilter(true), applyMode(true), firstTime(true) {} ~KoColorPopupActionPrivate() { delete colorSetWidget; delete colorChooser; delete opacitySlider; delete menu; } KoColor currentColor; KoColor buddyColor; KoColorSetWidget *colorSetWidget; KoTriangleColorSelector * colorChooser; KoColorSlider * opacitySlider; QMenu *menu; KoCheckerBoardPainter checkerPainter; bool showFilter; bool applyMode; bool firstTime; }; KoColorPopupAction::KoColorPopupAction(QObject *parent) : QAction(parent), d(new KoColorPopupActionPrivate()) { d->menu = new QMenu(); QWidget *widget = new QWidget(d->menu); QWidgetAction *wdgAction = new QWidgetAction(d->menu); d->colorSetWidget = new KoColorSetWidget(widget); KoResourceServer* rServer = KoResourceServerProvider::instance()->paletteServer(); - KoColorSet* defaultColorSet = rServer->resourceByName("Default"); + QPointer defaultColorSet = rServer->resourceByName("Default"); if (defaultColorSet) { d->colorSetWidget->setColorSet(defaultColorSet); } else { d->colorSetWidget->setColorSet(rServer->resources().first()); } d->colorChooser = new KoTriangleColorSelector( widget ); // prevent mouse release on color selector from closing popup d->colorChooser->setAttribute( Qt::WA_NoMousePropagation ); d->opacitySlider = new KoColorSlider( Qt::Vertical, widget ); d->opacitySlider->setFixedWidth(25); d->opacitySlider->setRange(0, 255); d->opacitySlider->setValue(255); d->opacitySlider->setToolTip( i18n( "Opacity" ) ); QGridLayout * layout = new QGridLayout( widget ); layout->addWidget( d->colorSetWidget, 0, 0, 1, -1 ); layout->addWidget( d->colorChooser, 1, 0 ); layout->addWidget( d->opacitySlider, 1, 1 ); layout->setMargin(4); wdgAction->setDefaultWidget(widget); d->menu->addAction(wdgAction); setMenu(d->menu); new QHBoxLayout(d->menu); d->menu->layout()->addWidget(widget); d->menu->layout()->setMargin(0); connect(this, SIGNAL(triggered()), this, SLOT(emitColorChanged())); connect(d->colorSetWidget, SIGNAL(colorChanged(const KoColor &, bool)), this, SLOT(colorWasSelected(const KoColor &, bool))); connect( d->colorChooser, SIGNAL( colorChanged( const QColor &) ), this, SLOT( colorWasEdited( const QColor &) ) ); connect( d->opacitySlider, SIGNAL(valueChanged(int)), this, SLOT(opacityWasChanged(int))); } KoColorPopupAction::~KoColorPopupAction() { delete d; } void KoColorPopupAction::setCurrentColor( const KoColor &color ) { KoColor minColor( color ); d->currentColor = minColor; d->colorChooser->blockSignals(true); d->colorChooser->slotSetColor(color); d->colorChooser->blockSignals(false); KoColor maxColor( color ); minColor.setOpacity( OPACITY_TRANSPARENT_U8 ); maxColor.setOpacity( OPACITY_OPAQUE_U8 ); d->opacitySlider->blockSignals( true ); d->opacitySlider->setColors( minColor, maxColor ); d->opacitySlider->setValue( color.opacityU8() ); d->opacitySlider->blockSignals( false ); updateIcon(); } void KoColorPopupAction::setCurrentColor( const QColor &_color ) { #ifndef NDEBUG if (!_color.isValid()) { warnWidgets << "Invalid color given, defaulting to black"; } #endif const QColor color(_color.isValid() ? _color : QColor(0,0,0,255)); setCurrentColor(KoColor(color, KoColorSpaceRegistry::instance()->rgb8() )); } QColor KoColorPopupAction::currentColor() const { return d->currentColor.toQColor(); } KoColor KoColorPopupAction::currentKoColor() const { return d->currentColor; } void KoColorPopupAction::updateIcon() { QSize iconSize; QToolButton *toolButton = dynamic_cast(parentWidget()); if (toolButton) { iconSize = QSize(toolButton->iconSize()); } else { iconSize = QSize(16, 16); } // This must be a QImage, as drawing to a QPixmap outside the // UI thread will cause sporadic crashes. QImage pm; if (icon().isNull()) { d->applyMode = false; } if(d->applyMode) { pm = icon().pixmap(iconSize).toImage(); if (pm.isNull()) { pm = QImage(iconSize, QImage::Format_ARGB32_Premultiplied); pm.fill(Qt::transparent); } QPainter p(&pm); p.fillRect(0, iconSize.height() - 4, iconSize.width(), 4, d->currentColor.toQColor()); p.end(); } else { pm = QImage(iconSize, QImage::Format_ARGB32_Premultiplied); pm.fill(Qt::transparent); QPainter p(&pm); d->checkerPainter.paint(p, QRect(QPoint(),iconSize)); p.fillRect(0, 0, iconSize.width(), iconSize.height(), d->currentColor.toQColor()); p.end(); } setIcon(QIcon(QPixmap::fromImage(pm))); } void KoColorPopupAction::emitColorChanged() { emit colorChanged( d->currentColor ); } void KoColorPopupAction::colorWasSelected(const KoColor &color, bool final) { d->currentColor = color; if (final) { menu()->hide(); emitColorChanged(); } updateIcon(); } void KoColorPopupAction::colorWasEdited( const QColor &color ) { d->currentColor = KoColor( color, KoColorSpaceRegistry::instance()->rgb8() ); quint8 opacity = d->opacitySlider->value(); d->currentColor.setOpacity( opacity ); KoColor minColor = d->currentColor; minColor.setOpacity( OPACITY_TRANSPARENT_U8 ); KoColor maxColor = minColor; maxColor.setOpacity( OPACITY_OPAQUE_U8 ); d->opacitySlider->setColors( minColor, maxColor ); emitColorChanged(); updateIcon(); } void KoColorPopupAction::opacityWasChanged( int opacity ) { d->currentColor.setOpacity( quint8(opacity) ); emitColorChanged(); } void KoColorPopupAction::slotTriggered(bool) { if (d->firstTime) { KoResourceServer* srv = KoResourceServerProvider::instance()->paletteServer(); QList palettes = srv->resources(); if (!palettes.empty()) { d->colorSetWidget->setColorSet(palettes.first()); } d->firstTime = false; } } diff --git a/libs/widgets/KoColorSetWidget.cpp b/libs/widgets/KoColorSetWidget.cpp index 2439ef6e1b..c3bb546a99 100644 --- a/libs/widgets/KoColorSetWidget.cpp +++ b/libs/widgets/KoColorSetWidget.cpp @@ -1,331 +1,332 @@ /* This file is part of the KDE project Copyright (c) 2007, 2012 C. Boemann Copyright (c) 2007-2008 Fredy Yanardi 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" #include "KoColorSetWidget_p.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include void KoColorSetWidget::KoColorSetWidgetPrivate::fillColors() { delete colorSetContainer; colorSetContainer = new QWidget(); colorSetLayout = new QVBoxLayout(); colorSetLayout->setMargin(3); colorSetLayout->setSpacing(0); // otherwise the use can click where there is none colorSetContainer->setBackgroundRole(QPalette::Dark); int patchSize = 12; int columns = 16; if (colorSet) { columns = colorSet->columnCount(); } colorSetContainer->setMinimumWidth(columns*patchSize+6); colorSetContainer->setLayout(colorSetLayout); patchWidgetList.clear(); colornames.clear(); colorNameCmb->clear(); QWidget *defaultGroupContainer = new QWidget(); QGridLayout *colorGroupLayout = new QGridLayout(); for(int i = 0; isetColumnMinimumWidth(i, patchSize); } defaultGroupContainer->setMinimumWidth(columns*patchSize); defaultGroupContainer->setMaximumWidth(columns*patchSize); colorGroupLayout->setSpacing(0); colorGroupLayout->setMargin(0); defaultGroupContainer->setLayout(colorGroupLayout); if (colorSet) { for( quint32 i = 0, p= 0; i < colorSet->nColorsGroup(); i++) { KoColorPatch *patch = new KoColorPatch(colorSetContainer); patch->setFrameStyle(QFrame::Plain | QFrame::Box); patch->setLineWidth(1); KoColorSetEntry c = colorSet->getColorGlobal(i); patch->setColor(c.color); patch->setToolTip(c.name); connect(patch, SIGNAL(triggered(KoColorPatch *)), thePublic, SLOT(colorTriggered(KoColorPatch *))); colorGroupLayout->addWidget(patch, p/columns, p%columns); patch->setDisplayRenderer(displayRenderer); patchWidgetList.append(patch); colornames.append(c.name); QPixmap colorsquare = QPixmap(12,12); colorsquare.fill(c.color.toQColor()); colorNameCmb->addItem(QIcon(colorsquare), c.name); ++p; } colorSetLayout->addWidget(defaultGroupContainer); Q_FOREACH(QString groupName, colorSet->getGroupNames()) { QGroupBox *groupbox = new QGroupBox(); groupbox->setTitle(groupName); QGridLayout *groupLayout = new QGridLayout(); for(int i = 0; isetColumnMinimumWidth(i, patchSize); } groupLayout->setSpacing(0); groupLayout->setMargin(0); groupbox->setMinimumWidth(columns*patchSize); groupbox->setMaximumWidth(columns*patchSize); groupbox->setLayout(groupLayout); groupbox->setFlat(true); for( quint32 i = 0, p= 0; i < colorSet->nColorsGroup(groupName); i++) { KoColorPatch *patch = new KoColorPatch(colorSetContainer); patch->setFrameStyle(QFrame::Plain | QFrame::Box); patch->setLineWidth(1); KoColorSetEntry c = colorSet->getColorGroup(i, groupName); patch->setColor(c.color); patch->setToolTip(c.name); connect(patch, SIGNAL(triggered(KoColorPatch *)), thePublic, SLOT(colorTriggered(KoColorPatch *))); groupLayout->addWidget(patch, p/columns, p%columns); patch->setDisplayRenderer(displayRenderer); patchWidgetList.append(patch); colornames.append(c.name); QPixmap colorsquare = QPixmap(12,12); colorsquare.fill(c.color.toQColor()); colorNameCmb->addItem(QIcon(colorsquare), c.name); ++p; } colorSetLayout->addWidget(groupbox); } } scrollArea->setWidget(colorSetContainer); connect(colorNameCmb, SIGNAL(activated(QString)), thePublic, SLOT(setColorFromString(QString)), Qt::UniqueConnection); } void KoColorSetWidget::KoColorSetWidgetPrivate::addRemoveColors() { KoResourceServer* srv = KoResourceServerProvider::instance()->paletteServer(); QList palettes = srv->resources(); Q_ASSERT(colorSet); KoEditColorSetDialog *dlg = new KoEditColorSetDialog(palettes, colorSet->name(), thePublic); if (dlg->exec() == KoDialog::Accepted ) { // always reload the color set KoColorSet * cs = dlg->activeColorSet(); // check if the selected colorset is predefined if( cs && !palettes.contains( cs ) ) { int i = 1; QFileInfo fileInfo; QString savePath = srv->saveLocation(); do { fileInfo.setFile(savePath + QString("%1.%2").arg(i++, 4, 10, QChar('0')).arg(colorSet->defaultFileExtension())); } while (fileInfo.exists()); cs->setFilename( fileInfo.filePath() ); cs->setValid( true ); // add new colorset to predefined colorsets if (!srv->addResource(cs)) { delete cs; cs = 0; } } if (cs) { thePublic->setColorSet(cs); } } delete dlg; } void KoColorSetWidget::KoColorSetWidgetPrivate::addRecent(const KoColor &color) { if(numRecents<6) { recentPatches[numRecents] = new KoColorPatch(thePublic); recentPatches[numRecents]->setFrameShape(QFrame::StyledPanel); recentPatches[numRecents]->setDisplayRenderer(displayRenderer); recentsLayout->insertWidget(numRecents+1, recentPatches[numRecents]); connect(recentPatches[numRecents], SIGNAL(triggered(KoColorPatch *)), thePublic, SLOT(colorTriggered(KoColorPatch *))); 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) ,d(new KoColorSetWidgetPrivate()) { d->thePublic = this; d->colorSet = 0; d->firstShowOfContainer = true; d->mainLayout = new QVBoxLayout(); d->mainLayout->setMargin(4); d->mainLayout->setSpacing(2); d->colorSetContainer = 0; d->numRecents = 0; d->recentsLayout = new QHBoxLayout(); d->mainLayout->addLayout(d->recentsLayout); 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); d->scrollArea = new QScrollArea(); d->scrollArea->setBackgroundRole(QPalette::Dark); d->mainLayout->addWidget(d->scrollArea); d->colorNameCmb = new QComboBox(this); d->colorNameCmb->setEditable(true); d->colorNameCmb->setInsertPolicy(QComboBox::NoInsert); d->mainLayout->addWidget(d->colorNameCmb); d->fillColors(); d->addRemoveButton = new QToolButton(this); d->addRemoveButton->setText(i18n("Add / Remove Colors...")); d->addRemoveButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); connect(d->addRemoveButton, SIGNAL(clicked()), SLOT(addRemoveColors())); d->mainLayout->addWidget(d->addRemoveButton); setLayout(d->mainLayout); KoColorSet *colorSet = new KoColorSet(); d->colorSet = colorSet; d->fillColors(); } KoColorSetWidget::~KoColorSetWidget() { KoResourceServer* srv = KoResourceServerProvider::instance()->paletteServer(); QList palettes = srv->resources(); if (!palettes.contains(d->colorSet)) { delete d->colorSet; } delete d; } void KoColorSetWidget::KoColorSetWidgetPrivate::colorTriggered(KoColorPatch *patch) { int i; emit thePublic->colorChanged(patch->color(), true); colorNameCmb->setCurrentIndex(colornames.indexOf(QRegExp(patch->toolTip()+"|Fixed"))); for(i = 0; i color()); } void KoColorSetWidget::KoColorSetWidgetPrivate::setColorFromString(QString s) { int i = colornames.indexOf(QRegExp(s+"|Fixed")); i = qMax(i,0); colorTriggered(patchWidgetList.at(i)); } -void KoColorSetWidget::setColorSet(KoColorSet *colorSet) +void KoColorSetWidget::setColorSet(QPointer colorSet) { + if (!colorSet) return; if (colorSet == d->colorSet) return; KoResourceServer* srv = KoResourceServerProvider::instance()->paletteServer(); QList palettes = srv->resources(); if (!palettes.contains(d->colorSet)) { delete d->colorSet; } d->colorSet = colorSet; d->fillColors(); } KoColorSet* KoColorSetWidget::colorSet() { return d->colorSet; } void KoColorSetWidget::setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer) { if (displayRenderer) { d->displayRenderer = displayRenderer; Q_FOREACH(KoColorPatch *p, d->patchWidgetList) { p->setDisplayRenderer(displayRenderer); } for (int i=0; i<6; i++) { if (d->recentPatches[i]) { d->recentPatches[i]->setDisplayRenderer(displayRenderer); } } } } void KoColorSetWidget::resizeEvent(QResizeEvent *event) { emit widgetSizeChanged(event->size()); QFrame::resizeEvent(event); } //have to include this because of Q_PRIVATE_SLOT #include "moc_KoColorSetWidget.cpp" diff --git a/libs/widgets/KoColorSetWidget.h b/libs/widgets/KoColorSetWidget.h index da8b82b05f..a9fc3fe9ad 100644 --- a/libs/widgets/KoColorSetWidget.h +++ b/libs/widgets/KoColorSetWidget.h @@ -1,107 +1,107 @@ /* This file is part of the KDE project Copyright (c) 2007 C. Boemann Copyright (c) 2007 Fredy Yanardi 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. */ #ifndef KOCOLORSETWIDGET_H_ #define KOCOLORSETWIDGET_H_ #include #include #include "kritawidgets_export.h" #include class KoColor; class KoColorSet; /** * @short A colormanaged widget for choosing a color from a colorset * * KoColorSetWidget is a widget for choosing a color (colormanaged via pigment). It shows a color * set plus optionally a checkbox to filter away bad matching colors. */ class KRITAWIDGETS_EXPORT KoColorSetWidget : public QFrame { Q_OBJECT public: /** * Constructor for the widget, where color is initially blackpoint of sRGB * * @param parent parent QWidget */ explicit KoColorSetWidget(QWidget *parent=0); /** * Destructor */ ~KoColorSetWidget() override; /** * Sets the color set that this widget shows. * @param colorSet pointer to the color set */ - void setColorSet(KoColorSet *colorSet); - + void setColorSet(QPointer colorSet); + /** * @brief setDisplayRenderer * Set the display renderer of this object. * @param displayRenderer */ void setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer); /** * Gets the current color set * @returns current color set,, 0 if none set */ KoColorSet* colorSet(); protected: void resizeEvent(QResizeEvent *event) override; ///< reimplemented from QFrame Q_SIGNALS: /** * Emitted every time the color changes (by calling setColor() or * by user interaction. * @param color the new color * @param final if the value is final (ie not produced by the pointer moving over around) */ void colorChanged(const KoColor &color, bool final); /** * Emitted every time the size of this widget changes because of new colorset with * different number of colors is loaded. This is useful for KoColorSetAction to update * correct size of the menu showing this widget. * @param size the new size */ void widgetSizeChanged(const QSize &size); private: Q_PRIVATE_SLOT(d, void colorTriggered(KoColorPatch *)) Q_PRIVATE_SLOT(d, void addRemoveColors()) Q_PRIVATE_SLOT(d, void setColorFromString(QString s)) class KoColorSetWidgetPrivate; KoColorSetWidgetPrivate * const d; }; #endif