From: khamidou Date: Fri, 29 Jul 2011 11:57:47 +0000 (+0200) Subject: making the big switch to webkit X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=HEAD;p=st.git making the big switch to webkit --- diff --git a/main.cpp b/main.cpp index 6e7efd9..ffc55cb 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ int main(int argc, char *argv[]) { + qRegisterMetaType("Task"); QApplication a(argc, argv); MainWindow w; w.show(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 99411c3..2d222e2 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -35,6 +35,7 @@ MainWindow::MainWindow(QWidget *parent) taskView->setModel((QAbstractItemModel *) tasks); connect(summaryView, SIGNAL(clicked(QModelIndex)), this, SLOT(changeTaskList(QModelIndex))); + connect(newListButton, SIGNAL(clicked()), this, SLOT(createNewList())); frame->setLayout(gridLayout); setCentralWidget(frame); @@ -61,3 +62,13 @@ void MainWindow::changeTaskList(const QModelIndex &index) delete oldModel; } + +void MainWindow::createNewList() +{ + QString title = newListField->text(); + if (title == "") + return; + + SummaryList * model = (SummaryList *) summaryView->model(); + model->createNewList(title); +} diff --git a/mainwindow.h b/mainwindow.h index 5d2c7e2..7875977 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -17,6 +17,7 @@ public: public slots: void changeTaskList(const QModelIndex & index); + void createNewList(void); private: QListView *summaryView; diff --git a/st.pro b/st.pro index 6dbab7b..f3bc10a 100644 --- a/st.pro +++ b/st.pro @@ -8,8 +8,10 @@ TEMPLATE = app SOURCES += main.cpp \ mainwindow.cpp \ tasklist.cpp \ - summarylist.cpp + summarylist.cpp \ + taskdelegate.cpp HEADERS += mainwindow.h \ tasklist.h \ - summarylist.h + summarylist.h \ + taskdelegate.h OTHER_FILES += README.txt diff --git a/summarylist.cpp b/summarylist.cpp index e3a2686..d2d615f 100644 --- a/summarylist.cpp +++ b/summarylist.cpp @@ -26,8 +26,8 @@ QVariant SummaryList::data ( const QModelIndex & index, int role) const /* otherwise, open the file and get the session name */ TaskList t(f); - if(t.sessionName() != "") - return t.sessionName(); + if(t.todoListName() != "") + return t.todoListName(); else return _directory.entryList().at(index.row()); } @@ -44,3 +44,10 @@ QString SummaryList::fileNameAt(const QModelIndex &index) { return _directory.absoluteFilePath(_directory.entryList().at(index.row())); } + +bool SummaryList::createNewList(QString listName) +{ + TaskList l(listName); + emit QAbstractItemModel::dataChanged(index(0), index(rowCount())); + return true; +} diff --git a/summarylist.h b/summarylist.h index 593818f..9f38c5f 100644 --- a/summarylist.h +++ b/summarylist.h @@ -18,6 +18,7 @@ public: QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const; int rowCount (const QModelIndex & parent = QModelIndex() ) const; QString fileNameAt(const QModelIndex &index); + bool createNewList(QString listName); }; #endif // SummaryList_H diff --git a/tasklist.cpp b/tasklist.cpp index 2253f31..04145cf 100644 --- a/tasklist.cpp +++ b/tasklist.cpp @@ -9,25 +9,44 @@ Task::Task(QDomElement &root) _title = titleElement.text(); - QDomElement length = titleElement.nextSiblingElement(); + QDomElement length = titleElement.nextSiblingElement("length"); QDomElement hours = length.firstChildElement("hours"); int hour = hours.text().toInt(); - QDomElement minutes = hours.nextSiblingElement(); + QDomElement minutes = hours.nextSiblingElement("minutes"); int minute = minutes.text().toInt(); _taskLength = QTime(hour, minute, 0); - QDomElement remaining = length.nextSiblingElement(); + QDomElement remaining = length.nextSiblingElement("remaining"); hours = remaining.firstChildElement("hours"); hour = hours.text().toInt(); - minutes = hours.nextSiblingElement(); + minutes = hours.nextSiblingElement("minutes"); minute = minutes.text().toInt(); _timeRemaining = QTime(hour, minute, 0); } +TaskList::TaskList(QString listName) +{ + QDomDocument doc; + QDomElement root = doc.createElement("todolist"); + doc.appendChild(root); + root.setAttribute("name", listName); + + QFile file("./tests/" + listName + ".xml"); + if(!file.open(QIODevice::WriteOnly | QFile::Truncate)) { + QMessageBox::critical(0, tr("Unable to create Todo List"), + tr("An error happened while creating the list file : ") + file.errorString()); + return; + } + + QTextStream stream(&file); + doc.save(stream, 4); + stream.flush(); +} + TaskList::TaskList(QFile &f) { QDomDocument doc; @@ -45,20 +64,19 @@ TaskList::TaskList(QFile &f) return; } - _sessionName = doc.firstChildElement().attribute("name"); - QDomElement child = doc.firstChildElement("session").firstChildElement(); - + _todoListName = doc.firstChildElement("todolist").attribute("name"); + QDomElement child = doc.firstChildElement("todolist").firstChildElement("task"); - if (child.isNull()) { + if (doc.firstChildElement("todolist").isNull()) { QMessageBox::critical(0, "Critical error !", "The Document doesn't have a root tag"); return; } - do { + while(!child.isNull()) { Task *t = new Task(child); _taskList.append(t); - child = child.nextSiblingElement(); - } while(!child.isNull()); + child = child.nextSiblingElement("task"); + } } @@ -88,3 +106,8 @@ int TaskList::rowCount (const QModelIndex & parent) const { return _taskList.length(); } + +int TaskList::columnCount( const QModelIndex & parent = QModelIndex() ) const +{ + return 1; +} diff --git a/tasklist.h b/tasklist.h index b6a1588..6366b87 100644 --- a/tasklist.h +++ b/tasklist.h @@ -6,27 +6,30 @@ #include #include -class Task : public QObject +class Task //: public QObject { /** this class models a task, which is a portion of a session */ - Q_OBJECT +// Q_OBJECT QString _title; QTime _taskLength; QTime _timeRemaining; public: + Task() : _title("Unnamed task"), _taskLength(), _timeRemaining() { }; Task(QString title, QTime taskLength) : _title(title), _taskLength(taskLength), _timeRemaining() { }; Task(QDomElement& root); QString title(void) { return _title; }; QTime taskLength(void) { return _taskLength; } }; +Q_DECLARE_METATYPE(Task) -class TaskList : QAbstractListModel +class TaskList : QAbstractItemModel { + /** TaskList is our model class. It loads and saves tasks from an xml file. Format of the xml file : - + Task 1 @@ -40,21 +43,22 @@ class TaskList : QAbstractListModel ... - + */ QList _taskList; -QString _sessionName; +QString _todoListName; public: TaskList(QFile& f); + TaskList(QString listName); ~TaskList(); QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const; int rowCount (const QModelIndex & parent = QModelIndex() ) const; - + int columnCount( const QModelIndex & parent = QModelIndex() ) const; + QModelIndex parent ( const QModelIndex & index ) const { return QModelIndex(); }; /* accessors */ - QString sessionName() { return _sessionName; }; + QString todoListName() { return _todoListName; }; }; - #endif // TASKLIST_H diff --git a/tests/first.xml b/tests/first.xml index acd170d..db0509a 100644 --- a/tests/first.xml +++ b/tests/first.xml @@ -1,5 +1,5 @@ - + Scale exercises @@ -34,4 +34,4 @@ - + diff --git a/tests/second.xml b/tests/second.xml index a2b033f..30a5ef8 100644 --- a/tests/second.xml +++ b/tests/second.xml @@ -1,5 +1,5 @@ - + Melodic exercises @@ -34,4 +34,4 @@ - +