• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • krita API Reference
  • KDE Home
  • Contact Us
 

KritaWidgets

  • sources
  • kfour-appscomplete
  • krita
  • libs
  • widgets
KoColorPopupAction.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  * SPDX-FileCopyrightText: 2007 C. Boemann <[email protected]>
3  * SPDX-FileCopyrightText: 2007 Fredy Yanardi <[email protected]>
4  *
5  * SPDX-License-Identifier: LGPL-2.0-or-later
6  */
7 
8 #include "KoColorPopupAction.h"
9 
10 #include "KoColorSetWidget.h"
11 #include "KoTriangleColorSelector.h"
12 #include "KoColorSlider.h"
13 #include "KoCheckerBoardPainter.h"
14 #include "KoResourceServer.h"
15 #include "KoResourceServerProvider.h"
16 #include <KoColorSpaceRegistry.h>
17 #include <KoColor.h>
18 
19 #include <WidgetsDebug.h>
20 #include <klocalizedstring.h>
21 
22 #include <QPainter>
23 #include <QWidgetAction>
24 #include <QMenu>
25 #include <QHBoxLayout>
26 #include <QGridLayout>
27 #include <QToolButton>
28 
29 class KoColorPopupAction::KoColorPopupActionPrivate
30 {
31 public:
32  KoColorPopupActionPrivate()
33  : colorSetWidget(0)
34  , colorChooser(0)
35  , opacitySlider(0)
36  , menu(0)
37  , checkerPainter(4)
38  , showFilter(true)
39  , applyMode(true)
40  , firstTime(true)
41  {}
42 
43  ~KoColorPopupActionPrivate()
44  {
45  delete menu;
46  }
47 
48  KoColor currentColor;
49  KoColor buddyColor;
50 
51  KoColorSetWidget *colorSetWidget;
52  KoTriangleColorSelector * colorChooser;
53  KoColorSlider * opacitySlider;
54  QMenu *menu;
55  KoCheckerBoardPainter checkerPainter;
56  bool showFilter;
57  bool applyMode;
58 
59  bool firstTime;
60 };
61 
62 KoColorPopupAction::KoColorPopupAction(QObject *parent)
63  : QAction(parent),
64  d(new KoColorPopupActionPrivate())
65 {
66  d->menu = new QMenu();
67  QWidget *widget = new QWidget(d->menu);
68  QWidgetAction *wdgAction = new QWidgetAction(d->menu);
69  d->colorSetWidget = new KoColorSetWidget(widget);
70  KoResourceServer<KoColorSet>* rServer = KoResourceServerProvider::instance()->paletteServer();
71 
72  KoColorSetSP defaultColorSet = rServer->resourceByName("Default");
73  if (!defaultColorSet && rServer->resourceCount() > 0) {
74  defaultColorSet = rServer->firstResource();
75  }
76  d->colorSetWidget->setColorSet(defaultColorSet);
77 
78  d->colorChooser = new KoTriangleColorSelector( widget );
79  // prevent mouse release on color selector from closing popup
80  d->colorChooser->setAttribute( Qt::WA_NoMousePropagation );
81  d->opacitySlider = new KoColorSlider( Qt::Vertical, widget );
82  d->opacitySlider->setFixedWidth(25);
83  d->opacitySlider->setRange(0, 255);
84  d->opacitySlider->setValue(255);
85  d->opacitySlider->setToolTip( i18n( "Opacity" ) );
86 
87  QGridLayout * layout = new QGridLayout( widget );
88  layout->addWidget( d->colorSetWidget, 0, 0, 1, -1 );
89  layout->addWidget( d->colorChooser, 1, 0 );
90  layout->addWidget( d->opacitySlider, 1, 1 );
91  layout->setMargin(4);
92 
93  wdgAction->setDefaultWidget(widget);
94  d->menu->addAction(wdgAction);
95  setMenu(d->menu);
96  new QHBoxLayout(d->menu);
97  d->menu->layout()->addWidget(widget);
98  d->menu->layout()->setMargin(0);
99 
100  connect(this, SIGNAL(triggered()), this, SLOT(emitColorChanged()));
101 
102  connect(d->colorSetWidget, SIGNAL(colorChanged(KoColor,bool)),
103  this, SLOT(colorWasSelected(KoColor,bool)));
104  connect(d->colorChooser, SIGNAL(colorChanged(QColor)),
105  this, SLOT(colorWasEdited(QColor)));
106  connect(d->opacitySlider, SIGNAL(valueChanged(int)),
107  this, SLOT(opacityWasChanged(int)));
108 }
109 
110 KoColorPopupAction::~KoColorPopupAction()
111 {
112  delete d;
113 }
114 
115 void KoColorPopupAction::setCurrentColor( const KoColor &color )
116 {
117  KoColor minColor( color );
118  d->currentColor = minColor;
119 
120  d->colorChooser->blockSignals(true);
121  d->colorChooser->slotSetColor(color);
122  d->colorChooser->blockSignals(false);
123 
124  KoColor maxColor( color );
125  minColor.setOpacity( OPACITY_TRANSPARENT_U8 );
126  maxColor.setOpacity( OPACITY_OPAQUE_U8 );
127 
128  d->opacitySlider->blockSignals( true );
129  d->opacitySlider->setColors( minColor, maxColor );
130  d->opacitySlider->setValue( color.opacityU8() );
131  d->opacitySlider->blockSignals( false );
132 
133  updateIcon();
134 }
135 
136 void KoColorPopupAction::setCurrentColor( const QColor &_color )
137 {
138 #ifndef NDEBUG
139  if (!_color.isValid()) {
140  warnWidgets << "Invalid color given, defaulting to black";
141  }
142 #endif
143  const QColor color(_color.isValid() ? _color : QColor(0,0,0,255));
144  setCurrentColor(KoColor(color, KoColorSpaceRegistry::instance()->rgb8() ));
145 }
146 
147 QColor KoColorPopupAction::currentColor() const
148 {
149  return d->currentColor.toQColor();
150 }
151 
152 KoColor KoColorPopupAction::currentKoColor() const
153 {
154  return d->currentColor;
155 }
156 
157 void KoColorPopupAction::updateIcon()
158 {
159  QSize iconSize;
160  QToolButton *toolButton = dynamic_cast<QToolButton*>(parentWidget());
161  if (toolButton) {
162  iconSize = QSize(toolButton->iconSize());
163  } else {
164  iconSize = QSize(16, 16);
165  }
166 
167  // This must be a QImage, as drawing to a QPixmap outside the
168  // UI thread will cause sporadic crashes.
169  QImage pm;
170 
171  if (icon().isNull()) {
172  d->applyMode = false;
173  }
174 
175  if(d->applyMode) {
176  pm = icon().pixmap(iconSize).toImage();
177  if (pm.isNull()) {
178  pm = QImage(iconSize, QImage::Format_ARGB32_Premultiplied);
179  pm.fill(Qt::transparent);
180  }
181  QPainter p(&pm);
182  p.fillRect(0, iconSize.height() - 4, iconSize.width(), 4, d->currentColor.toQColor());
183  p.end();
184  } else {
185  pm = QImage(iconSize, QImage::Format_ARGB32_Premultiplied);
186  pm.fill(Qt::transparent);
187  QPainter p(&pm);
188  d->checkerPainter.paint(p, QRect(QPoint(),iconSize));
189  p.fillRect(0, 0, iconSize.width(), iconSize.height(), d->currentColor.toQColor());
190  p.end();
191  }
192  setIcon(QIcon(QPixmap::fromImage(pm)));
193 }
194 
195 void KoColorPopupAction::emitColorChanged()
196 {
197  emit colorChanged( d->currentColor );
198 }
199 
200 void KoColorPopupAction::colorWasSelected(const KoColor &color, bool final)
201 {
202  d->currentColor = color;
203  if (final) {
204  menu()->hide();
205  emitColorChanged();
206  }
207  updateIcon();
208 }
209 
210 void KoColorPopupAction::colorWasEdited( const QColor &color )
211 {
212  d->currentColor = KoColor( color, KoColorSpaceRegistry::instance()->rgb8() );
213  quint8 opacity = d->opacitySlider->value();
214  d->currentColor.setOpacity( opacity );
215 
216  KoColor minColor = d->currentColor;
217  minColor.setOpacity( OPACITY_TRANSPARENT_U8 );
218  KoColor maxColor = minColor;
219  maxColor.setOpacity( OPACITY_OPAQUE_U8 );
220 
221  d->opacitySlider->setColors( minColor, maxColor );
222 
223  emitColorChanged();
224 
225  updateIcon();
226 }
227 
228 void KoColorPopupAction::opacityWasChanged( int opacity )
229 {
230  d->currentColor.setOpacity( quint8(opacity) );
231 
232  emitColorChanged();
233 }
QColor
KoTriangleColorSelector.h
KoColorPopupAction::currentColor
QColor currentColor() const
Returns the current color.
Definition: KoColorPopupAction.cpp:147
QGridLayout
QPixmap::fromImage
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QImage::fill
void fill(uint pixelValue)
QRect
KoColorPopupAction::KoColorPopupAction
KoColorPopupAction(QObject *parent=0)
Constructs a KoColorPopupAction with the specified parent.
Definition: KoColorPopupAction.cpp:62
KoColorPopupAction::colorChanged
void colorChanged(const KoColor &color)
Emitted every time the color changes (by calling setColor() or by user interaction.
QAction::setMenu
void setMenu(QMenu *menu)
QGridLayout::addWidget
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
QLayout::setMargin
void setMargin(int margin)
QAction::parentWidget
QWidget * parentWidget() const
QWidget
KoColorSlider.h
QMenu
KoColorPopupAction::~KoColorPopupAction
~KoColorPopupAction() override
Destructor.
Definition: KoColorPopupAction.cpp:110
QSize
QSize::width
int width() const
QWidgetAction
QPainter
QPainter::fillRect
void fillRect(const QRectF &rectangle, const QBrush &brush)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QWidgetAction::setDefaultWidget
void setDefaultWidget(QWidget *widget)
QWidget::hide
void hide()
QAction::menu
QMenu * menu() const
QAction::icon
QIcon icon() const
QPainter::end
bool end()
KoColorPopupAction::updateIcon
void updateIcon()
update the icon - only needed if you resize the iconsize in the widget that shows the action
Definition: KoColorPopupAction.cpp:157
KoColorSetWidget.h
QObject
QSize::height
int height() const
KoResourceServerProvider.h
QToolButton
QImage::isNull
bool isNull() const
KoColorPopupAction::currentKoColor
KoColor currentKoColor() const
Returns the current color as a KoColor.
Definition: KoColorPopupAction.cpp:152
KoColorPopupAction::setCurrentColor
void setCurrentColor(const QColor &color)
Sets a new color to be displayed.
Definition: KoColorPopupAction.cpp:136
QIcon
KoColorSetWidget
A colormanaged widget for choosing a color from a colorset.
Definition: KoColorSetWidget.h:30
QPixmap::toImage
QImage toImage() const
warnWidgets
#define warnWidgets
Definition: WidgetsDebug.h:16
QAbstractButton::iconSize
iconSize
QAction::triggered
void triggered(bool checked)
QIcon::pixmap
QPixmap pixmap(const QSize &size, Mode mode, State state) const
QHBoxLayout
WidgetsDebug.h
QAction
QColor::isValid
bool isValid() const
KoColorPopupAction.h
QImage
QPoint
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Jan 19 2021 23:44:00 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KritaWidgets

Skip menu "KritaWidgets"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

krita API Reference

Skip menu "krita API Reference"
  • libs
  •   KritaBasicFlakes
  •   brush
  •   KritaUndo2
  •   KritaFlake
  •   image
  •   KritaPlugin
  •   Krita
  •   KritaPigment
  •   KritaResources
  •   KritaStore
  •   ui
  •   KritaWidgets
  •   KritaWidgetUtils
  • plugins
  •   Assitants
  •   Extensions
  •   Filters
  •   Generators
  •   Formats
  •           src
  •   PaintOps
  •     libpaintop

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal