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