SearchModel.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 "SearchModel.h"
00025 #include "Gnutella/Packets/QueryHits.h"
00026
00027 using UIs::Searching::SearchModel;
00028 using UIs::Searching::SearchResult;
00029 using Gnutella::Packets::QueryHits;
00030
00031 SearchModel::IconProvider::IconProvider()
00032 : iconList(), videoFileExtensions(), audioFileExtensions(),
00033 compressedFileExtensions(), photoFileExtensions(), textFileExtensions()
00034 {
00035 videoFileExtensions.append ("avi");
00036 videoFileExtensions.append ("mpeg");
00037 videoFileExtensions.append ("mpg");
00038 videoFileExtensions.append ("wmv");
00039 videoFileExtensions.append ("mov");
00040 audioFileExtensions.append ("mp3");
00041 audioFileExtensions.append ("mp4");
00042 audioFileExtensions.append ("wave");
00043 audioFileExtensions.append ("midi");
00044 audioFileExtensions.append ("wma");
00045 compressedFileExtensions.append ("zip");
00046 compressedFileExtensions.append ("rar");
00047 compressedFileExtensions.append ("gz");
00048 compressedFileExtensions.append ("tar.gz");
00049 photoFileExtensions.append ("jpeg");
00050 photoFileExtensions.append ("jpg");
00051 photoFileExtensions.append ("png");
00052 photoFileExtensions.append ("bmp");
00053
00054 textFileExtensions.append ("txt");
00055 textFileExtensions.append ("rtf");
00056 textFileExtensions.append ("doc");
00057 textFileExtensions.append ("pdf");
00058
00059 iconList.append (QIcon ("Icons/video.png"));
00060 iconList.append (QIcon ("Icons/audio.png"));
00061 iconList.append (QIcon ("Icons/compressed.png"));
00062 iconList.append (QIcon ("Icons/photo.png"));
00063 iconList.append (QIcon ("Icons/text.png"));
00064 iconList.append (QIcon ("Icons/unknown.png"));
00065 }
00066
00067 QIcon SearchModel::IconProvider::icon (QString fileExtension) const
00068 {
00069 FileTypes fileType = unknown;
00070
00071 if (videoFileExtensions.contains (fileExtension)) {
00072 fileType = video;
00073 } else if (audioFileExtensions.contains (fileExtension)) {
00074 fileType = audio;
00075 } else if (compressedFileExtensions.contains (fileExtension)) {
00076 fileType = compressed;
00077 } else if (photoFileExtensions.contains (fileExtension)) {
00078 fileType = photo;
00079 } else if (textFileExtensions.contains (fileExtension)) {
00080 fileType = text;
00081 } else {
00082 fileType = unknown;
00083 }
00084
00085 return iconList.at (fileType);
00086
00087 }
00088
00089 SearchModel::SearchModel (QObject *parent)
00090 : QAbstractTableModel (parent), resultList(), iconProvider(),
00091 cCount (6), tabActive (true)
00092 {
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 }
00107
00108 void SearchModel::addHits(const QueryHits &)
00109 {
00110 }
00111
00112 void SearchModel::addItem(SearchResult *result)
00113 {
00114 int count = resultList.count();
00115 beginInsertRows (QModelIndex(), count, count);
00116
00117 result->showEmphasized = !tabActive;
00118
00119 int index = findIndex (result);
00120
00121 resultList.insert (index, *result);
00122
00123
00124
00125
00126 endInsertRows();
00127
00128 emit (itemAdded());
00129
00130 return;
00131 }
00132
00133 int SearchModel::rowCount (const QModelIndex &index) const
00134 {
00135 if (!index.isValid()) {
00136 return resultList.count();
00137 }
00138
00139 SearchResult *result = static_cast<SearchResult*> (index.internalPointer());
00140
00141 return result->hosts.count();
00142 }
00143
00144 int SearchModel::columnCount (const QModelIndex &) const
00145 {
00146 return cCount;
00147 }
00148
00149 QVariant SearchModel::data (const QModelIndex &index, int role) const
00150 {
00151 static QIcon icon ("glmdiggp.png");
00152
00153 if (!index.isValid())
00154 return QVariant();
00155
00156 SearchResult* result = static_cast <SearchResult*> (index.internalPointer());
00157 Q_ASSERT (result);
00158
00159 if (role == Qt::DisplayRole) {
00160 SearchModelField field = static_cast <SearchModelField> (index.column());
00161 switch (field) {
00162 case fileName:
00163 return result->fileName;
00164 case extension:
00165 return result->extension;
00166 case size:
00167 return result->size;
00168
00169
00170
00171
00172 case hosts:
00173 return result->host;
00174 case speed:
00175 return result->speed;
00176 case client:
00177 return result->client;
00178
00179
00180
00181
00182 default:
00183 return QVariant();
00184 }
00185 }
00186
00187 if (role == Qt::DecorationRole && index.column() == 0)
00188 return iconProvider.icon (result->extension.toLower());
00189
00190 if (role == Qt::TextColorRole)
00191 if (result->showEmphasized)
00192 return QColor (0, 0, 128);
00193
00194 return QVariant();
00195
00196 }
00197
00198 bool SearchModel::hasChildren (const QModelIndex &index) const
00199 {
00200 static SearchResult *result;
00201
00202 if(index.isValid()) {
00203 result = static_cast <SearchResult*> (index.internalPointer());
00204 return (result->hosts.count() > 0);
00205 }
00206
00207 return (resultList.count() > 0);
00208 }
00209 QModelIndex SearchModel::index(int row, int column, const QModelIndex &parent) const
00210 {
00211 static SearchResult *result;
00212
00213
00214 if (!parent.isValid()) {
00215 if (row >= resultList.count())
00216 return QModelIndex();
00217
00218 result = const_cast <SearchResult *> (&resultList.at (row));
00219 return createIndex (row, column, result);
00220 } else {
00221 Q_ASSERT (false);
00222
00223
00224
00225
00226
00227
00228 }
00229
00230 return QModelIndex();
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 QVariant SearchModel::headerData (int section, Qt::Orientation orientation, int role) const
00249 {
00250 if (role != Qt::DisplayRole)
00251 return QVariant();
00252
00253 if (orientation == Qt::Horizontal) {
00254 SearchModelField field = static_cast <SearchModelField> (section);
00255 switch (field) {
00256 case fileName:
00257 return QString ("File");
00258 case extension:
00259 return QString ("Extension");
00260 case size:
00261 return QString ("Size");
00262
00263
00264
00265
00266 case hosts:
00267 return QString ("Host(s)");
00268 case speed:
00269 return QString ("Speed");
00270 case client:
00271 return QString ("Client");
00272
00273
00274
00275
00276 default:
00277 return QString ("Column %1").arg (section);
00278 }
00279 }
00280
00281 return QString ("");
00282
00283 }
00284
00285 void SearchModel::tabActivated()
00286 {
00287 tabActive = true;
00288 }
00289
00290 void SearchModel::tabDisactivated()
00291 {
00292 tabActive = false;
00293 if (resultList.isEmpty()) return;
00294
00295 QList<SearchResult>::iterator it = resultList.begin();
00296 do {
00297 it->showEmphasized = false;
00298 it++;
00299 } while (it != resultList.end());
00300
00301 dataChanged (index (0, 0, QModelIndex()), index (resultList.count()-1, resultList.count()-1, QModelIndex()));
00302 }
00303
00304 int SearchModel::findIndex (SearchResult *result)
00305 {
00306 int i = 1;
00307 QString fileName = result->fileName;
00308
00309 if (resultList.isEmpty ()) return 0;
00310
00311 SearchResult res = resultList.first();
00312
00313 while ((fileName > res.fileName) && (i < resultList.count())) {
00314 res = resultList.at (i++);
00315 }
00316
00317 return i-1;
00318 }
00319