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#if TDE_VERSION >= TDE_MAKE_VERSION(3,3,90)
51 TDEAcceleratorManager::setNoAccel(this);
52#endif
53
54 Q_ASSERT(m_canvasController != 0);
55
56 setCaption(i18n("Painter's Toolchest"));
57 m_optionWidget = 0;
58 m_paintops = new TQValueList<KisID>();
59 m_displayedOps = new TQValueList<KisID>();
60
61 m_cmbPaintops = new TQComboBox(this, "KisPaintopBox::m_cmbPaintops");
62 m_cmbPaintops->setMinimumWidth(150);
63 TQToolTip::add(m_cmbPaintops, i18n("Styles of painting for the painting tools"));
64 m_layout = new TQHBoxLayout(this, 1, 1);
65 m_layout->addWidget(m_cmbPaintops);
66
67 connect(this, TQ_SIGNAL(selected(const KisID &, const KisPaintOpSettings *)), view, TQ_SLOT(paintopActivated(const KisID &, const KisPaintOpSettings *)));
68 connect(m_cmbPaintops, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotItemSelected(int)));
69
70 // XXX: Let's see... Are all paintops loaded and ready?
71 KisIDList keys = KisPaintOpRegistry::instance()->listKeys();
72 for ( KisIDList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
73 // add all paintops, and show/hide them afterwards
74 addItem(*it);
75 }
76
77 connect(view, TQ_SIGNAL(currentColorSpaceChanged(KisColorSpace*)),
78 this, TQ_SLOT(colorSpaceChanged(KisColorSpace*)));
79 connect(view, TQ_SIGNAL(sigInputDeviceChanged(const KisInputDevice&)),
80 this, TQ_SLOT(slotInputDeviceChanged(const KisInputDevice&)));
81
82 setCurrentPaintop(defaultPaintop(m_canvasController->currentInputDevice()));
83}
84
85KisPaintopBox::~KisPaintopBox()
86{
87 delete m_paintops;
88 delete m_displayedOps;
89}
90
91void KisPaintopBox::addItem(const KisID & paintop, const TQString & /*category*/)
92{
93 m_paintops->append(paintop);
94}
95
96void KisPaintopBox::slotItemSelected(int index)
97{
98 if ((uint)index > m_displayedOps->count()) {
99 return;
100 }
101
102 KisID paintop = *m_displayedOps->at(index);
103
104 setCurrentPaintop(paintop);
105}
106
107void KisPaintopBox::colorSpaceChanged(KisColorSpace *cs)
108{
109 TQValueList<KisID>::iterator it = m_paintops->begin();
110 TQValueList<KisID>::iterator end = m_paintops->end();
111 m_displayedOps->clear();
112 m_cmbPaintops->clear();
113
114 for ( ; it != end; ++it ) {
115 if (KisPaintOpRegistry::instance()->userVisible(*it, cs)) {
116 TQPixmap pm = paintopPixmap(*it);
117 if (pm.isNull()) {
118 TQPixmap p = TQPixmap( 16, 16 );
119 p.fill();
120 m_cmbPaintops->insertItem(p, (*it).name());
121 }
122 else {
123 m_cmbPaintops->insertItem(pm, (*it).name());
124 }
125 m_displayedOps->append(*it);
126 }
127 }
128
129 int index = m_displayedOps->findIndex(currentPaintop());
130
131 if (index == -1) {
132 // Must change the paintop as the current one is not supported
133 // by the new colourspace.
134 index = 0;
135 }
136
137 m_cmbPaintops->setCurrentItem( index );
138 slotItemSelected( index );
139}
140
141TQPixmap KisPaintopBox::paintopPixmap(const KisID & paintop)
142{
143 TQString pixmapName = KisPaintOpRegistry::instance()->pixmap(paintop);
144
145 if (pixmapName.isEmpty()) {
146 return TQPixmap();
147 }
148
149 TQString fname = KisFactory::instance()->dirs()->findResource("kis_images", pixmapName);
150
151 return TQPixmap(fname);
152}
153
154void KisPaintopBox::slotInputDeviceChanged(const KisInputDevice & inputDevice)
155{
156 KisID paintop;
157 InputDevicePaintopMap::iterator it = m_currentID.find(inputDevice);
158
159 if (it == m_currentID.end()) {
160 paintop = defaultPaintop(inputDevice);
161 } else {
162 paintop = (*it).second;
163 }
164
165 int index = m_displayedOps->findIndex(paintop);
166
167 if (index == -1) {
168 // Must change the paintop as the current one is not supported
169 // by the new colourspace.
170 index = 0;
171 paintop = *m_displayedOps->at(index);
172 }
173
174 m_cmbPaintops->setCurrentItem(index);
175 setCurrentPaintop(paintop);
176}
177
178void KisPaintopBox::updateOptionWidget()
179{
180 if (m_optionWidget != 0) {
181 m_layout->remove(m_optionWidget);
182 m_optionWidget->hide();
183 m_layout->invalidate();
184 }
185
186 const KisPaintOpSettings *settings = paintopSettings(currentPaintop(), m_canvasController->currentInputDevice());
187
188 if (settings != 0) {
189 m_optionWidget = settings->widget();
190 Q_ASSERT(m_optionWidget != 0);
191
192 m_layout->addWidget(m_optionWidget);
193 updateGeometry();
194 m_optionWidget->show();
195 }
196}
197
198const KisID& KisPaintopBox::currentPaintop()
199{
200 return m_currentID[m_canvasController->currentInputDevice()];
201}
202
203void KisPaintopBox::setCurrentPaintop(const KisID & paintop)
204{
205 m_currentID[m_canvasController->currentInputDevice()] = paintop;
206
207 updateOptionWidget();
208
209 emit selected(paintop, paintopSettings(paintop, m_canvasController->currentInputDevice()));
210}
211
212KisID KisPaintopBox::defaultPaintop(const KisInputDevice& inputDevice)
213{
214 if (inputDevice == KisInputDevice::eraser()) {
215 return KisID("eraser","");
216 } else {
217 return KisID("paintbrush","");
218 }
219}
220
221const KisPaintOpSettings *KisPaintopBox::paintopSettings(const KisID & paintop, const KisInputDevice & inputDevice)
222{
223 TQValueVector<KisPaintOpSettings *> settingsArray;
224 InputDevicePaintopSettingsMap::iterator it = m_inputDevicePaintopSettings.find(inputDevice);
225
226 if (it == m_inputDevicePaintopSettings.end()) {
227 // Create settings for each paintop.
228
229 for (TQValueList<KisID>::const_iterator pit = m_paintops->begin(); pit != m_paintops->end(); ++pit) {
230 KisPaintOpSettings *settings = KisPaintOpRegistry::instance()->settings(*pit, this, inputDevice);
231 settingsArray.append(settings);
232 if (settings && settings->widget()) {
233 settings->widget()->hide();
234 }
235 }
236 m_inputDevicePaintopSettings[inputDevice] = settingsArray;
237 } else {
238 settingsArray = (*it).second;
239 }
240
241 const int index = m_paintops->findIndex(paintop);
242 if (index >= 0 && index < (int)settingsArray.count())
243 return settingsArray[index];
244 else
245 return 0;
246}
247
248#include "kis_paintop_box.moc"
249
virtual KisInputDevice currentInputDevice() const =0
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