not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / shells / common / kcategorizeditemsview.cpp
blob8a0c8eade2cd61d4ab225f0217cbb9320ad75f28
1 /*
2 * Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library/Lesser General Public License
6 * version 2, or (at your option) any later version, as published by the
7 * Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library/Lesser General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "kcategorizeditemsview_p.h"
21 #include "kcategorizeditemsviewdelegate_p.h"
23 #include <KIcon>
24 #include <KDebug>
25 #include <KAction>
26 #include <KStandardAction>
28 #define UNIVERSAL_PADDING 6
30 KCategorizedItemsView::KCategorizedItemsView(QWidget * parent, Qt::WindowFlags f)
31 : QWidget(parent, f), m_modelCategories(NULL), m_modelFilters(NULL),
32 m_modelItems(NULL), m_modelFilterItems(NULL), m_delegate(NULL),
33 m_viewWidth(0)
35 setupUi(this);
36 itemsView->m_view = this;
38 textSearch->setClickMessage(i18n("Enter search phrase here"));
40 textSearch->setFocus();
42 connect(textSearch, SIGNAL(textChanged(QString)),
43 this, SLOT(searchTermChanged(QString)));
44 connect(comboFilters, SIGNAL(currentIndexChanged(int)),
45 this, SLOT(filterChanged(int)));
47 // we filter "activated" signals to re-emit them only when wanted
48 connect(itemsView, SIGNAL(activated(const QModelIndex &)),
49 this, SLOT(itemActivated(const QModelIndex &)));
50 connect(itemsView, SIGNAL(doubleClicked(const QModelIndex &)),
51 this, SLOT(itemDoubleClicked(const QModelIndex &)));
53 connect (itemsView, SIGNAL(clicked(const QModelIndex &)),
54 this, SIGNAL(clicked(const QModelIndex &)));
55 connect (itemsView, SIGNAL(entered(const QModelIndex &)),
56 this, SIGNAL(entered(const QModelIndex &)));
57 connect (itemsView, SIGNAL(pressed(const QModelIndex &)),
58 this, SIGNAL(pressed(const QModelIndex &)));
60 itemsView->header()->setVisible(false);
62 itemsView->setItemDelegate(m_delegate = new KCategorizedItemsViewDelegate(this));
63 //itemsView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
65 connect (m_delegate, SIGNAL(destroyApplets(const QString)),
66 parent, SLOT(destroyApplets(const QString)));
67 connect (m_delegate, SIGNAL(infoAboutApplet(const QString &)),
68 parent, SLOT(infoAboutApplet(const QString &)));
70 comboFilters->setItemDelegate(new KCategorizedItemsViewFilterDelegate(this));
72 itemsView->viewport()->setAttribute(Qt::WA_Hover);
73 itemsView->setAlternatingRowColors(true);
75 QAction * find = KStandardAction::find(textSearch, SLOT(setFocus()), this);
76 addAction(find);
79 KCategorizedItemsView::~KCategorizedItemsView()
81 delete m_modelFilterItems;
82 delete m_delegate;
85 void KCategorizedItemsView::resizeEvent (QResizeEvent *event)
87 updateColumnsWidth();
89 QWidget::resizeEvent(event);
92 bool KCategorizedItemsView::event (QEvent *event)
94 switch (event->type()) {
95 case QEvent::PolishRequest:
96 case QEvent::Polish:
97 updateColumnsWidth(true);
98 break;
99 default:
100 break;
103 return QWidget::event(event);
106 void KCategorizedItemsView::setFilterModel(QStandardItemModel *model)
108 comboFilters->setModel(model);
109 m_modelFilters = model;
112 void KCategorizedItemsView::setItemModel(QStandardItemModel *model)
114 if (!m_modelFilterItems) {
115 m_modelFilterItems = new DefaultItemFilterProxyModel(this);
116 connect(m_modelFilterItems, SIGNAL(searchTermChanged(QString)),
117 this, SLOT(slotSearchTermChanged(QString)));
120 m_modelItems = model;
121 m_modelFilterItems->setSortCaseSensitivity(Qt::CaseInsensitive);
122 m_modelFilterItems->setDynamicSortFilter(true);
123 m_modelFilterItems->setSourceModel(m_modelItems);
124 m_modelFilterItems->sort(0);
126 itemsView->setModel(m_modelFilterItems);
128 if (m_modelFilterItems->rowCount()) {
129 itemsView->verticalScrollBar()->setSingleStep(itemsView->sizeHintForRow(0));
133 void KCategorizedItemsView::searchTermChanged(const QString &text)
135 kDebug() << "EVENT\n" << text;
136 if (m_modelFilterItems) {
137 m_modelFilterItems->setSearch(text);
141 void KCategorizedItemsView::filterChanged(int index)
143 if (m_modelFilterItems) {
144 QVariant data = m_modelFilters->item(index)->data();
145 m_modelFilterItems->setFilter(qVariantValue<KCategorizedItemsViewModels::Filter>(data));
149 void KCategorizedItemsView::itemActivated(const QModelIndex &index)
151 // don't emit activated signal for "favicon" and "remove applet"
152 // columns so double clicking on these columns won't unexpectedly
153 // add an applet to the containment
154 if (index.column() == 1 || index.column() == 2 || index.column() == 3) {
155 return;
158 emit activated(index);
161 void KCategorizedItemsView::itemDoubleClicked(const QModelIndex &index)
163 // don't emit activated signal for "favicon" and "remove applet"
164 // columns so double clicking on these columns won't unexpectedly
165 // add an applet to the containment
166 if (index.column() == 1 || index.column() == 2 || index.column() == 3) {
167 return;
170 emit doubleClicked(index);
173 void KCategorizedItemsView::slotSearchTermChanged(const QString &term)
175 updateColumnsWidth();
178 void KCategorizedItemsView::updateColumnsWidth(bool force)
180 m_viewWidth = itemsView->viewport()->width();
182 if (force) {
183 m_viewWidth -= style()->pixelMetric(QStyle::PM_ScrollBarExtent) + UNIVERSAL_PADDING;
186 itemsView->setColumnWidth(0, m_delegate->columnWidth(0, m_viewWidth));
187 itemsView->setColumnWidth(1, m_delegate->columnWidth(1, m_viewWidth));
188 itemsView->setColumnWidth(2, m_delegate->columnWidth(2, m_viewWidth));
189 itemsView->setColumnWidth(3, m_delegate->columnWidth(3, m_viewWidth));
192 void KCategorizedItemsView::addEmblem(const QString &title, const QIcon &icon,
193 const Filter &filter)
195 m_emblems[title] = QPair<Filter, QIcon>(filter, icon);
198 void KCategorizedItemsView::clearEmblems()
200 m_emblems.clear();
203 AbstractItem *KCategorizedItemsView::getItemByProxyIndex(const QModelIndex &index) const
205 return (AbstractItem *)m_modelItems->itemFromIndex(m_modelFilterItems->mapToSource(index));
208 QList <AbstractItem *> KCategorizedItemsView::selectedItems() const
210 QList <AbstractItem *> items;
211 foreach (const QModelIndex &index, itemsView->selectionModel()->selectedIndexes()) {
212 if (index.column() == 0) {
213 items << getItemByProxyIndex(index);
216 return items;
219 #include "kcategorizeditemsview_p.moc"