From a3b83b7f81192bdbeabb1ba8a7eed57beb4d4233 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 7 Dec 2016 22:53:02 +0800 Subject: [PATCH] dynamically show or hide left corner tab list widget Signed-off-by: Le Tan --- src/veditwindow.cpp | 32 ++++++++++++++++++++++++++++++++ src/veditwindow.h | 4 ++++ 2 files changed, 36 insertions(+) diff --git a/src/veditwindow.cpp b/src/veditwindow.cpp index 88f286d2..841318bf 100644 --- a/src/veditwindow.cpp +++ b/src/veditwindow.cpp @@ -18,6 +18,8 @@ VEditWindow::VEditWindow(VNote *vnote, QWidget *parent) setMovable(true); setContextMenuPolicy(Qt::CustomContextMenu); + tabBar()->installEventFilter(this); + connect(this, &VEditWindow::tabCloseRequested, this, &VEditWindow::handleTabCloseRequest); connect(this, &VEditWindow::tabBarClicked, @@ -520,3 +522,33 @@ void VEditWindow::updateNotebookInfo(const VNotebook *p_notebook) } } } + +bool VEditWindow::eventFilter(QObject *watched, QEvent *event) +{ + if (event->type() == QEvent::Resize) { + setLeftCornerWidgetVisible(scrollerVisible()); + } + return QTabWidget::eventFilter(watched, event); +} + +bool VEditWindow::scrollerVisible() const +{ + QTabBar *bar = tabBar(); + int barWidth = bar->width(); + int nrTab = count(); + int tabsWidth = 0; + for (int i = 0; i < nrTab; ++i) { + tabsWidth += bar->tabRect(i).width(); + } + return tabsWidth > barWidth; +} + +void VEditWindow::setLeftCornerWidgetVisible(bool p_visible) +{ + if (p_visible) { + setCornerWidget(leftBtn, Qt::TopLeftCorner); + leftBtn->setVisible(true); + } else { + setCornerWidget(NULL, Qt::TopLeftCorner); + } +} diff --git a/src/veditwindow.h b/src/veditwindow.h index 4479caa4..ccedfa32 100644 --- a/src/veditwindow.h +++ b/src/veditwindow.h @@ -42,6 +42,7 @@ public: protected: void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE; signals: void tabStatusChanged(const VFile *p_file, bool p_editMode); @@ -77,6 +78,9 @@ private: inline QString generateTooltip(const VFile *p_file) const; inline QString generateTabText(const QString &p_name, bool p_modified) const; bool canRemoveSplit(); + // If the scroller of the tabBar() is visible. + bool scrollerVisible() const; + void setLeftCornerWidgetVisible(bool p_visible); VNote *vnote; // Button in the right corner