krita

kis_paintop_box.cc

00001 /*
00002  *  kis_paintop_box.cc - part of KImageShop/Krayon/Krita
00003  *
00004  *  Copyright (c) 2004 Boudewijn Rempt (boud@valdyas.org)
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 #include <qwidget.h>
00021 #include <qstring.h>
00022 #include <qvaluelist.h>
00023 #include <qpixmap.h>
00024 #include <qlayout.h>
00025 
00026 #include <klocale.h>
00027 #include <kactioncollection.h>
00028 #include <kdebug.h>
00029 #include <kglobal.h>
00030 #include <klocale.h>
00031 #include <kglobalsettings.h>
00032 #include <kaccelmanager.h>
00033 #include <kconfig.h>
00034 #include <kstandarddirs.h>
00035 
00036 #include <kis_paintop_registry.h>
00037 #include <kis_view.h>
00038 #include <kis_painter.h>
00039 #include <kis_paintop.h>
00040 #include <kis_layer.h>
00041 #include <kis_factory.h>
00042 
00043 #include "kis_paintop_box.h"
00044 
00045 KisPaintopBox::KisPaintopBox (KisView * view, QWidget *parent, const char * name)
00046     : super (parent, name),
00047       m_canvasController(view->getCanvasController())
00048 {
00049 #if KDE_VERSION >= KDE_MAKE_VERSION(3,3,90)
00050     KAcceleratorManager::setNoAccel(this);
00051 #endif
00052 
00053     Q_ASSERT(m_canvasController != 0);
00054 
00055     setCaption(i18n("Painter's Toolchest"));
00056     m_optionWidget = 0;
00057     m_paintops = new QValueList<KisID>();
00058     m_displayedOps = new QValueList<KisID>();
00059 
00060     m_cmbPaintops = new QComboBox(this, "KisPaintopBox::m_cmbPaintops");
00061     m_cmbPaintops->setMinimumWidth(150);
00062     m_layout = new QHBoxLayout(this, 1, 1);
00063     m_layout->addWidget(m_cmbPaintops);
00064 
00065     connect(this, SIGNAL(selected(const KisID &, const KisPaintOpSettings *)), view, SLOT(paintopActivated(const KisID &, const KisPaintOpSettings *)));
00066     connect(m_cmbPaintops, SIGNAL(activated(int)), this, SLOT(slotItemSelected(int)));
00067 
00068     // XXX: Let's see... Are all paintops loaded and ready?
00069     KisIDList keys = KisPaintOpRegistry::instance()->listKeys();
00070     for ( KisIDList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
00071         // add all paintops, and show/hide them afterwards
00072         addItem(*it);
00073     }
00074 
00075     connect(view, SIGNAL(currentColorSpaceChanged(KisColorSpace*)),
00076             this, SLOT(colorSpaceChanged(KisColorSpace*)));
00077     connect(view, SIGNAL(sigInputDeviceChanged(const KisInputDevice&)),
00078             this, SLOT(slotInputDeviceChanged(const KisInputDevice&)));
00079 
00080     setCurrentPaintop(defaultPaintop(m_canvasController->currentInputDevice()));
00081 }
00082 
00083 KisPaintopBox::~KisPaintopBox()
00084 {
00085     delete m_paintops;
00086     delete m_displayedOps;
00087 }
00088 
00089 void KisPaintopBox::addItem(const KisID & paintop, const QString & /*category*/)
00090 {
00091     m_paintops->append(paintop);
00092 }
00093 
00094 void KisPaintopBox::slotItemSelected(int index)
00095 {
00096     if ((uint)index > m_displayedOps->count()) {
00097         return;
00098     }
00099 
00100     KisID paintop = *m_displayedOps->at(index);
00101 
00102     setCurrentPaintop(paintop);
00103 }
00104 
00105 void KisPaintopBox::colorSpaceChanged(KisColorSpace *cs)
00106 {
00107     QValueList<KisID>::iterator it = m_paintops->begin();
00108     QValueList<KisID>::iterator end = m_paintops->end();
00109     m_displayedOps->clear();
00110     m_cmbPaintops->clear();
00111 
00112     for ( ; it != end; ++it ) {
00113         if (KisPaintOpRegistry::instance()->userVisible(*it, cs)) {
00114             QPixmap pm = paintopPixmap(*it);
00115             if (pm.isNull()) {
00116                 QPixmap p = QPixmap( 16, 16 );
00117                 p.fill();
00118                 m_cmbPaintops->insertItem(p,  (*it).name());
00119             }
00120             else {
00121                 m_cmbPaintops->insertItem(pm, (*it).name());
00122             }
00123             m_displayedOps->append(*it);
00124         }
00125     }
00126 
00127     int index = m_displayedOps->findIndex(currentPaintop());
00128 
00129     if (index == -1) {
00130         // Must change the paintop as the current one is not supported
00131         // by the new colourspace.
00132         index = 0;
00133     }
00134 
00135     m_cmbPaintops->setCurrentItem( index );
00136     slotItemSelected( index );
00137 }
00138 
00139 QPixmap KisPaintopBox::paintopPixmap(const KisID & paintop)
00140 {
00141     QString pixmapName = KisPaintOpRegistry::instance()->pixmap(paintop);
00142 
00143     if (pixmapName.isEmpty()) {
00144         return QPixmap();
00145     }
00146 
00147     QString fname = KisFactory::instance()->dirs()->findResource("kis_images", pixmapName);
00148 
00149     return QPixmap(fname);
00150 }
00151 
00152 void KisPaintopBox::slotInputDeviceChanged(const KisInputDevice & inputDevice)
00153 {
00154     KisID paintop;
00155     InputDevicePaintopMap::iterator it = m_currentID.find(inputDevice);
00156 
00157     if (it == m_currentID.end()) {
00158         paintop = defaultPaintop(inputDevice);
00159     } else {
00160         paintop = (*it).second;
00161     }
00162 
00163     int index = m_displayedOps->findIndex(paintop);
00164 
00165     if (index == -1) {
00166         // Must change the paintop as the current one is not supported
00167         // by the new colourspace.
00168         index = 0;
00169         paintop = *m_displayedOps->at(index);
00170     }
00171 
00172     m_cmbPaintops->setCurrentItem(index);
00173     setCurrentPaintop(paintop);
00174 }
00175 
00176 void KisPaintopBox::updateOptionWidget()
00177 {
00178     if (m_optionWidget != 0) {
00179         m_layout->remove(m_optionWidget);
00180         m_optionWidget->hide();
00181         m_layout->invalidate();
00182     }
00183 
00184     const KisPaintOpSettings *settings = paintopSettings(currentPaintop(), m_canvasController->currentInputDevice());
00185 
00186     if (settings != 0) {
00187         m_optionWidget = settings->widget();
00188         Q_ASSERT(m_optionWidget != 0);
00189 
00190         m_layout->addWidget(m_optionWidget);
00191         updateGeometry();
00192         m_optionWidget->show();
00193     }
00194 }
00195 
00196 const KisID& KisPaintopBox::currentPaintop()
00197 {
00198     return m_currentID[m_canvasController->currentInputDevice()];
00199 }
00200 
00201 void KisPaintopBox::setCurrentPaintop(const KisID & paintop)
00202 {
00203     m_currentID[m_canvasController->currentInputDevice()] = paintop;
00204 
00205     updateOptionWidget();
00206 
00207     emit selected(paintop, paintopSettings(paintop, m_canvasController->currentInputDevice()));
00208 }
00209 
00210 KisID KisPaintopBox::defaultPaintop(const KisInputDevice& inputDevice)
00211 {
00212     if (inputDevice == KisInputDevice::eraser()) {
00213         return KisID("eraser","");
00214     } else {
00215         return KisID("paintbrush","");
00216     }
00217 }
00218 
00219 const KisPaintOpSettings *KisPaintopBox::paintopSettings(const KisID & paintop, const KisInputDevice & inputDevice)
00220 {
00221     QValueVector<KisPaintOpSettings *> settingsArray;
00222     InputDevicePaintopSettingsMap::iterator it = m_inputDevicePaintopSettings.find(inputDevice);
00223 
00224     if (it == m_inputDevicePaintopSettings.end()) {
00225         // Create settings for each paintop.
00226 
00227         for (QValueList<KisID>::const_iterator pit = m_paintops->begin(); pit != m_paintops->end(); ++pit) {
00228             KisPaintOpSettings *settings = KisPaintOpRegistry::instance()->settings(*pit, this, inputDevice);
00229             settingsArray.append(settings);
00230             if (settings && settings->widget()) {
00231                 settings->widget()->hide();
00232             }
00233         }
00234         m_inputDevicePaintopSettings[inputDevice] = settingsArray;
00235     } else {
00236         settingsArray = (*it).second;
00237     }
00238 
00239     const int index = m_paintops->findIndex(paintop);
00240     if (index >= 0 && index < (int)settingsArray.count())
00241         return settingsArray[index];
00242     else
00243         return 0;
00244 }
00245 
00246 #include "kis_paintop_box.moc"
00247 
KDE Home | KDE Accessibility Home | Description of Access Keys