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