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 DolphinController
;
45 class DolphinColumnViewContainer
;
46 class DolphinDetailsView
;
47 class DolphinIconsView
;
48 class DolphinMainWindow
;
50 class DolphinSortFilterProxyModel
;
51 class KFilePreviewGenerator
;
53 class KActionCollection
;
57 class DolphinDetailsViewExpander
;
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. */
116 MaxSortEnum
= SortByType
120 * @param parent Parent widget of the view.
121 * @param url Specifies the content which should be shown.
122 * @param proxyModel Used proxy model which specifies the sorting. The
123 * model is not owned by the view and won't get
126 DolphinView(QWidget
* parent
,
128 DolphinSortFilterProxyModel
* proxyModel
);
130 virtual ~DolphinView();
133 * Returns the current active URL, where all actions are applied.
134 * The URL navigator is synchronized with this URL.
136 const KUrl
& url() const;
139 * Returns the root URL of the view, which is defined as the first
140 * visible path of DolphinView::url(). Usually the root URL is
141 * equal to DolphinView::url(), but in the case of the column view
142 * when 2 columns are shown, the root URL might be:
143 * /home/peter/Documents
144 * and DolphinView::url() might return
145 * /home/peter/Documents/Music/
147 KUrl
rootUrl() const;
150 * If \a active is true, the view will marked as active. The active
151 * view is defined as view where all actions are applied to.
153 void setActive(bool active
);
154 bool isActive() const;
157 * Changes the view mode for the current directory to \a mode.
158 * If the view properties should be remembered for each directory
159 * (GeneralSettings::globalViewProps() returns false), then the
160 * changed view mode will be stored automatically.
162 void setMode(Mode mode
);
165 /** See setShowPreview */
166 bool showPreview() const;
168 /** See setShowHiddenFiles */
169 bool showHiddenFiles() const;
171 /** See setCategorizedSorting */
172 bool categorizedSorting() const;
175 * Returns true, if the categorized sorting is supported by the current
176 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
177 * supports categorizations. To check whether the categorized
178 * sorting is set, use DolphinView::categorizedSorting().
180 bool supportsCategorizedSorting() const;
183 * Returns the selected items. The list is empty if no item has been
185 * @see DolphinView::selectedUrls()
187 KFileItemList
selectedItems() const;
190 * Returns a list of URLs for all selected items. An empty list
191 * is returned, if no item is selected.
192 * @see DolphinView::selectedItems()
194 KUrl::List
selectedUrls() const;
197 * Returns the number of selected items (this is faster than
198 * invoking selectedItems().count()).
200 int selectedItemsCount() const;
202 QItemSelectionModel
* selectionModel() const;
205 * Sets the upper left position of the view content
206 * to (x,y). The content of the view might be larger than the visible area
207 * and hence a scrolling must be done.
209 void setContentsPosition(int x
, int y
);
212 * Sets the upper left position of the view content
213 * to (x,y) after the directory loading is finished.
214 * This is useful when going back or forward in history.
216 void setRestoredContentsPosition(const QPoint
& pos
);
218 /** Returns the upper left position of the view content. */
219 QPoint
contentsPosition() const;
222 * Sets the zoom level to \a level. It is assured that the used
223 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
224 * ZoomLevelInfo::maximumLevel().
226 void setZoomLevel(int level
);
227 int zoomLevel() const;
230 * Returns true, if zooming in is possible. If false is returned,
231 * the maximum zooming level has been reached.
233 bool isZoomInPossible() const;
236 * Returns true, if zooming out is possible. If false is returned,
237 * the minimum zooming level has been reached.
239 bool isZoomOutPossible() const;
241 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
242 void setSorting(Sorting sorting
);
244 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
245 Sorting
sorting() const;
247 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
248 void setSortOrder(Qt::SortOrder order
);
250 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
251 Qt::SortOrder
sortOrder() const;
253 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
254 void setSortFoldersFirst(bool foldersFirst
);
256 /** Returns if files and folders are sorted separately or not. */
257 bool sortFoldersFirst() const;
259 /** Sets the additional information which should be shown for the items. */
260 void setAdditionalInfo(KFileItemDelegate::InformationList info
);
262 /** Returns the additional information which should be shown for the items. */
263 KFileItemDelegate::InformationList
additionalInfo() const;
265 /** Reloads the current directory. */
269 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
270 * This method only needs to get invoked if the view settings for the Icons View,
271 * Details View or Columns View have been changed.
276 * Changes the directory of the view to \a url. If \a rootUrl is empty, the view
277 * properties from \a url are used for adjusting the view mode and the other properties.
278 * If \a rootUrl is not empty, the view properties from the root URL are considered
279 * instead. Specifying a root URL is only required if a view having a different root URL
280 * (e. g. the column view) should be restored. Usually using DolphinView::setUrl()
281 * is enough for changing the current URL.
283 void updateView(const KUrl
& url
, const KUrl
& rootUrl
);
286 * Filters the currently shown items by \a nameFilter. All items
287 * which contain the given filter string will be shown.
289 void setNameFilter(const QString
& nameFilter
);
292 * Calculates the number of currently shown files into
293 * \a fileCount and the number of folders into \a folderCount.
294 * The size of all files is written into \a totalFileSize.
295 * It is recommend using this method instead of asking the
296 * directory lister or the model directly, as it takes
297 * filtering and hierarchical previews into account.
299 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
302 * Returns a textual representation of the state of the current
303 * folder or selected items, suitable for use in the status bar.
305 QString
statusBarText() const;
308 * Returns the version control actions that are provided for the items \p items.
309 * Usually the actions are presented in the context menu.
311 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
314 * Updates the state of the 'Additional Information' actions in \a collection.
316 void updateAdditionalInfoActions(KActionCollection
* collection
);
319 * Returns the state of the paste action:
320 * first is whether the action should be enabled
321 * second is the text for the action
323 QPair
<bool, QString
> pasteInfo() const;
326 * If \a tabsForFiles is true, the signal tabRequested() will also
327 * emitted also for files. Per default tabs for files is disabled
328 * and hence the signal tabRequested() will only be emitted for
331 void setTabsForFilesEnabled(bool tabsForFiles
);
332 bool isTabsForFilesEnabled() const;
335 * Marks the item \a url as active item as soon as it has
336 * been loaded by the directory lister. This is useful mark
337 * the previously visited directory as active when going
338 * back in history (the URL is known, but the item is not
341 void activateItem(const KUrl
& url
);
344 * Returns true if the current view allows folders to be expanded,
345 * i.e. presents a hierarchical view to the user.
347 bool itemsExpandable() const;
350 * Restores the view state (current item, contents position, details view expansion state)
352 void restoreState(QDataStream
&stream
);
355 * Saves the view state (current item, contents position, details view expansion state)
357 void saveState(QDataStream
&stream
);
361 * Changes the directory to \a url. If the current directory is equal to
362 * \a url, nothing will be done (use DolphinView::reload() instead).
364 void setUrl(const KUrl
& url
);
368 * @see DolphinView::selectedItems()
373 * Inverts the current selection: selected items get unselected,
374 * unselected items get selected.
375 * @see DolphinView::selectedItems()
377 void invertSelection();
379 /** Returns true, if at least one item is selected. */
380 bool hasSelection() const;
382 void clearSelection();
385 * Request of a selection change. The view will do its best to accommodate
386 * the request, but it is not guaranteed that all items in \a selection
387 * will actually get selected. The view will e.g. not select items which
388 * are not in the currently displayed folder.
390 void changeSelection(const KFileItemList
& selection
);
393 * Triggers the renaming of the currently selected items, where
394 * the user must input a new name for the items.
396 void renameSelectedItems();
399 * Moves all selected items to the trash.
401 void trashSelectedItems();
404 * Deletes all selected items.
406 void deleteSelectedItems();
409 * Copies all selected items to the clipboard and marks
412 void cutSelectedItems();
414 /** Copies all selected items to the clipboard. */
415 void copySelectedItems();
417 /** Pastes the clipboard data to this view. */
421 * Pastes the clipboard data into the currently selected
422 * folder. If the current selection is not exactly one folder, no
423 * paste operation is done.
425 void pasteIntoFolder();
428 * Turns on the file preview for the all files of the current directory,
429 * if \a show is true.
430 * If the view properties should be remembered for each directory
431 * (GeneralSettings::globalViewProps() returns false), then the
432 * preview setting will be stored automatically.
434 void setShowPreview(bool show
);
437 * Shows all hidden files of the current directory,
438 * if \a show is true.
439 * If the view properties should be remembered for each directory
440 * (GeneralSettings::globalViewProps() returns false), then the
441 * show hidden file setting will be stored automatically.
443 void setShowHiddenFiles(bool show
);
446 * Summarizes all sorted items by their category \a categorized
448 * If the view properties should be remembered for each directory
449 * (GeneralSettings::globalViewProps() returns false), then the
450 * categorized sorting setting will be stored automatically.
452 void setCategorizedSorting(bool categorized
);
454 /** Switches between an ascending and descending sorting order. */
455 void toggleSortOrder();
457 /** Switches between a separate sorting (with folders first) and a mixed sorting of files and folders. */
458 void toggleSortFoldersFirst();
461 * Switches on or off the displaying of additional information
462 * as specified by \a action.
464 void toggleAdditionalInfo(QAction
* action
);
468 * Is emitted if the view has been activated by e. g. a mouse click.
472 /** Is emitted if URL of the view has been changed to \a url. */
473 void urlChanged(const KUrl
& url
);
476 * Is emitted if the view requests a changing of the current
477 * URL to \a url (see DolphinController::triggerUrlChangeRequest()).
479 void requestUrlChange(const KUrl
& url
);
482 * Is emitted when clicking on an item with the left mouse button.
484 void itemTriggered(const KFileItem
& item
);
487 * Is emitted if a new tab should be opened for the URL \a url.
489 void tabRequested(const KUrl
& url
);
492 * Is emitted if the view mode (IconsView, DetailsView,
493 * PreviewsView) has been changed.
497 /** Is emitted if the 'show preview' property has been changed. */
498 void showPreviewChanged();
500 /** Is emitted if the 'show hidden files' property has been changed. */
501 void showHiddenFilesChanged();
503 /** Is emitted if the 'categorized sorting' property has been changed. */
504 void categorizedSortingChanged();
506 /** Is emitted if the sorting by name, size or date has been changed. */
507 void sortingChanged(DolphinView::Sorting sorting
);
509 /** Is emitted if the sort order (ascending or descending) has been changed. */
510 void sortOrderChanged(Qt::SortOrder order
);
512 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
513 void sortFoldersFirstChanged(bool foldersFirst
);
515 /** Is emitted if the additional information shown for this view has been changed. */
516 void additionalInfoChanged();
518 /** Is emitted if the zoom level has been changed by zooming in or out. */
519 void zoomLevelChanged(int level
);
522 * Is emitted if information of an item is requested to be shown e. g. in the panel.
523 * If item is null, no item information request is pending.
525 void requestItemInfo(const KFileItem
& item
);
527 /** Is emitted if the contents has been moved to \a x, \a y. */
528 void contentsMoved(int x
, int y
);
531 * Is emitted whenever the selection has been changed.
533 void selectionChanged(const KFileItemList
& selection
);
536 * Is emitted if a context menu is requested for the item \a item,
537 * which is part of \a url. If the item is null, the context menu
538 * for the URL should be shown and the custom actions \a customActions
541 void requestContextMenu(const KFileItem
& item
,
543 const QList
<QAction
*>& customActions
);
546 * Is emitted if an information message with the content \a msg
549 void infoMessage(const QString
& msg
);
552 * Is emitted if an error message with the content \a msg
555 void errorMessage(const QString
& msg
);
558 * Is emitted if an "operation completed" message with the content \a msg
561 void operationCompletedMessage(const QString
& msg
);
564 * Is emitted after DolphinView::setUrl() has been invoked and
565 * the path \a url is currently loaded. If this signal is emitted,
566 * it is assured that the view contains already the correct root
567 * URL and property settings.
569 void startedPathLoading(const KUrl
& url
);
572 * Emitted when KDirLister emits redirection.
573 * Testcase: fish://localhost
575 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
578 /** @see QWidget::mouseReleaseEvent */
579 virtual void mouseReleaseEvent(QMouseEvent
* event
);
580 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
584 * Marks the view as active (DolphinView:isActive() will return true)
585 * and emits the 'activated' signal if it is not already active.
590 * If the item \a item is a directory, then this
591 * directory will be loaded. If the item is a file, the corresponding
592 * application will get started.
594 void triggerItem(const KFileItem
& index
);
597 * Emits the signal \a selectionChanged() with a small delay. This is
598 * because getting all file items for the signal can be an expensive
599 * operation. Fast selection changes are collected in this case and
600 * the signal is emitted only after no selection change has been done
601 * within a small delay.
603 void emitDelayedSelectionChangedSignal();
606 * Is called by emitDelayedSelectionChangedSignal() and emits the
607 * signal \a selectionChanged() with all selected file items as parameter.
609 void emitSelectionChangedSignal();
612 * Opens the context menu on position \a pos. The position
613 * is used to check whether the context menu is related to an
614 * item or to the viewport.
616 void openContextMenu(const QPoint
& pos
, const QList
<QAction
*>& customActions
);
619 * Drops dragged URLs to the destination path \a destPath. If
620 * the URLs are dropped above an item inside the destination path,
621 * the item is indicated by \a destItem.
623 void dropUrls(const KFileItem
& destItem
,
624 const KUrl
& destPath
,
628 * Updates the view properties of the current URL to the
629 * sorting given by \a sorting.
631 void updateSorting(DolphinView::Sorting sorting
);
634 * Updates the view properties of the current URL to the
635 * sort order given by \a order.
637 void updateSortOrder(Qt::SortOrder order
);
640 * Updates the view properties of the current URL to the
641 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
643 void updateSortFoldersFirst(bool foldersFirst
);
646 * Updates the view properties of the current URL to the
647 * additional information given by \a info.
649 void updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
);
652 * Emits the signal contentsMoved with the current coordinates
653 * of the viewport as parameters.
655 void emitContentsMoved();
658 * Updates the status bar to show hover information for the
659 * item \a item. If currently other items are selected,
660 * no hover information is shown.
661 * @see DolphinView::clearHoverInformation()
663 void showHoverInformation(const KFileItem
& item
);
666 * Clears the hover information shown in the status bar.
667 * @see DolphinView::showHoverInformation().
669 void clearHoverInformation();
672 * Indicates in the status bar that the delete operation
673 * of the job \a job has been finished.
675 void slotDeleteFileFinished(KJob
* job
);
678 * Is emitted if the controller requests a changing of the current
681 void slotRequestUrlChange(const KUrl
& url
);
684 * Invoked when the directory lister has completed the loading of
685 * items. Assures that pasted items and renamed items get seleced.
687 void slotDirListerCompleted();
690 * Invoked when the loading of the directory is finished.
691 * Restores the active item and the scroll position if possible.
693 void slotLoadingCompleted();
696 * Is invoked when the KDirLister indicates refreshed items.
698 void slotRefreshItems();
701 * Observes the item with the URL \a url. As soon as the directory
702 * model indicates that the item is available, the item will
703 * get selected and it is assure that the item stays visible.
705 * @see selectAndScrollToCreatedItem()
707 void observeCreatedItem(const KUrl
& url
);
710 * Selects and scrolls to the item that got observed
711 * by observeCreatedItem().
713 void selectAndScrollToCreatedItem();
716 * Called when a redirection happens.
717 * Testcase: fish://localhost
719 void slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
722 * Restores the contents position, if history information about the old position is available.
724 void restoreContentsPosition();
727 void loadDirectory(const KUrl
& url
, bool reload
= false);
730 * Applies the view properties which are defined by the current URL
731 * to the DolphinView properties.
733 void applyViewProperties();
736 * Creates a new view representing the given view mode (DolphinView::mode()).
737 * The current view will get deleted.
744 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
745 * Pastes the clipboard data into the URL \a url.
747 void pasteToUrl(const KUrl
& url
);
750 * Checks whether the current item view has the same zoom level
751 * as \a oldZoomLevel. If this is not the case, the zoom level
752 * of the controller is updated and a zoomLevelChanged() signal
755 void updateZoomLevel(int oldZoomLevel
);
758 * Returns a list of URLs for all selected items. The list is
759 * simplified, so that when the URLs are part of different tree
760 * levels, only the parent is returned.
762 KUrl::List
simplifiedSelectedUrls() const;
765 * Returns the MIME data for all selected items.
767 QMimeData
* selectionMimeData() const;
770 * Is invoked after a paste operation or a drag & drop
771 * operation and adds the filenames of all URLs from \a mimeData to
772 * m_newFileNames. This allows to select all newly added
773 * items in slotDirListerCompleted().
775 void addNewFileNames(const QMimeData
* mimeData
);
779 * Abstracts the access to the different view implementations
780 * for icons-, details- and column-view.
785 ViewAccessor(DolphinSortFilterProxyModel
* proxyModel
);
788 void createView(QWidget
* parent
, DolphinController
* controller
, Mode mode
);
792 * Must be invoked before the URL has been changed and allows view implementations
793 * like the column view to create a new column.
795 void prepareUrlChange(const KUrl
& url
);
797 QAbstractItemView
* itemView() const;
798 KFileItemDelegate
* itemDelegate() const;
801 * Returns the widget that should be added to the layout as target. Usually
802 * the item view itself is returned, but in the case of the column view
803 * a container widget is returned.
805 QWidget
* layoutTarget() const;
807 KUrl
rootUrl() const;
809 bool supportsCategorizedSorting() const;
810 bool itemsExpandable() const;
811 QSet
<KUrl
> expandedUrls() const;
812 const DolphinDetailsViewExpander
* setExpandedUrls(const QSet
<KUrl
>& urlsToExpand
);
815 * Returns true, if a reloading of the items is required
816 * when the additional information properties have been changed
819 bool reloadOnAdditionalInfoChange() const;
821 DolphinModel
* dirModel() const;
822 DolphinSortFilterProxyModel
* proxyModel() const;
823 KDirLister
* dirLister() const;
826 DolphinIconsView
* m_iconsView
;
827 DolphinDetailsView
* m_detailsView
;
828 DolphinColumnViewContainer
* m_columnsContainer
;
829 DolphinSortFilterProxyModel
* m_proxyModel
;
830 QAbstractItemView
* m_dragSource
;
831 QPointer
<DolphinDetailsViewExpander
> m_detailsViewExpander
;
835 bool m_showPreview
: 1;
836 bool m_loadingDirectory
: 1;
837 bool m_storedCategorizedSorting
: 1;
838 bool m_tabsForFiles
: 1;
839 bool m_isContextMenuOpen
: 1; // TODO: workaround for Qt-issue 207192
840 bool m_ignoreViewProperties
: 1;
841 bool m_assureVisibleCurrentIndex
: 1;
842 bool m_expanderActive
: 1;
846 DolphinMainWindow
* m_mainWindow
;
847 QVBoxLayout
* m_topLayout
;
849 DolphinController
* m_controller
;
850 ViewAccessor m_viewAccessor
;
852 QItemSelectionModel
* m_selectionModel
; // allow to switch views without losing the selection
853 QTimer
* m_selectionChangedTimer
;
856 KUrl m_activeItemUrl
;
857 QPoint m_restoredContentsPosition
;
858 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
859 KFileItemList m_selectedItems
; // this is used for making the View to remember selections after F5
862 * Remembers the filenames that have been added by a paste operation
863 * or a drag & drop operation. Allows to select the items in
864 * slotDirListerCompleted().
866 QSet
<QString
> m_newFileNames
;
869 /// Allow using DolphinView::Mode in QVariant
870 Q_DECLARE_METATYPE(DolphinView::Mode
)
872 #endif // DOLPHINVIEW_H