summaryrefslogtreecommitdiffstats
path: root/krita/ui/kis_paintop_box.cc
blob: 3479075d29e81a728220011c8d636728c7f07255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 *  kis_paintop_box.cc - part of KImageShop/Krayon/Krita
 *
 *  Copyright (c) 2004 Boudewijn Rempt (boud@valdyas.org)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include <qwidget.h>
#include <qstring.h>
#include <qvaluelist.h>
#include <qpixmap.h>
#include <qlayout.h>
#include <qtooltip.h>

#include <klocale.h>
#include <kactioncollection.h>
#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
#include <kglobalsettings.h>
#include <kaccelmanager.h>
#include <kconfig.h>
#include <kstandarddirs.h>

#include <kis_paintop_registry.h>
#include <kis_view.h>
#include <kis_painter.h>
#include <kis_paintop.h>
#include <kis_layer.h>
#include <kis_factory.h>

#include "kis_paintop_box.h"

KisPaintopBox::KisPaintopBox (KisView * view, QWidget *parent, const char * name)
    : super (parent, name),
      m_canvasController(view->getCanvasController())
{
#if KDE_VERSION >= KDE_MAKE_VERSION(3,3,90)
    KAcceleratorManager::setNoAccel(this);
#endif

    Q_ASSERT(m_canvasController != 0);

    setCaption(i18n("Painter's Toolchest"));
    m_optionWidget = 0;
    m_paintops = new QValueList<KisID>();
    m_displayedOps = new QValueList<KisID>();

    m_cmbPaintops = new QComboBox(this, "KisPaintopBox::m_cmbPaintops");
    m_cmbPaintops->setMinimumWidth(150);
    QToolTip::add(m_cmbPaintops, i18n("Styles of painting for the painting tools"));
    m_layout = new QHBoxLayout(this, 1, 1);
    m_layout->addWidget(m_cmbPaintops);

    connect(this, SIGNAL(selected(const KisID &, const KisPaintOpSettings *)), view, SLOT(paintopActivated(const KisID &, const KisPaintOpSettings *)));
    connect(m_cmbPaintops, SIGNAL(activated(int)), this, SLOT(slotItemSelected(int)));

    // XXX: Let's see... Are all paintops loaded and ready?
    KisIDList keys = KisPaintOpRegistry::instance()->listKeys();
    for ( KisIDList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
        // add all paintops, and show/hide them afterwards
        addItem(*it);
    }

    connect(view, SIGNAL(currentColorSpaceChanged(KisColorSpace*)),
            this, SLOT(colorSpaceChanged(KisColorSpace*)));
    connect(view, SIGNAL(sigInputDeviceChanged(const KisInputDevice&)),
            this, SLOT(slotInputDeviceChanged(const KisInputDevice&)));

    setCurrentPaintop(defaultPaintop(m_canvasController->currentInputDevice()));
}

KisPaintopBox::~KisPaintopBox()
{
    delete m_paintops;
    delete m_displayedOps;
}

void KisPaintopBox::addItem(const KisID & paintop, const QString & /*category*/)
{
    m_paintops->append(paintop);
}

void KisPaintopBox::slotItemSelected(int index)
{
    if ((uint)index > m_displayedOps->count()) {
        return;
    }

    KisID paintop = *m_displayedOps->at(index);

    setCurrentPaintop(paintop);
}

void KisPaintopBox::colorSpaceChanged(KisColorSpace *cs)
{
    QValueList<KisID>::iterator it = m_paintops->begin();
    QValueList<KisID>::iterator end = m_paintops->end();
    m_displayedOps->clear();
    m_cmbPaintops->clear();

    for ( ; it != end; ++it ) {
        if (KisPaintOpRegistry::instance()->userVisible(*it, cs)) {
            QPixmap pm = paintopPixmap(*it);
            if (pm.isNull()) {
                QPixmap p = QPixmap( 16, 16 );
                p.fill();
                m_cmbPaintops->insertItem(p,  (*it).name());
            }
            else {
                m_cmbPaintops->insertItem(pm, (*it).name());
            }
            m_displayedOps->append(*it);
        }
    }

    int index = m_displayedOps->findIndex(currentPaintop());

    if (index == -1) {
        // Must change the paintop as the current one is not supported
        // by the new colourspace.
        index = 0;
    }

    m_cmbPaintops->setCurrentItem( index );
    slotItemSelected( index );
}

QPixmap KisPaintopBox::paintopPixmap(const KisID & paintop)
{
    QString pixmapName = KisPaintOpRegistry::instance()->pixmap(paintop);

    if (pixmapName.isEmpty()) {
        return QPixmap();
    }

    QString fname = KisFactory::instance()->dirs()->findResource("kis_images", pixmapName);

    return QPixmap(fname);
}

void KisPaintopBox::slotInputDeviceChanged(const KisInputDevice & inputDevice)
{
    KisID paintop;
    InputDevicePaintopMap::iterator it = m_currentID.find(inputDevice);

    if (it == m_currentID.end()) {
        paintop = defaultPaintop(inputDevice);
    } else {
        paintop = (*it).second;
    }

    int index = m_displayedOps->findIndex(paintop);

    if (index == -1) {
        // Must change the paintop as the current one is not supported
        // by the new colourspace.
        index = 0;
        paintop = *m_displayedOps->at(index);
    }

    m_cmbPaintops->setCurrentItem(index);
    setCurrentPaintop(paintop);
}

void KisPaintopBox::updateOptionWidget()
{
    if (m_optionWidget != 0) {
        m_layout->remove(m_optionWidget);
        m_optionWidget->hide();
        m_layout->invalidate();
    }

    const KisPaintOpSettings *settings = paintopSettings(currentPaintop(), m_canvasController->currentInputDevice());

    if (settings != 0) {
        m_optionWidget = settings->widget();
        Q_ASSERT(m_optionWidget != 0);

        m_layout->addWidget(m_optionWidget);
        updateGeometry();
        m_optionWidget->show();
    }
}

const KisID& KisPaintopBox::currentPaintop()
{
    return m_currentID[m_canvasController->currentInputDevice()];
}

void KisPaintopBox::setCurrentPaintop(const KisID & paintop)
{
    m_currentID[m_canvasController->currentInputDevice()] = paintop;

    updateOptionWidget();

    emit selected(paintop, paintopSettings(paintop, m_canvasController->currentInputDevice()));
}

KisID KisPaintopBox::defaultPaintop(const KisInputDevice& inputDevice)
{
    if (inputDevice == KisInputDevice::eraser()) {
        return KisID("eraser","");
    } else {
        return KisID("paintbrush","");
    }
}

const KisPaintOpSettings *KisPaintopBox::paintopSettings(const KisID & paintop, const KisInputDevice & inputDevice)
{
    QValueVector<KisPaintOpSettings *> settingsArray;
    InputDevicePaintopSettingsMap::iterator it = m_inputDevicePaintopSettings.find(inputDevice);

    if (it == m_inputDevicePaintopSettings.end()) {
        // Create settings for each paintop.

        for (QValueList<KisID>::const_iterator pit = m_paintops->begin(); pit != m_paintops->end(); ++pit) {
            KisPaintOpSettings *settings = KisPaintOpRegistry::instance()->settings(*pit, this, inputDevice);
            settingsArray.append(settings);
            if (settings && settings->widget()) {
                settings->widget()->hide();
            }
        }
        m_inputDevicePaintopSettings[inputDevice] = settingsArray;
    } else {
        settingsArray = (*it).second;
    }

    const int index = m_paintops->findIndex(paintop);
    if (index >= 0 && index < (int)settingsArray.count())
        return settingsArray[index];
    else
        return 0;
}

#include "kis_paintop_box.moc"