]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.h
Add clang-format and format code as in Frameworks
[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 <KUrlNavigator>
18
19 #include <QElapsedTimer>
20 #include <QPushButton>
21 #include <QWidget>
22
23 #if HAVE_KACTIVITIES
24 namespace KActivities
25 {
26 class ResourceInstance;
27 }
28 #endif
29
30 class FilterBar;
31 class KMessageWidget;
32 class QAction;
33 class QGridLayout;
34 class QUrl;
35 class DolphinSearchBox;
36 class DolphinStatusBar;
37 namespace SelectionMode
38 {
39 class TopBar;
40 }
41
42 /**
43 * @short Represents a view for the directory content
44 * including the navigation bar, filter bar and status bar.
45 *
46 * View modes for icons, compact and details are supported. Currently
47 * Dolphin allows to have up to two views inside the main window.
48 *
49 * @see DolphinView
50 * @see FilterBar
51 * @see KUrlNavigator
52 * @see DolphinStatusBar
53 */
54 class DolphinViewContainer : public QWidget
55 {
56 Q_OBJECT
57
58 public:
59 enum MessageType { Information, Warning, Error };
60
61 DolphinViewContainer(const QUrl &url, QWidget *parent);
62 ~DolphinViewContainer() override;
63
64 /**
65 * Returns the current active URL, where all actions are applied.
66 * The URL navigator is synchronized with this URL.
67 */
68 QUrl url() const;
69
70 /**
71 * If \a active is true, the view container will marked as active. The active
72 * view container is defined as view where all actions are applied to.
73 */
74 void setActive(bool active);
75 bool isActive() const;
76
77 /**
78 * If \a grab is set to true, the container automatically grabs the focus
79 * as soon as the URL has been changed. Per default the grabbing
80 * of the focus is enabled.
81 */
82 void setAutoGrabFocus(bool grab);
83 bool autoGrabFocus() const;
84
85 QString currentSearchText() const;
86
87 const DolphinStatusBar *statusBar() const;
88 DolphinStatusBar *statusBar();
89
90 /**
91 * @return An UrlNavigator that is controlling this view
92 * or nullptr if there is none.
93 * @see connectUrlNavigator()
94 * @see disconnectUrlNavigator()
95 *
96 * Use urlNavigatorInternalWithHistory() if you want to access the history.
97 * @see urlNavigatorInternalWithHistory()
98 */
99 const DolphinUrlNavigator *urlNavigator() const;
100 /**
101 * @return An UrlNavigator that is controlling this view
102 * or nullptr if there is none.
103 * @see connectUrlNavigator()
104 * @see disconnectUrlNavigator()
105 *
106 * Use urlNavigatorInternalWithHistory() if you want to access the history.
107 * @see urlNavigatorInternalWithHistory()
108 */
109 DolphinUrlNavigator *urlNavigator();
110
111 /**
112 * @return An UrlNavigator that contains this view's history.
113 * Use urlNavigator() instead when not accessing the history.
114 */
115 const DolphinUrlNavigator *urlNavigatorInternalWithHistory() const;
116 /**
117 * @return An UrlNavigator that contains this view's history.
118 * Use urlNavigator() instead when not accessing the history.
119 */
120 DolphinUrlNavigator *urlNavigatorInternalWithHistory();
121
122 const DolphinView *view() const;
123 DolphinView *view();
124
125 /**
126 * @param urlNavigator The UrlNavigator that is supposed to control
127 * this view.
128 */
129 void connectUrlNavigator(DolphinUrlNavigator *urlNavigator);
130
131 /**
132 * Disconnects the navigator that is currently controlling the view.
133 * This method completely reverses connectUrlNavigator().
134 */
135 void disconnectUrlNavigator();
136
137 /**
138 * Sets a selection mode that is useful for quick and easy selecting or deselecting of files.
139 * This method is the central authority about enabling or disabling selection mode:
140 * All other classes that want to enable or disable selection mode should trigger a call of this method.
141 *
142 * This method sets the selection mode for the view of this viewContainer and sets the visibility of the
143 * selection mode top and bottom bar which also belong to this viewContainer.
144 *
145 * @param enabled Whether to enable or disable selection mode.
146 * @param actionCollection The collection of actions from which the actions on the bottom bar are retrieved.
147 * @param bottomBarContents The contents the bar is supposed to show after this call.
148 */
149 void setSelectionModeEnabled(bool enabled,
150 KActionCollection *actionCollection = nullptr,
151 SelectionMode::BottomBar::Contents bottomBarContents = SelectionMode::BottomBar::Contents::GeneralContents);
152 /** @see setSelectionModeEnabled() */
153 bool isSelectionModeEnabled() const;
154
155 /**
156 * Shows the message \msg with the given type non-modal above
157 * the view-content.
158 */
159 void showMessage(const QString &msg, MessageType type);
160
161 /**
162 * Refreshes the view container to get synchronized with the (updated) Dolphin settings.
163 */
164 void readSettings();
165
166 /** Returns true, if the filter bar is visible. */
167 bool isFilterBarVisible() const;
168
169 /** Returns true if the search mode is enabled. */
170 bool isSearchModeEnabled() const;
171
172 /**
173 * @return Text that should be used for the current URL when creating
174 * a new place.
175 */
176 QString placesText() const;
177
178 /**
179 * Reload the view of this container. This will also hide messages in a messagewidget.
180 */
181 void reload();
182
183 /**
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().
187 */
188 QString captionWindowTitle() const;
189
190 /**
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
193 * factors.
194 */
195 QString caption() const;
196
197 /**
198 * Disable/enable the behavior of "select child when moving to parent folder"
199 * offered by KUrlNavigator.
200 *
201 * See KUrlNavigator::urlSelectionRequested
202 */
203 void disableUrlNavigatorSelectionRequests();
204 void enableUrlNavigatorSelectionRequests();
205 void clearFilterBar();
206
207 public Q_SLOTS:
208 /**
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()
212 * are emitted.
213 * @see DolphinViewContainer::urlNavigator()
214 */
215 void setUrl(const QUrl &url);
216
217 /**
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.
221 */
222 void setFilterBarVisible(bool visible);
223
224 /**
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.
227 */
228 void setSearchModeEnabled(bool enabled);
229
230 /** Used to notify the m_selectionModeBottomBar that there is no other ViewContainer in the tab. */
231 void slotSplitTabDisabled();
232
233 Q_SIGNALS:
234 /**
235 * Is emitted whenever the filter bar has changed its visibility state.
236 */
237 void showFilterBarChanged(bool shown);
238 /**
239 * Is emitted whenever the search mode has changed its state.
240 */
241 void searchModeEnabledChanged(bool enabled);
242
243 void selectionModeChanged(bool enabled);
244
245 /**
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
248 * state.
249 */
250 void writeStateChanged(bool isFolderWritable);
251
252 /**
253 * Is emitted when the Caption has been changed.
254 * @see DolphinViewContainer::caption()
255 */
256 void captionChanged();
257
258 /**
259 * Is emitted if a new tab should be opened in the background for the URL \a url.
260 */
261 void tabRequested(const QUrl &url);
262
263 /**
264 * Is emitted if a new tab should be opened for the URL \a url and set as active.
265 */
266 void activeTabRequested(const QUrl &url);
267
268 private Q_SLOTS:
269 /**
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.
278 */
279 void delayedStatusBarUpdate();
280
281 /**
282 * Is invoked by DolphinViewContainer::delayedStatusBarUpdate() and
283 * updates the status bar synchronously.
284 */
285 void updateStatusBar();
286
287 void updateDirectoryLoadingProgress(int percent);
288
289 void updateDirectorySortingProgress(int percent);
290
291 /**
292 * Updates the statusbar to show an undetermined progress with the correct
293 * context information whether a searching or a directory loading is done.
294 */
295 void slotDirectoryLoadingStarted();
296
297 /**
298 * Assures that the viewport position is restored and updates the
299 * statusbar to reflect the current content.
300 */
301 void slotDirectoryLoadingCompleted();
302
303 /**
304 * Updates the statusbar to show, that the directory loading has
305 * been canceled.
306 */
307 void slotDirectoryLoadingCanceled();
308
309 /**
310 * Is called if the URL set by DolphinView::setUrl() represents
311 * a file and not a directory. Takes care to activate the file.
312 */
313 void slotUrlIsFileError(const QUrl &url);
314
315 /**
316 * Handles clicking on an item. If the item is a directory, the
317 * directory is opened in the view. If the item is a file, the file
318 * gets started by the corresponding application.
319 */
320 void slotItemActivated(const KFileItem &item);
321
322 /**
323 * Handles activation of multiple files. The files get started by
324 * the corresponding applications.
325 */
326 void slotItemsActivated(const KFileItemList &items);
327
328 /**
329 * Shows the information for the item \a item inside the statusbar. If the
330 * item is null, the default statusbar information is shown.
331 */
332 void showItemInfo(const KFileItem &item);
333
334 void closeFilterBar();
335
336 /**
337 * Filters the currently shown items by \a nameFilter. All items
338 * which contain the given filter string will be shown.
339 */
340 void setNameFilter(const QString &nameFilter);
341
342 /**
343 * Marks the view container as active
344 * (see DolphinViewContainer::setActive()).
345 */
346 void activate();
347
348 /**
349 * Is invoked if the signal urlAboutToBeChanged() from the URL navigator
350 * is emitted. Tries to save the view-state.
351 */
352 void slotUrlNavigatorLocationAboutToBeChanged(const QUrl &url);
353
354 /**
355 * Restores the current view to show \a url and assures
356 * that the root URL of the view is respected.
357 */
358 void slotUrlNavigatorLocationChanged(const QUrl &url);
359
360 /**
361 * @see KUrlNavigator::urlSelectionRequested
362 */
363 void slotUrlSelectionRequested(const QUrl &url);
364
365 /**
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.
369 */
370 void redirect(const QUrl &oldUrl, const QUrl &newUrl);
371
372 /** Requests the focus for the view \a m_view. */
373 void requestFocus();
374
375 /**
376 * Gets the search URL from the searchbox and starts searching.
377 */
378 void startSearching();
379 void closeSearchBox();
380
381 /**
382 * Stops the loading of a directory. Is connected with the "stopPressed" signal
383 * from the statusbar.
384 */
385 void stopDirectoryLoading();
386
387 void slotStatusBarZoomLevelChanged(int zoomLevel);
388
389 /**
390 * Slot that calls showMessage(msg, Error).
391 */
392 void showErrorMessage(const QString &msg);
393
394 /**
395 * Is invoked when a KFilePlacesModel has been changed
396 * @see DolphinPlacesModelSingleton::instance().placesModel()
397 */
398 void slotPlacesModelChanged();
399
400 void slotHiddenFilesShownChanged(bool showHiddenFiles);
401 void slotSortHiddenLastChanged(bool hiddenLast);
402 void slotCurrentDirectoryRemoved();
403
404 void slotOpenUrlFinished(KJob *job);
405
406 private:
407 /**
408 * @return True if the URL protocol is a search URL (e. g. baloosearch:// or filenamesearch://).
409 */
410 bool isSearchUrl(const QUrl &url) const;
411
412 /**
413 * Saves the state of the current view: contents position,
414 * root URL, ...
415 */
416 void saveViewState();
417
418 /**
419 * Restores the state of the current view iff the URL navigator contains a
420 * non-empty location state.
421 */
422 void tryRestoreViewState();
423
424 /**
425 * @return Path of nearest existing ancestor directory.
426 */
427 QString getNearestExistingAncestorOfPath(const QString &path) const;
428
429 private:
430 QGridLayout *m_topLayout;
431
432 /**
433 * The internal UrlNavigator which is never visible to the user.
434 * m_urlNavigator is used even when another UrlNavigator is controlling
435 * the view to keep track of this object's history.
436 */
437 std::unique_ptr<DolphinUrlNavigator> m_urlNavigator;
438
439 /**
440 * The UrlNavigator that is currently connected to the view.
441 * This is a nullptr if no UrlNavigator is connected.
442 * Otherwise it's one of the UrlNavigators visible in the toolbar.
443 */
444 QPointer<DolphinUrlNavigator> m_urlNavigatorConnected;
445
446 DolphinSearchBox *m_searchBox;
447 bool m_searchModeEnabled;
448
449 KMessageWidget *m_messageWidget;
450
451 /// A bar shown at the top of the view to signify that selection mode is currently active.
452 SelectionMode::TopBar *m_selectionModeTopBar;
453
454 DolphinView *m_view;
455
456 FilterBar *m_filterBar;
457
458 /// A bar shown at the bottom of the view whose contents depend on what the user is currently doing.
459 SelectionMode::BottomBar *m_selectionModeBottomBar;
460
461 DolphinStatusBar *m_statusBar;
462 QTimer *m_statusBarTimer; // Triggers a delayed update
463 QElapsedTimer m_statusBarTimestamp; // Time in ms since last update
464 bool m_autoGrabFocus;
465 /**
466 * The visual state to be applied to the next UrlNavigator that gets
467 * connected to this ViewContainer.
468 */
469 std::unique_ptr<DolphinUrlNavigator::VisualState> m_urlNavigatorVisualState;
470
471 #if HAVE_KACTIVITIES
472 private:
473 KActivities::ResourceInstance *m_activityResourceInstance;
474 #endif
475 };
476
477 #endif // DOLPHINVIEWCONTAINER_H