• Skip to content
  • Skip to link menu
KDE API Documentation - ActivityInfoTree.cpp Source File (GCompris-qt)
  • KDE Home
  • Contact Us
 

GCompris-qt

  • src
  • core
ActivityInfoTree.cpp
1 /* GCompris - ActivityInfoTree.cpp
2  *
3  * Copyright (C) 2014 Bruno Coudoin <bruno.coudoin@gcompris.net>
4  *
5  * Authors:
6  * Bruno Coudoin <bruno.coudoin@gcompris.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21 #include "ActivityInfoTree.h"
22 #include "ApplicationInfo.h"
23 
24 #include <QtDebug>
25 #include <QQmlProperty>
26 #include <QQmlComponent>
27 #include <QResource>
28 #include <QStandardPaths>
29 #include <QCoreApplication>
30 #include <QTextStream>
31 
32 ActivityInfoTree::ActivityInfoTree(QObject *parent) : QObject(parent),
33  m_currentActivity(NULL)
34 {}
35 
36 
37 void ActivityInfoTree::setRootMenu(ActivityInfo *rootMenu)
38 {
39  m_rootMenu = rootMenu;
40 }
41 
42 ActivityInfo *ActivityInfoTree::getRootMenu() const
43 {
44  return m_rootMenu;
45 }
46 
47 QQmlListProperty<ActivityInfo> ActivityInfoTree::menuTree()
48 {
49  return QQmlListProperty<ActivityInfo>(this, NULL, &menuTreeCount, &menuTreeAt);
50 }
51 
52 int ActivityInfoTree::menuTreeCount(QQmlListProperty<ActivityInfo> *property)
53 {
54  ActivityInfoTree *obj = qobject_cast<ActivityInfoTree*>(property->object);
55  if(obj)
56  return obj->m_menuTree.count();
57  else
58  return 0;
59 }
60 
61 ActivityInfo *ActivityInfoTree::menuTreeAt(QQmlListProperty<ActivityInfo> *property, int index)
62 {
63  ActivityInfoTree *obj = qobject_cast<ActivityInfoTree*>(property->object);
64  if(obj)
65  return obj->m_menuTree.at(index);
66  else
67  return 0;
68 }
69 
70 ActivityInfo *ActivityInfoTree::menuTree(int index) const
71 {
72  return m_menuTree.at(index);
73 }
74 
75 void ActivityInfoTree::setCurrentActivity(ActivityInfo *currentActivity)
76 {
77  m_currentActivity = currentActivity;
78  emit currentActivityChanged();
79 }
80 
81 ActivityInfo *ActivityInfoTree::getCurrentActivity() const
82 {
83  return m_currentActivity;
84 }
85 
86 ActivityInfo *ActivityInfoTree::getParentActivity(ActivityInfo *root, ActivityInfo *menu)
87 {
88 
89  qDebug() << "Parent Path= " << menu->getSectionPath();
90 
91  Q_FOREACH( QObject *object, root->children() )
92  {
93  ActivityInfo *activityInfo = qobject_cast<ActivityInfo*>(object);
94  if(activityInfo->section() == menu->section()) {
95  return activityInfo;
96  }
97  }
98 
99  return m_menuTree.at(0);
100 }
101 
102 void ActivityInfoTree::menuTreeAppend(ActivityInfo *menu)
103 {
104  m_menuTreeFull.append(menu);
105 }
106 
107 void ActivityInfoTree::menuTreeAppend(QQmlEngine *engine,
108  const QDir &menuDir, const QString &menuFile)
109 {
110  QQmlComponent component(engine,
111  QUrl::fromLocalFile(menuDir.absolutePath() + '/' + menuFile));
112  QObject *object = component.create();
113  if(component.isReady()) {
114  if(QQmlProperty::read(object, "section").toString() == "/") {
115  menuTreeAppend(qobject_cast<ActivityInfo*>(object));
116  }
117  } else {
118  qDebug() << menuFile << ": Failed to load";
119  }
120 }
121 
122 void ActivityInfoTree::sortByDifficulty()
123 {
124  qSort(m_menuTree.begin(), m_menuTree.end(), SortByDifficulty());
125  emit menuTreeChanged();
126 }
127 
128 void ActivityInfoTree::sortByName()
129 {
130  qSort(m_menuTree.begin(), m_menuTree.end(), SortByName());
131  emit menuTreeChanged();
132 }
133 
134 // Filter the current activity list by the given tag
135 // the tag 'all' means no filter
136 // the tag 'favorite' means only marked as favorite
137 // The level is also filtered based on the global property
138 void ActivityInfoTree::filterByTag(const QString &tag)
139 {
140  m_menuTree.clear();
141  for(auto activity: m_menuTreeFull) {
142  if((activity->section().indexOf(tag) != -1 ||
143  tag == "all" ||
144  (tag == "favorite" && activity->favorite())) &&
145  (activity->difficulty() >= ApplicationSettings::getInstance()->filterLevelMin() &&
146  activity->difficulty() <= ApplicationSettings::getInstance()->filterLevelMax())) {
147  m_menuTree.push_back(activity);
148  }
149  }
150  sortByDifficulty();
151  emit menuTreeChanged();
152 }
153 
154 void ActivityInfoTree::filterLockedActivities()
155 {
156  // If we have the full version or if we show all the activities, we don't need to do anything
157  if(!ApplicationSettings::getInstance()->isDemoMode() || ApplicationSettings::getInstance()->showLockedActivities())
158  return;
159  for(auto activity: m_menuTree) {
160  // Remove non free activities if needed. We need to already have a menuTree filled!
161  if(!activity->demo()) {
162  m_menuTree.removeOne(activity);
163  }
164  }
165  emit menuTreeChanged();
166 }
167 
168 void ActivityInfoTree::filterEnabledActivities()
169 {
170  for(auto activity: m_menuTree) {
171  if(!activity->enabled()) {
172  m_menuTree.removeOne(activity);
173  }
174  }
175  emit menuTreeChanged();
176 }
177 
178 void ActivityInfoTree::exportAsSQL()
179 {
180  QTextStream cout(stdout);
181 
182  ApplicationSettings::getInstance()->setFilterLevelMin(1);
183  ApplicationSettings::getInstance()->setFilterLevelMax(6);
184  filterByTag("all");
185 
186  cout << "CREATE TABLE activities (" <<
187  "id INT UNIQUE, " <<
188  "name TEXT," <<
189  "section TEXT," <<
190  "author TEXT," <<
191  "difficulty INT," <<
192  "icon TEXT," <<
193  "title TEXT," <<
194  "description TEXT," <<
195  "prerequisite TEXT," <<
196  "goal TEXT," <<
197  "manual TEXT," <<
198  "credit TEXT," <<
199  "demo INT);" << endl;
200  cout << "DELETE FROM activities" << endl;
201 
202  int i(0);
203  for(auto activity: m_menuTree) {
204  cout << "INSERT INTO activities VALUES(" <<
205  i++ << ", " <<
206  "'" << activity->name() << "', " <<
207  "'" << activity->section() << "', " <<
208  "'" << activity->author() << "', " <<
209  activity->difficulty() << ", " <<
210  "'" << activity->icon() << "', " <<
211  "\"" << activity->title() << "\", " <<
212  "\"" << activity->description() << "\", " <<
213  "\"" << activity->prerequisite() << "\", " <<
214  "\"" << activity->goal().toHtmlEscaped() << "\", " <<
215  "\"" << activity->manual().toHtmlEscaped() << "\", " <<
216  "\"" << activity->credit() << "\", " <<
217  activity->demo() <<
218  ");" << endl;
219  }
220 }
221 
222 QObject *ActivityInfoTree::menuTreeProvider(QQmlEngine *engine, QJSEngine *scriptEngine)
223 {
224  Q_UNUSED(scriptEngine)
225 
226  ActivityInfoTree *menuTree = new ActivityInfoTree(NULL);
227 
228  QQmlComponent componentRoot(engine,
229  QUrl("qrc:/gcompris/src/activities/menu/ActivityInfo.qml"));
230  QObject *objectRoot = componentRoot.create();
231  menuTree->setRootMenu(qobject_cast<ActivityInfo*>(objectRoot));
232 
233 
234  QFile file(":/gcompris/src/activities/activities_out.txt");
235  if(!file.open(QFile::ReadOnly)) {
236  qDebug() << "Failed to load the activity list";
237  }
238  QTextStream in(&file);
239  while (!in.atEnd())
240  {
241  QString line = in.readLine();
242  if(!line.startsWith(QLatin1String("#"))) {
243  QString url = QString("qrc:/gcompris/src/activities/%1/ActivityInfo.qml").arg(line);
244 
245  if(!QResource::registerResource(
246  ApplicationInfo::getFilePath(line + ".rcc")))
247  qDebug() << "Failed to load the resource file " << line + ".rcc";
248 
249  QQmlComponent componentRoot(engine, QUrl(url));
250  QObject *objectRoot = componentRoot.create();
251  if(objectRoot) {
252  menuTree->menuTreeAppend(qobject_cast<ActivityInfo*>(objectRoot));
253  } else {
254  qDebug() << "ERROR: failed to load " << line << " " << componentRoot.errors();
255  }
256  }
257  }
258  file.close();
259 
260  menuTree->filterByTag("favorite");
261  menuTree->filterLockedActivities();
262  menuTree->filterEnabledActivities();
263  return menuTree;
264 }
265 
266 void ActivityInfoTree::init()
267 {
268  if(!QResource::registerResource(ApplicationInfo::getFilePath("core.rcc")))
269  qDebug() << "Failed to load the resource file " << ApplicationInfo::getFilePath("core.rcc");
270 
271  if(!QResource::registerResource(ApplicationInfo::getFilePath("menu.rcc")))
272  qDebug() << "Failed to load the resource file menu.rcc";
273 
274  if(!QResource::registerResource(ApplicationInfo::getFilePath("activities.rcc")))
275  qDebug() << "Failed to load the resource file activities.rcc";
276 
277  if(QResource::registerResource(QStandardPaths::writableLocation(QStandardPaths::DataLocation) +
278  "/data2/" + QString("full-%1.rcc").arg(COMPRESSED_AUDIO)))
279  qDebug() << "Registered the pre-download " << QString("full-%1.rcc").arg(COMPRESSED_AUDIO);;
280 
281  qmlRegisterSingletonType<QObject>("GCompris", 1, 0, "ActivityInfoTree", menuTreeProvider);
282  qmlRegisterType<ActivityInfo>("GCompris", 1, 0, "ActivityInfo");
283 }
284 
ApplicationInfo::getFilePath
static QString getFilePath(const QString &file)
Returns an absolute and platform independent path to the passed file.
Definition: ApplicationInfo.cpp:116
ActivityInfo
A QML component holding meta information about an activity.
Definition: ActivityInfo.h:37
ActivityInfo::section
QString section
Section(s) this activity belongs to.
Definition: ActivityInfo.h:56
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Tue Jun 2 2015 21:47:47 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

GCompris-qt

Skip menu "GCompris-qt"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Modules

Class Picker

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