1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "sidebarwidget.h"
35 #include "navigationsubwidget.h"
37 #include <coreplugin/coreconstants.h>
39 #include <QtGui/QToolBar>
40 #include <QtGui/QToolButton>
41 #include <QtGui/QAction>
42 #include <QtGui/QVBoxLayout>
47 class SideBarComboBox : public CommandComboBox
54 explicit SideBarComboBox(SideBarWidget *sideBarWidget) : m_sideBarWidget(sideBarWidget) {}
57 virtual const Core::Command *command(const QString &text) const
58 { return m_sideBarWidget->command(text); }
60 SideBarWidget *m_sideBarWidget;
63 SideBarWidget::SideBarWidget(SideBar *sideBar, const QString &id)
67 m_comboBox = new SideBarComboBox(this);
68 m_comboBox->setMinimumContentsLength(15);
70 m_toolbar = new QToolBar(this);
71 m_toolbar->setContentsMargins(0, 0, 0, 0);
72 m_toolbar->addWidget(m_comboBox);
74 QWidget *spacerItem = new QWidget(this);
75 spacerItem->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
76 m_toolbar->addWidget(spacerItem);
78 m_splitAction = new QAction(tr("Split"), m_toolbar);
79 m_splitAction->setToolTip(tr("Split"));
80 m_splitAction->setIcon(QIcon(QLatin1String(Constants::ICON_SPLIT_HORIZONTAL)));
81 connect(m_splitAction, SIGNAL(triggered()), this, SIGNAL(splitMe()));
82 m_toolbar->addAction(m_splitAction);
84 QAction *closeAction = new QAction(tr("Close"), m_toolbar);
85 closeAction->setToolTip(tr("Close"));
86 closeAction->setIcon(QIcon(QLatin1String(Constants::ICON_CLOSE)));
87 connect(closeAction, SIGNAL(triggered()), this, SIGNAL(closeMe()));
88 m_toolbar->addAction(closeAction);
90 QVBoxLayout *lay = new QVBoxLayout();
94 lay->addWidget(m_toolbar);
96 QStringList titleList = m_sideBar->availableItemTitles();
99 if (titleList.count()) {
100 foreach(const QString &itemTitle, titleList)
101 m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
103 m_comboBox->setCurrentIndex(0);
105 t = m_comboBox->itemData(0, SideBarComboBox::IdRole).toString();
109 connect(m_comboBox, SIGNAL(currentIndexChanged(int)),
110 this, SLOT(setCurrentIndex(int)));
113 SideBarWidget::~SideBarWidget()
117 QString SideBarWidget::currentItemTitle() const
119 return m_comboBox->currentText();
122 QString SideBarWidget::currentItemId() const
125 return m_currentItem->id();
129 void SideBarWidget::setCurrentItem(const QString &id)
132 int idx = m_comboBox->findData(QVariant(id), SideBarComboBox::IdRole);
137 bool blocked = m_comboBox->blockSignals(true);
138 m_comboBox->setCurrentIndex(idx);
139 m_comboBox->blockSignals(blocked);
142 SideBarItem *item = m_sideBar->item(id);
146 m_currentItem = item;
148 layout()->addWidget(m_currentItem->widget());
149 m_currentItem->widget()->show();
151 // Add buttons and remember their actions for later removal
152 foreach (QToolButton *b, m_currentItem->createToolBarWidgets())
153 m_addedToolBarActions.append(m_toolbar->insertWidget(m_splitAction, b));
156 void SideBarWidget::updateAvailableItems()
158 bool blocked = m_comboBox->blockSignals(true);
159 QString currentTitle = m_comboBox->currentText();
161 QStringList titleList = m_sideBar->availableItemTitles();
162 if (!currentTitle.isEmpty() && !titleList.contains(currentTitle))
163 titleList.append(currentTitle);
166 foreach(const QString &itemTitle, titleList)
167 m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
169 int idx = m_comboBox->findText(currentTitle);
174 m_comboBox->setCurrentIndex(idx);
175 m_splitAction->setEnabled(titleList.count() > 1);
176 m_comboBox->blockSignals(blocked);
179 void SideBarWidget::removeCurrentItem()
184 QWidget *w = m_currentItem->widget();
186 layout()->removeWidget(w);
188 m_sideBar->makeItemAvailable(m_currentItem);
190 // Delete custom toolbar widgets
191 qDeleteAll(m_addedToolBarActions);
192 m_addedToolBarActions.clear();
197 void SideBarWidget::setCurrentIndex(int)
199 setCurrentItem(m_comboBox->itemData(m_comboBox->currentIndex(),
200 SideBarComboBox::IdRole).toString());
201 emit currentWidgetChanged();
204 Core::Command *SideBarWidget::command(const QString &id) const
206 const QMap<QString, Core::Command*> shortcutMap = m_sideBar->shortcutMap();
207 QMap<QString, Core::Command*>::const_iterator r = shortcutMap.find(id);
208 if (r != shortcutMap.end())
213 } // namespace Internal