]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.h
DragAndDropHelper::updateDropAction: use StatJob for remote URLs
[dolphin.git] / src / dolphintabwidget.h
1 /*
2 * SPDX-FileCopyrightText: 2014 Emmanuel Pescosta <emmanuelpescosta099@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #ifndef DOLPHIN_TAB_WIDGET_H
8 #define DOLPHIN_TAB_WIDGET_H
9
10 #include "dolphinnavigatorswidgetaction.h"
11 #include "dolphintabpage.h"
12 #include "views/draganddrophelper.h"
13
14 #include <QTabWidget>
15 #include <QUrl>
16
17 #include <optional>
18
19 class DolphinViewContainer;
20 class KConfigGroup;
21
22 class DolphinTabWidget : public QTabWidget
23 {
24 Q_OBJECT
25
26 public:
27 /**
28 * @param navigatorsWidget The navigatorsWidget which is always going to be connected
29 * to the active tabPage.
30 */
31 explicit DolphinTabWidget(DolphinNavigatorsWidgetAction *navigatorsWidget, QWidget *parent);
32
33 /**
34 * Where a newly opened tab should be placed.
35 */
36 enum class NewTabPosition {
37 FollowSetting, ///< Honor openNewTabAfterLastTab setting
38 AfterCurrent, ///< After the current tab
39 AtEnd, ///< At the end of the tab bar
40 };
41
42 /**
43 * @return Tab page at the current index (can be 0 if tabs count is smaller than 1)
44 */
45 DolphinTabPage *currentTabPage() const;
46
47 /**
48 * @return the next tab page. If the current active tab is the last tab,
49 * it returns the first tab. If there is only one tab, returns nullptr
50 */
51 DolphinTabPage *nextTabPage() const;
52
53 /**
54 * @return the previous tab page. If the current active tab is the first tab,
55 * it returns the last tab. If there is only one tab, returns nullptr
56 */
57 DolphinTabPage *prevTabPage() const;
58
59 /**
60 * @return Tab page at the given \a index (can be 0 if the index is out-of-range)
61 */
62 DolphinTabPage *tabPageAt(const int index) const;
63
64 void saveProperties(KConfigGroup &group) const;
65 void readProperties(const KConfigGroup &group);
66
67 /**
68 * Refreshes the views of the main window by recreating them according to
69 * the given Dolphin settings.
70 */
71 void refreshViews();
72
73 /**
74 * Update the name of the tab with the index \a index.
75 */
76 void updateTabName(int index);
77
78 /**
79 * @return Whether any of the tab pages has @p url opened
80 * in their primary or secondary view.
81 */
82 bool isUrlOpen(const QUrl &url) const;
83
84 /**
85 * @return Whether the item with @p url can be found in any view only by switching
86 * between already open tabs and scrolling in their primary or secondary view.
87 */
88 bool isItemVisibleInAnyView(const QUrl &urlOfItem) const;
89
90 Q_SIGNALS:
91 /**
92 * Is emitted when the active view has been changed, by changing the current
93 * tab or by activating another view when split view is enabled in the current
94 * tab.
95 */
96 void activeViewChanged(DolphinViewContainer *viewContainer);
97
98 /**
99 * Is emitted when the number of open tabs has changed (e.g. by opening or
100 * closing a tab)
101 */
102 void tabCountChanged(int count);
103
104 /**
105 * Is emitted when a tab has been closed.
106 */
107 void rememberClosedTab(const QUrl &url, const QByteArray &state);
108
109 /**
110 * Is emitted when the url of the current tab has been changed. This signal
111 * is also emitted when the active view has been changed.
112 */
113 void currentUrlChanged(const QUrl &url);
114
115 /**
116 * Is emitted when the url of any tab has been changed (including the current tab).
117 */
118 void urlChanged(const QUrl &url);
119
120 public Q_SLOTS:
121 /**
122 * Opens a new view with the current URL that is part of a tab and activates
123 * the tab.
124 */
125 void openNewActivatedTab();
126
127 /**
128 * Opens a new tab showing the URL \a primaryUrl and the optional URL
129 * \a secondaryUrl and activates the tab.
130 */
131 void openNewActivatedTab(const QUrl &primaryUrl, const QUrl &secondaryUrl = QUrl());
132
133 /**
134 * Opens a new tab in the background showing the URL \a primaryUrl and the
135 * optional URL \a secondaryUrl.
136 */
137 void openNewTab(const QUrl &primaryUrl,
138 const QUrl &secondaryUrl = QUrl(),
139 DolphinTabWidget::NewTabPosition position = DolphinTabWidget::NewTabPosition::FollowSetting);
140
141 /**
142 * Opens each directory in \p dirs in a separate tab unless it is already open.
143 * If \a splitView is set, 2 directories are collected within one tab.
144 * \pre \a dirs must contain at least one url.
145 */
146 void openDirectories(const QList<QUrl> &dirs, bool splitView);
147
148 /**
149 * Opens the directories which contain the files \p files and selects all files.
150 * If \a splitView is set, 2 directories are collected within one tab.
151 * \pre \a files must contain at least one url.
152 */
153 void openFiles(const QList<QUrl> &files, bool splitView);
154
155 /**
156 * Closes the currently active tab.
157 */
158 void closeTab();
159
160 /**
161 * Closes the tab with the index \a index and activates the tab with index - 1.
162 */
163 void closeTab(const int index);
164
165 /**
166 * Activates the tab with the index \a index.
167 */
168 void activateTab(const int index);
169
170 /**
171 * Activates the last tab in the tab bar.
172 */
173 void activateLastTab();
174
175 /**
176 * Activates the next tab in the tab bar.
177 * If the current active tab is the last tab, it activates the first tab.
178 */
179 void activateNextTab();
180
181 /**
182 * Activates the previous tab in the tab bar.
183 * If the current active tab is the first tab, it activates the last tab.
184 */
185 void activatePrevTab();
186
187 /**
188 * Is called when the user wants to reopen a previously closed tab from
189 * the recent tabs menu.
190 */
191 void restoreClosedTab(const QByteArray &state);
192
193 /** Copies all selected items to the inactive view. */
194 void copyToInactiveSplitView();
195
196 /** Moves all selected items to the inactive view. */
197 void moveToInactiveSplitView();
198
199 private Q_SLOTS:
200 /**
201 * Opens the tab with the index \a index in a new Dolphin instance and closes
202 * this tab.
203 */
204 void detachTab(int index);
205
206 /**
207 * Opens a new tab showing the url from tab at the given \a index and
208 * activates the tab.
209 */
210 void openNewActivatedTab(int index);
211
212 /**
213 * Is connected to the KTabBar signal receivedDragMoveEvent.
214 * Allows dragging and dropping files onto tabs.
215 */
216 void tabDragMoveEvent(int tab, QDragMoveEvent *event);
217
218 /**
219 * Is connected to the KTabBar signal receivedDropEvent.
220 * Allows dragging and dropping files onto tabs.
221 */
222 void tabDropEvent(int tab, QDropEvent *event);
223
224 /**
225 * The active view url of a tab has been changed so update the text and the
226 * icon of the corresponding tab.
227 */
228 void tabUrlChanged(const QUrl &url);
229
230 void currentTabChanged(int index);
231
232 protected:
233 void tabInserted(int index) override;
234 void tabRemoved(int index) override;
235
236 private:
237 /**
238 * @param tabPage The tab page to get the name of
239 * @return The name of the tab page
240 */
241 QString tabName(DolphinTabPage *tabPage) const;
242
243 struct ViewIndex {
244 const int tabIndex;
245 const bool isInPrimaryView;
246 };
247
248 /**
249 * Getter for a view container.
250 * @param viewIndex specifies the tab and the view within that tab.
251 * @return the view container specified in @p viewIndex or nullptr if it doesn't exist.
252 */
253 DolphinViewContainer *viewContainerAt(ViewIndex viewIndex) const;
254
255 /**
256 * Makes the view container specified in @p viewIndex become the active view container within this tab widget.
257 * @param viewIndex Specifies the tab to activate and the view container within the tab to activate.
258 * @return the freshly activated view container or nullptr if there is no view container at @p viewIndex.
259 */
260 DolphinViewContainer *activateViewContainerAt(ViewIndex viewIndex);
261
262 /**
263 * Get the position of the view within this widget that is open at @p directory.
264 * @param directory The URL of the directory we want to find.
265 * @return a small struct containing the tab index of the view and whether it is
266 * in the primary view. A std::nullopt is returned if there is no view open for @p directory.
267 */
268 const std::optional<const ViewIndex> viewOpenAtDirectory(const QUrl &directory) const;
269
270 /**
271 * Get the position of the view within this widget that has @p item in the view.
272 * This means that the item can be seen by the user in that view when scrolled to the right position.
273 * If the view has folders expanded and @p item is one of them, the view will also be returned.
274 * @param item The URL of the item we want to find.
275 * @return a small struct containing the tab index of the view and whether it is
276 * in the primary view. A std::nullopt is returned if there is no view open that has @p item visible anywhere.
277 */
278 const std::optional<const ViewIndex> viewShowingItem(const QUrl &item) const;
279
280 DragAndDropHelper m_dragAndDropHelper;
281
282 private:
283 QPointer<DolphinTabPage> m_lastViewedTab;
284 QPointer<DolphinNavigatorsWidgetAction> m_navigatorsWidget;
285 };
286
287 #endif