diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mainwindow/menubar.cpp | 145 | ||||
-rw-r--r-- | src/subwindow/subwindow.cpp | 55 | ||||
-rw-r--r-- | src/subwindow/subwindow.h | 15 | ||||
-rw-r--r-- | src/subwindow/tabwidget.cpp | 4 |
4 files changed, 190 insertions, 29 deletions
diff --git a/src/mainwindow/menubar.cpp b/src/mainwindow/menubar.cpp index 84ce24f..bdfbd23 100644 --- a/src/mainwindow/menubar.cpp +++ b/src/mainwindow/menubar.cpp @@ -27,6 +27,13 @@ #include <QPrinter> #include <QPrinterInfo> #include <QWidgetAction> +#include <functional> + +inline void run_if(SubWindow *_subwindow, std::function<void(SubWindow*, int)> f) +{ + if(_subwindow != nullptr) + f(_subwindow, _subwindow->currentTabIndex()); +} MenuBar::MenuBar(const Configuration *config, MainWindow *parent) : QMenuBar(parent) @@ -164,6 +171,144 @@ MenuBar::MenuBar(const Configuration *config, MainWindow *parent) } }); + subwindow->addSeparator()->setText(tr("Tab Settings")); + + auto *actionPinTab = subwindow->addAction(tr("Pin tab")); + actionPinTab->setCheckable(true); + actionPinTab->setEnabled(false); + + auto *actionLockClose = subwindow->addAction(tr("Prevent tab from closing"), parent, [parent](bool checked) { + auto *_subwindow = parent->currentSubWindow(); + if(_subwindow != nullptr) { + const int idx = _subwindow->currentTabIndex(); + auto data = _subwindow->tabData(idx); + data.closeLocked = checked; + _subwindow->setTabData(data, idx); + } + }); + actionLockClose->setCheckable(true); + + auto *actionLockRefresh = subwindow->addAction(tr("Prevent tab from refreshing"), parent, [parent](bool checked) { + auto *_subwindow = parent->currentSubWindow(); + if(_subwindow != nullptr) { + const int idx = _subwindow->currentTabIndex(); + auto data = _subwindow->tabData(idx); + data.refreshLocked = checked; + _subwindow->setTabData(data, idx); + } + }); + actionLockRefresh->setCheckable(true); + + connect(subwindow, &QMenu::aboutToShow, subwindow, [=]() { + auto *_subwindow = parent->currentSubWindow(); + if(_subwindow != nullptr) { + auto data = _subwindow->tabData(_subwindow->currentTabIndex()); + actionLockClose->setChecked(data.closeLocked); + actionLockRefresh->setChecked(data.refreshLocked); + } + }); + + subwindow->addSeparator()->setText(tr("Tab Actions")); + + auto *leftTab = subwindow->addAction(tr("Switch to tab on the left"), parent, [parent]() { + run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) { + _subwindow->setCurrentTab(qMax(0, currentIdx - 1)); + }); + }); + config->setShortcut(leftTab, "subwindow.shortcuts.left"); + + auto *moveTabLeft = subwindow->addAction(tr("Move tab left"), parent, [parent]() { + run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) { + _subwindow->moveTab(currentIdx, currentIdx - 1); + }); + }); + config->setShortcut(moveTabLeft, "subwindow.shortcuts.moveLeft"); + + auto *rightTab = subwindow->addAction(tr("Switch to tab on the right"), parent, [parent]() { + run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) { + _subwindow->setCurrentTab(qMin(currentIdx + 1, _subwindow->tabCount() - 1)); + }); + }); + config->setShortcut(rightTab, "subwindow.shortcuts.right"); + + auto *moveTabRight = subwindow->addAction(tr("Move tab right"), parent, [parent]() { + run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) { + _subwindow->moveTab(currentIdx, currentIdx + 1); + }); + }); + config->setShortcut(moveTabRight, "subwindow.shortcuts.moveRight"); + + subwindow->addSeparator(); + + auto *closeTab = subwindow->addAction(tr("Close tab"), parent, [parent]() { + run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) { + _subwindow->closeTab(currentIdx); + }); + }); + config->setShortcut(closeTab, "subwindow.shortcuts.close"); + + subwindow->addAction(tr("Close tabs to the left"), parent, [parent]() { + run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) { + for(int i = currentIdx - 1; i >= 0; i--) { + const auto data = _subwindow->tabData(i); + if(!data.closeLocked) + _subwindow->closeTab(i); + } + }); + }); + subwindow->addAction(tr("Close tabs to the right"), parent, [parent]() { + run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) { + for(int i = _subwindow->tabCount() - 1; i > currentIdx; i--) { + const auto data = _subwindow->tabData(i); + if(!data.closeLocked) + _subwindow->closeTab(i); + } + }); + }); + subwindow->addAction(tr("Close all other tabs"), parent, [parent]() { + run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) { + for(int i = _subwindow->tabCount() -1; i >= 0; i--) { + if(i != currentIdx) { + const auto data = _subwindow->tabData(i); + if(!data.closeLocked) + _subwindow->closeTab(i); + } + } + }); + }); + + subwindow->addSeparator(); + + subwindow->addAction(tr("Refresh tabs to the left"), parent, [parent]() { + run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) { + for(int i = 0; i < currentIdx; i++) { + const auto data = _subwindow->tabData(i); + if(!data.refreshLocked) + _subwindow->view(i)->triggerPageAction(QWebEnginePage::Reload); + } + }); + }); + subwindow->addAction(tr("Refresh tabs to the right"), parent, [parent]() { + run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) { + for(int i = currentIdx + 1; i < _subwindow->tabCount(); i++) { + const auto data = _subwindow->tabData(i); + if(!data.refreshLocked) + _subwindow->view(i)->triggerPageAction(QWebEnginePage::Reload); + } + }); + }); + subwindow->addAction(tr("Refresh all other tabs"), parent, [parent]() { + run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) { + for(int i = 0; i < _subwindow->tabCount(); i++) { + if(i != currentIdx) { + const auto data = _subwindow->tabData(i); + if(!data.refreshLocked) + _subwindow->view(i)->triggerPageAction(QWebEnginePage::Reload); + } + } + }); + }); + subwindow->addSeparator(); auto *subwindowProfile = subwindow->addMenu(tr("Subwindow Profile")); diff --git a/src/subwindow/subwindow.cpp b/src/subwindow/subwindow.cpp index 4beba49..a83cf14 100644 --- a/src/subwindow/subwindow.cpp +++ b/src/subwindow/subwindow.cpp @@ -61,34 +61,6 @@ SubWindow::SubWindow(const Configuration *config, QWidget *parent, Qt::WindowFla menu->insertSeparator(firstAction); } - // general actions - auto *closeTab_shortcut = new QShortcut(QKeySequence(config->value<QString>("subwindow.shortcuts.close").value()), this); - connect(closeTab_shortcut, &QShortcut::activated, this, [=]() { - tabWidget->deleteTab(tabWidget->currentIndex()); - }); - - auto *leftTab_shortcut = new QShortcut(QKeySequence(config->value<QString>("subwindow.shortcuts.left").value()), this); - connect(leftTab_shortcut, &QShortcut::activated, this, [=]() { - tabWidget->setCurrentIndex(qMax(0, tabWidget->currentIndex() - 1)); - }); - - auto *moveTabLeft_shortcut = new QShortcut(QKeySequence(config->value<QString>("subwindow.shortcuts.moveLeft").value()), this); - connect(moveTabLeft_shortcut, &QShortcut::activated, this, [=]() { - auto idx = tabWidget->currentIndex(); - tabWidget->tabBar()->moveTab(idx, idx - 1); - }); - - auto *rightTab_shortcut = new QShortcut(QKeySequence(config->value<QString>("subwindow.shortcuts.right").value()), this); - connect(rightTab_shortcut, &QShortcut::activated, this, [=]() { - tabWidget->setCurrentIndex(qMin(tabWidget->currentIndex() + 1, tabWidget->count() - 1)); - }); - - auto *moveTabRight_shortcut = new QShortcut(QKeySequence(config->value<QString>("subwindow.shortcuts.moveRight").value()), this); - connect(moveTabRight_shortcut, &QShortcut::activated, this, [=]() { - auto idx = tabWidget->currentIndex(); - tabWidget->tabBar()->moveTab(idx, idx + 1); - }); - auto *fullScreen_shortcut = new QShortcut(QKeySequence(config->value<QString>("subwindow.shortcuts.fullscreen").value()), this); connect(fullScreen_shortcut, &QShortcut::activated, this, [=]() { auto *w = this->window(); @@ -130,6 +102,11 @@ SubWindow::~SubWindow() delete tabWidget; } +int SubWindow::currentTabIndex() const +{ + return tabWidget->currentIndex(); +} + WebView *SubWindow::currentView() { return qobject_cast<WebView *>(tabWidget->currentWidget()); @@ -164,6 +141,16 @@ WebProfile *SubWindow::profile() const return m_profile; } +void SubWindow::setTabData(TabData &data, int index) +{ + tabWidget->tabBar()->setTabData(index, QVariant::fromValue<TabData>(data)); +} + +SubWindow::TabData SubWindow::tabData(int index) const +{ + return tabWidget->tabBar()->tabData(index).value<TabData>(); +} + int SubWindow::addTab(const QUrl &url, WebProfile *profile) { Q_CHECK_PTR(m_profile); @@ -179,12 +166,22 @@ int SubWindow::addTab(const QUrl &url, WebProfile *profile) return tabWidget->addTab(view); } +void SubWindow::closeTab(int index) +{ + tabWidget->deleteTab(index); +} + void SubWindow::setCurrentTab(int index) { - if(index > 0) + if(index >= 0) tabWidget->setCurrentIndex(index); } +void SubWindow::moveTab(int from, int to) +{ + tabWidget->tabBar()->moveTab(from, to); +} + int SubWindow::restoreLastTab() { return tabWidget->restoreLastTab(); diff --git a/src/subwindow/subwindow.h b/src/subwindow/subwindow.h index 1fc0097..eb7973f 100644 --- a/src/subwindow/subwindow.h +++ b/src/subwindow/subwindow.h @@ -22,9 +22,16 @@ class SubWindow : public QMdiSubWindow Q_OBJECT public: + struct TabData + { + bool closeLocked = false; + bool refreshLocked = false; + }; + explicit SubWindow(const Configuration *config, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); ~SubWindow() override; + int currentTabIndex() const; WebView *currentView(); WebView *view(int index) const; int tabCount() const; @@ -32,13 +39,19 @@ public: void setProfile(WebProfile *profile); WebProfile *profile() const; + void setTabData(TabData &data, int index); + TabData tabData(int index) const; + signals: void currentViewChanged(WebView *view); void showStatusMessage(const QString &message, int timeout = 0); public slots: int addTab(const QUrl &url = QUrl(), WebProfile *profile = nullptr); + void closeTab(int index); + void setCurrentTab(int index); + void moveTab(int from, int to); int restoreLastTab(); void restoreTabMenu(QMenu *menu); @@ -51,4 +64,6 @@ private: QMetaObject::Connection linkHoveredConnection; }; +Q_DECLARE_METATYPE(SubWindow::TabData) + #endif // SMOLBOTE_SUBWINDOW_H diff --git a/src/subwindow/tabwidget.cpp b/src/subwindow/tabwidget.cpp index c635aee..58e9b03 100644 --- a/src/subwindow/tabwidget.cpp +++ b/src/subwindow/tabwidget.cpp @@ -15,6 +15,7 @@ #include <QTabBar> #include "webprofile.h" #include <QWebEngineHistory> +#include "subwindow.h" inline WebView *createViewFromInfo(TabWidget::TabInformation &tab, QWidget *parent) { @@ -93,6 +94,9 @@ int TabWidget::addTab(WebView *view) this->setTabIcon(idx, icon); }); + SubWindow::TabData data; + tabBar()->setTabData(idx, QVariant::fromValue<SubWindow::TabData>(data)); + return idx; } |