1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
25 #include <config-nepomuk.h>
27 #include "libdolphin_export.h"
29 #include <kparts/part.h>
30 #include <kfileitem.h>
31 #include <kfileitemdelegate.h>
32 #include <kio/fileundomanager.h>
37 #include <QLinkedList>
42 typedef KIO::FileUndoManager::CommandType CommandType
;
44 class DolphinController
;
45 class DolphinColumnView
;
46 class DolphinDetailsView
;
47 class DolphinFileItemDelegate
;
48 class DolphinIconsView
;
49 class DolphinMainWindow
;
51 class DolphinSortFilterProxyModel
;
52 class KFilePreviewGenerator
;
54 class KActionCollection
;
63 * @short Represents a view for the directory content.
65 * View modes for icons, details and columns are supported. It's
72 * @see DolphinIconsView
73 * @see DolphinDetailsView
74 * @see DolphinColumnView
76 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
82 * Defines the view mode for a directory. The view mode
83 * can be defined when constructing a DolphinView. The
84 * view mode is automatically updated if the directory itself
85 * defines a view mode (see class ViewProperties for details).
90 * The directory items are shown as icons including an
96 * The icon, the name and at least the size of the directory
97 * items are shown in a table. It is possible to add columns
98 * for date, group and permissions.
103 * Each folder is shown in a separate column.
106 MaxModeEnum
= ColumnView
109 /** Defines the sort order for the items of a directory. */
121 MaxSortEnum
= SortByTags
125 * @param parent Parent widget of the view.
126 * @param url Specifies the content which should be shown.
127 * @param dirLister Used directory lister. The lister is not owned
128 * by the view and won't get deleted.
129 * @param dolphinModel Used directory model. The model is not owned
130 * by the view and won't get deleted.
131 * @param proxyModel Used proxy model which specifies the sorting. The
132 * model is not owned by the view and won't get
135 DolphinView(QWidget
* parent
,
137 KDirLister
* dirLister
,
138 DolphinModel
* dolphinModel
,
139 DolphinSortFilterProxyModel
* proxyModel
);
141 virtual ~DolphinView();
144 * Returns the current active URL, where all actions are applied.
145 * The URL navigator is synchronized with this URL.
147 const KUrl
& url() const;
150 * Returns the root URL of the view, which is defined as the first
151 * visible path of DolphinView::url(). Usually the root URL is
152 * equal to DolphinView::url(), but in the case of the column view
153 * when 2 columns are shown, the root URL might be:
154 * /home/peter/Documents
155 * and DolphinView::url() might return
156 * /home/peter/Documents/Music/
158 KUrl
rootUrl() const;
161 * If \a active is true, the view will marked as active. The active
162 * view is defined as view where all actions are applied to.
164 void setActive(bool active
);
165 bool isActive() const;
168 * Changes the view mode for the current directory to \a mode.
169 * If the view properties should be remembered for each directory
170 * (GeneralSettings::globalViewProps() returns false), then the
171 * changed view mode will be stored automatically.
173 void setMode(Mode mode
);
176 /** See setShowPreview */
177 bool showPreview() const;
179 /** See setShowHiddenFiles */
180 bool showHiddenFiles() const;
182 /** See setCategorizedSorting */
183 bool categorizedSorting() const;
186 * Returns true, if the categorized sorting is supported by the current
187 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
188 * supports categorizations. To check whether the categorized
189 * sorting is set, use DolphinView::categorizedSorting().
191 bool supportsCategorizedSorting() const;
195 * @see DolphinView::selectedItems()
200 * Inverts the current selection: selected items get unselected,
201 * unselected items get selected.
202 * @see DolphinView::selectedItems()
204 void invertSelection();
206 /** Returns true, if at least one item is selected. */
207 bool hasSelection() const;
209 void clearSelection();
212 * Returns the selected items. The list is empty if no item has been
214 * @see DolphinView::selectedUrls()
216 KFileItemList
selectedItems() const;
219 * Returns a list of URLs for all selected items. An empty list
220 * is returned, if no item is selected.
221 * @see DolphinView::selectedItems()
223 KUrl::List
selectedUrls() const;
226 * Returns the number of selected items (this is faster than
227 * invoking selectedItems().count()).
229 int selectedItemsCount() const;
232 * Sets the upper left position of the view content
233 * to (x,y). The content of the view might be larger than the visible area
234 * and hence a scrolling must be done.
236 void setContentsPosition(int x
, int y
);
238 /** Returns the upper left position of the view content. */
239 QPoint
contentsPosition() const;
242 * Sets the zoom level to \a level. It is assured that the used
243 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
244 * ZoomLevelInfo::maximumLevel().
246 void setZoomLevel(int level
);
247 int zoomLevel() const;
250 * Returns true, if zooming in is possible. If false is returned,
251 * the maximum zooming level has been reached.
253 bool isZoomInPossible() const;
256 * Returns true, if zooming out is possible. If false is returned,
257 * the minimum zooming level has been reached.
259 bool isZoomOutPossible() const;
261 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
262 void setSorting(Sorting sorting
);
264 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
265 Sorting
sorting() const;
267 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
268 void setSortOrder(Qt::SortOrder order
);
270 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
271 Qt::SortOrder
sortOrder() const;
273 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
274 void setSortFoldersFirst(bool foldersFirst
);
276 /** Returns if files and folders are sorted separately or not. */
277 bool sortFoldersFirst() const;
279 /** Sets the additional information which should be shown for the items. */
280 void setAdditionalInfo(KFileItemDelegate::InformationList info
);
282 /** Returns the additional information which should be shown for the items. */
283 KFileItemDelegate::InformationList
additionalInfo() const;
285 /** Reloads the current directory. */
289 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
290 * This method only needs to get invoked if the view settings for the Icons View,
291 * Details View or Columns View have been changed.
296 * Changes the directory of the view to \a url. If \a rootUrl is empty, the view
297 * properties from \a url are used for adjusting the view mode and the other properties.
298 * If \a rootUrl is not empty, the view properties from the root URL are considered
299 * instead. Specifying a root URL is only required if a view having a different root URL
300 * (e. g. the column view) should be restored. Usually using DolphinView::setUrl()
301 * is enough for changing the current URL.
303 void updateView(const KUrl
& url
, const KUrl
& rootUrl
);
306 * Filters the currently shown items by \a nameFilter. All items
307 * which contain the given filter string will be shown.
309 void setNameFilter(const QString
& nameFilter
);
312 * Calculates the number of currently shown files into
313 * \a fileCount and the number of folders into \a folderCount.
314 * The size of all files is written into \a totalFileSize.
315 * It is recommend using this method instead of asking the
316 * directory lister or the model directly, as it takes
317 * filtering and hierarchical previews into account.
319 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
322 * Returns a textual representation of the state of the current
323 * folder or selected items, suitable for use in the status bar.
325 QString
statusBarText() const;
328 * Updates the state of the 'Additional Information' actions in \a collection.
330 void updateAdditionalInfoActions(KActionCollection
* collection
);
333 * Returns the state of the paste action:
334 * first is whether the action should be enabled
335 * second is the text for the action
337 QPair
<bool, QString
> pasteInfo() const;
340 * If \a tabsForFiles is true, the signal tabRequested() will also
341 * emitted also for files. Per default tabs for files is disabled
342 * and hence the signal tabRequested() will only be emitted for
345 void setTabsForFilesEnabled(bool tabsForFiles
);
346 bool isTabsForFilesEnabled() const;
349 * Marks the item \a url as active item as soon as it has
350 * been loaded by the directory lister. This is useful mark
351 * the previously visited directory as active when going
352 * back in history (the URL is known, but the item is not
355 void activateItem(const KUrl
& url
);
358 * Returns true if the current view allows folders to be expanded,
359 * i.e. presents a hierarchical view to the user.
361 bool itemsExpandable() const;
365 * Changes the directory to \a url. If the current directory is equal to
366 * \a url, nothing will be done (use DolphinView::reload() instead).
368 void setUrl(const KUrl
& url
);
371 * Request of a selection change. The view will do its best to accommodate
372 * the request, but it is not guaranteed that all items in \a selection
373 * will actually get selected. The view will e.g. not select items which
374 * are not in the currently displayed folder.
376 void changeSelection(const KFileItemList
& selection
);
379 * Triggers the renaming of the currently selected items, where
380 * the user must input a new name for the items.
382 void renameSelectedItems();
385 * Moves all selected items to the trash.
387 void trashSelectedItems();
390 * Deletes all selected items.
392 void deleteSelectedItems();
395 * Copies all selected items to the clipboard and marks
396 * the items as cutted.
398 void cutSelectedItems();
400 /** Copies all selected items to the clipboard. */
401 void copySelectedItems();
403 /** Pastes the clipboard data to this view. */
407 * Pastes the clipboard data into the currently selected
408 * folder. If the current selection is not exactly one folder, no
409 * paste operation is done.
411 void pasteIntoFolder();
414 * Turns on the file preview for the all files of the current directory,
415 * if \a show is true.
416 * If the view properties should be remembered for each directory
417 * (GeneralSettings::globalViewProps() returns false), then the
418 * preview setting will be stored automatically.
420 void setShowPreview(bool show
);
423 * Shows all hidden files of the current directory,
424 * if \a show is true.
425 * If the view properties should be remembered for each directory
426 * (GeneralSettings::globalViewProps() returns false), then the
427 * show hidden file setting will be stored automatically.
429 void setShowHiddenFiles(bool show
);
432 * Summarizes all sorted items by their category \a categorized
434 * If the view properties should be remembered for each directory
435 * (GeneralSettings::globalViewProps() returns false), then the
436 * categorized sorting setting will be stored automatically.
438 void setCategorizedSorting(bool categorized
);
440 /** Switches between an ascending and descending sorting order. */
441 void toggleSortOrder();
443 /** Switches between a separate sorting (with folders first) and a mixed sorting of files and folders. */
444 void toggleSortFoldersFirst();
447 * Switches on or off the displaying of additional information
448 * as specified by \a action.
450 void toggleAdditionalInfo(QAction
* action
);
454 * Is emitted if the view has been activated by e. g. a mouse click.
458 /** Is emitted if URL of the view has been changed to \a url. */
459 void urlChanged(const KUrl
& url
);
462 * Is emitted if the view requests a changing of the current
463 * URL to \a url (see DolphinController::triggerUrlChangeRequest()).
465 void requestUrlChange(const KUrl
& url
);
468 * Is emitted when clicking on an item with the left mouse button.
470 void itemTriggered(const KFileItem
& item
);
473 * Is emitted if a new tab should be opened for the URL \a url.
475 void tabRequested(const KUrl
& url
);
478 * Is emitted if the view mode (IconsView, DetailsView,
479 * PreviewsView) has been changed.
483 /** Is emitted if the 'show preview' property has been changed. */
484 void showPreviewChanged();
486 /** Is emitted if the 'show hidden files' property has been changed. */
487 void showHiddenFilesChanged();
489 /** Is emitted if the 'categorized sorting' property has been changed. */
490 void categorizedSortingChanged();
492 /** Is emitted if the sorting by name, size or date has been changed. */
493 void sortingChanged(DolphinView::Sorting sorting
);
495 /** Is emitted if the sort order (ascending or descending) has been changed. */
496 void sortOrderChanged(Qt::SortOrder order
);
498 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
499 void sortFoldersFirstChanged(bool foldersFirst
);
501 /** Is emitted if the additional information shown for this view has been changed. */
502 void additionalInfoChanged();
504 /** Is emitted if the zoom level has been changed by zooming in or out. */
505 void zoomLevelChanged(int level
);
508 * Is emitted if information of an item is requested to be shown e. g. in the panel.
509 * If item is null, no item information request is pending.
511 void requestItemInfo(const KFileItem
& item
);
513 /** Is emitted if the contents has been moved to \a x, \a y. */
514 void contentsMoved(int x
, int y
);
517 * Is emitted whenever the selection has been changed.
519 void selectionChanged(const KFileItemList
& selection
);
522 * Is emitted if a context menu is requested for the item \a item,
523 * which is part of \a url. If the item is null, the context menu
524 * for the URL should be shown and the custom actions \a customActions
527 void requestContextMenu(const KFileItem
& item
,
529 const QList
<QAction
*>& customActions
);
532 * Is emitted if an information message with the content \a msg
535 void infoMessage(const QString
& msg
);
538 * Is emitted if an error message with the content \a msg
541 void errorMessage(const QString
& msg
);
544 * Is emitted if an "operation completed" message with the content \a msg
547 void operationCompletedMessage(const QString
& msg
);
550 * Is emitted after DolphinView::setUrl() has been invoked and
551 * the path \a url is currently loaded. If this signal is emitted,
552 * it is assured that the view contains already the correct root
553 * URL and property settings.
555 void startedPathLoading(const KUrl
& url
);
558 * Emitted when KDirLister emits redirection.
559 * Testcase: fish://localhost
561 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
564 /** @see QWidget::mouseReleaseEvent */
565 virtual void mouseReleaseEvent(QMouseEvent
* event
);
566 virtual void wheelEvent(QWheelEvent
* event
);
567 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
571 * Marks the view as active (DolphinView:isActive() will return true)
572 * and emits the 'activated' signal if it is not already active.
577 * If the item \a item is a directory, then this
578 * directory will be loaded. If the item is a file, the corresponding
579 * application will get started.
581 void triggerItem(const KFileItem
& index
);
583 void emitSelectionChangedSignal();
586 * Opens the context menu on position \a pos. The position
587 * is used to check whether the context menu is related to an
588 * item or to the viewport.
590 void openContextMenu(const QPoint
& pos
, const QList
<QAction
*>& customActions
);
593 * Drops dragged URLs to the destination path \a destPath. If
594 * the URLs are dropped above an item inside the destination path,
595 * the item is indicated by \a destItem.
597 void dropUrls(const KFileItem
& destItem
,
598 const KUrl
& destPath
,
602 * Updates the view properties of the current URL to the
603 * sorting given by \a sorting.
605 void updateSorting(DolphinView::Sorting sorting
);
608 * Updates the view properties of the current URL to the
609 * sort order given by \a order.
611 void updateSortOrder(Qt::SortOrder order
);
614 * Updates the view properties of the current URL to the
615 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
617 void updateSortFoldersFirst(bool foldersFirst
);
620 * Updates the view properties of the current URL to the
621 * additional information given by \a info.
623 void updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
);
626 * Emits the signal contentsMoved with the current coordinates
627 * of the viewport as parameters.
629 void emitContentsMoved();
632 * Updates the status bar to show hover information for the
633 * item \a item. If currently other items are selected,
634 * no hover information is shown.
635 * @see DolphinView::clearHoverInformation()
637 void showHoverInformation(const KFileItem
& item
);
640 * Clears the hover information shown in the status bar.
641 * @see DolphinView::showHoverInformation().
643 void clearHoverInformation();
646 * Indicates in the status bar that the delete operation
647 * of the job \a job has been finished.
649 void slotDeleteFileFinished(KJob
* job
);
652 * Is emitted if the controller requests a changing of the current
655 void slotRequestUrlChange(const KUrl
& url
);
658 * Invoked when the directory lister has completed the loading of
659 * items. Assures that pasted items and renamed items get seleced.
661 void slotDirListerCompleted();
664 * Is invoked when the KDirLister indicates refreshed items.
666 void slotRefreshItems();
669 * If \a view can be positively identified as not being the source for the
670 * current drag operation, deleteLater() it immediately. Else stores
671 * it for later deletion.
673 void deleteWhenNotDragSource(QAbstractItemView
* view
);
676 * Observes the item with the URL \a url. As soon as the directory
677 * model indicates that the item is available, the item will
678 * get selected and it is assure that the item stays visible.
680 * @see selectAndScrollToCreatedItem()
682 void observeCreatedItem(const KUrl
& url
);
685 * Selects and scrolls to the item that got observed
686 * by observeCreatedItem().
688 void selectAndScrollToCreatedItem();
691 * Restore selection after view refresh.
693 void restoreSelection();
696 void loadDirectory(const KUrl
& url
, bool reload
= false);
699 * Returns the URL where the view properties should be stored. Usually
700 * DolphinView::url() is returned, but in the case of a Column View the
701 * view properties are always stored in the directory represented by the
702 * first column. It is recommendend whenever using the ViewProperties class
703 * to use DolphinView::viewPropertiesUrl() as URL.
705 KUrl
viewPropertiesUrl() const;
708 * Applies the view properties which are defined by the current URL
709 * m_url to the DolphinView properties.
711 void applyViewProperties(const KUrl
& url
);
714 * Creates a new view representing the given view mode (DolphinView::mode()).
715 * The current view will get deleted.
722 * Returns a pointer to the currently used item view, which is either
723 * a ListView or a TreeView.
725 QAbstractItemView
* itemView() const;
728 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
729 * Pastes the clipboard data into the URL \a url.
731 void pasteToUrl(const KUrl
& url
);
734 * Checks whether the current item view has the same zoom level
735 * as \a oldZoomLevel. If this is not the case, the zoom level
736 * of the controller is updated and a zoomLevelChanged() signal
739 void updateZoomLevel(int oldZoomLevel
);
742 * Returns a list of URLs for all selected items. The list is
743 * simplified, so that when the URLs are part of different tree
744 * levels, only the parent is returned.
746 KUrl::List
simplifiedSelectedUrls() const;
749 * Returns true, if the ColumnView is activated. As the column view
750 * requires some special handling for iterating through directories,
751 * this method has been introduced for convenience.
753 bool isColumnViewActive() const;
756 * Returns the MIME data for all selected items.
758 QMimeData
* selectionMimeData() const;
761 * Is invoked after a paste operation or a drag & drop
762 * operation and adds the filenames of all URLs from \a mimeData to
763 * m_newFileNames. This allows to select all newly added
764 * items in slotDirListerCompleted().
766 void addNewFileNames(const QMimeData
* mimeData
);
770 bool m_showPreview
: 1;
771 bool m_loadingDirectory
: 1;
772 bool m_storedCategorizedSorting
: 1;
773 bool m_tabsForFiles
: 1;
774 bool m_isContextMenuOpen
: 1; // TODO: workaround for Qt-issue 207192
775 bool m_ignoreViewProperties
: 1;
776 bool m_assureVisibleCurrentIndex
: 1;
780 DolphinMainWindow
* m_mainWindow
;
781 QVBoxLayout
* m_topLayout
;
783 DolphinController
* m_controller
;
784 DolphinIconsView
* m_iconsView
;
785 DolphinDetailsView
* m_detailsView
;
786 DolphinColumnView
* m_columnView
;
787 DolphinFileItemDelegate
* m_fileItemDelegate
;
788 QItemSelectionModel
* m_selectionModel
;
790 DolphinModel
* m_dolphinModel
;
791 KDirLister
* m_dirLister
;
792 DolphinSortFilterProxyModel
* m_proxyModel
;
794 KFilePreviewGenerator
* m_previewGenerator
;
795 ToolTipManager
* m_toolTipManager
;
798 KUrl m_activeItemUrl
;
799 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
800 KFileItemList m_selectedItems
; // this is used for making the View to remember selections after F5
803 * Remembers the filenames that have been added by a paste operation
804 * or a drag & drop operation. Allows to select the items in
805 * slotDirListerCompleted().
807 QSet
<QString
> m_newFileNames
;
809 QAbstractItemView
* m_expandedDragSource
;
812 inline bool DolphinView::isColumnViewActive() const
814 return m_columnView
!= 0;
817 /// Allow using DolphinView::Mode in QVariant
818 Q_DECLARE_METATYPE(DolphinView::Mode
)
820 #endif // DOLPHINVIEW_H