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