1
2/*
3 * sidepanel.cpp
4 *
5 * Copyright (C) 2010 David Hubner <hubnerd@ntlworld.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23//Local
24#include "sidepanel.h"
25
26//KDE
27#include <KLocale>
28#include <KDebug>
29
30//QT
31#include <QWidget>
32#include <QRegExp>
33#include <QMenu>
34
35SidePanel::SidePanel(QWidget *parent) : QTreeView(parent)
36{
37 setSortingEnabled(true);
38 setAnimated(true);
39 sortByColumn(0, Qt::AscendingOrder);
40 m_model = new InfoKcmModel(this);
41
42 m_proxyModel = new InfoKcmProxyModel(this);
43 m_proxyModel->setSourceModel(m_model);
44
45 createMenuActions();
46
47 setMouseTracking(true);
48 setModel(m_proxyModel);
49 connect(this,SIGNAL(activated(QModelIndex)),this,SLOT(activatedSlot(QModelIndex)));
50}
51
52SidePanel::~SidePanel()
53{
54 disconnect(this,SIGNAL(activated(QModelIndex)),this,SLOT(activatedSlot(QModelIndex)));
55
56 delete m_proxyModel;
57 delete m_model;
58}
59
60void SidePanel::activatedSlot(const QModelIndex &index)
61{
62 if(index.isValid() == false) return;
63
64 const KcmTreeItem *item = static_cast<KcmTreeItem*>(m_proxyModel->mapToSource(index).internalPointer());
65 emit activated(item);
66}
67
68void SidePanel::changeToFirstValidItem()
69{
70 QModelIndex rootIndex = m_proxyModel->mapFromSource(m_model->firstValid());
71 if(rootIndex.isValid() == false) return;
72
73 setCurrentIndex(rootIndex);
74 emit activatedSlot(rootIndex);
75}
76
77QModelIndex SidePanel::mapToProxySource(const QModelIndex& index)
78{
79 QModelIndex tmp = m_proxyModel->mapToSource(index);
80 if(tmp.isValid() == true) return tmp;
81 return QModelIndex();
82}
83
84void SidePanel::filterSideMenuSlot(const QString &pattern)
85{
86 if(pattern.isEmpty())
87 {
88 collapseAll();
89 }
90 else
91 {
92 expandAll();
93 }
94 m_proxyModel->setFilterRegExp(QRegExp(pattern,Qt::CaseInsensitive));
95}
96
97void SidePanel::createMenuActions()
98{
99 resetAct = new QAction(i18n("Clear Search"), this);
100 connect(resetAct, SIGNAL(triggered()), this, SLOT(resetSearchSlot()));
101
102 expAct = new QAction(i18n("Expand All Categories"), this);
103 connect(expAct, SIGNAL(triggered()), this, SLOT(expandAllSlot()));
104
105 colAct = new QAction(i18n("Collapse All Categories"), this);
106 connect(colAct, SIGNAL(triggered()), this, SLOT(collapseAllSlot()));
107}
108
109void SidePanel::contextMenuEvent(QContextMenuEvent *event)
110{
111 QMenu menu(this);
112
113 menu.addAction(colAct);
114 menu.addAction(expAct);
115 menu.addAction(resetAct);
116 menu.exec(event->globalPos());
117}
118
119void SidePanel::collapseAllSlot()
120{
121 collapseAll();
122}
123
124void SidePanel::expandAllSlot()
125{
126 expandAll();
127}
128
129QStringList SidePanel::allChildKeywords()
130{
131 return m_model->allChildrenKeywords();
132}
133
134void SidePanel::resetSearchSlot()
135{
136 filterSideMenuSlot("");
137}
138
139