1 /***************************************************************************
2 * Copyright (C) 2006-2009 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 DolphinColumnViewContainer
;
45 class DolphinDetailsView
;
46 class DolphinIconsView
;
48 class DolphinSortFilterProxyModel
;
49 class DolphinViewController
;
51 class KActionCollection
;
54 class ViewModeController
;
56 class DolphinDetailsViewExpander
;
59 * @short Represents a view for the directory content.
61 * View modes for icons, details and columns are supported. It's
68 * @see DolphinIconsView
69 * @see DolphinDetailsView
70 * @see DolphinColumnView
72 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
78 * Defines the view mode for a directory. The view mode
79 * can be defined when constructing a DolphinView. The
80 * view mode is automatically updated if the directory itself
81 * defines a view mode (see class ViewProperties for details).
86 * The directory items are shown as icons including an
92 * The icon, the name and at least the size of the directory
93 * items are shown in a table. It is possible to add columns
94 * for date, group and permissions.
99 * Each folder is shown in a separate column.
102 MaxModeEnum
= ColumnView
105 /** Defines the sort order for the items of a directory. */
117 MaxSortingEnum
= SortByPath
121 * @param parent Parent widget of the view.
122 * @param url Specifies the content which should be shown.
123 * @param proxyModel Used proxy model which specifies the sorting. The
124 * model is not owned by the view and won't get
127 DolphinView(QWidget
* parent
,
129 DolphinSortFilterProxyModel
* proxyModel
);
131 virtual ~DolphinView();
134 * Returns the current active URL, where all actions are applied.
135 * The URL navigator is synchronized with this URL.
140 * Returns the root URL of the view, which is defined as the first
141 * visible path of DolphinView::url(). Usually the root URL is
142 * equal to DolphinView::url(), but in the case of the column view
143 * when 2 columns are shown, the root URL might be:
144 * /home/peter/Documents
145 * and DolphinView::url() might return
146 * /home/peter/Documents/Music/
148 KUrl
rootUrl() const;
151 * If \a active is true, the view will marked as active. The active
152 * view is defined as view where all actions are applied to.
154 void setActive(bool active
);
155 bool isActive() const;
158 * Changes the view mode for the current directory to \a mode.
159 * If the view properties should be remembered for each directory
160 * (GeneralSettings::globalViewProps() returns false), then the
161 * changed view mode will be stored automatically.
163 void setMode(Mode mode
);
166 /** See setShowPreview */
167 bool showPreview() const;
169 /** See setShowHiddenFiles */
170 bool showHiddenFiles() const;
172 /** See setCategorizedSorting */
173 bool categorizedSorting() const;
176 * Returns true, if the categorized sorting is supported by the current
177 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
178 * supports categorizations. To check whether the categorized
179 * sorting is set, use DolphinView::categorizedSorting().
181 bool supportsCategorizedSorting() const;
184 * Marks the items indicated by \p urls to get selected after the
185 * directory DolphinView::url() has been loaded. Note that nothing
186 * gets selected if no loading of a directory has been triggered
187 * by DolphinView::setUrl() or DolphinView::reload().
189 void markUrlsAsSelected(const QList
<KUrl
>& urls
);
192 * Returns the selected items. The list is empty if no item has been
194 * @see DolphinView::selectedUrls()
196 KFileItemList
selectedItems() const;
199 * Returns a list of URLs for all selected items. An empty list
200 * is returned, if no item is selected.
201 * @see DolphinView::selectedItems()
203 KUrl::List
selectedUrls() const;
206 * Returns the number of selected items (this is faster than
207 * invoking selectedItems().count()).
209 int selectedItemsCount() const;
211 QItemSelectionModel
* selectionModel() const;
214 * Sets the zoom level to \a level. It is assured that the used
215 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
216 * ZoomLevelInfo::maximumLevel().
218 void setZoomLevel(int level
);
219 int zoomLevel() const;
222 * Returns true, if zooming in is possible. If false is returned,
223 * the maximum zooming level has been reached.
225 bool isZoomInPossible() const;
228 * Returns true, if zooming out is possible. If false is returned,
229 * the minimum zooming level has been reached.
231 bool isZoomOutPossible() const;
233 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
234 void setSorting(Sorting sorting
);
236 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
237 Sorting
sorting() const;
239 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
240 void setSortOrder(Qt::SortOrder order
);
242 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
243 Qt::SortOrder
sortOrder() const;
245 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
246 void setSortFoldersFirst(bool foldersFirst
);
248 /** Returns if files and folders are sorted separately or not. */
249 bool sortFoldersFirst() const;
251 /** Sets the additional information which should be shown for the items. */
252 void setAdditionalInfo(KFileItemDelegate::InformationList info
);
254 /** Returns the additional information which should be shown for the items. */
255 KFileItemDelegate::InformationList
additionalInfo() const;
257 /** Reloads the current directory. */
261 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
262 * This method only needs to get invoked if the view settings for the Icons View,
263 * Details View or Columns View have been changed.
268 * Filters the currently shown items by \a nameFilter. All items
269 * which contain the given filter string will be shown.
271 void setNameFilter(const QString
& nameFilter
);
274 * Calculates the number of currently shown files into
275 * \a fileCount and the number of folders into \a folderCount.
276 * The size of all files is written into \a totalFileSize.
277 * It is recommend using this method instead of asking the
278 * directory lister or the model directly, as it takes
279 * filtering and hierarchical previews into account.
281 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
284 * Returns a textual representation of the state of the current
285 * folder or selected items, suitable for use in the status bar.
287 QString
statusBarText() const;
290 * Returns the version control actions that are provided for the items \p items.
291 * Usually the actions are presented in the context menu.
293 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
296 * Updates the state of the 'Additional Information' actions in \a collection.
298 void updateAdditionalInfoActions(KActionCollection
* collection
);
301 * Returns the state of the paste action:
302 * first is whether the action should be enabled
303 * second is the text for the action
305 QPair
<bool, QString
> pasteInfo() const;
308 * If \a tabsForFiles is true, the signal tabRequested() will also
309 * emitted also for files. Per default tabs for files is disabled
310 * and hence the signal tabRequested() will only be emitted for
313 void setTabsForFilesEnabled(bool tabsForFiles
);
314 bool isTabsForFilesEnabled() const;
317 * Returns true if the current view allows folders to be expanded,
318 * i.e. presents a hierarchical view to the user.
320 bool itemsExpandable() const;
323 * Restores the view state (current item, contents position, details view expansion state)
325 void restoreState(QDataStream
& stream
);
328 * Saves the view state (current item, contents position, details view expansion state)
330 void saveState(QDataStream
& stream
);
334 * Changes the directory to \a url. If the current directory is equal to
335 * \a url, nothing will be done (use DolphinView::reload() instead).
337 void setUrl(const KUrl
& url
);
341 * @see DolphinView::selectedItems()
346 * Inverts the current selection: selected items get unselected,
347 * unselected items get selected.
348 * @see DolphinView::selectedItems()
350 void invertSelection();
352 /** Returns true, if at least one item is selected. */
353 bool hasSelection() const;
355 void clearSelection();
358 * Triggers the renaming of the currently selected items, where
359 * the user must input a new name for the items.
361 void renameSelectedItems();
364 * Moves all selected items to the trash.
366 void trashSelectedItems();
369 * Deletes all selected items.
371 void deleteSelectedItems();
374 * Copies all selected items to the clipboard and marks
377 void cutSelectedItems();
379 /** Copies all selected items to the clipboard. */
380 void copySelectedItems();
382 /** Pastes the clipboard data to this view. */
386 * Pastes the clipboard data into the currently selected
387 * folder. If the current selection is not exactly one folder, no
388 * paste operation is done.
390 void pasteIntoFolder();
393 * Turns on the file preview for the all files of the current directory,
394 * if \a show is true.
395 * If the view properties should be remembered for each directory
396 * (GeneralSettings::globalViewProps() returns false), then the
397 * preview setting will be stored automatically.
399 void setShowPreview(bool show
);
402 * Shows all hidden files of the current directory,
403 * if \a show is true.
404 * If the view properties should be remembered for each directory
405 * (GeneralSettings::globalViewProps() returns false), then the
406 * show hidden file setting will be stored automatically.
408 void setShowHiddenFiles(bool show
);
411 * Summarizes all sorted items by their category \a categorized
413 * If the view properties should be remembered for each directory
414 * (GeneralSettings::globalViewProps() returns false), then the
415 * categorized sorting setting will be stored automatically.
417 void setCategorizedSorting(bool categorized
);
419 /** Switches between an ascending and descending sorting order. */
420 void toggleSortOrder();
422 /** Switches between a separate sorting (with folders first) and a mixed sorting of files and folders. */
423 void toggleSortFoldersFirst();
426 * Switches on or off the displaying of additional information
427 * as specified by \a action.
429 void toggleAdditionalInfo(QAction
* action
);
433 * Is emitted if the view has been activated by e. g. a mouse click.
437 /** Is emitted if URL of the view has been changed to \a url. */
438 void urlChanged(const KUrl
& url
);
441 * Is emitted when clicking on an item with the left mouse button.
443 void itemTriggered(const KFileItem
& item
);
446 * Is emitted if a new tab should be opened for the URL \a url.
448 void tabRequested(const KUrl
& url
);
451 * Is emitted if the view mode (IconsView, DetailsView,
452 * PreviewsView) has been changed.
456 /** Is emitted if the 'show preview' property has been changed. */
457 void showPreviewChanged();
459 /** Is emitted if the 'show hidden files' property has been changed. */
460 void showHiddenFilesChanged();
462 /** Is emitted if the 'categorized sorting' property has been changed. */
463 void categorizedSortingChanged();
465 /** Is emitted if the sorting by name, size or date has been changed. */
466 void sortingChanged(DolphinView::Sorting sorting
);
468 /** Is emitted if the sort order (ascending or descending) has been changed. */
469 void sortOrderChanged(Qt::SortOrder order
);
471 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
472 void sortFoldersFirstChanged(bool foldersFirst
);
474 /** Is emitted if the additional information shown for this view has been changed. */
475 void additionalInfoChanged();
477 /** Is emitted if the zoom level has been changed by zooming in or out. */
478 void zoomLevelChanged(int level
);
481 * Is emitted if information of an item is requested to be shown e. g. in the panel.
482 * If item is null, no item information request is pending.
484 void requestItemInfo(const KFileItem
& item
);
487 * Is emitted whenever the selection has been changed.
489 void selectionChanged(const KFileItemList
& selection
);
492 * Is emitted if a context menu is requested for the item \a item,
493 * which is part of \a url. If the item is null, the context menu
494 * for the URL should be shown and the custom actions \a customActions
497 void requestContextMenu(const KFileItem
& item
,
499 const QList
<QAction
*>& customActions
);
502 * Is emitted if an information message with the content \a msg
505 void infoMessage(const QString
& msg
);
508 * Is emitted if an error message with the content \a msg
511 void errorMessage(const QString
& msg
);
514 * Is emitted if an "operation completed" message with the content \a msg
517 void operationCompletedMessage(const QString
& msg
);
520 * Is emitted after DolphinView::setUrl() has been invoked and
521 * the path \a url is currently loaded. If this signal is emitted,
522 * it is assured that the view contains already the correct root
523 * URL and property settings.
525 void startedPathLoading(const KUrl
& url
);
528 * Emitted when KDirLister emits redirection.
529 * Testcase: fish://localhost
531 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
534 /** @see QWidget::mouseReleaseEvent */
535 virtual void mouseReleaseEvent(QMouseEvent
* event
);
536 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
540 * Marks the view as active (DolphinView:isActive() will return true)
541 * and emits the 'activated' signal if it is not already active.
546 * If the item \a item is a directory, then this
547 * directory will be loaded. If the item is a file, the corresponding
548 * application will get started.
550 void triggerItem(const KFileItem
& index
);
553 * Emits the signal \a selectionChanged() with a small delay. This is
554 * because getting all file items for the signal can be an expensive
555 * operation. Fast selection changes are collected in this case and
556 * the signal is emitted only after no selection change has been done
557 * within a small delay.
559 void slotSelectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
);
562 * Is called by emitDelayedSelectionChangedSignal() and emits the
563 * signal \a selectionChanged() with all selected file items as parameter.
565 void emitSelectionChangedSignal();
568 * Opens the context menu on position \a pos. The position
569 * is used to check whether the context menu is related to an
570 * item or to the viewport.
572 void openContextMenu(const QPoint
& pos
, const QList
<QAction
*>& customActions
);
575 * Drops dragged URLs to the destination path \a destPath. If
576 * the URLs are dropped above an item inside the destination path,
577 * the item is indicated by \a destItem.
579 void dropUrls(const KFileItem
& destItem
,
580 const KUrl
& destPath
,
584 * Updates the view properties of the current URL to the
585 * sorting given by \a sorting.
587 void updateSorting(DolphinView::Sorting sorting
);
590 * Updates the view properties of the current URL to the
591 * sort order given by \a order.
593 void updateSortOrder(Qt::SortOrder order
);
596 * Updates the view properties of the current URL to the
597 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
599 void updateSortFoldersFirst(bool foldersFirst
);
602 * Updates the view properties of the current URL to the
603 * additional information given by \a info.
605 void updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
);
608 * Updates the status bar to show hover information for the
609 * item \a item. If currently other items are selected,
610 * no hover information is shown.
611 * @see DolphinView::clearHoverInformation()
613 void showHoverInformation(const KFileItem
& item
);
616 * Clears the hover information shown in the status bar.
617 * @see DolphinView::showHoverInformation().
619 void clearHoverInformation();
622 * Indicates in the status bar that the delete operation
623 * of the job \a job has been finished.
625 void slotDeleteFileFinished(KJob
* job
);
628 * Invoked when the directory lister has completed the loading of
629 * items. Assures that pasted items and renamed items get seleced.
631 void slotDirListerCompleted();
634 * Invoked when the loading of the directory is finished.
635 * Restores the active item and the scroll position if possible.
637 void slotLoadingCompleted();
640 * Is invoked when the KDirLister indicates refreshed items.
642 void slotRefreshItems();
645 * Observes the item with the URL \a url. As soon as the directory
646 * model indicates that the item is available, the item will
647 * get selected and it is assure that the item stays visible.
649 * @see selectAndScrollToCreatedItem()
651 void observeCreatedItem(const KUrl
& url
);
654 * Selects and scrolls to the item that got observed
655 * by observeCreatedItem().
657 void selectAndScrollToCreatedItem();
660 * Called when a redirection happens.
661 * Testcase: fish://localhost
663 void slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
666 * Restores the contents position, if history information about the old position is available.
668 void restoreContentsPosition();
671 void loadDirectory(const KUrl
& url
, bool reload
= false);
674 * Applies the view properties which are defined by the current URL
675 * to the DolphinView properties.
677 void applyViewProperties();
680 * Creates a new view representing the given view mode (DolphinView::mode()).
681 * The current view will get deleted.
688 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
689 * Pastes the clipboard data into the URL \a url.
691 void pasteToUrl(const KUrl
& url
);
694 * Checks whether the current item view has the same zoom level
695 * as \a oldZoomLevel. If this is not the case, the zoom level
696 * of the controller is updated and a zoomLevelChanged() signal
699 void updateZoomLevel(int oldZoomLevel
);
702 * Returns a list of URLs for all selected items. The list is
703 * simplified, so that when the URLs are part of different tree
704 * levels, only the parent is returned.
706 KUrl::List
simplifiedSelectedUrls() const;
709 * Returns the MIME data for all selected items.
711 QMimeData
* selectionMimeData() const;
714 * Is invoked after a paste operation or a drag & drop
715 * operation and adds the filenames of all URLs from \a mimeData to
716 * m_newFileNames. This allows to select all newly added
717 * items in slotDirListerCompleted().
719 void addNewFileNames(const QMimeData
* mimeData
);
723 * Abstracts the access to the different view implementations
724 * for icons-, details- and column-view.
729 ViewAccessor(DolphinSortFilterProxyModel
* proxyModel
);
732 void createView(QWidget
* parent
,
733 DolphinViewController
* dolphinViewController
,
734 const ViewModeController
* viewModeController
,
739 * Must be invoked before the URL has been changed and allows view implementations
740 * like the column view to create a new column.
742 void prepareUrlChange(const KUrl
& url
);
744 QAbstractItemView
* itemView() const;
745 KFileItemDelegate
* itemDelegate() const;
748 * Returns the widget that should be added to the layout as target. Usually
749 * the item view itself is returned, but in the case of the column view
750 * a container widget is returned.
752 QWidget
* layoutTarget() const;
754 KUrl
rootUrl() const;
756 bool supportsCategorizedSorting() const;
757 bool itemsExpandable() const;
758 QSet
<KUrl
> expandedUrls() const;
759 const DolphinDetailsViewExpander
* setExpandedUrls(const QSet
<KUrl
>& urlsToExpand
);
762 * Returns true, if a reloading of the items is required
763 * when the additional information properties have been changed
766 bool reloadOnAdditionalInfoChange() const;
768 DolphinModel
* dirModel() const;
769 DolphinSortFilterProxyModel
* proxyModel() const;
770 KDirLister
* dirLister() const;
773 DolphinIconsView
* m_iconsView
;
774 DolphinDetailsView
* m_detailsView
;
775 DolphinColumnViewContainer
* m_columnsContainer
;
776 DolphinSortFilterProxyModel
* m_proxyModel
;
777 QAbstractItemView
* m_dragSource
;
778 QPointer
<DolphinDetailsViewExpander
> m_detailsViewExpander
;
782 bool m_showPreview
: 1;
783 bool m_storedCategorizedSorting
: 1;
784 bool m_tabsForFiles
: 1;
785 bool m_isContextMenuOpen
: 1; // TODO: workaround for Qt-issue 207192
786 bool m_ignoreViewProperties
: 1;
787 bool m_assureVisibleCurrentIndex
: 1;
788 bool m_expanderActive
: 1;
792 QVBoxLayout
* m_topLayout
;
794 DolphinViewController
* m_dolphinViewController
;
795 ViewModeController
* m_viewModeController
;
796 ViewAccessor m_viewAccessor
;
798 QItemSelectionModel
* m_selectionModel
; // allow to switch views without losing the selection
799 QTimer
* m_selectionChangedTimer
;
802 KUrl m_activeItemUrl
;
803 QPoint m_restoredContentsPosition
;
804 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
805 KFileItemList m_selectedItems
; // this is used for making the View to remember selections after F5
808 * Remembers the filenames that have been added by a paste operation
809 * or a drag & drop operation. Allows to select the items in
810 * slotDirListerCompleted().
812 QSet
<QString
> m_newFileNames
;
815 /// Allow using DolphinView::Mode in QVariant
816 Q_DECLARE_METATYPE(DolphinView::Mode
)
818 #endif // DOLPHINVIEW_H