Rev 88 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
87 | rm5248 | 1 | #include <QDebug> |
88 | rm5248 | 2 | #include <QPushButton> |
87 | rm5248 | 3 | |
4 | #include "traintablemodel.h" |
||
88 | rm5248 | 5 | #include "dbexception.h" |
87 | rm5248 | 6 | |
7 | TrainTableModel::TrainTableModel( DBAccess* access, QObject *parent) : |
||
8 | QAbstractTableModel(parent) |
||
9 | { |
||
10 | this->access = access; |
||
11 | } |
||
12 | |||
13 | int TrainTableModel::rowCount(const QModelIndex &) const{ |
||
14 | return access->getNumberOfRecords(); |
||
15 | } |
||
16 | |||
17 | int TrainTableModel::columnCount(const QModelIndex &) const{ |
||
18 | return 5; |
||
19 | } |
||
20 | |||
21 | QVariant TrainTableModel::data(const QModelIndex &index, int role) const{ |
||
22 | int row = index.row(); |
||
23 | int col = index.column(); |
||
24 | |||
25 | if (role == Qt::DisplayRole || role == Qt::EditRole) { |
||
26 | TrainRecord* record = this->access->getRecord( row ); |
||
27 | if( record == NULL ){ |
||
91 | rm5248 | 28 | qDebug() << "ERROR: TrainRecord NULL. Row " << row; |
87 | rm5248 | 29 | return QVariant(); |
30 | } |
||
31 | switch( col ){ |
||
32 | case 0: return QString( record->getReportingMark().c_str() ); |
||
33 | case 1: return record->getRoadNumber(); |
||
34 | case 2: return record->getCost(); |
||
35 | case 3: return QString( record->getStockType().c_str() ); |
||
36 | case 4: return QString( record->getNotes().c_str() ); |
||
88 | rm5248 | 37 | case 5: return QString( "Delete" ); |
87 | rm5248 | 38 | } |
39 | } |
||
40 | return QVariant(); |
||
41 | } |
||
42 | |||
43 | QVariant TrainTableModel::headerData(int section, Qt::Orientation orientation, int role) const{ |
||
44 | if (role == Qt::DisplayRole) { |
||
45 | if( orientation == Qt::Horizontal ){ |
||
46 | switch( section ){ |
||
47 | case 0: return QString( "Reporting Mark" ); |
||
48 | case 1: return QString( "Road Number" ); |
||
49 | case 2: return QString( "Cost" ); |
||
50 | case 3: return QString( "Type" ); |
||
51 | case 4: return QString( "Notes" ); |
||
88 | rm5248 | 52 | case 5: return QString( "Delete" ); |
87 | rm5248 | 53 | } |
54 | } |
||
55 | } |
||
56 | return QVariant(); |
||
57 | } |
||
58 | |||
59 | Qt::ItemFlags TrainTableModel::flags(const QModelIndex &) const{ |
||
60 | return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ; |
||
61 | } |
||
62 | |||
63 | bool TrainTableModel::setData(const QModelIndex &index, const QVariant &value, int role){ |
||
64 | int row = index.row(); |
||
65 | int col = index.column(); |
||
66 | |||
67 | if( role == Qt::EditRole ){ |
||
68 | TrainRecord* record = this->access->getRecord( row ); |
||
69 | switch( col ){ |
||
70 | case 0: record->setReportingMark( value.toString().toStdString() ); |
||
71 | break; |
||
72 | case 1: record->setRoadNumber( value.toInt() ); |
||
73 | break; |
||
74 | case 2: record->setCost( value.toInt() ); |
||
75 | break; |
||
76 | case 3: record->setStockType( value.toString().toStdString() ); |
||
77 | break; |
||
78 | case 4: record->setNotes( value.toString().toStdString() ); |
||
79 | break; |
||
80 | } |
||
88 | rm5248 | 81 | try{ |
82 | access->updateRecord( record ); |
||
83 | }catch( DBException ex ){ |
||
84 | qDebug() << ex.what(); |
||
85 | } |
||
87 | rm5248 | 86 | } |
88 | rm5248 | 87 | |
88 | emit dataChanged( index, index ); |
||
87 | rm5248 | 89 | return true; |
90 | } |
||
91 | |||
92 | void TrainTableModel::updateFull(){ |
||
93 | QModelIndex topIndex = createIndex( 0, 0 ); |
||
94 | QModelIndex bottomIndex = createIndex( access->getNumberOfRecords(), 5 ); |
||
95 | |||
96 | emit dataChanged( topIndex, bottomIndex ); |
||
97 | } |
||
88 | rm5248 | 98 | |
99 | void TrainTableModel::updateLastRecord(){ |
||
100 | beginInsertRows( QModelIndex(), access->getNumberOfRecords(), access->getNumberOfRecords() ); |
||
101 | endInsertRows(); |
||
102 | |||
103 | emit dataChanged( QModelIndex(), QModelIndex() ); |
||
104 | } |
||
105 | |||
106 | void TrainTableModel::deleteRow( int row ){ |
||
107 | TrainRecord* rec = access->getRecord( row); |
||
108 | |||
109 | beginRemoveRows( QModelIndex(), rec->getKey(), rec->getKey() ); |
||
110 | endRemoveRows(); |
||
111 | |||
112 | access->deleteRecord( rec ); |
||
113 | |||
114 | emit dataChanged( QModelIndex(), QModelIndex() ); |
||
115 | } |