File indexing completed on 2024-04-21 15:12:08

0001 
0002 /* This file is part of the KDE project
0003    Copyright (C) 2000 David Faure <faure@kde.org>
0004                  2000 Carsten Pfeiffer <pfeiffer@kde.org>
0005                  2001 Klaas Freitag <freitag@suse.de>
0006 
0007    This library is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU Library General Public
0009    License version 2 as published by the Free Software Foundation.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019    Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #ifndef FILETREEBRANCH_H
0023 #define FILETREEBRANCH_H
0024 
0025 #include <qicon.h>
0026 #include <qhash.h>
0027 #include <qurl.h>
0028 
0029 #include <kfileitem.h>
0030 #include <kcoredirlister.h>
0031 
0032 #include <kio/global.h>
0033 #include <kio/job.h>
0034 
0035 #include "filetreeviewitem.h"
0036 
0037 class FileTreeView;
0038 
0039 /**
0040  * This is the branch class of the FileTreeView, which represents one
0041  * branch in the treeview. Every branch has a root which is an url. The branch
0042  * lists the files under the root. Every branch uses its own dirlister and can
0043  * have its own filter etc.
0044  *
0045  * @short Branch object for FileTreeView object.
0046  *
0047  */
0048 
0049 class FileTreeBranch : public KCoreDirLister
0050 {
0051     Q_OBJECT
0052 
0053 public:
0054     /**
0055      * constructs a branch for FileTreeView. Does not yet start to list it.
0056      * @param url start url of the branch.
0057      * @param name the name of the branch, which is displayed in the first column of the treeview.
0058      * @param pix is a pixmap to display as an icon of the branch.
0059      * @param showHidden flag to make hidden files visible or not.
0060      * @param branchRoot is the FileTreeViewItem to use as the root of the
0061      *        branch, with the default nullptr meaning to let FileTreeBranch create
0062      *        it for you.
0063      */
0064     explicit FileTreeBranch(FileTreeView *parent, const QUrl &url, const QString &name,
0065                    const QIcon &pix, bool showHidden = false,
0066                    FileTreeViewItem *branchRoot = nullptr);
0067 
0068     /**
0069      * @returns the root url of the branch.
0070      */
0071     QUrl rootUrl() const;
0072 
0073     /**
0074      * sets a FileTreeViewItem as root widget for the branch.
0075      * That must be created outside of the branch. All FileTreeViewItems
0076      * the branch is allocating will become children of that object.
0077      * @param r the FileTreeViewItem to become the root item.
0078      */
0079     virtual void setRoot(FileTreeViewItem *r);
0080 
0081     /**
0082      * @returns the root item.
0083      */
0084     FileTreeViewItem *root() const;
0085 
0086     /**
0087      * returns and sets the name of the branch.
0088      */
0089     QString name() const;
0090     virtual void setName(const QString &newName);
0091 
0092     /*
0093      * returns the current root item pixmap set in the constructor. The root
0094      * item pixmap defaults to the icon for directories.
0095      * @see openPixmap()
0096      */
0097     QIcon pixmap() const;
0098 
0099     /*
0100      * returns the current root item pixmap set by setOpenPixmap()
0101      * which is displayed if the branch is expanded.
0102      * The root item pixmap defaults to the icon for directories.
0103      * @see pixmap()
0104      * Note that it depends on FileTreeView::showFolderOpenPximap weather
0105      * open pixmap are displayed or not.
0106      */
0107     QIcon openPixmap() const;
0108     void setOpenPixmap(const QIcon &pix);
0109 
0110     /**
0111      * @returns whether the items in the branch show their file extensions in the
0112      * tree or not. See setShowExtensions for more information.
0113      * sets printing of the file extensions on or off. If you pass false to this
0114      * slot, all items of this branch will not show their file extensions in the
0115      * tree.
0116      * @param visible flags if the extensions should be visible or not.
0117      */
0118     bool showExtensions() const;
0119     virtual void setShowExtensions(bool visible = true);
0120 
0121     /**
0122      * sets the root of the branch open or closed.
0123      */
0124     void setOpen(bool open = true);
0125 
0126     /**
0127      * sets if children recursion is wanted or not. If this is switched off, the
0128      * child directories of a just opened directory are not listed internally.
0129      * That means that it can not be determined if the sub directories are
0130      * expandable or not. If this is switched off there will be no call to
0131      * setExpandable.
0132      * @param t set to true to switch on child recursion
0133      */
0134     bool childRecurse();
0135     void setChildRecurse(bool t = true);
0136 
0137     /**
0138      * find the according FileTreeViewItem for a url
0139      */
0140     virtual FileTreeViewItem *findItemByUrl(const QUrl &url);
0141 
0142     /**
0143      * find the according FileTreeViewItem for a relative path
0144      */
0145     virtual FileTreeViewItem *findItemByPath(const QString &path);
0146 
0147     virtual void itemRenamed(FileTreeViewItem *item);
0148 
0149 public slots:
0150     /**
0151      * populates a branch. This method must be called after a branch was added
0152      * to  a FileTreeView using method addBranch.
0153      * @param url is the url of the root item where the branch starts.
0154      * @param currItem is the current parent.
0155      */
0156     virtual bool populate(const QUrl &url, FileTreeViewItem *currItem);
0157 
0158 protected:
0159     /**
0160      * allocates a FileTreeViewItem for a new item in the branch.
0161      */
0162     virtual FileTreeViewItem *createTreeViewItem(FileTreeViewItem *parent,
0163             const KFileItem &fileItem);
0164 
0165 signals:
0166     /**
0167      * indicates start/finish lister activity
0168      */
0169     void populateStarted(FileTreeViewItem *item);
0170     void populateFinished(FileTreeViewItem *item);
0171 
0172     /**
0173      * emitted with a list of new or updated FileTreeViewItem's which were
0174      * found in a branch. Note that this signal is emitted very often and
0175      * may slow down the performance of the treeview!
0176      */
0177     void newTreeViewItems(FileTreeBranch *branch, const FileTreeViewItemList &items);
0178     void changedTreeViewItems(FileTreeBranch *branch, const FileTreeViewItemList &items);
0179 
0180     /**
0181      * emitted with the exact count of children for a directory.
0182      */
0183     void directoryChildCount(FileTreeViewItem *item, int count);
0184 
0185 private slots:
0186     void slotRefreshItems(const QList<QPair<KFileItem, KFileItem> > &items);
0187     void slotListerCompleted(const QUrl &url);
0188     void slotListerCanceled(const QUrl &url);
0189     void slotListerStarted(const QUrl &url);
0190     void slotListerClear();
0191     void slotListerClearUrl(const QUrl &url);
0192     void slotRedirect(const QUrl &oldUrl, const QUrl &newUrl);
0193     void slotItemsDeleted(const KFileItemList &items);
0194     void slotItemsAdded(const QUrl &parent, const KFileItemList &items);
0195 
0196 private:
0197     void itemDeleted(const KFileItem *fi);
0198 
0199     static void deleteChildrenOf(QTreeWidgetItem *parent);
0200 
0201     FileTreeViewItem *m_root;
0202     QUrl m_startURL;
0203     QString m_name;
0204     QIcon m_rootIcon;
0205     QIcon m_openRootIcon;
0206 
0207     /* this list holds the url's which children are opened. */
0208     QList<QUrl> m_openChildrenURLs;
0209 
0210     /* Used for caching purposes in findItemByURL() */
0211     QUrl m_lastFoundUrl;
0212     FileTreeViewItem *m_lastFoundItem;
0213 
0214     bool m_recurseChildren;
0215     bool m_showExtensions;
0216 
0217     QHash<QUrl, FileTreeViewItem *> m_itemMap;
0218 };
0219 
0220 /**
0221  * List of KFileTreeBranches
0222  */
0223 typedef QList<FileTreeBranch *> FileTreeBranchList;
0224 
0225 #endif                          // FILETREEBRANCH_H