SOme work on labels/w/status.

This commit is contained in:
Martin Rotter 2014-02-11 17:23:40 +01:00
parent 77c17f7e5a
commit fd6ce845ae
8 changed files with 112 additions and 28 deletions

View File

@ -292,6 +292,7 @@ set(APP_SOURCES
src/gui/plaintoolbutton.cpp src/gui/plaintoolbutton.cpp
src/gui/lineeditwithstatus.cpp src/gui/lineeditwithstatus.cpp
src/gui/widgetwithstatus.cpp src/gui/widgetwithstatus.cpp
src/gui/labelwithstatus.cpp
src/gui/messagebox.cpp src/gui/messagebox.cpp
# CORE sources. # CORE sources.
@ -359,6 +360,7 @@ set(APP_HEADERS
src/gui/plaintoolbutton.h src/gui/plaintoolbutton.h
src/gui/lineeditwithstatus.h src/gui/lineeditwithstatus.h
src/gui/widgetwithstatus.h src/gui/widgetwithstatus.h
src/gui/labelwithstatus.h
src/gui/messagebox.h src/gui/messagebox.h
# CORE headers. # CORE headers.

View File

@ -33,8 +33,8 @@ DatabaseFactory *DatabaseFactory::instance() {
return s_instance; return s_instance;
} }
int DatabaseFactory::mysqlTestConnection(const QString &hostname, int port, DatabaseFactory::MySQLError DatabaseFactory::mysqlTestConnection(const QString &hostname, int port,
const QString &username, const QString &password) { const QString &username, const QString &password) {
QSqlDatabase database = QSqlDatabase::addDatabase(APP_DB_DRIVER_MYSQL, QSqlDatabase database = QSqlDatabase::addDatabase(APP_DB_DRIVER_MYSQL,
APP_DB_TEST_MYSQL); APP_DB_TEST_MYSQL);
@ -47,29 +47,29 @@ int DatabaseFactory::mysqlTestConnection(const QString &hostname, int port,
// Connection succeeded, clean up the mess and return 0. // Connection succeeded, clean up the mess and return 0.
database.close(); database.close();
removeConnection(APP_DB_TEST_MYSQL); removeConnection(APP_DB_TEST_MYSQL);
return 0; return MySQLOk;
} }
else { else {
// Connection failed, do cleanup and return specific // Connection failed, do cleanup and return specific
// error code. // error code.
int error_code = database.lastError().number(); MySQLError error_code = static_cast<MySQLError>(database.lastError().number());
removeConnection(APP_DB_TEST_MYSQL); removeConnection(APP_DB_TEST_MYSQL);
return error_code; return error_code;
} }
} }
QString DatabaseFactory::mysqlInterpretErrorCode(int error_code) { QString DatabaseFactory::mysqlInterpretErrorCode(MySQLError error_code) {
switch (error_code) { switch (error_code) {
case 0: case MySQLOk:
return QObject::tr("Operation successful."); return QObject::tr("MySQL server works as expected.");
case 2002: case MySQLCantConnect:
case 2003: case MySQLConnectionError:
case 2005: case MySQLUnknownHost:
return QObject::tr("No MySQL server is running in the target destination."); return QObject::tr("No MySQL server is running in the target destination.");
case 1045: case MySQLAccessDenied:
return QObject::tr("Access denied. Invalid username or password used."); return QObject::tr("Access denied. Invalid username or password used.");
default: default:

View File

@ -24,6 +24,14 @@ class DatabaseFactory : public QObject {
FromSettings FromSettings
}; };
enum MySQLError {
MySQLOk = 0,
MySQLAccessDenied = 1045,
MySQLConnectionError = 2002,
MySQLCantConnect = 2003,
MySQLUnknownHost = 2005
};
// //
// GENERAL stuff. // GENERAL stuff.
// //
@ -57,10 +65,10 @@ class DatabaseFactory : public QObject {
// Tests if database connection with given data // Tests if database connection with given data
// can be established and returns 0 if it can. // can be established and returns 0 if it can.
// Otherwise returns MySQL-specific error code. // Otherwise returns MySQL-specific error code.
int mysqlTestConnection(const QString &hostname, int port, MySQLError mysqlTestConnection(const QString &hostname, int port,
const QString &username, const QString &password); const QString &username, const QString &password);
QString mysqlInterpretErrorCode(int error_code); QString mysqlInterpretErrorCode(MySQLError error_code);
private: private:
// //

View File

@ -42,6 +42,12 @@
#define AUTO_UPDATE_INTERVAL 60000 #define AUTO_UPDATE_INTERVAL 60000
#define STARTUP_UPDATE_DELAY 1500 #define STARTUP_UPDATE_DELAY 1500
#define MYSQL_CODE_OK 0
#define MYSQL_CODE_NOT_FOUND 2002
#define MYSQL_CODE_NOT_FOUND 2002
#define MYSQL_CODE_NOT_FOUND 2002
#define MYSQL_CODE_NOT_FOUND 2002
#define APP_DB_TEST_MYSQL "MySQLTest" #define APP_DB_TEST_MYSQL "MySQLTest"
#define APP_DB_MYSQL_PORT 3306 #define APP_DB_MYSQL_PORT 3306
#define APP_DB_DRIVER_SQLITE "QSQLITE" #define APP_DB_DRIVER_SQLITE "QSQLITE"

View File

@ -445,6 +445,10 @@ void FormSettings::loadDataStorage() {
onMysqlUsernameChanged(QString()); onMysqlUsernameChanged(QString());
onMysqlPasswordChanged(QString()); onMysqlPasswordChanged(QString());
m_ui->m_lblMysqlTestResult->setLayoutDirection(Qt::RightToLeft);
m_ui->m_lblMysqlTestResult->setStatus(WidgetWithStatus::Information,
tr("No connection test triggered so far."));
// Load SQLite. // Load SQLite.
m_ui->m_cmbDatabaseDriver->addItem( m_ui->m_cmbDatabaseDriver->addItem(
tr("SQLite (embedded database)"), APP_DB_DRIVER_SQLITE); tr("SQLite (embedded database)"), APP_DB_DRIVER_SQLITE);
@ -510,13 +514,24 @@ void FormSettings::saveDataStorage() {
} }
void FormSettings::mysqlTestConnection() { void FormSettings::mysqlTestConnection() {
int error_code = DatabaseFactory::instance()->mysqlTestConnection(m_ui->m_txtMysqlHostname->lineEdit()->text(), DatabaseFactory::MySQLError error_code = DatabaseFactory::instance()->mysqlTestConnection(m_ui->m_txtMysqlHostname->lineEdit()->text(),
m_ui->m_spinMysqlPort->value(), m_ui->m_spinMysqlPort->value(),
m_ui->m_txtMysqlUsername->lineEdit()->text(), m_ui->m_txtMysqlUsername->lineEdit()->text(),
m_ui->m_txtMysqlPassword->lineEdit()->text()); m_ui->m_txtMysqlPassword->lineEdit()->text());
// Let's interpret the result. switch (error_code) {
m_ui->m_lblMysqlTestResult->setText(DatabaseFactory::instance()->mysqlInterpretErrorCode(error_code)); case DatabaseFactory::MySQLOk:
m_ui->m_lblMysqlTestResult->setStatus(WidgetWithStatus::Ok,
DatabaseFactory::instance()->mysqlInterpretErrorCode(error_code));
break;
default:
m_ui->m_lblMysqlTestResult->setStatus(WidgetWithStatus::Error,
DatabaseFactory::instance()->mysqlInterpretErrorCode(error_code));
break;
}
} }
void FormSettings::onMysqlHostnameChanged(const QString &new_hostname) { void FormSettings::onMysqlHostnameChanged(const QString &new_hostname) {

View File

@ -17,7 +17,7 @@
<item row="0" column="1"> <item row="0" column="1">
<widget class="QStackedWidget" name="m_stackedSettings"> <widget class="QStackedWidget" name="m_stackedSettings">
<property name="currentIndex"> <property name="currentIndex">
<number>3</number> <number>1</number>
</property> </property>
<widget class="QWidget" name="m_pageGeneral"> <widget class="QWidget" name="m_pageGeneral">
<layout class="QFormLayout" name="formLayout_5"> <layout class="QFormLayout" name="formLayout_5">
@ -223,19 +223,13 @@ Authors of this application are NOT responsible for lost data.</string>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QLabel" name="m_lblMysqlTestResult"> <widget class="LabelWithStatus" name="m_lblMysqlTestResult" native="true">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred"> <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch> <horstretch>0</horstretch>
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="text">
<string>No test run so far.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -1127,6 +1121,12 @@ Authors of this application are NOT responsible for lost data.</string>
<header>lineeditwithstatus.h</header> <header>lineeditwithstatus.h</header>
<container>1</container> <container>1</container>
</customwidget> </customwidget>
<customwidget>
<class>LabelWithStatus</class>
<extends>QWidget</extends>
<header>labelwithstatus.h</header>
<container>1</container>
</customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections> <connections>

View File

@ -0,0 +1,28 @@
#include "gui/labelwithstatus.h"
#include "gui/plaintoolbutton.h"
#include <QHBoxLayout>
LabelWithStatus::LabelWithStatus(QWidget *parent)
: WidgetWithStatus(parent) {
m_wdgInput = new QLabel(this);
// Set correct size for the tool button.
int txt_input_height = m_wdgInput->sizeHint().height();
m_btnStatus->setFixedSize(txt_input_height + 4, txt_input_height + 4);
// Compose the layout.
m_layout->addWidget(m_wdgInput);
m_layout->addWidget(m_btnStatus);
}
LabelWithStatus::~LabelWithStatus() {
}
void LabelWithStatus::setStatus(WidgetWithStatus::StatusType status,
const QString &label_text) {
WidgetWithStatus::setStatus(status, label_text);
label()->setText(label_text);
}

25
src/gui/labelwithstatus.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef LABELWITHSTATUS_H
#define LABELWITHSTATUS_H
#include "gui/widgetwithstatus.h"
#include <QLabel>
class LabelWithStatus : public WidgetWithStatus {
Q_OBJECT
public:
// Constructors and destructors.
explicit LabelWithStatus(QWidget *parent = 0);
virtual ~LabelWithStatus();
void setStatus(StatusType status, const QString &label_text);
// Access to label.
inline QLabel *label() const {
return static_cast<QLabel*>(m_wdgInput);
}
};
#endif // LABELWITHSTATUS_H