OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / sidebarwidget.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "sidebarwidget.h"
35 #include "sidebar.h"
36 #include "navigationsubwidget.h"
37
38 #include <coreplugin/coreconstants.h>
39
40 #include <QtGui/QToolBar>
41 #include <QtGui/QToolButton>
42 #include <QtGui/QAction>
43 #include <QtGui/QVBoxLayout>
44
45 namespace Core {
46 namespace Internal {
47
48 class SideBarComboBox : public CommandComboBox
49 {
50 public:
51     enum DataRoles {
52         IdRole = Qt::UserRole
53     };
54
55     explicit SideBarComboBox(SideBarWidget *sideBarWidget) : m_sideBarWidget(sideBarWidget) {}
56
57 private:
58     virtual const Core::Command *command(const QString &text) const
59         { return m_sideBarWidget->command(text); }
60
61     SideBarWidget *m_sideBarWidget;
62 };
63
64 SideBarWidget::SideBarWidget(SideBar *sideBar, const QString &id)
65     : m_currentItem(0)
66     , m_sideBar(sideBar)
67 {
68     m_comboBox = new SideBarComboBox(this);
69     m_comboBox->setMinimumContentsLength(15);
70
71     m_toolbar = new QToolBar(this);
72     m_toolbar->setContentsMargins(0, 0, 0, 0);
73     m_toolbar->addWidget(m_comboBox);
74
75     QWidget *spacerItem = new QWidget(this);
76     spacerItem->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
77     m_toolbar->addWidget(spacerItem);
78
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);
84
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);
90
91     QVBoxLayout *lay = new QVBoxLayout();
92     lay->setMargin(0);
93     lay->setSpacing(0);
94     setLayout(lay);
95     lay->addWidget(m_toolbar);
96
97     QStringList titleList = m_sideBar->availableItemTitles();
98     qSort(titleList);
99     QString t = id;
100     if (titleList.count()) {
101         foreach(const QString &itemTitle, titleList)
102             m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
103
104         m_comboBox->setCurrentIndex(0);
105         if (t.isEmpty())
106             t = m_comboBox->itemData(0, SideBarComboBox::IdRole).toString();
107     }
108     setCurrentItem(t);
109
110     connect(m_comboBox, SIGNAL(currentIndexChanged(int)),
111             this, SLOT(setCurrentIndex(int)));
112 }
113
114 SideBarWidget::~SideBarWidget()
115 {
116 }
117
118 QString SideBarWidget::currentItemTitle() const
119 {
120     return m_comboBox->currentText();
121 }
122
123 QString SideBarWidget::currentItemId() const
124 {
125     if (m_currentItem)
126         return m_currentItem->id();
127     return QString();
128 }
129
130 void SideBarWidget::setCurrentItem(const QString &id)
131 {
132     if (!id.isEmpty()) {
133         int idx = m_comboBox->findData(QVariant(id), SideBarComboBox::IdRole);
134
135         if (idx < 0)
136             idx = 0;
137
138         bool blocked = m_comboBox->blockSignals(true);
139         m_comboBox->setCurrentIndex(idx);
140         m_comboBox->blockSignals(blocked);
141     }
142
143     SideBarItem *item = m_sideBar->item(id);
144     if (!item)
145         return;
146     removeCurrentItem();
147     m_currentItem = item;
148
149     layout()->addWidget(m_currentItem->widget());
150     m_currentItem->widget()->show();
151
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));
155 }
156
157 void SideBarWidget::updateAvailableItems()
158 {
159     bool blocked = m_comboBox->blockSignals(true);
160     QString currentTitle = m_comboBox->currentText();
161     m_comboBox->clear();
162     QStringList titleList = m_sideBar->availableItemTitles();
163     if (!currentTitle.isEmpty() && !titleList.contains(currentTitle))
164         titleList.append(currentTitle);
165     qSort(titleList);
166
167     foreach(const QString &itemTitle, titleList)
168         m_comboBox->addItem(itemTitle, m_sideBar->idForTitle(itemTitle));
169
170     int idx = m_comboBox->findText(currentTitle);
171
172     if (idx < 0)
173         idx = 0;
174
175     m_comboBox->setCurrentIndex(idx);
176     m_splitAction->setEnabled(titleList.count() > 1);
177     m_comboBox->blockSignals(blocked);
178 }
179
180 void SideBarWidget::removeCurrentItem()
181 {
182     if (!m_currentItem)
183         return;
184
185     QWidget *w = m_currentItem->widget();
186     w->hide();
187     layout()->removeWidget(w);
188     w->setParent(0);
189     m_sideBar->makeItemAvailable(m_currentItem);
190
191     // Delete custom toolbar widgets
192     qDeleteAll(m_addedToolBarActions);
193     m_addedToolBarActions.clear();
194
195     m_currentItem = 0;
196 }
197
198 void SideBarWidget::setCurrentIndex(int)
199 {
200     setCurrentItem(m_comboBox->itemData(m_comboBox->currentIndex(),
201                                         SideBarComboBox::IdRole).toString());
202     emit currentWidgetChanged();
203 }
204
205 Core::Command *SideBarWidget::command(const QString &id) const
206 {
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())
210         return r.value();
211     return 0;
212 }
213
214 } // namespace Internal
215 } // namespace Core