21 #include "ActivityInfoTree.h"
22 #include "ApplicationInfo.h"
25 #include <QQmlProperty>
26 #include <QQmlComponent>
28 #include <QStandardPaths>
29 #include <QCoreApplication>
30 #include <QTextStream>
32 ActivityInfoTree::ActivityInfoTree(QObject *parent) : QObject(parent),
33 m_currentActivity(NULL)
37 void ActivityInfoTree::setRootMenu(
ActivityInfo *rootMenu)
39 m_rootMenu = rootMenu;
47 QQmlListProperty<ActivityInfo> ActivityInfoTree::menuTree()
49 return QQmlListProperty<ActivityInfo>(
this, NULL, &menuTreeCount, &menuTreeAt);
52 int ActivityInfoTree::menuTreeCount(QQmlListProperty<ActivityInfo> *property)
54 ActivityInfoTree *obj = qobject_cast<ActivityInfoTree*>(
property->object);
56 return obj->m_menuTree.count();
61 ActivityInfo *ActivityInfoTree::menuTreeAt(QQmlListProperty<ActivityInfo> *property,
int index)
63 ActivityInfoTree *obj = qobject_cast<ActivityInfoTree*>(
property->object);
65 return obj->m_menuTree.at(index);
70 ActivityInfo *ActivityInfoTree::menuTree(
int index)
const
72 return m_menuTree.at(index);
75 void ActivityInfoTree::setCurrentActivity(
ActivityInfo *currentActivity)
77 m_currentActivity = currentActivity;
78 emit currentActivityChanged();
81 ActivityInfo *ActivityInfoTree::getCurrentActivity()
const
83 return m_currentActivity;
89 qDebug() <<
"Parent Path= " << menu->getSectionPath();
91 Q_FOREACH( QObject *
object, root->children() )
99 return m_menuTree.at(0);
102 void ActivityInfoTree::menuTreeAppend(
ActivityInfo *menu)
104 m_menuTreeFull.append(menu);
107 void ActivityInfoTree::menuTreeAppend(QQmlEngine *engine,
108 const QDir &menuDir,
const QString &menuFile)
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));
118 qDebug() << menuFile <<
": Failed to load";
122 void ActivityInfoTree::sortByDifficulty()
124 qSort(m_menuTree.begin(), m_menuTree.end(), SortByDifficulty());
125 emit menuTreeChanged();
128 void ActivityInfoTree::sortByName()
130 qSort(m_menuTree.begin(), m_menuTree.end(), SortByName());
131 emit menuTreeChanged();
138 void ActivityInfoTree::filterByTag(
const QString &tag)
141 for(
auto activity: m_menuTreeFull) {
142 if((activity->section().indexOf(tag) != -1 ||
144 (tag ==
"favorite" && activity->favorite())) &&
145 (activity->difficulty() >= ApplicationSettings::getInstance()->filterLevelMin() &&
146 activity->difficulty() <= ApplicationSettings::getInstance()->filterLevelMax())) {
147 m_menuTree.push_back(activity);
151 emit menuTreeChanged();
154 void ActivityInfoTree::filterLockedActivities()
157 if(!ApplicationSettings::getInstance()->isDemoMode() || ApplicationSettings::getInstance()->showLockedActivities())
159 for(
auto activity: m_menuTree) {
161 if(!activity->demo()) {
162 m_menuTree.removeOne(activity);
165 emit menuTreeChanged();
168 void ActivityInfoTree::filterEnabledActivities()
170 for(
auto activity: m_menuTree) {
171 if(!activity->enabled()) {
172 m_menuTree.removeOne(activity);
175 emit menuTreeChanged();
178 void ActivityInfoTree::exportAsSQL()
180 QTextStream cout(stdout);
182 ApplicationSettings::getInstance()->setFilterLevelMin(1);
183 ApplicationSettings::getInstance()->setFilterLevelMax(6);
186 cout <<
"CREATE TABLE activities (" <<
194 "description TEXT," <<
195 "prerequisite TEXT," <<
199 "demo INT);" << endl;
200 cout <<
"DELETE FROM activities" << endl;
203 for(
auto activity: m_menuTree) {
204 cout <<
"INSERT INTO activities VALUES(" <<
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() <<
"\", " <<
222 QObject *ActivityInfoTree::menuTreeProvider(QQmlEngine *engine, QJSEngine *scriptEngine)
224 Q_UNUSED(scriptEngine)
226 ActivityInfoTree *menuTree = new ActivityInfoTree(NULL);
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));
234 QFile file(":/gcompris/src/activities/activities_out.txt");
235 if(!file.open(QFile::ReadOnly)) {
236 qDebug() <<
"Failed to load the activity list";
238 QTextStream in(&file);
241 QString line = in.readLine();
242 if(!line.startsWith(QLatin1String(
"#"))) {
243 QString url = QString(
"qrc:/gcompris/src/activities/%1/ActivityInfo.qml").arg(line);
245 if(!QResource::registerResource(
247 qDebug() <<
"Failed to load the resource file " << line +
".rcc";
249 QQmlComponent componentRoot(engine, QUrl(url));
250 QObject *objectRoot = componentRoot.create();
252 menuTree->menuTreeAppend(qobject_cast<ActivityInfo*>(objectRoot));
254 qDebug() <<
"ERROR: failed to load " << line <<
" " << componentRoot.errors();
260 menuTree->filterByTag(
"favorite");
261 menuTree->filterLockedActivities();
262 menuTree->filterEnabledActivities();
266 void ActivityInfoTree::init()
272 qDebug() <<
"Failed to load the resource file menu.rcc";
275 qDebug() <<
"Failed to load the resource file activities.rcc";
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);;
281 qmlRegisterSingletonType<QObject>(
"GCompris", 1, 0,
"ActivityInfoTree", menuTreeProvider);
282 qmlRegisterType<ActivityInfo>(
"GCompris", 1, 0,
"ActivityInfo");
static QString getFilePath(const QString &file)
Returns an absolute and platform independent path to the passed file.
A QML component holding meta information about an activity.
QString section
Section(s) this activity belongs to.