#include "economyhoursmodel.h"
#include <QSqlQuery>
#include <QDebug>
/* Constructor
*
*/
EconomyHoursModel
::EconomyHoursModel( QObject *parent
) :{
}
/* Reimplemented function: flags
* Set the att coulumn editable flag
*/
Qt
::ItemFlags EconomyHoursModel
::flags( const QModelIndex &index
) const{
if( index.column() == 3 )
flags |= Qt::ItemIsEditable;
return flags;
}
/* Reimplemented function: setData
* Sets data for the att column
*/
bool EconomyHoursModel
::setData( const QModelIndex &index,
const QVariant &value,
int /*role*/ ) {
if( index.column() != 3 )
return false;
int id = data( idIndex ).toInt();
clear();
query.prepare( "UPDATE hours SET att=? WHERE id=?" );
query.addBindValue( value );
query.addBindValue( id );
bool ret = query.exec();
if( ret )
refresh();
return ret;
}
/* Public function: refresh
* Refresh the query with current start and end date
*/
void EconomyHoursModel::refresh()
{
query.prepare( "SELECT hours.date, hours.hours, repairmen.name, hours.att, hours.id\n"
"FROM hours\n"
"LEFT JOIN repairmen\n"
"ON repairmen.id=hours.repairmen_id\n"
"WHERE hours.date>=? AND hours.date<=?;" );
query.addBindValue( StartDate );
query.addBindValue( EndDate );
query.exec();
setQuery( query );
setHeaderData( 0, Qt::Horizontal, "Datum" );
setHeaderData( 1, Qt::Horizontal, "Antal" );
setHeaderData( 2, Qt::Horizontal, "Reparatör" );
setHeaderData( 3, Qt::Horizontal, "Att" );
setHeaderData( 4, Qt::Horizontal, "ID" );
}
/* Public function: setStartDate
* set start date for the query and refresh
*/
void EconomyHoursModel
::setStartDate( QDate date
) {
StartDate = date;
refresh();
}
/* Public function: setEndDate
* set end date for the query and refresh
*/
void EconomyHoursModel
::setEndDate( QDate date
) {
EndDate = date;
refresh();
}
#include "economyhoursmodel.h"
#include <QSqlQuery>
#include <QDebug>
/* Constructor
*
*/
EconomyHoursModel::EconomyHoursModel( QObject *parent ) :
QSqlQueryModel( parent )
{
}
/* Reimplemented function: flags
* Set the att coulumn editable flag
*/
Qt::ItemFlags EconomyHoursModel::flags( const QModelIndex &index ) const
{
Qt::ItemFlags flags = QSqlQueryModel::flags( index );
if( index.column() == 3 )
flags |= Qt::ItemIsEditable;
return flags;
}
/* Reimplemented function: setData
* Sets data for the att column
*/
bool EconomyHoursModel::setData( const QModelIndex &index, const QVariant &value, int /*role*/ )
{
if( index.column() != 3 )
return false;
QModelIndex idIndex = QSqlQueryModel::index( index.row(), 4 );
int id = data( idIndex ).toInt();
clear();
QSqlQuery query;
query.prepare( "UPDATE hours SET att=? WHERE id=?" );
query.addBindValue( value );
query.addBindValue( id );
bool ret = query.exec();
if( ret )
refresh();
return ret;
}
/* Public function: refresh
* Refresh the query with current start and end date
*/
void EconomyHoursModel::refresh()
{
QSqlQuery query;
query.prepare( "SELECT hours.date, hours.hours, repairmen.name, hours.att, hours.id\n"
"FROM hours\n"
"LEFT JOIN repairmen\n"
"ON repairmen.id=hours.repairmen_id\n"
"WHERE hours.date>=? AND hours.date<=?;" );
query.addBindValue( StartDate );
query.addBindValue( EndDate );
query.exec();
setQuery( query );
setHeaderData( 0, Qt::Horizontal, "Datum" );
setHeaderData( 1, Qt::Horizontal, "Antal" );
setHeaderData( 2, Qt::Horizontal, "Reparatör" );
setHeaderData( 3, Qt::Horizontal, "Att" );
setHeaderData( 4, Qt::Horizontal, "ID" );
}
/* Public function: setStartDate
* set start date for the query and refresh
*/
void EconomyHoursModel::setStartDate( QDate date )
{
StartDate = date;
refresh();
}
/* Public function: setEndDate
* set end date for the query and refresh
*/
void EconomyHoursModel::setEndDate( QDate date )
{
EndDate = date;
refresh();
}
To copy to clipboard, switch view to plain text mode
Bookmarks