#include "employeeform.h"
employeeForm
::employeeForm(int id,
QWidget *parent
){
lblNameCode
= new QLabel(tr
("Alias"));
lblNameCode->setBuddy(leNameCode);
hblNameCode->addWidget(lblNameCode);
hblNameCode->addWidget(leNameCode);
lblDept
= new QLabel(tr
("Department"));
lblDept->setBuddy(cbDept);
hblDept->addWidget(lblDept);
hblDept->addWidget(cbDept);
lblFName
= new QLabel(tr
("First Name:"));
lblFName->setBuddy(leFName);
hblName->addWidget(lblFName);
hblName->addWidget(leFName);
hblName->addSpacing(20);
lblLName
= new QLabel(tr
("Last Name:"));
lblLName->setBuddy(leLName);
hblName->addWidget(lblLName);
hblName->addWidget(leLName);
lblStatus
= new QLabel(tr
("Employment Status:"));
lblStatus->setBuddy(cbStatus);
hblStatus->addWidget(lblStatus);
hblStatus->addWidget(cbStatus);
deStartDate->setCalendarPopup(true);
deStartDate->setDateRange(today.addDays(-90),today.addDays(90));
deStartDate->setDate(today);
lblStartDate
= new QLabel(tr
("Start Date:"));
lblStartDate->setBuddy(deStartDate);
hblStartDate->addWidget(lblStartDate);
hblStartDate->addWidget(deStartDate);
//finally lets set up the model
tableModel->setTable("tEmployees");
tableModel
->setRelation
(tEmployee_DepartmentId,
QSqlRelation("tDepartment",
"idtDepartment",
"DepartmentName"));
tableModel
->setRelation
(tEmployee_StatusId,
QSqlRelation("tStatus",
"idtStatus",
"StatusDescription"));
//tableModel->setSort(tEmployee_NameCode,Qt::AscendingOrder);
tableModel->select();
//uses the relation set earlier when setRelation() was called.
//because of this, we cannot just use the table enums to get the model row, they may be different
QSqlTableModel *relationModel
= tableModel
->relationModel
(tEmployee_DepartmentId
);
cbDept->setModel(relationModel);
cbDept->setModelColumn(relationModel->fieldIndex("DepartmentName"));//cbDept->setModelColumn(tDepartment_Name);
QSqlTableModel *relationModel_status
= tableModel
->relationModel
(tEmployee_StatusId
);
cbStatus->setModel(relationModel_status);
cbStatus->setModelColumn(relationModel_status->fieldIndex("StatusDescription"));//cbStatus->setModelColumn(tStatus_Desc);
//now we can map the records from the database into the form...
mapper->setModel(tableModel);
mapper->addMapping(leNameCode, tEmployee_NameCode);
mapper->addMapping(leFName,tEmployee_FName);
mapper->addMapping(leLName,tEmployee_LName);
mapper->addMapping(cbDept, tEmployee_DepartmentId);
mapper->addMapping(deStartDate,tEmployee_StartDate);
mapper->addMapping(cbStatus,tEmployee_StatusId);
//finally now if the form was called with a valid id, then populated with that Ids records...
//otherwise just use the first record ...
if(id != -1)
{
for(int row = 0; row < tableModel->rowCount(); ++row)
{
if(record.value(tEmployee_Id).toInt() == id)
{
mapper->setCurrentIndex(row);
break;
}
}
}
else
mapper->toFirst();
nav = new dbrecordnav(this);
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
vblMain->addLayout(hblNameCode);
vblMain->addLayout(hblDept);
vblMain->addLayout(hblName);
vblMain->addLayout(hblStartDate);
vblMain->addLayout(hblStatus);
vblMain->addWidget(nav);
setLayout(vblMain);
//finally connect statements
connect(nav,SIGNAL(first()),this,SLOT(go2First()));
connect(nav,SIGNAL(prev()),this,SLOT(go2Prev()));
connect(nav,SIGNAL(next()),this,SLOT(go2Next()));
connect(nav,SIGNAL(last()),this,SLOT(go2Last()));
connect(nav,SIGNAL(newrec()),this,SLOT(addEmployee()));
connect(nav,SIGNAL(deleterec()),this,SLOT(deleteEmployee()));
connect(bClose,SIGNAL(clicked()),this,SLOT(close()));
}
void employeeForm::go2First()
{
int row = mapper->currentIndex();
mapper->submit();
mapper->setCurrentIndex(row);
mapper->toFirst();
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
}
void employeeForm::go2Prev()
{
int row = mapper->currentIndex();
mapper->submit();
mapper->setCurrentIndex(row);
mapper->toPrevious();
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
}
void employeeForm::go2Next()
{
int row = mapper->currentIndex();
mapper->submit();
mapper->setCurrentIndex(row);
mapper->toNext();
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
}
void employeeForm::go2Last()
{
int row = mapper->currentIndex();
mapper->submit();
mapper->setCurrentIndex(row);
mapper->toLast();
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
}
void employeeForm::deleteEmployee()
{
int row = mapper->currentIndex();
tableModel->removeRow(row);
mapper->submit();
mapper->setCurrentIndex(qMin(row,tableModel->rowCount()-1));
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
}
#include "employeeform.h"
employeeForm::employeeForm(int id, QWidget *parent)
:QDialog(parent)
{
leNameCode = new QLineEdit;
lblNameCode = new QLabel(tr("Alias"));
lblNameCode->setBuddy(leNameCode);
QHBoxLayout *hblNameCode = new QHBoxLayout();
hblNameCode->addWidget(lblNameCode);
hblNameCode->addWidget(leNameCode);
cbDept = new QComboBox;
lblDept = new QLabel(tr("Department"));
lblDept->setBuddy(cbDept);
QHBoxLayout *hblDept = new QHBoxLayout();
hblDept->addWidget(lblDept);
hblDept->addWidget(cbDept);
leFName = new QLineEdit;
lblFName = new QLabel(tr("First Name:"));
lblFName->setBuddy(leFName);
QHBoxLayout *hblName = new QHBoxLayout();
hblName->addWidget(lblFName);
hblName->addWidget(leFName);
hblName->addSpacing(20);
leLName = new QLineEdit;
lblLName = new QLabel(tr("Last Name:"));
lblLName->setBuddy(leLName);
hblName->addWidget(lblLName);
hblName->addWidget(leLName);
cbStatus = new QComboBox;
lblStatus = new QLabel(tr("Employment Status:"));
lblStatus->setBuddy(cbStatus);
QHBoxLayout *hblStatus = new QHBoxLayout();
hblStatus->addWidget(lblStatus);
hblStatus->addWidget(cbStatus);
deStartDate = new QDateEdit;
deStartDate->setCalendarPopup(true);
QDate today = QDate::currentDate();
deStartDate->setDateRange(today.addDays(-90),today.addDays(90));
deStartDate->setDate(today);
lblStartDate = new QLabel(tr("Start Date:"));
lblStartDate->setBuddy(deStartDate);
QHBoxLayout *hblStartDate = new QHBoxLayout();
hblStartDate->addWidget(lblStartDate);
hblStartDate->addWidget(deStartDate);
bClose = new QPushButton("&Close");
//finally lets set up the model
tableModel = new QSqlRelationalTableModel(this);
tableModel->setTable("tEmployees");
tableModel->setRelation(tEmployee_DepartmentId, QSqlRelation("tDepartment","idtDepartment","DepartmentName"));
tableModel->setRelation(tEmployee_StatusId,QSqlRelation("tStatus","idtStatus","StatusDescription"));
//tableModel->setSort(tEmployee_NameCode,Qt::AscendingOrder);
tableModel->select();
//uses the relation set earlier when setRelation() was called.
//because of this, we cannot just use the table enums to get the model row, they may be different
QSqlTableModel *relationModel = tableModel->relationModel(tEmployee_DepartmentId);
cbDept->setModel(relationModel);
cbDept->setModelColumn(relationModel->fieldIndex("DepartmentName"));//cbDept->setModelColumn(tDepartment_Name);
QSqlTableModel *relationModel_status = tableModel->relationModel(tEmployee_StatusId);
cbStatus->setModel(relationModel_status);
cbStatus->setModelColumn(relationModel_status->fieldIndex("StatusDescription"));//cbStatus->setModelColumn(tStatus_Desc);
//now we can map the records from the database into the form...
mapper = new QDataWidgetMapper(this);
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mapper->setModel(tableModel);
mapper->setItemDelegate(new QSqlRelationalDelegate(this));
mapper->addMapping(leNameCode, tEmployee_NameCode);
mapper->addMapping(leFName,tEmployee_FName);
mapper->addMapping(leLName,tEmployee_LName);
mapper->addMapping(cbDept, tEmployee_DepartmentId);
mapper->addMapping(deStartDate,tEmployee_StartDate);
mapper->addMapping(cbStatus,tEmployee_StatusId);
//finally now if the form was called with a valid id, then populated with that Ids records...
//otherwise just use the first record ...
if(id != -1)
{
for(int row = 0; row < tableModel->rowCount(); ++row)
{
QSqlRecord record = tableModel->record(row);
if(record.value(tEmployee_Id).toInt() == id)
{
mapper->setCurrentIndex(row);
break;
}
}
}
else
mapper->toFirst();
nav = new dbrecordnav(this);
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
QVBoxLayout *vblMain = new QVBoxLayout();
vblMain->addLayout(hblNameCode);
vblMain->addLayout(hblDept);
vblMain->addLayout(hblName);
vblMain->addLayout(hblStartDate);
vblMain->addLayout(hblStatus);
vblMain->addWidget(nav);
setLayout(vblMain);
//finally connect statements
connect(nav,SIGNAL(first()),this,SLOT(go2First()));
connect(nav,SIGNAL(prev()),this,SLOT(go2Prev()));
connect(nav,SIGNAL(next()),this,SLOT(go2Next()));
connect(nav,SIGNAL(last()),this,SLOT(go2Last()));
connect(nav,SIGNAL(newrec()),this,SLOT(addEmployee()));
connect(nav,SIGNAL(deleterec()),this,SLOT(deleteEmployee()));
connect(bClose,SIGNAL(clicked()),this,SLOT(close()));
}
void employeeForm::go2First()
{
int row = mapper->currentIndex();
mapper->submit();
mapper->setCurrentIndex(row);
mapper->toFirst();
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
}
void employeeForm::go2Prev()
{
int row = mapper->currentIndex();
mapper->submit();
mapper->setCurrentIndex(row);
mapper->toPrevious();
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
}
void employeeForm::go2Next()
{
int row = mapper->currentIndex();
mapper->submit();
mapper->setCurrentIndex(row);
mapper->toNext();
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
}
void employeeForm::go2Last()
{
int row = mapper->currentIndex();
mapper->submit();
mapper->setCurrentIndex(row);
mapper->toLast();
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
}
void employeeForm::deleteEmployee()
{
int row = mapper->currentIndex();
tableModel->removeRow(row);
mapper->submit();
mapper->setCurrentIndex(qMin(row,tableModel->rowCount()-1));
nav->setCurrentRec(mapper->currentIndex()+1);
nav->setTotalRec(tableModel->rowCount());
}
To copy to clipboard, switch view to plain text mode
Bookmarks