OSDN Git Service

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