File indexing completed on 2024-05-05 04:21:24

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #ifndef kpColorCells_H
0030 #define kpColorCells_H
0031 
0032 
0033 #include <QUrl>
0034 
0035 #include "lgpl/generic/widgets/kpColorCellsBase.h"
0036 
0037 
0038 
0039 class kpColorCollection;
0040 
0041 class kpColor;
0042 
0043 
0044 // This widget consists of rows of 11 cells of colors.  The cells become
0045 // shorter as soon as there are 3 rows.  After that, a vertical scrollbar
0046 // is usually displayed.
0047 //
0048 // By default, it is set to the DefaultColorCollection(), with 2 rows.
0049 //
0050 //
0051 // Cell widgets might not exist for 2 reasons:
0052 //
0053 // 1. The respective colors in the color collection are invalid.
0054 //    An easy way to create this situation is to appendRow().
0055 //
0056 // 2. The number of colors in the color collection is not divisible by
0057 //    the columnCount() [currently fixed at 11], so some cells in the
0058 //    last row might not be linked to colors in the color collection.
0059 //
0060 // Currently, this class always ensures that there is at least one
0061 // visual/table row (which might contain no cell widgets).
0062 //
0063 //
0064 // To determine where the color collection came from:
0065 //
0066 // 1. If url() is non-empty, it came from a file.
0067 //
0068 // 2. If url() is empty:
0069 //    a) If name() is non-empty, it came from KDE-managed color collection.
0070 //    b) If name() is empty, it came from DefaultColorCollection().
0071 //
0072 //
0073 // See also the API documentation for kpColorCellsBase.
0074 //
0075 // TODO: For now, only horizontal orientation is supported.
0076 class kpColorCells : public kpColorCellsBase
0077 {
0078 Q_OBJECT
0079 
0080 public:
0081     explicit kpColorCells (QWidget *parent,
0082                   Qt::Orientation o = Qt::Horizontal);
0083     ~kpColorCells () override;
0084 
0085     static kpColorCollection DefaultColorCollection ();
0086 
0087     Qt::Orientation orientation () const;
0088     void setOrientation (Qt::Orientation o);
0089 
0090 protected:
0091     void makeCellsMatchColorCollection ();
0092 
0093 public:
0094     bool isModified () const;
0095     // (this emits isModifiedChanged() if the modified state changes)
0096     void setModified (bool yes);
0097 public Q_SLOTS:
0098     // (this emits isModifiedChanged() if the modified state changes)
0099     void setModified ();
0100 
0101 public:
0102     // The source URL of the kpColorCollection.  Empty for color
0103     // collections that did not come from files.
0104     QUrl url () const;
0105 
0106     // The name of the kpColorCollection.  Empty for color collections
0107     // managed by KDE.
0108     QString name () const;
0109 
0110     const kpColorCollection *colorCollection () const;
0111 
0112 private:
0113     // Ensures there's a least one row of cells, to avoid a confusing UI.
0114     void ensureHaveAtLeastOneRow ();
0115 public:
0116     void setColorCollection (const kpColorCollection &colorCol,
0117         const QUrl &url = QUrl ());
0118 
0119     bool openColorCollection (const QUrl &url);
0120     bool saveColorCollectionAs (const QUrl &url);
0121     bool saveColorCollection ();
0122 
0123     // These add and delete visual/table rows, independent of whether the number
0124     // of colors in the color collection is divisible by the columnCount()
0125     // [currently fixed at 11].
0126     //
0127     // For instance, if you only had 15 colors in the color collection, there are
0128     // visually 2 rows (22 cells in total):
0129     //
0130     // 1. appendRow() will add a visual row so that there will be 3 visual rows
0131     //    (33 cells in total).  (22 - 15) + 11 invalid colors will be added to
0132     //    the back of the color collection.  Note that invalid colors are not
0133     //    saved by kpColorCollection, so those new cells not initialized by the
0134     //    user will not be saved.
0135     // 2. deleteRow() will delete a visual row so that there will be 1 visual
0136     //    row (11 cells in total) remaining.  (15 - 11) colors will be deleted
0137     //    from the back of the color collection.
0138     void appendRow ();
0139     void deleteLastRow ();
0140 
0141 Q_SIGNALS:
0142     void foregroundColorChanged (const kpColor &color);
0143     void backgroundColorChanged (const kpColor &color);
0144 
0145     void rowCountChanged (int rowCount);
0146 
0147     void nameChanged (const QString &name);
0148     void urlChanged (const QUrl &url);
0149 
0150     // Emitted when setModified() is called and the modified state changes.
0151     // It may be called at other times, even when the modified state did
0152     // not change.
0153     void isModifiedChanged (bool isModified);
0154 
0155 protected:
0156     void contextMenuEvent (QContextMenuEvent *e) override;
0157 
0158 protected Q_SLOTS:
0159     void slotColorSelected (int cell, const QColor &color, Qt::MouseButton button);
0160     void slotColorDoubleClicked (int cell, const QColor &color);
0161     void slotColorChanged (int cell, const QColor &color);
0162 
0163 private:
0164     struct kpColorCellsPrivate * const d;
0165 };
0166 
0167 
0168 #endif  // kpColorCells_H