-
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
153 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "tab-selector.h" | ||
#include <QAction> | ||
#include <QMenu> | ||
#include <QTabWidget> | ||
#include "tabs/search-tab.h" | ||
|
||
|
||
TabSelector::TabSelector(QTabWidget *tabWidget, QWidget *parent) | ||
: QPushButton(parent), m_tabWidget(tabWidget) | ||
{ | ||
m_menu = new QMenu(this); | ||
m_menu->setStyleSheet("QMenu { menu-scrollable: 1; }"); | ||
connect(m_menu, &QMenu::aboutToShow, this, &TabSelector::menuAboutToShow); | ||
connect(m_menu, &QMenu::triggered, this, &TabSelector::actionTriggered); | ||
setMenu(m_menu); | ||
|
||
updateCounter(); | ||
} | ||
|
||
void TabSelector::setShowTabCount(bool showTabCount) | ||
{ | ||
m_showTabCount = showTabCount; | ||
} | ||
|
||
void TabSelector::markStaticTab(QWidget *tab) | ||
{ | ||
m_staticTabs.append(tab); | ||
} | ||
|
||
void TabSelector::updateCounter() | ||
{ | ||
if (m_showTabCount) { | ||
const QString count = QString::number(m_tabWidget->count()); | ||
setText(count); | ||
setMaximumWidth(23 + 7 * count.length()); | ||
} else { | ||
setMaximumWidth(20); | ||
} | ||
} | ||
|
||
void TabSelector::menuAboutToShow() | ||
{ | ||
m_menu->clear(); | ||
|
||
// Add static tabs at the beginning | ||
for (QWidget *widget : m_staticTabs) { | ||
m_menu->addAction(widget->windowTitle())->setData(QVariant::fromValue(widget)); | ||
} | ||
|
||
// Add separator if necessary | ||
if (m_tabWidget->count() > m_staticTabs.count()) { | ||
m_menu->addSeparator(); | ||
} | ||
|
||
// Add "dynamic" tabs | ||
for (int i = 0; i < m_tabWidget->count(); ++i) { | ||
QWidget *widget = m_tabWidget->widget(i); | ||
if (m_staticTabs.contains(widget)) { | ||
continue; | ||
} | ||
|
||
m_menu->addAction(widget->windowTitle())->setData(QVariant::fromValue(widget)); | ||
} | ||
} | ||
|
||
void TabSelector::actionTriggered(QAction *action) | ||
{ | ||
QWidget *widget = action->data().value<QWidget*>(); | ||
if (widget == nullptr) { | ||
return; | ||
} | ||
|
||
m_tabWidget->setCurrentWidget(widget); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef TAB_SELECTOR_H | ||
#define TAB_SELECTOR_H | ||
|
||
#include <QList> | ||
#include <QPushButton> | ||
|
||
|
||
class QAction; | ||
class QMenu; | ||
class QTabWidget; | ||
class QWidget; | ||
|
||
class TabSelector : public QPushButton | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit TabSelector(QTabWidget *tabWidget, QWidget *parent = nullptr); | ||
|
||
public slots: | ||
void setShowTabCount(bool showTabCount); | ||
void markStaticTab(QWidget *tab); | ||
void updateCounter(); | ||
|
||
private slots: | ||
void menuAboutToShow(); | ||
void actionTriggered(QAction *action); | ||
|
||
signals: | ||
void selected(QWidget *widget); | ||
|
||
private: | ||
QTabWidget *m_tabWidget; | ||
QMenu *m_menu; | ||
QList<QWidget*> m_staticTabs; | ||
bool m_showTabCount = true; | ||
}; | ||
|
||
#endif // TAB_SELECTOR_H |