chalk

kis_paintop_box.cpp
1/*
2 * kis_paintop_box.cpp - part of KImageShop/Krayon/Chalk
3 *
4 * Copyright (c) 2004 Boudewijn Rempt (boud@valdyas.org)
5 *
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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 this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include <tqwidget.h>
21#include <tqstring.h>
22#include <tqvaluelist.h>
23#include <tqpixmap.h>
24#include <tqlayout.h>
25#include <tqtooltip.h>
26
27#include <tdelocale.h>
28#include <tdeactioncollection.h>
29#include <kdebug.h>
30#include <tdeglobal.h>
31#include <tdelocale.h>
32#include <tdeglobalsettings.h>
33#include <tdeaccelmanager.h>
34#include <tdeconfig.h>
35#include <kstandarddirs.h>
36
37#include <kis_paintop_registry.h>
38#include <kis_view.h>
39#include <kis_painter.h>
40#include <kis_paintop.h>
41#include <kis_layer.h>
42#include <kis_factory.h>
43
44#include "kis_paintop_box.h"
45
46KisPaintopBox::KisPaintopBox (KisView * view, TQWidget *parent, const char * name)
47 : super (parent, name),
48 m_canvasController(view->getCanvasController())
49{
50 TDEAcceleratorManager::setNoAccel(this);
51
52 Q_ASSERT(m_canvasController != 0);
53
54 setCaption(i18n("Painter's Toolchest"));
55 m_optionWidget = 0;
56 m_paintops = new TQValueList<KisID>();
57 m_displayedOps = new TQValueList<KisID>();
58
59 m_cmbPaintops = new TQComboBox(this, "KisPaintopBox::m_cmbPaintops");
60 m_cmbPaintops->setMinimumWidth(150);
61 TQToolTip::add(m_cmbPaintops, i18n("Styles of painting for the painting tools"));
62 m_layout = new TQHBoxLayout(this, 1, 1);
63 m_layout->addWidget(m_cmbPaintops);
64
65 connect(this, TQ_SIGNAL(selected(const KisID &, const KisPaintOpSettings *)), view, TQ_SLOT(paintopActivated(const KisID &, const KisPaintOpSettings *)));
66 connect(m_cmbPaintops, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotItemSelected(int)));
67
68 // XXX: Let's see... Are all paintops loaded and ready?
69 KisIDList keys = KisPaintOpRegistry::instance()->listKeys();
70 for ( KisIDList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
71 // add all paintops, and show/hide them afterwards
72 addItem(*it);
73 }
74
75 connect(view, TQ_SIGNAL(currentColorSpaceChanged(KisColorSpace*)),
76 this, TQ_SLOT(colorSpaceChanged(KisColorSpace*)));
77 connect(view, TQ_SIGNAL(sigInputDeviceChanged(const KisInputDevice&)),
78 this, TQ_SLOT(slotInputDeviceChanged(const KisInputDevice&)));
79
80 setCurrentPaintop(defaultPaintop(m_canvasController->currentInputDevice()));
81}
82
83KisPaintopBox::~KisPaintopBox()
84{
85 delete m_paintops;
86 delete m_displayedOps;
87}
88
89void KisPaintopBox::addItem(const KisID & paintop, const TQString & /*category*/)
90{
91 m_paintops->append(paintop);
92}
93
94void KisPaintopBox::slotItemSelected(int index)
95{
96 if ((uint)index > m_displayedOps->count()) {
97 return;
98 }
99
100 KisID paintop = *m_displayedOps->at(index);
101
102 setCurrentPaintop(paintop);
103}
104
105void KisPaintopBox::colorSpaceChanged(KisColorSpace *cs)
106{
107 TQValueList<KisID>::iterator it = m_paintops->begin();
108 TQValueList<KisID>::iterator end = m_paintops->end();
109 m_displayedOps->clear();
110 m_cmbPaintops->clear();
111
112 for ( ; it != end; ++it ) {
113 if (KisPaintOpRegistry::instance()->userVisible(*it, cs)) {
114 TQPixmap pm = paintopPixmap(*it);
115 if (pm.isNull()) {
116 TQPixmap p = TQPixmap( 16, 16 );
117 p.fill();
118 m_cmbPaintops->insertItem(p, (*it).name());
119 }
120 else {
121 m_cmbPaintops->insertItem(pm, (*it).name());
122 }
123 m_displayedOps->append(*it);
124 }
125 }
126
127 int index = m_displayedOps->findIndex(currentPaintop());
128
129 if (index == -1) {
130 // Must change the paintop as the current one is not supported
131 // by the new colourspace.
132 index = 0;
133 }
134
135 m_cmbPaintops->setCurrentItem( index );
136 slotItemSelected( index );
137}
138
139TQPixmap KisPaintopBox::paintopPixmap(const KisID & paintop)
140{
141 TQString pixmapName = KisPaintOpRegistry::instance()->pixmap(paintop);
142
143 if (pixmapName.isEmpty()) {
144 return TQPixmap();
145 }
146
147 TQString fname = KisFactory::instance()->dirs()->findResource("kis_images", pixmapName);
148
149 return TQPixmap(fname);
150}
151
152void KisPaintopBox::slotInputDeviceChanged(const KisInputDevice & inputDevice)
153{
154 KisID paintop;
155 InputDevicePaintopMap::iterator it = m_currentID.find(inputDevice);
156
157 if (it == m_currentID.end()) {
158 paintop = defaultPaintop(inputDevice);
159 } else {
160 paintop = (*it).second;
161 }
162
163 int index = m_displayedOps->findIndex(paintop);
164
165 if (index == -1) {
166 // Must change the paintop as the current one is not supported
167 // by the new colourspace.
168 index = 0;
169 paintop = *m_displayedOps->at(index);
170 }
171
172 m_cmbPaintops->setCurrentItem(index);
173 setCurrentPaintop(paintop);
174}
175
176void KisPaintopBox::updateOptionWidget()
177{
178 if (m_optionWidget != 0) {
179 m_layout->remove(m_optionWidget);
180 m_optionWidget->hide();
181 m_layout->invalidate();
182 }
183
184 const KisPaintOpSettings *settings = paintopSettings(currentPaintop(), m_canvasController->currentInputDevice());
185
186 if (settings != 0) {
187 m_optionWidget = settings->widget();
188 Q_ASSERT(m_optionWidget != 0);
189
190 m_layout->addWidget(m_optionWidget);
191 updateGeometry();
192 m_optionWidget->show();
193 }
194}
195
196const KisID& KisPaintopBox::currentPaintop()
197{
198 return m_currentID[m_canvasController->currentInputDevice()];
199}
200
201void KisPaintopBox::setCurrentPaintop(const KisID & paintop)
202{
203 m_currentID[m_canvasController->currentInputDevice()] = paintop;
204
205 updateOptionWidget();
206
207 emit selected(paintop, paintopSettings(paintop, m_canvasController->currentInputDevice()));
208}
209
210KisID KisPaintopBox::defaultPaintop(const KisInputDevice& inputDevice)
211{
212 if (inputDevice == KisInputDevice::eraser()) {
213 return KisID("eraser","");
214 } else {
215 return KisID("paintbrush","");
216 }
217}
218
219const KisPaintOpSettings *KisPaintopBox::paintopSettings(const KisID & paintop, const KisInputDevice & inputDevice)
220{
221 TQValueVector<KisPaintOpSettings *> settingsArray;
222 InputDevicePaintopSettingsMap::iterator it = m_inputDevicePaintopSettings.find(inputDevice);
223
224 if (it == m_inputDevicePaintopSettings.end()) {
225 // Create settings for each paintop.
226
227 for (TQValueList<KisID>::const_iterator pit = m_paintops->begin(); pit != m_paintops->end(); ++pit) {
228 KisPaintOpSettings *settings = KisPaintOpRegistry::instance()->settings(*pit, this, inputDevice);
229 settingsArray.append(settings);
230 if (settings && settings->widget()) {
231 settings->widget()->hide();
232 }
233 }
234 m_inputDevicePaintopSettings[inputDevice] = settingsArray;
235 } else {
236 settingsArray = (*it).second;
237 }
238
239 const int index = m_paintops->findIndex(paintop);
240 if (index >= 0 && index < (int)settingsArray.count())
241 return settingsArray[index];
242 else
243 return 0;
244}
245
246#include "kis_paintop_box.moc"
247
A colorspace is the definition of a certain color model in Chalk.
Chalk has a large number of extensible resources.
Definition kis_id.h:57