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>
25 typedef KIO::FileUndoManager::CommandType CommandType
;
27 class DolphinItemListView
;
29 class KItemListContainer
;
33 class VersionControlObserver
;
36 class QGraphicsSceneDragDropEvent
;
37 class QRegularExpression
;
40 * @short Represents a view for the directory content.
42 * View modes for icons, compact and details are supported. It's
50 class DOLPHIN_EXPORT DolphinView
: public QWidget
56 * Defines the view mode for a directory. The
57 * view mode is automatically updated if the directory itself
58 * defines a view mode (see class ViewProperties for details).
63 * The items are shown as icons with a name-label below.
68 * The icon, the name and the size of the items are
69 * shown per default as a table.
74 * The items are shown as icons with the name-label aligned
81 * @param url Specifies the content which should be shown.
82 * @param parent Parent widget of the view.
84 DolphinView(const QUrl
& url
, QWidget
* parent
);
86 ~DolphinView() override
;
89 * Returns the current active URL, where all actions are applied.
90 * The URL navigator is synchronized with this URL.
95 * If \a active is true, the view will marked as active. The active
96 * view is defined as view where all actions are applied to.
98 void setActive(bool active
);
99 bool isActive() const;
102 * Changes the view mode for the current directory to \a mode.
103 * If the view properties should be remembered for each directory
104 * (GeneralSettings::globalViewProps() returns false), then the
105 * changed view mode will be stored automatically.
107 void setMode(Mode mode
);
111 * Turns on the file preview for the all files of the current directory,
112 * if \a show is true.
113 * If the view properties should be remembered for each directory
114 * (GeneralSettings::globalViewProps() returns false), then the
115 * preview setting will be stored automatically.
117 void setPreviewsShown(bool show
);
118 bool previewsShown() const;
121 * Shows all hidden 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 * show hidden file setting will be stored automatically.
127 void setHiddenFilesShown(bool show
);
128 bool hiddenFilesShown() const;
131 * Turns on sorting by groups if \a enable is true.
133 void setGroupedSorting(bool grouped
);
134 bool groupedSorting() const;
137 * Returns the items of the view.
139 KFileItemList
items() const;
142 * @return The number of items. itemsCount() is faster in comparison
143 * to items().count().
145 int itemsCount() const;
148 * Returns the selected items. The list is empty if no item has been
151 KFileItemList
selectedItems() const;
154 * Returns the number of selected items (this is faster than
155 * invoking selectedItems().count()).
157 int selectedItemsCount() const;
160 * Marks the items indicated by \p urls to get selected after the
161 * directory DolphinView::url() has been loaded. Note that nothing
162 * gets selected if no loading of a directory has been triggered
163 * by DolphinView::setUrl() or DolphinView::reload().
165 void markUrlsAsSelected(const QList
<QUrl
> &urls
);
168 * Marks the item indicated by \p url to be scrolled to and as the
169 * current item after directory DolphinView::url() has been loaded.
171 void markUrlAsCurrent(const QUrl
& url
);
174 * All items that match the regular expression \a regexp will get selected
175 * if \a enabled is true and deselected if \a enabled is false.
177 * Note that to match the whole string the pattern should be anchored:
178 * - you can anchor the pattern with QRegularExpression::anchoredPattern()
179 * - if you use QRegularExpresssion::wildcardToRegularExpression(), don't use
180 * QRegularExpression::anchoredPattern() as the former already returns an
183 void selectItems(const QRegularExpression
®exp
, bool enabled
);
186 * Sets the zoom level to \a level. It is assured that the used
187 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
188 * ZoomLevelInfo::maximumLevel().
190 void setZoomLevel(int level
);
191 int zoomLevel() const;
194 * Resets the view's icon size to the default value
196 void resetZoomLevel();
198 void setSortRole(const QByteArray
& role
);
199 QByteArray
sortRole() const;
201 void setSortOrder(Qt::SortOrder order
);
202 Qt::SortOrder
sortOrder() const;
204 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
205 void setSortFoldersFirst(bool foldersFirst
);
206 bool sortFoldersFirst() const;
208 /** Sets the additional information which should be shown for the items. */
209 void setVisibleRoles(const QList
<QByteArray
>& roles
);
211 /** Returns the additional information which should be shown for the items. */
212 QList
<QByteArray
> visibleRoles() const;
217 * Refreshes the view to get synchronized with the settings (e.g. icons size,
223 * Saves the current settings (e.g. icons size, font, ..).
225 void writeSettings();
228 * Filters the currently shown items by \a nameFilter. All items
229 * which contain the given filter string will be shown.
231 void setNameFilter(const QString
& nameFilter
);
232 QString
nameFilter() const;
235 * Filters the currently shown items by \a filters. All items
236 * whose content-type matches those given by the list of filters
239 void setMimeTypeFilters(const QStringList
& filters
);
240 QStringList
mimeTypeFilters() const;
243 * Returns a textual representation of the state of the current
244 * folder or selected items, suitable for use in the status bar.
246 QString
statusBarText() const;
249 * Returns the version control actions that are provided for the items \p items.
250 * Usually the actions are presented in the context menu.
252 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
255 * Returns the state of the paste action:
256 * first is whether the action should be enabled
257 * second is the text for the action
259 QPair
<bool, QString
> pasteInfo() const;
262 * If \a tabsForFiles is true, the signal tabRequested() will also
263 * emitted also for files. Per default tabs for files is disabled
264 * and hence the signal tabRequested() will only be emitted for
267 void setTabsForFilesEnabled(bool tabsForFiles
);
268 bool isTabsForFilesEnabled() const;
271 * Returns true if the current view allows folders to be expanded,
272 * i.e. presents a hierarchical view to the user.
274 bool itemsExpandable() const;
277 * Restores the view state (current item, contents position, details view expansion state)
279 void restoreState(QDataStream
& stream
);
282 * Saves the view state (current item, contents position, details view expansion state)
284 void saveState(QDataStream
& stream
);
287 * Returns the root item which represents the current URL.
289 KFileItem
rootItem() const;
292 * Sets a context that is used for remembering the view-properties.
293 * Per default the context is empty and the path of the currently set URL
294 * is used for remembering the view-properties. Setting a custom context
295 * makes sense if specific types of URLs (e.g. search-URLs) should
296 * share common view-properties.
298 void setViewPropertiesContext(const QString
& context
);
299 QString
viewPropertiesContext() const;
302 * Checks if the given \a item can be opened as folder (e.g. archives).
303 * This function will also adjust the \a url (e.g. change the protocol).
304 * @return a valid and adjusted url if the item can be opened as folder,
305 * otherwise return an empty url.
307 static QUrl
openItemAsFolderUrl(const KFileItem
& item
, const bool browseThroughArchives
= true);
310 * Hides tooltip displayed over element.
312 void hideToolTip(const ToolTipManager::HideBehavior behavior
= ToolTipManager::HideBehavior::Later
);
316 * Changes the directory to \a url. If the current directory is equal to
317 * \a url, nothing will be done (use DolphinView::reload() instead).
319 void setUrl(const QUrl
& url
);
323 * @see DolphinView::selectedItems()
328 * Inverts the current selection: selected items get unselected,
329 * unselected items get selected.
330 * @see DolphinView::selectedItems()
332 void invertSelection();
334 void clearSelection();
337 * Triggers the renaming of the currently selected items, where
338 * the user must input a new name for the items.
340 void renameSelectedItems();
343 * Moves all selected items to the trash.
345 void trashSelectedItems();
348 * Deletes all selected items.
350 void deleteSelectedItems();
353 * Copies all selected items to the clipboard and marks
356 void cutSelectedItemsToClipboard();
358 /** Copies all selected items to the clipboard. */
359 void copySelectedItemsToClipboard();
362 * Copies all selected items to @p destinationUrl.
364 void copySelectedItems(const KFileItemList
&selection
, const QUrl
&destinationUrl
);
367 * Moves all selected items to @p destinationUrl.
369 void moveSelectedItems(const KFileItemList
&selection
, const QUrl
&destinationUrl
);
371 /** Pastes the clipboard data to this view. */
375 * Pastes the clipboard data into the currently selected
376 * folder. If the current selection is not exactly one folder, no
377 * paste operation is done.
379 void pasteIntoFolder();
382 * Copies the path of the first selected KFileItem into Clipboard.
384 void copyPathToClipboard();
387 * Creates duplicates of selected items, appending "copy"
390 void duplicateSelectedItems();
393 * Handles a drop of @p dropEvent onto widget @p dropWidget and destination @p destUrl
395 void dropUrls(const QUrl
&destUrl
, QDropEvent
*dropEvent
, QWidget
*dropWidget
);
400 * Applies the state that has been restored by restoreViewState()
403 void updateViewState();
405 /** Activates the view if the item list container gets focus. */
406 bool eventFilter(QObject
* watched
, QEvent
* event
) override
;
410 * Is emitted if the view has been activated by e. g. a mouse click.
414 /** Is emitted if the URL of the view has been changed to \a url. */
415 void urlChanged(const QUrl
& url
);
418 * Is emitted when clicking on an item with the left mouse button.
420 void itemActivated(const KFileItem
& item
);
423 * Is emitted when multiple items have been activated by e. g.
424 * context menu open with.
426 void itemsActivated(const KFileItemList
& items
);
429 * Is emitted if items have been added or deleted.
431 void itemCountChanged();
434 * Is emitted if a new tab should be opened for the URL \a url.
436 void tabRequested(const QUrl
& url
);
439 * Is emitted if the view mode (IconsView, DetailsView,
440 * PreviewsView) has been changed.
442 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
444 /** Is emitted if the 'show preview' property has been changed. */
445 void previewsShownChanged(bool shown
);
447 /** Is emitted if the 'show hidden files' property has been changed. */
448 void hiddenFilesShownChanged(bool shown
);
450 /** Is emitted if the 'grouped sorting' property has been changed. */
451 void groupedSortingChanged(bool groupedSorting
);
453 /** Is emitted if the sorting by name, size or date has been changed. */
454 void sortRoleChanged(const QByteArray
& role
);
456 /** Is emitted if the sort order (ascending or descending) has been changed. */
457 void sortOrderChanged(Qt::SortOrder order
);
460 * Is emitted if the sorting of files and folders (separate with folders
461 * first or mixed) has been changed.
463 void sortFoldersFirstChanged(bool foldersFirst
);
465 /** Is emitted if the additional information shown for this view has been changed. */
466 void visibleRolesChanged(const QList
<QByteArray
>& current
,
467 const QList
<QByteArray
>& previous
);
469 /** Is emitted if the zoom level has been changed by zooming in or out. */
470 void zoomLevelChanged(int current
, int previous
);
473 * Is emitted if information of an item is requested to be shown e. g. in the panel.
474 * If item is null, no item information request is pending.
476 void requestItemInfo(const KFileItem
& item
);
479 * Is emitted whenever the selection has been changed.
481 void selectionChanged(const KFileItemList
& selection
);
484 * Is emitted if a context menu is requested for the item \a item,
485 * which is part of \a url. If the item is null, the context menu
486 * for the URL should be shown and the custom actions \a customActions
489 void requestContextMenu(const QPoint
& pos
,
490 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 current directory is 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 directoryLoadingStarted();
521 * Is emitted after the directory triggered by DolphinView::setUrl()
524 void directoryLoadingCompleted();
527 * Is emitted after the directory loading triggered by DolphinView::setUrl()
530 void directoryLoadingCanceled();
533 * Is emitted after DolphinView::setUrl() has been invoked and provides
534 * the information how much percent of the current directory have been loaded.
536 void directoryLoadingProgress(int percent
);
539 * Is emitted if the sorting is done asynchronously and provides the
540 * progress information of the sorting.
542 void directorySortingProgress(int percent
);
545 * Emitted when the file-item-model emits redirection.
546 * Testcase: fish://localhost
548 void redirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
551 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
552 * In this case no signal errorMessage() will be emitted.
554 void urlIsFileError(const QUrl
& url
);
557 * Is emitted when the write state of the folder has been changed. The application
558 * should disable all actions like "Create New..." that depend on the write
561 void writeStateChanged(bool isFolderWritable
);
564 * Is emitted if the URL should be changed to the previous URL of the
565 * history (e.g. because the "back"-mousebutton has been pressed).
567 void goBackRequested();
570 * Is emitted if the URL should be changed to the next URL of the
571 * history (e.g. because the "next"-mousebutton has been pressed).
573 void goForwardRequested();
576 * Is emitted when the user wants to move the focus to another view.
578 void toggleActiveViewRequested();
581 * Is emitted when the user clicks a tag or a link
582 * in the metadata widget of a tooltip.
584 void urlActivated(const QUrl
& url
);
586 void goUpRequested();
589 /** Changes the zoom level if Control is pressed during a wheel event. */
590 void wheelEvent(QWheelEvent
* event
) override
;
592 void hideEvent(QHideEvent
* event
) override
;
593 bool event(QEvent
* event
) override
;
597 * Marks the view as active (DolphinView:isActive() will return true)
598 * and emits the 'activated' signal if it is not already active.
602 void slotItemActivated(int index
);
603 void slotItemsActivated(const KItemSet
& indexes
);
604 void slotItemMiddleClicked(int index
);
605 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
606 void slotViewContextMenuRequested(const QPointF
& pos
);
607 void slotHeaderContextMenuRequested(const QPointF
& pos
);
608 void slotHeaderColumnWidthChangeFinished(const QByteArray
& role
, qreal current
);
609 void slotItemHovered(int index
);
610 void slotItemUnhovered(int index
);
611 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
612 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
613 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
614 void slotRenameDialogRenamingFinished(const QList
<QUrl
>& urls
);
615 void slotSelectedItemTextPressed(int index
);
616 void slotCopyingDone(KIO::Job
*, const QUrl
&, const QUrl
&to
);
617 void slotIncreaseZoom();
618 void slotDecreaseZoom();
622 * Is called when new items get pasted or dropped.
624 void slotItemCreated(const QUrl
&url
);
626 * Is called after all pasted or dropped items have been copied to destination.
628 void slotJobResult(KJob
*job
);
631 * Emits the signal \a selectionChanged() with a small delay. This is
632 * because getting all file items for the selection can be an expensive
633 * operation. Fast selection changes are collected in this case and
634 * the signal is emitted only after no selection change has been done
635 * within a small delay.
637 void slotSelectionChanged(const KItemSet
& current
, const KItemSet
& previous
);
640 * Is called by emitDelayedSelectionChangedSignal() and emits the
641 * signal \a selectionChanged() with all selected file items as parameter.
643 void emitSelectionChangedSignal();
646 * Updates the view properties of the current URL to the
647 * sorting given by \a role.
649 void updateSortRole(const QByteArray
& role
);
652 * Updates the view properties of the current URL to the
653 * sort order given by \a order.
655 void updateSortOrder(Qt::SortOrder order
);
658 * Updates the view properties of the current URL to the
659 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
661 void updateSortFoldersFirst(bool foldersFirst
);
664 * Indicates in the status bar that the delete operation
665 * of the job \a job has been finished.
667 void slotDeleteFileFinished(KJob
* job
);
670 * Indicates in the status bar that the trash operation
671 * of the job \a job has been finished.
673 void slotTrashFileFinished(KJob
* job
);
676 * Invoked when the rename job is done, for error handling.
678 void slotRenamingResult(KJob
* job
);
681 * Invoked when the file item model has started the loading
682 * of the directory specified by DolphinView::url().
684 void slotDirectoryLoadingStarted();
687 * Invoked when the file item model indicates that the loading of a directory has
688 * been completed. Assures that pasted items and renamed items get selected.
690 void slotDirectoryLoadingCompleted();
693 * Invoked when the file item model indicates that the loading of a directory has
696 void slotDirectoryLoadingCanceled();
699 * Is invoked when items of KFileItemModel have been changed.
701 void slotItemsChanged();
704 * Is invoked when the sort order has been changed by the user by clicking
705 * on a header item. The view properties of the directory will get updated.
707 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
710 * Is invoked when the sort role has been changed by the user by clicking
711 * on a header item. The view properties of the directory will get updated.
713 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
716 * Is invoked when the visible roles have been changed by the user by dragging
717 * a header item. The view properties of the directory will get updated.
719 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
720 const QList
<QByteArray
>& previous
);
722 void slotRoleEditingCanceled();
723 void slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
726 * Observes the item with the URL \a url. As soon as the directory
727 * model indicates that the item is available, the item will
728 * get selected and it is assured that the item stays visible.
730 void observeCreatedItem(const QUrl
&url
);
733 * Called when a redirection happens.
734 * Testcase: fish://localhost
736 void slotDirectoryRedirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
739 * Calculates the number of currently shown files into
740 * \a fileCount and the number of folders into \a folderCount.
741 * The size of all files is written into \a totalFileSize.
742 * It is recommend using this method instead of asking the
743 * directory lister or the model directly, as it takes
744 * filtering and hierarchical previews into account.
746 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
748 void slotTwoClicksRenamingTimerTimeout();
751 void loadDirectory(const QUrl
& url
, bool reload
= false);
754 * Applies the view properties which are defined by the current URL
755 * to the DolphinView properties. The view properties are read from a
756 * .directory file either in the current directory, or in the
757 * share/apps/dolphin/view_properties/ subfolder of the user's .kde folder.
759 void applyViewProperties();
762 * Applies the given view properties to the DolphinView.
764 void applyViewProperties(const ViewProperties
& props
);
767 * Applies the m_mode property to the corresponding
768 * itemlayout-property of the KItemListView.
770 void applyModeToView();
773 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
774 * Pastes the clipboard data into the URL \a url.
776 void pasteToUrl(const QUrl
& url
);
779 * Returns a list of URLs for all selected items. The list is
780 * simplified, so that when the URLs are part of different tree
781 * levels, only the parent is returned.
783 QList
<QUrl
> simplifiedSelectedUrls() const;
786 * Returns the MIME data for all selected items.
788 QMimeData
* selectionMimeData() const;
791 * Updates m_isFolderWritable dependent on whether the folder represented by
792 * the current URL is writable. If the state has changed, the signal
793 * writeableStateChanged() will be emitted.
795 void updateWritableState();
798 * @return The current URL if no viewproperties-context is given (see
799 * DolphinView::viewPropertiesContext(), otherwise the context
802 QUrl
viewPropertiesUrl() const;
805 * Clears the selection and updates current item and selection according to the parameters
807 * @param current URL to be set as current
808 * @param selected list of selected items
810 void forceUrlsSelection(const QUrl
& current
, const QList
<QUrl
>& selected
);
812 void abortTwoClicksRenaming();
814 void updatePlaceholderLabel();
817 void updatePalette();
821 bool m_assureVisibleCurrentIndex
;
822 bool m_isFolderWritable
;
823 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
824 // tooltip may be shown when hovering an item.
828 QString m_viewPropertiesContext
;
830 QList
<QByteArray
> m_visibleRoles
;
832 QVBoxLayout
* m_topLayout
;
834 KFileItemModel
* m_model
;
835 DolphinItemListView
* m_view
;
836 KItemListContainer
* m_container
;
838 ToolTipManager
* m_toolTipManager
;
840 QTimer
* m_selectionChangedTimer
;
842 QUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
843 bool m_scrollToCurrentItem
; // Used for marking we need to scroll to current item or not
844 QPoint m_restoredContentsPosition
;
846 QList
<QUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
847 bool m_clearSelectionBeforeSelectingNewItems
;
848 bool m_markFirstNewlySelectedItemAsCurrent
;
850 VersionControlObserver
* m_versionControlObserver
;
852 QTimer
* m_twoClicksRenamingTimer
;
853 QUrl m_twoClicksRenamingItemUrl
;
854 QLabel
* m_placeholderLabel
;
857 friend class TestBase
;
858 friend class DolphinDetailsViewTest
;
859 friend class DolphinPart
; // Accesses m_model
862 /// Allow using DolphinView::Mode in QVariant
863 Q_DECLARE_METATYPE(DolphinView::Mode
)
865 #endif // DOLPHINVIEW_H