1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

[ComboTabBar] Move right/left containers to ComboTabBar class

It is now possible to set corner widgets of all tabbar, not only
main tabbar.
This commit is contained in:
nowrep 2014-03-18 17:35:44 +01:00
parent 9e9b880235
commit 27a3781ed0
7 changed files with 107 additions and 168 deletions

View File

@ -60,11 +60,25 @@ ComboTabBar::ComboTabBar(QWidget* parent)
m_mainTabBar->setActiveTabBar(true);
m_pinnedTabBar->setTabsClosable(false);
m_leftLayout = new QHBoxLayout;
m_leftLayout->setSpacing(0);
m_leftLayout->setContentsMargins(0, 0, 0, 0);
m_leftContainer = new QWidget(this);
m_leftContainer->setLayout(m_leftLayout);
m_rightLayout = new QHBoxLayout;
m_rightLayout->setSpacing(0);
m_rightLayout->setContentsMargins(0, 0, 0, 0);
m_rightContainer = new QWidget(this);
m_rightContainer->setLayout(m_rightLayout);
m_mainLayout = new QHBoxLayout;
m_mainLayout->setSpacing(0);
m_mainLayout->setContentsMargins(0, 0, 0, 0);
m_mainLayout->addWidget(m_pinnedTabBarWidget, 4);
m_mainLayout->addWidget(m_mainTabBarWidget, 1);
m_mainLayout->addWidget(m_leftContainer);
m_mainLayout->addWidget(m_pinnedTabBarWidget);
m_mainLayout->addWidget(m_mainTabBarWidget);
m_mainLayout->addWidget(m_rightContainer);
setLayout(m_mainLayout);
connect(m_mainTabBar, SIGNAL(currentChanged(int)), this, SLOT(slotCurrentChanged(int)));
@ -108,8 +122,7 @@ int ComboTabBar::insertTab(int index, const QIcon &icon, const QString &text, bo
if (tabsClosable()) {
QWidget* closeButton = m_mainTabBar->tabButton(index, closeButtonPosition());
if ((closeButton && closeButton->objectName() != QLatin1String("combotabbar_tabs_close_button")) ||
!closeButton) {
if ((closeButton && closeButton->objectName() != QLatin1String("combotabbar_tabs_close_button")) || !closeButton) {
// insert our close button
insertCloseButton(index + pinnedTabsCount());
if (closeButton) {
@ -465,9 +478,6 @@ void ComboTabBar::setObjectName(const QString &name)
{
m_mainTabBar->setObjectName(name);
m_pinnedTabBar->setObjectName(name);
m_pinnedTabBarWidget->setContainersName(name);
m_mainTabBarWidget->setContainersName(name);
}
void ComboTabBar::setMouseTracking(bool enable)
@ -499,6 +509,8 @@ void ComboTabBar::setUpLayout()
setFixedHeight(height);
m_pinnedTabBar->setFixedHeight(height);
m_leftContainer->setFixedHeight(height);
m_rightContainer->setFixedHeight(height);
m_mainTabBarWidget->setUpLayout();
m_pinnedTabBarWidget->setUpLayout();
@ -530,6 +542,7 @@ void ComboTabBar::setCloseButtonsToolTip(const QString &tip)
void ComboTabBar::enableBluredBackground(bool enable)
{
m_bluredBackground = enable;
m_mainTabBar->enableBluredBackground(enable);
m_pinnedTabBar->enableBluredBackground(enable);
m_mainTabBarWidget->enableBluredBackground(enable);
@ -581,19 +594,41 @@ void ComboTabBar::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
// Handle overflowing eg. when entering fullscreen
if (m_mainBarOverFlowed != m_mainTabBarWidget->isOverflowed()) {
setMinimumWidths();
}
// Workaround for containers being hidden on very fast resizing
if (m_rightContainer->isVisible()) {
m_rightContainer->hide();
m_rightContainer->show();
}
if (m_leftContainer->isVisible()) {
m_leftContainer->hide();
m_leftContainer->show();
}
}
bool ComboTabBar::eventFilter(QObject* obj, QEvent* ev)
{
if (m_bluredBackground) {
if (ev->type() == QEvent::Paint && (obj == m_leftContainer || obj == m_rightContainer)) {
QPaintEvent* event = static_cast<QPaintEvent*>(ev);
QPainter p(qobject_cast<QWidget*>(obj));
p.setCompositionMode(QPainter::CompositionMode_Clear);
p.fillRect(event->rect(), QColor(0, 0, 0, 0));
}
}
if (obj == m_mainTabBar && ev->type() == QEvent::Resize) {
QResizeEvent* event = static_cast<QResizeEvent*>(ev);
if (event->oldSize().height() != event->size().height()) {
setUpLayout();
}
}
if (ev->type() == QEvent::Wheel) {
// Handle wheel events exclusively in ComboTabBar
wheelEvent(static_cast<QWheelEvent*>(ev));
@ -667,13 +702,21 @@ bool ComboTabBar::isDragInProgress() const
return m_mainTabBar->isDragInProgress() || m_pinnedTabBar->isDragInProgress();
}
void ComboTabBar::addMainBarWidget(QWidget* widget, Qt::Alignment align, int stretch, Qt::Alignment layoutAlignment)
bool ComboTabBar::isMainBarOverflowed() const
{
if (align == Qt::AlignRight) {
m_mainTabBarWidget->addRightWidget(widget, stretch, layoutAlignment);
return m_mainBarOverFlowed;
}
void ComboTabBar::addCornerWidget(QWidget* widget, Qt::Corner corner)
{
if (corner == Qt::TopLeftCorner) {
m_leftLayout->addWidget(widget);
}
else if (corner == Qt::TopRightCorner) {
m_rightLayout->addWidget(widget);
}
else {
m_mainTabBarWidget->addLeftWidget(widget, stretch, layoutAlignment);
qWarning() << "ComboTabBar::addCornerWidget Only TopLeft and TopRight corners are implemented!";
}
}
@ -762,13 +805,18 @@ void ComboTabBar::setMinimumWidths()
int pinnedTabBarWidth = pinnedTabsCount() * comboTabBarPixelMetric(PinnedTabWidth);
m_pinnedTabBar->setMinimumWidth(pinnedTabBarWidth);
m_pinnedTabBarWidget->setMaximumWidth(pinnedTabBarWidth);
m_pinnedTabBarWidget->setFixedWidth(pinnedTabBarWidth);
// Width that is needed by main tabbar
int mainTabBarWidth = comboTabBarPixelMetric(NormalTabMinimumWidth) * (m_mainTabBar->count() - 1) +
comboTabBarPixelMetric(ActiveTabMinimumWidth) +
comboTabBarPixelMetric(ExtraReservedWidth);
if (mainTabBarWidth <= m_mainTabBarWidget->width()) {
// This is the full width that would be needed for the tabbar (including pinned tabbar)
int realTabBarWidth = mainTabBarWidth + m_pinnedTabBarWidget->width();
// Does it fit in our widget?
if (realTabBarWidth <= width()) {
if (m_mainBarOverFlowed) {
m_mainBarOverFlowed = false;
emit overFlowChanged(false);
@ -783,13 +831,9 @@ void ComboTabBar::setMinimumWidths()
emit overFlowChanged(true);
}
// The following line is the cause of calling tabSizeHint() for all tabs that is
// time consuming, Because of this we notify application to using a lighter
// version of it. (this is safe because all normal tabs have the same size)
// All tabs have now same width, we can use fast tabSizeHint
m_mainTabBar->useFastTabSizeHint(true);
if (m_mainTabBar->count() * comboTabBarPixelMetric(OverflowedTabWidth) != m_mainTabBar->minimumWidth()) {
m_mainTabBar->setMinimumWidth(m_mainTabBar->count() * comboTabBarPixelMetric(OverflowedTabWidth));
}
m_mainTabBar->setMinimumWidth(m_mainTabBar->count() * comboTabBarPixelMetric(OverflowedTabWidth));
}
}
@ -1089,13 +1133,12 @@ void TabBarHelper::mouseReleaseEvent(QMouseEvent* event)
QTabBar::mouseReleaseEvent(event);
if (m_pressedIndex >= 0) {
int length = qAbs(m_pressedGlobalX - event->globalX());
int duration = qMin((length * ANIMATION_DURATION) / tabRect(m_pressedIndex).width(),
ANIMATION_DURATION);
const int length = qAbs(m_pressedGlobalX - event->globalX());
const int duration = qMin((length * ANIMATION_DURATION) / tabRect(m_pressedIndex).width(), ANIMATION_DURATION);
QTimer::singleShot(duration, this, SLOT(resetDragState()));
m_pressedIndex = -1;
m_pressedGlobalX = -1;
QTimer::singleShot(duration, this, SLOT(resetDragState()));
}
}
@ -1169,37 +1212,12 @@ TabBarScrollWidget::TabBarScrollWidget(QTabBar* tabBar, QWidget* parent)
connect(m_rightScrollButton, SIGNAL(doubleClicked()), this, SLOT(scrollToRightEdge()));
connect(m_rightScrollButton, SIGNAL(middleMouseClicked()), this, SLOT(ensureVisible()));
m_leftLayout = new QHBoxLayout;
m_leftLayout->setSpacing(0);
m_leftLayout->setContentsMargins(0, 0, 0, 0);
m_rightLayout = new QHBoxLayout;
m_rightLayout->setSpacing(0);
m_rightLayout->setContentsMargins(0, 0, 0, 0);
QHBoxLayout* leftLayout = new QHBoxLayout;
leftLayout->setSpacing(0);
leftLayout->setContentsMargins(0, 0, 0, 0);
leftLayout->addLayout(m_leftLayout);
leftLayout->addWidget(m_leftScrollButton);
QHBoxLayout* rightLayout = new QHBoxLayout;
rightLayout->setSpacing(0);
rightLayout->setContentsMargins(0, 0, 0, 0);
rightLayout->addWidget(m_rightScrollButton);
rightLayout->addLayout(m_rightLayout);
m_leftContainer = new QWidget(this);
m_leftContainer->setLayout(leftLayout);
m_rightContainer = new QWidget(this);
m_rightContainer->setLayout(rightLayout);
m_leftContainer->installEventFilter(this);
m_rightContainer->installEventFilter(this);
QHBoxLayout* hLayout = new QHBoxLayout;
hLayout->setSpacing(0);
hLayout->setContentsMargins(0, 0, 0, 0);
hLayout->addWidget(m_leftContainer);
hLayout->addWidget(m_leftScrollButton);
hLayout->addWidget(m_scrollArea);
hLayout->addWidget(m_rightContainer);
hLayout->addWidget(m_rightScrollButton);
setLayout(hLayout);
m_scrollArea->viewport()->setAutoFillBackground(false);
@ -1209,16 +1227,6 @@ TabBarScrollWidget::TabBarScrollWidget(QTabBar* tabBar, QWidget* parent)
overFlowChanged(false);
}
void TabBarScrollWidget::addLeftWidget(QWidget* widget, int stretch, Qt::Alignment alignment)
{
m_leftLayout->addWidget(widget, stretch, alignment);
}
void TabBarScrollWidget::addRightWidget(QWidget* widget, int stretch, Qt::Alignment alignment)
{
m_rightLayout->addWidget(widget, stretch, alignment);
}
QTabBar* TabBarScrollWidget::tabBar()
{
return m_tabBar;
@ -1287,8 +1295,6 @@ void TabBarScrollWidget::setUpLayout()
const int height = m_tabBar->height();
setFixedHeight(height);
m_leftContainer->setFixedHeight(height);
m_rightContainer->setFixedHeight(height);
}
void TabBarScrollWidget::scrollBarValueChange()
@ -1299,17 +1305,13 @@ void TabBarScrollWidget::scrollBarValueChange()
void TabBarScrollWidget::overFlowChanged(bool overflowed)
{
m_leftScrollButton->setVisible(overflowed && m_usesScrollButtons);
m_rightScrollButton->setVisible(overflowed && m_usesScrollButtons);
bool showScrollButtons = overflowed && m_usesScrollButtons;
// Workaround for UI issue of buttons on very fast resizing
if (m_rightContainer->isVisible()) {
m_rightContainer->hide();
m_rightContainer->show();
}
if (m_leftContainer->isVisible()) {
m_leftContainer->hide();
m_leftContainer->show();
m_leftScrollButton->setVisible(showScrollButtons);
m_rightScrollButton->setVisible(showScrollButtons);
if (showScrollButtons) {
m_scrollArea->resize(m_scrollArea->size());
}
}
@ -1335,20 +1337,6 @@ void TabBarScrollWidget::scrollStart()
}
}
bool TabBarScrollWidget::eventFilter(QObject* obj, QEvent* ev)
{
if (m_bluredBackground) {
if (ev->type() == QEvent::Paint && (obj == m_leftContainer || obj == m_rightContainer)) {
QPaintEvent* event = static_cast<QPaintEvent*>(ev);
QPainter p(qobject_cast<QWidget*>(obj));
p.setCompositionMode(QPainter::CompositionMode_Clear);
p.fillRect(event->rect(), QColor(0, 0, 0, 0));
}
}
return QWidget::eventFilter(obj, ev);
}
void TabBarScrollWidget::scrollByWheel(QWheelEvent* event)
{
event->accept();
@ -1430,12 +1418,6 @@ int TabBarScrollWidget::tabAt(const QPoint &pos) const
return m_tabBar->tabAt(m_tabBar->mapFromGlobal(mapToGlobal(pos)));
}
void TabBarScrollWidget::setContainersName(const QString &name)
{
m_leftContainer->setObjectName(name);
m_rightContainer->setObjectName(name);
}
void TabBarScrollWidget::enableBluredBackground(bool enable)
{
if (enable != m_bluredBackground) {

View File

@ -130,7 +130,9 @@ public:
void setUsesScrollButtons(bool useButtons);
bool isDragInProgress() const;
void addMainBarWidget(QWidget* widget, Qt::Alignment align, int stretch = 0, Qt::Alignment layoutAlignment = 0);
bool isMainBarOverflowed() const;
void addCornerWidget(QWidget* widget, Qt::Corner corner);
public slots:
void setUpLayout();
@ -176,6 +178,10 @@ private:
void updatePinnedTabBarVisibility();
QHBoxLayout* m_mainLayout;
QHBoxLayout* m_leftLayout;
QHBoxLayout* m_rightLayout;
QWidget* m_leftContainer;
QWidget* m_rightContainer;
TabBarHelper* m_mainTabBar;
TabBarHelper* m_pinnedTabBar;
@ -186,6 +192,7 @@ private:
QString m_closeButtonsToolTip;
bool m_mainBarOverFlowed;
bool m_usesScrollButtons;
bool m_bluredBackground;
};
class QUPZILLA_EXPORT TabBarHelper : public QTabBar
@ -257,9 +264,6 @@ class QUPZILLA_EXPORT TabBarScrollWidget : public QWidget
public:
explicit TabBarScrollWidget(QTabBar* tabBar, QWidget* parent = 0);
void addLeftWidget(QWidget* widget, int stretch = 0, Qt::Alignment alignment = 0);
void addRightWidget(QWidget* widget, int stretch = 0, Qt::Alignment alignment = 0);
QTabBar* tabBar();
QScrollArea* scrollArea();
TabScrollBar* scrollBar();
@ -272,7 +276,6 @@ public:
bool isOverflowed() const;
int tabAt(const QPoint &pos) const;
void setContainersName(const QString &name);
void enableBluredBackground(bool enable);
public slots:
@ -289,18 +292,13 @@ private slots:
void scrollStart();
private:
bool eventFilter(QObject* obj, QEvent* ev);
void mouseMoveEvent(QMouseEvent* event);
QTabBar* m_tabBar;
QScrollArea* m_scrollArea;
TabScrollBar* m_scrollBar;
QHBoxLayout* m_leftLayout;
QHBoxLayout* m_rightLayout;
ToolButton* m_rightScrollButton;
ToolButton* m_leftScrollButton;
QWidget* m_leftContainer;
QWidget* m_rightContainer;
bool m_usesScrollButtons;
bool m_bluredBackground;
int m_totalDeltas;

View File

@ -70,6 +70,7 @@ void TabStackedWidget::setTabBar(ComboTabBar* tb)
connect(m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(showTab(int)));
connect(m_tabBar, SIGNAL(tabMoved(int,int)), this, SLOT(tabWasMoved(int,int)));
connect(m_tabBar, SIGNAL(overFlowChanged(bool)), this, SLOT(setUpLayout()));
if (m_tabBar->tabsClosable()) {
connect(m_tabBar, SIGNAL(tabCloseRequested(int)), this, SIGNAL(tabCloseRequested(int)));

View File

@ -79,7 +79,6 @@ TabBar::TabBar(BrowserWindow* window, TabWidget* tabWidget)
// ComboTabBar features
setUsesScrollButtons(true);
setCloseButtonsToolTip(BrowserWindow::tr("Close Tab"));
connect(this, SIGNAL(overFlowChanged(bool)), this, SLOT(overFlowChange(bool)));
connect(this, SIGNAL(scrollBarValueChanged(int)), this, SLOT(hideTabPreview()));
}
@ -105,21 +104,18 @@ void TabBar::updateVisibilityWithFullscreen(bool visible)
// It is needed to save original geometry, otherwise
// tabbar will get 3px height in fullscreen once it was hidden
ComboTabBar::setVisible(visible);
// Make sure to honor user preference
if (visible) {
visible = !(count() == 1 && m_hideTabBarWithOneTab);
}
ComboTabBar::setVisible(visible);
if (visible) {
setGeometry(m_originalGeometry);
emit showButtons();
}
else {
m_originalGeometry = geometry();
emit hideButtons();
}
m_tabWidget->setUpLayout();
}
void TabBar::setVisible(bool visible)
@ -133,12 +129,8 @@ void TabBar::setVisible(bool visible)
visible = !(count() == 1 && m_hideTabBarWithOneTab);
}
if (visible) {
emit showButtons();
}
else {
if (!visible) {
m_originalGeometry = geometry();
emit hideButtons();
}
hideTabPreview(false);
@ -241,11 +233,7 @@ QSize TabBar::tabSizeHint(int index, bool fast) const
size.setWidth(PINNED_TAB_WIDTH);
}
else {
int availableWidth = mainTabBarWidth();
if (!m_tabWidget->buttonClosedTabs()->isForceHidden()) {
availableWidth -= comboTabBarPixelMetric(ExtraReservedWidth);
}
int availableWidth = mainTabBarWidth() - comboTabBarPixelMetric(ExtraReservedWidth);
if (availableWidth < 0) {
return QSize(-1, -1);
@ -350,10 +338,7 @@ int TabBar::comboTabBarPixelMetric(ComboTabBar::SizeType sizeType) const
return 250;
case ComboTabBar::ExtraReservedWidth:
if (m_tabWidget->buttonClosedTabs()->isVisible()) {
return m_tabWidget->buttonClosedTabs()->width() + m_tabWidget->buttonAddTab()->width();
}
return m_tabWidget->buttonAddTab()->width();
return m_tabWidget->extraReservedWidth();
default:
break;
@ -537,22 +522,6 @@ void TabBar::hideTabPreview(bool delayed)
}
}
void TabBar::overFlowChange(bool overFlowed)
{
if (overFlowed) {
m_tabWidget->buttonAddTab()->setForceHidden(true);
m_tabWidget->buttonClosedTabs()->setForceHidden(true);
m_tabWidget->setUpLayout();
ensureVisible(currentIndex());
}
else {
m_tabWidget->buttonAddTab()->setForceHidden(false);
m_tabWidget->buttonClosedTabs()->setForceHidden(false);
m_tabWidget->showButtons();
m_tabWidget->setUpLayout();
}
}
void TabBar::tabInserted(int index)
{
Q_UNUSED(index)

View File

@ -56,9 +56,6 @@ signals:
void moveAddTabButton(int posX);
void showButtons();
void hideButtons();
private slots:
void currentTabChanged(int index);
@ -78,8 +75,6 @@ private slots:
void showTabPreview(bool delayed = true);
void hideTabPreview(bool delayed = true);
void overFlowChange(bool overFlowed);
private:
inline bool validIndex(int index) const { return index >= 0 && index < count(); }

View File

@ -143,8 +143,6 @@ TabWidget::TabWidget(BrowserWindow* window, QWidget* parent)
connect(m_tabBar, SIGNAL(tabMoved(int,int)), this, SLOT(tabMoved(int,int)));
connect(m_tabBar, SIGNAL(moveAddTabButton(int)), this, SLOT(moveAddTabButton(int)));
connect(m_tabBar, SIGNAL(showButtons()), this, SLOT(showButtons()));
connect(m_tabBar, SIGNAL(hideButtons()), this, SLOT(hideButtons()));
connect(mApp, SIGNAL(settingsReloaded()), this, SLOT(loadSettings()));
@ -198,9 +196,9 @@ TabWidget::TabWidget(BrowserWindow* window, QWidget* parent)
m_buttonClosedTabs2->hide();
connect(m_buttonClosedTabs2, SIGNAL(aboutToShowMenu()), this, SLOT(aboutToShowClosedTabsMenu()));
m_tabBar->addMainBarWidget(m_buttonAddTab2, Qt::AlignRight);
m_tabBar->addMainBarWidget(m_buttonClosedTabs2, Qt::AlignRight);
m_tabBar->addMainBarWidget(m_buttonListTabs, Qt::AlignRight);
m_tabBar->addCornerWidget(m_buttonAddTab2, Qt::TopRightCorner);
m_tabBar->addCornerWidget(m_buttonClosedTabs2, Qt::TopRightCorner);
m_tabBar->addCornerWidget(m_buttonListTabs, Qt::TopRightCorner);
connect(m_tabBar, SIGNAL(overFlowChanged(bool)), this, SLOT(tabBarOverFlowChanged(bool)));
loadSettings();
@ -268,26 +266,17 @@ void TabWidget::updateClosedTabsButton()
m_buttonClosedTabs2->setEnabled(canRestoreTab());
}
void TabWidget::showButtons()
void TabWidget::tabBarOverFlowChanged(bool overflowed)
{
// Show buttons inside tabbar
m_buttonClosedTabs->setVisible(m_showClosedTabsButton);
m_buttonAddTab->show();
}
m_buttonClosedTabs->setVisible(m_showClosedTabsButton && !overflowed);
m_buttonAddTab->setVisible(!overflowed);
void TabWidget::hideButtons()
{
// Hide buttons inside tabbar
m_buttonClosedTabs->hide();
m_buttonAddTab->hide();
}
void TabWidget::tabBarOverFlowChanged(bool overFlowed)
{
// Show buttons displayed outside tabbar (corner widgets)
m_buttonAddTab2->setVisible(overFlowed);
m_buttonClosedTabs2->setVisible(m_showClosedTabsButton && overFlowed);
m_buttonListTabs->setVisible(overFlowed);
m_buttonAddTab2->setVisible(overflowed);
m_buttonClosedTabs2->setVisible(m_showClosedTabsButton && overflowed);
m_buttonListTabs->setVisible(overflowed);
}
void TabWidget::moveAddTabButton(int posX)
@ -687,6 +676,12 @@ int TabWidget::lastTabIndex() const
return m_lastTabIndex;
}
int TabWidget::extraReservedWidth() const
{
int w = m_buttonAddTab->width();
return m_showClosedTabsButton ? w + m_buttonClosedTabs->width() : w;
}
TabBar* TabWidget::getTabBar() const
{
return m_tabBar;

View File

@ -89,6 +89,7 @@ public:
int normalTabsCount() const;
int pinnedTabsCount() const;
int lastTabIndex() const;
int extraReservedWidth() const;
TabBar* getTabBar() const;
ClosedTabsManager* closedTabsManager() const;
@ -121,10 +122,8 @@ public slots:
void clearClosedTabsList();
void moveAddTabButton(int posX);
void showButtons();
void hideButtons();
void tabBarOverFlowChanged(bool overFlowed);
void tabBarOverFlowChanged(bool overflowed);
signals:
void changed();