2007-09-26 09:44:06 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-10-12 10:44:02 +02:00
|
|
|
* it under the terms of the GNU Library/Lesser General Public License
|
|
|
|
* version 2, or (at your option) any later version, as published by the
|
|
|
|
* Free Software Foundation
|
2007-09-26 09:44:06 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
2007-10-12 10:44:02 +02:00
|
|
|
* You should have received a copy of the GNU Library/Lesser General Public
|
2007-09-26 09:44:06 +02:00
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2007-09-27 20:51:31 +02:00
|
|
|
#include "kcategorizeditemsview_p.h"
|
|
|
|
#include "kcategorizeditemsviewdelegate_p.h"
|
2007-09-26 09:44:06 +02:00
|
|
|
|
|
|
|
#include <KIcon>
|
|
|
|
#include <KDebug>
|
|
|
|
#include <KAction>
|
|
|
|
#include <KStandardAction>
|
|
|
|
|
2008-02-18 21:55:50 +01:00
|
|
|
#define UNIVERSAL_PADDING 6
|
|
|
|
|
2007-09-26 09:44:06 +02:00
|
|
|
KCategorizedItemsView::KCategorizedItemsView(QWidget * parent, Qt::WindowFlags f)
|
|
|
|
: QWidget(parent, f), m_modelCategories(NULL), m_modelFilters(NULL),
|
2007-10-21 15:51:49 +02:00
|
|
|
m_modelItems(NULL), m_modelFilterItems(NULL), m_delegate(NULL),
|
|
|
|
m_viewWidth(0)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
setupUi(this);
|
2007-09-29 21:11:27 +02:00
|
|
|
itemsView->m_view = this;
|
|
|
|
|
2007-09-26 09:44:06 +02:00
|
|
|
textSearch->setClickMessage(i18n("Enter search phrase here"));
|
2008-10-12 02:18:08 +02:00
|
|
|
|
2007-12-29 22:52:40 +01:00
|
|
|
textSearch->setFocus();
|
2008-10-12 02:18:08 +02:00
|
|
|
|
2007-09-26 09:44:06 +02:00
|
|
|
connect(textSearch, SIGNAL(textChanged(QString)),
|
|
|
|
this, SLOT(searchTermChanged(QString)));
|
|
|
|
connect(comboFilters, SIGNAL(currentIndexChanged(int)),
|
|
|
|
this, SLOT(filterChanged(int)));
|
2007-09-27 19:50:31 +02:00
|
|
|
|
2008-03-13 17:10:11 +01:00
|
|
|
// we filter "activated" signals to re-emit them only when wanted
|
2008-06-18 01:33:27 +02:00
|
|
|
connect(itemsView, SIGNAL(activated(const QModelIndex &)),
|
|
|
|
this, SLOT(itemActivated(const QModelIndex &)));
|
2008-06-29 02:06:04 +02:00
|
|
|
connect(itemsView, SIGNAL(doubleClicked(const QModelIndex &)),
|
|
|
|
this, SLOT(itemDoubleClicked(const QModelIndex &)));
|
2007-09-27 19:50:31 +02:00
|
|
|
|
2007-09-26 09:44:06 +02:00
|
|
|
connect (itemsView, SIGNAL(clicked(const QModelIndex &)),
|
2008-06-18 01:33:27 +02:00
|
|
|
this, SIGNAL(clicked(const QModelIndex &)));
|
2007-09-26 09:44:06 +02:00
|
|
|
connect (itemsView, SIGNAL(entered(const QModelIndex &)),
|
2008-06-18 01:33:27 +02:00
|
|
|
this, SIGNAL(entered(const QModelIndex &)));
|
2007-09-26 09:44:06 +02:00
|
|
|
connect (itemsView, SIGNAL(pressed(const QModelIndex &)),
|
2008-06-18 01:33:27 +02:00
|
|
|
this, SIGNAL(pressed(const QModelIndex &)));
|
2007-09-26 09:44:06 +02:00
|
|
|
|
|
|
|
itemsView->header()->setVisible(false);
|
2007-09-27 19:50:31 +02:00
|
|
|
|
2007-09-26 09:44:06 +02:00
|
|
|
itemsView->setItemDelegate(m_delegate = new KCategorizedItemsViewDelegate(this));
|
2008-02-17 10:23:19 +01:00
|
|
|
//itemsView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
2007-09-27 19:50:31 +02:00
|
|
|
|
2007-12-10 02:18:57 +01:00
|
|
|
connect (m_delegate, SIGNAL(destroyApplets(const QString)),
|
2008-06-18 01:33:27 +02:00
|
|
|
parent, SLOT(destroyApplets(const QString)));
|
2007-12-10 02:18:57 +01:00
|
|
|
|
2007-09-26 09:44:06 +02:00
|
|
|
comboFilters->setItemDelegate(new KCategorizedItemsViewFilterDelegate(this));
|
2007-09-27 19:50:31 +02:00
|
|
|
|
2007-09-26 09:44:06 +02:00
|
|
|
itemsView->viewport()->setAttribute(Qt::WA_Hover);
|
2008-03-12 18:14:15 +01:00
|
|
|
itemsView->setAlternatingRowColors(true);
|
2007-09-26 09:44:06 +02:00
|
|
|
|
|
|
|
QAction * find = KStandardAction::find(textSearch, SLOT(setFocus()), this);
|
|
|
|
addAction(find);
|
|
|
|
}
|
|
|
|
|
2008-02-18 21:55:50 +01:00
|
|
|
KCategorizedItemsView::~KCategorizedItemsView()
|
|
|
|
{
|
2007-09-26 09:44:06 +02:00
|
|
|
delete m_modelFilterItems;
|
|
|
|
delete m_delegate;
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void KCategorizedItemsView::resizeEvent (QResizeEvent *event)
|
2008-02-18 21:55:50 +01:00
|
|
|
{
|
|
|
|
updateColumnsWidth();
|
|
|
|
|
|
|
|
QWidget::resizeEvent(event);
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
bool KCategorizedItemsView::event (QEvent *event)
|
2008-02-18 21:55:50 +01:00
|
|
|
{
|
|
|
|
switch (event->type()) {
|
2008-10-12 02:18:08 +02:00
|
|
|
case QEvent::PolishRequest:
|
|
|
|
case QEvent::Polish:
|
|
|
|
updateColumnsWidth(true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2008-02-18 21:55:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return QWidget::event(event);
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void KCategorizedItemsView::setFilterModel(QStandardItemModel *model)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
comboFilters->setModel(model);
|
|
|
|
m_modelFilters = model;
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void KCategorizedItemsView::setItemModel(QStandardItemModel *model)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2007-11-06 22:10:11 +01:00
|
|
|
if (!m_modelFilterItems) {
|
|
|
|
m_modelFilterItems = new DefaultItemFilterProxyModel(this);
|
2008-10-12 02:18:08 +02:00
|
|
|
connect(m_modelFilterItems, SIGNAL(searchTermChanged(QString)),
|
|
|
|
this, SLOT(slotSearchTermChanged(QString)));
|
2007-11-06 22:10:11 +01:00
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
|
|
|
|
m_modelItems = model;
|
2007-11-22 20:39:16 +01:00
|
|
|
m_modelFilterItems->setSortCaseSensitivity(Qt::CaseInsensitive);
|
2008-06-18 01:33:27 +02:00
|
|
|
m_modelFilterItems->setDynamicSortFilter(true);
|
|
|
|
m_modelFilterItems->setSourceModel(m_modelItems);
|
2007-11-22 20:39:16 +01:00
|
|
|
m_modelFilterItems->sort(0);
|
2007-09-26 09:44:06 +02:00
|
|
|
|
|
|
|
itemsView->setModel(m_modelFilterItems);
|
2007-10-26 09:58:12 +02:00
|
|
|
|
|
|
|
if (m_modelFilterItems->rowCount()) {
|
|
|
|
itemsView->verticalScrollBar()->setSingleStep(itemsView->sizeHintForRow(0));
|
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void KCategorizedItemsView::searchTermChanged(const QString &text)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2008-01-08 02:25:09 +01:00
|
|
|
kDebug() << "EVENT\n" << text;
|
2007-09-26 09:44:06 +02:00
|
|
|
if (m_modelFilterItems) {
|
|
|
|
m_modelFilterItems->setSearch(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KCategorizedItemsView::filterChanged(int index)
|
|
|
|
{
|
|
|
|
if (m_modelFilterItems) {
|
|
|
|
QVariant data = m_modelFilters->item(index)->data();
|
|
|
|
m_modelFilterItems->setFilter(qVariantValue<KCategorizedItemsViewModels::Filter>(data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void KCategorizedItemsView::itemActivated(const QModelIndex &index)
|
2008-03-13 17:10:11 +01:00
|
|
|
{
|
|
|
|
// don't emit activated signal for "favicon" and "remove applet"
|
|
|
|
// columns so double clicking on these columns won't unexpectedly
|
|
|
|
// add an applet to the containment
|
2008-10-12 02:18:08 +02:00
|
|
|
if (index.column() == 1 || index.column() == 2) {
|
2008-03-13 17:10:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit activated(index);
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void KCategorizedItemsView::itemDoubleClicked(const QModelIndex &index)
|
2008-06-29 02:06:04 +02:00
|
|
|
{
|
|
|
|
// don't emit activated signal for "favicon" and "remove applet"
|
|
|
|
// columns so double clicking on these columns won't unexpectedly
|
|
|
|
// add an applet to the containment
|
2008-10-12 02:18:08 +02:00
|
|
|
if (index.column() == 1 || index.column() == 2) {
|
2008-06-29 02:06:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit doubleClicked(index);
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void KCategorizedItemsView::slotSearchTermChanged(const QString &term)
|
2008-08-11 03:41:29 +02:00
|
|
|
{
|
|
|
|
updateColumnsWidth();
|
|
|
|
}
|
|
|
|
|
2008-02-18 21:55:50 +01:00
|
|
|
void KCategorizedItemsView::updateColumnsWidth(bool force)
|
|
|
|
{
|
|
|
|
m_viewWidth = itemsView->viewport()->width();
|
|
|
|
|
|
|
|
if (force) {
|
|
|
|
m_viewWidth -= style()->pixelMetric(QStyle::PM_ScrollBarExtent) + UNIVERSAL_PADDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
itemsView->setColumnWidth(0, m_delegate->columnWidth(0, m_viewWidth));
|
|
|
|
itemsView->setColumnWidth(1, m_delegate->columnWidth(1, m_viewWidth));
|
|
|
|
itemsView->setColumnWidth(2, m_delegate->columnWidth(2, m_viewWidth));
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void KCategorizedItemsView::addEmblem(const QString &title, const QIcon &icon,
|
|
|
|
const Filter &filter)
|
|
|
|
{
|
2008-08-23 19:22:26 +02:00
|
|
|
m_emblems[title] = QPair<Filter, QIcon>(filter, icon);
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void KCategorizedItemsView::clearEmblems()
|
|
|
|
{
|
2007-09-26 09:44:06 +02:00
|
|
|
m_emblems.clear();
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
AbstractItem *KCategorizedItemsView::getItemByProxyIndex(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
return (AbstractItem *)m_modelItems->itemFromIndex(m_modelFilterItems->mapToSource(index));
|
2007-09-27 19:50:31 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
QList <AbstractItem *> KCategorizedItemsView::selectedItems() const
|
|
|
|
{
|
|
|
|
QList <AbstractItem *> items;
|
2008-04-26 18:19:00 +02:00
|
|
|
foreach (const QModelIndex &index, itemsView->selectionModel()->selectedIndexes()) {
|
2007-09-27 19:50:31 +02:00
|
|
|
if (index.column() == 0) {
|
|
|
|
items << getItemByProxyIndex(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return items;
|
|
|
|
}
|
2007-09-27 20:51:31 +02:00
|
|
|
|
|
|
|
#include "kcategorizeditemsview_p.moc"
|
2007-09-26 09:44:06 +02:00
|
|
|
|