-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
87 lines (75 loc) · 2.11 KB
/
Copy pathmain.cpp
File metadata and controls
87 lines (75 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "mainform.h"
#include <QApplication>
#include <QtSql>
#include <cstdlib>
#include <QtWidgets>
void createTables(const QString &sqlLine) {
QSqlQuery query;
query.exec(sqlLine);
}
void createDatabase() {
QFile f(":/resources/dbschema/db.schema");
if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
qDebug() << QObject::tr("cannot open resource file");
QTextStream in(&f);
QString line;
QString sqlStatement = "";
while (!in.atEnd()){
line = in.readLine();
if(line.startsWith('#') || line.isEmpty())
continue;
sqlStatement += line;
if(sqlStatement.trimmed().endsWith(";")){
createTables(sqlStatement);
sqlStatement = "";
}
}
}
void processSchema()
{
QProgressDialog progress;
progress.setWindowModality(Qt::WindowModal);
progress.setWindowTitle(QObject::tr("SIGA"));
progress.setLabelText(QObject::tr("Criando banco de dados..."));
progress.setMinimum(0);
progress.setMaximum(3);
progress.setValue(1);
qApp->processEvents();
createDatabase();
progress.setValue(2);
qApp->processEvents();
progress.setValue(3);
qApp->processEvents();
progress.setValue(progress.maximum());
qApp->processEvents();
}
bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("siga.dat");
if (!db.open()) {
QMessageBox::warning(0, QObject::tr("Database Error"),
db.lastError().text());
return false;
}
return true;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
bool existingData = QFile::exists("siga.dat");
if (!existingData)
if (!createConnection())
return 1;
else
processSchema();
else
if (!createConnection())
return 1;
QTranslator *appTranslator = new QTranslator;
appTranslator->load("translate_" + QLocale::system().name(), ":/translations");
app.installTranslator(appTranslator);
MainForm w(appTranslator);
w.showMaximized();
return app.exec();
}