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 DolphinDetailsViewExpander
;
47 class DolphinIconsView
;
49 class DolphinSortFilterProxyModel
;
50 class DolphinViewController
;
52 class KActionCollection
;
55 class ViewModeController
;
60 * @short Represents a view for the directory content.
62 * View modes for icons, details and columns are supported. It's
69 * @see DolphinIconsView
70 * @see DolphinDetailsView
71 * @see DolphinColumnView
73 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
79 * Defines the view mode for a directory. The view mode
80 * can be defined when constructing a DolphinView. The
81 * view mode is automatically updated if the directory itself
82 * defines a view mode (see class ViewProperties for details).
87 * The directory items are shown as icons including an
93 * The icon, the name and at least the size of the directory
94 * items are shown in a table. It is possible to add columns
95 * for date, group and permissions.
100 * Each folder is shown in a separate column.
103 MaxModeEnum
= ColumnView
106 /** Defines the sort order for the items of a directory. */
118 MaxSortingEnum
= SortByPath
122 * @param parent Parent widget of the view.
123 * @param url Specifies the content which should be shown.
124 * @param proxyModel Used proxy model which specifies the sorting. The
125 * model is not owned by the view and won't get
128 DolphinView(QWidget
* parent
,
130 DolphinSortFilterProxyModel
* proxyModel
);
132 virtual ~DolphinView();
135 * Returns the current active URL, where all actions are applied.
136 * The URL navigator is synchronized with this URL.
141 * Returns the root URL of the view, which is defined as the first
142 * visible path of DolphinView::url(). Usually the root URL is
143 * equal to DolphinView::url(), but in the case of the column view
144 * when 2 columns are shown, the root URL might be:
145 * /home/peter/Documents
146 * and DolphinView::url() might return
147 * /home/peter/Documents/Music/
149 KUrl
rootUrl() const;
152 * If \a active is true, the view will marked as active. The active
153 * view is defined as view where all actions are applied to.
155 void setActive(bool active
);
156 bool isActive() const;
159 * Changes the view mode for the current directory to \a mode.
160 * If the view properties should be remembered for each directory
161 * (GeneralSettings::globalViewProps() returns false), then the
162 * changed view mode will be stored automatically.
164 void setMode(Mode mode
);
167 /** See setShowPreview */
168 bool showPreview() const;
170 /** See setShowHiddenFiles */
171 bool showHiddenFiles() const;
173 /** See setCategorizedSorting */
174 bool categorizedSorting() const;
177 * Returns true, if the categorized sorting is supported by the current
178 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
179 * supports categorizations. To check whether the categorized
180 * sorting is set, use DolphinView::categorizedSorting().
182 bool supportsCategorizedSorting() const;
185 * Returns the items of the view.
187 KFileItemList
items() const;
190 * Returns the selected items. The list is empty if no item has been
192 * @see DolphinView::selectedUrls()
194 KFileItemList
selectedItems() const;
197 * Returns the number of selected items (this is faster than
198 * invoking selectedItems().count()).
200 int selectedItemsCount() const;
203 * Marks the items indicated by \p urls to get selected after the
204 * directory DolphinView::url() has been loaded. Note that nothing
205 * gets selected if no loading of a directory has been triggered
206 * by DolphinView::setUrl() or DolphinView::reload().
208 void markUrlsAsSelected(const QList
<KUrl
>& urls
);
211 * All items that match to the pattern \a pattern will get selected
212 * if \a enabled is true and deselected if \a enabled is false.
214 void setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
);
217 * Sets the zoom level to \a level. It is assured that the used
218 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
219 * ZoomLevelInfo::maximumLevel().
221 void setZoomLevel(int level
);
222 int zoomLevel() const;
225 * Returns true, if zooming in is possible. If false is returned,
226 * the maximum zooming level has been reached.
228 bool isZoomInPossible() const;
231 * Returns true, if zooming out is possible. If false is returned,
232 * the minimum zooming level has been reached.
234 bool isZoomOutPossible() const;
236 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
237 void setSorting(Sorting sorting
);
239 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
240 Sorting
sorting() const;
242 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
243 void setSortOrder(Qt::SortOrder order
);
245 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
246 Qt::SortOrder
sortOrder() const;
248 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
249 void setSortFoldersFirst(bool foldersFirst
);
251 /** Returns if files and folders are sorted separately or not. */
252 bool sortFoldersFirst() const;
254 /** Sets the additional information which should be shown for the items. */
255 void setAdditionalInfo(KFileItemDelegate::InformationList info
);
257 /** Returns the additional information which should be shown for the items. */
258 KFileItemDelegate::InformationList
additionalInfo() const;
260 /** Reloads the current directory. */
266 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
267 * This method only needs to get invoked if the view settings for the Icons View,
268 * Details View or Columns View have been changed.
273 * Filters the currently shown items by \a nameFilter. All items
274 * which contain the given filter string will be shown.
276 void setNameFilter(const QString
& nameFilter
);
279 * Calculates the number of currently shown files into
280 * \a fileCount and the number of folders into \a folderCount.
281 * The size of all files is written into \a totalFileSize.
282 * It is recommend using this method instead of asking the
283 * directory lister or the model directly, as it takes
284 * filtering and hierarchical previews into account.
286 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
289 * Returns a textual representation of the state of the current
290 * folder or selected items, suitable for use in the status bar.
292 QString
statusBarText() const;
295 * Returns the version control actions that are provided for the items \p items.
296 * Usually the actions are presented in the context menu.
298 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
301 * Updates the state of the 'Additional Information' actions in \a collection.
303 void updateAdditionalInfoActions(KActionCollection
* collection
);
306 * Returns the state of the paste action:
307 * first is whether the action should be enabled
308 * second is the text for the action
310 QPair
<bool, QString
> pasteInfo() const;
313 * If \a tabsForFiles is true, the signal tabRequested() will also
314 * emitted also for files. Per default tabs for files is disabled
315 * and hence the signal tabRequested() will only be emitted for
318 void setTabsForFilesEnabled(bool tabsForFiles
);
319 bool isTabsForFilesEnabled() const;
322 * Returns true if the current view allows folders to be expanded,
323 * i.e. presents a hierarchical view to the user.
325 bool itemsExpandable() const;
328 * Restores the view state (current item, contents position, details view expansion state)
330 void restoreState(QDataStream
& stream
);
333 * Saves the view state (current item, contents position, details view expansion state)
335 void saveState(QDataStream
& stream
);
337 /** Returns true, if at least one item is selected. */
338 bool hasSelection() const;
342 * Changes the directory to \a url. If the current directory is equal to
343 * \a url, nothing will be done (use DolphinView::reload() instead).
345 void setUrl(const KUrl
& url
);
349 * @see DolphinView::selectedItems()
354 * Inverts the current selection: selected items get unselected,
355 * unselected items get selected.
356 * @see DolphinView::selectedItems()
358 void invertSelection();
360 void clearSelection();
363 * Triggers the renaming of the currently selected items, where
364 * the user must input a new name for the items.
366 void renameSelectedItems();
369 * Moves all selected items to the trash.
371 void trashSelectedItems();
374 * Deletes all selected items.
376 void deleteSelectedItems();
379 * Copies all selected items to the clipboard and marks
382 void cutSelectedItems();
384 /** Copies all selected items to the clipboard. */
385 void copySelectedItems();
387 /** Pastes the clipboard data to this view. */
391 * Pastes the clipboard data into the currently selected
392 * folder. If the current selection is not exactly one folder, no
393 * paste operation is done.
395 void pasteIntoFolder();
398 * Turns on the file preview for the all files of the current directory,
399 * if \a show is true.
400 * If the view properties should be remembered for each directory
401 * (GeneralSettings::globalViewProps() returns false), then the
402 * preview setting will be stored automatically.
404 void setShowPreview(bool show
);
407 * Shows all hidden files of the current directory,
408 * if \a show is true.
409 * If the view properties should be remembered for each directory
410 * (GeneralSettings::globalViewProps() returns false), then the
411 * show hidden file setting will be stored automatically.
413 void setShowHiddenFiles(bool show
);
416 * Summarizes all sorted items by their category \a categorized
418 * If the view properties should be remembered for each directory
419 * (GeneralSettings::globalViewProps() returns false), then the
420 * categorized sorting setting will be stored automatically.
422 void setCategorizedSorting(bool categorized
);
426 * Is emitted if the view has been activated by e. g. a mouse click.
430 /** Is emitted if URL of the view has been changed to \a url. */
431 void urlChanged(const KUrl
& url
);
434 * Is emitted when clicking on an item with the left mouse button.
436 void itemTriggered(const KFileItem
& item
);
439 * Is emitted if a new tab should be opened for the URL \a url.
441 void tabRequested(const KUrl
& url
);
444 * Is emitted if the view mode (IconsView, DetailsView,
445 * PreviewsView) has been changed.
449 /** Is emitted if the 'show preview' property has been changed. */
450 void showPreviewChanged();
452 /** Is emitted if the 'show hidden files' property has been changed. */
453 void showHiddenFilesChanged();
455 /** Is emitted if the 'categorized sorting' property has been changed. */
456 void categorizedSortingChanged();
458 /** Is emitted if the sorting by name, size or date has been changed. */
459 void sortingChanged(DolphinView::Sorting sorting
);
461 /** Is emitted if the sort order (ascending or descending) has been changed. */
462 void sortOrderChanged(Qt::SortOrder order
);
464 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
465 void sortFoldersFirstChanged(bool foldersFirst
);
467 /** Is emitted if the additional information shown for this view has been changed. */
468 void additionalInfoChanged();
470 /** Is emitted if the zoom level has been changed by zooming in or out. */
471 void zoomLevelChanged(int level
);
474 * Is emitted if information of an item is requested to be shown e. g. in the panel.
475 * If item is null, no item information request is pending.
477 void requestItemInfo(const KFileItem
& item
);
480 * Is emitted whenever the selection has been changed.
482 void selectionChanged(const KFileItemList
& selection
);
485 * Is emitted if a context menu is requested for the item \a item,
486 * which is part of \a url. If the item is null, the context menu
487 * for the URL should be shown and the custom actions \a customActions
490 void requestContextMenu(const KFileItem
& item
,
492 const QList
<QAction
*>& customActions
);
495 * Is emitted if an information message with the content \a msg
498 void infoMessage(const QString
& msg
);
501 * Is emitted if an error message with the content \a msg
504 void errorMessage(const QString
& msg
);
507 * Is emitted if an "operation completed" message with the content \a msg
510 void operationCompletedMessage(const QString
& msg
);
513 * Is emitted after DolphinView::setUrl() has been invoked and
514 * the path \a url is currently loaded. If this signal is emitted,
515 * it is assured that the view contains already the correct root
516 * URL and property settings.
518 void startedPathLoading(const KUrl
& url
);
521 * Is emitted after the path triggered by DolphinView::setUrl()
524 void finishedPathLoading(const KUrl
& url
);
527 * Emitted when KDirLister emits redirection.
528 * Testcase: fish://localhost
530 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
533 /** @see QWidget::mouseReleaseEvent */
534 virtual void mouseReleaseEvent(QMouseEvent
* event
);
535 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
539 * Marks the view as active (DolphinView:isActive() will return true)
540 * and emits the 'activated' signal if it is not already active.
545 * If the item \a item is a directory, then this
546 * directory will be loaded. If the item is a file, the corresponding
547 * application will get started.
549 void triggerItem(const KFileItem
& index
);
552 * Emits the signal \a selectionChanged() with a small delay. This is
553 * because getting all file items for the signal can be an expensive
554 * operation. Fast selection changes are collected in this case and
555 * the signal is emitted only after no selection change has been done
556 * within a small delay.
558 void slotSelectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
);
561 * Is called by emitDelayedSelectionChangedSignal() and emits the
562 * signal \a selectionChanged() with all selected file items as parameter.
564 void emitSelectionChangedSignal();
567 * Opens the context menu on position \a pos. The position
568 * is used to check whether the context menu is related to an
569 * item or to the viewport.
571 void openContextMenu(const QPoint
& pos
, const QList
<QAction
*>& customActions
);
574 * Drops dragged URLs to the destination path \a destPath. If
575 * the URLs are dropped above an item inside the destination path,
576 * the item is indicated by \a destItem.
578 void dropUrls(const KFileItem
& destItem
,
579 const KUrl
& destPath
,
583 * Updates the view properties of the current URL to the
584 * sorting given by \a sorting.
586 void updateSorting(DolphinView::Sorting sorting
);
589 * Updates the view properties of the current URL to the
590 * sort order given by \a order.
592 void updateSortOrder(Qt::SortOrder order
);
595 * Updates the view properties of the current URL to the
596 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
598 void updateSortFoldersFirst(bool foldersFirst
);
601 * Updates the view properties of the current URL to the
602 * additional information given by \a info.
604 void updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
);
607 * Updates the status bar to show hover information for the
608 * item \a item. If currently other items are selected,
609 * no hover information is shown.
610 * @see DolphinView::clearHoverInformation()
612 void showHoverInformation(const KFileItem
& item
);
615 * Clears the hover information shown in the status bar.
616 * @see DolphinView::showHoverInformation().
618 void clearHoverInformation();
621 * Indicates in the status bar that the delete operation
622 * of the job \a job has been finished.
624 void slotDeleteFileFinished(KJob
* job
);
627 * Invoked when the directory lister has completed the loading of
628 * items. Assures that pasted items and renamed items get seleced.
630 void slotDirListerCompleted();
633 * Invoked when the loading of the directory is finished.
634 * Restores the active item and the scroll position if possible.
636 void slotLoadingCompleted();
639 * Is invoked when the KDirLister indicates refreshed items.
641 void slotRefreshItems();
644 * Observes the item with the URL \a url. As soon as the directory
645 * model indicates that the item is available, the item will
646 * get selected and it is assure that the item stays visible.
648 * @see selectAndScrollToCreatedItem()
650 void observeCreatedItem(const KUrl
& url
);
653 * Selects and scrolls to the item that got observed
654 * by observeCreatedItem().
656 void selectAndScrollToCreatedItem();
659 * Called when a redirection happens.
660 * Testcase: fish://localhost
662 void slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
665 * Restores the contents position, if history information about the old position is available.
667 void restoreContentsPosition();
670 void loadDirectory(const KUrl
& url
, bool reload
= false);
673 * Applies the view properties which are defined by the current URL
674 * to the DolphinView properties.
676 void applyViewProperties();
679 * Creates a new view representing the given view mode (DolphinView::mode()).
680 * The current view will get deleted.
687 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
688 * Pastes the clipboard data into the URL \a url.
690 void pasteToUrl(const KUrl
& url
);
693 * Checks whether the current item view has the same zoom level
694 * as \a oldZoomLevel. If this is not the case, the zoom level
695 * of the controller is updated and a zoomLevelChanged() signal
698 void updateZoomLevel(int oldZoomLevel
);
701 * Returns a list of URLs for all selected items. The list is
702 * simplified, so that when the URLs are part of different tree
703 * levels, only the parent is returned.
705 KUrl::List
simplifiedSelectedUrls() const;
708 * Returns the MIME data for all selected items.
710 QMimeData
* selectionMimeData() const;
713 * Is invoked after a paste operation or a drag & drop
714 * operation and adds the filenames of all URLs from \a mimeData to
715 * m_newFileNames. This allows to select all newly added
716 * items in slotDirListerCompleted().
718 void addNewFileNames(const QMimeData
* mimeData
);
721 * Helper method for DolphinView::setItemSelectionEnabled(): Returns the selection for
722 * all items of \p parent that match with the regular expression defined by \p pattern.
724 QItemSelection
childrenMatchingPattern(const QModelIndex
& parent
, const QRegExp
& pattern
) const;
726 void connectViewAccessor();
727 void disconnectViewAccessor();
731 * Abstracts the access to the different view implementations
732 * for icons-, details- and column-view.
737 ViewAccessor(DolphinSortFilterProxyModel
* proxyModel
);
740 void createView(QWidget
* parent
,
741 DolphinViewController
* dolphinViewController
,
742 const ViewModeController
* viewModeController
,
747 * Must be invoked before the URL has been changed and allows view implementations
748 * like the column view to create a new column.
750 void prepareUrlChange(const KUrl
& url
);
752 QAbstractItemView
* itemView() const;
753 KFileItemDelegate
* itemDelegate() const;
756 * Returns the widget that should be added to the layout as target. Usually
757 * the item view itself is returned, but in the case of the column view
758 * a container widget is returned.
760 QWidget
* layoutTarget() const;
762 void setRootUrl(const KUrl
& rootUrl
);
763 KUrl
rootUrl() const;
765 bool supportsCategorizedSorting() const;
766 bool itemsExpandable() const;
767 QSet
<KUrl
> expandedUrls() const;
768 const DolphinDetailsViewExpander
* setExpandedUrls(const QSet
<KUrl
>& urlsToExpand
);
771 * Returns true, if a reloading of the items is required
772 * when the additional information properties have been changed
775 bool reloadOnAdditionalInfoChange() const;
777 DolphinModel
* dirModel() const;
778 DolphinSortFilterProxyModel
* proxyModel() const;
779 KDirLister
* dirLister() const;
783 DolphinIconsView
* m_iconsView
;
784 DolphinDetailsView
* m_detailsView
;
785 DolphinColumnViewContainer
* m_columnsContainer
;
786 DolphinSortFilterProxyModel
* m_proxyModel
;
787 QAbstractItemView
* m_dragSource
;
788 QPointer
<DolphinDetailsViewExpander
> m_detailsViewExpander
;
792 bool m_showPreview
: 1;
793 bool m_storedCategorizedSorting
: 1;
794 bool m_tabsForFiles
: 1;
795 bool m_isContextMenuOpen
: 1; // TODO: workaround for Qt-issue 207192
796 bool m_assureVisibleCurrentIndex
: 1;
797 bool m_expanderActive
: 1;
801 QVBoxLayout
* m_topLayout
;
803 DolphinViewController
* m_dolphinViewController
;
804 ViewModeController
* m_viewModeController
;
805 ViewAccessor m_viewAccessor
;
807 QTimer
* m_selectionChangedTimer
;
809 KUrl m_activeItemUrl
;
810 QPoint m_restoredContentsPosition
;
811 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
812 KFileItemList m_selectedItems
; // this is used for making the View to remember selections after F5
815 * Remembers the filenames that have been added by a paste operation
816 * or a drag & drop operation. Allows to select the items in
817 * slotDirListerCompleted().
819 QSet
<QString
> m_newFileNames
;
822 friend class TestBase
;
825 /// Allow using DolphinView::Mode in QVariant
826 Q_DECLARE_METATYPE(DolphinView::Mode
)
828 #endif // DOLPHINVIEW_H