Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
306 lines (269 sloc) 8.89 KB
/**
* c-project - a digital registry.
* Copyright (c) 2010, Nycholas de Oliveira e Oliveira <nycholas@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the Nycholas de Oliveira e Oliveira nor the names of
* its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "act.h"
ActForm::ActForm(ActModel *model, QDialog *parent) :
QDialog(parent) {
setupUi(this);
actModel = model;
qDebug() << "ID:" << actModel->getId();
statusTimer = new QTimer(this);
createActions();
updateWidgets();
}
ActForm::~ActForm(void) {
}
void ActForm::timerStatusAction(void) {
statusLabel->hide();
statusLabel->setText("");
}
void ActForm::nextAction(void) {
qDebug() << "next";
QSqlQuery query = actModel->query();
if (!query.next())
return;
int idCol = query.record().indexOf("id");
if (query.next()) {
actModel->setId(query.value(idCol).toInt());
qDebug() << "next>>" << actModel->getId();
}
select();
int begin = actModel->begin();
int sizeAll = actModel->count();
int size = actModel->query().size();
nextPushButton->setEnabled(begin > 0);
previousPushButton->setEnabled(sizeAll > (size + begin));
}
void ActForm::previousAction(void) {
qDebug() << "pre";
QSqlQuery query = actModel->query();
if (!query.previous())
return;
int idCol = query.record().indexOf("id");
if (query.next()) {
actModel->setId(query.value(idCol).toInt());
qDebug() << "pre>>" << actModel->getId();
}
select();
int begin = actModel->begin();
int sizeAll = actModel->count();
int size = actModel->query().size();
nextPushButton->setEnabled(begin > 0);
previousPushButton->setEnabled(sizeAll > (size + begin));
}
void ActForm::saveAction(void) {
if (!save()) {
errorStatus(qApp->tr("Failure trying to register the record."));
} else {
if (actModel->getId() > 0) {
emit formChanged();
emit sendStatus(QString(qApp->tr(
"The act \"%1\" was changed successfully.")).arg(
actModel->getName()), 0);
} else {
emit formAdded();
emit sendStatus(QString(qApp->tr(
"The act \"%1\" was added successfully.")).arg(
actModel->getName()), 0);
}
updateModels();
updateForms();
close();
}
}
void ActForm::saveAndContinueSavingAction(void) {
if (!save()) {
errorStatus(qApp->tr("Failure trying to register the record."));
} else {
if (actModel->getId() > 0) {
emit formChanged();
okStatus(QString(qApp->tr(
"The act \"%1\" was changed successfully.")).arg(
actModel->getName()));
} else {
emit formAdded();
okStatus(
QString(qApp->tr("The act \"%1\" was added successfully.")).arg(
actModel->getName()));
}
updateModels();
updateForms();
}
}
void ActForm::removeAction(void) {
QMessageBox msgBox;
msgBox.setText(qApp->tr("Are you sure?"));
msgBox.setInformativeText(QString(qApp->tr(
"Are you sure you want to delete the selected act objects?\n"
"All of the following objects and their related items will be "
"deleted:\n\nAct: %1\n").arg(actModel->getName())));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No
| QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Yes);
int ret = msgBox.exec();
if (ret == QMessageBox::Cancel) {
updateModels();
updateForms();
close();
return;
} else if (ret == QMessageBox::No)
return;
if (!remove()) {
errorStatus(qApp->tr("Fails to remove the record."));
} else {
QMessageBox::information(0, qApp->tr("Act deleted"), QString(qApp->tr(
"Successfully deleted %1 act.")).arg("1"), QMessageBox::Ok);
emit
formDeleted();
emit
sendStatus(QString(qApp->tr("Successfully deleted %1 act.")).arg("1"),
0);
updateModels();
updateForms();
close();
}
}
void ActForm::cancelAction(void) {
close();
}
void ActForm::createActions(void) {
connect(statusTimer, SIGNAL(timeout()), this, SLOT(timerStatusAction()));
connect(nextPushButton, SIGNAL(released()), this, SLOT(nextAction()));
connect(previousPushButton, SIGNAL(released()), this,
SLOT(previousAction()));
connect(savePushButton, SIGNAL(released()), this, SLOT(saveAction()));
connect(saveAndContinueSavingPushButton, SIGNAL(released()), this, SLOT(
saveAndContinueSavingAction()));
connect(removePushButton, SIGNAL(released()), this, SLOT(removeAction()));
connect(cancelPushButton, SIGNAL(released()), this, SLOT(cancelAction()));
}
void ActForm::updateWidgets(void) {
updateForms();
statusLabel->hide();
}
void ActForm::updateModels(void) {
actModel = new ActModel(0, this);
int begin = actModel->begin();
int sizeAll = actModel->count();
int size = actModel->query().size();
nextPushButton->setEnabled(begin > 0);
previousPushButton->setEnabled(sizeAll > (size + begin));
}
void ActForm::updateForms(void) {
clear();
validMask();
focusDefault();
if (actModel->getId() > 0) {
select();
titleLabel->setText(qApp->tr("Change Act"));
actGroupBox->setTitle(qApp->tr("&Act details"));
savePushButton->setText(qApp->tr("&Update"));
saveAndContinueSavingPushButton->setText(qApp->tr(
"Update and &continue saving"));
removePushButton->show();
} else {
titleLabel->setText(qApp->tr("Add Act"));
actGroupBox->setTitle(qApp->tr("&Act details"));
savePushButton->setText(qApp->tr("&Save"));
saveAndContinueSavingPushButton->setText(qApp->tr(
"Save and &continue saving"));
removePushButton->hide();
}
nextPushButton->hide();
previousPushButton->hide();
savePushButton->setDefault(true);
}
void ActForm::select(void) {
QString value;
ActModel::selectById(actModel->getId(), actModel);
nameLineEdit->setText(actModel->getName());
descriptionTextEdit->setText(actModel->getDescription());
valueLineEdit->setText(value.sprintf("%.2f", actModel->getValue()).replace(
".", ","));
isActiveCheckBox->setChecked(actModel->getIsActive());
}
bool ActForm::save(void) {
QString value = valueLineEdit->text();
value = value.replace(".", "");
value = value.replace(",", ".");
actModel->setName(nameLineEdit->text());
actModel->setDescription(descriptionTextEdit->toPlainText());
actModel->setValue(value.toDouble());
actModel->setDateJoined(QDateTime::currentDateTime());
actModel->setIsActive(isActiveCheckBox->isChecked() ? 1 : 0);
return actModel->save();
}
bool ActForm::remove(void) {
return actModel->remove();
}
void ActForm::clear(void) {
nameLineEdit->clear();
descriptionTextEdit->clear();
valueLineEdit->setText("0,00");
isActiveCheckBox->setChecked(true);
}
void ActForm::validMask(void) {
// Format of Real: Brazilian Money
QRegExp valid("^\\d{1,3}(([.]\\d{3})*),(\\d{2})$");
valueLineEdit->setValidator(new QRegExpValidator(valid, valueLineEdit));
}
void ActForm::focusDefault(void) {
nameLineEdit->setFocus();
}
void ActForm::timerStatus(void) {
statusTimer->start(3000);
}
void ActForm::okStatus(const QString &msg) {
statusLabel->setText(msg);
statusLabel->setStyleSheet("color: green; background-color: white;"
"margin: 2px; padding: 3px; border: 1px solid green;");
statusLabel->show();
timerStatus();
}
void ActForm::infoStatus(const QString &msg) {
statusLabel->setText(msg);
statusLabel->setStyleSheet("color: blue; background-color: white;"
"margin: 2px; padding: 3px; border: 1px solid blue;");
statusLabel->show();
timerStatus();
}
void ActForm::alertStatus(const QString &msg) {
statusLabel->setText(msg);
statusLabel->setStyleSheet("color: yellow; background-color: white;"
"margin: 2px; padding: 3px; border: 1px solid yellow;");
statusLabel->show();
timerStatus();
}
void ActForm::errorStatus(const QString &msg) {
statusLabel->setText(msg);
statusLabel->setStyleSheet("color: red; background-color: white;"
"margin: 2px; padding: 3px; border: 1px solid red;");
statusLabel->show();
timerStatus();
}