]> khamidou — GitHub mirrors - st.git/commitdiff
fixed up loading from an external file
authorkhamidou <khamidou@gmail.com>
Wed, 27 Jul 2011 17:54:50 +0000 (19:54 +0200)
committerkhamidou <khamidou@gmail.com>
Wed, 27 Jul 2011 17:54:50 +0000 (19:54 +0200)
mainwindow.cpp
mainwindow.h
tasklist.cpp
tasklist.h
tests/first.xml [new file with mode: 0644]
tests/first.xml~ [new file with mode: 0644]

index 9716148bc2026cf172deca6d9160ea90d62a7eda..76f146ed1fe53e033d87b607819c23e56285ae79 100644 (file)
@@ -3,6 +3,16 @@
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)
 {
+
+    sessionView = new QListView(this);
+    QFile fd(tr("tests/first.xml"));
+    if(!fd.open(QIODevice::ReadOnly))
+       return;
+
+    tasks = new TaskList(fd);
+    sessionView->setModel((QAbstractItemModel *) tasks);
+    setCentralWidget(sessionView);
+
 }
 
 MainWindow::~MainWindow()
index dfb47d699a84b3ed52e00daa0832900e2e11e72c..da45e3e8c30ebbea1202682fb639ee36347e4907 100644 (file)
@@ -1,7 +1,10 @@
 #ifndef MAINWINDOW_H
 #define MAINWINDOW_H
 
+#include <QtCore>
 #include <QtGui>
+#include "tasklist.h"
+
 
 class MainWindow : public QMainWindow
 {
@@ -13,6 +16,7 @@ public:
 
 private:
     QListView *sessionView;
+    TaskList *tasks;
 };
 
 #endif // MAINWINDOW_H
index 1361f9055be11b84c27d781d31cf2a344856343b..72855697c5b20dcfb72948e4d836f70ea6e69937 100644 (file)
@@ -3,40 +3,85 @@
 Task::Task(QDomElement &root)
 {
     QDomElement titleElement = root.firstChildElement("title");
+    if (titleElement.isNull())
+       QMessageBox(QMessageBox::Critical, "Error !", "Error : the xml input file is malformed");
+    /* FIXME : add it to the rest */
+
     _title = titleElement.text();
 
-    QDomElement length = titleElement.nextSibling();
-    QDomElement hours = length.firstChild("hours");    
+    QDomElement length = titleElement.nextSiblingElement();
+    QDomElement hours = length.firstChildElement("hours");
 
     int hour = hours.text().toInt();
 
-    QDomElement minutes = hours.nextSibling();
-
+    QDomElement minutes = hours.nextSiblingElement();
     int minute = minutes.text().toInt();
 
     _taskLength = QTime(hour, minute, 0);
 
-    /* Fixme : implement remaining */
+    QDomElement remaining = length.nextSiblingElement();
+    hours = remaining.firstChildElement("hours");
+    hour = hours.text().toInt();
+    minutes = hours.nextSiblingElement();
+    minute = minutes.text().toInt();
+    _timeRemaining = QTime(hour, minute, 0);
+
+    qDebug() << minute << " " << hour;
+}
+
+TaskList::TaskList(QFile &f)
+{
+    QDomDocument doc;
+
+    QString s;
+    int errorLine;
+
+    if(!doc.setContent(/*(QIODevice *)*/ &f, &s, &errorLine)) {
+       f.close();
+       QString errorMsg = QString("Error while parsing the XML file %1!").arg(f.fileName());
+       QString t = s + "error at line %1";
+       QString errorText = t.arg(errorLine);
+
+       QMessageBox::critical(0, errorMsg, errorText);
+       return;
+    }
+
+    QDomElement child = doc.firstChildElement("session").firstChildElement();
+
+    if (child.isNull()) {
+       QMessageBox::critical(0, "Critical error !", "The Document doesn't have a root tag");
+       return;
+    }
+
+    do {
+       Task *t = new Task(child);
+       qDebug() << child.tagName();
+       _taskList.append(t);
+       child = child.nextSiblingElement();
+    } while(!child.isNull());
+
 }
 
-TaskList::TaskList()
+TaskList::~TaskList()
 {
+    QList<Task* >::iterator i;
+    for (i = _taskList.begin(); i != _taskList.end(); ++i)
+       delete(*i);
 }
 
 QVariant TaskList::data ( const QModelIndex & index, int role) const
 {
-    /*
     if (!index.isValid())
         return QVariant();
-    if (index.row() >= machineList.size() || index.row() < 0)
+    if (index.row() >= _taskList.size() || index.row() < 0)
         return QVariant();
 
-    if (role == Qt::DisplayRole)
-    return machineList.at(index.row());
+    if (role == Qt::DisplayRole) {
+           Task *t = _taskList.at(index.row());
+           return t->title();
+    }
 
     return QVariant();
-    */
-    return QVariant();
 }
 
 int TaskList::rowCount (const QModelIndex & parent) const
index 784ae412459f3c44504d637af91d98489a08087f..e9e64b91781465e6c8466611a45cfe8831b9c934 100644 (file)
@@ -2,10 +2,11 @@
 #define TASKLIST_H
 
 #include <QtCore>
+#include <QtGui>
 #include <QtXml>
 #include <QAbstractListModel>
 
-class Task
+class Task : public QObject
 {
     /** this class models a task, which is a portion of a session */
     Q_OBJECT
@@ -42,9 +43,11 @@ class TaskList : QAbstractListModel
                </session>
  */
 
-public:
-    TaskList();
+QList <Task *> _taskList;
+public:    
     TaskList(QFile& f);
+    ~TaskList();
+
     QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
     int rowCount (const QModelIndex & parent = QModelIndex() ) const;
 
diff --git a/tests/first.xml b/tests/first.xml
new file mode 100644 (file)
index 0000000..5b1f0ee
--- /dev/null
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<session>
+    <task>
+       <title>Scale exercises</title>
+       <length>
+           <hours>3</hours>
+           <minutes>5</minutes>
+       </length>
+       <elapsed>
+           <hours>3</hours>
+           <minutes>5</minutes>
+       </elapsed>
+    </task>
+    <task>
+       <title>Rhythm practice</title>
+       <length>
+           <hours>3</hours>
+           <minutes>5</minutes>
+       </length>
+       <elapsed>
+           <hours>3</hours>
+           <minutes>5</minutes>
+       </elapsed>
+       </task>
+</session>
diff --git a/tests/first.xml~ b/tests/first.xml~
new file mode 100644 (file)
index 0000000..41b4c8b
--- /dev/null
@@ -0,0 +1,13 @@
+               <session>
+                   <task>
+                       <title>Task 1</title>
+                       <length>
+                           <hours>3</hours>
+                           <minutes>5</minutes>
+                       </length>
+                       <elasped>
+                           <hours>3</hours>
+                           <minutes>5</minutes>
+                       </elapsed>
+                   </task>
+                   <task>