]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.h
Animate most of the bars
[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 class FilterBar;
24 class KMessageWidget;
25 class QAction;
26 class QGridLayout;
27 class QUrl;
28 class DolphinSearchBox;
29 class DolphinStatusBar;
30 namespace SelectionMode
31 {
32 class TopBar;
33 }
34
35 /**
36 * @short Represents a view for the directory content
37 * including the navigation bar, filter bar and status bar.
38 *
39 * View modes for icons, compact and details are supported. Currently
40 * Dolphin allows to have up to two views inside the main window.
41 *
42 * @see DolphinView
43 * @see FilterBar
44 * @see KUrlNavigator
45 * @see DolphinStatusBar
46 */
47 class DolphinViewContainer : public QWidget
48 {
49 Q_OBJECT
50
51 public:
52 enum MessageType { Information, Warning, Error };
53
54 DolphinViewContainer(const QUrl &url, QWidget *parent);
55 ~DolphinViewContainer() override;
56
57 /**
58 * Returns the current active URL, where all actions are applied.
59 * The URL navigator is synchronized with this URL.
60 */
61 QUrl url() const;
62 KFileItem rootItem() const;
63
64 /**
65 * If \a active is true, the view container will marked as active. The active
66 * view container is defined as view where all actions are applied to.
67 */
68 void setActive(bool active);
69 bool isActive() const;
70
71 /**
72 * If \a grab is set to true, the container automatically grabs the focus
73 * as soon as the URL has been changed. Per default the grabbing
74 * of the focus is enabled.
75 */
76 void setAutoGrabFocus(bool grab);
77 bool autoGrabFocus() const;
78
79 QString currentSearchText() const;
80
81 const DolphinStatusBar *statusBar() const;
82 DolphinStatusBar *statusBar();
83
84 /**
85 * @return An UrlNavigator that is controlling this view
86 * or nullptr if there is none.
87 * @see connectUrlNavigator()
88 * @see disconnectUrlNavigator()
89 *
90 * Use urlNavigatorInternalWithHistory() if you want to access the history.
91 * @see urlNavigatorInternalWithHistory()
92 */
93 const DolphinUrlNavigator *urlNavigator() const;
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 DolphinUrlNavigator *urlNavigator();
104
105 /**
106 * @return An UrlNavigator that contains this view's history.
107 * Use urlNavigator() instead when not accessing the history.
108 */
109 const DolphinUrlNavigator *urlNavigatorInternalWithHistory() const;
110 /**
111 * @return An UrlNavigator that contains this view's history.
112 * Use urlNavigator() instead when not accessing the history.
113 */
114 DolphinUrlNavigator *urlNavigatorInternalWithHistory();
115
116 const DolphinView *view() const;
117 DolphinView *view();
118
119 /**
120 * @param urlNavigator The UrlNavigator that is supposed to control
121 * this view.
122 */
123 void connectUrlNavigator(DolphinUrlNavigator *urlNavigator);
124
125 /**
126 * Disconnects the navigator that is currently controlling the view.
127 * This method completely reverses connectUrlNavigator().
128 */
129 void disconnectUrlNavigator();
130
131 /**
132 * Sets a selection mode that is useful for quick and easy selecting or deselecting of files.
133 * This method is the central authority about enabling or disabling selection mode:
134 * All other classes that want to enable or disable selection mode should trigger a call of this method.
135 *
136 * This method sets the selection mode for the view of this viewContainer and sets the visibility of the
137 * selection mode top and bottom bar which also belong to this viewContainer.
138 *
139 * @param enabled Whether to enable or disable selection mode.
140 * @param actionCollection The collection of actions from which the actions on the bottom bar are retrieved.
141 * @param bottomBarContents The contents the bar is supposed to show after this call.
142 */
143 void setSelectionModeEnabled(bool enabled,
144 KActionCollection *actionCollection = nullptr,
145 SelectionMode::BottomBar::Contents bottomBarContents = SelectionMode::BottomBar::Contents::GeneralContents);
146 /** @see setSelectionModeEnabled() */
147 bool isSelectionModeEnabled() const;
148
149 /**
150 * Shows the message \msg with the given type non-modal above
151 * the view-content.
152 */
153 void showMessage(const QString &msg, MessageType type);
154
155 /**
156 * Refreshes the view container to get synchronized with the (updated) Dolphin settings.
157 */
158 void readSettings();
159
160 /** @returns true, if the filter bar is visible.
161 * false, if it is hidden or currently animating towards a hidden state. */
162 bool isFilterBarVisible() const;
163
164 /** Returns true if the search mode is enabled. */
165 bool isSearchModeEnabled() const;
166
167 /**
168 * @return Text that should be used for the current URL when creating
169 * a new place.
170 */
171 QString placesText() const;
172
173 /**
174 * Reload the view of this container. This will also hide messages in a messagewidget.
175 */
176 void reload();
177
178 /**
179 * @return Returns a Caption suitable for display in the window title.
180 * It is calculated depending on GeneralSettings::showFullPathInTitlebar().
181 * If it's false, it calls caption().
182 */
183 QString captionWindowTitle() const;
184
185 /**
186 * @return Returns a Caption suitable for display to the user. It is
187 * calculated depending on settings, if a search is active and other
188 * factors.
189 */
190 QString caption() const;
191
192 /**
193 * Disable/enable the behavior of "select child when moving to parent folder"
194 * offered by KUrlNavigator.
195 *
196 * See KUrlNavigator::urlSelectionRequested
197 */
198 void disableUrlNavigatorSelectionRequests();
199 void enableUrlNavigatorSelectionRequests();
200 void clearFilterBar();
201
202 public Q_SLOTS:
203 /**
204 * Sets the current active URL, where all actions are applied. The
205 * URL navigator is synchronized with this URL. The signals
206 * KUrlNavigator::urlChanged() and KUrlNavigator::historyChanged()
207 * are emitted.
208 * @see DolphinViewContainer::urlNavigator()
209 */
210 void setUrl(const QUrl &url);
211
212 /**
213 * Popups the filter bar above the status bar if \a visible is true.
214 * It \a visible is true, it is assured that the filter bar gains
215 * the keyboard focus.
216 */
217 void setFilterBarVisible(bool visible);
218
219 /**
220 * Enables the search mode, if \p enabled is true. In the search mode the URL navigator
221 * will be hidden and replaced by a line editor that allows to enter a search term.
222 */
223 void setSearchModeEnabled(bool enabled);
224
225 /** Used to notify the m_selectionModeBottomBar that there is no other ViewContainer in the tab. */
226 void slotSplitTabDisabled();
227
228 Q_SIGNALS:
229 /**
230 * Is emitted whenever the filter bar has changed its visibility state.
231 */
232 void showFilterBarChanged(bool shown);
233 /**
234 * Is emitted whenever the search mode has changed its state.
235 */
236 void searchModeEnabledChanged(bool enabled);
237
238 void selectionModeChanged(bool enabled);
239
240 /**
241 * Is emitted when the write state of the folder has been changed. The application
242 * should disable all actions like "Create New..." that depend on the write
243 * state.
244 */
245 void writeStateChanged(bool isFolderWritable);
246
247 /**
248 * Is emitted when the Caption has been changed.
249 * @see DolphinViewContainer::caption()
250 */
251 void captionChanged();
252
253 /**
254 * Is emitted if a new tab should be opened in the background for the URL \a url.
255 */
256 void tabRequested(const QUrl &url);
257
258 /**
259 * Is emitted if a new tab should be opened for the URL \a url and set as active.
260 */
261 void activeTabRequested(const QUrl &url);
262
263 private Q_SLOTS:
264 /**
265 * Updates the number of items (= number of files + number of
266 * directories) in the statusbar. If files are selected, the number
267 * of selected files and the sum of the filesize is shown. The update
268 * is done asynchronously, as getting the sum of the
269 * filesizes can be an expensive operation.
270 * Unless a previous OperationCompletedMessage was set very shortly before
271 * calling this method, it will be overwritten (see DolphinStatusBar::setMessage).
272 * Previous ErrorMessages however are always preserved.
273 */
274 void delayedStatusBarUpdate();
275
276 /**
277 * Is invoked by DolphinViewContainer::delayedStatusBarUpdate() and
278 * updates the status bar synchronously.
279 */
280 void updateStatusBar();
281
282 void updateDirectoryLoadingProgress(int percent);
283
284 void updateDirectorySortingProgress(int percent);
285
286 /**
287 * Updates the statusbar to show an undetermined progress with the correct
288 * context information whether a searching or a directory loading is done.
289 */
290 void slotDirectoryLoadingStarted();
291
292 /**
293 * Assures that the viewport position is restored and updates the
294 * statusbar to reflect the current content.
295 */
296 void slotDirectoryLoadingCompleted();
297
298 /**
299 * Updates the statusbar to show, that the directory loading has
300 * been canceled.
301 */
302 void slotDirectoryLoadingCanceled();
303
304 /**
305 * Is called if the URL set by DolphinView::setUrl() represents
306 * a file and not a directory. Takes care to activate the file.
307 */
308 void slotUrlIsFileError(const QUrl &url);
309
310 /**
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.
314 */
315 void slotItemActivated(const KFileItem &item);
316
317 /**
318 * Handles activation of multiple files. The files get started by
319 * the corresponding applications.
320 */
321 void slotItemsActivated(const KFileItemList &items);
322
323 /**
324 * Handles middle click of file. It opens the file passed using the second application associated with the file's mimetype.
325 */
326 void slotfileMiddleClickActivated(const KFileItem &item);
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 openSearchBox();
380 void closeSearchBox();
381
382 /**
383 * Stops the loading of a directory. Is connected with the "stopPressed" signal
384 * from the statusbar.
385 */
386 void stopDirectoryLoading();
387
388 void slotStatusBarZoomLevelChanged(int zoomLevel);
389
390 /**
391 * Slot that calls showMessage(msg, Error).
392 */
393 void showErrorMessage(const QString &msg);
394
395 /**
396 * Is invoked when a KFilePlacesModel has been changed
397 * @see DolphinPlacesModelSingleton::instance().placesModel()
398 */
399 void slotPlacesModelChanged();
400
401 void slotHiddenFilesShownChanged(bool showHiddenFiles);
402 void slotSortHiddenLastChanged(bool hiddenLast);
403 void slotCurrentDirectoryRemoved();
404
405 void slotOpenUrlFinished(KJob *job);
406
407 private:
408 /**
409 * @return True if the URL protocol is a search URL (e. g. baloosearch:// or filenamesearch://).
410 */
411 bool isSearchUrl(const QUrl &url) const;
412
413 /**
414 * Saves the state of the current view: contents position,
415 * root URL, ...
416 */
417 void saveViewState();
418
419 /**
420 * Restores the state of the current view iff the URL navigator contains a
421 * non-empty location state.
422 */
423 void tryRestoreViewState();
424
425 /**
426 * @return Path of nearest existing ancestor directory.
427 */
428 QString getNearestExistingAncestorOfPath(const QString &path) const;
429
430 private:
431 QGridLayout *m_topLayout;
432
433 /**
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.
437 */
438 std::unique_ptr<DolphinUrlNavigator> m_urlNavigator;
439
440 /**
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.
444 */
445 QPointer<DolphinUrlNavigator> m_urlNavigatorConnected;
446
447 DolphinSearchBox *m_searchBox;
448 bool m_searchModeEnabled;
449
450 KMessageWidget *m_messageWidget;
451
452 /// A bar shown at the top of the view to signify that selection mode is currently active.
453 SelectionMode::TopBar *m_selectionModeTopBar;
454
455 DolphinView *m_view;
456
457 FilterBar *m_filterBar;
458
459 /// A bar shown at the bottom of the view whose contents depend on what the user is currently doing.
460 SelectionMode::BottomBar *m_selectionModeBottomBar;
461
462 DolphinStatusBar *m_statusBar;
463 QTimer *m_statusBarTimer; // Triggers a delayed update
464 QElapsedTimer m_statusBarTimestamp; // Time in ms since last update
465 bool m_autoGrabFocus;
466 /**
467 * The visual state to be applied to the next UrlNavigator that gets
468 * connected to this ViewContainer.
469 */
470 std::unique_ptr<DolphinUrlNavigator::VisualState> m_urlNavigatorVisualState;
471 };
472
473 #endif // DOLPHINVIEWCONTAINER_H