1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
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 ***************************************************************************/
24 #include "dolphin_export.h"
28 #include <config-baloo.h>
29 #include <kio/fileundomanager.h>
30 #include <kparts/part.h>
36 typedef KIO::FileUndoManager::CommandType CommandType
;
38 class DolphinItemListView
;
40 class KItemListContainer
;
44 class VersionControlObserver
;
46 class QGraphicsSceneDragDropEvent
;
50 * @short Represents a view for the directory content.
52 * View modes for icons, compact and details are supported. It's
60 class DOLPHIN_EXPORT DolphinView
: public QWidget
66 * Defines the view mode for a directory. The
67 * view mode is automatically updated if the directory itself
68 * defines a view mode (see class ViewProperties for details).
73 * The items are shown as icons with a name-label below.
78 * The icon, the name and the size of the items are
79 * shown per default as a table.
84 * The items are shown as icons with the name-label aligned
91 * @param url Specifies the content which should be shown.
92 * @param parent Parent widget of the view.
94 DolphinView(const QUrl
& url
, QWidget
* parent
);
96 ~DolphinView() override
;
99 * Returns the current active URL, where all actions are applied.
100 * The URL navigator is synchronized with this URL.
105 * If \a active is true, the view will marked as active. The active
106 * view is defined as view where all actions are applied to.
108 void setActive(bool active
);
109 bool isActive() const;
112 * Changes the view mode for the current directory to \a mode.
113 * If the view properties should be remembered for each directory
114 * (GeneralSettings::globalViewProps() returns false), then the
115 * changed view mode will be stored automatically.
117 void setMode(Mode mode
);
121 * Turns on the file preview for the all files of the current directory,
122 * if \a show is true.
123 * If the view properties should be remembered for each directory
124 * (GeneralSettings::globalViewProps() returns false), then the
125 * preview setting will be stored automatically.
127 void setPreviewsShown(bool show
);
128 bool previewsShown() const;
131 * Shows all hidden files of the current directory,
132 * if \a show is true.
133 * If the view properties should be remembered for each directory
134 * (GeneralSettings::globalViewProps() returns false), then the
135 * show hidden file setting will be stored automatically.
137 void setHiddenFilesShown(bool show
);
138 bool hiddenFilesShown() const;
141 * Turns on sorting by groups if \a enable is true.
143 void setGroupedSorting(bool grouped
);
144 bool groupedSorting() const;
147 * Returns the items of the view.
149 KFileItemList
items() const;
152 * @return The number of items. itemsCount() is faster in comparison
153 * to items().count().
155 int itemsCount() const;
158 * Returns the selected items. The list is empty if no item has been
161 KFileItemList
selectedItems() const;
164 * Returns the number of selected items (this is faster than
165 * invoking selectedItems().count()).
167 int selectedItemsCount() const;
170 * Marks the items indicated by \p urls to get selected after the
171 * directory DolphinView::url() has been loaded. Note that nothing
172 * gets selected if no loading of a directory has been triggered
173 * by DolphinView::setUrl() or DolphinView::reload().
175 void markUrlsAsSelected(const QList
<QUrl
> &urls
);
178 * Marks the item indicated by \p url to be scrolled to and as the
179 * current item after directory DolphinView::url() has been loaded.
181 void markUrlAsCurrent(const QUrl
& url
);
184 * All items that match to the pattern \a pattern will get selected
185 * if \a enabled is true and deselected if \a enabled is false.
187 void selectItems(const QRegExp
& pattern
, bool enabled
);
190 * Sets the zoom level to \a level. It is assured that the used
191 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
192 * ZoomLevelInfo::maximumLevel().
194 void setZoomLevel(int level
);
195 int zoomLevel() const;
197 void setSortRole(const QByteArray
& role
);
198 QByteArray
sortRole() const;
200 void setSortOrder(Qt::SortOrder order
);
201 Qt::SortOrder
sortOrder() const;
203 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
204 void setSortFoldersFirst(bool foldersFirst
);
205 bool sortFoldersFirst() const;
207 /** Sets the additional information which should be shown for the items. */
208 void setVisibleRoles(const QList
<QByteArray
>& roles
);
210 /** Returns the additional information which should be shown for the items. */
211 QList
<QByteArray
> visibleRoles() const;
216 * Refreshes the view to get synchronized with the settings (e.g. icons size,
222 * Saves the current settings (e.g. icons size, font, ..).
224 void writeSettings();
227 * Filters the currently shown items by \a nameFilter. All items
228 * which contain the given filter string will be shown.
230 void setNameFilter(const QString
& nameFilter
);
231 QString
nameFilter() const;
234 * Filters the currently shown items by \a filters. All items
235 * whose content-type matches those given by the list of filters
238 void setMimeTypeFilters(const QStringList
& filters
);
239 QStringList
mimeTypeFilters() const;
242 * Returns a textual representation of the state of the current
243 * folder or selected items, suitable for use in the status bar.
245 QString
statusBarText() const;
248 * Returns the version control actions that are provided for the items \p items.
249 * Usually the actions are presented in the context menu.
251 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
254 * Returns the state of the paste action:
255 * first is whether the action should be enabled
256 * second is the text for the action
258 QPair
<bool, QString
> pasteInfo() const;
261 * If \a tabsForFiles is true, the signal tabRequested() will also
262 * emitted also for files. Per default tabs for files is disabled
263 * and hence the signal tabRequested() will only be emitted for
266 void setTabsForFilesEnabled(bool tabsForFiles
);
267 bool isTabsForFilesEnabled() const;
270 * Returns true if the current view allows folders to be expanded,
271 * i.e. presents a hierarchical view to the user.
273 bool itemsExpandable() const;
276 * Restores the view state (current item, contents position, details view expansion state)
278 void restoreState(QDataStream
& stream
);
281 * Saves the view state (current item, contents position, details view expansion state)
283 void saveState(QDataStream
& stream
);
286 * Returns the root item which represents the current URL.
288 KFileItem
rootItem() const;
291 * Sets a context that is used for remembering the view-properties.
292 * Per default the context is empty and the path of the currently set URL
293 * is used for remembering the view-properties. Setting a custom context
294 * makes sense if specific types of URLs (e.g. search-URLs) should
295 * share common view-properties.
297 void setViewPropertiesContext(const QString
& context
);
298 QString
viewPropertiesContext() const;
301 * Checks if the given \a item can be opened as folder (e.g. archives).
302 * This function will also adjust the \a url (e.g. change the protocol).
303 * @return a valid and adjusted url if the item can be opened as folder,
304 * otherwise return an empty url.
306 static QUrl
openItemAsFolderUrl(const KFileItem
& item
, const bool browseThroughArchives
= true);
310 * Changes the directory to \a url. If the current directory is equal to
311 * \a url, nothing will be done (use DolphinView::reload() instead).
313 void setUrl(const QUrl
& url
);
317 * @see DolphinView::selectedItems()
322 * Inverts the current selection: selected items get unselected,
323 * unselected items get selected.
324 * @see DolphinView::selectedItems()
326 void invertSelection();
328 void clearSelection();
331 * Triggers the renaming of the currently selected items, where
332 * the user must input a new name for the items.
334 void renameSelectedItems();
337 * Moves all selected items to the trash.
339 void trashSelectedItems();
342 * Deletes all selected items.
344 void deleteSelectedItems();
347 * Copies all selected items to the clipboard and marks
350 void cutSelectedItems();
352 /** Copies all selected items to the clipboard. */
353 void copySelectedItems();
355 /** Pastes the clipboard data to this view. */
359 * Pastes the clipboard data into the currently selected
360 * folder. If the current selection is not exactly one folder, no
361 * paste operation is done.
363 void pasteIntoFolder();
366 * Handles a drop of @p dropEvent onto widget @p dropWidget and destination @p destUrl
368 void dropUrls(const QUrl
&destUrl
, QDropEvent
*dropEvent
, QWidget
*dropWidget
);
372 /** Activates the view if the item list container gets focus. */
373 bool eventFilter(QObject
* watched
, QEvent
* event
) override
;
377 * Is emitted if the view has been activated by e. g. a mouse click.
381 /** Is emitted if the URL of the view has been changed to \a url. */
382 void urlChanged(const QUrl
& url
);
385 * Is emitted when clicking on an item with the left mouse button.
387 void itemActivated(const KFileItem
& item
);
390 * Is emitted when multiple items have been activated by e. g.
391 * context menu open with.
393 void itemsActivated(const KFileItemList
& items
);
396 * Is emitted if items have been added or deleted.
398 void itemCountChanged();
401 * Is emitted if a new tab should be opened for the URL \a url.
403 void tabRequested(const QUrl
& url
);
406 * Is emitted if the view mode (IconsView, DetailsView,
407 * PreviewsView) has been changed.
409 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
411 /** Is emitted if the 'show preview' property has been changed. */
412 void previewsShownChanged(bool shown
);
414 /** Is emitted if the 'show hidden files' property has been changed. */
415 void hiddenFilesShownChanged(bool shown
);
417 /** Is emitted if the 'grouped sorting' property has been changed. */
418 void groupedSortingChanged(bool groupedSorting
);
420 /** Is emitted if the sorting by name, size or date has been changed. */
421 void sortRoleChanged(const QByteArray
& role
);
423 /** Is emitted if the sort order (ascending or descending) has been changed. */
424 void sortOrderChanged(Qt::SortOrder order
);
427 * Is emitted if the sorting of files and folders (separate with folders
428 * first or mixed) has been changed.
430 void sortFoldersFirstChanged(bool foldersFirst
);
432 /** Is emitted if the additional information shown for this view has been changed. */
433 void visibleRolesChanged(const QList
<QByteArray
>& current
,
434 const QList
<QByteArray
>& previous
);
436 /** Is emitted if the zoom level has been changed by zooming in or out. */
437 void zoomLevelChanged(int current
, int previous
);
440 * Is emitted if information of an item is requested to be shown e. g. in the panel.
441 * If item is null, no item information request is pending.
443 void requestItemInfo(const KFileItem
& item
);
446 * Is emitted whenever the selection has been changed.
448 void selectionChanged(const KFileItemList
& selection
);
451 * Is emitted if a context menu is requested for the item \a item,
452 * which is part of \a url. If the item is null, the context menu
453 * for the URL should be shown and the custom actions \a customActions
456 void requestContextMenu(const QPoint
& pos
,
457 const KFileItem
& item
,
459 const QList
<QAction
*>& customActions
);
462 * Is emitted if an information message with the content \a msg
465 void infoMessage(const QString
& msg
);
468 * Is emitted if an error message with the content \a msg
471 void errorMessage(const QString
& msg
);
474 * Is emitted if an "operation completed" message with the content \a msg
477 void operationCompletedMessage(const QString
& msg
);
480 * Is emitted after DolphinView::setUrl() has been invoked and
481 * the current directory is loaded. If this signal is emitted,
482 * it is assured that the view contains already the correct root
483 * URL and property settings.
485 void directoryLoadingStarted();
488 * Is emitted after the directory triggered by DolphinView::setUrl()
491 void directoryLoadingCompleted();
494 * Is emitted after the directory loading triggered by DolphinView::setUrl()
497 void directoryLoadingCanceled();
500 * Is emitted after DolphinView::setUrl() has been invoked and provides
501 * the information how much percent of the current directory have been loaded.
503 void directoryLoadingProgress(int percent
);
506 * Is emitted if the sorting is done asynchronously and provides the
507 * progress information of the sorting.
509 void directorySortingProgress(int percent
);
512 * Emitted when the file-item-model emits redirection.
513 * Testcase: fish://localhost
515 void redirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
518 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
519 * In this case no signal errorMessage() will be emitted.
521 void urlIsFileError(const QUrl
& url
);
524 * Is emitted when the write state of the folder has been changed. The application
525 * should disable all actions like "Create New..." that depend on the write
528 void writeStateChanged(bool isFolderWritable
);
531 * Is emitted if the URL should be changed to the previous URL of the
532 * history (e.g. because the "back"-mousebutton has been pressed).
534 void goBackRequested();
537 * Is emitted if the URL should be changed to the next URL of the
538 * history (e.g. because the "next"-mousebutton has been pressed).
540 void goForwardRequested();
543 * Is emitted when the user wants to move the focus to another view.
545 void toggleActiveViewRequested();
548 * Is emitted when the user clicks a tag or a link
549 * in the metadata widget of a tooltip.
551 void urlActivated(const QUrl
& url
);
554 /** Changes the zoom level if Control is pressed during a wheel event. */
555 void wheelEvent(QWheelEvent
* event
) override
;
557 void hideEvent(QHideEvent
* event
) override
;
558 bool event(QEvent
* event
) override
;
562 * Marks the view as active (DolphinView:isActive() will return true)
563 * and emits the 'activated' signal if it is not already active.
567 void slotItemActivated(int index
);
568 void slotItemsActivated(const KItemSet
& indexes
);
569 void slotItemMiddleClicked(int index
);
570 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
571 void slotViewContextMenuRequested(const QPointF
& pos
);
572 void slotHeaderContextMenuRequested(const QPointF
& pos
);
573 void slotHeaderColumnWidthChangeFinished(const QByteArray
& role
, qreal current
);
574 void slotItemHovered(int index
);
575 void slotItemUnhovered(int index
);
576 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
577 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
578 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
579 void slotRenameDialogRenamingFinished(const QList
<QUrl
>& urls
);
580 void slotSelectedItemTextPressed(int index
);
583 * Is called when new items get pasted or dropped.
585 void slotItemCreated(const QUrl
&url
);
587 * Is called after all pasted or dropped items have been copied to destination.
589 void slotPasteJobResult(KJob
*job
);
592 * Emits the signal \a selectionChanged() with a small delay. This is
593 * because getting all file items for the selection can be an expensive
594 * operation. Fast selection changes are collected in this case and
595 * the signal is emitted only after no selection change has been done
596 * within a small delay.
598 void slotSelectionChanged(const KItemSet
& current
, const KItemSet
& previous
);
601 * Is called by emitDelayedSelectionChangedSignal() and emits the
602 * signal \a selectionChanged() with all selected file items as parameter.
604 void emitSelectionChangedSignal();
607 * Updates the view properties of the current URL to the
608 * sorting given by \a role.
610 void updateSortRole(const QByteArray
& role
);
613 * Updates the view properties of the current URL to the
614 * sort order given by \a order.
616 void updateSortOrder(Qt::SortOrder order
);
619 * Updates the view properties of the current URL to the
620 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
622 void updateSortFoldersFirst(bool foldersFirst
);
625 * Indicates in the status bar that the delete operation
626 * of the job \a job has been finished.
628 void slotDeleteFileFinished(KJob
* job
);
631 * Indicates in the status bar that the trash operation
632 * of the job \a job has been finished.
634 void slotTrashFileFinished(KJob
* job
);
637 * Invoked when the rename job is done, for error handling.
639 void slotRenamingResult(KJob
* job
);
642 * Invoked when the file item model has started the loading
643 * of the directory specified by DolphinView::url().
645 void slotDirectoryLoadingStarted();
648 * Invoked when the file item model indicates that the loading of a directory has
649 * been completed. Assures that pasted items and renamed items get seleced.
651 void slotDirectoryLoadingCompleted();
654 * Is invoked when items of KFileItemModel have been changed.
656 void slotItemsChanged();
659 * Is invoked when the sort order has been changed by the user by clicking
660 * on a header item. The view properties of the directory will get updated.
662 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
665 * Is invoked when the sort role has been changed by the user by clicking
666 * on a header item. The view properties of the directory will get updated.
668 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
671 * Is invoked when the visible roles have been changed by the user by dragging
672 * a header item. The view properties of the directory will get updated.
674 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
675 const QList
<QByteArray
>& previous
);
677 void slotRoleEditingCanceled();
678 void slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
681 * Observes the item with the URL \a url. As soon as the directory
682 * model indicates that the item is available, the item will
683 * get selected and it is assured that the item stays visible.
685 void observeCreatedItem(const QUrl
&url
);
688 * Called when a redirection happens.
689 * Testcase: fish://localhost
691 void slotDirectoryRedirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
694 * Applies the state that has been restored by restoreViewState()
697 void updateViewState();
702 * Calculates the number of currently shown files into
703 * \a fileCount and the number of folders into \a folderCount.
704 * The size of all files is written into \a totalFileSize.
705 * It is recommend using this method instead of asking the
706 * directory lister or the model directly, as it takes
707 * filtering and hierarchical previews into account.
709 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
711 void slotTwoClicksRenamingTimerTimeout();
714 void loadDirectory(const QUrl
& url
, bool reload
= false);
717 * Applies the view properties which are defined by the current URL
718 * to the DolphinView properties. The view properties are read from a
719 * .directory file either in the current directory, or in the
720 * share/apps/dolphin/view_properties/ subfolder of the user's .kde folder.
722 void applyViewProperties();
725 * Applies the given view properties to the DolphinView.
727 void applyViewProperties(const ViewProperties
& props
);
730 * Applies the m_mode property to the corresponding
731 * itemlayout-property of the KItemListView.
733 void applyModeToView();
736 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
737 * Pastes the clipboard data into the URL \a url.
739 void pasteToUrl(const QUrl
& url
);
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 QList
<QUrl
> simplifiedSelectedUrls() const;
749 * Returns the MIME data for all selected items.
751 QMimeData
* selectionMimeData() const;
754 * Updates m_isFolderWritable dependent on whether the folder represented by
755 * the current URL is writable. If the state has changed, the signal
756 * writeableStateChanged() will be emitted.
758 void updateWritableState();
761 * @return The current URL if no viewproperties-context is given (see
762 * DolphinView::viewPropertiesContext(), otherwise the context
765 QUrl
viewPropertiesUrl() const;
768 * Clears the selection and updates current item and selection according to the parameters
770 * @param current URL to be set as current
771 * @param selected list of selected items
773 void forceUrlsSelection(const QUrl
& current
, const QList
<QUrl
>& selected
);
775 void abortTwoClicksRenaming();
778 void updatePalette();
782 bool m_assureVisibleCurrentIndex
;
783 bool m_isFolderWritable
;
784 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
785 // tooltip may be shown when hovering an item.
788 QString m_viewPropertiesContext
;
790 QList
<QByteArray
> m_visibleRoles
;
792 QVBoxLayout
* m_topLayout
;
794 KFileItemModel
* m_model
;
795 DolphinItemListView
* m_view
;
796 KItemListContainer
* m_container
;
798 ToolTipManager
* m_toolTipManager
;
800 QTimer
* m_selectionChangedTimer
;
802 QUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
803 bool m_scrollToCurrentItem
; // Used for marking we need to scroll to current item or not
804 QPoint m_restoredContentsPosition
;
806 QList
<QUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
807 bool m_clearSelectionBeforeSelectingNewItems
;
808 bool m_markFirstNewlySelectedItemAsCurrent
;
810 VersionControlObserver
* m_versionControlObserver
;
812 QTimer
* m_twoClicksRenamingTimer
;
813 QUrl m_twoClicksRenamingItemUrl
;
816 friend class TestBase
;
817 friend class DolphinDetailsViewTest
;
818 friend class DolphinPart
; // Accesses m_model
821 /// Allow using DolphinView::Mode in QVariant
822 Q_DECLARE_METATYPE(DolphinView::Mode
)
824 #endif // DOLPHINVIEW_H