]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.h
Fix item highlighting through DBus
[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 /**
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 * @return Whether any of the tab pages has @p url opened
75 * in their primary or secondary view.
76 */
77 bool isUrlOpen(const QUrl& url) const;
78
79 /**
80 * @return Whether the item with @p url can be found in any view only by switching
81 * between already open tabs and scrolling in their primary or secondary view.
82 */
83 bool isItemVisibleInAnyView(const QUrl& urlOfItem) const;
84
85 Q_SIGNALS:
86 /**
87 * Is emitted when the active view has been changed, by changing the current
88 * tab or by activating another view when split view is enabled in the current
89 * tab.
90 */
91 void activeViewChanged(DolphinViewContainer* viewContainer);
92
93 /**
94 * Is emitted when the number of open tabs has changed (e.g. by opening or
95 * closing a tab)
96 */
97 void tabCountChanged(int count);
98
99 /**
100 * Is emitted when a tab has been closed.
101 */
102 void rememberClosedTab(const QUrl& url, const QByteArray& state);
103
104 /**
105 * Is emitted when the url of the current tab has been changed. This signal
106 * is also emitted when the active view has been changed.
107 */
108 void currentUrlChanged(const QUrl& url);
109
110 public Q_SLOTS:
111 /**
112 * Opens a new view with the current URL that is part of a tab and activates
113 * the tab.
114 */
115 void openNewActivatedTab();
116
117 /**
118 * Opens a new tab showing the URL \a primaryUrl and the optional URL
119 * \a secondaryUrl and activates the tab.
120 */
121 void openNewActivatedTab(const QUrl& primaryUrl, const QUrl& secondaryUrl = QUrl());
122
123 /**
124 * Opens a new tab in the background showing the URL \a primaryUrl and the
125 * optional URL \a secondaryUrl.
126 */
127 void openNewTab(const QUrl &primaryUrl, const QUrl &secondaryUrl = QUrl(), NewTabPosition position = NewTabPosition::FollowSetting);
128
129 /**
130 * Opens each directory in \p dirs in a separate tab unless it is already open.
131 * If \a splitView is set, 2 directories are collected within one tab.
132 * \pre \a dirs must contain at least one url.
133 */
134 void openDirectories(const QList<QUrl>& dirs, bool splitView);
135
136 /**
137 * Opens the directories which contain the files \p files and selects all files.
138 * If \a splitView is set, 2 directories are collected within one tab.
139 * \pre \a files must contain at least one url.
140 */
141 void openFiles(const QList<QUrl> &files, bool splitView);
142
143 /**
144 * Closes the currently active tab.
145 */
146 void closeTab();
147
148 /**
149 * Closes the tab with the index \a index and activates the tab with index - 1.
150 */
151 void closeTab(const int index);
152
153 /**
154 * Activates the tab with the index \a index.
155 */
156 void activateTab(const int index);
157
158 /**
159 * Activates the last tab in the tab bar.
160 */
161 void activateLastTab();
162
163 /**
164 * Activates the next tab in the tab bar.
165 * If the current active tab is the last tab, it activates the first tab.
166 */
167 void activateNextTab();
168
169 /**
170 * Activates the previous tab in the tab bar.
171 * If the current active tab is the first tab, it activates the last tab.
172 */
173 void activatePrevTab();
174
175 /**
176 * Is called when the user wants to reopen a previously closed tab from
177 * the recent tabs menu.
178 */
179 void restoreClosedTab(const QByteArray& state);
180
181 /** Copies all selected items to the inactive view. */
182 void copyToInactiveSplitView();
183
184 /** Moves all selected items to the inactive view. */
185 void moveToInactiveSplitView();
186
187 private Q_SLOTS:
188 /**
189 * Opens the tab with the index \a index in a new Dolphin instance and closes
190 * this tab.
191 */
192 void detachTab(int index);
193
194 /**
195 * Opens a new tab showing the url from tab at the given \a index and
196 * activates the tab.
197 */
198 void openNewActivatedTab(int index);
199
200 /**
201 * Is connected to the KTabBar signal receivedDropEvent.
202 * Allows dragging and dropping files onto tabs.
203 */
204 void tabDropEvent(int tab, QDropEvent* event);
205
206 /**
207 * The active view url of a tab has been changed so update the text and the
208 * icon of the corresponding tab.
209 */
210 void tabUrlChanged(const QUrl& url);
211
212 void currentTabChanged(int index);
213
214 protected:
215 void tabInserted(int index) override;
216 void tabRemoved(int index) override;
217
218 private:
219 /**
220 * @param tabPage The tab page to get the name of
221 * @return The name of the tab page
222 */
223 QString tabName(DolphinTabPage* tabPage) const;
224
225 struct ViewIndex {
226 const int tabIndex;
227 const bool isInPrimaryView;
228 };
229 /**
230 * Get the position of the view within this widget that is open at @p directory.
231 * @param directory The URL of the directory we want to find.
232 * @return a small struct containing the tab index of the view and whether it is
233 * in the primary view. A std::nullopt is returned if there is no view open for @p directory.
234 */
235 const std::optional<const ViewIndex> viewOpenAtDirectory(const QUrl& directory) const;
236
237 /**
238 * Get the position of the view within this widget that has @p item in the view.
239 * This means that the item can be seen by the user in that view when scrolled to the right position.
240 * If the view has folders expanded and @p item is one of them, the view will also be returned.
241 * @param item The URL of the item we want to find.
242 * @return a small struct containing the tab index of the view and whether it is
243 * in the primary view. A std::nullopt is returned if there is no view open that has @p item visible anywhere.
244 */
245 const std::optional<const ViewIndex> viewShowingItem(const QUrl& item) const;
246
247 private:
248 QPointer<DolphinTabPage> m_lastViewedTab;
249 QPointer<DolphinNavigatorsWidgetAction> m_navigatorsWidget;
250 };
251
252 #endif