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
;
35 class QGraphicsSceneDragDropEvent
;
36 class QRegularExpression
;
39 * @short Represents a view for the directory content.
41 * View modes for icons, compact and details are supported. It's
49 class DOLPHIN_EXPORT DolphinView
: public QWidget
55 * Defines the view mode for a directory. The
56 * view mode is automatically updated if the directory itself
57 * defines a view mode (see class ViewProperties for details).
62 * The items are shown as icons with a name-label below.
67 * The icon, the name and the size of the items are
68 * shown per default as a table.
73 * The items are shown as icons with the name-label aligned
80 * @param url Specifies the content which should be shown.
81 * @param parent Parent widget of the view.
83 DolphinView(const QUrl
& url
, QWidget
* parent
);
85 ~DolphinView() override
;
88 * Returns the current active URL, where all actions are applied.
89 * The URL navigator is synchronized with this URL.
94 * If \a active is true, the view will marked as active. The active
95 * view is defined as view where all actions are applied to.
97 void setActive(bool active
);
98 bool isActive() const;
101 * Changes the view mode for the current directory to \a mode.
102 * If the view properties should be remembered for each directory
103 * (GeneralSettings::globalViewProps() returns false), then the
104 * changed view mode will be stored automatically.
106 void setMode(Mode mode
);
110 * Turns on the file preview for the all files of the current directory,
111 * if \a show is true.
112 * If the view properties should be remembered for each directory
113 * (GeneralSettings::globalViewProps() returns false), then the
114 * preview setting will be stored automatically.
116 void setPreviewsShown(bool show
);
117 bool previewsShown() const;
120 * Shows all hidden files of the current directory,
121 * if \a show is true.
122 * If the view properties should be remembered for each directory
123 * (GeneralSettings::globalViewProps() returns false), then the
124 * show hidden file setting will be stored automatically.
126 void setHiddenFilesShown(bool show
);
127 bool hiddenFilesShown() const;
130 * Turns on sorting by groups if \a enable is true.
132 void setGroupedSorting(bool grouped
);
133 bool groupedSorting() const;
136 * Returns the items of the view.
138 KFileItemList
items() const;
141 * @return The number of items. itemsCount() is faster in comparison
142 * to items().count().
144 int itemsCount() const;
147 * Returns the selected items. The list is empty if no item has been
150 KFileItemList
selectedItems() const;
153 * Returns the number of selected items (this is faster than
154 * invoking selectedItems().count()).
156 int selectedItemsCount() const;
159 * Marks the items indicated by \p urls to get selected after the
160 * directory DolphinView::url() has been loaded. Note that nothing
161 * gets selected if no loading of a directory has been triggered
162 * by DolphinView::setUrl() or DolphinView::reload().
164 void markUrlsAsSelected(const QList
<QUrl
> &urls
);
167 * Marks the item indicated by \p url to be scrolled to and as the
168 * current item after directory DolphinView::url() has been loaded.
170 void markUrlAsCurrent(const QUrl
& url
);
173 * All items that match the regular expression \a regexp will get selected
174 * if \a enabled is true and deselected if \a enabled is false.
176 * Note that to match the whole string the pattern should be anchored:
177 * - you can anchor the pattern with QRegularExpression::anchoredPattern()
178 * - if you use QRegularExpresssion::wildcardToRegularExpression(), don't use
179 * QRegularExpression::anchoredPattern() as the former already returns an
182 void selectItems(const QRegularExpression
®exp
, bool enabled
);
185 * Sets the zoom level to \a level. It is assured that the used
186 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
187 * ZoomLevelInfo::maximumLevel().
189 void setZoomLevel(int level
);
190 int zoomLevel() const;
193 * Resets the view's icon size to the default value
195 void resetZoomLevel();
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);
309 * Hides tooltip displayed over element.
311 void hideToolTip(const ToolTipManager::HideBehavior behavior
= ToolTipManager::HideBehavior::Later
);
315 * Changes the directory to \a url. If the current directory is equal to
316 * \a url, nothing will be done (use DolphinView::reload() instead).
318 void setUrl(const QUrl
& url
);
322 * @see DolphinView::selectedItems()
327 * Inverts the current selection: selected items get unselected,
328 * unselected items get selected.
329 * @see DolphinView::selectedItems()
331 void invertSelection();
333 void clearSelection();
336 * Triggers the renaming of the currently selected items, where
337 * the user must input a new name for the items.
339 void renameSelectedItems();
342 * Moves all selected items to the trash.
344 void trashSelectedItems();
347 * Deletes all selected items.
349 void deleteSelectedItems();
352 * Copies all selected items to the clipboard and marks
355 void cutSelectedItemsToClipboard();
357 /** Copies all selected items to the clipboard. */
358 void copySelectedItemsToClipboard();
361 * Copies all selected items to @p destinationUrl.
363 void copySelectedItems(const KFileItemList
&selection
, const QUrl
&destinationUrl
);
366 * Moves all selected items to @p destinationUrl.
368 void moveSelectedItems(const KFileItemList
&selection
, const QUrl
&destinationUrl
);
370 /** Pastes the clipboard data to this view. */
374 * Pastes the clipboard data into the currently selected
375 * folder. If the current selection is not exactly one folder, no
376 * paste operation is done.
378 void pasteIntoFolder();
381 * Copies the path of the first selected KFileItem into Clipboard.
383 void copyPathToClipboard();
386 * Creates duplicates of selected items, appending "copy"
389 void duplicateSelectedItems();
392 * Handles a drop of @p dropEvent onto widget @p dropWidget and destination @p destUrl
394 void dropUrls(const QUrl
&destUrl
, QDropEvent
*dropEvent
, QWidget
*dropWidget
);
399 * Applies the state that has been restored by restoreViewState()
402 void updateViewState();
404 /** Activates the view if the item list container gets focus. */
405 bool eventFilter(QObject
* watched
, QEvent
* event
) override
;
409 * Is emitted if the view has been activated by e. g. a mouse click.
413 /** Is emitted if the URL of the view has been changed to \a url. */
414 void urlChanged(const QUrl
& url
);
417 * Is emitted when clicking on an item with the left mouse button.
419 void itemActivated(const KFileItem
& item
);
422 * Is emitted when multiple items have been activated by e. g.
423 * context menu open with.
425 void itemsActivated(const KFileItemList
& items
);
428 * Is emitted if items have been added or deleted.
430 void itemCountChanged();
433 * Is emitted if a new tab should be opened for the URL \a url.
435 void tabRequested(const QUrl
& url
, DolphinTabWidget::TabPlacement tabPlacement
);
438 * Is emitted if the view mode (IconsView, DetailsView,
439 * PreviewsView) has been changed.
441 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
443 /** Is emitted if the 'show preview' property has been changed. */
444 void previewsShownChanged(bool shown
);
446 /** Is emitted if the 'show hidden files' property has been changed. */
447 void hiddenFilesShownChanged(bool shown
);
449 /** Is emitted if the 'grouped sorting' property has been changed. */
450 void groupedSortingChanged(bool groupedSorting
);
452 /** Is emitted if the sorting by name, size or date has been changed. */
453 void sortRoleChanged(const QByteArray
& role
);
455 /** Is emitted if the sort order (ascending or descending) has been changed. */
456 void sortOrderChanged(Qt::SortOrder order
);
459 * Is emitted if the sorting of files and folders (separate with folders
460 * first or mixed) has been changed.
462 void sortFoldersFirstChanged(bool foldersFirst
);
464 /** Is emitted if the additional information shown for this view has been changed. */
465 void visibleRolesChanged(const QList
<QByteArray
>& current
,
466 const QList
<QByteArray
>& previous
);
468 /** Is emitted if the zoom level has been changed by zooming in or out. */
469 void zoomLevelChanged(int current
, int previous
);
472 * Is emitted if information of an item is requested to be shown e. g. in the panel.
473 * If item is null, no item information request is pending.
475 void requestItemInfo(const KFileItem
& item
);
478 * Is emitted whenever the selection has been changed.
480 void selectionChanged(const KFileItemList
& selection
);
483 * Is emitted if a context menu is requested for the item \a item,
484 * which is part of \a url. If the item is null, the context menu
485 * for the URL should be shown and the custom actions \a customActions
488 void requestContextMenu(const QPoint
& pos
,
489 const KFileItem
& item
,
491 const QList
<QAction
*>& customActions
);
494 * Is emitted if an information message with the content \a msg
497 void infoMessage(const QString
& msg
);
500 * Is emitted if an error message with the content \a msg
503 void errorMessage(const QString
& msg
);
506 * Is emitted if an "operation completed" message with the content \a msg
509 void operationCompletedMessage(const QString
& msg
);
512 * Is emitted after DolphinView::setUrl() has been invoked and
513 * the current directory is loaded. If this signal is emitted,
514 * it is assured that the view contains already the correct root
515 * URL and property settings.
517 void directoryLoadingStarted();
520 * Is emitted after the directory triggered by DolphinView::setUrl()
523 void directoryLoadingCompleted();
526 * Is emitted after the directory loading triggered by DolphinView::setUrl()
529 void directoryLoadingCanceled();
532 * Is emitted after DolphinView::setUrl() has been invoked and provides
533 * the information how much percent of the current directory have been loaded.
535 void directoryLoadingProgress(int percent
);
538 * Is emitted if the sorting is done asynchronously and provides the
539 * progress information of the sorting.
541 void directorySortingProgress(int percent
);
544 * Emitted when the file-item-model emits redirection.
545 * Testcase: fish://localhost
547 void redirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
550 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
551 * In this case no signal errorMessage() will be emitted.
553 void urlIsFileError(const QUrl
& url
);
556 * Is emitted when the write state of the folder has been changed. The application
557 * should disable all actions like "Create New..." that depend on the write
560 void writeStateChanged(bool isFolderWritable
);
563 * Is emitted if the URL should be changed to the previous URL of the
564 * history (e.g. because the "back"-mousebutton has been pressed).
566 void goBackRequested();
569 * Is emitted if the URL should be changed to the next URL of the
570 * history (e.g. because the "next"-mousebutton has been pressed).
572 void goForwardRequested();
575 * Is emitted when the user wants to move the focus to another view.
577 void toggleActiveViewRequested();
580 * Is emitted when the user clicks a tag or a link
581 * in the metadata widget of a tooltip.
583 void urlActivated(const QUrl
& url
);
585 void goUpRequested();
588 /** Changes the zoom level if Control is pressed during a wheel event. */
589 void wheelEvent(QWheelEvent
* event
) override
;
591 void hideEvent(QHideEvent
* event
) override
;
592 bool event(QEvent
* event
) override
;
596 * Marks the view as active (DolphinView:isActive() will return true)
597 * and emits the 'activated' signal if it is not already active.
601 void slotItemActivated(int index
);
602 void slotItemsActivated(const KItemSet
& indexes
);
603 void slotItemMiddleClicked(int index
);
604 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
605 void slotViewContextMenuRequested(const QPointF
& pos
);
606 void slotHeaderContextMenuRequested(const QPointF
& pos
);
607 void slotHeaderColumnWidthChangeFinished(const QByteArray
& role
, qreal current
);
608 void slotItemHovered(int index
);
609 void slotItemUnhovered(int index
);
610 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
611 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
612 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
613 void slotRenameDialogRenamingFinished(const QList
<QUrl
>& urls
);
614 void slotSelectedItemTextPressed(int index
);
615 void slotCopyingDone(KIO::Job
*, const QUrl
&, const QUrl
&to
);
616 void slotIncreaseZoom();
617 void slotDecreaseZoom();
621 * Is called when new items get pasted or dropped.
623 void slotItemCreated(const QUrl
&url
);
625 * Is called after all pasted or dropped items have been copied to destination.
627 void slotJobResult(KJob
*job
);
630 * Emits the signal \a selectionChanged() with a small delay. This is
631 * because getting all file items for the selection can be an expensive
632 * operation. Fast selection changes are collected in this case and
633 * the signal is emitted only after no selection change has been done
634 * within a small delay.
636 void slotSelectionChanged(const KItemSet
& current
, const KItemSet
& previous
);
639 * Is called by emitDelayedSelectionChangedSignal() and emits the
640 * signal \a selectionChanged() with all selected file items as parameter.
642 void emitSelectionChangedSignal();
645 * Updates the view properties of the current URL to the
646 * sorting given by \a role.
648 void updateSortRole(const QByteArray
& role
);
651 * Updates the view properties of the current URL to the
652 * sort order given by \a order.
654 void updateSortOrder(Qt::SortOrder order
);
657 * Updates the view properties of the current URL to the
658 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
660 void updateSortFoldersFirst(bool foldersFirst
);
663 * Indicates in the status bar that the delete operation
664 * of the job \a job has been finished.
666 void slotDeleteFileFinished(KJob
* job
);
669 * Indicates in the status bar that the trash operation
670 * of the job \a job has been finished.
672 void slotTrashFileFinished(KJob
* job
);
675 * Invoked when the rename job is done, for error handling.
677 void slotRenamingResult(KJob
* job
);
680 * Invoked when the file item model has started the loading
681 * of the directory specified by DolphinView::url().
683 void slotDirectoryLoadingStarted();
686 * Invoked when the file item model indicates that the loading of a directory has
687 * been completed. Assures that pasted items and renamed items get selected.
689 void slotDirectoryLoadingCompleted();
692 * Is invoked when items of KFileItemModel have been changed.
694 void slotItemsChanged();
697 * Is invoked when the sort order has been changed by the user by clicking
698 * on a header item. The view properties of the directory will get updated.
700 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
703 * Is invoked when the sort role has been changed by the user by clicking
704 * on a header item. The view properties of the directory will get updated.
706 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
709 * Is invoked when the visible roles have been changed by the user by dragging
710 * a header item. The view properties of the directory will get updated.
712 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
713 const QList
<QByteArray
>& previous
);
715 void slotRoleEditingCanceled();
716 void slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
719 * Observes the item with the URL \a url. As soon as the directory
720 * model indicates that the item is available, the item will
721 * get selected and it is assured that the item stays visible.
723 void observeCreatedItem(const QUrl
&url
);
726 * Called when a redirection happens.
727 * Testcase: fish://localhost
729 void slotDirectoryRedirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
732 * Calculates the number of currently shown files into
733 * \a fileCount and the number of folders into \a folderCount.
734 * The size of all files is written into \a totalFileSize.
735 * It is recommend using this method instead of asking the
736 * directory lister or the model directly, as it takes
737 * filtering and hierarchical previews into account.
739 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
741 void slotTwoClicksRenamingTimerTimeout();
744 void loadDirectory(const QUrl
& url
, bool reload
= false);
747 * Applies the view properties which are defined by the current URL
748 * to the DolphinView properties. The view properties are read from a
749 * .directory file either in the current directory, or in the
750 * share/apps/dolphin/view_properties/ subfolder of the user's .kde folder.
752 void applyViewProperties();
755 * Applies the given view properties to the DolphinView.
757 void applyViewProperties(const ViewProperties
& props
);
760 * Applies the m_mode property to the corresponding
761 * itemlayout-property of the KItemListView.
763 void applyModeToView();
766 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
767 * Pastes the clipboard data into the URL \a url.
769 void pasteToUrl(const QUrl
& url
);
772 * Returns a list of URLs for all selected items. The list is
773 * simplified, so that when the URLs are part of different tree
774 * levels, only the parent is returned.
776 QList
<QUrl
> simplifiedSelectedUrls() const;
779 * Returns the MIME data for all selected items.
781 QMimeData
* selectionMimeData() const;
784 * Updates m_isFolderWritable dependent on whether the folder represented by
785 * the current URL is writable. If the state has changed, the signal
786 * writeableStateChanged() will be emitted.
788 void updateWritableState();
791 * @return The current URL if no viewproperties-context is given (see
792 * DolphinView::viewPropertiesContext(), otherwise the context
795 QUrl
viewPropertiesUrl() const;
798 * Clears the selection and updates current item and selection according to the parameters
800 * @param current URL to be set as current
801 * @param selected list of selected items
803 void forceUrlsSelection(const QUrl
& current
, const QList
<QUrl
>& selected
);
805 void abortTwoClicksRenaming();
808 void updatePalette();
812 bool m_assureVisibleCurrentIndex
;
813 bool m_isFolderWritable
;
814 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
815 // tooltip may be shown when hovering an item.
818 QString m_viewPropertiesContext
;
820 QList
<QByteArray
> m_visibleRoles
;
822 QVBoxLayout
* m_topLayout
;
824 KFileItemModel
* m_model
;
825 DolphinItemListView
* m_view
;
826 KItemListContainer
* m_container
;
828 ToolTipManager
* m_toolTipManager
;
830 QTimer
* m_selectionChangedTimer
;
832 QUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
833 bool m_scrollToCurrentItem
; // Used for marking we need to scroll to current item or not
834 QPoint m_restoredContentsPosition
;
836 QList
<QUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
837 bool m_clearSelectionBeforeSelectingNewItems
;
838 bool m_markFirstNewlySelectedItemAsCurrent
;
840 VersionControlObserver
* m_versionControlObserver
;
842 QTimer
* m_twoClicksRenamingTimer
;
843 QUrl m_twoClicksRenamingItemUrl
;
846 friend class TestBase
;
847 friend class DolphinDetailsViewTest
;
848 friend class DolphinPart
; // Accesses m_model
851 /// Allow using DolphinView::Mode in QVariant
852 Q_DECLARE_METATYPE(DolphinView::Mode
)
854 #endif // DOLPHINVIEW_H