2 ******************************************************************************
4 * @file mytabbedstackwidget.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "mytabbedstackwidget.h"
29 #include <QVBoxLayout>
30 #include <QHBoxLayout>
32 #include <QtCore/QDebug>
35 MyTabbedStackWidget::MyTabbedStackWidget(QWidget
*parent
, bool isVertical
, bool iconAbove
)
37 m_vertical(isVertical
),
38 m_iconAbove(iconAbove
)
40 m_listWidget
= new QListWidget();
41 m_listWidget
->setObjectName("list");
42 m_stackWidget
= new QStackedWidget();
44 QBoxLayout
*toplevelLayout
;
46 toplevelLayout
= new QHBoxLayout();
47 toplevelLayout
->addWidget(m_listWidget
);
48 toplevelLayout
->addWidget(m_stackWidget
);
49 m_listWidget
->setFlow(QListView::TopToBottom
);
50 m_listWidget
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Expanding
);
51 m_listWidget
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
53 toplevelLayout
= new QVBoxLayout();
54 toplevelLayout
->addWidget(m_stackWidget
);
55 toplevelLayout
->addWidget(m_listWidget
);
56 m_listWidget
->setFlow(QListView::LeftToRight
);
57 m_listWidget
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
58 m_listWidget
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
61 if (m_iconAbove
&& m_vertical
) {
62 m_listWidget
->setFixedWidth(LIST_VIEW_WIDTH
); // this should be computed instead
63 m_listWidget
->setWrapping(false);
66 m_listWidget
->setContentsMargins(0, 0, 0, 0);
67 m_listWidget
->setSpacing(0);
68 m_listWidget
->setViewMode(QListView::IconMode
);
69 m_listWidget
->setMovement(QListView::Static
);
70 m_listWidget
->setUniformItemSizes(true);
71 m_listWidget
->setStyleSheet("#list {border: 0px; margin-left: 9px; margin-top: 9px; background-color: transparent; }");
73 m_stackWidget
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Expanding
);
74 m_stackWidget
->setContentsMargins(0, 0, 0, 0);
76 toplevelLayout
->setSpacing(0);
77 toplevelLayout
->setContentsMargins(0, 0, 0, 0);
78 setLayout(toplevelLayout
);
80 connect(m_listWidget
, SIGNAL(currentRowChanged(int)), this, SLOT(showWidget(int)), Qt::QueuedConnection
);
83 void MyTabbedStackWidget::insertTab(const int index
, QWidget
*tab
, const QIcon
&icon
, const QString
&label
)
85 // create and insert item
86 QListWidgetItem
*item
= new QListWidgetItem(icon
, label
);
88 item
->setFlags(Qt::ItemIsEnabled
| Qt::ItemIsSelectable
);
89 item
->setTextAlignment(Qt::AlignHCenter
| Qt::AlignBottom
);
90 item
->setToolTip(label
);
91 m_listWidget
->insertItem(index
, item
);
93 // setup and insert widget
94 tab
->setContentsMargins(0, 0, 0, 0);
95 m_stackWidget
->insertWidget(index
, tab
);
98 void MyTabbedStackWidget::replaceTab(int index
, QWidget
*tab
)
100 QWidget
*wid
= m_stackWidget
->widget(index
);
102 // setup and insert new widget
103 tab
->setContentsMargins(0, 0, 0, 0);
104 m_stackWidget
->insertWidget(index
, tab
);
105 // check special case when replacing currenlty selected tab
106 if (index
== currentIndex()) {
107 // currently selected tab is being replaced so select the new tab before removing the old one
108 m_stackWidget
->setCurrentWidget(tab
);
110 // remove and delete old widget
111 m_stackWidget
->removeWidget(wid
);
115 void MyTabbedStackWidget::removeTab(int index
)
117 QWidget
*wid
= m_stackWidget
->widget(index
);
119 m_stackWidget
->removeWidget(wid
);
122 QListWidgetItem
*item
= m_listWidget
->item(index
);
123 m_listWidget
->removeItemWidget(item
);
127 void MyTabbedStackWidget::setWidgetsEnabled(bool enabled
)
129 for (int i
= 0; i
< m_stackWidget
->count(); i
++) {
130 m_stackWidget
->widget(i
)->setEnabled(enabled
);
134 int MyTabbedStackWidget::currentIndex() const
136 return m_listWidget
->currentRow();
139 void MyTabbedStackWidget::setCurrentIndex(int index
)
141 m_listWidget
->setCurrentRow(index
);
144 void MyTabbedStackWidget::showWidget(int index
)
146 if (m_stackWidget
->currentIndex() == index
) {
149 bool proceed
= false;
150 emit
currentAboutToShow(index
, &proceed
);
152 m_stackWidget
->setCurrentIndex(index
);
153 emit
currentChanged(index
);
155 m_listWidget
->setCurrentRow(m_stackWidget
->currentIndex(), QItemSelectionModel::ClearAndSelect
);
159 void MyTabbedStackWidget::resizeEvent(QResizeEvent
*event
)
161 QWidget::resizeEvent(event
);
163 m_listWidget
->setFixedWidth(m_listWidget
->verticalScrollBar()->isVisible() ? LIST_VIEW_WIDTH
+ 20 : LIST_VIEW_WIDTH
);
166 void MyTabbedStackWidget::insertCornerWidget(int index
, QWidget
*widget
)