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