1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
/***************************************************************************
palette.cpp - description
-------------------
begin : Sat Jul 8 2000
copyright : (C) 2000 by Artur Rataj
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <qstring.h>
#include <qstringlist.h>
#include <qtextstream.h>
#include <qregexp.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include "main.h"
#include "color.h"
#include "palette.h"
Palette::Palette() {
init();
}
Palette::Palette(const Palette& palette) {
init();
for(int colorIndex = 0; colorIndex < palette.length(); ++colorIndex) {
Color* color = new Color(*( (Palette&)palette ).color( colorIndex ));
append(color);
}
setName(palette.name());
}
Palette::~Palette() {
}
void Palette::init() {
colors.setAutoDelete(true);
}
QStringList Palette::kdePalettes() {
QStringList paletteList;
KGlobal::dirs()->findAllResources("config", palettesDir + "/*", false, true, paletteList);
return paletteList;
}
void Palette::setName(const QString& name) {
m_name = name;
}
const QString& Palette::name() const {
return m_name;
}
void Palette::insert(const int index, Color* const color) {
colors.insert(index, color);
}
void Palette::append(Color* const color) {
colors.append(color);
}
void Palette::remove(const int index) {
colors.remove(index);
}
int Palette::length() const {
return (int)colors.count();
}
Color* Palette::color(const int index) {
return colors.at(index);
}
Palette Palette::copy(const int index, const int length) {
Palette newPalette;
for(int colorIndex = index; colorIndex < index + length; ++colorIndex)
newPalette.append(new Color( *color(colorIndex) ));
newPalette.setName(name());
return newPalette;
}
Palette Palette::cut(const int index, const int length) {
Palette newPalette;
for(int colorNum = 0; colorNum < length; ++colorNum) {
newPalette.append(new Color( *color(index) ));
remove(index);
}
newPalette.setName(name());
return newPalette;
}
void Palette::paste(const int index, Palette& palette) {
for(int colorIndex = 0; colorIndex < palette.length(); ++colorIndex)
insert(index + colorIndex, new Color( *palette.color(colorIndex) ));
}
bool Palette::load(QTextStream& stream, bool loadName /* = true */) {
bool result = true;
setName("KDE palette");
int lineNum = 0;
while (!stream.atEnd()) {
QString string = stream.readLine().append(' ');
if(string.find( QRegExp("[^\\s]") ) == -1 ||
string.stripWhiteSpace().at( 0 ) == '#' ||
( loadName && lineNum == 0 )) {
if(loadName && lineNum == 0)
setName(string.stripWhiteSpace());
} else {
Color* newColor = new Color();
int position = string.find(QRegExp( "[^\\s]" ));
for(int componentIndex = 0; componentIndex < Color::COMPONENTS_NUM;
++componentIndex) {
if(position == -1) {
m_errorString = i18n("Invalid format");
result = false;
break;
}
int endPosition = string.find(QRegExp( "\\s" ), position);
if(endPosition == -1) {
m_errorString = i18n("Invalid format");
result = false;
break;
}
QString componentString = string.mid(position, endPosition - position);
int componentValue = componentString.toInt(&result);
if(!result ||
componentValue < 0 ||
componentValue > RGB_MAX_COMPONENT_VALUE) {
m_errorString = i18n("Invalid format");
result = false;
break;
}
newColor->setComponent(componentIndex, componentValue);
position = string.find(QRegExp( "[^\\s]" ), endPosition);
}
if(!result) {
delete newColor;
break;
}
if(position != -1)
newColor->setName(string.mid( position ).stripWhiteSpace());
colors.append(newColor);
}
++lineNum;
}
if(!result)
deleteContents();
return result;
}
bool Palette::load(const QString& fileName) {
bool result = true;
QFile file(fileName);
if(!file.open( IO_ReadOnly )) {
m_errorString = i18n("Could not open file");
result = false;
} else {
QTextStream stream(&file);
result = load(stream);
file.close();
}
return result;
}
bool Palette::save(QTextStream& stream, const QFile* file /* = 0 */,
bool saveName /* = true */) {
bool result = true;
if(saveName)
stream << name() + QString("\n");
if(file && file->status() != IO_Ok) {
m_errorString = i18n("Write error");
result = false;
} else
for(int colorIndex = 0; colorIndex < length(); ++colorIndex) {
Color* col = color(colorIndex);
QString redComponentString;
QString greenComponentString;
QString blueComponentString;
redComponentString.setNum(col->component( Color::RED_INDEX ));
greenComponentString.setNum(col->component( Color::GREEN_INDEX ));
blueComponentString.setNum(col->component( Color::BLUE_INDEX ));
QString nameString = col->name();
if(!nameString.isEmpty())
nameString.prepend(" ");
stream << redComponentString + QString(" ") +
greenComponentString + QString(" ") +
blueComponentString +
nameString + QString("\n");
if(file && file->status() != IO_Ok) {
m_errorString = i18n("Write error");
result = false;
break;
}
}
return result;
}
bool Palette::save(const QString& fileName) {
bool result = true;
QFile file(fileName);
if(!file.open( IO_WriteOnly|IO_Truncate )) {
m_errorString = i18n("Could not open file for writing");
result = false;
} else {
QTextStream stream(&file);
result = save(stream);
file.close();
}
return result;
}
void Palette::deleteContents() {
colors.clear();
}
const QString& Palette::errorString() const {
return m_errorString;
}
|