streamdirectorymodel.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2008-2010  Lukas Sommer < SommerLuk at gmail dot com >
00003 
00004     This program is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU General Public License as
00006     published by the Free Software Foundation; either version 2 of
00007     the License or (at your option) version 3 or any later version
00008     accepted by the membership of KDE e.V. (or its successor approved
00009     by the membership of KDE e.V.), which shall act as a proxy
00010     defined in Section 14 of version 3 of the license.
00011 
00012     This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "streamdirectorymodel.h"
00022 
00023 #include <QMimeData>
00024 #include <QUrl>
00025 #include <KLocale>
00026 #include "streamdirectoryentry.h"
00027 
00028 streamDirectoryModel::streamDirectoryModel(QObject *parent, const QString & genreIconFileName)
00029   : QAbstractItemModel(parent)
00030 {
00031   // internals
00032   rootEntry = new streamDirectoryEntry_root();  // very important!
00033   genreIcon = KIcon(genreIconFileName);
00034   setSupportedDragActions(Qt::CopyAction);
00035 };
00036 
00037 streamDirectoryModel::~streamDirectoryModel()
00038 {
00039   delete rootEntry;
00040 }
00041 
00042 QModelIndex streamDirectoryModel::parent(const QModelIndex & index) const
00043 {
00044   if (!index.isValid()) {
00045     return QModelIndex();
00046   };
00047 
00048   streamDirectoryEntry *childItem = static_cast<streamDirectoryEntry *>(index.internalPointer());
00049   streamDirectoryEntry *parentItem = childItem->parent();
00050   if (parentItem == rootEntry) {
00051     return QModelIndex();
00052   } else {
00053     return createIndex(parentItem->row(), 0, parentItem);
00054   };
00055 }
00056 
00057 QModelIndex streamDirectoryModel::index(int row, int column, const QModelIndex &parent) const
00058 {
00059   // variables
00060   streamDirectoryEntry *parentItem;
00061   streamDirectoryEntry *childItem;
00062 
00063   // code
00064   if (!hasIndex(row, column, parent)) {
00065     return QModelIndex();
00066   };
00067 
00068   if (!parent.isValid()) {
00069     parentItem = rootEntry;
00070   } else {
00071     parentItem = static_cast<streamDirectoryEntry *>(parent.internalPointer());
00072   };
00073   childItem = parentItem->child(row);
00074   if (childItem) {
00075     return createIndex(row, column, childItem);
00076   } else {
00077     return QModelIndex();
00078   };
00079 }
00080 
00081 int streamDirectoryModel::rowCount(const QModelIndex &parent) const
00082 {
00083   // variables
00084   streamDirectoryEntry *parentItem;
00085 
00086   // code
00087   if (parent.column() > 0) {
00088     return 0;
00089   };
00090 
00091   if (!parent.isValid()) {
00092     parentItem = rootEntry;
00093   } else {
00094     parentItem = static_cast<streamDirectoryEntry *>(parent.internalPointer());
00095   };
00096   return parentItem->childCount();
00097 }
00098 
00099 int streamDirectoryModel::columnCount(const QModelIndex &) const
00100 {
00101   return 5;
00102 }
00103 
00104 QVariant streamDirectoryModel::data(const QModelIndex &index, int role) const
00105 {
00106   if (!index.isValid()) {
00107     return QVariant();
00108   };
00109 
00110   if (role != Qt::DisplayRole && role != Qt::UserRole) {
00111     return QVariant();
00112   };
00113 
00114   streamDirectoryEntry *item = static_cast<streamDirectoryEntry *>(index.internalPointer());
00115   if (role == Qt::DisplayRole) {
00116     return item->data(index.column());
00117   } else {
00118     return item->rawData(index.column());
00119   };
00120 }
00121 
00122 QVariant streamDirectoryModel::headerData(int section,
00123                                           Qt::Orientation orientation,
00124                                           int role) const
00125 {
00126   if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
00127     switch (section) {
00128       case 0: return i18nc("@title:column", "stream name");
00129       case 1: return i18nc("@title:column", "data format");
00130       case 2: return i18nc("@title:column", "bit rate");
00131       case 3: return i18nc("@title:column", "track information");
00132       case 4: return i18nc("@title:column", "URL");
00133     };
00134   };
00135   return QAbstractItemModel::headerData(section, orientation, role);
00136 }
00137 
00138 bool streamDirectoryModel::hasChildren(const QModelIndex & parent) const
00139 {
00140   if (parent.isValid()) { // means: is a genre or a stream
00141     return (static_cast<streamDirectoryEntry *>(parent.internalPointer()))->hasChildren();
00142   } else {
00143     return rootEntry->hasChildren();
00144   };
00145 }
00146 
00147 Qt::ItemFlags streamDirectoryModel::flags(const QModelIndex & index) const
00148 {
00149   // variables
00150   Qt::ItemFlags returnValue;
00151 
00152   // code
00153   returnValue = QAbstractItemModel::flags(index);
00154   if (index.isValid()) { // add drag support only for valid items!
00155     returnValue = returnValue | Qt::ItemIsDragEnabled;
00156   };
00157   return returnValue;
00158 }
00159 
00160 QStringList streamDirectoryModel::mimeTypes() const
00161 {
00162   // variables
00163   QStringList types;
00164 
00165   // code
00166   types << "text/uri-list";
00167   return types;
00168 }
00169 
00170 QMimeData *streamDirectoryModel::mimeData(const QModelIndexList & indexes) const
00171 {
00172   // TODO Wie werden ausgewählte Genres gehandhabt?
00173   // variables
00174   QMimeData *returnValue;
00175   QList<QUrl> list;
00176 
00177   // code
00178   if (indexes.size() == 0) {
00179     returnValue = 0;  // because QAbstractItemModel::mimeData() would do the same!
00180   } else {
00181     returnValue = new QMimeData();
00182     foreach (const QModelIndex & index, indexes) {
00183       // We get an own index for each column. To make sure to export each row only once,
00184       // we export only for column 0.
00185       if (index.isValid() && (index.column() == 0)) {
00186         list.append(
00187           QUrl(static_cast<streamDirectoryEntry *>(index.internalPointer())->value()));
00188       };
00189     };
00190     returnValue->setUrls(list);
00191   };
00192   return returnValue;
00193 }

doxygen