1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "sidebarwidget.h"
36 #include "navigationsubwidget.h"
38 #include <coreplugin/coreconstants.h>
40 #include <QtGui/QToolBar>
41 #include <QtGui/QToolButton>
42 #include <QtGui/QAction>
43 #include <QtGui/QVBoxLayout>
48 class SideBarComboBox : public CommandComboBox
55 explicit SideBarComboBox(SideBarWidget *sideBarWidget) : m_sideBarWidget(sideBarWidget) {}
58 virtual const Core::Command *command(const QString &text) const
59 { return m_sideBarWidget->command(text); }
61 SideBarWidget *m_sideBarWidget;
64 SideBarWidget::SideBarWidget(SideBar *sideBar, const QString &id)
68 m_comboBox = new SideBarComboBox(this);
69 m_comboBox->setMinimumContentsLength(15);
71 m_toolbar = new QToolBar(this);
72 m_toolbar->setContentsMargins(0, 0, 0, 0);
73 m_toolbar->addWidget(m_comboBox);
75 QWidget *spacerItem = new QWidget(this);
76 spacerItem->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
77 m_toolbar->addWidget(spacerItem);
79 m_splitAction = new QAction(tr("Split"), m_toolbar);
80 m_splitAction->setToolTip(tr("Split"));
81 m_splitAction->setIcon(QIcon(QLatin1String(Constants::ICON_SPLIT_HORIZONTAL)));
82 connect(m_splitAction, SIGNAL(triggered()), this, SIGNAL(splitMe()));
83 m_toolbar->addAction(m_splitAction);
85 QAction *closeAction = new QAction(tr("Close"), m_toolbar);
86 closeAction->setToolTip(tr("Close"));
87 closeAction->setIcon(QIcon(QLatin1String(Constants::ICON_CLOSE)));
88 connect(closeAction, SIGNAL(triggered()), this, SIGNAL(closeMe()));
89 m_toolbar->addAction(closeAction);
91 QVBoxLayout *lay = new QVBoxLayout();
95 lay->addWidget(m_toolbar);
97 QStringList titleList = m_sideBar->availableItemTitles();
100 if (titleList.count()) {
101 foreach(const QString &itemTitle, titleList)
102 m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
104 m_comboBox->setCurrentIndex(0);
106 t = m_comboBox->itemData(0, SideBarComboBox::IdRole).toString();
110 connect(m_comboBox, SIGNAL(currentIndexChanged(int)),
111 this, SLOT(setCurrentIndex(int)));
114 SideBarWidget::~SideBarWidget()
118 QString SideBarWidget::currentItemTitle() const
120 return m_comboBox->currentText();
123 QString SideBarWidget::currentItemId() const
126 return m_currentItem->id();
130 void SideBarWidget::setCurrentItem(const QString &id)
133 int idx = m_comboBox->findData(QVariant(id), SideBarComboBox::IdRole);
138 bool blocked = m_comboBox->blockSignals(true);
139 m_comboBox->setCurrentIndex(idx);
140 m_comboBox->blockSignals(blocked);
143 SideBarItem *item = m_sideBar->item(id);
147 m_currentItem = item;
149 layout()->addWidget(m_currentItem->widget());
150 m_currentItem->widget()->show();
152 // Add buttons and remember their actions for later removal
153 foreach (QToolButton *b, m_currentItem->createToolBarWidgets())
154 m_addedToolBarActions.append(m_toolbar->insertWidget(m_splitAction, b));
157 void SideBarWidget::updateAvailableItems()
159 bool blocked = m_comboBox->blockSignals(true);
160 QString currentTitle = m_comboBox->currentText();
162 QStringList titleList = m_sideBar->availableItemTitles();
163 if (!currentTitle.isEmpty() && !titleList.contains(currentTitle))
164 titleList.append(currentTitle);
167 foreach(const QString &itemTitle, titleList)
168 m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
170 int idx = m_comboBox->findText(currentTitle);
175 m_comboBox->setCurrentIndex(idx);
176 m_splitAction->setEnabled(titleList.count() > 1);
177 m_comboBox->blockSignals(blocked);
180 void SideBarWidget::removeCurrentItem()
185 QWidget *w = m_currentItem->widget();
187 layout()->removeWidget(w);
189 m_sideBar->makeItemAvailable(m_currentItem);
191 // Delete custom toolbar widgets
192 qDeleteAll(m_addedToolBarActions);
193 m_addedToolBarActions.clear();
198 void SideBarWidget::setCurrentIndex(int)
200 setCurrentItem(m_comboBox->itemData(m_comboBox->currentIndex(),
201 SideBarComboBox::IdRole).toString());
202 emit currentWidgetChanged();
205 Core::Command *SideBarWidget::command(const QString &id) const
207 const QMap<QString, Core::Command*> shortcutMap = m_sideBar->shortcutMap();
208 QMap<QString, Core::Command*>::const_iterator r = shortcutMap.find(id);
209 if (r != shortcutMap.end())
214 } // namespace Internal