From: khamidou Date: Thu, 14 Jul 2011 12:00:43 +0000 (+0200) Subject: moving to sqlrelationalmodel X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=cf8ec6aa898749f1dee0edca36e0ef38a4edbb4b;p=tt.git moving to sqlrelationalmodel --- diff --git a/mainwindow.cpp b/mainwindow.cpp index 6470dfa..0901bc5 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -8,16 +8,18 @@ MainWindow::MainWindow(QWidget *parent) : setWindowTitle(tr("Task Tracker")); setMinimumSize(640, 480); - table = new TaskModel(); + initDb(); + model = new QSqlRelationalTableModel(); + initializeModel(model); + tableView = new QTableView(); QPushButton *button = new QPushButton("yop"); tabWidget->addTab(tableView, tr("Tasks")); tabWidget->addTab(button, tr("Stats")); - - tableView->setModel(table); tableView->setItemDelegate(new TableDelegate()); + tableView->setModel(model); tableView->resizeColumnsToContents(); tableView->adjustSize(); @@ -32,5 +34,5 @@ MainWindow::MainWindow(QWidget *parent) : MainWindow::~MainWindow() { - + closeDb(); } diff --git a/mainwindow.h b/mainwindow.h index ede8501..85c8567 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -1,6 +1,7 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H +#include #include #include #include @@ -23,8 +24,9 @@ public: private: Ui::MainWindow *ui; QTabWidget *tabWidget; - TaskModel *table; + //TaskModel *table; QTableView *tableView; + QSqlRelationalTableModel *model; }; #endif // MAINWINDOW_H diff --git a/tabledelegate.cpp b/tabledelegate.cpp index 739b57d..98eea96 100644 --- a/tabledelegate.cpp +++ b/tabledelegate.cpp @@ -3,16 +3,14 @@ TableDelegate::TableDelegate(QWidget *parent) : QStyledItemDelegate(parent) -{ - map = new QSignalMapper(this); +{ } QWidget *TableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const -{ - std::cout << index.column(); +{ if (index.column() == 2) { QComboBox *lWidget = new QComboBox(parent); lWidget->addItem("lololol"); @@ -20,10 +18,6 @@ QWidget *TableDelegate::createEditor(QWidget *parent, lWidget->addItem("lodslolol"); lWidget->addItem("dflol"); - - connect(lWidget, SIGNAL(editingFinished()), - this, SLOT(commitAndCloseEditor())); - return lWidget; } else { return QStyledItemDelegate::createEditor(parent, option, index); @@ -41,8 +35,14 @@ void TableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, QSize TableDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { } -void TableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {} +void TableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { + QComboBox *box = qobject_cast(editor); + if (box == NULL) + return; +} void TableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, - const QModelIndex &index) const { } + const QModelIndex &index) const { + +} void TableDelegate::commitAndCloseEditor() { } diff --git a/tabledelegate.h b/tabledelegate.h index 3d5760b..a14f926 100644 --- a/tabledelegate.h +++ b/tabledelegate.h @@ -28,7 +28,7 @@ class TableDelegate : public QStyledItemDelegate void commitAndCloseEditor(); private: - QSignalMapper *map; /* On en a besoin pour les boutons et les combobox */ + QSignalMapper *signalMapper; /* Needed for the comboboxes and buttons */ }; #endif // TABLEDELEGATE_H diff --git a/taskmodel.cpp b/taskmodel.cpp index 86a63c1..16ad84c 100644 --- a/taskmodel.cpp +++ b/taskmodel.cpp @@ -1,54 +1,48 @@ #include "taskmodel.h" -TaskModel::TaskModel(QObject *parent) : QAbstractTableModel(parent) -{ +void initDb() { + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(":memory:"); + if (!db.open()) { + QMessageBox::critical(0, qApp->tr("Cannot open database"), + qApp->tr("Unable to establish a database connection.\n" + "Click Cancel to exit."), QMessageBox::Cancel); + } + createDb(); } -QVariant TaskModel::data(const QModelIndex &index, int role) const -{ - if (!index.isValid()) - return QVariant(); - - if (index.row() >= 4 || index.row() < 0) - return QVariant(); - - if (role == Qt::DisplayRole) { - if (index.column() == 0) - return tr("gerard"); - else if (index.column() == 1) - return tr("ho"); - else if (index.column() == 2) - return tr("paf"); - else if (index.column() == 3) - return tr("paf"); - } - return QVariant(); +bool createDb() { + QSqlQuery query; + query.exec("create table tasks (id integer primary key," + "time_spent integer, description varchar(255)," + "category_id integer);"); + + query.exec("create table tasks_types (id integer primary key," + "type varchar(255));"); + query.exec("insert into tasks_types values(1, 'Travail');"); + query.exec("insert into tasks values(1, 1, 'Finir tt', 1)"); + return true; } -QVariant TaskModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (role != Qt::DisplayRole) - return QVariant(); - - if (orientation == Qt::Horizontal) { - switch (section) { - case 0: - return tr("Task"); - - case 1: - return tr("Total time spent on this task"); - - case 2: - return tr("Start counting"); - - case 3: - return tr("Category"); - - - default: - return QVariant(); - } - } - return QVariant(); +void initializeModel(QSqlRelationalTableModel *model) + { + model->setTable("tasks"); + model->setEditStrategy(QSqlTableModel::OnManualSubmit); + model->setRelation(3, QSqlRelation("tasks_types", "type", "Type")); + model->removeColumn(0); + model->setHeaderData(0, Qt::Horizontal, QObject::tr("Task")); + model->setHeaderData(1, Qt::Horizontal, QObject::tr("Time spent today")); + model->setHeaderData(2, Qt::Horizontal, QObject::tr("Category")); + bool t = model->insertColumn(2); + std::cout << t << std::endl; + + model->setHeaderData(2, Qt::Horizontal, QObject::tr("Start counting")); + + model->select(); + } + +void closeDb() { + QSqlDatabase db = QSqlDatabase::database(); + db.close(); } diff --git a/taskmodel.h b/taskmodel.h index a307ab6..645e498 100644 --- a/taskmodel.h +++ b/taskmodel.h @@ -1,31 +1,16 @@ #ifndef TASKMODEL_H #define TASKMODEL_H -#include +#include +#include +#include +#include +#include -class TaskModel : public QAbstractTableModel -{ - Q_OBJECT -public: - TaskModel(QObject *parent=0); - - int rowCount(const QModelIndex &parent) const { return 2; }; - int columnCount(const QModelIndex &parent) const { return 4; }; - QVariant data(const QModelIndex &index, int role) const; - QVariant headerData(int section, Qt::Orientation orientation, int role) const; - Qt::ItemFlags flags(const QModelIndex &index) const { - if (!index.isValid()) - return Qt::ItemIsEnabled; - - return QAbstractTableModel::flags(index) | Qt::ItemIsEditable; }; - - bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) { return false; }; - bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex()) { return false; }; - bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex()) {return false; }; - -private: - QList< QPair > listOfPairs; -}; +void initDb(); +bool createDb(); +void initializeModel(QSqlRelationalTableModel *model); +void closeDb(); #endif // TASKMODEL_H diff --git a/tt.pro b/tt.pro index fcd5b5b..e9b5eb7 100644 --- a/tt.pro +++ b/tt.pro @@ -2,7 +2,7 @@ # Project created by QtCreator 2011-06-25T14:16:34 # ------------------------------------------------- QT += network \ - xml + xml sql TARGET = tt TEMPLATE = app SOURCES += main.cpp \