From dc054c93f940e9be9eb5496c06b48439e7ee2c22 Mon Sep 17 00:00:00 2001 From: khamidou Date: Wed, 27 Jul 2011 19:54:50 +0200 Subject: [PATCH] fixed up loading from an external file --- mainwindow.cpp | 10 +++++++ mainwindow.h | 4 +++ tasklist.cpp | 69 +++++++++++++++++++++++++++++++++++++++--------- tasklist.h | 9 ++++--- tests/first.xml | 25 ++++++++++++++++++ tests/first.xml~ | 13 +++++++++ 6 files changed, 115 insertions(+), 15 deletions(-) create mode 100644 tests/first.xml create mode 100644 tests/first.xml~ diff --git a/mainwindow.cpp b/mainwindow.cpp index 9716148..76f146e 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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() diff --git a/mainwindow.h b/mainwindow.h index dfb47d6..da45e3e 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -1,7 +1,10 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H +#include #include +#include "tasklist.h" + class MainWindow : public QMainWindow { @@ -13,6 +16,7 @@ public: private: QListView *sessionView; + TaskList *tasks; }; #endif // MAINWINDOW_H diff --git a/tasklist.cpp b/tasklist.cpp index 1361f90..7285569 100644 --- a/tasklist.cpp +++ b/tasklist.cpp @@ -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::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 diff --git a/tasklist.h b/tasklist.h index 784ae41..e9e64b9 100644 --- a/tasklist.h +++ b/tasklist.h @@ -2,10 +2,11 @@ #define TASKLIST_H #include +#include #include #include -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 */ -public: - TaskList(); +QList _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 index 0000000..5b1f0ee --- /dev/null +++ b/tests/first.xml @@ -0,0 +1,25 @@ + + + + Scale exercises + + 3 + 5 + + + 3 + 5 + + + + Rhythm practice + + 3 + 5 + + + 3 + 5 + + + diff --git a/tests/first.xml~ b/tests/first.xml~ new file mode 100644 index 0000000..41b4c8b --- /dev/null +++ b/tests/first.xml~ @@ -0,0 +1,13 @@ + + + Task 1 + + 3 + 5 + + + 3 + 5 + + + -- 2.43.0