Subversion Repositories Programming Utils

Rev

Rev 88 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <QDebug>
#include <QPushButton>

#include "traintablemodel.h"
#include "dbexception.h"

TrainTableModel::TrainTableModel( DBAccess* access, QObject *parent) :
    QAbstractTableModel(parent)
{
    this->access = access;
}

int TrainTableModel::rowCount(const QModelIndex &) const{
    return access->getNumberOfRecords();
}

int TrainTableModel::columnCount(const QModelIndex &) const{
    return 5;
}

QVariant TrainTableModel::data(const QModelIndex &index, int role) const{
    int row = index.row();
    int col = index.column();

    if (role == Qt::DisplayRole || role == Qt::EditRole) {
        TrainRecord* record = this->access->getRecord( row );
        if( record == NULL ){
            qDebug() << "ERROR: TrainRecord NULL.  Row " << row;
            return QVariant();
        }
        switch( col ){
        case 0: return QString( record->getReportingMark().c_str() );
        case 1: return record->getRoadNumber();
        case 2: return record->getCost();
        case 3: return QString( record->getStockType().c_str() );
        case 4: return QString( record->getNotes().c_str() );
        case 5: return QString( "Delete" );
        }
    }
    return QVariant();
}

QVariant TrainTableModel::headerData(int section, Qt::Orientation orientation, int role) const{
    if (role == Qt::DisplayRole) {
        if( orientation == Qt::Horizontal ){
            switch( section ){
            case 0: return QString( "Reporting Mark" );
            case 1: return QString( "Road Number" );
            case 2: return QString( "Cost" );
            case 3: return QString( "Type" );
            case 4: return QString( "Notes" );
            case 5: return QString( "Delete" );
            }
        }
    }
    return QVariant();
}

Qt::ItemFlags TrainTableModel::flags(const QModelIndex &) const{
    return Qt::ItemIsSelectable |  Qt::ItemIsEditable | Qt::ItemIsEnabled ;
}

bool TrainTableModel::setData(const QModelIndex &index, const QVariant &value, int role){
    int row = index.row();
    int col = index.column();

    if( role == Qt::EditRole ){
        TrainRecord* record = this->access->getRecord( row );
        switch( col ){
        case 0: record->setReportingMark( value.toString().toStdString() );
            break;
        case 1: record->setRoadNumber( value.toInt() );
            break;
        case 2: record->setCost( value.toInt() );
            break;
        case 3: record->setStockType( value.toString().toStdString() );
            break;
        case 4: record->setNotes( value.toString().toStdString() );
            break;
        }
        try{
            access->updateRecord( record );
        }catch( DBException ex ){
            qDebug() << ex.what();
        }
    }

    emit dataChanged( index, index );
    return true;
}

void TrainTableModel::updateFull(){
    QModelIndex topIndex = createIndex( 0, 0 );
    QModelIndex bottomIndex = createIndex( access->getNumberOfRecords(), 5 );

    emit dataChanged( topIndex, bottomIndex );
}

void TrainTableModel::updateLastRecord(){
   beginInsertRows( QModelIndex(), access->getNumberOfRecords(), access->getNumberOfRecords() );
   endInsertRows();

   emit dataChanged( QModelIndex(), QModelIndex() );
}

void TrainTableModel::deleteRow( int row ){
    TrainRecord* rec = access->getRecord( row);

    beginRemoveRows( QModelIndex(), rec->getKey(), rec->getKey() );
    endRemoveRows();

    access->deleteRecord( rec );

    emit dataChanged( QModelIndex(), QModelIndex() );
}