From 8fe6d2ec950ed1ebdedd776bab125dff90e5a20f Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sun, 30 Apr 2017 10:30:47 +0200 Subject: Add support for rotating the tabs using the scroll-wheel. --- plugingui/stackedwidget.cc | 45 +++++++++++++++++++++++++++++++++++++++++++++ plugingui/stackedwidget.h | 18 +++++++++++++----- plugingui/tabbutton.cc | 5 +++++ plugingui/tabbutton.h | 4 ++++ plugingui/tabwidget.cc | 19 +++++++++++++++++++ plugingui/tabwidget.h | 2 ++ 6 files changed, 88 insertions(+), 5 deletions(-) diff --git a/plugingui/stackedwidget.cc b/plugingui/stackedwidget.cc index 069e80b..05f8f3c 100644 --- a/plugingui/stackedwidget.cc +++ b/plugingui/stackedwidget.cc @@ -93,6 +93,51 @@ void StackedWidget::setCurrentWidget(Widget *widget) currentChanged(currentWidget); } +Widget* StackedWidget::getWidgetAfter(Widget* widget) +{ + bool found_it{false}; + + for(auto w : widgets) + { + if(found_it) + { + return w; + } + + if(w == widget) + { + found_it = true; + } + } + + if(found_it) + { + // widget was the last in the list. + return nullptr; + } + + // The Widget pointed to by 'widget' was not in the list... + return nullptr; +} + +Widget* StackedWidget::getWidgetBefore(Widget* widget) +{ + Widget* last{nullptr}; + + for(auto w : widgets) + { + if(w == widget) + { + return last; + } + + last = w; + } + + // The Widget pointed to by 'widget' was not in the list... + return nullptr; +} + void StackedWidget::sizeChanged(int width, int height) { // Propagate size change to child: diff --git a/plugingui/stackedwidget.h b/plugingui/stackedwidget.h index 13d764b..24213f7 100644 --- a/plugingui/stackedwidget.h +++ b/plugingui/stackedwidget.h @@ -42,21 +42,29 @@ class StackedWidget : public Widget { public: - StackedWidget(Widget *parent); + StackedWidget(Widget* parent); ~StackedWidget(); //! Add a widget to the stack. - void addWidget(Widget *widget); + void addWidget(Widget* widget); //! Remove a widget from the stack. - void removeWidget(Widget *widget); + void removeWidget(Widget* widget); //! Get currently visible widget. - Widget *getCurrentWidget() const; + Widget* getCurrentWidget() const; //! Show widget. Hide all the others. //! If widget is not in the stack nothing happens. - void setCurrentWidget(Widget *widget); + void setCurrentWidget(Widget* widget); + + //! Returns a pointer to the Widget after the one referenced by 'widget' or + //! nullptr if 'widget' is the last in the list. + Widget* getWidgetAfter(Widget* widget); + + //! Returns a pointer to the Widget beforer the one referenced by 'widget' or + //! nullptr if 'widget' is the first in the list. + Widget* getWidgetBefore(Widget* widget); //! Reports whn a new widget is shown. Notifier currentChanged; diff --git a/plugingui/tabbutton.cc b/plugingui/tabbutton.cc index f46daa4..845dedb 100644 --- a/plugingui/tabbutton.cc +++ b/plugingui/tabbutton.cc @@ -106,6 +106,11 @@ void TabButton::repaintEvent(RepaintEvent* e) p.drawText(x, y, font, text, true); } +void TabButton::scrollEvent(ScrollEvent* scroll_event) +{ + scrollNotifier(scroll_event->delta); +} + void TabButton::clickHandler() { switchTabNotifier(tab_widget); diff --git a/plugingui/tabbutton.h b/plugingui/tabbutton.h index 1c4d84b..29e6e05 100644 --- a/plugingui/tabbutton.h +++ b/plugingui/tabbutton.h @@ -35,6 +35,8 @@ namespace GUI { +class ScrollEvent; + class TabButton : public ButtonBase { @@ -48,10 +50,12 @@ public: void setActive(bool active); Notifier switchTabNotifier; + Notifier scrollNotifier; // float delta protected: // From Widget: virtual void repaintEvent(RepaintEvent* e) override; + virtual void scrollEvent(ScrollEvent* scroll_event) override; private: void clickHandler(); diff --git a/plugingui/tabwidget.cc b/plugingui/tabwidget.cc index 06e7425..dde2137 100644 --- a/plugingui/tabwidget.cc +++ b/plugingui/tabwidget.cc @@ -46,6 +46,7 @@ void TabWidget::addTab(const std::string& title, Widget* widget) button.setText(title); stack.addWidget(widget); CONNECT(&button, switchTabNotifier, this, &TabWidget::switchTab); + CONNECT(&button, scrollNotifier, this, &TabWidget::rotateTab); sizeChanged(width(), height()); } @@ -54,6 +55,24 @@ std::size_t TabWidget::getBarHeight() const return topbar.height(); } +void TabWidget::rotateTab(float delta) +{ + Widget* widget{nullptr}; + if(delta > 0.0f) + { + widget = stack.getWidgetAfter(stack.getCurrentWidget()); + } + else + { + widget = stack.getWidgetBefore(stack.getCurrentWidget()); + } + + if(widget) + { + switchTab(widget); + } +} + void TabWidget::switchTab(Widget* tabWidget) { stack.setCurrentWidget(tabWidget); diff --git a/plugingui/tabwidget.h b/plugingui/tabwidget.h index ca96dc2..3f0e041 100644 --- a/plugingui/tabwidget.h +++ b/plugingui/tabwidget.h @@ -52,6 +52,8 @@ private: void sizeChanged(int width, int height); private: + //! Switch to the next tab if delta is > 0 or previous tab if delta is <= 0. + void rotateTab(float delta); void switchTab(Widget* tabWidget); void setActiveButtons(Widget* current_widget); -- cgit v1.2.3