00001 #include <QDebug>
00002
00003 #include "accountstablemodel.h"
00004 #include "../Data/datadefinitions.h"
00005
00006 AccountsTableModel::AccountsTableModel(QObject *parent) : QAbstractTableModel(parent)
00007 {
00008 }
00009
00010 int AccountsTableModel::rowCount(const QModelIndex &parent) const
00011 {
00012 Q_UNUSED(parent);
00013 return entries.size();
00014 }
00015
00016 int AccountsTableModel::columnCount(const QModelIndex &parent) const
00017 {
00018 Q_UNUSED(parent);
00019 return 3;
00020 }
00021
00022 QVariant AccountsTableModel::data(const QModelIndex &index, int role) const
00023 {
00024 if(!index.isValid())
00025 {
00026 return QVariant();
00027 }
00028
00029 if(index.row() >= entries.size() || index.row() < 0)
00030 {
00031 return QVariant();
00032 }
00033 if(role == Qt::DisplayRole)
00034 {
00035 QStringList pair = entries.value(index.row());
00036 if(index.column() >= 0 && index.column() <= 8)
00037 {
00038 return pair.value(index.column());
00039 }
00040 }
00041 return QVariant();
00042 }
00043
00044 QStringList AccountsTableModel::data(const int row) const
00045 {
00046 return entries.value(row);
00047 }
00048
00049 QVariant AccountsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
00050 {
00051 if(role != Qt::DisplayRole)
00052 {
00053 return QVariant();
00054 }
00055
00056 if(orientation == Qt::Horizontal)
00057 {
00058 AccountsTableHeaders *columns = new AccountsTableHeaders;
00059
00060 switch (section)
00061 {
00062 case AccountsTableHeaders::AccountNumber:
00063 return columns->header(AccountsTableHeaders::AccountNumber);
00064 case AccountsTableHeaders::Name:
00065 return columns->header(AccountsTableHeaders::Name);
00066 case AccountsTableHeaders::Description:
00067 return columns->header(AccountsTableHeaders::Description);
00068 default:
00069 return QVariant();
00070 }
00071 }
00072 return QVariant();
00073 }
00074
00075 bool AccountsTableModel::insertRows(int position, int rows, const QModelIndex &index)
00076 {
00077 for (int row = 0; row < rows; row++)
00078 {
00079 beginInsertRows(index, position, position + row);
00080
00081 QStringList pair;
00082 for(int i = 0; i < columnCount(index); i++)
00083 {
00084 pair << "";
00085 }
00086 entries.insert(position, pair);
00087
00088 endInsertRows();
00089 }
00090 return true;
00091 }
00092
00093 bool AccountsTableModel::removeRows(int position, int rows, const QModelIndex &index)
00094 {
00095 for (int row = 0; row < rows; ++row)
00096 {
00097 beginRemoveRows(index, position, position + row);
00098
00099 entries.removeAt(position + row);
00100
00101 endRemoveRows();
00102 }
00103 return true;
00104 }
00105
00106 bool AccountsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
00107 {
00108 if(index.isValid() && role == Qt::EditRole)
00109 {
00110 int row = index.row();
00111 QStringList p = entries.value(row);
00112
00113 if(index.column() >= 0 && index.column() <= 8)
00114 {
00115 p.replace(index.column(), value.toString());
00116 }
00117 else
00118 {
00119 return false;
00120 }
00121
00122 entries.replace(row, p);
00123
00124 emit(dataChanged(index, index));
00125 return true;
00126 }
00127 return false;
00128 }
00129
00130 Qt::ItemFlags AccountsTableModel::flags(const QModelIndex &index) const
00131 {
00132 if(!index.isValid())
00133 {
00134 return Qt::ItemIsSelectable;
00135 }
00136 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
00137 }
00138
00139 bool AccountsTableModel::addEntry(QString number, QString name, QString description)
00140 {
00141 if(!existAccountByNumber(number))
00142 {
00143 QModelIndex i;
00144 if(!insertRows(rowCount(QModelIndex()), 1, QModelIndex()))
00145 {
00146 return false;
00147 }
00148 int row = rowCount(QModelIndex()) -1;
00149
00150 i = index(row, AccountsTableHeaders::AccountNumber, QModelIndex());
00151 setData(i, number, Qt::EditRole);
00152
00153 i = index(i.row(), AccountsTableHeaders::Name, QModelIndex());
00154 setData(i, name, Qt::EditRole);
00155
00156 i = index(i.row(), AccountsTableHeaders::Description, QModelIndex());
00157 setData(i, description, Qt::EditRole);
00158
00159 return true;
00160 }
00161 else
00162 {
00163 return false;
00164 }
00165 }
00166
00167 bool AccountsTableModel::existAccountByNumber(QString number)
00168 {
00169 for(int i = 0; i < entries.size(); i++)
00170 {
00171 if(entries.value(i).value(0) == number)
00172 {
00173 return true;
00174 }
00175 }
00176 return false;
00177 }
00178
00179 QDomElement AccountsTableModel::dataNode()
00180 {
00181 QDomDocument doc;
00182 QDomElement accounts = doc.createElement("tableOfAccounts");
00183
00184 for(int i = 0; i < entries.count(); i++)
00185 {
00186 QDomElement account = doc.createElement("account");
00187 account.setAttribute("number", entries.value(i).value(0));
00188 account.setAttribute("name", entries.value(i).value(1));
00189 account.setAttribute("description", entries.value(i).value(2));
00190 accounts.appendChild(account);
00191 }
00192 return accounts;
00193 }