KDELibs4Support

kcolordialog.h
1 /* This file is part of the KDE libraries
2  Copyright (C) 1997 Martin Jones ([email protected])
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 //----------------------------------------------------------------------
20 // KDE color selection dialog.
21 
22 // layout management added Oct 1997 by Mario Weilguni
24 
25 #ifndef KCOLORDIALOG_H
26 #define KCOLORDIALOG_H
27 
28 #include <kdialog.h>
29 #include <QPixmap>
30 #include <QScrollArea>
31 #include <QTableWidget>
32 #include <kcolorchoosermode.h>
33 
34 #include <kdelibs4support_export.h>
35 
36 /**
37 * A table of editable color cells.
38 *
39 * @author Martin Jones <[email protected]>
40 */
41 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KColorCells : public QTableWidget
42 {
43  Q_OBJECT
44  Q_PROPERTY(bool acceptDrags READ acceptDrags WRITE setAcceptDrags)
45  Q_PROPERTY(bool shading READ shading WRITE setShading)
46 
47 public:
48  /**
49  * Constructs a new table of color cells, consisting of
50  * @p rows * @p columns colors.
51  *
52  * @param parent The parent of the new widget
53  * @param rows The number of rows in the table
54  * @param columns The number of columns in the table
55  */
56  KColorCells(QWidget *parent, int rows, int columns);
57  ~KColorCells() override;
58 
59  /** Sets the color in the given index in the table */
60  void setColor(int index, const QColor &col);
61  /** Returns the color at a given index in the table */
62  QColor color(int index) const;
63  /** Returns the total number of color cells in the table */
64  int count() const;
65 
66  void setShading(bool shade);
67  bool shading() const;
68 
69  void setAcceptDrags(bool acceptDrags);
70  bool acceptDrags() const;
71 
72  /** Sets the currently selected cell to @p index */
73  void setSelected(int index);
74  /** Returns the index of the cell which is currently selected */
75  int selectedIndex() const;
76 
77 Q_SIGNALS:
78  /** Emitted when a color is selected in the table */
79  void colorSelected(int index, const QColor &color);
80  /** Emitted when a color in the table is double-clicked */
81  void colorDoubleClicked(int index, const QColor &color);
82 
83 protected:
84  // the three methods below are used to ensure equal column widths and row heights
85  // for all cells and to update the widths/heights when the widget is resized
86  int sizeHintForColumn(int column) const override;
87  int sizeHintForRow(int column) const override;
88  void resizeEvent(QResizeEvent *event) override;
89 
90  void mouseReleaseEvent(QMouseEvent *) override;
91  void mousePressEvent(QMouseEvent *) override;
92  void mouseMoveEvent(QMouseEvent *) override;
93  void dragEnterEvent(QDragEnterEvent *) override;
94  void dragMoveEvent(QDragMoveEvent *) override;
95  void dropEvent(QDropEvent *) override;
96  void mouseDoubleClickEvent(QMouseEvent *) override;
97 
98  int positionToCell(const QPoint &pos, bool ignoreBorders = false) const;
99 
100 private:
101  class KColorCellsPrivate;
102  friend class KColorCellsPrivate;
103  KColorCellsPrivate *const d;
104 
106 };
107 
108 /**
109  * @short A color displayer.
110  *
111  * The KColorPatch widget is a (usually small) widget showing
112  * a selected color e.g. in the KColorDialog. It
113  * automatically handles drag and drop from and on the widget.
114  *
115  * \image html kcolorpatch.png "KDE Color Patch"
116  */
117 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KColorPatch : public QFrame
118 {
119  Q_OBJECT
120  Q_PROPERTY(QColor color READ color WRITE setColor)
121 
122 public:
123  KColorPatch(QWidget *parent);
124  ~KColorPatch() override;
125 
126  /**
127  * Get the currently displayed color
128  */
129  QColor color() const;
130 
131  /**
132  * Set the color to display and update the display
133  *
134  * @param col color to display
135  */
136  void setColor(const QColor &col);
137 
138 Q_SIGNALS:
139  /**
140  * This signal is emitted whenever the current color
141  * changes due to a drop event
142  */
143  void colorChanged(const QColor &);
144 
145 protected:
146  void paintEvent(QPaintEvent *pe) override;
147  void mouseMoveEvent(QMouseEvent *) override;
148  void dragEnterEvent(QDragEnterEvent *) override;
149  void dropEvent(QDropEvent *) override;
150 
151 private:
152  class KColorPatchPrivate;
153  KColorPatchPrivate *const d;
154 
156 };
157 
158 /**
159  * @short A color selection dialog.
160  *
161  * <b>Features:</b>\n
162  *
163  * @li Color selection from a wide range of palettes.
164  * @li Color selection from a palette of H vs S and V selectors.
165  * @li Direct input of HSV or RGB values.
166  * @li Saving of custom colors
167  *
168  * In most cases, you will want to use the static method KColorDialog::getColor().
169  * This pops up the dialog (with an initial selection provided by you), lets the
170  * user choose a color, and returns.
171  *
172  * Example:
173  *
174  * \code
175  * QColor myColor;
176  * int result = KColorDialog::getColor( myColor );
177  * if ( result == KColorDialog::Accepted )
178  * ...
179  * \endcode
180  *
181  * To react to the color selection as it is being selected, the colorSelected() signal
182  * can be used. This can be used still in a modal way, for example:
183  *
184  * \code
185  * KColorDialog dialog(this);
186  * connect(&dialog, SIGNAL(colorSelected(const QColor &)), this, SLOT(temporarilyChangeColor(const QColor &)));
187  * QColor myColor;
188  * dialog.setColor(myColor);
189  * int result = dialog.exec();
190  *
191  * if ( result == KColorDialog::Accepted )
192  * changeColor( dialog.color() );
193  * else
194  * temporarilyChangeColor(myColor); //change back to original color
195  * \endcode
196  *
197  *
198  * @image html kcolordialog.png "KDE Color Dialog"
199  *
200  * The color dialog is really a collection of several widgets which can
201  * you can also use separately: the quadratic plane in the top left of
202  * the dialog is a KXYSelector. Right next to it is a KHSSelector
203  * for choosing hue/saturation.
204  *
205  * On the right side of the dialog you see a KColorTable showing
206  * a number of colors with a combo box which offers several predefined
207  * palettes or a palette configured by the user. The small field showing
208  * the currently selected color is a KColorPatch.
209  *
210  **/
211 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KColorDialog : public KDialog
212 {
213  Q_OBJECT
214  Q_PROPERTY(bool isAlphaChannelEnabled READ isAlphaChannelEnabled WRITE setAlphaChannelEnabled)
215  Q_PROPERTY(QColor defaultColor READ defaultColor WRITE setDefaultColor)
216  Q_PROPERTY(QColor color READ color WRITE setColor)
217 
218 public:
219  /**
220  * Constructs a color selection dialog.
221  */
222  KDELIBS4SUPPORT_DEPRECATED explicit KColorDialog(QWidget *parent = nullptr, bool modal = false);
223  /**
224  * Destroys the color selection dialog.
225  */
226  ~KColorDialog() override;
227 
228  /**
229  * Returns the currently selected color.
230  **/
231  QColor color() const;
232 
233  /**
234  * Creates a modal color dialog, let the user choose a
235  * color, and returns when the dialog is closed.
236  *
237  * The selected color is returned in the argument @p theColor.
238  *
239  * @param theColor if valid, specifies the color to be initially selected. On
240  * return, holds the selected color.
241  * @returns QDialog::result().
242  */
243  static int getColor(QColor &theColor, QWidget *parent = nullptr);
244 
245  /**
246  * Creates a modal color dialog, lets the user choose a
247  * color, and returns when the dialog is closed.
248  *
249  * The selected color is returned in the argument @p theColor.
250  *
251  * This version takes a @p defaultColor argument, which sets the color
252  * selected by the "default color" checkbox. When this checkbox is checked,
253  * the invalid color (QColor()) is returned into @p theColor.
254  *
255  * @param theColor if valid, specifies the color to be initially selected. On
256  * return, holds the selected color.
257  * @param defaultColor color selected by the "default color" checkbox
258  * @returns QDialog::result().
259  */
260  static int getColor(QColor &theColor, const QColor &defaultColor, QWidget *parent = nullptr);
261 
262  /**
263  * Gets the color from the pixel at point p on the screen.
264  */
265  static QColor grabColor(const QPoint &p);
266 
267  /**
268  * Call this to make the dialog show a "Default Color" checkbox.
269  * If this checkbox is selected, the dialog will return an "invalid" color (QColor()).
270  * This can be used to mean "the default text color", for instance,
271  * the one with the KDE text color on screen, but black when printing.
272  */
273  void setDefaultColor(const QColor &defaultCol);
274 
275  /**
276  * @return the value passed to setDefaultColor
277  */
278  QColor defaultColor() const;
279 
280  /**
281  * When set to true, the user is allowed to change the alpha component of the color.
282  * The default value is false.
283  * @since 4.5
284  */
285  void setAlphaChannelEnabled(bool alpha);
286 
287  /**
288  * Returns true when the user can change the alpha channel.
289  * @since 4.5
290  */
291  bool isAlphaChannelEnabled() const;
292 
293 public Q_SLOTS:
294  /**
295  * Preselects a color.
296  */
297  void setColor(const QColor &col);
298 
299 Q_SIGNALS:
300  /**
301  * Emitted when a color is selected.
302  * Connect to this to monitor the color as it as selected if you are
303  * not running modal.
304  */
305  void colorSelected(const QColor &col);
306 
307 private:
308  Q_PRIVATE_SLOT(d, void slotRGBChanged(void))
309  Q_PRIVATE_SLOT(d, void slotAlphaChanged(void))
310  Q_PRIVATE_SLOT(d, void slotHSVChanged(void))
311  Q_PRIVATE_SLOT(d, void slotHtmlChanged(void))
312  Q_PRIVATE_SLOT(d, void slotHSChanged(int, int))
313  Q_PRIVATE_SLOT(d, void slotVChanged(int))
314  Q_PRIVATE_SLOT(d, void slotAChanged(int))
315  Q_PRIVATE_SLOT(d, void slotColorSelected(const QColor &col))
316  Q_PRIVATE_SLOT(d, void slotColorSelected(const QColor &col, const QString &name))
317  Q_PRIVATE_SLOT(d, void slotColorDoubleClicked(const QColor &col, const QString &name))
318  Q_PRIVATE_SLOT(d, void slotColorPicker())
319  Q_PRIVATE_SLOT(d, void slotAddToCustomColors())
320  Q_PRIVATE_SLOT(d, void slotDefaultColorClicked())
321  Q_PRIVATE_SLOT(d, void slotModeChanged(int id))
322 
323  /**
324  * Write the settings of the dialog to config file.
325  **/
326  Q_PRIVATE_SLOT(d, void slotWriteSettings())
327 
328 private:
329  /**
330  * Read the settings for the dialog from config file.
331  **/
332  void readSettings();
333 
334 protected:
335  void mouseMoveEvent(QMouseEvent *) override;
336  void mouseReleaseEvent(QMouseEvent *) override;
337  void keyPressEvent(QKeyEvent *) override;
338  bool eventFilter(QObject *obj, QEvent *ev) override;
339 
340 private:
341  class KColorDialogPrivate;
342  KColorDialogPrivate *const d;
343 
345 };
346 
347 #endif // KCOLORDIALOG_H
A table of editable color cells.
Definition: kcolordialog.h:41
virtual bool eventFilter(QObject *o, QEvent *e) override
Q_PROPERTY(...)
virtual void mouseReleaseEvent(QMouseEvent *event) override
Q_INVOKABLE QColor getColor(QString name)
Q_SLOTSQ_SLOTS
virtual void dropEvent(QDropEvent *event)
virtual void dropEvent(QDropEvent *event) override
virtual void mousePressEvent(QMouseEvent *event) override
virtual int sizeHintForRow(int row) const const override
A dialog base class with standard buttons and predefined layouts.
Definition: kdialog.h:128
virtual void mouseDoubleClickEvent(QMouseEvent *event) override
virtual void dragEnterEvent(QDragEnterEvent *event) override
virtual int sizeHintForColumn(int column) const const override
Q_SIGNALSQ_SIGNALS
virtual void mouseReleaseEvent(QMouseEvent *event)
A color selection dialog.
Definition: kcolordialog.h:211
virtual void dragMoveEvent(QDragMoveEvent *event) override
virtual void paintEvent(QPaintEvent *) override
virtual void resizeEvent(QResizeEvent *event) override
virtual void mouseMoveEvent(QMouseEvent *event)
A color displayer.
Definition: kcolordialog.h:117
Q_DISABLE_COPY(Class)
void keyPressEvent(QKeyEvent *) override
Definition: kdialog.cpp:416
virtual void dragEnterEvent(QDragEnterEvent *event)
virtual void mouseMoveEvent(QMouseEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:55:55 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.