diff --git a/src/widgets/sidebar.cpp b/src/widgets/sidebar.cpp index c6b7fbf6..13e99e5d 100644 --- a/src/widgets/sidebar.cpp +++ b/src/widgets/sidebar.cpp @@ -1,292 +1,295 @@ /************************************************************************************** Copyright (C) 2004 by Jeroen Wijnhout (Jeroen.Wijnhout@kdemail.net) (C) 2006 by Thomas Braun (thomas.braun@virtuell-zuhause.de) - (C) 2006-2011 by Michel Ludwig (michel.ludwig@kdemail.net) + (C) 2006-2018 by Michel Ludwig (michel.ludwig@kdemail.net) **************************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "sidebar.h" #include #include #include #include "kiledebug.h" namespace KileWidget { -SideBar::SideBar(QWidget *parent, Qt::Orientation orientation /*= Vertical*/) : - QWidget(parent), - m_orientation(orientation), - m_minimized(true), - m_directionalSize(0) +SideBar::SideBar(QWidget *parent, Qt::Orientation orientation /*= Vertical*/) + : QWidget(parent), + m_orientation(orientation), + m_minimized(true), + m_directionalSize(0) { - QBoxLayout *layout = Q_NULLPTR, *extraLayout = Q_NULLPTR; - KMultiTabBar::KMultiTabBarPosition tabbarpos = KMultiTabBar::Top; - m_extraWidget = new QWidget(this); - - if (orientation == Qt::Horizontal) { - layout = new QVBoxLayout(this); - extraLayout = new QHBoxLayout(m_extraWidget); - tabbarpos = KMultiTabBar::Top; - } - else if(orientation == Qt::Vertical) { - layout = new QHBoxLayout(this); - extraLayout = new QVBoxLayout(m_extraWidget); - tabbarpos = KMultiTabBar::Left; - } - - m_tabStack = new QStackedWidget(this); - m_tabStack->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)); - m_tabStack->setVisible(false); - - m_tabBar = new KMultiTabBar(tabbarpos, this); - m_tabBar->setStyle(KMultiTabBar::KDEV3ICON); - - m_extraWidget->setLayout(extraLayout); - extraLayout->addWidget(m_tabBar); - - if(orientation == Qt::Horizontal) { - layout->addWidget(m_tabStack); - layout->addWidget(m_extraWidget); - m_tabBar->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum)); - } - else if(orientation == Qt::Vertical) { - layout->addWidget(m_extraWidget); - layout->addWidget(m_tabStack); - m_tabBar->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding)); - } - - layout->setMargin(0); - layout->setSpacing(0); - extraLayout->setMargin(0); - extraLayout->setSpacing(0); - - setLayout(layout); + QBoxLayout *layout = Q_NULLPTR, *extraLayout = Q_NULLPTR; + KMultiTabBar::KMultiTabBarPosition tabbarpos = KMultiTabBar::Top; + m_extraWidget = new QWidget(this); + + if (orientation == Qt::Horizontal) { + layout = new QVBoxLayout(this); + extraLayout = new QHBoxLayout(m_extraWidget); + tabbarpos = KMultiTabBar::Top; + } + else if(orientation == Qt::Vertical) { + layout = new QHBoxLayout(this); + extraLayout = new QVBoxLayout(m_extraWidget); + tabbarpos = KMultiTabBar::Left; + } + + m_tabStack = new QStackedWidget(this); + m_tabStack->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)); + m_tabStack->setVisible(false); + + m_tabBar = new KMultiTabBar(tabbarpos, this); + m_tabBar->setStyle(KMultiTabBar::KDEV3ICON); + + m_extraWidget->setLayout(extraLayout); + extraLayout->addWidget(m_tabBar); + + if(orientation == Qt::Horizontal) { + layout->addWidget(m_tabStack); + layout->addWidget(m_extraWidget); + m_tabBar->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum)); + } + else if(orientation == Qt::Vertical) { + layout->addWidget(m_extraWidget); + layout->addWidget(m_tabStack); + m_tabBar->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding)); + } + + layout->setMargin(0); + layout->setSpacing(0); + extraLayout->setMargin(0); + extraLayout->setSpacing(0); + + setLayout(layout); } SideBar::~SideBar() { } int SideBar::addPage(QWidget *widget, const QIcon &pic, const QString &text /* = QString()*/) { - int index = m_tabStack->addWidget(widget); - m_tabBar->appendTab(pic, index, text); - connect(m_tabBar->tab(index), SIGNAL(clicked(int)), this, SLOT(tabClicked(int))); + int index = m_tabStack->addWidget(widget); + m_tabBar->appendTab(pic, index, text); + connect(m_tabBar->tab(index), SIGNAL(clicked(int)), this, SLOT(tabClicked(int))); - switchToTab(index); + switchToTab(index); - return index; + return index; } void SideBar::removePage(QWidget *w) { - int nTabs = m_tabStack->count(); - int index = m_tabStack->indexOf(w); - int currentIndex = currentTab(); - m_tabStack->removeWidget(w); - disconnect(m_tabBar->tab(index), SIGNAL(clicked(int)), this, SLOT(showTab(int))); - m_tabBar->removeTab(index); - if(index == currentIndex && nTabs >= 2) { - switchToTab(findNextShownTab(index)); - } + int nTabs = m_tabStack->count(); + int index = m_tabStack->indexOf(w); + int currentIndex = currentTab(); + m_tabStack->removeWidget(w); + disconnect(m_tabBar->tab(index), SIGNAL(clicked(int)), this, SLOT(showTab(int))); + m_tabBar->removeTab(index); + if(index == currentIndex && nTabs >= 2) { + switchToTab(findNextShownTab(index)); + } } QWidget* SideBar::currentPage() { - if(isMinimized()) { - return Q_NULLPTR; - } + if(isMinimized()) { + return Q_NULLPTR; + } - return m_tabStack->currentWidget(); + return m_tabStack->currentWidget(); } int SideBar::currentTab() { - if(m_minimized) { - return -1; - } + if(m_minimized) { + return -1; + } - return m_tabStack->currentIndex(); + return m_tabStack->currentIndex(); } bool SideBar::isMinimized() { - return m_minimized; + return m_minimized; } int SideBar::count() { - return m_tabStack->count(); + return m_tabStack->count(); } void SideBar::shrink() { - KILE_DEBUG_MAIN; - if(isMinimized()) { - return; - } - - int currentIndex = currentTab(); // before changing m_minimized! - m_tabStack->setVisible(false); - m_minimized = true; - - if(m_orientation == Qt::Horizontal) { - m_directionalSize = height(); - setFixedHeight(m_tabBar->sizeHint().height()); - } - else if(m_orientation == Qt::Vertical) { - m_directionalSize = width(); - setFixedWidth(m_tabBar->sizeHint().width()); - } - - // deselect the currect tab - m_tabBar->setTab(currentIndex, false); - - emit visibilityChanged(false); + KILE_DEBUG_MAIN; + if(isMinimized()) { + return; + } + + int currentIndex = currentTab(); // before changing m_minimized! + m_tabStack->setVisible(false); + m_minimized = true; + + if(m_orientation == Qt::Horizontal) { + m_directionalSize = height(); + setFixedHeight(m_tabBar->sizeHint().height()); + } + else if(m_orientation == Qt::Vertical) { + m_directionalSize = width(); + setFixedWidth(m_tabBar->sizeHint().width()); + } + + // deselect the currect tab + m_tabBar->setTab(currentIndex, false); + + emit visibilityChanged(false); } void SideBar::expand() { - KILE_DEBUG_MAIN; - if(!isMinimized()) { - return; - } - - KILE_DEBUG_MAIN << "directional size = " << m_directionalSize; - if(m_orientation == Qt::Horizontal) { - setMinimumHeight(0); - setMaximumHeight(QWIDGETSIZE_MAX); - m_tabStack->resize(m_tabStack->width(), m_directionalSize); - } - else if(m_orientation == Qt::Vertical) { - setMinimumWidth(0); - setMaximumWidth(QWIDGETSIZE_MAX); - m_tabStack->resize(m_directionalSize, m_tabStack->height()); - } - - m_tabStack->setVisible(true); - m_minimized = false; - - emit visibilityChanged(true); + KILE_DEBUG_MAIN; + if(!isMinimized()) { + return; + } + + KILE_DEBUG_MAIN << "directional size = " << m_directionalSize; + if(m_orientation == Qt::Horizontal) { + setMinimumHeight(0); + setMaximumHeight(QWIDGETSIZE_MAX); + m_tabStack->resize(m_tabStack->width(), m_directionalSize); + } + else if(m_orientation == Qt::Vertical) { + setMinimumWidth(0); + setMaximumWidth(QWIDGETSIZE_MAX); + m_tabStack->resize(m_directionalSize, m_tabStack->height()); + } + + m_tabStack->setVisible(true); + m_minimized = false; + + emit visibilityChanged(true); } void SideBar::tabClicked(int i) { - int currentIndex = currentTab(); - - if(i == currentIndex && !isMinimized()) { - shrink(); - } - else { - switchToTab(i); - } + int currentIndex = currentTab(); + + if(i == currentIndex && !isMinimized()) { + shrink(); + } + else { + switchToTab(i); + } } int SideBar::findNextShownTab(int i) { - int nTabs = m_tabStack->count(); - if(nTabs <= 0) { - return -1; - } - for(int j = 1; j < nTabs; ++j) { - int index = (i + j) % nTabs; - - if(m_tabBar->tab(index)->isVisible()) { - return index; - } - } - return -1; + int nTabs = m_tabStack->count(); + if(nTabs <= 0) { + return -1; + } + for(int j = 1; j < nTabs; ++j) { + int index = (i + j) % nTabs; + + if(m_tabBar->tab(index)->isVisible()) { + return index; + } + } + return -1; } void SideBar::setPageVisible(QWidget *w, bool b) { - int nTabs = m_tabStack->count(); - int index = m_tabStack->indexOf(w); - int currentIndex = currentTab(); - - KMultiTabBarTab *tab = m_tabBar->tab(index); - tab->setVisible(b); - if(!b && index == currentIndex && nTabs >= 2) { - switchToTab(findNextShownTab(index)); - } + int nTabs = m_tabStack->count(); + int index = m_tabStack->indexOf(w); + int currentIndex = currentTab(); + if(index < 0) { + KILE_WARNING_MAIN << "widget" << w << "not found in side bar!"; + return; + } + + KMultiTabBarTab *tab = m_tabBar->tab(index); + tab->setVisible(b); + if(!b && index == currentIndex && nTabs >= 2) { + switchToTab(findNextShownTab(index)); + } } void SideBar::showPage(QWidget *widget) { - KILE_DEBUG_MAIN << "===SideBar::showPage(" << widget << ")"; - int i = m_tabStack->indexOf(widget); - KILE_DEBUG_MAIN << "i is " << i; - if(i >= 0) { - switchToTab(i); - } + KILE_DEBUG_MAIN << "===SideBar::showPage(" << widget << ")"; + int i = m_tabStack->indexOf(widget); + KILE_DEBUG_MAIN << "i is " << i; + if(i >= 0) { + switchToTab(i); + } } int SideBar::directionalSize() { - if(m_minimized) { - return m_directionalSize; - } - - if(m_orientation == Qt::Horizontal) { - return m_tabStack->height(); - } - else if(m_orientation == Qt::Vertical) { - return m_tabStack->width(); - } - - return 0; + if(m_minimized) { + return m_directionalSize; + } + + if(m_orientation == Qt::Horizontal) { + return m_tabStack->height(); + } + else if(m_orientation == Qt::Vertical) { + return m_tabStack->width(); + } + + return 0; } void SideBar::setDirectionalSize(int i) { - KILE_DEBUG_MAIN << "size = " << i; - m_directionalSize = i; - if(m_orientation == Qt::Horizontal) { - m_tabStack->resize(m_tabStack->width(), i); - } - else if(m_orientation == Qt::Vertical) { - m_tabStack->resize(i, m_tabStack->height()); - } + KILE_DEBUG_MAIN << "size = " << i; + m_directionalSize = i; + if(m_orientation == Qt::Horizontal) { + m_tabStack->resize(m_tabStack->width(), i); + } + else if(m_orientation == Qt::Vertical) { + m_tabStack->resize(i, m_tabStack->height()); + } } void SideBar::addExtraWidget(QWidget *w) { - m_extraWidget->layout()->addWidget(w); + m_extraWidget->layout()->addWidget(w); } void SideBar::switchToTab(int id) { - KILE_DEBUG_MAIN << "id = " << id; - int nTabs = m_tabStack->count(); - int currentIndex = currentTab(); - - if(id >= nTabs || id < 0 || m_tabBar->tab(id)->isHidden()) { - shrink(); - return; - } - // currentIndex == id is allowed if we are expanding, for example - if(currentIndex >= 0) { - m_tabBar->setTab(currentIndex, false); - } - m_tabBar->setTab(id, true); - - m_tabStack->setCurrentIndex(id); - expand(); + KILE_DEBUG_MAIN << "id = " << id; + int nTabs = m_tabStack->count(); + int currentIndex = currentTab(); + + if(id >= nTabs || id < 0 || m_tabBar->tab(id)->isHidden()) { + shrink(); + return; + } + // currentIndex == id is allowed if we are expanding, for example + if(currentIndex >= 0) { + m_tabBar->setTab(currentIndex, false); + } + m_tabBar->setTab(id, true); + + m_tabStack->setCurrentIndex(id); + expand(); } BottomBar::BottomBar(QWidget *parent) : SideBar(parent, Qt::Horizontal) { } } - diff --git a/src/widgets/sidebar.h b/src/widgets/sidebar.h index 66b64f23..3e4fe16a 100644 --- a/src/widgets/sidebar.h +++ b/src/widgets/sidebar.h @@ -1,115 +1,115 @@ /************************************************************************************** Copyright (C) 2004 by Jeroen Wijnhout (Jeroen.Wijnhout@kdemail.net) (C) 2006 by Thomas Braun (thomas.braun@virtuell-zuhause.de) - (C) 2006-2011 by Michel Ludwig (michel.ludwig@kdemail.net) + (C) 2006-2018 by Michel Ludwig (michel.ludwig@kdemail.net) **************************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #ifndef SIDEBAR_H #define SIDEBAR_H #include #include #include #include namespace KileWidget { class SideBar : public QWidget { - Q_OBJECT + Q_OBJECT public: - explicit SideBar(QWidget *parent = Q_NULLPTR, Qt::Orientation orientation = Qt::Vertical); - virtual ~SideBar(); + explicit SideBar(QWidget *parent = Q_NULLPTR, Qt::Orientation orientation = Qt::Vertical); + virtual ~SideBar(); - int addPage(QWidget *tab, const QIcon &pic, const QString &text = QString()); - void removePage(QWidget *w); + int addPage(QWidget *tab, const QIcon &pic, const QString &text = QString()); + void removePage(QWidget *w); - QWidget* currentPage(); + QWidget* currentPage(); - /** - * Returns the index of the widget which is currently shown, or -1 if the side bar is minimized. - **/ - int currentTab(); + /** + * Returns the index of the widget which is currently shown, or -1 if the side bar is minimized. + **/ + int currentTab(); - bool isMinimized(); + bool isMinimized(); - int count(); + int count(); - /** - * Shows or hides the tab connected to the widget "w". If the tab to be hidden is - * currently selected, the next tab will be shown (cyclically). - * - * @param b set to "true" to show the tab connected to the widget "w", "false" to - * hide it - **/ - void setPageVisible(QWidget *w, bool b); + /** + * Shows or hides the tab connected to the widget "w". If the tab to be hidden is + * currently selected, the next tab will be shown (cyclically). + * + * @param b set to "true" to show the tab connected to the widget "w", "false" to + * hide it + **/ + void setPageVisible(QWidget *w, bool b); - /** - * Returns the side bar's height if its orientation is vertical, its width otherwise. - **/ - int directionalSize(); + /** + * Returns the side bar's height if its orientation is vertical, its width otherwise. + **/ + int directionalSize(); - /** - * Sets the side bar's height if its orientation is vertical, its width otherwise. - **/ - void setDirectionalSize(int i); + /** + * Sets the side bar's height if its orientation is vertical, its width otherwise. + **/ + void setDirectionalSize(int i); - /** - * Add a widget to the (right or bottom) of the tab bar, which is not connected to any tabs. - **/ - void addExtraWidget(QWidget *w); + /** + * Add a widget to the (right or bottom) of the tab bar, which is not connected to any tabs. + **/ + void addExtraWidget(QWidget *w); Q_SIGNALS: - void visibilityChanged(bool b); + void visibilityChanged(bool b); public Q_SLOTS: - void showPage(QWidget *w); + void showPage(QWidget *w); - /** - * Shows the widget with index 'id'. If 'id' is not a valid index, the side bar will be - * minimized. - **/ - void switchToTab(int id); + /** + * Shows the widget with index 'id'. If 'id' is not a valid index, the side bar will be + * minimized. + **/ + void switchToTab(int id); - void shrink(); + void shrink(); private: - int findNextShownTab(int i); + int findNextShownTab(int i); protected Q_SLOTS: - void expand(); - void tabClicked(int i); + void expand(); + void tabClicked(int i); protected: - Qt::Orientation m_orientation; - bool m_minimized; /* using m_tabStack->isVisible is not enough */ - int m_directionalSize; /* directional size in the unminized state */ - int m_currentTab; - QStackedWidget *m_tabStack; - KMultiTabBar *m_tabBar; - QWidget *m_extraWidget; + Qt::Orientation m_orientation; + bool m_minimized; /* using m_tabStack->isVisible is not enough */ + int m_directionalSize; /* directional size in the unminized state */ + int m_currentTab; + QStackedWidget *m_tabStack; + KMultiTabBar *m_tabBar; + QWidget *m_extraWidget; }; class BottomBar : public SideBar { - Q_OBJECT + Q_OBJECT public: - BottomBar(QWidget *parent = Q_NULLPTR); + BottomBar(QWidget *parent = Q_NULLPTR); }; } #endif