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