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