1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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.
16 ** GNU Lesser General Public License Usage
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.
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
28 **************************************************************************/
30 #include "editortoolbar.h"
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>
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>
45 #include <utils/parameteraction.h>
46 #include <utils/qtcassert.h>
47 #include <utils/styledbar.h>
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>
63 Q_DECLARE_METATYPE(Core::IEditor*)
72 Mimic the look of the text editor toolbar as defined in e.g. EditorView::EditorView
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),
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)),
84 m_toolBarPlaceholder(new QWidget),
85 m_defaultToolBar(new QWidget(this)),
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);
95 m_defaultToolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
96 m_activeToolBar = m_defaultToolBar;
98 m_editorsListModel = EditorManager::instance()->openedEditorsModel();
99 connect(m_goBackAction, SIGNAL(triggered()), this, SIGNAL(goBackClicked()));
100 connect(m_goForwardAction, SIGNAL(triggered()), this, SIGNAL(goForwardClicked()));
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);
108 m_lockButton->setAutoRaise(true);
109 m_lockButton->setProperty("type", QLatin1String("dockbutton"));
110 m_lockButton->setVisible(false);
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);
117 m_toolBarPlaceholder->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
119 m_backButton = new QToolButton(this);
120 m_backButton->setDefaultAction(m_goBackAction);
122 m_forwardButton= new QToolButton(this);
123 m_forwardButton->setDefaultAction(m_goForwardAction);
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);
135 setLayout(toplayout);
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)));
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);
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()));
155 void EditorToolBar::removeToolbarForEditor(IEditor *editor)
157 QTC_ASSERT(editor, return)
158 disconnect(editor, SIGNAL(changed()), this, SLOT(checkEditorStatus()));
160 QWidget *toolBar = editor->toolBar();
162 if (m_activeToolBar == toolBar) {
163 m_activeToolBar = m_defaultToolBar;
164 m_activeToolBar->setVisible(true);
166 m_toolBarPlaceholder->layout()->removeWidget(toolBar);
167 toolBar->setVisible(false);
168 toolBar->setParent(0);
172 void EditorToolBar::closeView()
174 if (!currentEditor())
177 if (m_isStandalone) {
178 EditorManager *em = ICore::instance()->editorManager();
179 if (IEditor *editor = currentEditor()) {
180 em->closeEditor(editor);
186 void EditorToolBar::addEditor(IEditor *editor)
188 QTC_ASSERT(editor, return)
189 connect(editor, SIGNAL(changed()), this, SLOT(checkEditorStatus()));
190 QWidget *toolBar = editor->toolBar();
192 if (toolBar && !m_isStandalone)
193 addCenterToolBar(toolBar);
195 updateEditorStatus(editor);
198 void EditorToolBar::addCenterToolBar(QWidget *toolBar)
200 QTC_ASSERT(toolBar, return)
201 toolBar->setVisible(false); // will be made visible in setCurrentEditor
202 m_toolBarPlaceholder->layout()->addWidget(toolBar);
204 updateToolBar(toolBar);
207 void EditorToolBar::updateToolBar(QWidget *toolBar)
210 toolBar = m_defaultToolBar;
211 if (m_activeToolBar == toolBar)
213 toolBar->setVisible(true);
214 m_activeToolBar->setVisible(false);
215 m_activeToolBar = toolBar;
218 void EditorToolBar::setToolbarCreationFlags(ToolbarCreationFlags flags)
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*)));
225 disconnect(m_editorList, SIGNAL(activated(int)), this, SIGNAL(listSelectionActivated(int)));
226 connect(m_editorList, SIGNAL(activated(int)), this, SLOT(changeActiveEditor(int)));
230 void EditorToolBar::setCurrentEditor(IEditor *editor)
232 QTC_ASSERT(editor, return)
233 m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
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.
238 updateToolBar(editor->toolBar());
240 updateEditorStatus(editor);
243 void EditorToolBar::updateEditorListSelection(IEditor *newSelection)
246 m_editorList->setCurrentIndex(m_editorsListModel->indexOf(newSelection).row());
249 void EditorToolBar::changeActiveEditor(int row)
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*>();
257 if (editor != em->currentEditor())
258 em->activateEditor(editor, EditorManager::NoModeSwitch);
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);
266 m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
270 void EditorToolBar::listContextMenu(QPoint pos)
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())
277 menu.addAction(tr("Copy Full Path to Clipboard"));
278 if (menu.exec(m_editorList->mapToGlobal(pos))) {
279 QApplication::clipboard()->setText(QDir::toNativeSeparators(fileName));
283 void EditorToolBar::makeEditorWritable()
286 ICore::instance()->editorManager()->makeEditorWritable(currentEditor());
289 void EditorToolBar::setCanGoBack(bool canGoBack)
291 m_goBackAction->setEnabled(canGoBack);
294 void EditorToolBar::setCanGoForward(bool canGoForward)
296 m_goForwardAction->setEnabled(canGoForward);
299 void EditorToolBar::updateActionShortcuts()
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());
307 IEditor *EditorToolBar::currentEditor() const
309 return ICore::instance()->editorManager()->currentEditor();
312 void EditorToolBar::checkEditorStatus()
314 IEditor *editor = qobject_cast<IEditor *>(sender());
315 IEditor *current = currentEditor();
317 if (current == editor)
318 updateEditorStatus(editor);
321 void EditorToolBar::updateEditorStatus(IEditor *editor)
323 m_lockButton->setVisible(editor != 0);
324 m_closeButton->setEnabled(editor != 0);
326 if (!editor || !editor->file()) {
327 m_editorList->setToolTip(QString());
331 m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
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"));
338 m_lockButton->setIcon(QIcon(m_editorsListModel->unlockedIcon()));
339 m_lockButton->setEnabled(false);
340 m_lockButton->setToolTip(tr("File is writable"));
342 if (editor == currentEditor())
343 m_editorList->setToolTip(
344 currentEditor()->file()->fileName().isEmpty()
345 ? currentEditor()->displayName()
346 : QDir::toNativeSeparators(editor->file()->fileName())
351 void EditorToolBar::setNavigationVisible(bool isVisible)
353 m_goBackAction->setVisible(isVisible);
354 m_goForwardAction->setVisible(isVisible);
355 m_backButton->setVisible(isVisible);
356 m_forwardButton->setVisible(isVisible);