~ubuntu-branches/ubuntu/lucid/ktorrent/lucid

« back to all changes in this revision

Viewing changes to plugins/infowidget/chunkdownloadmodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2009-02-16 18:37:14 UTC
  • mfrom: (1.1.25 upstream) (0.4.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090216183714-52tf47jrnmk4xkmp
Tags: 3.2+dfsg.1-2ubuntu1
* Merge with Debian, remaining changes: (LP: #296433)
  - Use Kubuntu's kde4.mk
  - Build-depend on libboost-serialization1.35-dev since unversioned -dev is
    in universe
  - Change plasma-applet-ktorrent to plasma-widget-ktorrent since we're
    supposed to call them widgets for the users

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
                cd->getStats(stats);
37
37
        }
38
38
                        
39
 
        bool ChunkDownloadModel::Item::changed() const
 
39
        bool ChunkDownloadModel::Item::changed(int col,bool & modified) const
40
40
        {
41
41
                ChunkDownloadInterface::Stats s;
42
42
                cd->getStats(s);
 
43
                bool ret = false;
 
44
                switch (col)
 
45
                {
 
46
                        case 1: ret = s.pieces_downloaded != stats.pieces_downloaded; break;
 
47
                        case 2: ret = s.current_peer_id != stats.current_peer_id; break;
 
48
                        case 3: ret = s.download_speed != stats.download_speed; break;
 
49
                        default: break;
 
50
                }
43
51
 
44
 
                if (s.pieces_downloaded != stats.pieces_downloaded || 
 
52
                modified = s.pieces_downloaded != stats.pieces_downloaded || 
45
53
                        s.download_speed != stats.download_speed || 
46
 
                        s.num_downloaders != stats.num_downloaders || 
47
 
                        s.current_peer_id != stats.current_peer_id)
48
 
                {
49
 
                        stats = s;
50
 
                        return true;
51
 
                }
52
 
                return false;
 
54
                        s.current_peer_id != stats.current_peer_id;
 
55
                
 
56
                stats = s;
 
57
                return ret;
53
58
        }
54
59
        
55
60
        QVariant ChunkDownloadModel::Item::data(int col) const
59
64
                        case 0: return stats.chunk_index;
60
65
                        case 1: return QString("%1 / %2").arg(stats.pieces_downloaded).arg(stats.total_pieces);
61
66
                        case 2: return stats.current_peer_id;
62
 
                        case 3: return KBytesPerSecToString(stats.download_speed / 1024.0);
63
 
                        case 4: return stats.num_downloaders;
64
 
                        case 5: return files;
 
67
                        case 3: return BytesPerSecToString(stats.download_speed);
 
68
                        case 4: return files;
65
69
                }
66
70
                return QVariant();
67
71
        }
68
72
        
69
 
        QVariant ChunkDownloadModel::Item::dataForSorting(int col) const
 
73
        bool ChunkDownloadModel::Item::lessThan(int col,const Item* other) const
70
74
        {
71
75
                switch (col)
72
76
                {
73
 
                        case 0: return stats.chunk_index;
74
 
                        case 1: return stats.pieces_downloaded;
75
 
                        case 2: return stats.current_peer_id;
76
 
                        case 3: return stats.download_speed;
77
 
                        case 4: return stats.num_downloaders;
78
 
                        case 5: return files;
 
77
                        case 0: return stats.chunk_index < other->stats.chunk_index;
 
78
                        case 1: return stats.pieces_downloaded < other->stats.pieces_downloaded;
 
79
                        case 2: return stats.current_peer_id < other->stats.current_peer_id;
 
80
                        case 3: return stats.download_speed < other->stats.download_speed;
 
81
                        case 4: return files < other->files;
79
82
                }
80
 
                return QVariant();
 
83
                return false;
81
84
        }
82
85
        
83
86
        /////////////////////////////////////////////////////////////
85
88
        ChunkDownloadModel::ChunkDownloadModel ( QObject* parent )
86
89
                        : QAbstractTableModel(parent),tc(0)
87
90
        {
 
91
                sort_column = 0;
 
92
                sort_order = Qt::AscendingOrder;
88
93
        }
89
94
 
90
95
 
91
96
        ChunkDownloadModel::~ChunkDownloadModel()
92
97
        {
 
98
                qDeleteAll(items);
93
99
        }
94
100
        
95
101
        void ChunkDownloadModel::downloadAdded(bt::ChunkDownloadInterface* cd)
114
120
                                        files += tf.getPath();
115
121
                                        n++;
116
122
                                }
 
123
                                else if (stats.chunk_index < tf.getFirstChunk())
 
124
                                        break;
117
125
                        }
118
126
                }
119
127
                
120
 
                items.append(Item(cd,files));
121
 
                insertRow(items.count() - 1);
 
128
                Item* nitem = new Item(cd,files);
 
129
                items.append(nitem);
 
130
                insertRow(items.count() - 1);   
 
131
                sort(sort_column,sort_order);
122
132
        }
123
133
        
124
134
        void ChunkDownloadModel::downloadRemoved(bt::ChunkDownloadInterface* cd)
125
135
        {
126
136
                int idx = 0;
127
 
                for (QList<Item>::iterator i = items.begin();i != items.end();i++)
 
137
                for (QList<Item*>::iterator i = items.begin();i != items.end();i++)
128
138
                {
129
 
                        const Item & item = *i;
130
 
                        if (item.cd == cd)
 
139
                        const Item* item = *i;
 
140
                        if (item->cd == cd)
131
141
                        {
132
142
                                items.erase(i);
 
143
                                delete item;
133
144
                                removeRow(idx);
134
145
                                break;
135
146
                        }
139
150
        
140
151
        void ChunkDownloadModel::changeTC(bt::TorrentInterface* tc)
141
152
        {
 
153
                qDeleteAll(items);
142
154
                items.clear();
143
155
                this->tc = tc;
144
156
                reset();
146
158
        
147
159
        void ChunkDownloadModel::clear()
148
160
        {
 
161
                qDeleteAll(items);
149
162
                items.clear();
150
163
                reset();
151
164
        }
152
165
        
153
 
        bool ChunkDownloadModel::update()
 
166
        void ChunkDownloadModel::update()
154
167
        {
155
 
                bool ret = false;
 
168
                bool resort = false;
156
169
                Uint32 idx=0;
157
 
                foreach (const Item & i,items)
 
170
                foreach (Item* i,items)
158
171
                {
159
 
                        if (i.changed())
160
 
                                ret = true;
 
172
                        bool modified = false;
 
173
                        if (i->changed(sort_column,modified))
 
174
                                resort = true;
 
175
                        
 
176
                        if (modified && !resort)
 
177
                                emit dataChanged(index(idx,1),index(idx,3));
161
178
                        idx++;
162
179
                }
163
 
                return ret;
 
180
        
 
181
                if (resort)
 
182
                        sort(sort_column,sort_order);
164
183
        }
165
184
 
166
 
        int ChunkDownloadModel::rowCount ( const QModelIndex & parent ) const
 
185
        int ChunkDownloadModel::rowCount(const QModelIndex & parent) const
167
186
        {
168
187
                if (parent.isValid())
169
188
                        return 0;
171
190
                        return items.count();
172
191
        }
173
192
        
174
 
        int ChunkDownloadModel::columnCount ( const QModelIndex & parent ) const
 
193
        int ChunkDownloadModel::columnCount(const QModelIndex & parent) const
175
194
        {
176
195
                if (parent.isValid())
177
196
                        return 0;
178
197
                else
179
 
                        return 6;
 
198
                        return 5;
180
199
        }
181
200
        
182
 
        QVariant ChunkDownloadModel::headerData ( int section, Qt::Orientation orientation,int role ) const
 
201
        QVariant ChunkDownloadModel::headerData(int section,Qt::Orientation orientation,int role) const
183
202
        {
184
 
                if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
 
203
                if (orientation != Qt::Horizontal)
185
204
                        return QVariant();
186
205
                 
187
 
                switch (section)
188
 
                {
189
 
                        case 0: return i18n("Chunk");
190
 
                        case 1: return i18n("Progress");
191
 
                        case 2: return i18n("Peer");
192
 
                        case 3: return i18n("Down Speed");
193
 
                        case 4: return i18n("Assigned Peers");
194
 
                        case 5: return i18n("Files");
195
 
                        default: return QVariant();
196
 
                }
197
 
        }
198
 
        
199
 
        QVariant ChunkDownloadModel::data ( const QModelIndex & index,int role ) const
 
206
                if (role == Qt::DisplayRole)
 
207
                {
 
208
                        switch (section)
 
209
                        {
 
210
                                case 0: return i18n("Chunk");
 
211
                                case 1: return i18n("Progress");
 
212
                                case 2: return i18n("Peer");
 
213
                                case 3: return i18n("Down Speed");
 
214
                                case 4: return i18n("Files");
 
215
                                default: return QVariant();
 
216
                        }
 
217
                }
 
218
                else if (role == Qt::ToolTipRole)
 
219
                {
 
220
                        switch (section)
 
221
                        {
 
222
                                case 0: return i18n("Number of the chunk");
 
223
                                case 1: return i18n("Download progress of the chunk");
 
224
                                case 2: return i18n("Which peer we are downloading it from");
 
225
                                case 3: return i18n("Download speed of the chunk");
 
226
                                case 4: return i18n("Which files the chunk is located in");
 
227
                                default: return QVariant();
 
228
                        }
 
229
                }
 
230
                
 
231
                return QVariant();
 
232
        }
 
233
        
 
234
        QModelIndex ChunkDownloadModel::index(int row,int column,const QModelIndex & parent) const
 
235
        {
 
236
                if (!hasIndex(row,column,parent) || parent.isValid())
 
237
                        return QModelIndex();
 
238
                else
 
239
                        return createIndex(row,column,items[row]);
 
240
        }
 
241
        
 
242
        QVariant ChunkDownloadModel::data(const QModelIndex & index,int role) const
200
243
        {
201
244
                if (!index.isValid() || index.row() >= items.count() || index.row() < 0)
202
245
                        return QVariant(); 
203
246
                
204
247
                if (role == Qt::DisplayRole)
205
 
                        return items[index.row()].data(index.column());
206
 
                else if (role == Qt::UserRole) // UserRole is for sorting
207
 
                        return items[index.row()].dataForSorting(index.column());
208
 
                
 
248
                        return items[index.row()]->data(index.column());
 
249
                                
209
250
                return QVariant();
210
251
        }
211
252
        
212
 
        bool ChunkDownloadModel::removeRows ( int row,int count,const QModelIndex & /*parent*/ )
 
253
        bool ChunkDownloadModel::removeRows(int row,int count,const QModelIndex & /*parent*/ )
213
254
        {
214
255
                beginRemoveRows(QModelIndex(),row,row + count - 1);
215
256
                endRemoveRows();
216
257
                return true;
217
258
        }
218
259
        
219
 
        bool ChunkDownloadModel::insertRows ( int row,int count,const QModelIndex & /*parent*/ )
 
260
        bool ChunkDownloadModel::insertRows(int row,int count,const QModelIndex & /*parent*/ )
220
261
        {
221
262
                beginInsertRows(QModelIndex(),row,row + count - 1);
222
263
                endInsertRows();
223
264
                return true;
224
265
        }
 
266
        
 
267
        class ChunkDownloadModelItemCmp
 
268
        {
 
269
        public:
 
270
                ChunkDownloadModelItemCmp(int col,Qt::SortOrder order) : col(col),order(order)
 
271
                {}
 
272
        
 
273
                bool operator()(ChunkDownloadModel::Item* a,ChunkDownloadModel::Item* b)
 
274
                {
 
275
                        if (order == Qt::AscendingOrder)
 
276
                                return a->lessThan(col,b);
 
277
                        else
 
278
                                return !a->lessThan(col,b);
 
279
                }
 
280
        
 
281
                int col;
 
282
                Qt::SortOrder order;
 
283
        };
225
284
 
 
285
        void ChunkDownloadModel::sort(int col, Qt::SortOrder order)
 
286
        {
 
287
                sort_column = col;
 
288
                sort_order = order;
 
289
                emit layoutAboutToBeChanged();
 
290
                qStableSort(items.begin(),items.end(),ChunkDownloadModelItemCmp(col,order));
 
291
                emit layoutChanged();
 
292
        }
226
293
}