OSDN Git Service

Less includes
[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(":/help/images/previous.png")), EditorManager::tr("Go Back"), parent)),
81         m_goForwardAction(new QAction(QIcon(QLatin1String(":/help/images/next.png")), 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(":/core/images/closebutton.png"));
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->closeDuplicate(editor);
181             em->closeEditor(editor);
182         }
183     }
184     emit closeClicked();
185 }
186
187 void EditorToolBar::addEditor(IEditor *editor)
188 {
189     QTC_ASSERT(editor, return)
190     connect(editor, SIGNAL(changed()), this, SLOT(checkEditorStatus()));
191     QWidget *toolBar = editor->toolBar();
192
193     if (toolBar && !m_isStandalone)
194         addCenterToolBar(toolBar);
195
196     updateEditorStatus(editor);
197 }
198
199 void EditorToolBar::addCenterToolBar(QWidget *toolBar)
200 {
201     QTC_ASSERT(toolBar, return)
202     toolBar->setVisible(false); // will be made visible in setCurrentEditor
203     m_toolBarPlaceholder->layout()->addWidget(toolBar);
204
205     updateToolBar(toolBar);
206 }
207
208 void EditorToolBar::updateToolBar(QWidget *toolBar)
209 {
210     if (!toolBar)
211         toolBar = m_defaultToolBar;
212     if (m_activeToolBar == toolBar)
213         return;
214     toolBar->setVisible(true);
215     m_activeToolBar->setVisible(false);
216     m_activeToolBar = toolBar;
217 }
218
219 void EditorToolBar::setToolbarCreationFlags(ToolbarCreationFlags flags)
220 {
221     m_isStandalone = flags & FlagsStandalone;
222     if (m_isStandalone) {
223         EditorManager *em = EditorManager::instance();
224         connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)), SLOT(updateEditorListSelection(Core::IEditor*)));
225
226         disconnect(m_editorList, SIGNAL(activated(int)), this, SIGNAL(listSelectionActivated(int)));
227         connect(m_editorList, SIGNAL(activated(int)), this, SLOT(changeActiveEditor(int)));
228     }
229 }
230
231 void EditorToolBar::setCurrentEditor(IEditor *editor)
232 {
233     QTC_ASSERT(editor, return)
234     m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
235
236     // If we never added the toolbar from the editor,  we will never change
237     // the editor, so there's no need to update the toolbar either.
238     if (!m_isStandalone)
239         updateToolBar(editor->toolBar());
240
241     updateEditorStatus(editor);
242 }
243
244 void EditorToolBar::updateEditorListSelection(IEditor *newSelection)
245 {
246     if (newSelection)
247         m_editorList->setCurrentIndex(m_editorsListModel->indexOf(newSelection).row());
248 }
249
250 void EditorToolBar::changeActiveEditor(int row)
251 {
252     EditorManager *em = ICore::instance()->editorManager();
253     QAbstractItemModel *model = m_editorList->model();
254     const QModelIndex modelIndex = model->index(row, 0);
255     IEditor *editor = model->data(modelIndex, Qt::UserRole).value<IEditor*>();
256
257     if (editor) {
258         if (editor != em->currentEditor())
259             em->activateEditor(editor, EditorManager::NoModeSwitch);
260     } else {
261         //em->activateEditor(model->index(index, 0), this);
262         QString fileName = model->data(modelIndex, Qt::UserRole + 1).toString();
263         QByteArray kind = model->data(modelIndex, Qt::UserRole + 2).toByteArray();
264         editor = em->openEditor(fileName, kind, EditorManager::NoModeSwitch);
265     }
266     if (editor) {
267         m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
268     }
269 }
270
271 void EditorToolBar::listContextMenu(QPoint pos)
272 {
273     QModelIndex index = m_editorsListModel->index(m_editorList->currentIndex(), 0);
274     QString fileName = m_editorsListModel->data(index, Qt::UserRole + 1).toString();
275     if (fileName.isEmpty())
276         return;
277     QMenu menu;
278     menu.addAction(tr("Copy full path to clipboard"));
279     if (menu.exec(m_editorList->mapToGlobal(pos))) {
280         QApplication::clipboard()->setText(fileName);
281     }
282 }
283
284 void EditorToolBar::makeEditorWritable()
285 {
286     if (currentEditor())
287         ICore::instance()->editorManager()->makeEditorWritable(currentEditor());
288 }
289
290 void EditorToolBar::setCanGoBack(bool canGoBack)
291 {
292     m_goBackAction->setEnabled(canGoBack);
293 }
294
295 void EditorToolBar::setCanGoForward(bool canGoForward)
296 {
297     m_goForwardAction->setEnabled(canGoForward);
298 }
299
300 void EditorToolBar::updateActionShortcuts()
301 {
302     ActionManager *am = ICore::instance()->actionManager();
303     m_closeButton->setToolTip(am->command(Constants::CLOSE)->stringWithAppendedShortcut(EditorManager::tr("Close")));
304     m_goBackAction->setToolTip(am->command(Constants::GO_BACK)->action()->toolTip());
305     m_goForwardAction->setToolTip(am->command(Constants::GO_FORWARD)->action()->toolTip());
306 }
307
308 IEditor *EditorToolBar::currentEditor() const
309 {
310     return ICore::instance()->editorManager()->currentEditor();
311 }
312
313 void EditorToolBar::checkEditorStatus()
314 {
315     IEditor *editor = qobject_cast<IEditor *>(sender());
316     IEditor *current = currentEditor();
317
318     if (current == editor)
319         updateEditorStatus(editor);
320 }
321
322 void EditorToolBar::updateEditorStatus(IEditor *editor)
323 {
324     m_lockButton->setVisible(editor != 0);
325     m_closeButton->setEnabled(editor != 0);
326
327     if (!editor || !editor->file()) {
328         m_editorList->setToolTip(QString());
329         return;
330     }
331
332     m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
333
334     if (editor->file()->isReadOnly()) {
335         m_lockButton->setIcon(QIcon(m_editorsListModel->lockedIcon()));
336         m_lockButton->setEnabled(!editor->file()->fileName().isEmpty());
337         m_lockButton->setToolTip(tr("Make writable"));
338     } else {
339         m_lockButton->setIcon(QIcon(m_editorsListModel->unlockedIcon()));
340         m_lockButton->setEnabled(false);
341         m_lockButton->setToolTip(tr("File is writable"));
342     }
343     if (editor == currentEditor())
344         m_editorList->setToolTip(
345                 currentEditor()->file()->fileName().isEmpty()
346                 ? currentEditor()->displayName()
347                     : QDir::toNativeSeparators(editor->file()->fileName())
348                     );
349
350 }
351
352 void EditorToolBar::setNavigationVisible(bool isVisible)
353 {
354     m_goBackAction->setVisible(isVisible);
355     m_goForwardAction->setVisible(isVisible);
356     m_backButton->setVisible(isVisible);
357     m_forwardButton->setVisible(isVisible);
358 }
359
360 } // Core