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 #include <qtooltip.h>
00026 
00027 #include <klocale.h>
00028 #include <kactioncollection.h>
00029 #include <kdebug.h>
00030 #include <kglobal.h>
00031 #include <klocale.h>
00032 #include <kglobalsettings.h>
00033 #include <kaccelmanager.h>
00034 #include <kconfig.h>
00035 #include <kstandarddirs.h>
00036 
00037 #include <kis_paintop_registry.h>
00038 #include <kis_view.h>
00039 #include <kis_painter.h>
00040 #include <kis_paintop.h>
00041 #include <kis_layer.h>
00042 #include <kis_factory.h>
00043 
00044 #include "kis_paintop_box.h"
00045 
00046 KisPaintopBox::KisPaintopBox (KisView * view, QWidget *parent, const char * name)
00047     : super (parent, name),
00048       m_canvasController(view->getCanvasController())
00049 {
00050 #if KDE_VERSION >= KDE_MAKE_VERSION(3,3,90)
00051     KAcceleratorManager::setNoAccel(this);
00052 #endif
00053 
00054     Q_ASSERT(m_canvasController != 0);
00055 
00056     setCaption(i18n("Painter's Toolchest"));
00057     m_optionWidget = 0;
00058     m_paintops = new QValueList<KisID>();
00059     m_displayedOps = new QValueList<KisID>();
00060 
00061     m_cmbPaintops = new QComboBox(this, "KisPaintopBox::m_cmbPaintops");
00062     m_cmbPaintops->setMinimumWidth(150);
00063     QToolTip::add(m_cmbPaintops, i18n("Styles of painting for the painting tools"));
00064     m_layout = new QHBoxLayout(this, 1, 1);
00065     m_layout->addWidget(m_cmbPaintops);
00066 
00067     connect(this, SIGNAL(selected(const KisID &, const KisPaintOpSettings *)), view, SLOT(paintopActivated(const KisID &, const KisPaintOpSettings *)));
00068     connect(m_cmbPaintops, SIGNAL(activated(int)), this, SLOT(slotItemSelected(int)));
00069 
00070     // XXX: Let's see... Are all paintops loaded and ready?
00071     KisIDList keys = KisPaintOpRegistry::instance()->listKeys();
00072     for ( KisIDList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
00073         // add all paintops, and show/hide them afterwards
00074         addItem(*it);
00075     }
00076 
00077     connect(view, SIGNAL(currentColorSpaceChanged(KisColorSpace*)),
00078             this, SLOT(colorSpaceChanged(KisColorSpace*)));
00079     connect(view, SIGNAL(sigInputDeviceChanged(const KisInputDevice&)),
00080             this, SLOT(slotInputDeviceChanged(const KisInputDevice&)));
00081 
00082     setCurrentPaintop(defaultPaintop(m_canvasController->currentInputDevice()));
00083 }
00084 
00085 KisPaintopBox::~KisPaintopBox()
00086 {
00087     delete m_paintops;
00088     delete m_displayedOps;
00089 }
00090 
00091 void KisPaintopBox::addItem(const KisID & paintop, const QString & /*category*/)
00092 {
00093     m_paintops->append(paintop);
00094 }
00095 
00096 void KisPaintopBox::slotItemSelected(int index)
00097 {
00098     if ((uint)index > m_displayedOps->count()) {
00099         return;
00100     }
00101 
00102     KisID paintop = *m_displayedOps->at(index);
00103 
00104     setCurrentPaintop(paintop);
00105 }
00106 
00107 void KisPaintopBox::colorSpaceChanged(KisColorSpace *cs)
00108 {
00109     QValueList<KisID>::iterator it = m_paintops->begin();
00110     QValueList<KisID>::iterator end = m_paintops->end();
00111     m_displayedOps->clear();
00112     m_cmbPaintops->clear();
00113 
00114     for ( ; it != end; ++it ) {
00115         if (KisPaintOpRegistry::instance()->userVisible(*it, cs)) {
00116             QPixmap pm = paintopPixmap(*it);
00117             if (pm.isNull()) {
00118                 QPixmap p = QPixmap( 16, 16 );
00119                 p.fill();
00120                 m_cmbPaintops->insertItem(p,  (*it).name());
00121             }
00122             else {
00123                 m_cmbPaintops->insertItem(pm, (*it).name());
00124             }
00125             m_displayedOps->append(*it);
00126         }
00127     }
00128 
00129     int index = m_displayedOps->findIndex(currentPaintop());
00130 
00131     if (index == -1) {
00132         // Must change the paintop as the current one is not supported
00133         // by the new colourspace.
00134         index = 0;
00135     }
00136 
00137     m_cmbPaintops->setCurrentItem( index );
00138     slotItemSelected( index );
00139 }
00140 
00141 QPixmap KisPaintopBox::paintopPixmap(const KisID & paintop)
00142 {
00143     QString pixmapName = KisPaintOpRegistry::instance()->pixmap(paintop);
00144 
00145     if (pixmapName.isEmpty()) {
00146         return QPixmap();
00147     }
00148 
00149     QString fname = KisFactory::instance()->dirs()->findResource("kis_images", pixmapName);
00150 
00151     return QPixmap(fname);
00152 }
00153 
00154 void KisPaintopBox::slotInputDeviceChanged(const KisInputDevice & inputDevice)
00155 {
00156     KisID paintop;
00157     InputDevicePaintopMap::iterator it = m_currentID.find(inputDevice);
00158 
00159     if (it == m_currentID.end()) {
00160         paintop = defaultPaintop(inputDevice);
00161     } else {
00162         paintop = (*it).second;
00163     }
00164 
00165     int index = m_displayedOps->findIndex(paintop);
00166 
00167     if (index == -1) {
00168         // Must change the paintop as the current one is not supported
00169         // by the new colourspace.
00170         index = 0;
00171         paintop = *m_displayedOps->at(index);
00172     }
00173 
00174     m_cmbPaintops->setCurrentItem(index);
00175     setCurrentPaintop(paintop);
00176 }
00177 
00178 void KisPaintopBox::updateOptionWidget()
00179 {
00180     if (m_optionWidget != 0) {
00181         m_layout->remove(m_optionWidget);
00182         m_optionWidget->hide();
00183         m_layout->invalidate();
00184     }
00185 
00186     const KisPaintOpSettings *settings = paintopSettings(currentPaintop(), m_canvasController->currentInputDevice());
00187 
00188     if (settings != 0) {
00189         m_optionWidget = settings->widget();
00190         Q_ASSERT(m_optionWidget != 0);
00191 
00192         m_layout->addWidget(m_optionWidget);
00193         updateGeometry();
00194         m_optionWidget->show();
00195     }
00196 }
00197 
00198 const KisID& KisPaintopBox::currentPaintop()
00199 {
00200     return m_currentID[m_canvasController->currentInputDevice()];
00201 }
00202 
00203 void KisPaintopBox::setCurrentPaintop(const KisID & paintop)
00204 {
00205     m_currentID[m_canvasController->currentInputDevice()] = paintop;
00206 
00207     updateOptionWidget();
00208 
00209     emit selected(paintop, paintopSettings(paintop, m_canvasController->currentInputDevice()));
00210 }
00211 
00212 KisID KisPaintopBox::defaultPaintop(const KisInputDevice& inputDevice)
00213 {
00214     if (inputDevice == KisInputDevice::eraser()) {
00215         return KisID("eraser","");
00216     } else {
00217         return KisID("paintbrush","");
00218     }
00219 }
00220 
00221 const KisPaintOpSettings *KisPaintopBox::paintopSettings(const KisID & paintop, const KisInputDevice & inputDevice)
00222 {
00223     QValueVector<KisPaintOpSettings *> settingsArray;
00224     InputDevicePaintopSettingsMap::iterator it = m_inputDevicePaintopSettings.find(inputDevice);
00225 
00226     if (it == m_inputDevicePaintopSettings.end()) {
00227         // Create settings for each paintop.
00228 
00229         for (QValueList<KisID>::const_iterator pit = m_paintops->begin(); pit != m_paintops->end(); ++pit) {
00230             KisPaintOpSettings *settings = KisPaintOpRegistry::instance()->settings(*pit, this, inputDevice);
00231             settingsArray.append(settings);
00232             if (settings && settings->widget()) {
00233                 settings->widget()->hide();
00234             }
00235         }
00236         m_inputDevicePaintopSettings[inputDevice] = settingsArray;
00237     } else {
00238         settingsArray = (*it).second;
00239     }
00240 
00241     const int index = m_paintops->findIndex(paintop);
00242     if (index >= 0 && index < (int)settingsArray.count())
00243         return settingsArray[index];
00244     else
00245         return 0;
00246 }
00247 
00248 #include "kis_paintop_box.moc"
00249 
KDE Home | KDE Accessibility Home | Description of Access Keys