#include "frmstatus.h"
#include "ui_frmstatus.h"
frmStatus
::frmStatus(QWidget *parent
) : ui(new Ui::frmStatus)
{
ui->setupUi(this);
tableModel->setTable("tStatus");
tableModel->select();
tableModel->setHeaderData(0, Qt::Horizontal, tr("Status ID"),Qt::DisplayRole);
tableModel->setHeaderData(1,Qt::Horizontal,tr("Status Description"), Qt::DisplayRole);
qDebug()<<"row count:"<<tableModel->rowCount();
nav = new dbrecordnav(this);
hb1
->addWidget
(new QLabel(""),
1);
hb1->addWidget(nav);
hb1
->addWidget
(new QLabel(""),
1);
ui->verticalLayout->addLayout(hb1);
nav->setCurrentRec( 1);
nav->setTotalRec(tableModel->rowCount());
ui->tableView->setModel(tableModel);
ui->tableView->horizontalHeader()->setClickable(true);
ui->tableView->setCurrentIndex(mi);
setModal(true);
#ifdef RELEASE
ui->tableView->hideColumn(0);
#endif
ui->tableView->resizeColumnToContents(1);
itemSelModel = ui->tableView->selectionModel();
ui->tableView->installEventFilter(this);
//ui->tableView->setItemDelegateForColumn(1,statusDescEdit);
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(addStatus()));
connect(nav,SIGNAL(deleterec()),this,SLOT(deleteStatus()));
connect(nav,SIGNAL(changeRecord(int)),this,SLOT(go2Rec(int)));
connect(this,SIGNAL(dirty()),nav,SLOT(handleDirtyRecord()));
connect(this,SIGNAL(revert()),nav,SLOT(handleCleanRecord()));
// connect(nav,SIGNAL(storeRecChanges()),this,SLOT(saveCurRec()));
}
frmStatus::~frmStatus()
{
delete ui;
}
{
if(object == ui->tableView)
{
if (e
->type
() == QEvent::KeyPress) {
QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(e
);
#ifdef DEBUG
qDebug() << "EVENT FILTER key press" << keyEvent->key();
#endif
if(keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return)
{
#ifdef DEBUG
qDebug()<<"Pressed Enter key before model reset!";
#endif
//goahead and try to save
if(!tableModel->submit()){qDebug()<<"Submit failed... try again!"<<tableModel->lastError().text();}
//e->ignore();
return true;
}
else if(keyEvent->key() == Qt::Key_Down)
{
this->go2Next();
return true;
}
else if(keyEvent->key() == Qt::Key_Up)
{
this->go2Prev();
return true;
}
else if(keyEvent->key() == Qt::Key_Escape)
{
qDebug()<<"EVENT FILTER: Escape pressed";
blnDirty = false;
revertForm();
return true;
}
else
{
//dirty
qDebug()<<"form dirty...";
blnDirty = true;
emit dirty();
return false;
}
}
else
{
return false;
}
}
else
{
// pass the event on to the parent class
return QDialog::eventFilter(object, e
);
}
}
{
if(current.row()==-1)
{
nav->setCurrentRec(previous.row()+1);
tableModel->select();
ui->tableView->setModel(tableModel);
itemSelModel = ui->tableView->selectionModel();
QModelIndex mi
= ui
->tableView
->model
()->index
(previous.
row(),
1);
ui->tableView->setCurrentIndex(mi);
ui->tableView->setRootIndex(mi);
#ifdef DEBUG
qDebug()<<"Model Reset!";
qDebug()<<"Current index of table view:"<<ui->tableView->currentIndex().row();
#endif
}
else
{
qDebug()<<"Normal Cell Change..." <<current.row()<<previous.row();
nav->setCurrentRec(current.row()+1);
if(!(tableModel->submit())){qDebug()<<"Error with submit on row change:"<<tableModel->lastError().text();}
qDebug()<<"Normal Cell Change...After Submit!" <<current.row()<<previous.row();
//ui->tableView->setModel(tableModel);
// itemSelModel = ui->tableView->selectionModel();
//QModelIndex mi = ui->tableView->model()->index(0,1);
//ui->tableView->setCurrentIndex(mi);
//ui->tableView->setRootIndex(mi);
//itemSelModel->setCurrentIndex(mi,QItemSelectionModel::Current);
}
qDebug()<<"atend_Current index of table view:"<<ui->tableView->currentIndex().row();
}
void frmStatus::addStatus()
{
tableModel->insertRows(0,1);
tableModel
->setData
(tableModel
->index
(0,
1),
QString(" "));
if(!tableModel->submit()){qDebug()<<"Submit failed... try again!"<<tableModel->lastError().text();}
nav->setTotalRec(tableModel->rowCount());
QModelIndex mi
= ui
->tableView
->model
()->index
(tableModel
->rowCount
()-1,
1);
ui->tableView->setCurrentIndex(mi);
ui->tableView->resizeRowsToContents();
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
void frmStatus::deleteStatus()
{
qDebug()<<"Current index of table view:"<<ui->tableView->currentIndex().row();
tableModel->removeRow(nav->getCurrentRec()-1);
if(!tableModel->submit()){qDebug()<<"Submit failed... try again!"<<tableModel->lastError().text();}
nav->setTotalRec(tableModel->rowCount());
QModelIndex mi
= ui
->tableView
->model
()->index
(tableModel
->rowCount
()-1,
1);
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
qDebug()<<"Current index of table view:"<<ui->tableView->currentIndex().row();
ui->tableView->resizeRowsToContents();
}
void frmStatus::go2First()
{
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
void frmStatus::go2Prev()
{
//check if already at beginning...
if(!(ui->tableView->currentIndex().row() == 0))
{
QModelIndex mi
= ui
->tableView
->model
()->index
(ui
->tableView
->currentIndex
().
row()-1,
1);
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
}
void frmStatus::go2Next()
{
//check if already at end...
if(!(ui->tableView->currentIndex().row() == tableModel->rowCount()-1))
{
QModelIndex mi
= ui
->tableView
->model
()->index
(ui
->tableView
->currentIndex
().
row()+1,
1);
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
}
void frmStatus::go2Last()
{
QModelIndex mi
= ui
->tableView
->model
()->index
(tableModel
->rowCount
()-1,
1);
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
void frmStatus::go2Rec(int a)
{
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
/*void frmStatus::keyPressEvent(QKeyEvent *event)
{
if(event->key()==Qt::Key_Escape)
{
qDebug()<<"STATUS FORM - Esc pressed";
blnDirty = false;
revertForm();
}
else if(event->key() == Qt::Key_Return||event->key() == Qt::Key_Enter)
{
qDebug()<<"Enter Key Pressed - STATUS FORM";
}
else
{
qDebug()<<"form dirty...";
blnDirty = true;
emit dirty();
}
}*/
void frmStatus::revertForm()
{
emit revert(); //resets nav icon...
}
/*
The following are only used in Forms with mappign widgets not controlled by a table view.
here the view handles all the submitting etc... we don't need to do any additional...
just connect up the signals/slots to make the form function as we want...*/
/*
void frmStatus::disconnectDirty(){}
void frmStatus::connectDirty(){}
void frmStatus::setDirty() { blnDirty = true; emit dirty();qDebug()<<"we got a dirty() signal!";} //set whenever something changes on the form
void frmStatus::setDirty(int){blnDirty = true;emit dirty();} //set whenever something changes on the form
void frmStatus::setDirty(QString){blnDirty = true;emit dirty();} //set whenever something changes on the form
void frmStatus::setDirty(QDate){blnDirty = true;emit dirty();} //set whenever something changes on the form
void frmStatus::revertForm()
{
}
bool frmStatus::getCurRecData()
{
bool res;
return res;
}
bool frmStatus::getUpdateRecData()
{
bool res;
return res;
}
bool frmStatus::updateSQLData()
{
bool res;
return res;
}
bool frmStatus::handleNewRec()
{
bool res;
return res;
}
void frmStatus::saveCurRec()
{
}
*/
#include "frmstatus.h"
#include "ui_frmstatus.h"
frmStatus::frmStatus(QWidget *parent) :
QDialog(parent),
ui(new Ui::frmStatus)
{
ui->setupUi(this);
tableModel = new QSqlTableModel();
tableModel->setTable("tStatus");
tableModel->setEditStrategy(QSqlTableModel::OnRowChange);
tableModel->select();
tableModel->setHeaderData(0, Qt::Horizontal, tr("Status ID"),Qt::DisplayRole);
tableModel->setHeaderData(1,Qt::Horizontal,tr("Status Description"), Qt::DisplayRole);
qDebug()<<"row count:"<<tableModel->rowCount();
nav = new dbrecordnav(this);
QHBoxLayout *hb1 = new QHBoxLayout();
hb1->addWidget(new QLabel(""),1);
hb1->addWidget(nav);
hb1->addWidget(new QLabel(""),1);
ui->verticalLayout->addLayout(hb1);
nav->setCurrentRec( 1);
nav->setTotalRec(tableModel->rowCount());
ui->tableView->setModel(tableModel);
ui->tableView->horizontalHeader()->setClickable(true);
QModelIndex mi = ui->tableView->model()->index(0,1);
ui->tableView->setCurrentIndex(mi);
setModal(true);
#ifdef RELEASE
ui->tableView->hideColumn(0);
#endif
ui->tableView->resizeColumnToContents(1);
itemSelModel = new QItemSelectionModel(tableModel,this);
itemSelModel = ui->tableView->selectionModel();
ui->tableView->installEventFilter(this);
//ui->tableView->setItemDelegateForColumn(1,statusDescEdit);
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(addStatus()));
connect(nav,SIGNAL(deleterec()),this,SLOT(deleteStatus()));
connect(nav,SIGNAL(changeRecord(int)),this,SLOT(go2Rec(int)));
connect(this,SIGNAL(dirty()),nav,SLOT(handleDirtyRecord()));
connect(this,SIGNAL(revert()),nav,SLOT(handleCleanRecord()));
// connect(nav,SIGNAL(storeRecChanges()),this,SLOT(saveCurRec()));
connect(itemSelModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(rowChanged(QModelIndex, QModelIndex)));
}
frmStatus::~frmStatus()
{
delete ui;
}
bool frmStatus::eventFilter(QObject *object, QEvent *e)
{
if(object == ui->tableView)
{
if (e->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(e);
#ifdef DEBUG
qDebug() << "EVENT FILTER key press" << keyEvent->key();
#endif
if(keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return)
{
#ifdef DEBUG
qDebug()<<"Pressed Enter key before model reset!";
#endif
//goahead and try to save
if(!tableModel->submit()){qDebug()<<"Submit failed... try again!"<<tableModel->lastError().text();}
//e->ignore();
return true;
}
else if(keyEvent->key() == Qt::Key_Down)
{
this->go2Next();
return true;
}
else if(keyEvent->key() == Qt::Key_Up)
{
this->go2Prev();
return true;
}
else if(keyEvent->key() == Qt::Key_Escape)
{
qDebug()<<"EVENT FILTER: Escape pressed";
blnDirty = false;
revertForm();
return true;
}
else
{
//dirty
qDebug()<<"form dirty...";
blnDirty = true;
emit dirty();
return false;
}
}
else
{
return false;
}
}
else
{
// pass the event on to the parent class
return QDialog::eventFilter(object, e);
}
}
void frmStatus::rowChanged(QModelIndex current, QModelIndex previous)
{
disconnect(itemSelModel,SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),this,SLOT(rowChanged(QModelIndex, QModelIndex)));
if(current.row()==-1)
{
nav->setCurrentRec(previous.row()+1);
tableModel->select();
ui->tableView->setModel(tableModel);
itemSelModel = ui->tableView->selectionModel();
QModelIndex mi = ui->tableView->model()->index(previous.row(),1);
ui->tableView->setCurrentIndex(mi);
ui->tableView->setRootIndex(mi);
itemSelModel->setCurrentIndex(mi,QItemSelectionModel::Current);
#ifdef DEBUG
qDebug()<<"Model Reset!";
qDebug()<<"Current index of table view:"<<ui->tableView->currentIndex().row();
#endif
}
else
{
qDebug()<<"Normal Cell Change..." <<current.row()<<previous.row();
nav->setCurrentRec(current.row()+1);
if(!(tableModel->submit())){qDebug()<<"Error with submit on row change:"<<tableModel->lastError().text();}
qDebug()<<"Normal Cell Change...After Submit!" <<current.row()<<previous.row();
//ui->tableView->setModel(tableModel);
// itemSelModel = ui->tableView->selectionModel();
//QModelIndex mi = ui->tableView->model()->index(0,1);
//ui->tableView->setCurrentIndex(mi);
//ui->tableView->setRootIndex(mi);
//itemSelModel->setCurrentIndex(mi,QItemSelectionModel::Current);
}
connect(itemSelModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(rowChanged(QModelIndex, QModelIndex)));
qDebug()<<"atend_Current index of table view:"<<ui->tableView->currentIndex().row();
}
void frmStatus::addStatus()
{
tableModel->insertRows(0,1);
tableModel->setData(tableModel->index(0,1),QString(" "));
if(!tableModel->submit()){qDebug()<<"Submit failed... try again!"<<tableModel->lastError().text();}
nav->setTotalRec(tableModel->rowCount());
QModelIndex mi = ui->tableView->model()->index(tableModel->rowCount()-1,1);
ui->tableView->setCurrentIndex(mi);
ui->tableView->resizeRowsToContents();
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
void frmStatus::deleteStatus()
{
qDebug()<<"Current index of table view:"<<ui->tableView->currentIndex().row();
tableModel->removeRow(nav->getCurrentRec()-1);
if(!tableModel->submit()){qDebug()<<"Submit failed... try again!"<<tableModel->lastError().text();}
nav->setTotalRec(tableModel->rowCount());
QModelIndex mi = ui->tableView->model()->index(tableModel->rowCount()-1,1);
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
qDebug()<<"Current index of table view:"<<ui->tableView->currentIndex().row();
ui->tableView->resizeRowsToContents();
}
void frmStatus::go2First()
{
QModelIndex mi = ui->tableView->model()->index(0,1);
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
void frmStatus::go2Prev()
{
//check if already at beginning...
if(!(ui->tableView->currentIndex().row() == 0))
{
QModelIndex mi = ui->tableView->model()->index(ui->tableView->currentIndex().row()-1,1);
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
}
void frmStatus::go2Next()
{
//check if already at end...
if(!(ui->tableView->currentIndex().row() == tableModel->rowCount()-1))
{
QModelIndex mi = ui->tableView->model()->index(ui->tableView->currentIndex().row()+1,1);
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
}
void frmStatus::go2Last()
{
QModelIndex mi = ui->tableView->model()->index(tableModel->rowCount()-1,1);
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
void frmStatus::go2Rec(int a)
{
QModelIndex mi = ui->tableView->model()->index(a-1,1);
ui->tableView->setCurrentIndex(mi);
nav->setCurrentRec(ui->tableView->currentIndex().row()+1);
}
/*void frmStatus::keyPressEvent(QKeyEvent *event)
{
if(event->key()==Qt::Key_Escape)
{
qDebug()<<"STATUS FORM - Esc pressed";
blnDirty = false;
revertForm();
}
else if(event->key() == Qt::Key_Return||event->key() == Qt::Key_Enter)
{
qDebug()<<"Enter Key Pressed - STATUS FORM";
}
else
{
qDebug()<<"form dirty...";
blnDirty = true;
emit dirty();
}
}*/
void frmStatus::revertForm()
{
emit revert(); //resets nav icon...
}
/*
The following are only used in Forms with mappign widgets not controlled by a table view.
here the view handles all the submitting etc... we don't need to do any additional...
just connect up the signals/slots to make the form function as we want...*/
/*
void frmStatus::disconnectDirty(){}
void frmStatus::connectDirty(){}
void frmStatus::setDirty() { blnDirty = true; emit dirty();qDebug()<<"we got a dirty() signal!";} //set whenever something changes on the form
void frmStatus::setDirty(int){blnDirty = true;emit dirty();} //set whenever something changes on the form
void frmStatus::setDirty(QString){blnDirty = true;emit dirty();} //set whenever something changes on the form
void frmStatus::setDirty(QDate){blnDirty = true;emit dirty();} //set whenever something changes on the form
void frmStatus::revertForm()
{
}
bool frmStatus::getCurRecData()
{
bool res;
return res;
}
bool frmStatus::getUpdateRecData()
{
bool res;
return res;
}
bool frmStatus::updateSQLData()
{
bool res;
return res;
}
bool frmStatus::handleNewRec()
{
bool res;
return res;
}
void frmStatus::saveCurRec()
{
}
*/
To copy to clipboard, switch view to plain text mode
Bookmarks