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
;
36 namespace SelectionMode
42 * @short Represents a view for the directory content
43 * including the navigation bar, filter bar and status bar.
45 * View modes for icons, compact and details are supported. Currently
46 * Dolphin allows to have up to two views inside the main window.
51 * @see DolphinStatusBar
53 class DolphinViewContainer
: public QWidget
58 DolphinViewContainer(const QUrl
&url
, QWidget
*parent
);
59 ~DolphinViewContainer() override
;
62 * Returns the current active URL, where all actions are applied.
63 * The URL navigator is synchronized with this URL.
66 KFileItem
rootItem() const;
69 * If \a active is true, the view container will marked as active. The active
70 * view container is defined as view where all actions are applied to.
72 void setActive(bool active
);
73 bool isActive() const;
76 * If \a grab is set to true, the container automatically grabs the focus
77 * as soon as the URL has been changed. Per default the grabbing
78 * of the focus is enabled.
80 void setAutoGrabFocus(bool grab
);
81 bool autoGrabFocus() const;
83 QString
currentSearchText() const;
85 const DolphinStatusBar
*statusBar() const;
86 DolphinStatusBar
*statusBar();
89 * @return An UrlNavigator that is controlling this view
90 * or nullptr if there is none.
91 * @see connectUrlNavigator()
92 * @see disconnectUrlNavigator()
94 * Use urlNavigatorInternalWithHistory() if you want to access the history.
95 * @see urlNavigatorInternalWithHistory()
97 const DolphinUrlNavigator
*urlNavigator() const;
99 * @return An UrlNavigator that is controlling this view
100 * or nullptr if there is none.
101 * @see connectUrlNavigator()
102 * @see disconnectUrlNavigator()
104 * Use urlNavigatorInternalWithHistory() if you want to access the history.
105 * @see urlNavigatorInternalWithHistory()
107 DolphinUrlNavigator
*urlNavigator();
110 * @return An UrlNavigator that contains this view's history.
111 * Use urlNavigator() instead when not accessing the history.
113 const DolphinUrlNavigator
*urlNavigatorInternalWithHistory() const;
115 * @return An UrlNavigator that contains this view's history.
116 * Use urlNavigator() instead when not accessing the history.
118 DolphinUrlNavigator
*urlNavigatorInternalWithHistory();
120 const DolphinView
*view() const;
124 * @param urlNavigator The UrlNavigator that is supposed to control
127 void connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
);
130 * Disconnects the navigator that is currently controlling the view.
131 * This method completely reverses connectUrlNavigator().
133 void disconnectUrlNavigator();
136 * Sets a selection mode that is useful for quick and easy selecting or deselecting of files.
137 * This method is the central authority about enabling or disabling selection mode:
138 * All other classes that want to enable or disable selection mode should trigger a call of this method.
140 * This method sets the selection mode for the view of this viewContainer and sets the visibility of the
141 * selection mode top and bottom bar which also belong to this viewContainer.
143 * @param enabled Whether to enable or disable selection mode.
144 * @param actionCollection The collection of actions from which the actions on the bottom bar are retrieved.
145 * @param bottomBarContents The contents the bar is supposed to show after this call.
147 void setSelectionModeEnabled(bool enabled
,
148 KActionCollection
*actionCollection
= nullptr,
149 SelectionMode::BottomBar::Contents bottomBarContents
= SelectionMode::BottomBar::Contents::GeneralContents
);
150 /** @see setSelectionModeEnabled() */
151 bool isSelectionModeEnabled() const;
154 * Shows the message \message with the given type \messageType non-modal above the view-content.
155 * \buttonActions defines actions which the user can trigger as a response to this message. They are presented as buttons below the \message.
157 void showMessage(const QString
&message
, KMessageWidget::MessageType messageType
, std::initializer_list
<QAction
*> buttonActions
= {});
160 * Refreshes the view container to get synchronized with the (updated) Dolphin settings.
164 /** @returns true, if the filter bar is visible.
165 * false, if it is hidden or currently animating towards a hidden state. */
166 bool isFilterBarVisible() const;
168 /** Returns true if the search mode is enabled. */
169 bool isSearchModeEnabled() const;
172 * @return Text that should be used for the current URL when creating
175 QString
placesText() const;
178 * Reload the view of this container. This will also hide messages in a messagewidget.
183 * @return Returns a Caption suitable for display in the window title.
184 * It is calculated depending on GeneralSettings::showFullPathInTitlebar().
185 * If it's false, it calls caption().
187 QString
captionWindowTitle() const;
190 * @return Returns a Caption suitable for display to the user. It is
191 * calculated depending on settings, if a search is active and other
194 QString
caption() const;
197 * Disable/enable the behavior of "select child when moving to parent folder"
198 * offered by KUrlNavigator.
200 * See KUrlNavigator::urlSelectionRequested
202 void disableUrlNavigatorSelectionRequests();
203 void enableUrlNavigatorSelectionRequests();
204 void clearFilterBar();
208 * Sets the current active URL, where all actions are applied. The
209 * URL navigator is synchronized with this URL. The signals
210 * KUrlNavigator::urlChanged() and KUrlNavigator::historyChanged()
212 * @see DolphinViewContainer::urlNavigator()
214 void setUrl(const QUrl
&url
);
217 * Popups the filter bar above the status bar if \a visible is true.
218 * It \a visible is true, it is assured that the filter bar gains
219 * the keyboard focus.
221 void setFilterBarVisible(bool visible
);
224 * Enables the search mode, if \p enabled is true. In the search mode the URL navigator
225 * will be hidden and replaced by a line editor that allows to enter a search term.
227 void setSearchModeEnabled(bool enabled
);
229 /** Used to notify the m_selectionModeBottomBar that there is no other ViewContainer in the tab. */
230 void slotSplitTabDisabled();
234 * Is emitted whenever the filter bar has changed its visibility state.
236 void showFilterBarChanged(bool shown
);
238 * Is emitted whenever the search mode has changed its state.
240 void searchModeEnabledChanged(bool enabled
);
242 void selectionModeChanged(bool enabled
);
245 * Is emitted when the write state of the folder has been changed. The application
246 * should disable all actions like "Create New..." that depend on the write
249 void writeStateChanged(bool isFolderWritable
);
252 * Is emitted when the Caption has been changed.
253 * @see DolphinViewContainer::caption()
255 void captionChanged();
258 * Is emitted if a new tab should be opened in the background for the URL \a url.
260 void tabRequested(const QUrl
&url
);
263 * Is emitted if a new tab should be opened for the URL \a url and set as active.
265 void activeTabRequested(const QUrl
&url
);
269 * Updates the number of items (= number of files + number of
270 * directories) in the statusbar. If files are selected, the number
271 * of selected files and the sum of the filesize is shown. The update
272 * is done asynchronously, as getting the sum of the
273 * filesizes can be an expensive operation.
274 * Unless a previous OperationCompletedMessage was set very shortly before
275 * calling this method, it will be overwritten (see DolphinStatusBar::setMessage).
276 * Previous ErrorMessages however are always preserved.
278 void delayedStatusBarUpdate();
281 * Is invoked by DolphinViewContainer::delayedStatusBarUpdate() and
282 * updates the status bar synchronously.
284 void updateStatusBar();
287 * Updates the statusbar to show an undetermined progress with the correct
288 * context information whether a searching or a directory loading is done.
290 void slotDirectoryLoadingStarted();
293 * Assures that the viewport position is restored and updates the
294 * statusbar to reflect the current content.
296 void slotDirectoryLoadingCompleted();
299 * Updates the statusbar to show, that the directory loading has
302 void slotDirectoryLoadingCanceled();
305 * Is called if the URL set by DolphinView::setUrl() represents
306 * a file and not a directory. Takes care to activate the file.
308 void slotUrlIsFileError(const QUrl
&url
);
311 * Handles clicking on an item. If the item is a directory, the
312 * directory is opened in the view. If the item is a file, the file
313 * gets started by the corresponding application.
315 void slotItemActivated(const KFileItem
&item
);
318 * Handles activation of multiple files. The files get started by
319 * the corresponding applications.
321 void slotItemsActivated(const KFileItemList
&items
);
324 * Handles middle click of file. It opens the file passed using the second application associated with the file's mimetype.
326 void slotfileMiddleClickActivated(const KFileItem
&item
);
329 * Shows the information for the item \a item inside the statusbar. If the
330 * item is null, the default statusbar information is shown.
332 void showItemInfo(const KFileItem
&item
);
334 void closeFilterBar();
337 * Filters the currently shown items by \a nameFilter. All items
338 * which contain the given filter string will be shown.
340 void setNameFilter(const QString
&nameFilter
);
343 * Marks the view container as active
344 * (see DolphinViewContainer::setActive()).
349 * Is invoked if the signal urlAboutToBeChanged() from the URL navigator
350 * is emitted. Tries to save the view-state.
352 void slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&url
);
355 * Restores the current view to show \a url and assures
356 * that the root URL of the view is respected.
358 void slotUrlNavigatorLocationChanged(const QUrl
&url
);
361 * @see KUrlNavigator::urlSelectionRequested
363 void slotUrlSelectionRequested(const QUrl
&url
);
366 * Is invoked when a redirection is done and changes the
367 * URL of the URL navigator to \a newUrl without triggering
368 * a reloading of the directory.
370 void redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
);
372 /** Requests the focus for the view \a m_view. */
376 * Gets the search URL from the searchbox and starts searching.
378 void startSearching();
379 void openSearchBox();
380 void closeSearchBox();
383 * Stops the loading of a directory. Is connected with the "stopPressed" signal
384 * from the statusbar.
386 void stopDirectoryLoading();
388 void slotStatusBarZoomLevelChanged(int zoomLevel
);
391 * Slot that calls showMessage(message, KMessageWidget::Error).
393 void showErrorMessage(const QString
&message
);
396 * Is invoked when a KFilePlacesModel has been changed
397 * @see DolphinPlacesModelSingleton::instance().placesModel()
399 void slotPlacesModelChanged();
401 void slotHiddenFilesShownChanged(bool showHiddenFiles
);
402 void slotSortHiddenLastChanged(bool hiddenLast
);
403 void slotCurrentDirectoryRemoved();
405 void slotOpenUrlFinished(KJob
*job
);
409 * @return True if the URL protocol is a search URL (e. g. baloosearch:// or filenamesearch://).
411 bool isSearchUrl(const QUrl
&url
) const;
414 * Saves the state of the current view: contents position,
417 void saveViewState();
420 * Restores the state of the current view iff the URL navigator contains a
421 * non-empty location state.
423 void tryRestoreViewState();
426 * @return Path of nearest existing ancestor directory.
428 QString
getNearestExistingAncestorOfPath(const QString
&path
) const;
431 QGridLayout
*m_topLayout
;
434 * The internal UrlNavigator which is never visible to the user.
435 * m_urlNavigator is used even when another UrlNavigator is controlling
436 * the view to keep track of this object's history.
438 std::unique_ptr
<DolphinUrlNavigator
> m_urlNavigator
;
441 * The UrlNavigator that is currently connected to the view.
442 * This is a nullptr if no UrlNavigator is connected.
443 * Otherwise it's one of the UrlNavigators visible in the toolbar.
445 QPointer
<DolphinUrlNavigator
> m_urlNavigatorConnected
;
447 DolphinSearchBox
*m_searchBox
;
448 bool m_searchModeEnabled
;
450 /// A bar shown at the top of the view to signify that the view is currently viewed and acted on with elevated privileges.
451 Admin::Bar
*m_adminBar
;
453 KMessageWidget
*m_messageWidget
;
455 /// A bar shown at the top of the view to signify that selection mode is currently active.
456 SelectionMode::TopBar
*m_selectionModeTopBar
;
460 FilterBar
*m_filterBar
;
462 /// A bar shown at the bottom of the view whose contents depend on what the user is currently doing.
463 SelectionMode::BottomBar
*m_selectionModeBottomBar
;
465 DolphinStatusBar
*m_statusBar
;
466 QTimer
*m_statusBarTimer
; // Triggers a delayed update
467 QElapsedTimer m_statusBarTimestamp
; // Time in ms since last update
468 bool m_autoGrabFocus
;
470 * The visual state to be applied to the next UrlNavigator that gets
471 * connected to this ViewContainer.
473 std::unique_ptr
<DolphinUrlNavigator::VisualState
> m_urlNavigatorVisualState
;
476 #endif // DOLPHINVIEWCONTAINER_H