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/lineeditwithstatus.cpp
src/gui/widgetwithstatus.cpp
src/gui/labelwithstatus.cpp
src/gui/messagebox.cpp
# CORE sources.
@ -359,6 +360,7 @@ set(APP_HEADERS
src/gui/plaintoolbutton.h
src/gui/lineeditwithstatus.h
src/gui/widgetwithstatus.h
src/gui/labelwithstatus.h
src/gui/messagebox.h
# CORE headers.

View File

@ -33,8 +33,8 @@ DatabaseFactory *DatabaseFactory::instance() {
return s_instance;
}
int DatabaseFactory::mysqlTestConnection(const QString &hostname, int port,
const QString &username, const QString &password) {
DatabaseFactory::MySQLError DatabaseFactory::mysqlTestConnection(const QString &hostname, int port,
const QString &username, const QString &password) {
QSqlDatabase database = QSqlDatabase::addDatabase(APP_DB_DRIVER_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.
database.close();
removeConnection(APP_DB_TEST_MYSQL);
return 0;
return MySQLOk;
}
else {
// Connection failed, do cleanup and return specific
// error code.
int error_code = database.lastError().number();
MySQLError error_code = static_cast<MySQLError>(database.lastError().number());
removeConnection(APP_DB_TEST_MYSQL);
return error_code;
}
}
QString DatabaseFactory::mysqlInterpretErrorCode(int error_code) {
QString DatabaseFactory::mysqlInterpretErrorCode(MySQLError error_code) {
switch (error_code) {
case 0:
return QObject::tr("Operation successful.");
case MySQLOk:
return QObject::tr("MySQL server works as expected.");
case 2002:
case 2003:
case 2005:
case MySQLCantConnect:
case MySQLConnectionError:
case MySQLUnknownHost:
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.");
default:

View File

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

View File

@ -42,6 +42,12 @@
#define AUTO_UPDATE_INTERVAL 60000
#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_MYSQL_PORT 3306
#define APP_DB_DRIVER_SQLITE "QSQLITE"

View File

@ -445,6 +445,10 @@ void FormSettings::loadDataStorage() {
onMysqlUsernameChanged(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.
m_ui->m_cmbDatabaseDriver->addItem(
tr("SQLite (embedded database)"), APP_DB_DRIVER_SQLITE);
@ -510,13 +514,24 @@ void FormSettings::saveDataStorage() {
}
void FormSettings::mysqlTestConnection() {
int error_code = DatabaseFactory::instance()->mysqlTestConnection(m_ui->m_txtMysqlHostname->lineEdit()->text(),
m_ui->m_spinMysqlPort->value(),
m_ui->m_txtMysqlUsername->lineEdit()->text(),
m_ui->m_txtMysqlPassword->lineEdit()->text());
DatabaseFactory::MySQLError error_code = DatabaseFactory::instance()->mysqlTestConnection(m_ui->m_txtMysqlHostname->lineEdit()->text(),
m_ui->m_spinMysqlPort->value(),
m_ui->m_txtMysqlUsername->lineEdit()->text(),
m_ui->m_txtMysqlPassword->lineEdit()->text());
// Let's interpret the result.
m_ui->m_lblMysqlTestResult->setText(DatabaseFactory::instance()->mysqlInterpretErrorCode(error_code));
switch (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) {

View File

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