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