]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.h
Add the UrlNavigator to the toolbar automatically if needed
[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 KUrlNavigator;
32 class DolphinSearchBox;
33 class DolphinStatusBar;
34
35 /**
36 * @short Represents a view for the directory content
37 * including the navigation bar, filter bar and status bar.
38 *
39 * View modes for icons, compact and details are supported. Currently
40 * Dolphin allows to have up to two views inside the main window.
41 *
42 * @see DolphinView
43 * @see FilterBar
44 * @see KUrlNavigator
45 * @see DolphinStatusBar
46 */
47 class DolphinViewContainer : public QWidget
48 {
49 Q_OBJECT
50
51 public:
52 enum MessageType
53 {
54 Information,
55 Warning,
56 Error
57 };
58
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
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 inline void connectToInternalUrlNavigator()
130 {
131 connectUrlNavigator(m_urlNavigator);
132 }
133
134 /**
135 * Disconnects the navigator that is currently controling the view.
136 * This method completely reverses connectUrlNavigator().
137 */
138 void disconnectUrlNavigator();
139
140 /**
141 * Shows the message \msg with the given type non-modal above
142 * the view-content.
143 * @return the KMessageWidget used to show the message
144 */
145 KMessageWidget *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 public slots:
185 /**
186 * Sets the current active URL, where all actions are applied. The
187 * URL navigator is synchronized with this URL. The signals
188 * KUrlNavigator::urlChanged() and KUrlNavigator::historyChanged()
189 * are emitted.
190 * @see DolphinViewContainer::urlNavigator()
191 */
192 void setUrl(const QUrl& url);
193
194 /**
195 * Popups the filter bar above the status bar if \a visible is true.
196 * It \a visible is true, it is assured that the filter bar gains
197 * the keyboard focus.
198 */
199 void setFilterBarVisible(bool visible);
200
201 /**
202 * Enables the search mode, if \p enabled is true. In the search mode the URL navigator
203 * will be hidden and replaced by a line editor that allows to enter a search term.
204 */
205 void setSearchModeEnabled(bool enabled);
206
207 signals:
208 /**
209 * Is emitted whenever the filter bar has changed its visibility state.
210 */
211 void showFilterBarChanged(bool shown);
212 /**
213 * Is emitted whenever the search mode has changed its state.
214 */
215 void searchModeEnabledChanged(bool enabled);
216
217 /**
218 * Is emitted when the write state of the folder has been changed. The application
219 * should disable all actions like "Create New..." that depend on the write
220 * state.
221 */
222 void writeStateChanged(bool isFolderWritable);
223
224 private slots:
225 /**
226 * Updates the number of items (= number of files + number of
227 * directories) in the statusbar. If files are selected, the number
228 * of selected files and the sum of the filesize is shown. The update
229 * is done asynchronously, as getting the sum of the
230 * filesizes can be an expensive operation.
231 * Unless a previous OperationCompletedMessage was set very shortly before
232 * calling this method, it will be overwritten (see DolphinStatusBar::setMessage).
233 * Previous ErrorMessages however are always preserved.
234 */
235 void delayedStatusBarUpdate();
236
237 /**
238 * Is invoked by DolphinViewContainer::delayedStatusBarUpdate() and
239 * updates the status bar synchronously.
240 */
241 void updateStatusBar();
242
243 void updateDirectoryLoadingProgress(int percent);
244
245 void updateDirectorySortingProgress(int percent);
246
247 void updateNavigatorWidgetVisibility();
248
249 /**
250 * Updates the statusbar to show an undetermined progress with the correct
251 * context information whether a searching or a directory loading is done.
252 */
253 void slotDirectoryLoadingStarted();
254
255 /**
256 * Assures that the viewport position is restored and updates the
257 * statusbar to reflect the current content.
258 */
259 void slotDirectoryLoadingCompleted();
260
261 /**
262 * Updates the statusbar to show, that the directory loading has
263 * been canceled.
264 */
265 void slotDirectoryLoadingCanceled();
266
267 /**
268 * Is called if the URL set by DolphinView::setUrl() represents
269 * a file and not a directory. Takes care to activate the file.
270 */
271 void slotUrlIsFileError(const QUrl& url);
272
273 /**
274 * Handles clicking on an item. If the item is a directory, the
275 * directory is opened in the view. If the item is a file, the file
276 * gets started by the corresponding application.
277 */
278 void slotItemActivated(const KFileItem& item);
279
280 /**
281 * Handles activation of multiple files. The files get started by
282 * the corresponding applications.
283 */
284 void slotItemsActivated(const KFileItemList& items);
285
286 /**
287 * Shows the information for the item \a item inside the statusbar. If the
288 * item is null, the default statusbar information is shown.
289 */
290 void showItemInfo(const KFileItem& item);
291
292 void closeFilterBar();
293
294 /**
295 * Filters the currently shown items by \a nameFilter. All items
296 * which contain the given filter string will be shown.
297 */
298 void setNameFilter(const QString& nameFilter);
299
300 /**
301 * Marks the view container as active
302 * (see DolphinViewContainer::setActive()).
303 */
304 void activate();
305
306 /**
307 * Is invoked if the signal urlAboutToBeChanged() from the URL navigator
308 * is emitted. Tries to save the view-state.
309 */
310 void slotUrlNavigatorLocationAboutToBeChanged(const QUrl& url);
311
312 /**
313 * Restores the current view to show \a url and assures
314 * that the root URL of the view is respected.
315 */
316 void slotUrlNavigatorLocationChanged(const QUrl& url);
317
318 /**
319 * @see KUrlNavigator::urlSelectionRequested
320 */
321 void slotUrlSelectionRequested(const QUrl& url);
322
323 /**
324 * Is invoked when a redirection is done and changes the
325 * URL of the URL navigator to \a newUrl without triggering
326 * a reloading of the directory.
327 */
328 void redirect(const QUrl& oldUrl, const QUrl& newUrl);
329
330 /** Requests the focus for the view \a m_view. */
331 void requestFocus();
332
333 /**
334 * Gets the search URL from the searchbox and starts searching.
335 */
336 void startSearching();
337 void closeSearchBox();
338
339 /**
340 * Stops the loading of a directory. Is connected with the "stopPressed" signal
341 * from the statusbar.
342 */
343 void stopDirectoryLoading();
344
345 void slotStatusBarZoomLevelChanged(int zoomLevel);
346
347 /**
348 * Slot that calls showMessage(msg, Error).
349 */
350 void showErrorMessage(const QString& msg);
351
352 private:
353 /**
354 * @return True if the URL protocol is a search URL (e. g. baloosearch:// or filenamesearch://).
355 */
356 bool isSearchUrl(const QUrl& url) const;
357
358 /**
359 * Saves the state of the current view: contents position,
360 * root URL, ...
361 */
362 void saveViewState();
363
364 /**
365 * Restores the state of the current view iff the URL navigator contains a
366 * non-empty location state.
367 */
368 void tryRestoreViewState();
369
370 private:
371 QVBoxLayout* m_topLayout;
372 QWidget* m_navigatorWidget;
373
374 /**
375 * The UrlNavigator within the m_navigatorWidget. m_urlNavigator is
376 * used even when another UrlNavigator is controlling the view to keep
377 * track of this view containers history.
378 */
379 DolphinUrlNavigator *m_urlNavigator;
380
381 /**
382 * The UrlNavigator that is currently connected to the view. This could
383 * either be m_urlNavigator, the urlNavigator in the toolbar or nullptr.
384 */
385 QPointer<DolphinUrlNavigator> m_urlNavigatorConnected;
386 QPushButton* m_emptyTrashButton;
387 DolphinSearchBox* m_searchBox;
388 bool m_searchModeEnabled;
389 KMessageWidget* m_messageWidget;
390
391 DolphinView* m_view;
392
393 FilterBar* m_filterBar;
394
395 DolphinStatusBar* m_statusBar;
396 QTimer* m_statusBarTimer; // Triggers a delayed update
397 QElapsedTimer m_statusBarTimestamp; // Time in ms since last update
398 bool m_autoGrabFocus;
399
400 #ifdef HAVE_KACTIVITIES
401 private:
402 KActivities::ResourceInstance * m_activityResourceInstance;
403 #endif
404 };
405
406 #endif // DOLPHINVIEWCONTAINER_H