• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeplasma-addons API Reference
  • KDE Home
  • Contact Us
 

liblancelot

  • sources
  • kde-4.14
  • kdeplasma-addons
  • libs
  • lancelot
  • models
StandardActionTreeModel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010 Ivan Cukic <ivan.cukic(at)kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser/Library General Public License version 2,
6  * or (at your option) any later version, as published by the Free
7  * Software Foundation
8  *
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 Lesser/Library General Public License for more details
13  *
14  * You should have received a copy of the GNU Lesser/Library 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.
18  */
19 
20 #include "StandardActionTreeModel.h"
21 
22 #include <iostream>
23 
24 namespace Lancelot
25 {
26 
27 class StandardActionTreeModel::Private {
28 public:
29  Item * root;
30  bool deleteRoot;
31  QHash < Item * , StandardActionTreeModel * > childModels;
32 };
33 
34 StandardActionTreeModel::Item::Item(QString itemTitle,
35  QString itemDescription, QIcon itemIcon, QVariant itemData)
36  : title(itemTitle), description(itemDescription), icon(itemIcon), data(itemData)
37 {
38 }
39 
40 StandardActionTreeModel::Item::~Item()
41 {
42  qDeleteAll(children);
43 }
44 
45 StandardActionTreeModel::StandardActionTreeModel()
46  : ActionTreeModel(), d(new Private())
47 {
48  d->root = new Item();
49  d->deleteRoot = true;
50 }
51 
52 StandardActionTreeModel::StandardActionTreeModel(Item * root)
53  : ActionTreeModel(), d(new Private())
54 {
55  if (!root) {
56  d->root = new Item();
57  d->deleteRoot = true;
58  } else {
59  d->root = root;
60  }
61 }
62 
63 StandardActionTreeModel::~StandardActionTreeModel()
64 {
65  if (d->deleteRoot) {
66  qDeleteAll(d->childModels);
67  delete d->root;
68  }
69  delete d;
70 }
71 
72 StandardActionTreeModel::Item * StandardActionTreeModel::root() const
73 {
74  return d->root;
75 }
76 
77 ActionTreeModel * StandardActionTreeModel::child(int index)
78 {
79  if (index < 0 || index >= d->root->children.size()) {
80  return NULL;
81  }
82 
83  Item * childItem = d->root->children.value(index);
84 
85  if (childItem->children.size() == 0) {
86  return NULL;
87  }
88 
89  if (!d->childModels.contains(childItem)) {
90  d->childModels[childItem] = // new StandardActionTreeModel(childItem);
91  createChild(index);
92  }
93 
94  return d->childModels[childItem];
95 }
96 
97 bool StandardActionTreeModel::isCategory(int index) const
98 {
99  if (index < 0 || index >= d->root->children.size()) {
100  return false;
101  }
102 
103  // std::cout << "### index # " << index << "\n";
104  // std::cout << " d # " << (void *)d << "\n";
105  // std::cout << " d->root # " << (void *)d->root << "\n";
106  // std::cout << " children # " << d->root->children.size() << "\n\n";
107  // foreach (Item i, d->root->children) {
108  // std::cout << qPrintable(i.title) << "\n";
109  // }
110 
111  return d->root->children.at(index)->children.size() != 0;
112 }
113 
114 QString StandardActionTreeModel::selfTitle() const
115 {
116  return d->root->title;
117 }
118 
119 QIcon StandardActionTreeModel::selfIcon() const
120 {
121  return d->root->icon;
122 }
123 
124 QString StandardActionTreeModel::title(int index) const
125 {
126  if (index < 0 || index >= d->root->children.size()) {
127  return QString();
128  }
129 
130  return d->root->children.at(index)->title;
131 }
132 
133 QString StandardActionTreeModel::description(int index) const
134 {
135  if (index < 0 || index >= d->root->children.size()) {
136  return QString();
137  }
138 
139  return d->root->children.at(index)->description;
140 }
141 
142 QIcon StandardActionTreeModel::icon(int index) const
143 {
144  if (index < 0 || index >= d->root->children.size()) {
145  return QIcon();
146  }
147 
148  return d->root->children.at(index)->icon;
149 }
150 
151 QVariant StandardActionTreeModel::data(int index) const
152 {
153  if (index < 0 || index >= d->root->children.size()) {
154  return QVariant();
155  }
156 
157  return d->root->children.at(index)->data;
158 }
159 
160 int StandardActionTreeModel::size() const
161 {
162  return d->root->children.size();
163 }
164 
165 void StandardActionTreeModel::add(Item * item, Item * parent)
166 {
167  if (parent == NULL) parent = d->root;
168 
169  parent->children << item;
170 }
171 
172 void StandardActionTreeModel::add(const QString & title, const QString & description, QIcon icon, const QVariant & data, Item * parent)
173 {
174  add(new Item(title, description, icon, data), parent);
175 }
176 
177 void StandardActionTreeModel::set(int index, Item * item, Item * parent)
178 {
179  if (parent == NULL) parent = d->root;
180  if (index < 0 || index >= parent->children.size()) return;
181 
182  delete parent->children[index];
183  parent->children[index] = item;
184 }
185 
186 void StandardActionTreeModel::set(int index, const QString & title, const QString & description, QIcon icon, const QVariant & data, Item * parent)
187 {
188  set(index, new Item(title, description, icon, data), parent);
189 }
190 
191 void StandardActionTreeModel::removeAt(int index, Item * parent)
192 {
193  if (parent == NULL) parent = d->root;
194  parent->children.removeAt(index);
195 }
196 
197 void StandardActionTreeModel::clear(Item * parent)
198 {
199  if (parent == NULL) parent = d->root;
200  parent->children.clear();
201 }
202 
203 StandardActionTreeModel::Item * StandardActionTreeModel::itemAt(int index, Item * parent)
204 {
205  if (parent == NULL) parent = d->root;
206  return parent->children[index];
207 }
208 
209 } // namespace Lancelot
210 
Lancelot::StandardActionTreeModel::Item::Item
Item(QString itemTitle=QString(), QString itemDescription=QString(), QIcon itemIcon=QIcon(), QVariant itemData=QVariant())
Definition: StandardActionTreeModel.cpp:34
Lancelot::StandardActionTreeModel::removeAt
void removeAt(int index, Item *parent=NULL)
Removes an item.
Definition: StandardActionTreeModel.cpp:191
QObject::children
const QObjectList & children() const
Lancelot::StandardActionTreeModel::title
L_Override QString title(int index) const
Definition: StandardActionTreeModel.cpp:124
Lancelot::StandardActionTreeModel::root
Item * root() const
Definition: StandardActionTreeModel.cpp:72
Lancelot::StandardActionTreeModel::icon
L_Override QIcon icon(int index) const
Definition: StandardActionTreeModel.cpp:142
Lancelot::StandardActionTreeModel::Item::children
QList< Item * > children
Definition: StandardActionTreeModel.h:49
Lancelot::StandardActionTreeModel::createChild
virtual StandardActionTreeModel * createChild(int index)=0
Lancelot::StandardActionTreeModel::Item::~Item
~Item()
Definition: StandardActionTreeModel.cpp:40
Lancelot::StandardActionTreeModel::child
L_Override ActionTreeModel * child(int index)
Definition: StandardActionTreeModel.cpp:77
Lancelot::StandardActionTreeModel::data
QVariant data(int index) const
Definition: StandardActionTreeModel.cpp:151
Lancelot::StandardActionTreeModel::Item
This class represents an item in the list model.
Definition: StandardActionTreeModel.h:35
QHash
StandardActionTreeModel.h
Lancelot::StandardActionTreeModel::add
void add(Item *item, Item *parent=NULL)
Adds a new item into the model.
Definition: StandardActionTreeModel.cpp:165
Lancelot::StandardActionTreeModel::size
L_Override int size() const
Definition: StandardActionTreeModel.cpp:160
Lancelot::StandardActionTreeModel::isCategory
L_Override bool isCategory(int index) const
Definition: StandardActionTreeModel.cpp:97
QString
Lancelot::ActionTreeModel
Definition: ActionTreeModel.h:30
Lancelot::StandardActionTreeModel::selfIcon
L_Override QIcon selfIcon() const
Definition: StandardActionTreeModel.cpp:119
Lancelot::StandardActionTreeModel::clear
void clear(Item *parent=NULL)
Clears all items.
Definition: StandardActionTreeModel.cpp:197
Lancelot::StandardActionTreeModel::StandardActionTreeModel
StandardActionTreeModel()
Definition: StandardActionTreeModel.cpp:45
Lancelot::StandardActionTreeModel::~StandardActionTreeModel
virtual ~StandardActionTreeModel()
Definition: StandardActionTreeModel.cpp:63
Lancelot::StandardActionTreeModel::set
void set(int index, Item *item, Item *parent=NULL)
Replaces existing item at specified index with a new one.
Definition: StandardActionTreeModel.cpp:177
Lancelot::StandardActionTreeModel::itemAt
Item * itemAt(int index, Item *parent=NULL)
Definition: StandardActionTreeModel.cpp:203
Lancelot::StandardActionTreeModel::selfTitle
L_Override QString selfTitle() const
Definition: StandardActionTreeModel.cpp:114
QIcon
Lancelot::StandardActionTreeModel::description
L_Override QString description(int index) const
Definition: StandardActionTreeModel.cpp:133
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:39:12 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

liblancelot

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

kdeplasma-addons API Reference

Skip menu "kdeplasma-addons API Reference"
  •     GroupingDesktop
  •   liblancelot

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal