2 * SPDX-FileCopyrightText: 2007 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #ifndef DOLPHINVIEWCONTAINER_H
8 #define DOLPHINVIEWCONTAINER_H
10 #include "config-dolphin.h"
11 #include "dolphinurlnavigator.h"
12 #include "selectionmode/bottombar.h"
13 #include "views/dolphinview.h"
17 #include <KMessageWidget>
18 #include <KUrlNavigator>
20 #include <QElapsedTimer>
21 #include <QPushButton>
24 #include <initializer_list>
34 class DolphinSearchBox
;
35 class DolphinStatusBar
;
37 namespace SelectionMode
43 * @short Represents a view for the directory content
44 * including the navigation bar, filter bar and status bar.
46 * View modes for icons, compact and details are supported. Currently
47 * Dolphin allows to have up to two views inside the main window.
52 * @see DolphinStatusBar
54 class DolphinViewContainer
: public QWidget
59 DolphinViewContainer(const QUrl
&url
, QWidget
*parent
);
60 ~DolphinViewContainer() override
;
63 * Returns the current active URL, where all actions are applied.
64 * The URL navigator is synchronized with this URL.
67 KFileItem
rootItem() const;
70 * If \a active is true, the view container will marked as active. The active
71 * view container is defined as view where all actions are applied to.
73 void setActive(bool active
);
74 bool isActive() const;
77 * If \a grab is set to true, the container automatically grabs the focus
78 * as soon as the URL has been changed. Per default the grabbing
79 * of the focus is enabled.
81 void setAutoGrabFocus(bool grab
);
82 bool autoGrabFocus() const;
84 QString
currentSearchText() const;
86 const DolphinStatusBar
*statusBar() const;
87 DolphinStatusBar
*statusBar();
90 * @return An UrlNavigator that is controlling this view
91 * or nullptr if there is none.
92 * @see connectUrlNavigator()
93 * @see disconnectUrlNavigator()
95 * Use urlNavigatorInternalWithHistory() if you want to access the history.
96 * @see urlNavigatorInternalWithHistory()
98 const DolphinUrlNavigator
*urlNavigator() const;
100 * @return An UrlNavigator that is controlling this view
101 * or nullptr if there is none.
102 * @see connectUrlNavigator()
103 * @see disconnectUrlNavigator()
105 * Use urlNavigatorInternalWithHistory() if you want to access the history.
106 * @see urlNavigatorInternalWithHistory()
108 DolphinUrlNavigator
*urlNavigator();
111 * @return An UrlNavigator that contains this view's history.
112 * Use urlNavigator() instead when not accessing the history.
114 const DolphinUrlNavigator
*urlNavigatorInternalWithHistory() const;
116 * @return An UrlNavigator that contains this view's history.
117 * Use urlNavigator() instead when not accessing the history.
119 DolphinUrlNavigator
*urlNavigatorInternalWithHistory();
121 const DolphinView
*view() const;
125 * @param urlNavigator The UrlNavigator that is supposed to control
128 void connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
);
131 * Disconnects the navigator that is currently controlling the view.
132 * This method completely reverses connectUrlNavigator().
134 void disconnectUrlNavigator();
137 * Sets a selection mode that is useful for quick and easy selecting or deselecting of files.
138 * This method is the central authority about enabling or disabling selection mode:
139 * All other classes that want to enable or disable selection mode should trigger a call of this method.
141 * This method sets the selection mode for the view of this viewContainer and sets the visibility of the
142 * selection mode top and bottom bar which also belong to this viewContainer.
144 * @param enabled Whether to enable or disable selection mode.
145 * @param actionCollection The collection of actions from which the actions on the bottom bar are retrieved.
146 * @param bottomBarContents The contents the bar is supposed to show after this call.
148 void setSelectionModeEnabled(bool enabled
,
149 KActionCollection
*actionCollection
= nullptr,
150 SelectionMode::BottomBar::Contents bottomBarContents
= SelectionMode::BottomBar::Contents::GeneralContents
);
151 /** @see setSelectionModeEnabled() */
152 bool isSelectionModeEnabled() const;
155 * Shows the message \message with the given type \messageType non-modal above the view-content.
156 * \buttonActions defines actions which the user can trigger as a response to this message. They are presented as buttons below the \message.
158 void showMessage(const QString
&message
, KMessageWidget::MessageType messageType
, std::initializer_list
<QAction
*> buttonActions
= {});
161 * Refreshes the view container to get synchronized with the (updated) Dolphin settings.
165 /** @returns true, if the filter bar is visible.
166 * false, if it is hidden or currently animating towards a hidden state. */
167 bool isFilterBarVisible() const;
169 /** Returns true if the search mode is enabled. */
170 bool isSearchModeEnabled() const;
173 * @return Text that should be used for the current URL when creating
176 QString
placesText() const;
179 * Reload the view of this container. This will also hide messages in a messagewidget.
184 * @return Returns a Caption suitable for display in the window title.
185 * It is calculated depending on GeneralSettings::showFullPathInTitlebar().
186 * If it's false, it calls caption().
188 QString
captionWindowTitle() const;
191 * @return Returns a Caption suitable for display to the user. It is
192 * calculated depending on settings, if a search is active and other
195 QString
caption() const;
198 * Disable/enable the behavior of "select child when moving to parent folder"
199 * offered by KUrlNavigator.
201 * See KUrlNavigator::urlSelectionRequested
203 void disableUrlNavigatorSelectionRequests();
204 void enableUrlNavigatorSelectionRequests();
205 void clearFilterBar();
209 * Sets the current active URL, where all actions are applied. The
210 * URL navigator is synchronized with this URL. The signals
211 * KUrlNavigator::urlChanged() and KUrlNavigator::historyChanged()
213 * @see DolphinViewContainer::urlNavigator()
215 void setUrl(const QUrl
&url
);
218 * Popups the filter bar above the status bar if \a visible is true.
219 * It \a visible is true, it is assured that the filter bar gains
220 * the keyboard focus.
222 void setFilterBarVisible(bool visible
);
225 * Enables the search mode, if \p enabled is true. In the search mode the URL navigator
226 * will be hidden and replaced by a line editor that allows to enter a search term.
228 void setSearchModeEnabled(bool enabled
);
230 /** Used to notify the m_selectionModeBottomBar that there is no other ViewContainer in the tab. */
231 void slotSplitTabDisabled();
235 * Is emitted whenever the filter bar has changed its visibility state.
237 void showFilterBarChanged(bool shown
);
239 * Is emitted whenever the search mode has changed its state.
241 void searchModeEnabledChanged(bool enabled
);
243 void selectionModeChanged(bool enabled
);
246 * Is emitted when the write state of the folder has been changed. The application
247 * should disable all actions like "Create New..." that depend on the write
250 void writeStateChanged(bool isFolderWritable
);
253 * Is emitted when the Caption has been changed.
254 * @see DolphinViewContainer::caption()
256 void captionChanged();
259 * Is emitted if a new tab should be opened in the background for the URL \a url.
261 void tabRequested(const QUrl
&url
);
264 * Is emitted if a new tab should be opened for the URL \a url and set as active.
266 void activeTabRequested(const QUrl
&url
);
270 * Updates the number of items (= number of files + number of
271 * directories) in the statusbar. If files are selected, the number
272 * of selected files and the sum of the filesize is shown. The update
273 * is done asynchronously, as getting the sum of the
274 * filesizes can be an expensive operation.
275 * Unless a previous OperationCompletedMessage was set very shortly before
276 * calling this method, it will be overwritten (see DolphinStatusBar::setMessage).
277 * Previous ErrorMessages however are always preserved.
279 void delayedStatusBarUpdate();
282 * Is invoked by DolphinViewContainer::delayedStatusBarUpdate() and
283 * updates the status bar synchronously.
285 void updateStatusBar();
288 * Updates the statusbar to show an undetermined progress with the correct
289 * context information whether a searching or a directory loading is done.
291 void slotDirectoryLoadingStarted();
294 * Assures that the viewport position is restored and updates the
295 * statusbar to reflect the current content.
297 void slotDirectoryLoadingCompleted();
300 * Updates the statusbar to show, that the directory loading has
303 void slotDirectoryLoadingCanceled();
306 * Is called if the URL set by DolphinView::setUrl() represents
307 * a file and not a directory. Takes care to activate the file.
309 void slotUrlIsFileError(const QUrl
&url
);
312 * Handles clicking on an item. If the item is a directory, the
313 * directory is opened in the view. If the item is a file, the file
314 * gets started by the corresponding application.
316 void slotItemActivated(const KFileItem
&item
);
319 * Handles activation of multiple files. The files get started by
320 * the corresponding applications.
322 void slotItemsActivated(const KFileItemList
&items
);
325 * Handles middle click of file. It opens the file passed using the second application associated with the file's mimetype.
327 void slotfileMiddleClickActivated(const KFileItem
&item
);
330 * Shows the information for the item \a item inside the statusbar. If the
331 * item is null, the default statusbar information is shown.
333 void showItemInfo(const KFileItem
&item
);
335 void closeFilterBar();
338 * Filters the currently shown items by \a nameFilter. All items
339 * which contain the given filter string will be shown.
341 void setNameFilter(const QString
&nameFilter
);
344 * Marks the view container as active
345 * (see DolphinViewContainer::setActive()).
350 * Is invoked if the signal urlAboutToBeChanged() from the URL navigator
351 * is emitted. Tries to save the view-state.
353 void slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&url
);
356 * Restores the current view to show \a url and assures
357 * that the root URL of the view is respected.
359 void slotUrlNavigatorLocationChanged(const QUrl
&url
);
362 * @see KUrlNavigator::urlSelectionRequested
364 void slotUrlSelectionRequested(const QUrl
&url
);
367 * Is invoked when a redirection is done and changes the
368 * URL of the URL navigator to \a newUrl without triggering
369 * a reloading of the directory.
371 void redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
);
373 /** Requests the focus for the view \a m_view. */
377 * Gets the search URL from the searchbox and starts searching.
379 void startSearching();
380 void openSearchBox();
381 void closeSearchBox();
384 * Stops the loading of a directory. Is connected with the "stopPressed" signal
385 * from the statusbar.
387 void stopDirectoryLoading();
389 void slotStatusBarZoomLevelChanged(int zoomLevel
);
392 * Creates and shows an error message based on \p message and \p kioErrorCode.
394 void slotErrorMessageFromView(const QString
&message
, const int kioErrorCode
);
397 * Slot that calls showMessage(message, KMessageWidget::Error).
399 void showErrorMessage(const QString
&message
);
402 * Is invoked when a KFilePlacesModel has been changed
403 * @see DolphinPlacesModelSingleton::instance().placesModel()
405 void slotPlacesModelChanged();
407 void slotHiddenFilesShownChanged(bool showHiddenFiles
);
408 void slotSortHiddenLastChanged(bool hiddenLast
);
409 void slotCurrentDirectoryRemoved();
411 void slotOpenUrlFinished(KJob
*job
);
415 * @return True if the URL protocol is a search URL (e. g. baloosearch:// or filenamesearch://).
417 bool isSearchUrl(const QUrl
&url
) const;
420 * Saves the state of the current view: contents position,
423 void saveViewState();
426 * Restores the state of the current view iff the URL navigator contains a
427 * non-empty location state.
429 void tryRestoreViewState();
432 * @return Path of nearest existing ancestor directory.
434 QString
getNearestExistingAncestorOfPath(const QString
&path
) const;
437 * Update the geometry of statusbar depending on what mode it is using.
439 void updateStatusBarGeometry();
442 * @return Preferred geometry of the small statusbar.
444 QRect
preferredSmallStatusBarGeometry();
447 bool eventFilter(QObject
*object
, QEvent
*event
) override
;
450 QGridLayout
*m_topLayout
;
453 * The internal UrlNavigator which is never visible to the user.
454 * m_urlNavigator is used even when another UrlNavigator is controlling
455 * the view to keep track of this object's history.
457 std::unique_ptr
<DolphinUrlNavigator
> m_urlNavigator
;
460 * The UrlNavigator that is currently connected to the view.
461 * This is a nullptr if no UrlNavigator is connected.
462 * Otherwise it's one of the UrlNavigators visible in the toolbar.
464 QPointer
<DolphinUrlNavigator
> m_urlNavigatorConnected
;
466 DolphinSearchBox
*m_searchBox
;
467 bool m_searchModeEnabled
;
469 /// A bar shown at the top of the view to signify that the view is currently viewed and acted on with elevated privileges.
470 Admin::Bar
*m_adminBar
;
471 /// An action to switch to the admin protocol. This variable will always be nullptr unless kio-admin was installed. @see Admin::WorkerIntegration.
472 QAction
*m_authorizeToEnterFolderAction
;
474 KMessageWidget
*m_messageWidget
;
476 /// A bar shown at the top of the view to signify that selection mode is currently active.
477 SelectionMode::TopBar
*m_selectionModeTopBar
;
481 FilterBar
*m_filterBar
;
483 /// A bar shown at the bottom of the view whose contents depend on what the user is currently doing.
484 SelectionMode::BottomBar
*m_selectionModeBottomBar
;
486 DolphinStatusBar
*m_statusBar
;
487 QTimer
*m_statusBarTimer
; // Triggers a delayed update
488 QElapsedTimer m_statusBarTimestamp
; // Time in ms since last update
489 bool m_autoGrabFocus
;
491 * The visual state to be applied to the next UrlNavigator that gets
492 * connected to this ViewContainer.
494 std::unique_ptr
<DolphinUrlNavigator::VisualState
> m_urlNavigatorVisualState
;
497 #endif // DOLPHINVIEWCONTAINER_H