OSDN Git Service

Merge remote branch 'origin/2.0'
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / editortoolbar.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
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 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #include "editortoolbar.h"
31
32 #include <coreplugin/coreconstants.h>
33 #include <coreplugin/editormanager/ieditor.h>
34 #include <coreplugin/icore.h>
35 #include <coreplugin/minisplitter.h>
36 #include <coreplugin/sidebar.h>
37
38 #include <coreplugin/editormanager/editorview.h>
39 #include <coreplugin/editormanager/editormanager.h>
40 #include <coreplugin/editormanager/openeditorsmodel.h>
41 #include <coreplugin/editormanager/ieditor.h>
42 #include <coreplugin/actionmanager/actionmanager.h>
43 #include <coreplugin/actionmanager/command.h>
44
45 #include <utils/parameteraction.h>
46 #include <utils/qtcassert.h>
47 #include <utils/styledbar.h>
48
49 #include <QtCore/QSettings>
50 #include <QtCore/QEvent>
51 #include <QtCore/QDir>
52 #include <QtGui/QApplication>
53 #include <QtGui/QPlainTextEdit>
54 #include <QtGui/QVBoxLayout>
55 #include <QtGui/QScrollArea>
56 #include <QtGui/QTabWidget>
57 #include <QtGui/QToolButton>
58 #include <QtGui/QMenu>
59 #include <QtGui/QClipboard>
60 #include <QtGui/QLabel>
61 #include <QtGui/QToolBar>
62
63 Q_DECLARE_METATYPE(Core::IEditor*)
64
65 enum {
66     debug = false
67 };
68
69 namespace Core {
70
71 /*!
72   Mimic the look of the text editor toolbar as defined in e.g. EditorView::EditorView
73   */
74 EditorToolBar::EditorToolBar(QWidget *parent) :
75         Utils::StyledBar(parent),
76         m_editorList(new QComboBox(this)),
77         m_closeButton(new QToolButton),
78         m_lockButton(new QToolButton),
79
80         m_goBackAction(new QAction(QIcon(QLatin1String(Constants::ICON_PREV)), EditorManager::tr("Go Back"), parent)),
81         m_goForwardAction(new QAction(QIcon(QLatin1String(Constants::ICON_NEXT)), EditorManager::tr("Go Forward"), parent)),
82
83         m_activeToolBar(0),
84         m_toolBarPlaceholder(new QWidget),
85         m_defaultToolBar(new QWidget(this)),
86         m_isStandalone(false)
87 {
88     QHBoxLayout *toolBarLayout = new QHBoxLayout(this);
89     toolBarLayout->setMargin(0);
90     toolBarLayout->setSpacing(0);
91     toolBarLayout->addWidget(m_defaultToolBar);
92     m_toolBarPlaceholder->setLayout(toolBarLayout);
93     m_toolBarPlaceholder->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
94
95     m_defaultToolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
96     m_activeToolBar = m_defaultToolBar;
97
98     m_editorsListModel = EditorManager::instance()->openedEditorsModel();
99     connect(m_goBackAction, SIGNAL(triggered()), this, SIGNAL(goBackClicked()));
100     connect(m_goForwardAction, SIGNAL(triggered()), this, SIGNAL(goForwardClicked()));
101
102     m_editorList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
103     m_editorList->setMinimumContentsLength(20);
104     m_editorList->setModel(m_editorsListModel);
105     m_editorList->setMaxVisibleItems(40);
106     m_editorList->setContextMenuPolicy(Qt::CustomContextMenu);
107
108     m_lockButton->setAutoRaise(true);
109     m_lockButton->setProperty("type", QLatin1String("dockbutton"));
110     m_lockButton->setVisible(false);
111
112     m_closeButton->setAutoRaise(true);
113     m_closeButton->setIcon(QIcon(QLatin1String(Constants::ICON_CLOSE)));
114     m_closeButton->setProperty("type", QLatin1String("dockbutton"));
115     m_closeButton->setEnabled(false);
116
117     m_toolBarPlaceholder->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
118
119     m_backButton = new QToolButton(this);
120     m_backButton->setDefaultAction(m_goBackAction);
121
122     m_forwardButton= new QToolButton(this);
123     m_forwardButton->setDefaultAction(m_goForwardAction);
124
125     QHBoxLayout *toplayout = new QHBoxLayout(this);
126     toplayout->setSpacing(0);
127     toplayout->setMargin(0);
128     toplayout->addWidget(m_backButton);
129     toplayout->addWidget(m_forwardButton);
130     toplayout->addWidget(m_editorList);
131     toplayout->addWidget(m_toolBarPlaceholder, 1); // Custom toolbar stretches
132     toplayout->addWidget(m_lockButton);
133     toplayout->addWidget(m_closeButton);
134
135     setLayout(toplayout);
136
137     // this signal is disconnected for standalone toolbars and replaced with
138     // a private slot connection
139     connect(m_editorList, SIGNAL(activated(int)), this, SIGNAL(listSelectionActivated(int)));
140
141     connect(m_editorList, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(listContextMenu(QPoint)));
142     connect(m_lockButton, SIGNAL(clicked()), this, SLOT(makeEditorWritable()));
143     connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeView()), Qt::QueuedConnection);
144
145     ActionManager *am = ICore::instance()->actionManager();
146     connect(am->command(Constants::CLOSE), SIGNAL(keySequenceChanged()),
147             this, SLOT(updateActionShortcuts()));
148     connect(am->command(Constants::GO_BACK), SIGNAL(keySequenceChanged()),
149             this, SLOT(updateActionShortcuts()));
150     connect(am->command(Constants::GO_FORWARD), SIGNAL(keySequenceChanged()),
151             this, SLOT(updateActionShortcuts()));
152
153 }
154
155 void EditorToolBar::removeToolbarForEditor(IEditor *editor)
156 {
157     QTC_ASSERT(editor, return)
158     disconnect(editor, SIGNAL(changed()), this, SLOT(checkEditorStatus()));
159
160     QWidget *toolBar = editor->toolBar();
161     if (toolBar != 0) {
162         if (m_activeToolBar == toolBar) {
163             m_activeToolBar = m_defaultToolBar;
164             m_activeToolBar->setVisible(true);
165         }
166         m_toolBarPlaceholder->layout()->removeWidget(toolBar);
167         toolBar->setVisible(false);
168         toolBar->setParent(0);
169     }
170 }
171
172 void EditorToolBar::closeView()
173 {
174     if (!currentEditor())
175         return;
176
177     if (m_isStandalone) {
178         EditorManager *em = ICore::instance()->editorManager();
179         if (IEditor *editor = currentEditor()) {
180             em->closeEditor(editor);
181         }
182     }
183     emit closeClicked();
184 }
185
186 void EditorToolBar::addEditor(IEditor *editor)
187 {
188     QTC_ASSERT(editor, return)
189     connect(editor, SIGNAL(changed()), this, SLOT(checkEditorStatus()));
190     QWidget *toolBar = editor->toolBar();
191
192     if (toolBar && !m_isStandalone)
193         addCenterToolBar(toolBar);
194
195     updateEditorStatus(editor);
196 }
197
198 void EditorToolBar::addCenterToolBar(QWidget *toolBar)
199 {
200     QTC_ASSERT(toolBar, return)
201     toolBar->setVisible(false); // will be made visible in setCurrentEditor
202     m_toolBarPlaceholder->layout()->addWidget(toolBar);
203
204     updateToolBar(toolBar);
205 }
206
207 void EditorToolBar::updateToolBar(QWidget *toolBar)
208 {
209     if (!toolBar)
210         toolBar = m_defaultToolBar;
211     if (m_activeToolBar == toolBar)
212         return;
213     toolBar->setVisible(true);
214     m_activeToolBar->setVisible(false);
215     m_activeToolBar = toolBar;
216 }
217
218 void EditorToolBar::setToolbarCreationFlags(ToolbarCreationFlags flags)
219 {
220     m_isStandalone = flags & FlagsStandalone;
221     if (m_isStandalone) {
222         EditorManager *em = EditorManager::instance();
223         connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)), SLOT(updateEditorListSelection(Core::IEditor*)));
224
225         disconnect(m_editorList, SIGNAL(activated(int)), this, SIGNAL(listSelectionActivated(int)));
226         connect(m_editorList, SIGNAL(activated(int)), this, SLOT(changeActiveEditor(int)));
227     }
228 }
229
230 void EditorToolBar::setCurrentEditor(IEditor *editor)
231 {
232     QTC_ASSERT(editor, return)
233     m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
234
235     // If we never added the toolbar from the editor,  we will never change
236     // the editor, so there's no need to update the toolbar either.
237     if (!m_isStandalone)
238         updateToolBar(editor->toolBar());
239
240     updateEditorStatus(editor);
241 }
242
243 void EditorToolBar::updateEditorListSelection(IEditor *newSelection)
244 {
245     if (newSelection)
246         m_editorList->setCurrentIndex(m_editorsListModel->indexOf(newSelection).row());
247 }
248
249 void EditorToolBar::changeActiveEditor(int row)
250 {
251     EditorManager *em = ICore::instance()->editorManager();
252     QAbstractItemModel *model = m_editorList->model();
253     const QModelIndex modelIndex = model->index(row, 0);
254     IEditor *editor = model->data(modelIndex, Qt::UserRole).value<IEditor*>();
255
256     if (editor) {
257         if (editor != em->currentEditor())
258             em->activateEditor(editor, EditorManager::NoModeSwitch);
259     } else {
260         //em->activateEditor(model->index(index, 0), this);
261         QString fileName = model->data(modelIndex, Qt::UserRole + 1).toString();
262         QByteArray kind = model->data(modelIndex, Qt::UserRole + 2).toByteArray();
263         editor = em->openEditor(fileName, kind, EditorManager::NoModeSwitch);
264     }
265     if (editor) {
266         m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
267     }
268 }
269
270 void EditorToolBar::listContextMenu(QPoint pos)
271 {
272     QModelIndex index = m_editorsListModel->index(m_editorList->currentIndex(), 0);
273     QString fileName = m_editorsListModel->data(index, Qt::UserRole + 1).toString();
274     if (fileName.isEmpty())
275         return;
276     QMenu menu;
277     menu.addAction(tr("Copy Full Path to Clipboard"));
278     if (menu.exec(m_editorList->mapToGlobal(pos))) {
279         QApplication::clipboard()->setText(QDir::toNativeSeparators(fileName));
280     }
281 }
282
283 void EditorToolBar::makeEditorWritable()
284 {
285     if (currentEditor())
286         ICore::instance()->editorManager()->makeEditorWritable(currentEditor());
287 }
288
289 void EditorToolBar::setCanGoBack(bool canGoBack)
290 {
291     m_goBackAction->setEnabled(canGoBack);
292 }
293
294 void EditorToolBar::setCanGoForward(bool canGoForward)
295 {
296     m_goForwardAction->setEnabled(canGoForward);
297 }
298
299 void EditorToolBar::updateActionShortcuts()
300 {
301     ActionManager *am = ICore::instance()->actionManager();
302     m_closeButton->setToolTip(am->command(Constants::CLOSE)->stringWithAppendedShortcut(EditorManager::tr("Close")));
303     m_goBackAction->setToolTip(am->command(Constants::GO_BACK)->action()->toolTip());
304     m_goForwardAction->setToolTip(am->command(Constants::GO_FORWARD)->action()->toolTip());
305 }
306
307 IEditor *EditorToolBar::currentEditor() const
308 {
309     return ICore::instance()->editorManager()->currentEditor();
310 }
311
312 void EditorToolBar::checkEditorStatus()
313 {
314     IEditor *editor = qobject_cast<IEditor *>(sender());
315     IEditor *current = currentEditor();
316
317     if (current == editor)
318         updateEditorStatus(editor);
319 }
320
321 void EditorToolBar::updateEditorStatus(IEditor *editor)
322 {
323     m_lockButton->setVisible(editor != 0);
324     m_closeButton->setEnabled(editor != 0);
325
326     if (!editor || !editor->file()) {
327         m_editorList->setToolTip(QString());
328         return;
329     }
330
331     m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
332
333     if (editor->file()->isReadOnly()) {
334         m_lockButton->setIcon(QIcon(m_editorsListModel->lockedIcon()));
335         m_lockButton->setEnabled(!editor->file()->fileName().isEmpty());
336         m_lockButton->setToolTip(tr("Make writable"));
337     } else {
338         m_lockButton->setIcon(QIcon(m_editorsListModel->unlockedIcon()));
339         m_lockButton->setEnabled(false);
340         m_lockButton->setToolTip(tr("File is writable"));
341     }
342     if (editor == currentEditor())
343         m_editorList->setToolTip(
344                 currentEditor()->file()->fileName().isEmpty()
345                 ? currentEditor()->displayName()
346                     : QDir::toNativeSeparators(editor->file()->fileName())
347                     );
348
349 }
350
351 void EditorToolBar::setNavigationVisible(bool isVisible)
352 {
353     m_goBackAction->setVisible(isVisible);
354     m_goForwardAction->setVisible(isVisible);
355     m_backButton->setVisible(isVisible);
356     m_forwardButton->setVisible(isVisible);
357 }
358
359 } // Core