Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "endorse-sql-table-model.h" |
| 12 | |
| 13 | #ifndef Q_MOC_RUN |
| 14 | #include "logging.h" |
| 15 | #endif |
| 16 | |
| 17 | |
| 18 | INIT_LOGGER("EndorseSqlTableModel"); |
| 19 | |
| 20 | EndorseSqlTableModel::EndorseSqlTableModel(QObject * parent, QSqlDatabase db) |
| 21 | : QSqlTableModel(parent, db) |
| 22 | {} |
| 23 | |
| 24 | EndorseSqlTableModel::~EndorseSqlTableModel() |
| 25 | {} |
| 26 | |
| 27 | Qt::ItemFlags |
| 28 | EndorseSqlTableModel::flags ( const QModelIndex & index ) |
| 29 | { |
| 30 | if(index.column() == 3) |
| 31 | { |
| 32 | _LOG_DEBUG("index: row: " << index.row() << " " << index.column() << " check!"); |
| 33 | return Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsUserCheckable; |
| 34 | } |
| 35 | else |
| 36 | { |
| 37 | _LOG_DEBUG("index: row: " << index.row() << " " << index.column() << " no-check!"); |
| 38 | return QSqlTableModel::flags(index); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | bool |
| 43 | EndorseSqlTableModel::setData ( const QModelIndex & index, const QVariant & value, int role) |
| 44 | { |
| 45 | bool success = true; |
| 46 | |
| 47 | if (index.column() == 3) |
| 48 | { |
| 49 | _LOG_DEBUG("setData: " << role); |
| 50 | if(role == Qt::CheckStateRole) |
| 51 | { |
| 52 | QString val = (value==Qt::Checked)?"1":"0"; |
| 53 | return QSqlTableModel::setData(index, val, role); |
| 54 | } |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | return QSqlTableModel::setData(index, value, role); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | QVariant |
| 65 | EndorseSqlTableModel::data ( const QModelIndex & index, int role) const |
| 66 | { |
| 67 | QVariant value = QSqlTableModel::data(index, role); |
| 68 | // if (index.column() == 3 && role == Qt::CheckStateRole) |
| 69 | if (index.column() == 3) |
| 70 | { |
| 71 | if(role == Qt::CheckStateRole) |
| 72 | { |
| 73 | bool aBool = ((value.toInt() != 0) ? 1 : 0); |
| 74 | if (aBool) |
| 75 | return Qt::Checked; |
| 76 | else |
| 77 | return Qt::Unchecked; |
| 78 | } |
| 79 | if(role == Qt::DisplayRole) |
| 80 | return QVariant(); |
| 81 | |
| 82 | return value; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | return value; |
| 87 | } |
| 88 | } |