krita

kis_palette_widget.cc

00001 /*
00002  *  Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 
00022 #include <qcheckbox.h>
00023 #include <qcombobox.h>
00024 #include <qdrawutil.h>
00025 #include <qevent.h>
00026 #include <qfile.h>
00027 #include <qimage.h>
00028 #include <qlabel.h>
00029 #include <qlayout.h>
00030 #include <qlineedit.h>
00031 #include <qvalidator.h>
00032 #include <qpainter.h>
00033 #include <qpushbutton.h>
00034 #include <qspinbox.h>
00035 #include <qtimer.h>
00036 
00037 #include <kapplication.h>
00038 #include <kconfig.h>
00039 #include <kglobal.h>
00040 #include <kglobalsettings.h>
00041 #include <kiconloader.h>
00042 #include <klistbox.h>
00043 #include <klocale.h>
00044 #include <kmessagebox.h>
00045 #include <kseparator.h>
00046 #include <kpalette.h>
00047 #include <kimageeffect.h>
00048 
00049 #include <kcolordialog.h>
00050 #include <kcolordrag.h>
00051 #include <config.h>
00052 #include <kdebug.h>
00053 
00054 #include <kis_meta_registry.h>
00055 #include <kis_color.h>
00056 #include <kis_factory.h>
00057 #include <kis_colorspace_factory_registry.h>
00058 #include "kis_palette_widget.h"
00059 #include "kis_resource.h"
00060 #include "kis_palette.h"
00061 #include "kis_palette_view.h"
00062 
00063 KisPaletteWidget::KisPaletteWidget( QWidget *parent, int minWidth, int cols)
00064     : QWidget( parent ), mMinWidth(minWidth), mCols(cols)
00065 {
00066     init = false;
00067 
00068     m_currentPalette = 0;
00069 
00070     QVBoxLayout *layout = new QVBoxLayout( this );
00071 
00072     combo = new QComboBox( false, this );
00073     combo->setFocusPolicy( QWidget::ClickFocus );
00074     layout->addWidget(combo);
00075 
00076     m_view = new KisPaletteView(this, 0, minWidth, cols);
00077     layout->addWidget( m_view );
00078 
00079     //setFixedSize(sizeHint());
00080 
00081     connect(combo, SIGNAL(activated(const QString &)),
00082             this, SLOT(slotSetPalette(const QString &)));
00083     connect(m_view, SIGNAL(colorSelected(const KisColor &)),
00084             this, SIGNAL(colorSelected(const KisColor &)));
00085     connect(m_view, SIGNAL(colorSelected(const QColor &)),
00086             this, SIGNAL(colorSelected(const QColor &)));
00087     connect(m_view, SIGNAL(colorDoubleClicked(const KisColor &, const QString &)),
00088             this, SIGNAL(colorDoubleClicked(const KisColor &, const QString &)));
00089 }
00090 
00091 KisPaletteWidget::~KisPaletteWidget()
00092 {
00093 }
00094 
00095 QString KisPaletteWidget::palette() const
00096 {
00097     return combo->currentText();
00098 }
00099 
00100 
00101 // 2000-02-12 Espen Sand
00102 // Set the color in two steps. The setPalette() slot will not emit a signal
00103 // with the current color setting. The reason is that setPalette() is used
00104 // by the color selector dialog on startup. In the color selector dialog
00105 // we normally want to display a startup color which we specify
00106 // when the dialog is started. The slotSetPalette() slot below will
00107 // set the palette and then use the information to emit a signal with the
00108 // new color setting. It is only used by the combobox widget.
00109 //
00110 void KisPaletteWidget::slotSetPalette( const QString &_paletteName )
00111 {
00112     setPalette( _paletteName );
00113     m_view->slotColorCellSelected(0); // FIXME: We need to save the current value!!
00114 }
00115 
00116 
00117 void KisPaletteWidget::setPalette( const QString &_paletteName )
00118 {
00119     QString paletteName( _paletteName);
00120 
00121     m_currentPalette = m_namedPaletteMap[paletteName];
00122 
00123     if (combo->currentText() != paletteName)
00124     {
00125         bool found = false;
00126         for(int i = 0; i < combo->count(); i++)
00127         {
00128             if (combo->text(i) == paletteName)
00129             {
00130                 combo->setCurrentItem(i);
00131                 found = true;
00132                 break;
00133             }
00134         }
00135         if (!found)
00136         {
00137             combo->insertItem(paletteName);
00138             combo->setCurrentItem(combo->count()-1);
00139         }
00140     }
00141 
00142     m_view->setPalette(m_currentPalette);
00143 }
00144 
00145 void KisPaletteWidget::slotAddPalette(KisResource * palette)
00146 {
00147     KisPalette * p = dynamic_cast<KisPalette*>(palette);
00148 
00149     m_namedPaletteMap.insert(palette->name(), p);
00150 
00151     combo->insertItem(palette->name());
00152 
00153     if (!init) {
00154         combo->setCurrentItem(0);
00155         setPalette(combo ->currentText());
00156         init = true;
00157     }
00158 }
00159 
00160 
00161 #include "kis_palette_widget.moc"
00162 
KDE Home | KDE Accessibility Home | Description of Access Keys