TransferModel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "Qt.h"
00024 #include "TransferModel.h"
00025
00026 using UIs::Transfer;
00027 using UIs::TransferModel;
00028
00029 TransferModel::TransferModel (QObject *parent)
00030 : QAbstractTableModel (parent), transferList(), cCount (0)
00031 {
00032 cCount = 7;
00033 }
00034
00035 void TransferModel::addItem(Transfer *transfer, QTreeView *)
00036 {
00037 int count = transferList.count();
00038
00039 beginInsertRows (QModelIndex(), count, count);
00040
00041 transferList.append (*transfer);
00042 for (int i=0; i<transferList.last().childList.count(); i++) {
00043 transferList.last().childList.at(i).parent = &transferList.last();
00044 }
00045
00046 endInsertRows();
00047
00048 return;
00049 }
00050
00051 int TransferModel::rowCount (const QModelIndex &index) const
00052 {
00053 static Transfer *transfer;
00054
00055 if (!index.isValid()) {
00056 return transferList.count();
00057 }
00058
00059 transfer = static_cast <Transfer*> (index.internalPointer());
00060 return transfer->childList.count();
00061 }
00062
00063 int TransferModel::columnCount (const QModelIndex &) const
00064 {
00065 return cCount;
00066 }
00067
00068 QVariant TransferModel::data (const QModelIndex &index, int role) const
00069 {
00070 static QIcon icon ("Icons/glmdiggp.png");
00071 static Transfer* transfer;
00072
00073 transfer = static_cast <Transfer*> (index.internalPointer());
00074 Q_ASSERT (transfer);
00075
00076
00077
00078
00079
00080
00081
00082
00083 if (!index.isValid())
00084 return QVariant();
00085
00086
00087
00088
00089 if (role == Qt::DisplayRole)
00090 switch (index.column()) {
00091 case 0:
00092 qDebug() << transfer->fileName << " data";
00093 return transfer->fileName;
00094 case 1:
00095 return QString ("%1 MB").arg (transfer->size);
00096 case 2:
00097 return new QProgressDialog();
00098 case 3:
00099 return transfer->speed;
00100 case 4:
00101 return transfer->status;
00102 case 5:
00103 return QString ("%1%").arg (transfer->completed);
00104 case 6:
00105 return transfer->client;
00106 default:
00107 return QString();
00108 }
00109
00110 if (role == Qt::DecorationRole && index.column() == 0 && transfer->parent)
00111 return icon;
00112
00113 if (role == Qt::ToolTipRole){
00114
00115
00116
00117 return QString("test TollTip %1 %2").arg(index.row()).arg(index.column());
00118 }
00119
00120 return QVariant();
00121
00122 }
00123
00124 bool TransferModel::hasChildren (const QModelIndex &index) const
00125 {
00126 static Transfer *transfer;
00127
00128 if(index.isValid()) {
00129 transfer = static_cast <Transfer*> (index.internalPointer());
00130 return transfer->childList.count();
00131 }
00132
00133 return transferList.count();
00134 }
00135 QModelIndex TransferModel::index(int row, int column, const QModelIndex &parent) const
00136 {
00137 static Transfer *transfer;
00138 static Transfer* child;
00139
00140 if (row > transferList.count())
00141 return QModelIndex();
00142
00143 if (!parent.isValid()) {
00144 transfer = const_cast <Transfer *> (&transferList.at (row));
00145 return createIndex (row, column, transfer);
00146 } else {
00147 transfer = static_cast <Transfer *> (parent.internalPointer());
00148 Q_ASSERT(transfer);
00149 if (row < transfer->childList.count()) {
00150
00151
00152 child = const_cast <Transfer*> (&transfer->childList.at (row));
00153
00154
00155
00156
00157
00158 return createIndex(row, column, child);
00159 }
00160 }
00161
00162 return QModelIndex();
00163 }
00164
00165 QModelIndex TransferModel::parent ( const QModelIndex & index ) const
00166 {
00167 static Transfer* child;
00168
00169 if (index.isValid()) {
00170 child = static_cast<Transfer*> (index.internalPointer());
00171
00172 if (child->parent) {
00173 return createIndex (0, 0, child->parent);
00174 }
00175 }
00176
00177 return QModelIndex();
00178 }
00179
00180 QVariant TransferModel::headerData (int section, Qt::Orientation orientation, int role) const
00181 {
00182 if (role != Qt::DisplayRole)
00183 return QVariant();
00184
00185 if (orientation == Qt::Horizontal)
00186 switch (section) {
00187 case 0:
00188 return QString ("File Name");
00189 case 1:
00190 return QString ("Size");
00191 case 2:
00192 return QString ("Progress");
00193 case 3:
00194 return QString ("Speed");
00195 case 4:
00196 return QString ("Status");
00197 case 5:
00198 return QString ("Completed");
00199 case 6:
00200 return QString ("Client");
00201 default:
00202 return QString ("Column %1").arg (section);
00203 }
00204
00205 return QString ();
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225