SearchModel.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 "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 /*  SearchResult res("file1", "txt", 1343, 234, "123.34", "LimeWire");
00094     SearchResult res1("file2", "jpg", 1343, 234, "123.34", "LimeWire");
00095     SearchResult res2("file3", "zip", 1343, 234, "123.34", "LimeWire");
00096     SearchResult res3("file4", "avi", 1343, 234, "123.34", "LimeWire");
00097     SearchResult res4("file5", "mp3", 1343, 234, "123.34", "LimeWire");
00098     SearchResult res5("file6", "abc", 1343, 234, "123.34", "LimeWire");
00099 
00100     addItem (&res);
00101     addItem (&res1);
00102     addItem (&res2);
00103     addItem (&res3);
00104     addItem (&res4);
00105     addItem (&res5);
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 /*  for (int i=0; i<resultList.at(index).hosts.count(); i++) {
00123         resultList.at(index).hosts.at(i).parent = (SearchResult*) &resultList.at(index);
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 //      case rating:
00169 //          return result->rating;
00170 //      case status:
00171 //          return result->status;
00172         case hosts:
00173             return result->host;
00174         case speed:
00175             return result->speed;
00176         case client:
00177             return result->client;
00178 //      case duration:
00179 //          return result->duration;
00180 //      case bitrate:
00181 //          return result->bitrate;
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     //static SearchResult* host;
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 /*      result = static_cast <SearchResult*> (parent.internalPointer());
00223         Q_ASSERT(result);
00224         if (row < result->hosts.count()) {
00225             host = (SearchResult*) &result->hosts.at (row);
00226             return createIndex (row, column, (void*) host);
00227         }*/
00228     }
00229 
00230     return QModelIndex();
00231 }
00232 /*
00233 QModelIndex SearchModel::parent ( const QModelIndex & index ) const
00234 {
00235     static SearchResult * host;
00236 
00237     if (index.isValid()) {
00238         host = static_cast<SearchResult*> (index.internalPointer());
00239 //          qDebug() << child->fileName << "parent child.fileName";
00240         if (host->parent) {
00241             return createIndex (0, 0, host->parent);
00242         }
00243     }
00244 
00245     return QModelIndex();
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 //      case rating:
00263 //          return QString ("Rating");
00264 //      case staus:
00265 //          return QString ("Status");
00266         case hosts:
00267             return QString ("Host(s)");
00268         case speed:
00269             return QString ("Speed");
00270         case client:
00271             return QString ("Client");
00272 //      case duration:
00273 //          return QString ("Duration");
00274 //      case bitrate:
00275 //          return QString ("Bitrate");
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