~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to krita/ui/kis_paintop_box.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-04-20 21:38:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060420213853-j5lxluqvymxt2zny
Tags: 1:1.5.0-0ubuntu2
UbuntuĀ uploadĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  kis_paintop_box.cc - part of KImageShop/Krayon/Krita
 
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 <qwidget.h>
 
21
#include <qstring.h>
 
22
#include <qvaluelist.h>
 
23
#include <qpixmap.h>
 
24
#include <qlayout.h>
 
25
 
 
26
#include <klocale.h>
 
27
#include <kactioncollection.h>
 
28
#include <kdebug.h>
 
29
#include <kglobal.h>
 
30
#include <klocale.h>
 
31
#include <kglobalsettings.h>
 
32
#include <kaccelmanager.h>
 
33
#include <kconfig.h>
 
34
#include <kstandarddirs.h>
 
35
 
 
36
#include <kis_paintop_registry.h>
 
37
#include <kis_view.h>
 
38
#include <kis_painter.h>
 
39
#include <kis_paintop.h>
 
40
#include <kis_layer.h>
 
41
#include <kis_factory.h>
 
42
 
 
43
#include "kis_paintop_box.h"
 
44
 
 
45
KisPaintopBox::KisPaintopBox (KisView * view, QWidget *parent, const char * name)
 
46
    : super (parent, name),
 
47
      m_canvasController(view->getCanvasController())
 
48
{
 
49
#if KDE_VERSION >= KDE_MAKE_VERSION(3,3,90)
 
50
    KAcceleratorManager::setNoAccel(this);
 
51
#endif
 
52
 
 
53
    Q_ASSERT(m_canvasController != 0);
 
54
 
 
55
    setCaption(i18n("Painter's Toolchest"));
 
56
    m_optionWidget = 0;
 
57
    m_paintops = new QValueList<KisID>();
 
58
    m_displayedOps = new QValueList<KisID>();
 
59
 
 
60
    m_cmbPaintops = new QComboBox(this, "KisPaintopBox::m_cmbPaintops");
 
61
    m_cmbPaintops->setMinimumWidth(150);
 
62
    m_layout = new QHBoxLayout(this, 1, 1);
 
63
    m_layout->addWidget(m_cmbPaintops);
 
64
 
 
65
    connect(this, SIGNAL(selected(const KisID &, const KisPaintOpSettings *)), view, SLOT(paintopActivated(const KisID &, const KisPaintOpSettings *)));
 
66
    connect(m_cmbPaintops, SIGNAL(activated(int)), this, 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, SIGNAL(currentColorSpaceChanged(KisColorSpace*)),
 
76
            this, SLOT(colorSpaceChanged(KisColorSpace*)));
 
77
    connect(view, SIGNAL(sigInputDeviceChanged(const KisInputDevice&)),
 
78
            this, SLOT(slotInputDeviceChanged(const KisInputDevice&)));
 
79
 
 
80
    setCurrentPaintop(defaultPaintop(m_canvasController->currentInputDevice()));
 
81
}
 
82
 
 
83
KisPaintopBox::~KisPaintopBox()
 
84
{
 
85
    delete m_paintops;
 
86
    delete m_displayedOps;
 
87
}
 
88
 
 
89
void KisPaintopBox::addItem(const KisID & paintop, const QString & /*category*/)
 
90
{
 
91
    m_paintops->append(paintop);
 
92
}
 
93
 
 
94
void 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
 
 
105
void KisPaintopBox::colorSpaceChanged(KisColorSpace *cs)
 
106
{
 
107
    QValueList<KisID>::iterator it = m_paintops->begin();
 
108
    QValueList<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
            QPixmap pm = paintopPixmap(*it);
 
115
            if (pm.isNull()) {
 
116
                QPixmap p = QPixmap( 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
 
 
139
QPixmap KisPaintopBox::paintopPixmap(const KisID & paintop)
 
140
{
 
141
    QString pixmapName = KisPaintOpRegistry::instance()->pixmap(paintop);
 
142
 
 
143
    if (pixmapName.isEmpty()) {
 
144
        return QPixmap();
 
145
    }
 
146
 
 
147
    QString fname = KisFactory::instance()->dirs()->findResource("kis_images", pixmapName);
 
148
 
 
149
    return QPixmap(fname);
 
150
}
 
151
 
 
152
void 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
 
 
176
void 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
 
 
196
const KisID& KisPaintopBox::currentPaintop()
 
197
{
 
198
    return m_currentID[m_canvasController->currentInputDevice()];
 
199
}
 
200
 
 
201
void 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
 
 
210
KisID KisPaintopBox::defaultPaintop(const KisInputDevice& inputDevice)
 
211
{
 
212
    if (inputDevice == KisInputDevice::eraser()) {
 
213
        return KisID("eraser","");
 
214
    } else {
 
215
        return KisID("paintbrush","");
 
216
    }
 
217
}
 
218
 
 
219
const KisPaintOpSettings *KisPaintopBox::paintopSettings(const KisID & paintop, const KisInputDevice & inputDevice)
 
220
{
 
221
    QValueVector<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 (QValueList<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