File indexing completed on 2024-04-28 16:26:34
0001 /************************************************************************************** 0002 Copyright (C) 2004 by Jeroen Wijnhout (Jeroen.Wijnhout@kdemail.net) 0003 (C) 2006 by Thomas Braun (thomas.braun@virtuell-zuhause.de) 0004 (C) 2006-2018 by Michel Ludwig (michel.ludwig@kdemail.net) 0005 **************************************************************************************/ 0006 0007 /*************************************************************************** 0008 * * 0009 * This program is free software; you can redistribute it and/or modify * 0010 * it under the terms of the GNU General Public License as published by * 0011 * the Free Software Foundation; either version 2 of the License, or * 0012 * (at your option) any later version. * 0013 * * 0014 ***************************************************************************/ 0015 0016 #include "sidebar.h" 0017 0018 #include <QHBoxLayout> 0019 #include <QVBoxLayout> 0020 #include <QLayout> 0021 0022 #include "kiledebug.h" 0023 0024 namespace KileWidget { 0025 0026 SideBar::SideBar(QWidget *parent, Qt::Orientation orientation /*= Vertical*/) 0027 : QWidget(parent), 0028 m_orientation(orientation), 0029 m_minimized(true), 0030 m_directionalSize(0) 0031 { 0032 QBoxLayout *layout = Q_NULLPTR, *extraLayout = Q_NULLPTR; 0033 KMultiTabBar::KMultiTabBarPosition tabbarpos = KMultiTabBar::Top; 0034 m_extraWidget = new QWidget(this); 0035 0036 if (orientation == Qt::Horizontal) { 0037 layout = new QVBoxLayout(this); 0038 extraLayout = new QHBoxLayout(m_extraWidget); 0039 tabbarpos = KMultiTabBar::Top; 0040 } 0041 else if(orientation == Qt::Vertical) { 0042 layout = new QHBoxLayout(this); 0043 extraLayout = new QVBoxLayout(m_extraWidget); 0044 tabbarpos = KMultiTabBar::Left; 0045 } 0046 0047 m_tabStack = new QStackedWidget(this); 0048 m_tabStack->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)); 0049 m_tabStack->setVisible(false); 0050 0051 m_tabBar = new KMultiTabBar(tabbarpos, this); 0052 m_tabBar->setStyle(KMultiTabBar::KDEV3ICON); 0053 0054 m_extraWidget->setLayout(extraLayout); 0055 extraLayout->addWidget(m_tabBar); 0056 0057 if(orientation == Qt::Horizontal) { 0058 layout->addWidget(m_tabStack); 0059 layout->addWidget(m_extraWidget); 0060 m_tabBar->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum)); 0061 } 0062 else if(orientation == Qt::Vertical) { 0063 layout->addWidget(m_extraWidget); 0064 layout->addWidget(m_tabStack); 0065 m_tabBar->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding)); 0066 } 0067 0068 layout->setContentsMargins(0, 0, 0, 0); 0069 layout->setSpacing(0); 0070 extraLayout->setContentsMargins(0, 0, 0, 0); 0071 extraLayout->setSpacing(0); 0072 0073 setLayout(layout); 0074 } 0075 0076 SideBar::~SideBar() 0077 { 0078 } 0079 0080 int SideBar::addPage(QWidget *widget, const QIcon &pic, const QString &text /* = QString()*/) 0081 { 0082 int index = m_tabStack->addWidget(widget); 0083 m_tabBar->appendTab(pic, index, text); 0084 connect(m_tabBar->tab(index), SIGNAL(clicked(int)), this, SLOT(tabClicked(int))); 0085 0086 switchToTab(index); 0087 0088 return index; 0089 } 0090 0091 void SideBar::removePage(QWidget *w) 0092 { 0093 int nTabs = m_tabStack->count(); 0094 int index = m_tabStack->indexOf(w); 0095 int currentIndex = currentTab(); 0096 m_tabStack->removeWidget(w); 0097 disconnect(m_tabBar->tab(index), SIGNAL(clicked(int)), this, SLOT(showTab(int))); 0098 m_tabBar->removeTab(index); 0099 if(index == currentIndex && nTabs >= 2) { 0100 switchToTab(findNextShownTab(index)); 0101 } 0102 } 0103 0104 QWidget* SideBar::currentPage() 0105 { 0106 if(isMinimized()) { 0107 return Q_NULLPTR; 0108 } 0109 0110 return m_tabStack->currentWidget(); 0111 } 0112 0113 int SideBar::currentTab() 0114 { 0115 if(m_minimized) { 0116 return -1; 0117 } 0118 0119 return m_tabStack->currentIndex(); 0120 } 0121 0122 bool SideBar::isMinimized() 0123 { 0124 return m_minimized; 0125 } 0126 0127 int SideBar::count() 0128 { 0129 return m_tabStack->count(); 0130 } 0131 0132 void SideBar::shrink() 0133 { 0134 KILE_DEBUG_MAIN; 0135 if(isMinimized()) { 0136 return; 0137 } 0138 0139 int currentIndex = currentTab(); // before changing m_minimized! 0140 m_tabStack->setVisible(false); 0141 m_minimized = true; 0142 0143 if(m_orientation == Qt::Horizontal) { 0144 m_directionalSize = height(); 0145 setFixedHeight(m_tabBar->sizeHint().height()); 0146 } 0147 else if(m_orientation == Qt::Vertical) { 0148 m_directionalSize = width(); 0149 setFixedWidth(m_tabBar->sizeHint().width()); 0150 } 0151 0152 // deselect the currect tab 0153 m_tabBar->setTab(currentIndex, false); 0154 0155 emit visibilityChanged(false); 0156 } 0157 0158 void SideBar::expand() 0159 { 0160 KILE_DEBUG_MAIN; 0161 if(!isMinimized()) { 0162 return; 0163 } 0164 0165 KILE_DEBUG_MAIN << "directional size = " << m_directionalSize; 0166 if(m_orientation == Qt::Horizontal) { 0167 setMinimumHeight(0); 0168 setMaximumHeight(QWIDGETSIZE_MAX); 0169 m_tabStack->resize(m_tabStack->width(), m_directionalSize); 0170 } 0171 else if(m_orientation == Qt::Vertical) { 0172 setMinimumWidth(0); 0173 setMaximumWidth(QWIDGETSIZE_MAX); 0174 m_tabStack->resize(m_directionalSize, m_tabStack->height()); 0175 } 0176 0177 m_tabStack->setVisible(true); 0178 m_minimized = false; 0179 0180 emit visibilityChanged(true); 0181 } 0182 0183 void SideBar::tabClicked(int i) 0184 { 0185 int currentIndex = currentTab(); 0186 0187 if(i == currentIndex && !isMinimized()) { 0188 shrink(); 0189 } 0190 else { 0191 switchToTab(i); 0192 } 0193 } 0194 0195 int SideBar::findNextShownTab(int i) 0196 { 0197 int nTabs = m_tabStack->count(); 0198 if(nTabs <= 0) { 0199 return -1; 0200 } 0201 for(int j = 1; j < nTabs; ++j) { 0202 int index = (i + j) % nTabs; 0203 0204 if(m_tabBar->tab(index)->isVisible()) { 0205 return index; 0206 } 0207 } 0208 return -1; 0209 } 0210 0211 void SideBar::setPageVisible(QWidget *w, bool b) 0212 { 0213 int nTabs = m_tabStack->count(); 0214 int index = m_tabStack->indexOf(w); 0215 int currentIndex = currentTab(); 0216 if(index < 0) { 0217 KILE_WARNING_MAIN << "widget" << w << "not found in side bar!"; 0218 return; 0219 } 0220 0221 KMultiTabBarTab *tab = m_tabBar->tab(index); 0222 tab->setVisible(b); 0223 if(!b && index == currentIndex && nTabs >= 2) { 0224 switchToTab(findNextShownTab(index)); 0225 } 0226 } 0227 0228 void SideBar::showPage(QWidget *widget) 0229 { 0230 KILE_DEBUG_MAIN << "===SideBar::showPage(" << widget << ")"; 0231 int i = m_tabStack->indexOf(widget); 0232 KILE_DEBUG_MAIN << "i is " << i; 0233 if(i >= 0) { 0234 switchToTab(i); 0235 } 0236 } 0237 0238 int SideBar::directionalSize() 0239 { 0240 if(m_minimized) { 0241 return m_directionalSize; 0242 } 0243 0244 if(m_orientation == Qt::Horizontal) { 0245 return m_tabStack->height(); 0246 } 0247 else if(m_orientation == Qt::Vertical) { 0248 return m_tabStack->width(); 0249 } 0250 0251 return 0; 0252 } 0253 0254 void SideBar::setDirectionalSize(int i) 0255 { 0256 KILE_DEBUG_MAIN << "size = " << i; 0257 m_directionalSize = i; 0258 if(m_orientation == Qt::Horizontal) { 0259 m_tabStack->resize(m_tabStack->width(), i); 0260 } 0261 else if(m_orientation == Qt::Vertical) { 0262 m_tabStack->resize(i, m_tabStack->height()); 0263 } 0264 } 0265 0266 void SideBar::addExtraWidget(QWidget *w) 0267 { 0268 m_extraWidget->layout()->addWidget(w); 0269 } 0270 0271 void SideBar::switchToTab(int id) 0272 { 0273 KILE_DEBUG_MAIN << "id = " << id; 0274 int nTabs = m_tabStack->count(); 0275 int currentIndex = currentTab(); 0276 0277 if(id >= nTabs || id < 0 || m_tabBar->tab(id)->isHidden()) { 0278 shrink(); 0279 return; 0280 } 0281 // currentIndex == id is allowed if we are expanding, for example 0282 if(currentIndex >= 0) { 0283 m_tabBar->setTab(currentIndex, false); 0284 } 0285 m_tabBar->setTab(id, true); 0286 0287 m_tabStack->setCurrentIndex(id); 0288 expand(); 0289 } 0290 0291 BottomBar::BottomBar(QWidget *parent) : SideBar(parent, Qt::Horizontal) 0292 { 0293 } 0294 0295 }