2 * SPDX-FileCopyrightText: 2006-2009 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2006 Gregor Kališnik <gregor@podnapisi.net>
5 * SPDX-License-Identifier: GPL-2.0-or-later
11 #include "dolphintabwidget.h"
12 #include "dolphin_export.h"
13 #include "tooltips/tooltipmanager.h"
17 #include <config-baloo.h>
18 #include <kio/fileundomanager.h>
19 #include <kparts/part.h>
26 typedef KIO::FileUndoManager::CommandType CommandType
;
28 class DolphinItemListView
;
30 class KItemListContainer
;
34 class VersionControlObserver
;
37 class QGraphicsSceneDragDropEvent
;
38 class QRegularExpression
;
41 * @short Represents a view for the directory content.
43 * View modes for icons, compact and details are supported. It's
51 class DOLPHIN_EXPORT DolphinView
: public QWidget
57 * Defines the view mode for a directory. The
58 * view mode is automatically updated if the directory itself
59 * defines a view mode (see class ViewProperties for details).
64 * The items are shown as icons with a name-label below.
69 * The icon, the name and the size of the items are
70 * shown per default as a table.
75 * The items are shown as icons with the name-label aligned
82 * @param url Specifies the content which should be shown.
83 * @param parent Parent widget of the view.
85 DolphinView(const QUrl
& url
, QWidget
* parent
);
87 ~DolphinView() override
;
90 * Returns the current active URL, where all actions are applied.
91 * The URL navigator is synchronized with this URL.
96 * If \a active is true, the view will marked as active. The active
97 * view is defined as view where all actions are applied to.
99 void setActive(bool active
);
100 bool isActive() const;
103 * Changes the view mode for the current directory to \a mode.
104 * If the view properties should be remembered for each directory
105 * (GeneralSettings::globalViewProps() returns false), then the
106 * changed view mode will be stored automatically.
108 void setMode(Mode mode
);
112 * Turns on the file preview for the all files of the current directory,
113 * if \a show is true.
114 * If the view properties should be remembered for each directory
115 * (GeneralSettings::globalViewProps() returns false), then the
116 * preview setting will be stored automatically.
118 void setPreviewsShown(bool show
);
119 bool previewsShown() const;
122 * Shows all hidden files of the current directory,
123 * if \a show is true.
124 * If the view properties should be remembered for each directory
125 * (GeneralSettings::globalViewProps() returns false), then the
126 * show hidden file setting will be stored automatically.
128 void setHiddenFilesShown(bool show
);
129 bool hiddenFilesShown() const;
132 * Turns on sorting by groups if \a enable is true.
134 void setGroupedSorting(bool grouped
);
135 bool groupedSorting() const;
138 * Returns the items of the view.
140 KFileItemList
items() const;
143 * @return The number of items. itemsCount() is faster in comparison
144 * to items().count().
146 int itemsCount() const;
149 * Returns the selected items. The list is empty if no item has been
152 KFileItemList
selectedItems() const;
155 * Returns the number of selected items (this is faster than
156 * invoking selectedItems().count()).
158 int selectedItemsCount() const;
161 * Marks the items indicated by \p urls to get selected after the
162 * directory DolphinView::url() has been loaded. Note that nothing
163 * gets selected if no loading of a directory has been triggered
164 * by DolphinView::setUrl() or DolphinView::reload().
166 void markUrlsAsSelected(const QList
<QUrl
> &urls
);
169 * Marks the item indicated by \p url to be scrolled to and as the
170 * current item after directory DolphinView::url() has been loaded.
172 void markUrlAsCurrent(const QUrl
& url
);
175 * All items that match the regular expression \a regexp will get selected
176 * if \a enabled is true and deselected if \a enabled is false.
178 * Note that to match the whole string the pattern should be anchored:
179 * - you can anchor the pattern with QRegularExpression::anchoredPattern()
180 * - if you use QRegularExpresssion::wildcardToRegularExpression(), don't use
181 * QRegularExpression::anchoredPattern() as the former already returns an
184 void selectItems(const QRegularExpression
®exp
, bool enabled
);
187 * Sets the zoom level to \a level. It is assured that the used
188 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
189 * ZoomLevelInfo::maximumLevel().
191 void setZoomLevel(int level
);
192 int zoomLevel() const;
195 * Resets the view's icon size to the default value
197 void resetZoomLevel();
199 void setSortRole(const QByteArray
& role
);
200 QByteArray
sortRole() const;
202 void setSortOrder(Qt::SortOrder order
);
203 Qt::SortOrder
sortOrder() const;
205 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
206 void setSortFoldersFirst(bool foldersFirst
);
207 bool sortFoldersFirst() const;
209 /** Sets a separate sorting with hidden files and folders last (true) or not (false). */
210 void setSortHiddenLast(bool hiddenLast
);
211 bool sortHiddenLast() const;
213 /** Sets the additional information which should be shown for the items. */
214 void setVisibleRoles(const QList
<QByteArray
>& roles
);
216 /** Returns the additional information which should be shown for the items. */
217 QList
<QByteArray
> visibleRoles() const;
222 * Refreshes the view to get synchronized with the settings (e.g. icons size,
228 * Saves the current settings (e.g. icons size, font, ..).
230 void writeSettings();
233 * Filters the currently shown items by \a nameFilter. All items
234 * which contain the given filter string will be shown.
236 void setNameFilter(const QString
& nameFilter
);
237 QString
nameFilter() const;
240 * Filters the currently shown items by \a filters. All items
241 * whose content-type matches those given by the list of filters
244 void setMimeTypeFilters(const QStringList
& filters
);
245 QStringList
mimeTypeFilters() const;
248 * Tells the view to generate an updated status bar text. The result
249 * is returned through the statusBarTextChanged(QString statusBarText) signal.
250 * It will carry a textual representation of the state of the current
251 * folder or selected items, suitable for use in the status bar.
252 * Any pending requests of status bar text are killed.
254 void requestStatusBarText();
257 * Returns the version control actions that are provided for the items \p items.
258 * Usually the actions are presented in the context menu.
260 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
263 * Returns the state of the paste action:
264 * first is whether the action should be enabled
265 * second is the text for the action
267 QPair
<bool, QString
> pasteInfo() const;
270 * If \a tabsForFiles is true, the signal tabRequested() will also
271 * emitted also for files. Per default tabs for files is disabled
272 * and hence the signal tabRequested() will only be emitted for
275 void setTabsForFilesEnabled(bool tabsForFiles
);
276 bool isTabsForFilesEnabled() const;
279 * Returns true if the current view allows folders to be expanded,
280 * i.e. presents a hierarchical view to the user.
282 bool itemsExpandable() const;
285 * Restores the view state (current item, contents position, details view expansion state)
287 void restoreState(QDataStream
& stream
);
290 * Saves the view state (current item, contents position, details view expansion state)
292 void saveState(QDataStream
& stream
);
295 * Returns the root item which represents the current URL.
297 KFileItem
rootItem() const;
300 * Sets a context that is used for remembering the view-properties.
301 * Per default the context is empty and the path of the currently set URL
302 * is used for remembering the view-properties. Setting a custom context
303 * makes sense if specific types of URLs (e.g. search-URLs) should
304 * share common view-properties.
306 void setViewPropertiesContext(const QString
& context
);
307 QString
viewPropertiesContext() const;
310 * Checks if the given \a item can be opened as folder (e.g. archives).
311 * This function will also adjust the \a url (e.g. change the protocol).
312 * @return a valid and adjusted url if the item can be opened as folder,
313 * otherwise return an empty url.
315 static QUrl
openItemAsFolderUrl(const KFileItem
& item
, const bool browseThroughArchives
= true);
318 * Hides tooltip displayed over element.
320 void hideToolTip(const ToolTipManager::HideBehavior behavior
= ToolTipManager::HideBehavior::Later
);
324 * Changes the directory to \a url. If the current directory is equal to
325 * \a url, nothing will be done (use DolphinView::reload() instead).
327 void setUrl(const QUrl
& url
);
331 * @see DolphinView::selectedItems()
336 * Inverts the current selection: selected items get unselected,
337 * unselected items get selected.
338 * @see DolphinView::selectedItems()
340 void invertSelection();
342 void clearSelection();
345 * Triggers the renaming of the currently selected items, where
346 * the user must input a new name for the items.
348 void renameSelectedItems();
351 * Moves all selected items to the trash.
353 void trashSelectedItems();
356 * Deletes all selected items.
358 void deleteSelectedItems();
361 * Copies all selected items to the clipboard and marks
364 void cutSelectedItemsToClipboard();
366 /** Copies all selected items to the clipboard. */
367 void copySelectedItemsToClipboard();
370 * Copies all selected items to @p destinationUrl.
372 void copySelectedItems(const KFileItemList
&selection
, const QUrl
&destinationUrl
);
375 * Moves all selected items to @p destinationUrl.
377 void moveSelectedItems(const KFileItemList
&selection
, const QUrl
&destinationUrl
);
379 /** Pastes the clipboard data to this view. */
383 * Pastes the clipboard data into the currently selected
384 * folder. If the current selection is not exactly one folder, no
385 * paste operation is done.
387 void pasteIntoFolder();
390 * Copies the path of the first selected KFileItem into Clipboard.
392 void copyPathToClipboard();
395 * Creates duplicates of selected items, appending "copy"
398 void duplicateSelectedItems();
401 * Handles a drop of @p dropEvent onto widget @p dropWidget and destination @p destUrl
403 void dropUrls(const QUrl
&destUrl
, QDropEvent
*dropEvent
, QWidget
*dropWidget
);
408 * Applies the state that has been restored by restoreViewState()
411 void updateViewState();
413 /** Activates the view if the item list container gets focus. */
414 bool eventFilter(QObject
* watched
, QEvent
* event
) override
;
418 * Is emitted if the view has been activated by e. g. a mouse click.
422 /** Is emitted if the URL of the view has been changed to \a url. */
423 void urlChanged(const QUrl
& url
);
426 * Is emitted when clicking on an item with the left mouse button.
428 void itemActivated(const KFileItem
& item
);
431 * Is emitted when multiple items have been activated by e. g.
432 * context menu open with.
434 void itemsActivated(const KFileItemList
& items
);
437 * Is emitted if items have been added or deleted.
439 void itemCountChanged();
442 * Is emitted if a new tab should be opened for the URL \a url.
444 void tabRequested(const QUrl
& url
);
447 * Is emitted if the view mode (IconsView, DetailsView,
448 * PreviewsView) has been changed.
450 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
452 /** Is emitted if the 'show preview' property has been changed. */
453 void previewsShownChanged(bool shown
);
455 /** Is emitted if the 'show hidden files' property has been changed. */
456 void hiddenFilesShownChanged(bool shown
);
458 /** Is emitted if the 'grouped sorting' property has been changed. */
459 void groupedSortingChanged(bool groupedSorting
);
461 /** Is emmited in reaction to a requestStatusBarText() call.
462 * @see requestStatusBarText() */
463 void statusBarTextChanged(QString statusBarText
);
465 /** Is emitted if the sorting by name, size or date has been changed. */
466 void sortRoleChanged(const QByteArray
& role
);
468 /** Is emitted if the sort order (ascending or descending) has been changed. */
469 void sortOrderChanged(Qt::SortOrder order
);
472 * Is emitted if the sorting of files and folders (separate with folders
473 * first or mixed) has been changed.
475 void sortFoldersFirstChanged(bool foldersFirst
);
478 * Is emitted if the sorting of hidden files has been changed.
480 void sortHiddenLastChanged(bool hiddenLast
);
482 /** Is emitted if the additional information shown for this view has been changed. */
483 void visibleRolesChanged(const QList
<QByteArray
>& current
,
484 const QList
<QByteArray
>& previous
);
486 /** Is emitted if the zoom level has been changed by zooming in or out. */
487 void zoomLevelChanged(int current
, int previous
);
490 * Is emitted if information of an item is requested to be shown e. g. in the panel.
491 * If item is null, no item information request is pending.
493 void requestItemInfo(const KFileItem
& item
);
496 * Is emitted whenever the selection has been changed.
498 void selectionChanged(const KFileItemList
& selection
);
501 * Is emitted if a context menu is requested for the item \a item,
502 * which is part of \a url. If the item is null, the context menu
503 * for the URL should be shown and the custom actions \a customActions
506 void requestContextMenu(const QPoint
& pos
,
507 const KFileItem
& item
,
509 const QList
<QAction
*>& customActions
);
512 * Is emitted if an information message with the content \a msg
515 void infoMessage(const QString
& msg
);
518 * Is emitted if an error message with the content \a msg
521 void errorMessage(const QString
& msg
);
524 * Is emitted if an "operation completed" message with the content \a msg
527 void operationCompletedMessage(const QString
& msg
);
530 * Is emitted after DolphinView::setUrl() has been invoked and
531 * the current directory is loaded. If this signal is emitted,
532 * it is assured that the view contains already the correct root
533 * URL and property settings.
535 void directoryLoadingStarted();
538 * Is emitted after the directory triggered by DolphinView::setUrl()
541 void directoryLoadingCompleted();
544 * Is emitted after the directory loading triggered by DolphinView::setUrl()
547 void directoryLoadingCanceled();
550 * Is emitted after DolphinView::setUrl() has been invoked and provides
551 * the information how much percent of the current directory have been loaded.
553 void directoryLoadingProgress(int percent
);
556 * Is emitted if the sorting is done asynchronously and provides the
557 * progress information of the sorting.
559 void directorySortingProgress(int percent
);
562 * Emitted when the file-item-model emits redirection.
563 * Testcase: fish://localhost
565 void redirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
568 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
569 * In this case no signal errorMessage() will be emitted.
571 void urlIsFileError(const QUrl
& url
);
574 * Is emitted when the write state of the folder has been changed. The application
575 * should disable all actions like "Create New..." that depend on the write
578 void writeStateChanged(bool isFolderWritable
);
581 * Is emitted if the URL should be changed to the previous URL of the
582 * history (e.g. because the "back"-mousebutton has been pressed).
584 void goBackRequested();
587 * Is emitted if the URL should be changed to the next URL of the
588 * history (e.g. because the "next"-mousebutton has been pressed).
590 void goForwardRequested();
593 * Is emitted when the user wants to move the focus to another view.
595 void toggleActiveViewRequested();
598 * Is emitted when the user clicks a tag or a link
599 * in the metadata widget of a tooltip.
601 void urlActivated(const QUrl
& url
);
603 void goUpRequested();
605 void fileItemsChanged(const KFileItemList
&changedFileItems
);
608 /** Changes the zoom level if Control is pressed during a wheel event. */
609 void wheelEvent(QWheelEvent
* event
) override
;
611 void hideEvent(QHideEvent
* event
) override
;
612 bool event(QEvent
* event
) override
;
616 * Marks the view as active (DolphinView:isActive() will return true)
617 * and emits the 'activated' signal if it is not already active.
621 void slotItemActivated(int index
);
622 void slotItemsActivated(const KItemSet
& indexes
);
623 void slotItemMiddleClicked(int index
);
624 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
625 void slotViewContextMenuRequested(const QPointF
& pos
);
626 void slotHeaderContextMenuRequested(const QPointF
& pos
);
627 void slotHeaderColumnWidthChangeFinished(const QByteArray
& role
, qreal current
);
628 void slotItemHovered(int index
);
629 void slotItemUnhovered(int index
);
630 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
631 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
632 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
633 void slotRenameDialogRenamingFinished(const QList
<QUrl
>& urls
);
634 void slotSelectedItemTextPressed(int index
);
635 void slotCopyingDone(KIO::Job
*, const QUrl
&, const QUrl
&to
);
636 void slotIncreaseZoom();
637 void slotDecreaseZoom();
641 * Is called when new items get pasted or dropped.
643 void slotItemCreated(const QUrl
&url
);
645 * Is called after all pasted or dropped items have been copied to destination.
647 void slotJobResult(KJob
*job
);
650 * Emits the signal \a selectionChanged() with a small delay. This is
651 * because getting all file items for the selection can be an expensive
652 * operation. Fast selection changes are collected in this case and
653 * the signal is emitted only after no selection change has been done
654 * within a small delay.
656 void slotSelectionChanged(const KItemSet
& current
, const KItemSet
& previous
);
659 * Is called by emitDelayedSelectionChangedSignal() and emits the
660 * signal \a selectionChanged() with all selected file items as parameter.
662 void emitSelectionChangedSignal();
665 * Helper method for DolphinView::requestStatusBarText().
666 * Calculates the amount of folders and files and their total size in
667 * response to a KStatJob::result(), then calls emitStatusBarText().
668 * @see requestStatusBarText()
669 * @see emitStatusBarText()
671 void slotStatJobResult(KJob
*job
);
674 * Updates the view properties of the current URL to the
675 * sorting given by \a role.
677 void updateSortRole(const QByteArray
& role
);
680 * Updates the view properties of the current URL to the
681 * sort order given by \a order.
683 void updateSortOrder(Qt::SortOrder order
);
686 * Updates the view properties of the current URL to the
687 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
689 void updateSortFoldersFirst(bool foldersFirst
);
692 * Updates the view properties of the current URL to the
693 * sorting of hidden files given by \a hiddenLast.
695 void updateSortHiddenLast(bool hiddenLast
);
698 * Indicates in the status bar that the delete operation
699 * of the job \a job has been finished.
701 void slotDeleteFileFinished(KJob
* job
);
704 * Indicates in the status bar that the trash operation
705 * of the job \a job has been finished.
707 void slotTrashFileFinished(KJob
* job
);
710 * Invoked when the rename job is done, for error handling.
712 void slotRenamingResult(KJob
* job
);
715 * Invoked when the file item model has started the loading
716 * of the directory specified by DolphinView::url().
718 void slotDirectoryLoadingStarted();
721 * Invoked when the file item model indicates that the loading of a directory has
722 * been completed. Assures that pasted items and renamed items get selected.
724 void slotDirectoryLoadingCompleted();
727 * Invoked when the file item model indicates that the loading of a directory has
730 void slotDirectoryLoadingCanceled();
733 * Is invoked when items of KFileItemModel have been changed.
735 void slotItemsChanged();
738 * Is invoked when the sort order has been changed by the user by clicking
739 * on a header item. The view properties of the directory will get updated.
741 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
744 * Is invoked when the sort role has been changed by the user by clicking
745 * on a header item. The view properties of the directory will get updated.
747 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
750 * Is invoked when the visible roles have been changed by the user by dragging
751 * a header item. The view properties of the directory will get updated.
753 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
754 const QList
<QByteArray
>& previous
);
756 void slotRoleEditingCanceled();
757 void slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
760 * Observes the item with the URL \a url. As soon as the directory
761 * model indicates that the item is available, the item will
762 * get selected and it is assured that the item stays visible.
764 void observeCreatedItem(const QUrl
&url
);
767 * Called when a redirection happens.
768 * Testcase: fish://localhost
770 void slotDirectoryRedirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
772 void slotTwoClicksRenamingTimerTimeout();
775 void loadDirectory(const QUrl
& url
, bool reload
= false);
778 * Applies the view properties which are defined by the current URL
779 * to the DolphinView properties. The view properties are read from a
780 * .directory file either in the current directory, or in the
781 * share/apps/dolphin/view_properties/ subfolder of the user's .kde folder.
783 void applyViewProperties();
786 * Applies the given view properties to the DolphinView.
788 void applyViewProperties(const ViewProperties
& props
);
791 * Applies the m_mode property to the corresponding
792 * itemlayout-property of the KItemListView.
794 void applyModeToView();
801 * Helper method for DolphinView::requestStatusBarText().
802 * Generates the status bar text from the parameters and
803 * then emits statusBarTextChanged().
804 * @param totalFileSize the sum of the sizes of the files
805 * @param selection if HasSelection is passed, the emitted status bar text will say
806 * that the folders and files which are counted here are selected.
808 void emitStatusBarText(const int folderCount
, const int fileCount
,
809 KIO::filesize_t totalFileSize
, const Selection selection
);
812 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
813 * Pastes the clipboard data into the URL \a url.
815 void pasteToUrl(const QUrl
& url
);
818 * Returns a list of URLs for all selected items. The list is
819 * simplified, so that when the URLs are part of different tree
820 * levels, only the parent is returned.
822 QList
<QUrl
> simplifiedSelectedUrls() const;
825 * Returns the MIME data for all selected items.
827 QMimeData
* selectionMimeData() const;
830 * Updates m_isFolderWritable dependent on whether the folder represented by
831 * the current URL is writable. If the state has changed, the signal
832 * writeableStateChanged() will be emitted.
834 void updateWritableState();
837 * @return The current URL if no viewproperties-context is given (see
838 * DolphinView::viewPropertiesContext(), otherwise the context
841 QUrl
viewPropertiesUrl() const;
844 * Clears the selection and updates current item and selection according to the parameters
846 * @param current URL to be set as current
847 * @param selected list of selected items
849 void forceUrlsSelection(const QUrl
& current
, const QList
<QUrl
>& selected
);
851 void abortTwoClicksRenaming();
853 void updatePlaceholderLabel();
856 void updatePalette();
857 void showLoadingPlaceholder();
861 bool m_assureVisibleCurrentIndex
;
862 bool m_isFolderWritable
;
863 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
864 // tooltip may be shown when hovering an item.
868 QString m_viewPropertiesContext
;
870 QList
<QByteArray
> m_visibleRoles
;
872 QPointer
<KIO::StatJob
> m_statJobForStatusBarText
;
874 QVBoxLayout
* m_topLayout
;
876 KFileItemModel
* m_model
;
877 DolphinItemListView
* m_view
;
878 KItemListContainer
* m_container
;
880 ToolTipManager
* m_toolTipManager
;
882 QTimer
* m_selectionChangedTimer
;
884 QUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
885 bool m_scrollToCurrentItem
; // Used for marking we need to scroll to current item or not
886 QPoint m_restoredContentsPosition
;
888 QList
<QUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
889 bool m_clearSelectionBeforeSelectingNewItems
;
890 bool m_markFirstNewlySelectedItemAsCurrent
;
892 VersionControlObserver
* m_versionControlObserver
;
894 QTimer
* m_twoClicksRenamingTimer
;
895 QUrl m_twoClicksRenamingItemUrl
;
896 QLabel
* m_placeholderLabel
;
897 QTimer
* m_showLoadingPlaceholderTimer
;
900 friend class TestBase
;
901 friend class DolphinDetailsViewTest
;
902 friend class DolphinPart
; // Accesses m_model
905 /// Allow using DolphinView::Mode in QVariant
906 Q_DECLARE_METATYPE(DolphinView::Mode
)
908 #endif // DOLPHINVIEW_H