File indexing completed on 2025-01-12 12:39:32
0001 /* This file is part of the KDE Project -*- mode:c++; -*- 0002 Copyright (C) 2000 David Faure <faure@kde.org> 0003 2000 Carsten Pfeiffer <pfeiffer@kde.org> 0004 2002 Klaas Freitag <freitag@suse.de> 0005 0006 This library is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU Library General Public 0008 License version 2 as published by the Free Software Foundation. 0009 0010 This library is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 Library General Public License for more details. 0014 0015 You should have received a copy of the GNU Library General Public License 0016 along with this library; see the file COPYING.LIB. If not, write to 0017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #ifndef FILETREEVIEW_H 0022 #define FILETREEVIEW_H 0023 0024 #include <qtreewidget.h> 0025 0026 #include "filetreebranch.h" 0027 0028 class QTimer; 0029 0030 /** 0031 * @short A tree view on the file system. 0032 * 0033 * The view is able to handle more than one base URL, each represented 0034 * by a FileTreeBranch. The branches and subdirectories are automatically 0035 * listed and populated when they are expanded. 0036 * 0037 * The typical usage is: 0038 * 0039 * 1. Create and layout a FileTreeView. 0040 * 0041 * 2. Call addBranch() to create one or more branches. 0042 * 0043 * 3. Retrieve the root item with KFileTreeBranch::root(), and expand it 0044 * if desired. That starts the listing. 0045 * 0046 * @author David Faure 0047 * @author Carsten Pfeiffer 0048 * @author Klaas Freitag 0049 * @author Jonathan Marten 0050 **/ 0051 0052 class FileTreeView : public QTreeWidget 0053 { 0054 Q_OBJECT 0055 0056 public: 0057 /** 0058 * Create a new view widget. 0059 * 0060 * @param parent Parent widget 0061 * 0062 * @note The default is for the view to not accept either drags or 0063 * drops. The caller must use @c setAcceptDrops(true) for drops to be 0064 * accepted, and @c setDragEnabled(true) for drags to be accepted. 0065 **/ 0066 explicit FileTreeView(QWidget *parent = nullptr); 0067 0068 /** 0069 * Destructor. All of the branches are automatically deleted. 0070 **/ 0071 virtual ~FileTreeView(); 0072 0073 /** 0074 * Get the currently selected item. 0075 * 0076 * @return the selected item, 0077 * or @c nullptr if there is no selected item. 0078 **/ 0079 FileTreeViewItem *selectedFileTreeViewItem() const; 0080 0081 /** 0082 * Get the KFileItem (file information) for the currently selected item. 0083 * 0084 * @return the @c KFileItem for the selected item, 0085 * or @c nullptr if there is no selected item. 0086 **/ 0087 const KFileItem *selectedFileItem() const; 0088 0089 /** 0090 * Get the URL of for the currently selected item. 0091 * 0092 * @return the URL of the selected item, 0093 * or an invalid URL if there is no selected item. 0094 **/ 0095 QUrl selectedUrl() const; 0096 0097 /** 0098 * Get the currently highlighted item. 0099 * 0100 * @return the highlighted item, 0101 * or @c nullptr if there is no highlighted item. 0102 **/ 0103 FileTreeViewItem *highlightedFileTreeViewItem() const; 0104 0105 /** 0106 * Get the KFileItem (file information) for the currently highlighted item. 0107 * 0108 * @return the @c KFileItem for the highlighted item, 0109 * or @c nullptr if there is no highlighted item. 0110 **/ 0111 const KFileItem *highlightedFileItem() const; 0112 0113 /** 0114 * Get the URL of for the currently highlighted item. 0115 * 0116 * @return the URL of the highlighted item, 0117 * or an invalid URL if there is no highlighted item. 0118 **/ 0119 QUrl highlightedUrl() const; 0120 0121 /** 0122 * Add a branch to the view. 0123 * 0124 * This creates a branch for the specified @p path and 0125 * adds it to the tree view. 0126 * 0127 * @param path base URL of the branch 0128 * @param name name of the branch (the text for column 0) 0129 * @param showHidden if @c true, hidden files and directories will be visible 0130 * @return the new branch, or @c nullptr if the branch could not be created 0131 * 0132 * @note No directory listing starts until the branch is expanded, 0133 * either by the user opening the root item or by calling @c setExpanded() 0134 * on the root item. 0135 **/ 0136 virtual FileTreeBranch *addBranch(const QUrl &path, const QString &name, bool showHidden = false); 0137 0138 /** 0139 * Add a branch to the view, with a specified pixmap. 0140 * 0141 * This creates a branch for the specified @p path and 0142 * adds it to the tree view. 0143 * 0144 * @param path base URL of the branch 0145 * @param name name of the branch (the text for column 0) 0146 * @param pix pixmap for the branch 0147 * @param showHidden if @c true, hidden files and directories will be visible 0148 * @return the new branch, or @c nullptr if the branch could not be created 0149 **/ 0150 virtual FileTreeBranch *addBranch(const QUrl &path, const QString &name, 0151 const QIcon &pix, bool showHidden = false); 0152 0153 /** 0154 * Add a user created branch to the view. 0155 * 0156 * @param branch the branch. The tree view takes ownership of this branch, 0157 * and will delete it when the tree view is destroyed. 0158 * 0159 * @return the specified branch 0160 **/ 0161 virtual FileTreeBranch *addBranch(FileTreeBranch *branch); 0162 0163 /** 0164 * Remove a branch from the view. 0165 * 0166 * @param branch the branch to remove 0167 * @return @c true if the branch was successfully removed 0168 * 0169 * @note The @p branch is not automatically deleted. 0170 **/ 0171 virtual bool removeBranch(FileTreeBranch *branch); 0172 0173 /** 0174 * Search for a branch by name. 0175 * 0176 * @param searchName the name of a branch to search for. 0177 * @return the named branch, or @c nullptr if the branch was not found. 0178 **/ 0179 FileTreeBranch *branch(const QString &searchName) const; 0180 0181 /** 0182 * Get a list of all existing branches. 0183 * 0184 * @return a list of all existing branches in the view 0185 **/ 0186 const FileTreeBranchList &branches() const; 0187 0188 /** 0189 * Set the directory-only mode for a branch. 0190 * 0191 * @param branch the branch to set 0192 * @param dirsOnly if @c true, only directories will be shown. 0193 * 0194 * @see KDirLister::setDirOnlyMode 0195 **/ 0196 virtual void setDirOnlyMode(FileTreeBranch *branch, bool dirsOnly); 0197 0198 /** 0199 * Search a specified branch for an item identified by a relative URL. 0200 * 0201 * @param branch the branch to search in 0202 * @param relUrl the URL path to search for, relative to the root URL of the branch 0203 * @return the found item, or @c nullptr if nothing was found 0204 **/ 0205 FileTreeViewItem *findItemInBranch(FileTreeBranch *branch, const QString &relUrl) const; 0206 0207 /** 0208 * Search a named branch for an item identified by a relative URL. 0209 * 0210 * @param branchName the name of the branch to search in 0211 * @param relUrl the URL path to search for, relative to the root URL of the branch 0212 * @return the found item, or @c nullptr if nothing was found 0213 **/ 0214 FileTreeViewItem *findItemInBranch(const QString &branchName, const QString &relUrl) const; 0215 0216 /** 0217 * Get the flag indicating whether extended folder pixmaps are displayed. 0218 * 0219 * @return the extended folder pixmap option 0220 * @see setShowFolderOpenPixmap() 0221 **/ 0222 bool showFolderOpenPixmap() const; 0223 0224 public slots: 0225 /** 0226 * Set the flag to show 'extended' folder icons. 0227 * 0228 * If set on, folders will have an "open folder" pixmap displayed 0229 * if their children are visible, and the standard "closed folder" pixmap 0230 * if they are closed. If set off, the plain "folder" pixmap is displayed. 0231 * 0232 * The default is @c true. 0233 * 0234 * @param showIt the new option setting 0235 * @see showFolderOpenPixmap() 0236 **/ 0237 virtual void setShowFolderOpenPixmap(bool showIt = true); 0238 0239 protected: 0240 /** 0241 * @reimp 0242 * @see QTreeWidget::mimeData() 0243 **/ 0244 virtual QMimeData *mimeData(const QList<QTreeWidgetItem *> items) const override; 0245 0246 /** 0247 * @reimp 0248 * @see QAbstractItemView::dragEnterEvent() 0249 **/ 0250 virtual void dragEnterEvent(QDragEnterEvent *ev) override; 0251 0252 /** 0253 * @reimp 0254 * @see QTreeView::dragMoveEvent() 0255 **/ 0256 virtual void dragMoveEvent(QDragMoveEvent *ev) override; 0257 0258 /** 0259 * @reimp 0260 * @see QAbstractItemView::dragLeaveEvent() 0261 **/ 0262 virtual void dragLeaveEvent(QDragLeaveEvent *ev) override; 0263 0264 /** 0265 * @reimp 0266 * @see QTreeWidget::dropEvent() 0267 **/ 0268 virtual void dropEvent(QDropEvent *ev) override; 0269 0270 protected slots: 0271 /** 0272 * New items have been added to or have appeared in a branch. 0273 * 0274 * @param branch the branch containing the new items 0275 * @param newItems a list of the new items 0276 **/ 0277 void slotNewTreeViewItems(FileTreeBranch *branch, 0278 const FileTreeViewItemList &newItems); 0279 0280 /** 0281 * Set the URL of the item that will be selected after new 0282 * items have been added. 0283 * 0284 * @param url the URL of the item to select 0285 **/ 0286 void slotSetNextUrlToSelect(const QUrl &url); 0287 0288 /** 0289 * Model data has changed. Used to detect the renaming of an item. 0290 * 0291 * @see QAbstractItemModel::dataChanged() 0292 **/ 0293 void slotDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); 0294 0295 signals: 0296 /** 0297 * The mouse has been moved over an item. 0298 * 0299 * @param path The path of the item. If the branch root is local, then 0300 * the file pathname of the item is sent. Otherwise, the pretty URL 0301 * is sent. 0302 * 0303 * @see QTreeWidget::itemEntered() 0304 **/ 0305 void onItem(const QString &path); 0306 0307 /** 0308 * Something has been dropped onto the view. 0309 * 0310 * A move or copy drag can be distinguished by examining the 0311 * event's drop action. 0312 * 0313 * @param ev the drop event data 0314 * @param item the item over which the drop was released 0315 * 0316 * @note Only drops of URLs are accepted (@c ev->mimeData()->hasUrls() 0317 * must be @c true). 0318 **/ 0319 void dropped(QDropEvent *ev, FileTreeViewItem *item); 0320 0321 /** 0322 * An item has been renamed. 0323 * 0324 * @param item the renamed item 0325 * @param newName the new name of the item 0326 **/ 0327 void fileRenamed(FileTreeViewItem *item, const QString &newName); 0328 0329 private slots: 0330 void slotExecuted(QTreeWidgetItem *item); 0331 void slotExpanded(QTreeWidgetItem *item); 0332 void slotCollapsed(QTreeWidgetItem *item); 0333 void slotDoubleClicked(QTreeWidgetItem *item); 0334 0335 void slotSelectionChanged(); 0336 0337 void slotOnItem(QTreeWidgetItem *item); 0338 0339 void slotAutoOpenFolder(); 0340 void slotStartAnimation(FileTreeViewItem *item); 0341 void slotStopAnimation(FileTreeViewItem *item); 0342 0343 private: 0344 QIcon itemIcon(FileTreeViewItem *item) const; 0345 void setDropItem(QTreeWidgetItem *item); 0346 0347 FileTreeBranchList m_branches; // list of the branches 0348 int m_busyCount; // branches currently listing 0349 QUrl m_nextUrlToSelect; // next item to select 0350 bool m_wantOpenFolderPixmaps; // want open-folder pixmaps? 0351 0352 QTreeWidgetItem *m_currentBeforeDropItem; // current item before drag 0353 QTreeWidgetItem *m_dropItem; // item mouse is over 0354 0355 QIcon m_openFolderPixmap; // pixmap for open folders 0356 QTimer *m_autoOpenTimer; // timer for auto open 0357 }; 0358 0359 #endif // FILETREEVIEW_H