]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.h
GIT_SILENT Update Appstream for new release
[dolphin.git] / src / dolphinviewcontainer.h
1 /*
2 * SPDX-FileCopyrightText: 2007 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #ifndef DOLPHINVIEWCONTAINER_H
8 #define DOLPHINVIEWCONTAINER_H
9
10 #include "config-dolphin.h"
11 #include "dolphinurlnavigator.h"
12 #include "selectionmode/bottombar.h"
13 #include "views/dolphinview.h"
14
15 #include <KFileItem>
16 #include <KIO/Job>
17 #include <KMessageWidget>
18 #include <KUrlNavigator>
19
20 #include <QElapsedTimer>
21 #include <QPushButton>
22 #include <QWidget>
23
24 #include <initializer_list>
25
26 namespace Admin
27 {
28 class Bar;
29 }
30 class FilterBar;
31 class QAction;
32 class QGridLayout;
33 class QUrl;
34 namespace Search
35 {
36 class Bar;
37 }
38 class DolphinStatusBar;
39 class KFileItemList;
40 namespace SelectionMode
41 {
42 class TopBar;
43 }
44
45 /**
46 * @return True if the URL protocol is a search URL (e. g. baloosearch:// or filenamesearch://).
47 */
48 bool isSearchUrl(const QUrl &url);
49
50 /**
51 * @short Represents a view for the directory content
52 * including the navigation bar, filter bar and status bar.
53 *
54 * View modes for icons, compact and details are supported. Currently
55 * Dolphin allows to have up to two views inside the main window.
56 *
57 * @see DolphinView
58 * @see FilterBar
59 * @see KUrlNavigator
60 * @see DolphinStatusBar
61 */
62 class DolphinViewContainer : public QWidget
63 {
64 Q_OBJECT
65
66 public:
67 DolphinViewContainer(const QUrl &url, QWidget *parent);
68 ~DolphinViewContainer() override;
69
70 /**
71 * Returns the current active URL, where all actions are applied.
72 * The URL navigator is synchronized with this URL.
73 */
74 QUrl url() const;
75 KFileItem rootItem() const;
76
77 /**
78 * If \a active is true, the view container will marked as active. The active
79 * view container is defined as view where all actions are applied to.
80 */
81 void setActive(bool active);
82 bool isActive() const;
83
84 /**
85 * If \a grab is set to true, the container automatically grabs the focus
86 * as soon as the URL has been changed. Per default the grabbing
87 * of the focus is enabled.
88 */
89 void setGrabFocusOnUrlChange(bool grabFocus);
90
91 const DolphinStatusBar *statusBar() const;
92 DolphinStatusBar *statusBar();
93
94 /**
95 * @return An UrlNavigator that is controlling this view
96 * or nullptr if there is none.
97 * @see connectUrlNavigator()
98 * @see disconnectUrlNavigator()
99 *
100 * Use urlNavigatorInternalWithHistory() if you want to access the history.
101 * @see urlNavigatorInternalWithHistory()
102 */
103 const DolphinUrlNavigator *urlNavigator() const;
104 /**
105 * @return An UrlNavigator that is controlling this view
106 * or nullptr if there is none.
107 * @see connectUrlNavigator()
108 * @see disconnectUrlNavigator()
109 *
110 * Use urlNavigatorInternalWithHistory() if you want to access the history.
111 * @see urlNavigatorInternalWithHistory()
112 */
113 DolphinUrlNavigator *urlNavigator();
114
115 /**
116 * @return An UrlNavigator that contains this view's history.
117 * Use urlNavigator() instead when not accessing the history.
118 */
119 const DolphinUrlNavigator *urlNavigatorInternalWithHistory() const;
120 /**
121 * @return An UrlNavigator that contains this view's history.
122 * Use urlNavigator() instead when not accessing the history.
123 */
124 DolphinUrlNavigator *urlNavigatorInternalWithHistory();
125
126 const DolphinView *view() const;
127 DolphinView *view();
128
129 /**
130 * @param urlNavigator The UrlNavigator that is supposed to control
131 * this view.
132 */
133 void connectUrlNavigator(DolphinUrlNavigator *urlNavigator);
134
135 /**
136 * Disconnects the navigator that is currently controlling the view.
137 * This method completely reverses connectUrlNavigator().
138 */
139 void disconnectUrlNavigator();
140
141 /**
142 * Sets the visibility of this objects search configuration user interface. This search bar is the primary interface in Dolphin to search for files and
143 * folders.
144 *
145 * The signal searchBarVisibilityChanged will be emitted when the new visibility state is different from the old.
146 *
147 * Typically an animation will play when the search bar is shown or hidden, so the visibility of the bar will not necessarily match @p visible when this
148 * method returns. Instead use isSearchBarVisible(), which will always communicate the visibility state the search bar is heading to.
149 *
150 * @see Search::Bar.
151 * @see isSearchBarVisible().
152 */
153 void setSearchBarVisible(bool visible);
154
155 /** @returns true if the search bar is visible while not being in the process to hide itself. */
156 bool isSearchBarVisible() const;
157
158 /**
159 * Moves keyboard focus to the search bar. The search term is fully selected to allow easy replacing.
160 */
161 void setFocusToSearchBar();
162
163 /**
164 * Sets a selection mode that is useful for quick and easy selecting or deselecting of files.
165 * This method is the central authority about enabling or disabling selection mode:
166 * All other classes that want to enable or disable selection mode should trigger a call of this method.
167 *
168 * This method sets the selection mode for the view of this viewContainer and sets the visibility of the
169 * selection mode top and bottom bar which also belong to this viewContainer.
170 *
171 * @param enabled Whether to enable or disable selection mode.
172 * @param actionCollection The collection of actions from which the actions on the bottom bar are retrieved.
173 * @param bottomBarContents The contents the bar is supposed to show after this call.
174 */
175 void setSelectionModeEnabled(bool enabled,
176 KActionCollection *actionCollection = nullptr,
177 SelectionMode::BottomBar::Contents bottomBarContents = SelectionMode::BottomBar::Contents::GeneralContents);
178 /** @see setSelectionModeEnabled() */
179 bool isSelectionModeEnabled() const;
180
181 /**
182 * Shows the message \message with the given type \messageType non-modal above the view-content.
183 * \buttonActions defines actions which the user can trigger as a response to this message. They are presented as buttons below the \message.
184 */
185 void showMessage(const QString &message, KMessageWidget::MessageType messageType, std::initializer_list<QAction *> buttonActions = {});
186
187 /**
188 * Forwards to DolphinStatusBar::showProgress(). Only exception: The button to cancel the task is hidden.
189 * @see DolphinStatusBar::showProgress().
190 */
191 void showProgress(const QString &currentlyRunningTaskTitle, int progressPercent);
192
193 /**
194 * Refreshes the view container to get synchronized with the (updated) Dolphin settings.
195 */
196 void readSettings();
197
198 /** @returns true, if the filter bar is visible.
199 * false, if it is hidden or currently animating towards a hidden state. */
200 bool isFilterBarVisible() const;
201
202 /**
203 * @return Text that should be used for the current URL when creating
204 * a new place.
205 */
206 QString placesText() const;
207
208 /**
209 * Reload the view of this container. This will also hide messages in a messagewidget.
210 */
211 void reload();
212
213 /**
214 * @return Returns a Caption suitable for display in the window title.
215 * It is calculated depending on GeneralSettings::showFullPathInTitlebar().
216 * If it's false, it calls caption().
217 */
218 QString captionWindowTitle() const;
219
220 /**
221 * @return Returns a Caption suitable for display to the user. It is
222 * calculated depending on settings, if a search is active and other
223 * factors.
224 */
225 QString caption() const;
226
227 /**
228 * Disable/enable the behavior of "select child when moving to parent folder"
229 * offered by KUrlNavigator.
230 *
231 * See KUrlNavigator::urlSelectionRequested
232 */
233 void disableUrlNavigatorSelectionRequests();
234 void enableUrlNavigatorSelectionRequests();
235 void clearFilterBar();
236
237 public Q_SLOTS:
238 /**
239 * Sets the current active URL, where all actions are applied. The
240 * URL navigator is synchronized with this URL. The signals
241 * KUrlNavigator::urlChanged() and KUrlNavigator::historyChanged()
242 * are emitted.
243 * @see DolphinViewContainer::urlNavigator()
244 */
245 void setUrl(const QUrl &url);
246
247 /**
248 * Popups the filter bar above the status bar if \a visible is true.
249 * It \a visible is true, it is assured that the filter bar gains
250 * the keyboard focus.
251 */
252 void setFilterBarVisible(bool visible);
253
254 /** Used to notify the m_selectionModeBottomBar that there is no other ViewContainer in the tab. */
255 void slotSplitTabDisabled();
256
257 Q_SIGNALS:
258 /**
259 * Is emitted whenever the filter bar has changed its visibility state.
260 */
261 void showFilterBarChanged(bool shown);
262 /**
263 * Is emitted whenever a change to the search bar's visibility is invoked. The visibility change might not have actually already taken effect by the time
264 * this signal is emitted because typically the showing and hiding is animated.
265 * @param visible The visibility state the search bar is going to end up at.
266 * @see Search::Bar.
267 * @see setSearchBarVisible().
268 * @see isSearchBarVisible().
269 */
270 void searchBarVisibilityChanged(bool visible);
271
272 void selectionModeChanged(bool enabled);
273
274 /**
275 * Is emitted when the write state of the folder has been changed. The application
276 * should disable all actions like "Create New..." that depend on the write
277 * state.
278 */
279 void writeStateChanged(bool isFolderWritable);
280
281 /**
282 * Is emitted when the Caption has been changed.
283 * @see DolphinViewContainer::caption()
284 */
285 void captionChanged();
286
287 /**
288 * Is emitted if a new tab should be opened in the background for the URL \a url.
289 */
290 void tabRequested(const QUrl &url);
291
292 /**
293 * Is emitted if a new tab should be opened for the URL \a url and set as active.
294 */
295 void activeTabRequested(const QUrl &url);
296
297 private Q_SLOTS:
298 /**
299 * Updates the number of items (= number of files + number of
300 * directories) in the statusbar. If files are selected, the number
301 * of selected files and the sum of the filesize is shown. The update
302 * is done asynchronously, as getting the sum of the
303 * filesizes can be an expensive operation.
304 * Unless a previous OperationCompletedMessage was set very shortly before
305 * calling this method, it will be overwritten (see DolphinStatusBar::setMessage).
306 * Previous ErrorMessages however are always preserved.
307 */
308 void delayedStatusBarUpdate();
309
310 /**
311 * Is invoked by DolphinViewContainer::delayedStatusBarUpdate() and
312 * updates the status bar synchronously.
313 */
314 void updateStatusBar();
315
316 /**
317 * Updates the statusbar to show an undetermined progress with the correct
318 * context information whether a searching or a directory loading is done.
319 */
320 void slotDirectoryLoadingStarted();
321
322 /**
323 * Assures that the viewport position is restored and updates the
324 * statusbar to reflect the current content.
325 */
326 void slotDirectoryLoadingCompleted();
327
328 /**
329 * Updates the statusbar to show, that the directory loading has
330 * been canceled.
331 */
332 void slotDirectoryLoadingCanceled();
333
334 /**
335 * Is called if the URL set by DolphinView::setUrl() represents
336 * a file and not a directory. Takes care to activate the file.
337 */
338 void slotUrlIsFileError(const QUrl &url);
339
340 /**
341 * Handles clicking on an item. If the item is a directory, the
342 * directory is opened in the view. If the item is a file, the file
343 * gets started by the corresponding application.
344 */
345 void slotItemActivated(const KFileItem &item);
346
347 /**
348 * Handles activation of multiple files. The files get started by
349 * the corresponding applications.
350 */
351 void slotItemsActivated(const KFileItemList &items);
352
353 /**
354 * Handles middle click of file. It opens the file passed using the second application associated with the file's mimetype.
355 */
356 void slotfileMiddleClickActivated(const KFileItem &item);
357
358 /**
359 * Shows the information for the item \a item inside the statusbar. If the
360 * item is null, the default statusbar information is shown.
361 */
362 void showItemInfo(const KFileItem &item);
363
364 void closeFilterBar();
365
366 /**
367 * Filters the currently shown items by \a nameFilter. All items
368 * which contain the given filter string will be shown.
369 */
370 void setNameFilter(const QString &nameFilter);
371
372 /**
373 * Marks the view container as active
374 * (see DolphinViewContainer::setActive()).
375 */
376 void activate();
377
378 /**
379 * Is invoked if the signal urlAboutToBeChanged() from the URL navigator
380 * is emitted. Tries to save the view-state.
381 */
382 void slotUrlNavigatorLocationAboutToBeChanged(const QUrl &url);
383
384 /**
385 * Restores the current view to show \a url and assures
386 * that the root URL of the view is respected.
387 */
388 void slotUrlNavigatorLocationChanged(const QUrl &url);
389
390 /**
391 * @see KUrlNavigator::urlSelectionRequested
392 */
393 void slotUrlSelectionRequested(const QUrl &url);
394
395 /**
396 * Is invoked when a redirection is done and changes the
397 * URL of the URL navigator to \a newUrl without triggering
398 * a reloading of the directory.
399 */
400 void redirect(const QUrl &oldUrl, const QUrl &newUrl);
401
402 /** Requests the focus for the view \a m_view. */
403 void requestFocus();
404
405 /**
406 * Stops the loading of a directory. Is connected with the "stopPressed" signal
407 * from the statusbar.
408 */
409 void stopDirectoryLoading();
410
411 void slotStatusBarZoomLevelChanged(int zoomLevel);
412
413 /**
414 * Creates and shows an error message based on \p message and \p kioErrorCode.
415 */
416 void slotErrorMessageFromView(const QString &message, const int kioErrorCode);
417
418 /**
419 * Slot that calls showMessage(message, KMessageWidget::Error).
420 */
421 void showErrorMessage(const QString &message);
422
423 /**
424 * Is invoked when a KFilePlacesModel has been changed
425 * @see DolphinPlacesModelSingleton::instance().placesModel()
426 */
427 void slotPlacesModelChanged();
428
429 void slotHiddenFilesShownChanged(bool showHiddenFiles);
430 void slotSortHiddenLastChanged(bool hiddenLast);
431 void slotCurrentDirectoryRemoved();
432
433 void slotOpenUrlFinished(KJob *job);
434
435 private:
436 /**
437 * Saves the state of the current view: contents position,
438 * root URL, ...
439 */
440 void saveViewState();
441
442 /**
443 * Restores the state of the current view iff the URL navigator contains a
444 * non-empty location state.
445 */
446 void tryRestoreViewState();
447
448 /**
449 * @return Path of nearest existing ancestor directory.
450 */
451 QString getNearestExistingAncestorOfPath(const QString &path) const;
452
453 /**
454 * Update the geometry of statusbar depending on what mode it is using.
455 */
456 void updateStatusBarGeometry();
457
458 /**
459 * @return Preferred geometry of the small statusbar.
460 */
461 QRect preferredSmallStatusBarGeometry();
462
463 protected:
464 bool eventFilter(QObject *object, QEvent *event) override;
465
466 private:
467 QGridLayout *m_topLayout;
468
469 /**
470 * The internal UrlNavigator which is never visible to the user.
471 * m_urlNavigator is used even when another UrlNavigator is controlling
472 * the view to keep track of this object's history.
473 */
474 std::unique_ptr<DolphinUrlNavigator> m_urlNavigator;
475
476 /**
477 * The UrlNavigator that is currently connected to the view.
478 * This is a nullptr if no UrlNavigator is connected.
479 * Otherwise it's one of the UrlNavigators visible in the toolbar.
480 */
481 QPointer<DolphinUrlNavigator> m_urlNavigatorConnected;
482
483 Search::Bar *m_searchBar;
484 bool m_searchModeEnabled;
485
486 /// A bar shown at the top of the view to signify that the view is currently viewed and acted on with elevated privileges.
487 Admin::Bar *m_adminBar;
488 /// An action to switch to the admin protocol. This variable will always be nullptr unless kio-admin was installed. @see Admin::WorkerIntegration.
489 QAction *m_authorizeToEnterFolderAction;
490
491 KMessageWidget *m_messageWidget;
492
493 /// A bar shown at the top of the view to signify that selection mode is currently active.
494 SelectionMode::TopBar *m_selectionModeTopBar;
495
496 DolphinView *m_view;
497
498 FilterBar *m_filterBar;
499
500 /// A bar shown at the bottom of the view whose contents depend on what the user is currently doing.
501 SelectionMode::BottomBar *m_selectionModeBottomBar;
502
503 DolphinStatusBar *m_statusBar;
504 QTimer *m_statusBarTimer; // Triggers a delayed update
505 QElapsedTimer m_statusBarTimestamp; // Time in ms since last update
506 bool m_grabFocusOnUrlChange;
507 /**
508 * The visual state to be applied to the next UrlNavigator that gets
509 * connected to this ViewContainer.
510 */
511 std::unique_ptr<DolphinUrlNavigator::VisualState> m_urlNavigatorVisualState;
512 };
513
514 #endif // DOLPHINVIEWCONTAINER_H