Code reorganization
This commit is contained in:
parent
6851637b7d
commit
26c71d86a7
3
db.cpp
3
db.cpp
|
@ -1,3 +1,4 @@
|
|||
#include "db.h"
|
||||
|
||||
QSqlDatabase db;
|
||||
QSqlDatabase db;
|
||||
QSqlQuery query;
|
1
db.h
1
db.h
|
@ -7,5 +7,6 @@
|
|||
#include <QSqlRecord>
|
||||
|
||||
extern QSqlDatabase db;
|
||||
extern QSqlQuery query;
|
||||
|
||||
#endif
|
|
@ -91,30 +91,11 @@ WHERE id = :id");
|
|||
if ( ! query.exec() ) {
|
||||
qDebug() << fullQuery(query);
|
||||
qDebug() << db.lastError().text();
|
||||
setStatus(Ui::ERROR, "query error");
|
||||
Ui::status(Ui::ERROR, "query error");
|
||||
return;
|
||||
}
|
||||
|
||||
setStatus(Ui::SUCCESS, "successfull update");
|
||||
}
|
||||
|
||||
void EditWindow::setStatus(Ui::StatusType type, QString message) {
|
||||
QStatusBar* statusBar = this->findChild<QStatusBar*>("statusBar");
|
||||
int timeout = 0;
|
||||
|
||||
/* choose background color */
|
||||
switch (type) {
|
||||
case Ui::INFO: statusBar->setStyleSheet("background-color: #9dd;"); break;
|
||||
case Ui::SUCCESS: statusBar->setStyleSheet("background-color: #5a0;"); break;
|
||||
case Ui::WARNING: statusBar->setStyleSheet("background-color: #ff7;"); break;
|
||||
case Ui::ERROR: statusBar->setStyleSheet("background-color: #d00;"); break;
|
||||
}
|
||||
/* choose timeout */
|
||||
switch (type) {
|
||||
case Ui::INFO: case Ui::SUCCESS: timeout = 10000; break;
|
||||
case Ui::WARNING: case Ui::ERROR: timeout = 0; break;
|
||||
}
|
||||
statusBar->showMessage(message, timeout);
|
||||
Ui::status(Ui::SUCCESS, "successfull update");
|
||||
}
|
||||
|
||||
void EditWindow::on_buttonAnnulla_clicked() {
|
||||
|
|
12
editwindow.h
12
editwindow.h
|
@ -10,17 +10,11 @@
|
|||
#include "db.h"
|
||||
#include "fullquery.h"
|
||||
#include "editwindow.h"
|
||||
#include "status.h"
|
||||
#include "ui_editwindow.h"
|
||||
|
||||
namespace Ui {
|
||||
class EditWindow;
|
||||
|
||||
enum StatusType {
|
||||
INFO,
|
||||
SUCCESS,
|
||||
WARNING,
|
||||
ERROR
|
||||
};
|
||||
}
|
||||
|
||||
class EditWindow : public QMainWindow {
|
||||
|
@ -30,8 +24,6 @@ public:
|
|||
explicit EditWindow(int idSocio, QWidget *parent);
|
||||
~EditWindow();
|
||||
|
||||
void setStatus(Ui::StatusType type, QString message);
|
||||
|
||||
private slots:
|
||||
void on_buttonSalva_clicked();
|
||||
void on_buttonAnnulla_clicked();
|
||||
|
@ -48,8 +40,6 @@ private:
|
|||
QComboBox* comboFonte = nullptr;
|
||||
QCheckBox* checkAbilitaQuestionario = nullptr;
|
||||
|
||||
QSqlQuery query;
|
||||
|
||||
Ui::EditWindow *ui;
|
||||
};
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>652</width>
|
||||
<height>294</height>
|
||||
<height>273</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
|
@ -319,7 +319,6 @@
|
|||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusBar"/>
|
||||
</widget>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<tabstops>
|
||||
|
|
|
@ -5,7 +5,7 @@ QString fullQuery(const QSqlQuery& query) {
|
|||
QMapIterator<QString, QVariant> it(query.boundValues());
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
str.replace(it.key(),it.value().toString());
|
||||
str.replace(it.key(), it.value().toString());
|
||||
}
|
||||
return str;
|
||||
}
|
|
@ -30,6 +30,7 @@ SOURCES += \
|
|||
fullquery.cpp \
|
||||
main.cpp \
|
||||
mainwindow.cpp \
|
||||
status.cpp \
|
||||
SociModel.cpp
|
||||
|
||||
|
||||
|
@ -38,6 +39,7 @@ HEADERS += \
|
|||
editwindow.h \
|
||||
fullquery.h \
|
||||
mainwindow.h \
|
||||
status.h \
|
||||
SociModel.h
|
||||
|
||||
|
||||
|
|
22
main.cpp
22
main.cpp
|
@ -1,7 +1,6 @@
|
|||
#include <iostream>
|
||||
#include <QApplication>
|
||||
#include <QMessageBox>
|
||||
#include <QSettings>
|
||||
|
||||
#include "db.h"
|
||||
#include "editwindow.h"
|
||||
|
@ -11,23 +10,12 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
QApplication a(argc, argv);
|
||||
|
||||
/* settings are automatically retrieved from ~/.config/it.linux.golem/gestionale.conf */
|
||||
QSettings settings("it.linux.golem", "gestionale");
|
||||
settings.beginGroup("database");
|
||||
db = QSqlDatabase::addDatabase("QMYSQL"); /* QMYSQL <-- database driver */
|
||||
db.setHostName(settings.value("hostname").toString());
|
||||
db.setDatabaseName(settings.value("database").toString());
|
||||
db.setUserName(settings.value("username").toString());
|
||||
db.setPassword(settings.value("password").toString());
|
||||
mw = new MainWindow;
|
||||
mw->show();
|
||||
|
||||
if (! db.open()) {
|
||||
QMessageBox msgBox;
|
||||
msgBox.setText(db.lastError().text());
|
||||
msgBox.exec();
|
||||
}
|
||||
int ret = a.exec();
|
||||
|
||||
MainWindow w;
|
||||
w.show();
|
||||
delete mw;
|
||||
|
||||
return a.exec();
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -1,29 +1,54 @@
|
|||
#include "mainwindow.h"
|
||||
|
||||
MainWindow* mw;
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||
ui->setupUi(this);
|
||||
|
||||
lineCognome = this->findChild<QLineEdit*>("lineCognome");
|
||||
lineNome = this->findChild<QLineEdit*>("lineCognome");
|
||||
lineNome = this->findChild<QLineEdit*>("lineNome");
|
||||
tableSoci = this->findChild<QTableView*>("tableSoci");
|
||||
}
|
||||
|
||||
void MainWindow::connectDatabase(void) {
|
||||
/* retrieve settings for this application, group database */
|
||||
QSettings settings("it.linux.golem", "gestionale");
|
||||
settings.beginGroup("database");
|
||||
|
||||
/* open database and attach query */
|
||||
db = QSqlDatabase::addDatabase("QMYSQL"); /* QMYSQL <-- database driver */
|
||||
db.setHostName(settings.value("hostname").toString());
|
||||
db.setDatabaseName(settings.value("database").toString());
|
||||
db.setUserName(settings.value("username").toString());
|
||||
db.setPassword(settings.value("password").toString());
|
||||
query = QSqlQuery(db);
|
||||
|
||||
status(Ui::INFO, QString("Connecting to %1...").arg(settings.value("hostname").toString()));
|
||||
|
||||
if (! db.open())
|
||||
status(Ui::ERROR, db.lastError().text());
|
||||
else
|
||||
status(Ui::INFO, QString("Connected to %1").arg(settings.value("hostname").toString()));
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() {
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void MainWindow::on_buttonCerca_clicked() {
|
||||
status(Ui::INFO, "Message placeholder");
|
||||
if (!db.open()) connectDatabase();
|
||||
|
||||
query.prepare("SELECT id, nome, cognome FROM socio WHERE cognome LIKE :cognome");
|
||||
query.bindValue( ":cognome", QString("%%1%").arg(lineCognome->text()) );
|
||||
|
||||
if ( ! query.exec() ) { qDebug() << query.lastQuery(); return; }
|
||||
//if ( query.size() == 0) { setStatus(Ui::INFO, "no results found"); return; }
|
||||
if ( query.size() == 0) { status(Ui::INFO, "no results found"); return; }
|
||||
|
||||
if (sociModel != nullptr) delete sociModel; // TODO -- does this look a cunning thing?
|
||||
sociModel = new SociModel(nullptr, query);
|
||||
|
||||
//setStatus(Ui::INFO, QString("%1 results found").arg(query.size()));
|
||||
status(Ui::INFO, QString("%1 results found").arg(query.size()));
|
||||
|
||||
tableSoci->setModel(sociModel);
|
||||
/* the signal is automagically added by some obscure qt build system, provided the function is named in a standard way */
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <QDebug>
|
||||
#include <QSqlQuery>
|
||||
#include <QSettings>
|
||||
#include <QTableWidgetItem>
|
||||
|
||||
#include "editwindow.h"
|
||||
|
@ -34,11 +35,12 @@ private:
|
|||
QLineEdit* lineNome = nullptr;
|
||||
QLineEdit* lineCognome = nullptr;
|
||||
QTableView* tableSoci = nullptr;
|
||||
// QDateEdit* dateDataNascita = nullptr;
|
||||
|
||||
QSqlQuery query;
|
||||
|
||||
Ui::MainWindow *ui;
|
||||
|
||||
void connectDatabase(void);
|
||||
};
|
||||
|
||||
extern MainWindow* mw;
|
||||
|
||||
#endif
|
|
@ -114,7 +114,7 @@
|
|||
</widget>
|
||||
<addaction name="menuwhat_s_this"/>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusbar"/>
|
||||
<widget class="QStatusBar" name="statusBar"/>
|
||||
<action name="actionExit">
|
||||
<property name="text">
|
||||
<string>Exit</string>
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
#include "status.h"
|
||||
|
||||
namespace Ui {
|
||||
|
||||
void status(StatusType type, QString message) {
|
||||
//QStatusBar* statusBar = mw->findChild<QStatusBar*>("statusBar");
|
||||
QStatusBar* statusBar = mw->statusBar();
|
||||
QString prefix;
|
||||
int timeout = 0;
|
||||
|
||||
/* choose background color */
|
||||
if (statusBar != nullptr) {
|
||||
switch (type) {
|
||||
case INFO: statusBar->setStyleSheet("color: #007;"); break;
|
||||
case SUCCESS: statusBar->setStyleSheet("color: #0a0;"); break;
|
||||
case WARNING: statusBar->setStyleSheet("color: #f50;"); break;
|
||||
case ERROR: statusBar->setStyleSheet("color: #d00;"); break;
|
||||
default: statusBar->setStyleSheet("color: #555;"); break;
|
||||
}
|
||||
}
|
||||
/* choose stderr prefix */
|
||||
switch(type) {
|
||||
case INFO: prefix = "[II]"; break;
|
||||
case SUCCESS: prefix = "[OK]"; break;
|
||||
case WARNING: prefix = "[WW]"; break;
|
||||
case ERROR: prefix = "[EE]"; break;
|
||||
default: prefix = "[??]"; break;
|
||||
}
|
||||
/* choose timeout */
|
||||
switch (type) {
|
||||
case INFO: case SUCCESS: timeout = 10000; break;
|
||||
case WARNING: case ERROR: timeout = 0; break;
|
||||
default: timeout = 0; break;
|
||||
}
|
||||
|
||||
/* print on stderr */
|
||||
switch (type) {
|
||||
case INFO: case SUCCESS: qDebug().noquote() << prefix << message; break;
|
||||
case WARNING: qWarning().noquote() << prefix << message; break;
|
||||
case ERROR: qCritical().noquote() << prefix << message; break;
|
||||
default: qWarning().noquote() << prefix << message; break;
|
||||
}
|
||||
/* show message in GUI */
|
||||
if (statusBar != nullptr) {
|
||||
statusBar->showMessage(message, timeout);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue