krita

kis_palette_view.cc

00001 /*
00002  *  Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
00003  *            (c) 2005 Bart Coppens <kde@bartcoppens.be>
00004  *
00005  *  Based on already much changed code by Waldo Bastian <bastian@kde.org> from KisPaletteWidget
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 
00025 #include <qcheckbox.h>
00026 #include <qcombobox.h>
00027 #include <qdrawutil.h>
00028 #include <qevent.h>
00029 #include <qfile.h>
00030 #include <qimage.h>
00031 #include <qlabel.h>
00032 #include <qlayout.h>
00033 #include <qlineedit.h>
00034 #include <qvalidator.h>
00035 #include <qpainter.h>
00036 #include <qpushbutton.h>
00037 #include <qspinbox.h>
00038 #include <qtimer.h>
00039 
00040 #include <kapplication.h>
00041 #include <kconfig.h>
00042 #include <kglobal.h>
00043 #include <kglobalsettings.h>
00044 #include <kiconloader.h>
00045 #include <klistbox.h>
00046 #include <klocale.h>
00047 #include <kmessagebox.h>
00048 #include <kseparator.h>
00049 #include <kpalette.h>
00050 #include <kimageeffect.h>
00051 
00052 #include <kcolordialog.h>
00053 #include <kcolordrag.h>
00054 #include <config.h>
00055 #include <kdebug.h>
00056 
00057 #include <kis_meta_registry.h>
00058 #include <kis_color.h>
00059 #include <kis_factory.h>
00060 #include <kis_colorspace_factory_registry.h>
00061 #include "kis_palette_view.h"
00062 #include "kis_resource.h"
00063 #include "kis_palette.h"
00064 
00065 KisPaletteView::KisPaletteView(QWidget *parent, const char* name, int minWidth, int cols)
00066     : QScrollView( parent, name ), mMinWidth(minWidth), mCols(cols)
00067 {
00068     m_cells = 0;
00069     m_currentPalette = 0;
00070 
00071     QSize cellSize = QSize( mMinWidth, 50);
00072 
00073     setHScrollBarMode(QScrollView::AlwaysOff);
00074     setVScrollBarMode(QScrollView::AlwaysOn);
00075 
00076     QSize minSize = QSize(verticalScrollBar()->width(), 0);
00077     minSize += QSize(frameWidth(), 0);
00078     minSize += QSize(cellSize);
00079 
00080     setMinimumSize(minSize);
00081     setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored));
00082 }
00083 
00084 KisPaletteView::~KisPaletteView()
00085 {
00086 }
00087 
00088 KisPalette* KisPaletteView::palette() const
00089 {
00090     return m_currentPalette;
00091 }
00092 
00093 void KisPaletteView::setPalette(KisPalette* palette)
00094 {
00095     m_currentPalette = palette;
00096     delete m_cells;
00097 
00098     int rows = (m_currentPalette->nColors() + mCols -1 ) / mCols;
00099 
00100     if (rows < 1) rows = 1;
00101 
00102     m_cells = new KColorCells(viewport(), rows, mCols);
00103     Q_CHECK_PTR(m_cells);
00104 
00105     m_cells->setShading(false);
00106     m_cells->setAcceptDrags(false);
00107 
00108     QSize cellSize = QSize( mMinWidth, mMinWidth * rows / mCols);
00109     m_cells->setFixedSize( cellSize );
00110 
00111     for( int i = 0; i < m_currentPalette->nColors(); i++)
00112     {
00113         QColor c = m_currentPalette->getColor(i).color;
00114         m_cells->setColor( i, c );
00115     }
00116 
00117     connect(m_cells, SIGNAL(colorSelected(int)),
00118             SLOT(slotColorCellSelected(int)));
00119 
00120     connect(m_cells, SIGNAL(colorDoubleClicked(int)),
00121             SLOT(slotColorCellDoubleClicked(int)) );
00122 
00123     addChild( m_cells );
00124     m_cells->show();
00125     updateScrollBars();
00126 }
00127 
00128 void KisPaletteView::slotColorCellSelected( int col )
00129 {
00130     KisColorSpace * cs = KisMetaRegistry::instance()->csRegistry()->getRGB8();
00131     if (!m_currentPalette || (col >= m_currentPalette->nColors()))
00132         return;
00133 
00134     m_currentEntry = m_currentPalette->getColor(col);
00135     emit colorSelected(KisColor(m_currentPalette->getColor(col).color, cs));
00136     emit colorSelected(m_currentPalette->getColor(col).color);
00137 }
00138 
00139 void KisPaletteView::slotColorCellDoubleClicked( int col )
00140 {
00141     KisColorSpace * cs = KisMetaRegistry::instance()->csRegistry()->getRGB8();
00142     if (!m_currentPalette || (col >= m_currentPalette->nColors()))
00143         return;
00144 
00145     emit colorDoubleClicked(KisColor(m_currentPalette->getColor(col).color, cs),
00146                             m_currentPalette->getColor(col).name);
00147 }
00148 
00149 #include "kis_palette_view.moc"
00150 
KDE Home | KDE Accessibility Home | Description of Access Keys