]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.h
Rewrite search integration
[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 * Refreshes the view container to get synchronized with the (updated) Dolphin settings.
189 */
190 void readSettings();
191
192 /** @returns true, if the filter bar is visible.
193 * false, if it is hidden or currently animating towards a hidden state. */
194 bool isFilterBarVisible() const;
195
196 /**
197 * @return Text that should be used for the current URL when creating
198 * a new place.
199 */
200 QString placesText() const;
201
202 /**
203 * Reload the view of this container. This will also hide messages in a messagewidget.
204 */
205 void reload();
206
207 /**
208 * @return Returns a Caption suitable for display in the window title.
209 * It is calculated depending on GeneralSettings::showFullPathInTitlebar().
210 * If it's false, it calls caption().
211 */
212 QString captionWindowTitle() const;
213
214 /**
215 * @return Returns a Caption suitable for display to the user. It is
216 * calculated depending on settings, if a search is active and other
217 * factors.
218 */
219 QString caption() const;
220
221 /**
222 * Disable/enable the behavior of "select child when moving to parent folder"
223 * offered by KUrlNavigator.
224 *
225 * See KUrlNavigator::urlSelectionRequested
226 */
227 void disableUrlNavigatorSelectionRequests();
228 void enableUrlNavigatorSelectionRequests();
229 void clearFilterBar();
230
231 public Q_SLOTS:
232 /**
233 * Sets the current active URL, where all actions are applied. The
234 * URL navigator is synchronized with this URL. The signals
235 * KUrlNavigator::urlChanged() and KUrlNavigator::historyChanged()
236 * are emitted.
237 * @see DolphinViewContainer::urlNavigator()
238 */
239 void setUrl(const QUrl &url);
240
241 /**
242 * Popups the filter bar above the status bar if \a visible is true.
243 * It \a visible is true, it is assured that the filter bar gains
244 * the keyboard focus.
245 */
246 void setFilterBarVisible(bool visible);
247
248 /** Used to notify the m_selectionModeBottomBar that there is no other ViewContainer in the tab. */
249 void slotSplitTabDisabled();
250
251 Q_SIGNALS:
252 /**
253 * Is emitted whenever the filter bar has changed its visibility state.
254 */
255 void showFilterBarChanged(bool shown);
256 /**
257 * 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
258 * this signal is emitted because typically the showing and hiding is animated.
259 * @param visible The visibility state the search bar is going to end up at.
260 * @see Search::Bar.
261 * @see setSearchBarVisible().
262 * @see isSearchBarVisible().
263 */
264 void searchBarVisibilityChanged(bool visible);
265
266 void selectionModeChanged(bool enabled);
267
268 /**
269 * Is emitted when the write state of the folder has been changed. The application
270 * should disable all actions like "Create New..." that depend on the write
271 * state.
272 */
273 void writeStateChanged(bool isFolderWritable);
274
275 /**
276 * Is emitted when the Caption has been changed.
277 * @see DolphinViewContainer::caption()
278 */
279 void captionChanged();
280
281 /**
282 * Is emitted if a new tab should be opened in the background for the URL \a url.
283 */
284 void tabRequested(const QUrl &url);
285
286 /**
287 * Is emitted if a new tab should be opened for the URL \a url and set as active.
288 */
289 void activeTabRequested(const QUrl &url);
290
291 private Q_SLOTS:
292 /**
293 * Updates the number of items (= number of files + number of
294 * directories) in the statusbar. If files are selected, the number
295 * of selected files and the sum of the filesize is shown. The update
296 * is done asynchronously, as getting the sum of the
297 * filesizes can be an expensive operation.
298 * Unless a previous OperationCompletedMessage was set very shortly before
299 * calling this method, it will be overwritten (see DolphinStatusBar::setMessage).
300 * Previous ErrorMessages however are always preserved.
301 */
302 void delayedStatusBarUpdate();
303
304 /**
305 * Is invoked by DolphinViewContainer::delayedStatusBarUpdate() and
306 * updates the status bar synchronously.
307 */
308 void updateStatusBar();
309
310 /**
311 * Updates the statusbar to show an undetermined progress with the correct
312 * context information whether a searching or a directory loading is done.
313 */
314 void slotDirectoryLoadingStarted();
315
316 /**
317 * Assures that the viewport position is restored and updates the
318 * statusbar to reflect the current content.
319 */
320 void slotDirectoryLoadingCompleted();
321
322 /**
323 * Updates the statusbar to show, that the directory loading has
324 * been canceled.
325 */
326 void slotDirectoryLoadingCanceled();
327
328 /**
329 * Is called if the URL set by DolphinView::setUrl() represents
330 * a file and not a directory. Takes care to activate the file.
331 */
332 void slotUrlIsFileError(const QUrl &url);
333
334 /**
335 * Handles clicking on an item. If the item is a directory, the
336 * directory is opened in the view. If the item is a file, the file
337 * gets started by the corresponding application.
338 */
339 void slotItemActivated(const KFileItem &item);
340
341 /**
342 * Handles activation of multiple files. The files get started by
343 * the corresponding applications.
344 */
345 void slotItemsActivated(const KFileItemList &items);
346
347 /**
348 * Handles middle click of file. It opens the file passed using the second application associated with the file's mimetype.
349 */
350 void slotfileMiddleClickActivated(const KFileItem &item);
351
352 /**
353 * Shows the information for the item \a item inside the statusbar. If the
354 * item is null, the default statusbar information is shown.
355 */
356 void showItemInfo(const KFileItem &item);
357
358 void closeFilterBar();
359
360 /**
361 * Filters the currently shown items by \a nameFilter. All items
362 * which contain the given filter string will be shown.
363 */
364 void setNameFilter(const QString &nameFilter);
365
366 /**
367 * Marks the view container as active
368 * (see DolphinViewContainer::setActive()).
369 */
370 void activate();
371
372 /**
373 * Is invoked if the signal urlAboutToBeChanged() from the URL navigator
374 * is emitted. Tries to save the view-state.
375 */
376 void slotUrlNavigatorLocationAboutToBeChanged(const QUrl &url);
377
378 /**
379 * Restores the current view to show \a url and assures
380 * that the root URL of the view is respected.
381 */
382 void slotUrlNavigatorLocationChanged(const QUrl &url);
383
384 /**
385 * @see KUrlNavigator::urlSelectionRequested
386 */
387 void slotUrlSelectionRequested(const QUrl &url);
388
389 /**
390 * Is invoked when a redirection is done and changes the
391 * URL of the URL navigator to \a newUrl without triggering
392 * a reloading of the directory.
393 */
394 void redirect(const QUrl &oldUrl, const QUrl &newUrl);
395
396 /** Requests the focus for the view \a m_view. */
397 void requestFocus();
398
399 /**
400 * Stops the loading of a directory. Is connected with the "stopPressed" signal
401 * from the statusbar.
402 */
403 void stopDirectoryLoading();
404
405 void slotStatusBarZoomLevelChanged(int zoomLevel);
406
407 /**
408 * Creates and shows an error message based on \p message and \p kioErrorCode.
409 */
410 void slotErrorMessageFromView(const QString &message, const int kioErrorCode);
411
412 /**
413 * Slot that calls showMessage(message, KMessageWidget::Error).
414 */
415 void showErrorMessage(const QString &message);
416
417 /**
418 * Is invoked when a KFilePlacesModel has been changed
419 * @see DolphinPlacesModelSingleton::instance().placesModel()
420 */
421 void slotPlacesModelChanged();
422
423 void slotHiddenFilesShownChanged(bool showHiddenFiles);
424 void slotSortHiddenLastChanged(bool hiddenLast);
425 void slotCurrentDirectoryRemoved();
426
427 void slotOpenUrlFinished(KJob *job);
428
429 private:
430 /**
431 * Saves the state of the current view: contents position,
432 * root URL, ...
433 */
434 void saveViewState();
435
436 /**
437 * Restores the state of the current view iff the URL navigator contains a
438 * non-empty location state.
439 */
440 void tryRestoreViewState();
441
442 /**
443 * @return Path of nearest existing ancestor directory.
444 */
445 QString getNearestExistingAncestorOfPath(const QString &path) const;
446
447 /**
448 * Update the geometry of statusbar depending on what mode it is using.
449 */
450 void updateStatusBarGeometry();
451
452 /**
453 * @return Preferred geometry of the small statusbar.
454 */
455 QRect preferredSmallStatusBarGeometry();
456
457 protected:
458 bool eventFilter(QObject *object, QEvent *event) override;
459
460 private:
461 QGridLayout *m_topLayout;
462
463 /**
464 * The internal UrlNavigator which is never visible to the user.
465 * m_urlNavigator is used even when another UrlNavigator is controlling
466 * the view to keep track of this object's history.
467 */
468 std::unique_ptr<DolphinUrlNavigator> m_urlNavigator;
469
470 /**
471 * The UrlNavigator that is currently connected to the view.
472 * This is a nullptr if no UrlNavigator is connected.
473 * Otherwise it's one of the UrlNavigators visible in the toolbar.
474 */
475 QPointer<DolphinUrlNavigator> m_urlNavigatorConnected;
476
477 Search::Bar *m_searchBar;
478 bool m_searchModeEnabled;
479
480 /// A bar shown at the top of the view to signify that the view is currently viewed and acted on with elevated privileges.
481 Admin::Bar *m_adminBar;
482 /// An action to switch to the admin protocol. This variable will always be nullptr unless kio-admin was installed. @see Admin::WorkerIntegration.
483 QAction *m_authorizeToEnterFolderAction;
484
485 KMessageWidget *m_messageWidget;
486
487 /// A bar shown at the top of the view to signify that selection mode is currently active.
488 SelectionMode::TopBar *m_selectionModeTopBar;
489
490 DolphinView *m_view;
491
492 FilterBar *m_filterBar;
493
494 /// A bar shown at the bottom of the view whose contents depend on what the user is currently doing.
495 SelectionMode::BottomBar *m_selectionModeBottomBar;
496
497 DolphinStatusBar *m_statusBar;
498 QTimer *m_statusBarTimer; // Triggers a delayed update
499 QElapsedTimer m_statusBarTimestamp; // Time in ms since last update
500 bool m_grabFocusOnUrlChange;
501 /**
502 * The visual state to be applied to the next UrlNavigator that gets
503 * connected to this ViewContainer.
504 */
505 std::unique_ptr<DolphinUrlNavigator::VisualState> m_urlNavigatorVisualState;
506 };
507
508 #endif // DOLPHINVIEWCONTAINER_H