Changeset View
Changeset View
Standalone View
Standalone View
libs/widgets/KoColorSetWidget.cpp
Context not available. | |||||
20 | #include "KoColorSetWidget.h" | 20 | #include "KoColorSetWidget.h" | ||
---|---|---|---|---|---|
21 | #include "KoColorSetWidget_p.h" | 21 | #include "KoColorSetWidget_p.h" | ||
22 | 22 | | |||
23 | #include <QTimer> | | |||
24 | #include <QApplication> | 23 | #include <QApplication> | ||
25 | #include <QSize> | 24 | #include <QSize> | ||
26 | #include <QToolButton> | 25 | #include <QToolButton> | ||
Context not available. | |||||
41 | #include <ksharedconfig.h> | 40 | #include <ksharedconfig.h> | ||
42 | 41 | | |||
43 | #include <resources/KoColorSet.h> | 42 | #include <resources/KoColorSet.h> | ||
44 | #include <resources/KoColorSetEntry.h> | | |||
45 | #include <KoColorPatch.h> | 43 | #include <KoColorPatch.h> | ||
46 | #include <KoEditColorSetDialog.h> | | |||
47 | #include <KoColorSpaceRegistry.h> | 44 | #include <KoColorSpaceRegistry.h> | ||
48 | #include <KoResourceServer.h> | 45 | #include <KoResourceServer.h> | ||
49 | #include <KoResourceServerProvider.h> | 46 | #include <KoResourceServerProvider.h> | ||
47 | #include <KoResourceServerAdapter.h> | ||||
50 | 48 | | |||
51 | void KoColorSetWidget::KoColorSetWidgetPrivate::fillColors() | 49 | #include <kis_palette_view.h> | ||
52 | { | 50 | #include <KisPaletteDelegate.h> | ||
53 | delete colorSetContainer; | 51 | #include <KisPaletteModel.h> | ||
54 | colorSetContainer = new QWidget(); | 52 | #include <kis_icon_utils.h> | ||
55 | colorSetLayout = new QVBoxLayout(); | | |||
56 | colorSetLayout->setMargin(3); | | |||
57 | colorSetLayout->setSpacing(0); // otherwise the use can click where there is none | | |||
58 | colorSetContainer->setBackgroundRole(QPalette::Dark); | | |||
59 | | ||||
60 | int patchSize = 12; | | |||
61 | | ||||
62 | int columns = 16; | | |||
63 | if (colorSet) { | | |||
64 | columns = colorSet->columnCount(); | | |||
65 | } | | |||
66 | colorSetContainer->setMinimumWidth(columns*patchSize+6); | | |||
67 | colorSetContainer->setLayout(colorSetLayout); | | |||
68 | patchWidgetList.clear(); | | |||
69 | colornames.clear(); | | |||
70 | colorNameCmb->clear(); | | |||
71 | | ||||
72 | QWidget *defaultGroupContainer = new QWidget(); | | |||
73 | QGridLayout *colorGroupLayout = new QGridLayout(); | | |||
74 | for(int i = 0; i<columns; i++) { | | |||
75 | colorGroupLayout->setColumnMinimumWidth(i, patchSize); | | |||
76 | } | | |||
77 | defaultGroupContainer->setMinimumWidth(columns*patchSize); | | |||
78 | defaultGroupContainer->setMaximumWidth(columns*patchSize); | | |||
79 | colorGroupLayout->setSpacing(0); | | |||
80 | colorGroupLayout->setMargin(0); | | |||
81 | defaultGroupContainer->setLayout(colorGroupLayout); | | |||
82 | if (colorSet) { | | |||
83 | for( quint32 i = 0, p= 0; i < colorSet->nColorsGroup(); i++) { | | |||
84 | KoColorPatch *patch = new KoColorPatch(colorSetContainer); | | |||
85 | patch->setFrameStyle(QFrame::Plain | QFrame::Box); | | |||
86 | patch->setLineWidth(1); | | |||
87 | KoColorSetEntry c = colorSet->getColorGlobal(i); | | |||
88 | patch->setColor(c.color()); | | |||
89 | patch->setToolTip(c.name()); | | |||
90 | connect(patch, SIGNAL(triggered(KoColorPatch *)), thePublic, SLOT(colorTriggered(KoColorPatch *))); | | |||
91 | colorGroupLayout->addWidget(patch, p/columns, p%columns); | | |||
92 | patch->setDisplayRenderer(displayRenderer); | | |||
93 | patchWidgetList.append(patch); | | |||
94 | colornames.append(c.name()); | | |||
95 | QPixmap colorsquare = QPixmap(12,12); | | |||
96 | colorsquare.fill(c.color().toQColor()); | | |||
97 | colorNameCmb->addItem(QIcon(colorsquare), c.name()); | | |||
98 | ++p; | | |||
99 | } | | |||
100 | colorSetLayout->addWidget(defaultGroupContainer); | | |||
101 | Q_FOREACH(QString groupName, colorSet->getGroupNames()) { | | |||
102 | QGroupBox *groupbox = new QGroupBox(); | | |||
103 | groupbox->setTitle(groupName); | | |||
104 | QGridLayout *groupLayout = new QGridLayout(); | | |||
105 | for(int i = 0; i<columns; i++) { | | |||
106 | groupLayout->setColumnMinimumWidth(i, patchSize); | | |||
107 | } | | |||
108 | groupLayout->setSpacing(0); | | |||
109 | groupLayout->setMargin(0); | | |||
110 | groupbox->setMinimumWidth(columns*patchSize); | | |||
111 | groupbox->setMaximumWidth(columns*patchSize); | | |||
112 | groupbox->setLayout(groupLayout); | | |||
113 | groupbox->setFlat(true); | | |||
114 | for( quint32 i = 0, p= 0; i < colorSet->nColorsGroup(groupName); i++) { | | |||
115 | KoColorPatch *patch = new KoColorPatch(colorSetContainer); | | |||
116 | patch->setFrameStyle(QFrame::Plain | QFrame::Box); | | |||
117 | patch->setLineWidth(1); | | |||
118 | KoColorSetEntry c = colorSet->getColorGroup(i, groupName); | | |||
119 | patch->setColor(c.color()); | | |||
120 | patch->setToolTip(c.name()); | | |||
121 | connect(patch, SIGNAL(triggered(KoColorPatch *)), thePublic, SLOT(colorTriggered(KoColorPatch *))); | | |||
122 | groupLayout->addWidget(patch, p/columns, p%columns); | | |||
123 | patch->setDisplayRenderer(displayRenderer); | | |||
124 | patchWidgetList.append(patch); | | |||
125 | colornames.append(c.name()); | | |||
126 | QPixmap colorsquare = QPixmap(12,12); | | |||
127 | colorsquare.fill(c.color().toQColor()); | | |||
128 | colorNameCmb->addItem(QIcon(colorsquare), c.name()); | | |||
129 | ++p; | | |||
130 | } | | |||
131 | colorSetLayout->addWidget(groupbox); | | |||
132 | } | | |||
133 | } | | |||
134 | | ||||
135 | scrollArea->setWidget(colorSetContainer); | | |||
136 | connect(colorNameCmb, SIGNAL(activated(QString)), thePublic, SLOT(setColorFromString(QString)), Qt::UniqueConnection); | | |||
137 | } | | |||
138 | | ||||
139 | void KoColorSetWidget::KoColorSetWidgetPrivate::addRemoveColors() | | |||
140 | { | | |||
141 | KoResourceServer<KoColorSet>* srv = KoResourceServerProvider::instance()->paletteServer(); | | |||
142 | QList<KoColorSet*> palettes = srv->resources(); | | |||
143 | | ||||
144 | Q_ASSERT(colorSet); | | |||
145 | KoEditColorSetDialog *dlg = new KoEditColorSetDialog(palettes, colorSet->name(), thePublic); | | |||
146 | if (dlg->exec() == KoDialog::Accepted ) { // always reload the color set | | |||
147 | KoColorSet * cs = dlg->activeColorSet(); | | |||
148 | // check if the selected colorset is predefined | | |||
149 | if( cs && !palettes.contains( cs ) ) { | | |||
150 | int i = 1; | | |||
151 | QFileInfo fileInfo; | | |||
152 | QString savePath = srv->saveLocation(); | | |||
153 | | ||||
154 | do { | | |||
155 | fileInfo.setFile(savePath + QString("%1.%2").arg(i++, 4, 10, QChar('0')).arg(colorSet->defaultFileExtension())); | | |||
156 | } | | |||
157 | while (fileInfo.exists()); | | |||
158 | | ||||
159 | cs->setFilename( fileInfo.filePath() ); | | |||
160 | cs->setValid( true ); | | |||
161 | | ||||
162 | // add new colorset to predefined colorsets | | |||
163 | if (!srv->addResource(cs)) { | | |||
164 | | ||||
165 | delete cs; | | |||
166 | cs = 0; | | |||
167 | } | | |||
168 | } | | |||
169 | if (cs) { | | |||
170 | thePublic->setColorSet(cs); | | |||
171 | } | | |||
172 | } | | |||
173 | delete dlg; | | |||
174 | } | | |||
175 | 53 | | |||
176 | void KoColorSetWidget::KoColorSetWidgetPrivate::addRecent(const KoColor &color) | 54 | void KoColorSetWidget::KoColorSetWidgetPrivate::addRecent(const KoColor &color) | ||
177 | { | 55 | { | ||
178 | if(numRecents<6) { | 56 | if(numRecents < 6) { | ||
179 | recentPatches[numRecents] = new KoColorPatch(thePublic); | 57 | recentPatches[numRecents] = new KoColorPatch(thePublic); | ||
180 | recentPatches[numRecents]->setFrameShape(QFrame::StyledPanel); | 58 | recentPatches[numRecents]->setFrameShape(QFrame::StyledPanel); | ||
181 | recentPatches[numRecents]->setDisplayRenderer(displayRenderer); | 59 | recentPatches[numRecents]->setDisplayRenderer(displayRenderer); | ||
182 | recentsLayout->insertWidget(numRecents+1, recentPatches[numRecents]); | 60 | recentsLayout->insertWidget(numRecents + 1, recentPatches[numRecents]); | ||
183 | connect(recentPatches[numRecents], SIGNAL(triggered(KoColorPatch *)), thePublic, SLOT(colorTriggered(KoColorPatch *))); | 61 | connect(recentPatches[numRecents], SIGNAL(triggered(KoColorPatch *)), thePublic, SLOT(slotPatchTriggered(KoColorPatch *))); | ||
184 | numRecents++; | 62 | numRecents++; | ||
185 | } | 63 | } | ||
186 | // shift colors to the right | 64 | // shift colors to the right | ||
Context not available. | |||||
205 | 83 | | |||
206 | KoColorSetWidget::KoColorSetWidget(QWidget *parent) | 84 | KoColorSetWidget::KoColorSetWidget(QWidget *parent) | ||
207 | : QFrame(parent) | 85 | : QFrame(parent) | ||
208 | ,d(new KoColorSetWidgetPrivate()) | 86 | , d(new KoColorSetWidgetPrivate()) | ||
209 | { | 87 | { | ||
210 | d->thePublic = this; | 88 | d->thePublic = this; | ||
211 | d->colorSet = 0; | | |||
212 | | ||||
213 | d->firstShowOfContainer = true; | | |||
214 | | ||||
215 | d->mainLayout = new QVBoxLayout(); | | |||
216 | d->mainLayout->setMargin(4); | | |||
217 | d->mainLayout->setSpacing(2); | | |||
218 | | ||||
219 | d->colorSetContainer = 0; | | |||
220 | 89 | | |||
221 | d->numRecents = 0; | 90 | d->numRecents = 0; | ||
222 | d->recentsLayout = new QHBoxLayout(); | 91 | d->recentsLayout = new QHBoxLayout; | ||
223 | d->mainLayout->addLayout(d->recentsLayout); | | |||
224 | d->recentsLayout->setMargin(0); | 92 | d->recentsLayout->setMargin(0); | ||
225 | d->recentsLayout->addWidget(new QLabel(i18n("Recent:"))); | 93 | d->recentsLayout->addWidget(new QLabel(i18n("Recent:"))); | ||
226 | d->recentsLayout->addStretch(1); | 94 | d->recentsLayout->addStretch(1); | ||
Context not available. | |||||
229 | color.fromQColor(QColor(128,0,0)); | 97 | color.fromQColor(QColor(128,0,0)); | ||
230 | d->addRecent(color); | 98 | d->addRecent(color); | ||
231 | 99 | | |||
232 | d->scrollArea = new QScrollArea(); | 100 | d->paletteView = new KisPaletteView(this); | ||
233 | d->scrollArea->setBackgroundRole(QPalette::Dark); | 101 | KisPaletteModel *paletteModel = new KisPaletteModel(d->paletteView); | ||
234 | d->mainLayout->addWidget(d->scrollArea); | 102 | d->paletteView->setPaletteModel(paletteModel); | ||
235 | d->colorNameCmb = new QComboBox(this); | 103 | d->paletteView->setDisplayRenderer(d->displayRenderer); | ||
236 | d->colorNameCmb->setEditable(true); | 104 | | ||
237 | d->colorNameCmb->setInsertPolicy(QComboBox::NoInsert); | 105 | d->paletteChooser = new KisPaletteListWidget(this); | ||
238 | d->mainLayout->addWidget(d->colorNameCmb); | 106 | d->paletteChooserButton = new KisPopupButton(this); | ||
239 | d->fillColors(); | 107 | d->paletteChooserButton->setPopupWidget(d->paletteChooser); | ||
108 | d->paletteChooserButton->setIcon(KisIconUtils::loadIcon("hi16-palette_library")); | ||||
109 | d->paletteChooserButton->setToolTip(i18n("Choose palette")); | ||||
240 | 110 | | |||
111 | d->colorNameCmb = new KisPaletteComboBox(this); | ||||
112 | d->colorNameCmb->setCompanionView(d->paletteView); | ||||
241 | 113 | | |||
242 | d->addRemoveButton = new QToolButton(this); | 114 | d->bottomLayout = new QHBoxLayout; | ||
243 | d->addRemoveButton->setText(i18n("Add / Remove Colors...")); | 115 | d->bottomLayout->addWidget(d->paletteChooserButton); | ||
244 | d->addRemoveButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); | 116 | d->bottomLayout->addWidget(d->colorNameCmb); | ||
245 | connect(d->addRemoveButton, SIGNAL(clicked()), SLOT(addRemoveColors())); | 117 | d->bottomLayout->setStretch(0, 0); // minimize chooser button | ||
246 | d->mainLayout->addWidget(d->addRemoveButton); | 118 | d->bottomLayout->setStretch(1, 1); // maximize color name cmb | ||
119 | | ||||
120 | d->mainLayout = new QVBoxLayout(this); | ||||
121 | d->mainLayout->setMargin(4); | ||||
122 | d->mainLayout->setSpacing(2); | ||||
123 | d->mainLayout->addLayout(d->recentsLayout); | ||||
124 | d->mainLayout->addWidget(d->paletteView); | ||||
125 | d->mainLayout->addLayout(d->bottomLayout); | ||||
247 | 126 | | |||
248 | setLayout(d->mainLayout); | 127 | setLayout(d->mainLayout); | ||
249 | 128 | | |||
250 | KoColorSet *colorSet = new KoColorSet(); | 129 | connect(d->paletteChooser, SIGNAL(sigPaletteSelected(KoColorSet*)), | ||
251 | d->colorSet = colorSet; | 130 | SLOT(slotPaletteChoosen(KoColorSet*))); | ||
252 | d->fillColors(); | 131 | connect(d->paletteView, SIGNAL(sigColorSelected(KoColor)), | ||
132 | SLOT(slotColorSelectedByPalette(KoColor))); | ||||
133 | connect(d->colorNameCmb, SIGNAL(sigColorSelected(KoColor)), | ||||
134 | SLOT(slotNameListSelection(KoColor))); | ||||
135 | | ||||
136 | d->rServer = KoResourceServerProvider::instance()->paletteServer(); | ||||
137 | QPointer<KoColorSet> defaultColorSet = d->rServer->resourceByName("Default"); | ||||
138 | if (!defaultColorSet && d->rServer->resources().count() > 0) { | ||||
139 | defaultColorSet = d->rServer->resources().first(); | ||||
140 | } | ||||
141 | setColorSet(defaultColorSet); | ||||
253 | } | 142 | } | ||
254 | 143 | | |||
255 | KoColorSetWidget::~KoColorSetWidget() | 144 | KoColorSetWidget::~KoColorSetWidget() | ||
256 | { | 145 | { | ||
257 | KoResourceServer<KoColorSet>* srv = KoResourceServerProvider::instance()->paletteServer(); | | |||
258 | QList<KoColorSet*> palettes = srv->resources(); | | |||
259 | if (!palettes.contains(d->colorSet)) { | | |||
260 | delete d->colorSet; | | |||
261 | } | | |||
262 | delete d; | 146 | delete d; | ||
263 | } | 147 | } | ||
264 | 148 | | |||
265 | void KoColorSetWidget::KoColorSetWidgetPrivate::colorTriggered(KoColorPatch *patch) | | |||
266 | { | | |||
267 | int i; | | |||
268 | | ||||
269 | emit thePublic->colorChanged(patch->color(), true); | | |||
270 | | ||||
271 | colorNameCmb->setCurrentIndex(colornames.indexOf(QRegExp(patch->toolTip()+"|Fixed"))); | | |||
272 | | ||||
273 | for (i = 0; i <numRecents; i++) | | |||
274 | if(patch == recentPatches[i]) { | | |||
275 | activateRecent(i); | | |||
276 | break; | | |||
277 | } | | |||
278 | | ||||
279 | if (i == numRecents) // we didn't find it above | | |||
280 | addRecent(patch->color()); | | |||
281 | } | | |||
282 | | ||||
283 | void KoColorSetWidget::KoColorSetWidgetPrivate::setColorFromString(QString s) | | |||
284 | { | | |||
285 | int i = colornames.indexOf(QRegExp(s+"|Fixed")); | | |||
286 | i = qMax(i,0); | | |||
287 | colorTriggered(patchWidgetList.at(i)); | | |||
288 | } | | |||
289 | | ||||
290 | void KoColorSetWidget::setColorSet(QPointer<KoColorSet> colorSet) | 149 | void KoColorSetWidget::setColorSet(QPointer<KoColorSet> colorSet) | ||
291 | { | 150 | { | ||
292 | if (!colorSet) return; | 151 | if (!colorSet) return; | ||
293 | if (colorSet == d->colorSet) return; | 152 | if (colorSet == d->colorSet) return; | ||
294 | 153 | | |||
295 | KoResourceServer<KoColorSet>* srv = KoResourceServerProvider::instance()->paletteServer(); | 154 | d->paletteView->paletteModel()->setPalette(colorSet.data()); | ||
296 | QList<KoColorSet*> palettes = srv->resources(); | | |||
297 | if (!palettes.contains(d->colorSet)) { | | |||
298 | delete d->colorSet; | | |||
299 | } | | |||
300 | | ||||
301 | d->colorSet = colorSet; | 155 | d->colorSet = colorSet; | ||
302 | d->fillColors(); | | |||
303 | } | 156 | } | ||
304 | 157 | | |||
305 | KoColorSet* KoColorSetWidget::colorSet() | 158 | KoColorSet* KoColorSetWidget::colorSet() | ||
Context not available. | |||||
311 | { | 164 | { | ||
312 | if (displayRenderer) { | 165 | if (displayRenderer) { | ||
313 | d->displayRenderer = displayRenderer; | 166 | d->displayRenderer = displayRenderer; | ||
314 | Q_FOREACH(KoColorPatch *p, d->patchWidgetList) { | | |||
315 | p->setDisplayRenderer(displayRenderer); | | |||
316 | } | | |||
317 | for (int i=0; i<6; i++) { | 167 | for (int i=0; i<6; i++) { | ||
318 | if (d->recentPatches[i]) { | 168 | if (d->recentPatches[i]) { | ||
319 | d->recentPatches[i]->setDisplayRenderer(displayRenderer); | 169 | d->recentPatches[i]->setDisplayRenderer(displayRenderer); | ||
Context not available. | |||||
328 | QFrame::resizeEvent(event); | 178 | QFrame::resizeEvent(event); | ||
329 | } | 179 | } | ||
330 | 180 | | |||
181 | void KoColorSetWidget::slotColorSelectedByPalette(const KoColor &color) | ||||
182 | { | ||||
183 | emit colorChanged(color, true); | ||||
184 | d->addRecent(color); | ||||
185 | } | ||||
186 | | ||||
187 | void KoColorSetWidget::slotPatchTriggered(KoColorPatch *patch) | ||||
188 | { | ||||
189 | emit colorChanged(patch->color(), true); | ||||
190 | | ||||
191 | int i; | ||||
192 | | ||||
193 | for (i = 0; i < d->numRecents; i++) { | ||||
194 | if(patch == d->recentPatches[i]) { | ||||
195 | d->activateRecent(i); | ||||
196 | break; | ||||
197 | } | ||||
198 | } | ||||
199 | | ||||
200 | if (i == d->numRecents) { // we didn't find it above | ||||
201 | d->addRecent(patch->color()); | ||||
202 | } | ||||
203 | } | ||||
204 | | ||||
205 | void KoColorSetWidget::slotPaletteChoosen(KoColorSet *colorSet) | ||||
206 | { | ||||
207 | d->colorSet = colorSet; | ||||
208 | d->paletteView->paletteModel()->setPalette(colorSet); | ||||
209 | } | ||||
210 | | ||||
211 | void KoColorSetWidget::slotNameListSelection(const KoColor &color) | ||||
212 | { | ||||
213 | emit colorChanged(color, true); | ||||
214 | } | ||||
215 | | ||||
331 | //have to include this because of Q_PRIVATE_SLOT | 216 | //have to include this because of Q_PRIVATE_SLOT | ||
332 | #include "moc_KoColorSetWidget.cpp" | 217 | #include "moc_KoColorSetWidget.cpp" | ||
333 | | ||||
Context not available. |