File indexing completed on 2024-12-22 04:07:31

0001 
0002 // SYNC: Periodically merge in changes from:
0003 //
0004 //           trunk/KDE/kdelibs/kdeui/colors/kcolordialog.{h,cpp}
0005 //
0006 //       which this is a fork of.
0007 //
0008 //       Our changes can be merged back into KDE (grep for "Added for KolourPaint" and similar).
0009 
0010 /* This file is part of the KDE libraries
0011     Copyright (C) 1997 Martin Jones (mjones@kde.org)
0012 
0013     This library is free software; you can redistribute it and/or
0014     modify it under the terms of the GNU Library General Public
0015     License as published by the Free Software Foundation; either
0016     version 2 of the License, or (at your option) any later version.
0017 
0018     This library is distributed in the hope that it will be useful,
0019     but WITHOUT ANY WARRANTY; without even the implied warranty of
0020     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0021     Library General Public License for more details.
0022 
0023     You should have received a copy of the GNU Library General Public License
0024     along with this library; see the file COPYING.LIB.  If not, write to
0025     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0026     Boston, MA 02110-1301, USA.
0027 */
0028 //----------------------------------------------------------------------
0029 // KDE color selection dialog.
0030 
0031 // layout management added Oct 1997 by Mario Weilguni
0032 // <mweilguni@sime.com>
0033 
0034 #ifndef kpColorCellsBase_H
0035 #define kpColorCellsBase_H
0036 
0037 #include <kolourpaint_lgpl_export.h>
0038 
0039 #include <QTableWidget>
0040 #include <QLoggingCategory>
0041 
0042 Q_DECLARE_LOGGING_CATEGORY(kpLogColorCollection)
0043 
0044 /**
0045 * A table of editable color cells.
0046 *
0047 * @author Martin Jones <mjones@kde.org>
0048 *
0049 * Added for KolourPaint:
0050 *
0051 * If you have not called setColor() for a cell, its widget will not exist.
0052 * So it is possible to have "holes" in this rectangular table of cells.
0053 * You can delete a cell widget by calling setColor() with an invalid QColor.
0054 *
0055 * An invariant is that color() returns an invalid color iff the cells' widget
0056 * does not exist.  Note that:
0057 *
0058 * 1. You can double click on cells that don't contain a widget
0059 * 2. You can drop onto -- but not drag from -- a cell that doesn't contain a
0060 *    widget
0061 *
0062 * If a color is dragged and dropped to-and-from the same instance of this
0063 * widget, then the colors in the source and destination cells are swapped
0064 * (this is a "move action").
0065 *
0066 * If CTRL is held or they are not from the same instance, then the source
0067 * cell's color is copied into the destination cell, without any change to
0068 * the source cell (this is a "copy action").
0069 */
0070 class KOLOURPAINT_LGPL_EXPORT kpColorCellsBase : public QTableWidget
0071 {
0072   Q_OBJECT
0073 public:
0074   /**
0075    * Constructs a new table of color cells, consisting of
0076    * @p rows * @p columns colors.
0077    *
0078    * @param parent The parent of the new widget
0079    * @param rows The number of rows in the table
0080    * @param columns The number of columns in the table
0081    *
0082    * Specifying <rows> and <columns> was made optional for KolourPaint.
0083    */
0084   explicit kpColorCellsBase( QWidget *parent, int rows = 0, int columns = 0 );
0085   ~kpColorCellsBase() override;
0086 
0087 private:
0088   /** Added for KolourPaint. */
0089   void invalidateAllColors ();
0090 
0091 public:
0092   /** Added for KolourPaint.
0093       WARNING: These are not virtual in QTableWidget.
0094   */
0095   void clear ();
0096   void clearContents ();
0097 
0098   /** Added for KolourPaint. */
0099   void setRowColumnCounts (int rows, int columns);
0100 
0101   /** Added for KolourPaint.
0102       WARNING: These are not virtual in QTableWidget.
0103   */
0104   void setColumnCount (int columns);
0105   void setRowCount (int rows);
0106 
0107   /** Sets the color in the given index in the table.
0108 
0109       The following behavior change was added for KolourPaint:
0110 
0111           If <col> is not valid, the cell widget at <index> is deleted.
0112   */
0113   void setColor( int index, const QColor &col );
0114   /** Returns the color at a given index in the table.
0115       If a cell widget does not exist at <index>, the invalid color is
0116       returned.
0117    */
0118   QColor color( int index ) const;
0119   /** Returns the total number of color cells in the table */
0120   int count() const;
0121 
0122   void setShading(bool shade);
0123   void setAcceptDrags(bool acceptDrags);
0124 
0125   /** Whether component cells should resize with the entire widget.
0126       Default is true.
0127 
0128       Added for KolourPaint.
0129   */
0130   void setCellsResizable(bool yes);
0131 
0132   /** Sets the currently selected cell to @p index */
0133   void setSelected(int index);
0134   /** Returns the index of the cell which is currently selected */
0135   int  selectedIndex() const;
0136 
0137 Q_SIGNALS:
0138   /** Emitted when a color is selected in the table */
0139   void colorSelected( int index , const QColor& color );
0140   /** Emitted with the above.
0141 
0142       Added for KolourPaint.
0143   */
0144   void colorSelectedWhitButton( int index , const QColor& color, Qt::MouseButton button );
0145 
0146   /** Emitted when a color in the table is double-clicked */
0147   void colorDoubleClicked( int index , const QColor& color );
0148 
0149   /** Emitted when setColor() is called.
0150       This includes when a color is dropped onto the table, via drag-and-drop.
0151 
0152       Added for KolourPaint.
0153   */
0154   void colorChanged( int index , const QColor& color );
0155 
0156 protected:
0157   /** Grays out the cells, when the object is disabled.
0158       Added for KolourPaint.
0159   */
0160   void changeEvent( QEvent* event ) override;
0161 
0162   // the three methods below are used to ensure equal column widths and row heights
0163   // for all cells and to update the widths/heights when the widget is resized
0164   int sizeHintForColumn(int column) const override;
0165   int sizeHintForRow(int column) const override;
0166   void resizeEvent( QResizeEvent* event ) override;
0167 
0168   void mouseReleaseEvent( QMouseEvent * ) override;
0169   void mousePressEvent( QMouseEvent * ) override;
0170   void mouseMoveEvent( QMouseEvent * ) override;
0171   void dragEnterEvent( QDragEnterEvent * ) override;
0172   void dragMoveEvent( QDragMoveEvent * ) override;
0173   void dropEvent( QDropEvent *) override;
0174   void mouseDoubleClickEvent( QMouseEvent * ) override;
0175 
0176   /** <allowEmptyCell> was added for KolourPaint. */
0177   int positionToCell(const QPoint &pos, bool ignoreBorders=false,
0178     bool allowEmptyCell=false) const;
0179 
0180 private:
0181   class kpColorCellsBasePrivate;
0182   friend class kpColorCellsBasePrivate;
0183   kpColorCellsBasePrivate *const d;
0184 
0185   Q_DISABLE_COPY(kpColorCellsBase)
0186 };
0187 
0188 #endif      // kpColorCellsBase_H