TransferModel.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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     //pixmap.load ("glmdiggp.png");
00077     //pixmap = pixmap.scaled (50, 15);
00078 //  pixmap = pixmap.scaledToWidth(50);
00079 
00080     //pBar.setRange(1, 100);
00081     //pBar.setValue(5 + 10*index.row());
00082 
00083     if (!index.isValid())
00084         return QVariant();
00085 
00086 //    if (index.row() < 0 || index.row() >= transferList.size())
00087 //        return QVariant();
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();   //transfer->progress;
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 //      QDialog widget;
00115 //      widget.show();
00116 //      widget.exec();
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 //          qDebug() << transfer->fileName << " -> index tree name";
00151 //          qDebug() << transfer->port << " -> index tree port";
00152             child = const_cast <Transfer*> (&transfer->childList.at (row));
00153 //          ptr = &child;
00154 //          qDebug() << child.name << " -> index tree name";
00155 //          qDebug() << child.port << " -> index tree port";
00156 //          qDebug() << child.childList.count() << " -> index tree children";
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 //          qDebug() << child->fileName << "parent child.fileName";
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 /*QItemSelectionModel::SelectionFlags TransferModel::selectionCommand (const QModelIndex *index, QEvent *e)
00209 {
00210     if (e->type() == QEvent::ToolTip) {
00211         int i;
00212         i=2;
00213     }
00214 
00215     switch (e->type()){
00216         case QEvent::ToolTip:
00217             QHelpEvent *hE = static_cast<QHelpEvent *> (e);
00218             QPoint pos = hE->pos();
00219             QToolTip::showText (hE->globalPos(), "proba");
00220             return true; // \todo
00221     };
00222 
00223     return QAbstractTableModel::event (e);
00224 }
00225 */