1 /* 2 Drawpile - a collaborative drawing program. 3 4 Copyright (C) 2007-2015 Calle Laakkonen 5 6 Drawpile is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 Drawpile is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with Drawpile. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 #ifndef LOCALPALETTE_H 20 #define LOCALPALETTE_H 21 22 #include <QList> 23 #include <QColor> 24 #include <QObject> 25 26 class QFileInfo; 27 28 struct PaletteColor { 29 QColor color; 30 QString name; 31 colorPaletteColor32 PaletteColor(const QColor &c=QColor(), const QString &n=QString()) : color(c), name(n) { } 33 }; 34 35 Q_DECLARE_TYPEINFO(PaletteColor, Q_MOVABLE_TYPE); 36 37 class Palette : public QObject { 38 Q_OBJECT 39 public: 40 //! Construct a blank palette 41 explicit Palette(QObject *parent=0); 42 explicit Palette(const QString &name, QObject *parent=0); 43 Palette(const QString& name, const QString& filename, bool readonly, QObject *parent=0); 44 45 //! Load a palette from a file 46 static Palette *fromFile(const QFileInfo& file, bool readonly=false, QObject *parent=0); 47 48 static Palette *copy(const Palette *pal, const QString &newname, QObject *parent=0); 49 50 //! Set the name of the palette 51 void setName(const QString& name); 52 53 //! Get the name of the palette name()54 const QString& name() const { return _name; } 55 56 //! Get the filename of the palette filename()57 const QString& filename() const { return _filename; } 58 59 //! Get the optimum number of columns to display the colors in columns()60 int columns() const { return _columns; } 61 62 //! Set the optimum number of columns 63 void setColumns(int columns); 64 65 //! Has the palette been modified since it was last saved/loaded? isModified()66 bool isModified() const { return _modified; } 67 68 //! Is the palette write-protected? isWriteProtected()69 bool isWriteProtected() const { return _writeprotect | _readonly; } 70 71 //! Set read-only mode setWriteProtected(bool wp)72 void setWriteProtected(bool wp) { _writeprotect = wp; } 73 74 //! Is the palette file read-only? isReadonly()75 bool isReadonly() const { return _readonly; } 76 77 //! Save palette to its original file (filename must be set) 78 bool save(); 79 80 //! Save the palette to an external file 81 bool exportPalette(const QString &filename, QString *errorString=nullptr); 82 83 //! Delete the palette file (if it exists) 84 bool deleteFile(); 85 86 //! Get the number of colors count()87 int count() const { return _colors.size(); } 88 89 //! Get the color at index color(int index)90 PaletteColor color(int index) const { return _colors.at(index); } 91 92 //! Set a color at the specified index 93 void setColor(int index, const PaletteColor &color); 94 void setColor(int index, const QColor& color, const QString &name=QString()) { setColor(index, PaletteColor(color, name)); } 95 96 //! Insert a new color 97 void insertColor(int index, const QColor& color, const QString &name=QString()); 98 99 //! Append anew color to the end of the palette 100 void appendColor(const QColor &color, const QString &name=QString()); 101 102 //! Remove a color 103 void removeColor(int index); 104 105 signals: 106 void nameChanged(); 107 void columnsChanged(); 108 void colorsChanged(); 109 110 private: 111 bool save(const QString& filename); 112 113 QList<PaletteColor> _colors; 114 QString _name; 115 QString _oldname; 116 QString _filename; 117 118 int _columns; 119 bool _modified; 120 bool _readonly; 121 bool _writeprotect; 122 }; 123 124 #endif 125 126