src/dbconfigureform.cpp
author viric@llimona
Thu, 18 May 2006 23:05:01 +0200
changeset 0 04114bce8fd0
permissions -rw-r--r--
Initial from sourceforge's cvs.

#include <qpushbutton.h>
#include <qapplication.h>
#include <qvgroupbox.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qregexp.h>
#include "dbconfigureform.h"
#include "inputfield.h"
#include "inputcombo.h"
#include <qvalidator.h>
#include <qsqldatabase.h>
#include <qcombobox.h>

DBConfigureForm :: DBConfigureForm( Tdb_config *mydb_config,
		QWidget *parent, const char *name )
		: QDialog( parent, name, TRUE)
{
	QVBoxLayout *mainlayout = new QVBoxLayout( this ,10 );

	// We store the pointer to the data we should set
	db_config = mydb_config;

	// Data InputFields
	QVGroupBox *fields = new QVGroupBox( "Data", this );

	host = new InputField( tr("Host:"), db_config->host, fields);
	database = new InputField( tr("Database:"), db_config->database,
			fields);
	username = new InputField( tr("Username:"),
			db_config->username, fields);
	password = new InputField( tr("Password:"), db_config->password,
			fields);
	driver = new InputCombo( tr("Driver:"), QSqlDatabase::drivers(),
			fields);

	// Password echo mode for password input field.
	password->qLineEdit()->setEchoMode(QLineEdit::Password);
	// Set the default setting in driver combo
	driver->qComboBox()->setCurrentText(db_config->driver);

	mainlayout->addWidget( fields );


	// Apply button
	apply_button = new QPushButton( tr("Apply"), this,
			"ApplyButton");
	// Accept button
	QPushButton *accept_button = new QPushButton( tr("Accept"), this,
			"AcceptButton");
	// Cancel button
	QPushButton *cancel_button = new QPushButton( tr("Cancel"),
			this, "CancelButton");

	// Main form buttons
	QHBoxLayout *buttons = new QHBoxLayout( mainlayout, 10 );
	buttons->addWidget(apply_button);
	buttons->addWidget(accept_button);
	buttons->addWidget(cancel_button);

	accept_button->setDefault(TRUE);

	setCaption( tr("DB Configure Form") );

	// Connects: buttons
	connect( apply_button, SIGNAL(clicked()), this,
			SLOT(updateVariables()) );
	connect( accept_button, SIGNAL(clicked()), this,
			SLOT(acceptChanges()) );
	connect( cancel_button, SIGNAL(clicked()), this, SLOT(reject()) );

	// Connects: textboxes
	connect( host->qLineEdit(), SIGNAL(textChanged(const QString&)), this,
			SLOT(setFormChanged()) );
	connect( database->qLineEdit(), SIGNAL(textChanged(const QString&)),
			this, SLOT(setFormChanged()) );
	connect( username->qLineEdit(), SIGNAL(textChanged(const QString&)),
			this, SLOT(setFormChanged()) );
	connect( password->qLineEdit(), SIGNAL(textChanged(const QString&)),
			this, SLOT(setFormChanged()) );
	connect( driver->qComboBox(), SIGNAL(activated(int)),
			this, SLOT(setFormChanged()) );

	// Initial state of the form
	setFormNoChanged();

	// Validators for InputFields
	host->qLineEdit()->setValidator(
			new QRegExpValidator( QRegExp( "[0-9a-z_\\-\\.]*" ),
			host->qLineEdit() ));
	username->qLineEdit()->setValidator(
			new QRegExpValidator( QRegExp( "[0-9a-zA-Z_\\-\\.]+" ),
			host->qLineEdit() ));
	password->qLineEdit()->setValidator(
			new QRegExpValidator( QRegExp( "[0-9a-zA-Z_\\-\\.]*" ),
			host->qLineEdit() ));
	database->qLineEdit()->setValidator(
			new QRegExpValidator( QRegExp( "[0-9a-zA-Z_\\-\\.]+" ),
			host->qLineEdit() ));
	
}


int DBConfigureForm :: updateVariables( )
{
	/*
	// ** Check if data is correct. Don't let it end if sth's wrong.
	if (host->text().find( QRegExp( "[^a-z_\\-\\.]" ) ) != -1 )
	{
		// *** A Message Box should be shown
		qWarning("Host isn't good.");
		return 1; // ERROR
	}
	*/

	// Save data
	db_config->host = host->text();
	db_config->username = username->text();
	db_config->database = database->text();
	db_config->password = password->text();
	db_config->driver = driver->text();

	setFormNoChanged();

	return 0;	// OK
}

void DBConfigureForm :: acceptChanges( )
{
	if (!updateVariables())
		accept();
}

void DBConfigureForm :: setFormChanged()
{
	apply_button->setEnabled(true);
}
void DBConfigureForm :: setFormNoChanged()
{
	apply_button->setEnabled(false);
}