dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
mytabbedstackwidget.cpp
Go to the documentation of this file.
1 
14 /*
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  * for more details.
24  *
25  * You should have received a copy of the GNU General Public License along
26  * with this program; if not, see <http://www.gnu.org/licenses/>
27  *
28  * Additional note on redistribution: The copyright and license notices above
29  * must be maintained in each individual source file that is a derivative work
30  * of this source file; otherwise redistribution is prohibited.
31  */
32 
33 #include "mytabbedstackwidget.h"
34 #include <QVBoxLayout>
35 #include <QHBoxLayout>
36 #include <QLabel>
37 #include <QtCore/QDebug>
38 
39 MyTabbedStackWidget::MyTabbedStackWidget(QWidget *parent, bool isVertical, bool iconAbove)
40  : QWidget(parent),
41  m_vertical(isVertical),
42  m_iconAbove(iconAbove)
43 {
44  m_listWidget = new QListWidget(this);
45  m_stackWidget = new QStackedWidget();
46  m_stackWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
47 
48  QBoxLayout *toplevelLayout;
49  if (m_vertical) {
50  toplevelLayout = new QHBoxLayout;
51  toplevelLayout->addWidget(m_listWidget);
52  toplevelLayout->addWidget(m_stackWidget);
53  m_listWidget->setFlow(QListView::TopToBottom);
54  m_listWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
55  m_listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
56  } else {
57  toplevelLayout = new QVBoxLayout;
58  toplevelLayout->addWidget(m_stackWidget);
59  toplevelLayout->addWidget(m_listWidget);
60  m_listWidget->setFlow(QListView::LeftToRight);
61  m_listWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
62  m_listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63  }
64 
65  if (m_iconAbove && m_vertical) {
66  m_listWidget->setFixedWidth(91); // this should be computed instead
67  m_listWidget->setWrapping(false);
68  // make the scrollbar small and similar to OS X so it doesn't overlay the icons
69  m_listWidget->setStyleSheet(QString("QScrollBar:vertical { width: 6px; border-width: 0px; background: none; margin: 2px 0px 2px 0px; }"
70  "QScrollBar::handle:vertical { background: #5c5c5c; border-radius: 3px; }"
71  "QScrollBar::add-line:vertical { width: 0; height: 0; }"
72  "QScrollBar::sub-line:vertical { width: 0; height: 0 }"));
73  }
74 
75  toplevelLayout->setSpacing(0);
76  toplevelLayout->setContentsMargins(0, 0, 0, 0);
77  m_listWidget->setContentsMargins(0, 0, 4, 0);
78  m_listWidget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
79  m_listWidget->setSpacing(4);
80  m_listWidget->setViewMode(QListView::IconMode);
81  m_stackWidget->setContentsMargins(0, 0, 0, 0);
82  setLayout(toplevelLayout);
83 
84  connect(m_listWidget, SIGNAL(currentRowChanged(int)), this, SLOT(showWidget(int)),Qt::QueuedConnection);
85 }
86 
87 void MyTabbedStackWidget::insertTab(const int index, QWidget *tab, const QIcon &icon, const QString &label)
88 {
89  tab->setContentsMargins(0, 0, 0, 0);
90  m_stackWidget->insertWidget(index, tab);
91  QListWidgetItem *item = new QListWidgetItem(icon, label);
92  item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
93  item->setTextAlignment(Qt::AlignHCenter | Qt::AlignBottom);
94  item->setToolTip(label);
95 
96  if (m_vertical) {
97  item->setSizeHint(QSize(85, 82));
98  }
99 
100  m_listWidget->insertItem(index, item);
101 }
102 
104 {
105  QWidget * wid=m_stackWidget->widget(index);
106  m_stackWidget->removeWidget(wid);
107  delete wid;
108  QListWidgetItem *item = m_listWidget->item(index);
109  m_listWidget->removeItemWidget(item);
110  delete item;
111 }
112 
114 {
115  return m_listWidget->currentRow();
116 }
117 
119 {
120  m_listWidget->setCurrentRow(index);
121 }
122 
123 void MyTabbedStackWidget::showWidget(int index)
124 {
125  if(m_stackWidget->currentIndex()==index)
126  return;
127  bool proceed=false;
128  emit currentAboutToShow(index,&proceed);
129  if(proceed)
130  {
131  m_stackWidget->setCurrentIndex(index);
132  emit currentChanged(index);
133  }
134  else
135  {
136  m_listWidget->setCurrentRow(m_stackWidget->currentIndex(),QItemSelectionModel::ClearAndSelect);
137  }
138 }
139 
140 void MyTabbedStackWidget::insertCornerWidget(int index, QWidget *widget)
141 {
142  Q_UNUSED(index);
143 
144  widget->hide();
145 }
146 
147 void MyTabbedStackWidget::setHidden(int index, bool hide)
148 {
149  QListWidgetItem *i = m_listWidget->item(index);
150 
151  if (!i) {
152  return;
153  }
154 
155  i->setHidden(hide);
156 }
void currentChanged(int index)
MyTabbedStackWidget(QWidget *parent=nullptr, bool isVertical=false, bool iconAbove=true)
void currentAboutToShow(int index, bool *proceed)
for i
Definition: OPPlots.m:140
void insertCornerWidget(int index, QWidget *widget)
void insertTab(int index, QWidget *tab, const QIcon &icon, const QString &label)
void setCurrentIndex(int index)
void setHidden(int index, bool hide)