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(":/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)),
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(":/core/images/closebutton.png"));
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->closeDuplicate(editor);
181 em->closeEditor(editor);
187 void EditorToolBar::addEditor(IEditor *editor)
189 QTC_ASSERT(editor, return)
190 connect(editor, SIGNAL(changed()), this, SLOT(checkEditorStatus()));
191 QWidget *toolBar = editor->toolBar();
193 if (toolBar && !m_isStandalone)
194 addCenterToolBar(toolBar);
196 updateEditorStatus(editor);
199 void EditorToolBar::addCenterToolBar(QWidget *toolBar)
201 QTC_ASSERT(toolBar, return)
202 toolBar->setVisible(false); // will be made visible in setCurrentEditor
203 m_toolBarPlaceholder->layout()->addWidget(toolBar);
205 updateToolBar(toolBar);
208 void EditorToolBar::updateToolBar(QWidget *toolBar)
211 toolBar = m_defaultToolBar;
212 if (m_activeToolBar == toolBar)
214 toolBar->setVisible(true);
215 m_activeToolBar->setVisible(false);
216 m_activeToolBar = toolBar;
219 void EditorToolBar::setToolbarCreationFlags(ToolbarCreationFlags flags)
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*)));
226 disconnect(m_editorList, SIGNAL(activated(int)), this, SIGNAL(listSelectionActivated(int)));
227 connect(m_editorList, SIGNAL(activated(int)), this, SLOT(changeActiveEditor(int)));
231 void EditorToolBar::setCurrentEditor(IEditor *editor)
233 QTC_ASSERT(editor, return)
234 m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
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.
239 updateToolBar(editor->toolBar());
241 updateEditorStatus(editor);
244 void EditorToolBar::updateEditorListSelection(IEditor *newSelection)
247 m_editorList->setCurrentIndex(m_editorsListModel->indexOf(newSelection).row());
250 void EditorToolBar::changeActiveEditor(int row)
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*>();
258 if (editor != em->currentEditor())
259 em->activateEditor(editor, EditorManager::NoModeSwitch);
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);
267 m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
271 void EditorToolBar::listContextMenu(QPoint pos)
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())
278 menu.addAction(tr("Copy full path to clipboard"));
279 if (menu.exec(m_editorList->mapToGlobal(pos))) {
280 QApplication::clipboard()->setText(fileName);
284 void EditorToolBar::makeEditorWritable()
287 ICore::instance()->editorManager()->makeEditorWritable(currentEditor());
290 void EditorToolBar::setCanGoBack(bool canGoBack)
292 m_goBackAction->setEnabled(canGoBack);
295 void EditorToolBar::setCanGoForward(bool canGoForward)
297 m_goForwardAction->setEnabled(canGoForward);
300 void EditorToolBar::updateActionShortcuts()
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());
308 IEditor *EditorToolBar::currentEditor() const
310 return ICore::instance()->editorManager()->currentEditor();
313 void EditorToolBar::checkEditorStatus()
315 IEditor *editor = qobject_cast<IEditor *>(sender());
316 IEditor *current = currentEditor();
318 if (current == editor)
319 updateEditorStatus(editor);
322 void EditorToolBar::updateEditorStatus(IEditor *editor)
324 m_lockButton->setVisible(editor != 0);
325 m_closeButton->setEnabled(editor != 0);
327 if (!editor || !editor->file()) {
328 m_editorList->setToolTip(QString());
332 m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
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"));
339 m_lockButton->setIcon(QIcon(m_editorsListModel->unlockedIcon()));
340 m_lockButton->setEnabled(false);
341 m_lockButton->setToolTip(tr("File is writable"));
343 if (editor == currentEditor())
344 m_editorList->setToolTip(
345 currentEditor()->file()->fileName().isEmpty()
346 ? currentEditor()->displayName()
347 : QDir::toNativeSeparators(editor->file()->fileName())
352 void EditorToolBar::setNavigationVisible(bool isVisible)
354 m_goBackAction->setVisible(isVisible);
355 m_goForwardAction->setVisible(isVisible);
356 m_backButton->setVisible(isVisible);
357 m_forwardButton->setVisible(isVisible);