]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.h
GIT_SILENT Sync po/docbooks with svn
[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 class DolphinSearchBox;
35 class DolphinStatusBar;
36 class KFileItemList;
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 DolphinViewContainer(const QUrl &url, QWidget *parent);
60 ~DolphinViewContainer() override;
61
62 /**
63 * Returns the current active URL, where all actions are applied.
64 * The URL navigator is synchronized with this URL.
65 */
66 QUrl url() const;
67 KFileItem rootItem() const;
68
69 /**
70 * If \a active is true, the view container will marked as active. The active
71 * view container is defined as view where all actions are applied to.
72 */
73 void setActive(bool active);
74 bool isActive() const;
75
76 /**
77 * If \a grab is set to true, the container automatically grabs the focus
78 * as soon as the URL has been changed. Per default the grabbing
79 * of the focus is enabled.
80 */
81 void setAutoGrabFocus(bool grab);
82 bool autoGrabFocus() const;
83
84 QString currentSearchText() const;
85
86 const DolphinStatusBar *statusBar() const;
87 DolphinStatusBar *statusBar();
88
89 /**
90 * @return An UrlNavigator that is controlling this view
91 * or nullptr if there is none.
92 * @see connectUrlNavigator()
93 * @see disconnectUrlNavigator()
94 *
95 * Use urlNavigatorInternalWithHistory() if you want to access the history.
96 * @see urlNavigatorInternalWithHistory()
97 */
98 const DolphinUrlNavigator *urlNavigator() const;
99 /**
100 * @return An UrlNavigator that is controlling this view
101 * or nullptr if there is none.
102 * @see connectUrlNavigator()
103 * @see disconnectUrlNavigator()
104 *
105 * Use urlNavigatorInternalWithHistory() if you want to access the history.
106 * @see urlNavigatorInternalWithHistory()
107 */
108 DolphinUrlNavigator *urlNavigator();
109
110 /**
111 * @return An UrlNavigator that contains this view's history.
112 * Use urlNavigator() instead when not accessing the history.
113 */
114 const DolphinUrlNavigator *urlNavigatorInternalWithHistory() const;
115 /**
116 * @return An UrlNavigator that contains this view's history.
117 * Use urlNavigator() instead when not accessing the history.
118 */
119 DolphinUrlNavigator *urlNavigatorInternalWithHistory();
120
121 const DolphinView *view() const;
122 DolphinView *view();
123
124 /**
125 * @param urlNavigator The UrlNavigator that is supposed to control
126 * this view.
127 */
128 void connectUrlNavigator(DolphinUrlNavigator *urlNavigator);
129
130 /**
131 * Disconnects the navigator that is currently controlling the view.
132 * This method completely reverses connectUrlNavigator().
133 */
134 void disconnectUrlNavigator();
135
136 /**
137 * Sets a selection mode that is useful for quick and easy selecting or deselecting of files.
138 * This method is the central authority about enabling or disabling selection mode:
139 * All other classes that want to enable or disable selection mode should trigger a call of this method.
140 *
141 * This method sets the selection mode for the view of this viewContainer and sets the visibility of the
142 * selection mode top and bottom bar which also belong to this viewContainer.
143 *
144 * @param enabled Whether to enable or disable selection mode.
145 * @param actionCollection The collection of actions from which the actions on the bottom bar are retrieved.
146 * @param bottomBarContents The contents the bar is supposed to show after this call.
147 */
148 void setSelectionModeEnabled(bool enabled,
149 KActionCollection *actionCollection = nullptr,
150 SelectionMode::BottomBar::Contents bottomBarContents = SelectionMode::BottomBar::Contents::GeneralContents);
151 /** @see setSelectionModeEnabled() */
152 bool isSelectionModeEnabled() const;
153
154 /**
155 * Shows the message \message with the given type \messageType non-modal above the view-content.
156 * \buttonActions defines actions which the user can trigger as a response to this message. They are presented as buttons below the \message.
157 */
158 void showMessage(const QString &message, KMessageWidget::MessageType messageType, std::initializer_list<QAction *> buttonActions = {});
159
160 /**
161 * Refreshes the view container to get synchronized with the (updated) Dolphin settings.
162 */
163 void readSettings();
164
165 /** @returns true, if the filter bar is visible.
166 * false, if it is hidden or currently animating towards a hidden state. */
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 /**
288 * Updates the statusbar to show an undetermined progress with the correct
289 * context information whether a searching or a directory loading is done.
290 */
291 void slotDirectoryLoadingStarted();
292
293 /**
294 * Assures that the viewport position is restored and updates the
295 * statusbar to reflect the current content.
296 */
297 void slotDirectoryLoadingCompleted();
298
299 /**
300 * Updates the statusbar to show, that the directory loading has
301 * been canceled.
302 */
303 void slotDirectoryLoadingCanceled();
304
305 /**
306 * Is called if the URL set by DolphinView::setUrl() represents
307 * a file and not a directory. Takes care to activate the file.
308 */
309 void slotUrlIsFileError(const QUrl &url);
310
311 /**
312 * Handles clicking on an item. If the item is a directory, the
313 * directory is opened in the view. If the item is a file, the file
314 * gets started by the corresponding application.
315 */
316 void slotItemActivated(const KFileItem &item);
317
318 /**
319 * Handles activation of multiple files. The files get started by
320 * the corresponding applications.
321 */
322 void slotItemsActivated(const KFileItemList &items);
323
324 /**
325 * Handles middle click of file. It opens the file passed using the second application associated with the file's mimetype.
326 */
327 void slotfileMiddleClickActivated(const KFileItem &item);
328
329 /**
330 * Shows the information for the item \a item inside the statusbar. If the
331 * item is null, the default statusbar information is shown.
332 */
333 void showItemInfo(const KFileItem &item);
334
335 void closeFilterBar();
336
337 /**
338 * Filters the currently shown items by \a nameFilter. All items
339 * which contain the given filter string will be shown.
340 */
341 void setNameFilter(const QString &nameFilter);
342
343 /**
344 * Marks the view container as active
345 * (see DolphinViewContainer::setActive()).
346 */
347 void activate();
348
349 /**
350 * Is invoked if the signal urlAboutToBeChanged() from the URL navigator
351 * is emitted. Tries to save the view-state.
352 */
353 void slotUrlNavigatorLocationAboutToBeChanged(const QUrl &url);
354
355 /**
356 * Restores the current view to show \a url and assures
357 * that the root URL of the view is respected.
358 */
359 void slotUrlNavigatorLocationChanged(const QUrl &url);
360
361 /**
362 * @see KUrlNavigator::urlSelectionRequested
363 */
364 void slotUrlSelectionRequested(const QUrl &url);
365
366 /**
367 * Is invoked when a redirection is done and changes the
368 * URL of the URL navigator to \a newUrl without triggering
369 * a reloading of the directory.
370 */
371 void redirect(const QUrl &oldUrl, const QUrl &newUrl);
372
373 /** Requests the focus for the view \a m_view. */
374 void requestFocus();
375
376 /**
377 * Gets the search URL from the searchbox and starts searching.
378 */
379 void startSearching();
380 void openSearchBox();
381 void closeSearchBox();
382
383 /**
384 * Stops the loading of a directory. Is connected with the "stopPressed" signal
385 * from the statusbar.
386 */
387 void stopDirectoryLoading();
388
389 void slotStatusBarZoomLevelChanged(int zoomLevel);
390
391 /**
392 * Creates and shows an error message based on \p message and \p kioErrorCode.
393 */
394 void slotErrorMessageFromView(const QString &message, const int kioErrorCode);
395
396 /**
397 * Slot that calls showMessage(message, KMessageWidget::Error).
398 */
399 void showErrorMessage(const QString &message);
400
401 /**
402 * Is invoked when a KFilePlacesModel has been changed
403 * @see DolphinPlacesModelSingleton::instance().placesModel()
404 */
405 void slotPlacesModelChanged();
406
407 void slotHiddenFilesShownChanged(bool showHiddenFiles);
408 void slotSortHiddenLastChanged(bool hiddenLast);
409 void slotCurrentDirectoryRemoved();
410
411 void slotOpenUrlFinished(KJob *job);
412
413 private:
414 /**
415 * @return True if the URL protocol is a search URL (e. g. baloosearch:// or filenamesearch://).
416 */
417 bool isSearchUrl(const QUrl &url) const;
418
419 /**
420 * Saves the state of the current view: contents position,
421 * root URL, ...
422 */
423 void saveViewState();
424
425 /**
426 * Restores the state of the current view iff the URL navigator contains a
427 * non-empty location state.
428 */
429 void tryRestoreViewState();
430
431 /**
432 * @return Path of nearest existing ancestor directory.
433 */
434 QString getNearestExistingAncestorOfPath(const QString &path) const;
435
436 /**
437 * Update the geometry of statusbar depending on what mode it is using.
438 */
439 void updateStatusBarGeometry();
440
441 /**
442 * @return Preferred geometry of the small statusbar.
443 */
444 QRect preferredSmallStatusBarGeometry();
445
446 protected:
447 bool eventFilter(QObject *object, QEvent *event) override;
448
449 private:
450 QGridLayout *m_topLayout;
451
452 /**
453 * The internal UrlNavigator which is never visible to the user.
454 * m_urlNavigator is used even when another UrlNavigator is controlling
455 * the view to keep track of this object's history.
456 */
457 std::unique_ptr<DolphinUrlNavigator> m_urlNavigator;
458
459 /**
460 * The UrlNavigator that is currently connected to the view.
461 * This is a nullptr if no UrlNavigator is connected.
462 * Otherwise it's one of the UrlNavigators visible in the toolbar.
463 */
464 QPointer<DolphinUrlNavigator> m_urlNavigatorConnected;
465
466 DolphinSearchBox *m_searchBox;
467 bool m_searchModeEnabled;
468
469 /// A bar shown at the top of the view to signify that the view is currently viewed and acted on with elevated privileges.
470 Admin::Bar *m_adminBar;
471 /// An action to switch to the admin protocol. This variable will always be nullptr unless kio-admin was installed. @see Admin::WorkerIntegration.
472 QAction *m_authorizeToEnterFolderAction;
473
474 KMessageWidget *m_messageWidget;
475
476 /// A bar shown at the top of the view to signify that selection mode is currently active.
477 SelectionMode::TopBar *m_selectionModeTopBar;
478
479 DolphinView *m_view;
480
481 FilterBar *m_filterBar;
482
483 /// A bar shown at the bottom of the view whose contents depend on what the user is currently doing.
484 SelectionMode::BottomBar *m_selectionModeBottomBar;
485
486 DolphinStatusBar *m_statusBar;
487 QTimer *m_statusBarTimer; // Triggers a delayed update
488 QElapsedTimer m_statusBarTimestamp; // Time in ms since last update
489 bool m_autoGrabFocus;
490 /**
491 * The visual state to be applied to the next UrlNavigator that gets
492 * connected to this ViewContainer.
493 */
494 std::unique_ptr<DolphinUrlNavigator::VisualState> m_urlNavigatorVisualState;
495 };
496
497 #endif // DOLPHINVIEWCONTAINER_H