Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 5c360b9d93
Fetching contributors…

Cannot retrieve contributors at this time

374 lines (327 sloc) 11.733 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 "coupon.h"
Coupon::Coupon(QWidget *parent) :
QWidget(parent) {
setupUi(this);
statusTimer = new QTimer(this);
createModels();
createViews();
createActions();
updateModels();
createSearchAdvanceWidget();
updateWidgets();
}
Coupon::~Coupon(void) {
}
void Coupon::timerStatusAction(void) {
statusLabel->hide();
statusLabel->setText("");
}
void Coupon::newAction(void) {
couponModel->setId(0);
CouponForm *form = new CouponForm(couponModel);
connect(form, SIGNAL(formAdded()), this, SLOT(updateModels()));
connect(form, SIGNAL(formChanged()), this, SLOT(updateModels()));
connect(form, SIGNAL(formDeleted()), this, SLOT(updateModels()));
connect(form, SIGNAL(sendStatus(const QString &, int)), this,
SLOT(updateStatus(const QString &, int)));
form->show();
}
void Coupon::activateAction(void) {
QModelIndex index = couponTableView->currentIndex();
if (!index.isValid()) {
infoStatus(qApp->tr("Please select an item to edit."));
return;
}
QSqlRecord record = couponModel->record(index.row());
int id = record.value(coupon_id).toInt();
CouponModel *m = new CouponModel(0, this);
CouponModel::selectById(id, m);
m->setDateChanged(QDateTime::currentDateTime());
m->setIsActive(1);
if (!m->status()) {
errorStatus(qApp->tr("Failure trying to register the record."));
return;
}
okStatus(qApp->tr("Successfully changed coupon."));
updateModels();
}
void Coupon::desactivateAction(void) {
QModelIndex index = couponTableView->currentIndex();
if (!index.isValid()) {
infoStatus(qApp->tr("Please select an item to edit."));
return;
}
QSqlRecord record = couponModel->record(index.row());
int id = record.value(coupon_id).toInt();
CouponModel *m = new CouponModel(0, this);
CouponModel::selectById(id, m);
m->setDateChanged(QDateTime::currentDateTime());
m->setIsActive(0);
if (!m->status()) {
errorStatus(qApp->tr("Failure trying to register the record."));
return;
}
okStatus(qApp->tr("Successfully changed coupon."));
updateModels();
}
void Coupon::removeAction(void) {
QModelIndex index = couponTableView->currentIndex();
if (!index.isValid()) {
infoStatus(qApp->tr("Please select an item to edit."));
return;
}
QSqlRecord record = couponModel->record(index.row());
int id = record.value(coupon_id).toInt();
QString name = record.value(coupon_name).toString();
QMessageBox msgBox;
msgBox.setText("Are you sure?");
msgBox.setInformativeText(QString(qApp->tr(
"Are you sure you want to delete the selected coupon objects?\n"
"All of the following objects and their related items will be "
"deleted:\n\nCoupon: %1\n").arg(name)));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No
| QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Yes);
int ret = msgBox.exec();
if (ret == QMessageBox::Cancel) {
return;
} else if (ret == QMessageBox::No)
return;
CouponModel *m = new CouponModel(0, this);
CouponModel::selectById(id, m);
if (!m->remove()) {
errorStatus(qApp->tr("Fails to remove the record."));
return;
}
okStatus(qApp->tr("Successfully deleted coupon."));
updateModels();
}
void Coupon::createSearchAdvanceWidget(void) {
couponSearch = new CouponSearch(couponModel);
connect(couponSearch, SIGNAL(formSearched()), this, SLOT(
updateSearchForm()));
connect(couponSearch, SIGNAL(formSearchClose()), this, SLOT(
updateSearchFormClose()));
}
void Coupon::searchAdvancedAction(void) {
if (!couponSearch->isVisible()) {
couponSearch->show();
couponSearch->raise();
couponSearch->activateWindow();
} else {
couponSearch->hide();
}
}
void Coupon::searchTextChangedAction() {
QString text = searchLineEdit->text();
if (text.isEmpty() || text.isNull()) {
couponModel->setF("");
} else {
couponModel->setF(QString("name LIKE '%1\%'").arg(text));
}
couponModel->setBegin(0);
updateModels();
}
void Coupon::closeAction(void) {
close();
}
void Coupon::selectedItemViewAction(const QModelIndex &index) {
if (!index.isValid()) {
infoStatus(qApp->tr("Please select an item to edit."));
return;
}
QSqlRecord record = couponModel->record(index.row());
int id = record.value(coupon_id).toInt();
couponModel->setId(id);
CouponForm *form = new CouponForm(couponModel);
connect(form, SIGNAL(formAdded()), this, SLOT(updateModels()));
connect(form, SIGNAL(formChanged()), this, SLOT(updateModels()));
connect(form, SIGNAL(formDeleted()), this, SLOT(updateModels()));
connect(form, SIGNAL(sendStatus(const QString &, int)), this,
SLOT(updateStatus(const QString &, int)));
form->show();
}
void Coupon::lastestAction(void) {
couponModel->setBegin(0);
updateModels();
}
void Coupon::nextAction(void) {
couponModel->setBegin(couponModel->begin() - couponModel->limit());
updateModels();
}
void Coupon::previousAction(void) {
couponModel->setBegin(couponModel->begin() + couponModel->limit());
updateModels();
}
void Coupon::oldestAction(void) {
int count = couponModel->count();
int limit = couponModel->limit();
couponModel->setBegin((limit * count / limit) - limit);
updateModels();
}
void Coupon::createModels(void) {
couponModel = new CouponModel(0, this);
couponModel->setTable("admin_register_coupon");
couponModel->setHeaderData(coupon_id, Qt::Horizontal, qApp->tr("Id"));
couponModel->setHeaderData(coupon_name, Qt::Horizontal, qApp->tr("Name"));
couponModel->setHeaderData(coupon_description, Qt::Horizontal, qApp->tr(
"Description"));
couponModel->setSort(coupon_id, Qt::DescendingOrder);
}
void Coupon::createViews(void) {
couponTableView->setModel(couponModel);
couponTableView->setItemDelegate(
new QSqlRelationalDelegate(couponTableView));
couponTableView->setSelectionMode(QAbstractItemView::SingleSelection);
couponTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
couponTableView->setColumnHidden(coupon_idAuthUser, true);
couponTableView->setColumnHidden(coupon_dateJoined, true);
couponTableView->setColumnHidden(coupon_dateChanged, true);
couponTableView->setColumnHidden(coupon_isActive, true);
couponTableView->resizeColumnsToContents();
couponTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
QHeaderView *header = couponTableView->horizontalHeader();
header->setStretchLastSection(true);
connect(header, SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)), this,
SLOT(updateModels()));
}
void Coupon::createActions(void) {
connect(statusTimer, SIGNAL(timeout()), this, SLOT(timerStatusAction()));
connect(newPushButton, SIGNAL(released()), this, SLOT(newAction()));
connect(activatePushButton, SIGNAL(released()), this,
SLOT(activateAction()));
connect(deactivatePushButton, SIGNAL(released()), this, SLOT(
desactivateAction()));
connect(removePushButton, SIGNAL(released()), this, SLOT(removeAction()));
connect(searchAdvancedPushButton, SIGNAL(released()), this, SLOT(
searchAdvancedAction()));
connect(searchLineEdit, SIGNAL(returnPressed()), this, SLOT(
searchTextChangedAction()));
//connect(closePushButton, SIGNAL(released()), this, SLOT(closeAction()));
connect(couponTableView, SIGNAL(activated(const QModelIndex &)), this,
SLOT(selectedItemViewAction(const QModelIndex &)));
connect(latestPushButton, SIGNAL(released()), this, SLOT(lastestAction()));
connect(nextPushButton, SIGNAL(released()), this, SLOT(nextAction()));
connect(previousPushButton, SIGNAL(released()), this,
SLOT(previousAction()));
connect(oldestPushButton, SIGNAL(released()), this, SLOT(oldestAction()));
}
void Coupon::updateWidgets(void) {
if (couponModel->query().size() > 0) {
activatePushButton->show();
deactivatePushButton->show();
removePushButton->show();
} else {
activatePushButton->hide();
deactivatePushButton->hide();
removePushButton->hide();
}
couponTableView->setTabKeyNavigation(true);
statusLabel->hide();
}
void Coupon::updateModels(void) {
couponModel->paginator();
qDebug() << "Query:" << couponModel->query().lastQuery();
int begin = couponModel->begin();
int limit = couponModel->limit();
int sizeAll = couponModel->count();
int size = couponModel->query().size();
statusCouponTableViewLabel->setText(sizeAll > 1 ? QString(qApp->tr(
"%1 coupons")).arg(sizeAll) : QString(qApp->tr("%1 coupon")).arg(
sizeAll));
statusPaginationLabel->setText(QString(qApp->tr(
"<b>%1</b> - <b>%2</b> de <b>%3</b>")).arg(begin).arg(limit).arg(
sizeAll));
latestPushButton->setEnabled(begin > 0);
nextPushButton->setEnabled(begin > 0);
previousPushButton->setEnabled(sizeAll > (size + begin));
oldestPushButton->setEnabled(sizeAll > (size + begin));
updateWidgets();
}
void Coupon::timerStatus(void) {
statusTimer->start(3000);
}
void Coupon::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 Coupon::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 Coupon::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 Coupon::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();
}
void Coupon::updateSearchForm(void) {
searchLineEdit->clear();
updateModels();
}
void Coupon::updateSearchFormClose(void) {
searchLineEdit->clear();
updateModels();
}
void Coupon::updateStatus(const QString &msg, int code) {
switch (code) {
case 0:
okStatus(msg);
break;
case 1:
infoStatus(msg);
break;
case 2:
alertStatus(msg);
break;
case 3:
errorStatus(msg);
break;
default:
okStatus(msg);
break;
}
}
Jump to Line
Something went wrong with that request. Please try again.