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