• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libplasma

kcategorizeditemsview.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library/Lesser General Public License
00006  *   version 2, or (at your option) any later version, as published by the
00007  *   Free Software Foundation
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 Library/Lesser General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "kcategorizeditemsview_p.h"
00021 #include "kcategorizeditemsviewdelegate_p.h"
00022 
00023 #include <KIcon>
00024 #include <KDebug>
00025 #include <KAction>
00026 #include <KStandardAction>
00027 
00028 #define UNIVERSAL_PADDING 6
00029 
00030 KCategorizedItemsView::KCategorizedItemsView(QWidget * parent, Qt::WindowFlags f)
00031         : QWidget(parent, f), m_modelCategories(NULL), m_modelFilters(NULL),
00032         m_modelItems(NULL), m_modelFilterItems(NULL), m_delegate(NULL),
00033         m_viewWidth(0)
00034 {
00035     setupUi(this);
00036     itemsView->m_view = this;
00037 
00038     textSearch->setClickMessage(i18n("Enter search phrase here"));
00039     
00040     textSearch->setFocus();
00041     
00042     connect(textSearch, SIGNAL(textChanged(QString)),
00043             this, SLOT(searchTermChanged(QString)));
00044     connect(comboFilters, SIGNAL(currentIndexChanged(int)),
00045             this, SLOT(filterChanged(int)));
00046 
00047     // we filter "activated" signals to re-emit them only when wanted
00048     connect(itemsView, SIGNAL(activated(const QModelIndex &)),
00049             this, SLOT(itemActivated(const QModelIndex &)));
00050     connect(itemsView, SIGNAL(doubleClicked(const QModelIndex &)),
00051             this, SLOT(itemDoubleClicked(const QModelIndex &)));
00052 
00053     connect (itemsView, SIGNAL(clicked(const QModelIndex &)),
00054              this, SIGNAL(clicked(const QModelIndex &)));
00055     connect (itemsView, SIGNAL(entered(const QModelIndex &)),
00056              this, SIGNAL(entered(const QModelIndex &)));
00057     connect (itemsView, SIGNAL(pressed(const QModelIndex &)),
00058              this, SIGNAL(pressed(const QModelIndex &)));
00059 
00060     itemsView->header()->setVisible(false);
00061 
00062     itemsView->setItemDelegate(m_delegate = new KCategorizedItemsViewDelegate(this));
00063     //itemsView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
00064 
00065     connect (m_delegate, SIGNAL(destroyApplets(const QString)),
00066              parent, SLOT(destroyApplets(const QString)));
00067 
00068     comboFilters->setItemDelegate(new KCategorizedItemsViewFilterDelegate(this));
00069 
00070     itemsView->viewport()->setAttribute(Qt::WA_Hover);
00071     itemsView->setAlternatingRowColors(true);
00072 
00073     QAction * find = KStandardAction::find(textSearch, SLOT(setFocus()), this);
00074     addAction(find);
00075 }
00076 
00077 KCategorizedItemsView::~KCategorizedItemsView()
00078 {
00079     delete m_modelFilterItems;
00080     delete m_delegate;
00081 }
00082 
00083 void KCategorizedItemsView::resizeEvent ( QResizeEvent * event )
00084 {
00085     updateColumnsWidth();
00086 
00087     QWidget::resizeEvent(event);
00088 }
00089 
00090 bool KCategorizedItemsView::event ( QEvent * event )
00091 {
00092     switch (event->type()) {
00093         case QEvent::PolishRequest:
00094         case QEvent::Polish:
00095             updateColumnsWidth(true);
00096             break;
00097         default:
00098             break;
00099     }
00100 
00101     return QWidget::event(event);
00102 }
00103 
00104 void KCategorizedItemsView::setFilterModel(QStandardItemModel * model)
00105 {
00106     comboFilters->setModel(model);
00107     m_modelFilters = model;
00108 }
00109 
00110 void KCategorizedItemsView::setItemModel(QStandardItemModel * model)
00111 {
00112     if (!m_modelFilterItems) {
00113         m_modelFilterItems = new DefaultItemFilterProxyModel(this);
00114     }
00115 
00116     m_modelItems = model;
00117     m_modelFilterItems->setSortCaseSensitivity(Qt::CaseInsensitive);
00118     m_modelFilterItems->setDynamicSortFilter(true);
00119     m_modelFilterItems->setSourceModel(m_modelItems);
00120     m_modelFilterItems->sort(0);
00121 
00122     itemsView->setModel(m_modelFilterItems);
00123 
00124     if (m_modelFilterItems->rowCount()) {
00125         itemsView->verticalScrollBar()->setSingleStep(itemsView->sizeHintForRow(0));
00126     }
00127 }
00128 
00129 void KCategorizedItemsView::searchTermChanged(const QString & text)
00130 {
00131     kDebug() << "EVENT\n" << text;
00132     if (m_modelFilterItems) {
00133         m_modelFilterItems->setSearch(text);
00134     }
00135 }
00136 
00137 void KCategorizedItemsView::filterChanged(int index)
00138 {
00139     if (m_modelFilterItems) {
00140         QVariant data = m_modelFilters->item(index)->data();
00141         m_modelFilterItems->setFilter(qVariantValue<KCategorizedItemsViewModels::Filter>(data));
00142     }
00143 }
00144 
00145 void KCategorizedItemsView::itemActivated( const QModelIndex& index )
00146 {
00147     // don't emit activated signal for "favicon" and "remove applet"
00148     // columns so double clicking on these columns won't unexpectedly
00149     // add an applet to the containment
00150     if ( index.column() == 1 || index.column() == 2 ) {
00151         return;
00152     }
00153 
00154     emit activated(index);
00155 }
00156 
00157 void KCategorizedItemsView::itemDoubleClicked(const QModelIndex& index)
00158 {
00159     // don't emit activated signal for "favicon" and "remove applet"
00160     // columns so double clicking on these columns won't unexpectedly
00161     // add an applet to the containment
00162     if ( index.column() == 1 || index.column() == 2 ) {
00163         return;
00164     }
00165 
00166     emit doubleClicked(index);
00167 }
00168 
00169 void KCategorizedItemsView::updateColumnsWidth(bool force)
00170 {
00171     m_viewWidth = itemsView->viewport()->width();
00172 
00173     if (force) {
00174         m_viewWidth -= style()->pixelMetric(QStyle::PM_ScrollBarExtent) + UNIVERSAL_PADDING;
00175     }
00176 
00177     itemsView->setColumnWidth(0, m_delegate->columnWidth(0, m_viewWidth));
00178     itemsView->setColumnWidth(1, m_delegate->columnWidth(1, m_viewWidth));
00179     itemsView->setColumnWidth(2, m_delegate->columnWidth(2, m_viewWidth));
00180 }
00181 
00182 void KCategorizedItemsView::addEmblem(const QString & title, QIcon * icon, const Filter & filter) {
00183     m_emblems[title] = QPair<Filter, QIcon *>(filter, icon);
00184 }
00185 
00186 void KCategorizedItemsView::clearEmblems() {
00187     m_emblems.clear();
00188 }
00189 
00190 AbstractItem * KCategorizedItemsView::getItemByProxyIndex(const QModelIndex & index) const {
00191     return (AbstractItem *) m_modelItems->itemFromIndex(
00192         m_modelFilterItems->mapToSource(index)
00193     );
00194 }
00195 
00196 
00197 QList < AbstractItem * > KCategorizedItemsView::selectedItems() const {
00198     QList < AbstractItem * > items;
00199     foreach (const QModelIndex &index, itemsView->selectionModel()->selectedIndexes()) {
00200         if (index.column() == 0) {
00201             items << getItemByProxyIndex(index);
00202         }
00203     }
00204     return items;
00205 }
00206 
00207 #include "kcategorizeditemsview_p.moc"
00208 

libplasma

Skip menu "libplasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal