krita/ui

kis_cmb_composite.cc

Go to the documentation of this file.
00001 /*
00002  *  kis_cmb_composite.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 
00021 #include "widgets/kis_cmb_composite.h"
00022 
00023 #include <QItemDelegate>
00024 
00025 #include <klocale.h>
00026 #include <kis_debug.h>
00027 #include <KoCompositeOp.h>
00028 #include "../kis_composite_ops_model.h"
00029 #include <KCategorizedSortFilterProxyModel>
00030 #include <kis_categorized_item_delegate.h>
00031 
00032 KisCmbComposite::KisCmbComposite(QWidget * parent, const char * name)
00033         : KComboBox(parent), m_lastModel(0), m_sortModel(0)
00034 {
00035     setObjectName(name);
00036     setEditable(false);
00037     connect(this, SIGNAL(activated(int)), this, SLOT(slotOpActivated(int)));
00038     connect(this, SIGNAL(highlighted(int)), this, SLOT(slotOpHighlighted(int)));
00039     setItemDelegate(new KisCategorizedItemDelegate(new QItemDelegate));
00040     m_sortModel = new KCategorizedSortFilterProxyModel;
00041     m_sortModel->setSortRole(KisCompositeOpsModel::CompositeOpSortRole);
00042     m_sortModel->setCategorizedModel(true);
00043     setModel(m_sortModel);
00044 }
00045 
00046 KisCmbComposite::~KisCmbComposite()
00047 {
00048 }
00049 
00050 void KisCmbComposite::setCompositeOpList(const QList<KoCompositeOp*> & list)
00051 {
00052     KisCompositeOpsModel* model = new KisCompositeOpsModel(list);
00053     m_sortModel->setSourceModel(model);
00054     m_sortModel->sort(0);
00055 
00056     delete m_lastModel;
00057     m_lastModel = model;
00058 }
00059 
00060 const QString& KisCmbComposite::itemAt(int idx) const
00061 {
00062     return m_lastModel->itemAt(m_sortModel->mapToSource(m_sortModel->index(idx, 0)));
00063 }
00064 
00065 const QString& KisCmbComposite::currentItem() const
00066 {
00067     return itemAt(currentIndex());
00068 }
00069 
00070 void KisCmbComposite::setCurrent(const KoCompositeOp* op)
00071 {
00072     QModelIndex index = m_sortModel->mapFromSource(m_lastModel->indexOf(op));
00073     if (index.isValid()) {
00074         KComboBox::setCurrentIndex(index.row());
00075     }
00076 }
00077 
00078 void KisCmbComposite::setCurrent(const QString & s)
00079 {
00080     QModelIndex index = m_sortModel->mapFromSource(m_lastModel->indexOf(s));
00081     if (index.isValid()) {
00082         KComboBox::setCurrentIndex(index.row());
00083     }
00084 }
00085 
00086 void KisCmbComposite::slotOpActivated(int i)
00087 {
00088     if (i >= m_lastModel->rowCount()) return;
00089 
00090     emit activated(itemAt(i));
00091 }
00092 
00093 void KisCmbComposite::slotOpHighlighted(int i)
00094 {
00095     if (i >= m_lastModel->rowCount()) return;
00096 
00097     emit activated(itemAt(i));
00098 }
00099 
00100 
00101 #include "kis_cmb_composite.moc"
00102