chalk

kis_custom_palette.cpp
1/*
2 * Copyright (c) 2005 Bart Coppens <kde@bartcoppens.be>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include <KoImageResource.h>
20#include <kdebug.h>
21#include <tqlineedit.h>
22#include <tqimage.h>
23#include <tqpushbutton.h>
24#include <tqregexp.h>
25#include <tqvalidator.h>
26
27#include <tdeglobal.h>
28#include <kstandarddirs.h>
29#include <tdetempfile.h>
30#include <kcolordialog.h>
31#include <kinputdialog.h>
32#include <tdelocale.h>
33#include <tdemessagebox.h>
34
35#include "kis_view.h"
36#include "kis_palette.h"
37#include "kis_palette_view.h"
38#include "kis_custom_palette.h"
39#include "kis_resource_mediator.h"
40#include "kis_resourceserver.h"
41
42KisCustomPalette::KisCustomPalette(TQWidget *parent, const char* name, const TQString& caption, KisView* view)
43 : KisWdgCustomPalette(parent, name), m_view(view)
44{
45 Q_ASSERT(m_view);
46 m_mediator = 0;
47 m_server = 0;
48 m_editMode = false;
49 setCaption(caption);
50
51 m_palette = new KisPalette();
52 m_ownPalette = true;
53 this->view->setPalette(m_palette);
54
55 connect(addColor, TQ_SIGNAL(pressed()), this, TQ_SLOT(slotAddNew()));
56 connect(removeColor, TQ_SIGNAL(pressed()), this, TQ_SLOT(slotRemoveCurrent()));
57 connect(addPalette, TQ_SIGNAL(pressed()), this, TQ_SLOT(slotAddPredefined()));
58}
59
60KisCustomPalette::~KisCustomPalette() {
61 if (m_ownPalette)
62 delete m_palette;
63}
64
65void KisCustomPalette::setPalette(KisPalette* p) {
66 if (m_ownPalette)
67 delete m_palette;
68 m_ownPalette = false;
69 m_palette = p;
70 view->setPalette(m_palette);
71}
72
73void KisCustomPalette::setEditMode(bool b) {
74 m_editMode = b;
75
76 if (m_editMode) {
77 addPalette->setText(i18n("Save changes"));
78 } else {
79 addPalette->setText(i18n("Add to Predefined Palettes"));
80 }
81}
82
83void KisCustomPalette::slotAddNew() {
84 // Let the user select a new color
85 // FIXME also let him add the current paint color to the palette
86 // or even better, let the color picker have an option 'Add to palette'!
87
88 TQColor color;
89 int result = KColorDialog::getColor(color);
90 if (result != KColorDialog::Accepted)
91 return;
92
93 bool ok;
94 TQRegExpValidator validator(TQRegExp(".*"), this);
95 TQString name = KInputDialog::getText(i18n("Add Color to Palette"),
96 i18n("Color name (optional):"),
97 TQString(), &ok,
98 0, 0, &validator);
99 if (!ok)
100 return;
101
102 KisPaletteEntry entry;
103 entry.color = color;
104 entry.name = name;
105
106 m_palette->add(entry);
107
108 // Just reload the palette completely for the view updating
109 view->setPalette(m_palette);
110}
111
112void KisCustomPalette::slotRemoveCurrent() {
113 m_palette->remove(view->currentEntry());
114 // Just reload the palette completely for the view updating
115 view->setPalette(m_palette);
116}
117
118void KisCustomPalette::slotAddPredefined() {
119 m_palette->setName(palettename->text());
120
121 if (!m_editMode) {
122 // Save in the directory that is likely to be: ~/.trinity/share/apps/chalk/palettes
123 // a unique file with this palettename
124 TQString dir = TDEGlobal::dirs()->saveLocation("data", "chalk/palettes");
125 TQString extension;
126
127 extension = ".gpl";
128 KTempFile file(dir, extension);
129 file.close(); // If we don't, and palette->save first, it might get truncated!
130
131 // Save it to that file
132 m_palette->setFilename(file.name());
133 } else {
134 // The filename is already set
135 }
136
137 if (!m_palette->save()) {
138 KMessageBox::error(0, i18n("Cannot write to palette file %1. Maybe it is read-only.")
139 .arg(m_palette->filename()), i18n("Palette"));
140 return;
141 }
142
143 // Add it to the palette server, so that it automatically gets to the mediators, and
144 // so to the other choosers can pick it up, if they want to
145 // This probably leaks!
146 if (m_server)
147 m_server->addResource(new KisPalette(*m_palette));
148}
149
150
151#include "kis_custom_palette.moc"
Open Gimp, Photoshop or RIFF palette files.
Definition kis_palette.h:50