mirror-chatterino2/src/widgets/Notebook.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

187 lines
4.9 KiB
C++
Raw Normal View History

#pragma once
2016-12-29 17:31:07 +01:00
2018-06-26 14:09:39 +02:00
#include "widgets/BaseWidget.hpp"
2016-12-29 17:31:07 +01:00
#include <pajlada/signals/signal.hpp>
#include <pajlada/signals/signalholder.hpp>
2017-01-18 04:52:47 +01:00
#include <QList>
#include <QMenu>
#include <QMessageBox>
2017-01-18 04:52:47 +01:00
#include <QWidget>
#include <functional>
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-01-18 21:30:23 +01:00
2017-11-12 17:21:50 +01:00
class Window;
class UpdateDialog;
class NotebookButton;
class NotebookTab;
class SplitContainer;
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 *)>;
2018-05-23 11:59:37 +02:00
class Notebook : public BaseWidget
2018-04-18 09:12:29 +02:00
{
Q_OBJECT
public:
2018-05-23 11:59:37 +02:00
explicit Notebook(QWidget *parent);
~Notebook() override = default;
2018-04-18 09:12:29 +02:00
2018-05-23 11:59:37 +02:00
NotebookTab *addPage(QWidget *page, QString title = QString(),
bool select = false);
2018-04-18 09:12:29 +02:00
void removePage(QWidget *page);
void removeCurrentPage();
int indexOf(QWidget *page) const;
virtual void select(QWidget *page, bool focusPage = true);
void selectIndex(int index, bool focusPage = true);
void selectVisibleIndex(int index, bool focusPage = true);
void selectNextTab(bool focusPage = true);
void selectPreviousTab(bool focusPage = true);
void selectLastTab(bool focusPage = true);
2018-04-18 09:12:29 +02:00
int getPageCount() const;
2018-05-23 04:22:17 +02:00
QWidget *getPageAt(int index) const;
2018-04-18 09:12:29 +02:00
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 getShowTabs() const;
void setShowTabs(bool value);
2018-04-18 09:12:29 +02:00
bool getShowAddButton() const;
void setShowAddButton(bool value);
void setTabLocation(NotebookTabLocation location);
bool isNotebookLayoutLocked() const;
void setLockNotebookLayout(bool value);
void addNotebookActionsToMenu(QMenu *menu);
// Update layout and tab visibility
void refresh();
2018-04-18 09:12:29 +02:00
protected:
2018-07-06 17:11:37 +02:00
virtual void scaleChangedEvent(float scale_) override;
2018-04-18 09:12:29 +02:00
virtual void resizeEvent(QResizeEvent *) override;
virtual void mousePressEvent(QMouseEvent *event) override;
2018-04-18 09:12:29 +02:00
virtual void paintEvent(QPaintEvent *) override;
2018-05-23 04:22:17 +02:00
NotebookButton *getAddButton();
NotebookButton *addCustomButton();
2018-05-23 04:22:17 +02:00
2018-04-18 09:12:29 +02:00
struct Item {
2018-09-04 21:39:54 +02:00
NotebookTab *tab{};
QWidget *page{};
QWidget *selectedWidget{};
2018-04-18 09:12:29 +02:00
};
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 updateTabVisibilityMenuAction();
void resizeAddButton();
2018-05-31 16:02:20 +02:00
bool containsPage(QWidget *page);
Item *findItem(QWidget *page);
2018-05-31 16:02:20 +02:00
static bool containsChild(const QObject *obj, const QObject *child);
2018-07-06 19:23:47 +02:00
NotebookTab *getTabFromPage(QWidget *page);
2018-05-31 16:02:20 +02:00
// Returns the number of buttons in `customButtons_` that are visible
size_t visibleButtonCount() const;
2018-07-06 19:23:47 +02:00
QList<Item> items_;
QMenu menu_;
2018-07-06 19:23:47 +02:00
QWidget *selectedPage_ = nullptr;
2018-04-18 09:12:29 +02:00
NotebookButton *addButton_;
2018-07-06 19:23:47 +02:00
std::vector<NotebookButton *> customButtons_;
2018-04-18 09:12:29 +02:00
2018-07-06 19:23:47 +02:00
bool allowUserTabManagement_ = false;
bool showTabs_ = true;
2018-07-06 19:23:47 +02:00
bool showAddButton_ = false;
int lineOffset_ = 20;
bool lockNotebookLayout_ = false;
NotebookTabLocation tabLocation_ = NotebookTabLocation::Top;
QAction *lockNotebookLayoutAction_;
QAction *showTabsAction_;
// This filter, if set, is used to figure out the visibility of
// the tabs in this notebook.
TabVisibilityFilter tabVisibilityFilter_;
2018-04-18 09:12:29 +02:00
};
class SplitNotebook : public Notebook
2016-12-29 17:31:07 +01:00
{
public:
SplitNotebook(Window *parent);
2018-05-23 04:22:17 +02:00
SplitContainer *addPage(bool select = false);
2018-04-10 15:59:53 +02:00
SplitContainer *getOrAddSelectedPage();
void select(QWidget *page, bool focusPage = true) override;
void themeChangedEvent() override;
2020-09-26 01:52:39 +02:00
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();
2016-12-29 17:31:07 +01:00
};
2017-04-14 17:52:22 +02:00
} // namespace chatterino