-
-
Notifications
You must be signed in to change notification settings - Fork 460
/
Copy pathNotebook.hpp
257 lines (196 loc) · 6.83 KB
/
Notebook.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#pragma once
#include "widgets/BaseWidget.hpp"
#include <pajlada/signals/signal.hpp>
#include <pajlada/signals/signalholder.hpp>
#include <QList>
#include <QMenu>
#include <QMessageBox>
#include <QWidget>
#include <functional>
namespace chatterino {
class Window;
class UpdateDialog;
class NotebookButton;
class NotebookTab;
class SplitContainer;
class Split;
enum NotebookTabLocation { Top = 0, Left = 1, Right = 2, Bottom = 3 };
// Controls the visibility of tabs in this notebook
enum NotebookTabVisibility : int {
// Show all tabs
AllTabs = 0,
// Only show tabs containing splits that are live
LiveOnly = 1,
};
using TabVisibilityFilter = std::function<bool(const NotebookTab *)>;
class Notebook : public BaseWidget
{
Q_OBJECT
public:
explicit Notebook(QWidget *parent);
~Notebook() override = default;
NotebookTab *addPage(QWidget *page, QString title = QString(),
bool select = false);
/**
* @brief Adds a page to the Notebook at a given position.
*
* @param position if set to -1, adds the page to the end
**/
NotebookTab *addPageAt(QWidget *page, int position,
QString title = QString(), bool select = false);
void removePage(QWidget *page);
void duplicatePage(QWidget *page);
void removeCurrentPage();
/**
* @brief Returns index of page in Notebook, or -1 if not found.
**/
int indexOf(QWidget *page) const;
/**
* @brief Returns the visible index of page in Notebook, or -1 if not found.
* Given page should be visible according to the set TabVisibilityFilter.
**/
int visibleIndexOf(QWidget *page) const;
/**
* @brief Returns the number of visible tabs in Notebook.
**/
int getVisibleTabCount() const;
/**
* @brief Selects the Notebook tab containing the given page.
**/
virtual void select(QWidget *page, bool focusPage = true);
/**
* @brief Selects the Notebook tab at the given index. Ignores whether tabs
* are visible or not.
**/
void selectIndex(int index, bool focusPage = true);
/**
* @brief Selects the index'th visible tab in the Notebook.
*
* For example, selecting the 0th visible tab selects the first tab in this
* Notebook that is visible according to the TabVisibilityFilter. If no filter
* is set, equivalent to Notebook::selectIndex.
**/
void selectVisibleIndex(int index, bool focusPage = true);
/**
* @brief Selects the next visible tab. Wraps to the start if required.
**/
void selectNextTab(bool focusPage = true);
/**
* @brief Selects the previous visible tab. Wraps to the end if required.
**/
void selectPreviousTab(bool focusPage = true);
/**
* @brief Selects the last visible tab.
**/
void selectLastTab(bool focusPage = true);
int getPageCount() const;
QWidget *getPageAt(int index) const;
int getSelectedIndex() const;
QWidget *getSelectedPage() const;
QWidget *tabAt(QPoint point, int &index, int maxWidth = 2000000000);
void rearrangePage(QWidget *page, int index);
bool getAllowUserTabManagement() const;
void setAllowUserTabManagement(bool value);
bool getShowAddButton() const;
void setShowAddButton(bool value);
void setTabLocation(NotebookTabLocation location);
bool isNotebookLayoutLocked() const;
void setLockNotebookLayout(bool value);
virtual void addNotebookActionsToMenu(QMenu *menu);
// Update layout and tab visibility
void refresh();
protected:
bool getShowTabs() const;
void setShowTabs(bool value);
void scaleChangedEvent(float scale_) override;
void resizeEvent(QResizeEvent *) override;
void mousePressEvent(QMouseEvent *event) override;
void paintEvent(QPaintEvent *) override;
NotebookButton *getAddButton();
NotebookButton *addCustomButton();
struct Item {
NotebookTab *tab{};
QWidget *page{};
QWidget *selectedWidget{};
};
const QList<Item> items()
{
return items_;
}
/**
* @brief Apply the given tab visibility filter
*
* An empty function can be provided to denote that no filter will be applied
*
* Tabs will be redrawn after this function is called.
**/
void setTabVisibilityFilter(TabVisibilityFilter filter);
/**
* @brief shouldShowTab has the final say whether a tab should be visible right now.
**/
bool shouldShowTab(const NotebookTab *tab) const;
private:
void performLayout(bool animate = false);
/**
* @brief Show a popup informing the user of some big tab visibility changes
**/
void showTabVisibilityInfoPopup();
/**
* @brief Updates the visibility state of all tabs
**/
void updateTabVisibility();
void resizeAddButton();
bool containsPage(QWidget *page);
Item *findItem(QWidget *page);
static bool containsChild(const QObject *obj, const QObject *child);
NotebookTab *getTabFromPage(QWidget *page);
// Returns the number of buttons in `customButtons_` that are visible
size_t visibleButtonCount() const;
QList<Item> items_;
QMenu *menu_ = nullptr;
QWidget *selectedPage_ = nullptr;
NotebookButton *addButton_;
std::vector<NotebookButton *> customButtons_;
bool allowUserTabManagement_ = false;
bool showTabs_ = true;
bool showAddButton_ = false;
int lineOffset_ = 20;
bool lockNotebookLayout_ = false;
bool refreshPaused_ = false;
bool refreshRequested_ = false;
NotebookTabLocation tabLocation_ = NotebookTabLocation::Top;
QAction *lockNotebookLayoutAction_;
QAction *toggleTopMostAction_;
// This filter, if set, is used to figure out the visibility of
// the tabs in this notebook.
TabVisibilityFilter tabVisibilityFilter_;
};
class SplitNotebook : public Notebook
{
public:
SplitNotebook(Window *parent);
SplitContainer *addPage(bool select = false);
SplitContainer *getOrAddSelectedPage();
/// Returns `nullptr` when no page is selected.
SplitContainer *getSelectedPage();
void select(QWidget *page, bool focusPage = true) override;
void themeChangedEvent() override;
void addNotebookActionsToMenu(QMenu *menu) override;
void forEachSplit(const std::function<void(Split *)> &cb);
/**
* Toggles between the "Show all tabs" and "Hide all tabs" tab visibility states
*/
void toggleTabVisibility();
QAction *showAllTabsAction;
QAction *onlyShowLiveTabsAction;
QAction *hideAllTabsAction;
protected:
void showEvent(QShowEvent *event) override;
private:
void addCustomButtons();
pajlada::Signals::SignalHolder signalHolder_;
// Main window on Windows has basically a duplicate of this in Window
NotebookButton *streamerModeIcon_{};
void updateStreamerModeIcon();
};
} // namespace chatterino