File indexing completed on 2024-04-21 15:38:26

0001 /**
0002  * Copyright (C) 2011 by Koos Vriezen <koos.vriezen@gmail.com>
0003  *
0004  *  This library is free software; you can redistribute it and/or
0005  *  modify it under the terms of the GNU Library General Public
0006  *  License version 2 as published by the Free Software Foundation.
0007  *
0008  *  This library is distributed in the hope that it will be useful,
0009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011  *  Library General Public License for more details.
0012  *
0013  *  You should have received a copy of the GNU Library General Public License
0014  *  along with this library; see the file COPYING.LIB.  If not, write to
0015  *  the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
0016  *  Boston, MA 02110-1301, USA.
0017  **/
0018 
0019 #ifndef KMPLAYER_PLAYMODEL_H
0020 #define KMPLAYER_PLAYMODEL_H
0021 
0022 #include "config-kmplayer.h"
0023 
0024 #include <QAbstractItemModel>
0025 #include <QModelIndex>
0026 #include <QPixmap>
0027 
0028 #include "kmplayerplaylist.h"
0029 
0030 class QPixmap;
0031 class KIconLoader;
0032 struct TreeUpdate;
0033 
0034 namespace KMPlayer {
0035 
0036 class PlayModel; 
0037 class TopPlayItem;
0038 
0039 /*
0040  * An item in the playlist
0041  */
0042 class KMPLAYER_NO_EXPORT PlayItem
0043 {
0044 public:
0045     PlayItem (Node *e, PlayItem *parent)
0046         : item_flags (Qt::ItemIsEnabled | Qt::ItemIsSelectable),
0047           node (e), parent_item (parent)
0048     {}
0049     PlayItem (Attribute *a, PlayItem *pa)
0050         : item_flags (Qt::ItemIsEnabled | Qt::ItemIsSelectable),
0051           attribute (a), parent_item (pa)
0052     {}
0053     virtual ~PlayItem () { deleteChildren (); }
0054 
0055     void deleteChildren () { qDeleteAll (child_items); child_items.clear (); }
0056     void appendChild (PlayItem *child) { child_items.append (child); }
0057     PlayItem *child (unsigned i) {
0058         return i < (unsigned) child_items.size() ? child_items.at (i) : NULL;
0059     }
0060     int childCount () const { return child_items.count(); }
0061     int row () const {
0062         return parent_item->child_items.indexOf (const_cast <PlayItem*>( this));
0063     }
0064     PlayItem *parent () { return parent_item; }
0065     TopPlayItem *rootItem () KMPLAYER_EXPORT;
0066 
0067     QString title;
0068     Qt::ItemFlags item_flags;
0069 
0070     NodePtrW node;
0071     AttributePtrW attribute;
0072 
0073     QList<PlayItem*> child_items;
0074     PlayItem *parent_item;
0075 };
0076 
0077 class KMPLAYER_NO_EXPORT TopPlayItem : public PlayItem
0078 {
0079 public:
0080     TopPlayItem (PlayModel *m, int _id, Node *e, int flags)
0081       : PlayItem (e, NULL),
0082         model (m),
0083         id (_id),
0084         root_flags (flags),
0085         show_all_nodes (false),
0086         have_dark_nodes (false)
0087     {}
0088     Qt::ItemFlags itemFlags () KMPLAYER_EXPORT;
0089     void add ();
0090     void remove ();
0091     QPixmap icon;
0092     QString source;
0093     PlayModel *model;
0094     int id;
0095     int root_flags;
0096     bool show_all_nodes;
0097     bool have_dark_nodes;
0098 };
0099 
0100 class KMPLAYER_EXPORT PlayModel : public QAbstractItemModel
0101 {
0102     friend class TopPlayItem;
0103 
0104     Q_OBJECT
0105 
0106 public:
0107     enum { UrlRole = Qt::UserRole + 1 };
0108 
0109     enum Flags {
0110         AllowDrops = 0x01, AllowDrag = 0x02,
0111         InPlaceEdit = 0x04, TreeEdit = 0x08,
0112         Moveable = 0x10, Deleteable = 0x20
0113     };
0114 
0115     PlayModel(QObject *parent, KIconLoader *) KDE_NO_CDTOR_EXPORT;
0116     ~PlayModel() KDE_NO_CDTOR_EXPORT;
0117 
0118     QVariant data (const QModelIndex &index, int role) const KMPLAYER_NO_EXPORT;
0119     bool setData (const QModelIndex&, const QVariant& v, int role) KMPLAYER_NO_EXPORT;
0120     Qt::ItemFlags flags (const QModelIndex &index) const KMPLAYER_NO_EXPORT;
0121     QVariant headerData (int section, Qt::Orientation orientation,
0122             int role = Qt::DisplayRole) const KMPLAYER_NO_EXPORT;
0123     QModelIndex index (int row, int column,
0124             const QModelIndex &parent = QModelIndex()) const KMPLAYER_NO_EXPORT;
0125     QModelIndex parent (const QModelIndex &index) const KMPLAYER_NO_EXPORT;
0126     bool hasChildren (const QModelIndex& parent = QModelIndex ()) const KMPLAYER_NO_EXPORT;
0127     int rowCount (const QModelIndex &parent = QModelIndex()) const KMPLAYER_NO_EXPORT;
0128     int columnCount (const QModelIndex &parent = QModelIndex()) const KMPLAYER_NO_EXPORT;
0129 
0130     PlayItem *rootItem () const KMPLAYER_NO_EXPORT { return root_item; }
0131     QModelIndex indexFromItem (PlayItem *item) const KMPLAYER_NO_EXPORT;
0132     PlayItem *itemFromIndex (const QModelIndex& index) const KMPLAYER_NO_EXPORT;
0133 
0134     int addTree (NodePtr r, const QString &src, const QString &ico, int flgs);
0135     PlayItem *updateTree (TopPlayItem *ritem, NodePtr active);
0136 signals:
0137     void updating (const QModelIndex&);
0138     void updated (const QModelIndex&, const QModelIndex&, bool sel, bool exp);
0139 
0140 public slots:
0141     void updateTree (int id, NodePtr root, NodePtr active, bool sel, bool open);
0142 
0143 private slots:
0144     void updateTrees() KMPLAYER_NO_EXPORT;
0145 
0146 private:
0147     PlayItem *populate (Node *e, Node *focus,
0148             TopPlayItem *root, PlayItem *item,
0149             PlayItem **curitem) KMPLAYER_NO_EXPORT;
0150     SharedPtr <TreeUpdate> tree_update;
0151     QPixmap auxiliary_pix;
0152     QPixmap config_pix;
0153     QPixmap folder_pix;
0154     QPixmap img_pix;
0155     QPixmap info_pix;
0156     QPixmap menu_pix;
0157     QPixmap unknown_pix;
0158     QPixmap url_pix;
0159     QPixmap video_pix;
0160     PlayItem *root_item;
0161     int last_id;
0162 };
0163 
0164 }
0165 
0166 #endif
0167