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

Plasma

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     connect (m_delegate, SIGNAL(infoAboutApplet(const QString &)),
00068              parent, SLOT(infoAboutApplet(const QString &)));
00069 
00070     comboFilters->setItemDelegate(new KCategorizedItemsViewFilterDelegate(this));
00071 
00072     itemsView->viewport()->setAttribute(Qt::WA_Hover);
00073     itemsView->setAlternatingRowColors(true);
00074 
00075     QAction * find = KStandardAction::find(textSearch, SLOT(setFocus()), this);
00076     addAction(find);
00077 }
00078 
00079 KCategorizedItemsView::~KCategorizedItemsView()
00080 {
00081     delete m_modelFilterItems;
00082     delete m_delegate;
00083 }
00084 
00085 void KCategorizedItemsView::resizeEvent (QResizeEvent *event)
00086 {
00087     updateColumnsWidth();
00088 
00089     QWidget::resizeEvent(event);
00090 }
00091 
00092 bool KCategorizedItemsView::event (QEvent *event)
00093 {
00094     switch (event->type()) {
00095     case QEvent::PolishRequest:
00096     case QEvent::Polish:
00097         updateColumnsWidth(true);
00098         break;
00099     default:
00100         break;
00101     }
00102 
00103     return QWidget::event(event);
00104 }
00105 
00106 void KCategorizedItemsView::setFilterModel(QStandardItemModel *model)
00107 {
00108     comboFilters->setModel(model);
00109     m_modelFilters = model;
00110 }
00111 
00112 void KCategorizedItemsView::setItemModel(QStandardItemModel *model)
00113 {
00114     if (!m_modelFilterItems) {
00115         m_modelFilterItems = new DefaultItemFilterProxyModel(this);
00116         connect(m_modelFilterItems, SIGNAL(searchTermChanged(QString)),
00117                 this, SLOT(slotSearchTermChanged(QString)));
00118     }
00119 
00120     m_modelItems = model;
00121     m_modelFilterItems->setSortCaseSensitivity(Qt::CaseInsensitive);
00122     m_modelFilterItems->setDynamicSortFilter(true);
00123     m_modelFilterItems->setSourceModel(m_modelItems);
00124     m_modelFilterItems->sort(0);
00125 
00126     itemsView->setModel(m_modelFilterItems);
00127 
00128     if (m_modelFilterItems->rowCount()) {
00129         itemsView->verticalScrollBar()->setSingleStep(itemsView->sizeHintForRow(0));
00130     }
00131 }
00132 
00133 void KCategorizedItemsView::searchTermChanged(const QString &text)
00134 {
00135     kDebug() << "EVENT\n" << text;
00136     if (m_modelFilterItems) {
00137         m_modelFilterItems->setSearch(text);
00138     }
00139 }
00140 
00141 void KCategorizedItemsView::filterChanged(int index)
00142 {
00143     if (m_modelFilterItems) {
00144         QVariant data = m_modelFilters->item(index)->data();
00145         m_modelFilterItems->setFilter(qVariantValue<KCategorizedItemsViewModels::Filter>(data));
00146     }
00147 }
00148 
00149 void KCategorizedItemsView::itemActivated(const QModelIndex &index)
00150 {
00151     // don't emit activated signal for "favicon" and "remove applet"
00152     // columns so double clicking on these columns won't unexpectedly
00153     // add an applet to the containment
00154     if (index.column() == 1 || index.column() == 2 || index.column() == 3) {
00155         return;
00156     }
00157 
00158     emit activated(index);
00159 }
00160 
00161 void KCategorizedItemsView::itemDoubleClicked(const QModelIndex &index)
00162 {
00163     // don't emit activated signal for "favicon" and "remove applet"
00164     // columns so double clicking on these columns won't unexpectedly
00165     // add an applet to the containment
00166     if (index.column() == 1 || index.column() == 2 || index.column() == 3) {
00167         return;
00168     }
00169 
00170     emit doubleClicked(index);
00171 }
00172 
00173 void KCategorizedItemsView::slotSearchTermChanged(const QString &term)
00174 {
00175     updateColumnsWidth();
00176 }
00177 
00178 void KCategorizedItemsView::updateColumnsWidth(bool force)
00179 {
00180     m_viewWidth = itemsView->viewport()->width();
00181 
00182     if (force) {
00183         m_viewWidth -= style()->pixelMetric(QStyle::PM_ScrollBarExtent) + UNIVERSAL_PADDING;
00184     }
00185 
00186     itemsView->setColumnWidth(0, m_delegate->columnWidth(0, m_viewWidth));
00187     itemsView->setColumnWidth(1, m_delegate->columnWidth(1, m_viewWidth));
00188     itemsView->setColumnWidth(2, m_delegate->columnWidth(2, m_viewWidth));
00189     itemsView->setColumnWidth(3, m_delegate->columnWidth(3, m_viewWidth));
00190 }
00191 
00192 void KCategorizedItemsView::addEmblem(const QString &title, const QIcon &icon,
00193                                       const Filter &filter)
00194 {
00195     m_emblems[title] = QPair<Filter, QIcon>(filter, icon);
00196 }
00197 
00198 void KCategorizedItemsView::clearEmblems()
00199 {
00200     m_emblems.clear();
00201 }
00202 
00203 AbstractItem *KCategorizedItemsView::getItemByProxyIndex(const QModelIndex &index) const
00204 {
00205     return (AbstractItem *)m_modelItems->itemFromIndex(m_modelFilterItems->mapToSource(index));
00206 }
00207 
00208 QList <AbstractItem *> KCategorizedItemsView::selectedItems() const
00209 {
00210     QList <AbstractItem *> items;
00211     foreach (const QModelIndex &index, itemsView->selectionModel()->selectedIndexes()) {
00212         if (index.column() == 0) {
00213             items << getItemByProxyIndex(index);
00214         }
00215     }
00216     return items;
00217 }
00218 
00219 #include "kcategorizeditemsview_p.moc"
00220 

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
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