void Database::loadDatabase()
{
QString path
= settings.
value("Database/path").
toString();
qDebug() << settings.value("Database/path").toString();
if(file.exists()){
//database exists keep on going
qDebug("Database::Database(): Database exists keep on going");
//create database
database
= QSqlDatabase::addDatabase("QSQLITE", defaultCon
);
//set database path
database.setDatabaseName(path);
//try to open the database if needed
if(!database.isOpen())
database.open();
//is the database open, if so continue
if(database.isOpen()){
//check if the tables exists
if(!tables.contains("clients", Qt::CaseInsensitive) && !tables.contains("orders", Qt::CaseInsensitive) && !tables.contains("articles", Qt::CaseInsensitive)){
//tables doesn't exists yet, create the tables
qDebug("Database::Database(): There are no tables, create some tables");
//create the tables, and if it went wrong popup the messagebox
if(!this->createTables())
QMessageBox::critical(0, tr
("Database error"), tr
("Kan geen tabellen genereren") );
}
}else{
//couldn't connect to the database
qDebug("Database::Database(): Connection failed");
QMessageBox::critical(0, tr
("Database connectie error"), tr
("Kan geen verbinding maken met de database, neem contact op met de ontwikkelaar of probeer het nog een keer."));
}
}else{
//There is no database yet, popup the file dialog
qDebug("Database::Database(): There is no database yet, popup the file dialog");
this->databaseDialog();
}
}
void Database::databaseDialog(){
qDebug("Database::databaseDialog(): constructing the dialog..");
dialog.
setDirectory(QDir::current());
dialog.setWindowTitle("Database opslag plaats");
//add the db extension to the filename
dialog.setDefaultSuffix("db");
//if somebody presses cancel quit the application
QObject::connect(&dialog,
SIGNAL(rejected
()),
qApp,
SLOT(quit
()));
//popup the dialog
if(dialog.exec())
savePath = dialog.selectedFiles();
if(savePath.count() == 1){
qDebug("Put the path in the settings file");
//put it in the settings
settings.setValue("Database/path", savePath.at(0));
//create an temporary database object, just for creating the file....
//create database file
tmpDatabase.setDatabaseName(savePath.at(0));
tmpDatabase.open();
tmpDatabase.close();
tmpDatabase.removeDatabase(savePath.at(0));
//try loading the database again
this->loadDatabase();
}
}
void Database::loadDatabase()
{
QSettings settings;
QString path = settings.value("Database/path").toString();
qDebug() << settings.value("Database/path").toString();
QFileInfo file(path);
if(file.exists()){
//database exists keep on going
qDebug("Database::Database(): Database exists keep on going");
//create database
database = QSqlDatabase::addDatabase("QSQLITE", defaultCon);
//set database path
database.setDatabaseName(path);
//try to open the database if needed
if(!database.isOpen())
database.open();
//is the database open, if so continue
if(database.isOpen()){
QStringList tables = database.tables();
//check if the tables exists
if(!tables.contains("clients", Qt::CaseInsensitive) && !tables.contains("orders", Qt::CaseInsensitive) && !tables.contains("articles", Qt::CaseInsensitive)){
//tables doesn't exists yet, create the tables
qDebug("Database::Database(): There are no tables, create some tables");
//create the tables, and if it went wrong popup the messagebox
if(!this->createTables())
QMessageBox::critical(0, tr("Database error"), tr("Kan geen tabellen genereren") );
}
}else{
//couldn't connect to the database
qDebug("Database::Database(): Connection failed");
QMessageBox::critical(0, tr("Database connectie error"), tr("Kan geen verbinding maken met de database, neem contact op met de ontwikkelaar of probeer het nog een keer."));
}
}else{
//There is no database yet, popup the file dialog
qDebug("Database::Database(): There is no database yet, popup the file dialog");
this->databaseDialog();
}
}
void Database::databaseDialog(){
qDebug("Database::databaseDialog(): constructing the dialog..");
QStringList savePath;
QFileDialog dialog;
dialog.setDirectory(QDir::current());
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setWindowTitle("Database opslag plaats");
//add the db extension to the filename
dialog.setDefaultSuffix("db");
//if somebody presses cancel quit the application
QObject::connect(&dialog, SIGNAL(rejected()), qApp, SLOT(quit()));
//popup the dialog
if(dialog.exec())
savePath = dialog.selectedFiles();
if(savePath.count() == 1){
qDebug("Put the path in the settings file");
//put it in the settings
QSettings settings;
settings.setValue("Database/path", savePath.at(0));
//create an temporary database object, just for creating the file....
QSqlDatabase tmpDatabase;
tmpDatabase = QSqlDatabase::addDatabase("QSQLITE", QLatin1String("tempConnection"));
//create database file
tmpDatabase.setDatabaseName(savePath.at(0));
tmpDatabase.open();
tmpDatabase.close();
tmpDatabase.removeDatabase(savePath.at(0));
//try loading the database again
this->loadDatabase();
}
}
To copy to clipboard, switch view to plain text mode
#pragma mark settings
void MainWindow::writeSettings()
{
settings.setValue("MainWindow/Size", size());
settings.setValue("MainWindow/Properties", saveState());
}
void MainWindow::readSettings()
{
resize(settings.value("MainWindow/Size", sizeHint()).toSize());
restoreState(settings.value("MainWindow/Properties").toByteArray());
}
#pragma mark destructor
MainWindow::~MainWindow()
{
//write settings
writeSettings();
}
#pragma mark settings
void MainWindow::writeSettings()
{
QSettings settings;
settings.setValue("MainWindow/Size", size());
settings.setValue("MainWindow/Properties", saveState());
}
void MainWindow::readSettings()
{
QSettings settings;
resize(settings.value("MainWindow/Size", sizeHint()).toSize());
restoreState(settings.value("MainWindow/Properties").toByteArray());
}
#pragma mark destructor
MainWindow::~MainWindow()
{
//write settings
writeSettings();
}
To copy to clipboard, switch view to plain text mode
Bookmarks