gsafetutorial.h
#ifndef GSAFETUTORIAL_GSAFE
#define GSAFETUTORIAL_GSAFE
#include <QtCore>
#include <QtGui>
#include "datalib.h"
#include "dialib.h"
class MainDialog : public QDialog , public HDialogData
{
Q_OBJECT
public:
MainDialog(QWidget *parent,bool createemptydb);
~MainDialog();
HTable *ptable;
HList *plist;
public slots:
int showError(QString err);
int insertRecord(void);
int editRecord(QString id);
};
#endif
gsafetutorial.cpp
#include <QtCore>
#include <QtSql>
#include <QtGui>
#include "gsafetutorial.h"
#include "dconsole.h"
#include "datalib.h"
#include "hfactory.h"
#include "guilib.h"
#include "dialib.h"
#define DBFILE "c:\\temp\\testdb.sqllite"
MainDialog::MainDialog(QWidget *parent,bool createemptydb)
: QDialog(parent)
{
HSqlHandler sqlhandler;
HFactory factory;
if(factory.readFromFile("metadata.xml"))
{
error("Can't read the meta-data definition xml file! (metadata.xml)");
exit(1);
};
if(!connect(&factory,SIGNAL(errorSignal(QString)),this,SLOT(showError(QString))))
error("Can't connect error handler!"); ptable = factory.genHTable("excercises"); plist = factory.genHList("excercises");
HTable *typt = factory.genHTable("typetable"); if(createemptydb)
{
sdebug("\n");
sqlhandler.submit0ResultQuery(typt->sqlCreateString(),"Error creating database...");
if(sqlhandler.errorStatus())
{
QMessageBox::information(this,"Error creating empty database...","Error (1):-(");
return;
}
sqlhandler.submit0ResultQuery("INSERT INTO types VALUES(\'1\',\'Homework\');","Error inserting data");
sqlhandler.submit0ResultQuery("INSERT INTO types VALUES(\'2\',\'Workplace\');","Error inserting data");
sqlhandler.submit0ResultQuery("INSERT INTO types VALUES(\'3\',\'Friend\');","Error inserting data");
if(sqlhandler.errorStatus())
{
QMessageBox::information(this,"Error creating empty database...","Error (2) :-(");
return;
}
sqlhandler.submit0ResultQuery(ptable->sqlCreateString(),"Error creating empty database...");
if(sqlhandler.errorStatus())
QMessageBox::information(this,"Error creating empty database...","Error (3) :-(");
else
QMessageBox::information(this,"Creating empty database...","Success :-)");
} makeGui(this,"My tasks (gSAFE tutorial)",plist,NULL,
"EscC|Vert|StrToE|DeleteData|DestructiveClose|NonModal|InScroll","Hit doubleclick to edit!","insert","refresh list",NULL,640,480);
connect(plist,SIGNAL(actionOnRecord(QString)),this,SLOT(editRecord(QString)));
connect(toolbutton2,SIGNAL(clicked()),plist,SLOT(readList()));
connect(toolbutton1,SIGNAL(clicked()),this,SLOT(insertRecord()));
sdebug("Read list from the database...");
plist->readList();
sdebug("...finished.");
sdebug(ptable->sqlCreateString());
}
int MainDialog::insertRecord(void)
{
ptable->returnToDefault();
if(HDialog::run(this,"Insert a task",ptable,NULL,"Ok|OkIsAccept|EscC|StrToE|InScroll","Task","","",NULL,400,400))
{
ptable->insertRecord();
HRefreshAgent::notify("excerc");
}
return 0;
}
int MainDialog::editRecord(QString id)
{
ptable->updateWithKey(id);
if(HDialog::run(this,"Edit a task",ptable,NULL,"Ok|OkIsAccept|EscC|StrToE|InScroll","Task","","",NULL,400,400))
{
ptable->saveRecord();
HRefreshAgent::notify("excerc");
}
return 0;
}
int MainDialog::showError(QString err)
{
QMessageBox::warning(this,"Error received:",err);
return 0;
}
MainDialog::~MainDialog()
{
}
int main(int argi,char **argc)
{
bool newdb;
QApplication app(argi,argc); HSqlInterface::setSqlMode("QtSqlite_Win");
dconsole();
QFile dbasefile(DBFILE);
if(dbasefile.exists()) newdb = false;
else newdb = true;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(DBFILE);
if(!db.open())
{
QMessageBox::information(NULL,"gSAFE-Qt4","Cannot establish the database connection...");
return -1;
}
sdebug("Database succesfull opened..."); new HRefreshAgent();
MainDialog *maindialog = new MainDialog(0,newdb);
maindialog->show();
app.exec();
db.close();
return 0;
}