editortoolbar.cpp 11.8 KB
Newer Older
Lasse Holmstedt's avatar
Lasse Holmstedt committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

30
#include "editortoolbar.h"
Lasse Holmstedt's avatar
Lasse Holmstedt committed
31
32
33
34

#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
35
#include <coreplugin/minisplitter.h>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
36
37
38
39
40
41
42
43
44
45
#include <coreplugin/sidebar.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/openeditorsmodel.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/modemanager.h>

#include <utils/parameteraction.h>
#include <utils/qtcassert.h>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
46
#include <utils/styledbar.h>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <QtCore/QSettings>
#include <QtCore/QEvent>
#include <QtCore/QDir>
#include <QtGui/QApplication>
#include <QtGui/QPlainTextEdit>
#include <QtGui/QVBoxLayout>
#include <QtGui/QScrollArea>
#include <QtGui/QTabWidget>
#include <QtGui/QToolButton>
#include <QtGui/QMenu>
#include <QtGui/QClipboard>
#include <QtGui/QLabel>
#include <QtGui/QToolBar>

Q_DECLARE_METATYPE(Core::IEditor*)

enum {
    debug = false
};

68
namespace Core {
Lasse Holmstedt's avatar
Lasse Holmstedt committed
69
70
71
72

/*!
  Mimic the look of the text editor toolbar as defined in e.g. EditorView::EditorView
  */
Lasse Holmstedt's avatar
Lasse Holmstedt committed
73
74
75
76
EditorToolBar::EditorToolBar(QWidget *parent) :
        Utils::StyledBar(parent),
        m_editorList(new QComboBox(this)),
        m_rightToolBar(new QToolBar(this)),
Lasse Holmstedt's avatar
Lasse Holmstedt committed
77
78
        m_closeButton(new QToolButton),
        m_lockButton(new QToolButton),
Lasse Holmstedt's avatar
Lasse Holmstedt committed
79

Lasse Holmstedt's avatar
Lasse Holmstedt committed
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)),
Lasse Holmstedt's avatar
Lasse Holmstedt committed
82
83
84
85
86

        m_activeToolBar(0),
        m_toolBarPlaceholder(new QWidget),
        m_defaultToolBar(new QWidget(this)),
        m_ignoreEditorToolbar(false)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
87
{
Lasse Holmstedt's avatar
Lasse Holmstedt committed
88
89
90
91
92
93
    QHBoxLayout *toolBarLayout = new QHBoxLayout(this);
    toolBarLayout->setMargin(0);
    toolBarLayout->setSpacing(0);
    toolBarLayout->addWidget(m_defaultToolBar);
    m_toolBarPlaceholder->setLayout(toolBarLayout);
    m_toolBarPlaceholder->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
94

Lasse Holmstedt's avatar
Lasse Holmstedt committed
95
96
97
98
99
100
    m_defaultToolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    m_activeToolBar = m_defaultToolBar;

    m_editorsListModel = EditorManager::instance()->openedEditorsModel();
    connect(m_goBackAction, SIGNAL(triggered()), this, SIGNAL(goBackClicked()));
    connect(m_goForwardAction, SIGNAL(triggered()), this, SIGNAL(goForwardClicked()));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
101
102
103
104
105
106
107
108
109

    m_editorList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    m_editorList->setMinimumContentsLength(20);
    m_editorList->setModel(m_editorsListModel);
    m_editorList->setMaxVisibleItems(40);
    m_editorList->setContextMenuPolicy(Qt::CustomContextMenu);

    m_lockButton->setAutoRaise(true);
    m_lockButton->setProperty("type", QLatin1String("dockbutton"));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
110
    m_lockButton->setVisible(false);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
111
112
113
114

    m_closeButton->setAutoRaise(true);
    m_closeButton->setIcon(QIcon(":/core/images/closebutton.png"));
    m_closeButton->setProperty("type", QLatin1String("dockbutton"));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
115
    m_closeButton->setEnabled(false);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
116

Lasse Holmstedt's avatar
Lasse Holmstedt committed
117
118
119
120
121
122
123
    m_toolBarPlaceholder->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);

    m_backButton = new QToolButton(this);
    m_backButton->setDefaultAction(m_goBackAction);

    m_forwardButton= new QToolButton(this);
    m_forwardButton->setDefaultAction(m_goForwardAction);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
124

125
126
127
    m_rightToolBar->setLayoutDirection(Qt::RightToLeft);
    m_rightToolBar->addWidget(m_closeButton);
    m_rightToolBar->addWidget(m_lockButton);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
128
129
130
131

    QHBoxLayout *toplayout = new QHBoxLayout(this);
    toplayout->setSpacing(0);
    toplayout->setMargin(0);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
132
133
134
135
    toplayout->addWidget(m_backButton);
    toplayout->addWidget(m_forwardButton);
    toplayout->addWidget(m_editorList);
    toplayout->addWidget(m_toolBarPlaceholder, 1); // Custom toolbar stretches
136
    toplayout->addWidget(m_rightToolBar);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
137

Lasse Holmstedt's avatar
Lasse Holmstedt committed
138
139
    setLayout(toplayout);

Lasse Holmstedt's avatar
Lasse Holmstedt committed
140
141
142
    connect(m_editorList, SIGNAL(activated(int)), this, SLOT(listSelectionActivated(int)));
    connect(m_editorList, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(listContextMenu(QPoint)));
    connect(m_lockButton, SIGNAL(clicked()), this, SLOT(makeEditorWritable()));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
143
    connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeView()), Qt::QueuedConnection);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
144

Lasse Holmstedt's avatar
Lasse Holmstedt committed
145
146
147
148
149
150
151
152
153
154
    EditorManager *em = EditorManager::instance();
    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)), SLOT(updateEditorListSelection(Core::IEditor*)));

    ActionManager *am = ICore::instance()->actionManager();
    connect(am->command(Constants::CLOSE), SIGNAL(keySequenceChanged()),
            this, SLOT(updateActionShortcuts()));
    connect(am->command(Constants::GO_BACK), SIGNAL(keySequenceChanged()),
            this, SLOT(updateActionShortcuts()));
    connect(am->command(Constants::GO_FORWARD), SIGNAL(keySequenceChanged()),
            this, SLOT(updateActionShortcuts()));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
155
156
157

}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
158
void EditorToolBar::removeToolbarForEditor(IEditor *editor)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
159
{
Lasse Holmstedt's avatar
Lasse Holmstedt committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    disconnect(editor, SIGNAL(changed()), this, SLOT(checkEditorStatus()));

    QWidget *toolBar = editor->toolBar();
    if (toolBar != 0) {
        if (m_activeToolBar == toolBar) {
            m_activeToolBar = m_defaultToolBar;
            m_activeToolBar->setVisible(true);
        }
        m_toolBarPlaceholder->layout()->removeWidget(toolBar);
        toolBar->setVisible(false);
        toolBar->setParent(0);
    }
}

void EditorToolBar::closeView()
{
    if (!currentEditor())
        return;

    EditorManager *em = ICore::instance()->editorManager();
    if (IEditor *editor = currentEditor()) {
            em->closeDuplicate(editor);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
182
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
183
184
185
    emit closeClicked();
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
186
void EditorToolBar::addEditor(IEditor *editor)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
187
188
189
190
191
192
{
    connect(editor, SIGNAL(changed()), this, SLOT(checkEditorStatus()));
    QWidget *toolBar = editor->toolBar();

    if (toolBar && !m_ignoreEditorToolbar)
        addCenterToolBar(toolBar);
193

Lasse Holmstedt's avatar
Lasse Holmstedt committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    updateEditorStatus(editor);
}

void EditorToolBar::addCenterToolBar(QWidget *toolBar)
{
    toolBar->setVisible(false); // will be made visible in setCurrentEditor
    m_toolBarPlaceholder->layout()->addWidget(toolBar);

    updateToolBar(toolBar);
}

void EditorToolBar::updateToolBar(QWidget *toolBar)
{
    if (!toolBar)
        toolBar = m_defaultToolBar;
    if (m_activeToolBar == toolBar)
        return;
    toolBar->setVisible(true);
    m_activeToolBar->setVisible(false);
    m_activeToolBar = toolBar;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
214
215
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
216
217
218
219
void EditorToolBar::setToolbarCreationFlags(ToolbarCreationFlags flags)
{
    m_ignoreEditorToolbar = flags & FlagsIgnoreIEditorToolBar;
}
Lasse Holmstedt's avatar
Lasse Holmstedt committed
220
221

void EditorToolBar::setCurrentEditor(IEditor *editor)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
222
{
Lasse Holmstedt's avatar
Lasse Holmstedt committed
223
224
225
226
227
228
229
230
    m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());

    // If we never added the toolbar from the editor,  we will never change
    // the editor, so there's no need to update the toolbar either.
    if (!m_ignoreEditorToolbar)
        updateToolBar(editor->toolBar());

    updateEditorStatus(editor);
231
}
Lasse Holmstedt's avatar
Lasse Holmstedt committed
232

Lasse Holmstedt's avatar
Lasse Holmstedt committed
233
void EditorToolBar::updateEditorListSelection(IEditor *newSelection)
234
235
236
{
    if (newSelection) {
        m_editorList->setCurrentIndex(m_editorsListModel->indexOf(newSelection).row());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
237
238
239
    }
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
240
void EditorToolBar::listSelectionActivated(int row)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
241
{
242
243
    EditorManager *em = ICore::instance()->editorManager();
    QAbstractItemModel *model = m_editorList->model();
Lasse Holmstedt's avatar
Lasse Holmstedt committed
244
    const QModelIndex modelIndex = model->index(row, 0);
245
    IEditor *editor = model->data(modelIndex, Qt::UserRole).value<IEditor*>();
Lasse Holmstedt's avatar
Lasse Holmstedt committed
246
247
248
249
    if (editor) {
        if (editor != em->currentEditor())
            em->activateEditor(editor, EditorManager::NoModeSwitch);
    } else {
Lasse Holmstedt's avatar
Lasse Holmstedt committed
250
        //em->activateEditor(model->index(index, 0), this);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
251
252
253
254
255
256
257
258
259
        QString fileName = model->data(modelIndex, Qt::UserRole + 1).toString();
        QByteArray kind = model->data(modelIndex, Qt::UserRole + 2).toByteArray();
        editor = em->openEditor(fileName, kind, EditorManager::NoModeSwitch);
    }
    if (editor) {
        m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());
    }
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
260
void EditorToolBar::listContextMenu(QPoint pos)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
261
262
263
264
265
266
267
268
269
270
271
272
{
    QModelIndex index = m_editorsListModel->index(m_editorList->currentIndex(), 0);
    QString fileName = m_editorsListModel->data(index, Qt::UserRole + 1).toString();
    if (fileName.isEmpty())
        return;
    QMenu menu;
    menu.addAction(tr("Copy full path to clipboard"));
    if (menu.exec(m_editorList->mapToGlobal(pos))) {
        QApplication::clipboard()->setText(fileName);
    }
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
void EditorToolBar::makeEditorWritable()
{
    if (currentEditor())
        ICore::instance()->editorManager()->makeEditorWritable(currentEditor());
}

void EditorToolBar::setCanGoBack(bool canGoBack)
{
    m_goBackAction->setEnabled(canGoBack);
}

void EditorToolBar::setCanGoForward(bool canGoForward)
{
    m_goForwardAction->setEnabled(canGoForward);
}

void EditorToolBar::updateActionShortcuts()
Lasse Holmstedt's avatar
Lasse Holmstedt committed
290
{
Lasse Holmstedt's avatar
Lasse Holmstedt committed
291
292
293
294
    ActionManager *am = ICore::instance()->actionManager();
    m_closeButton->setToolTip(am->command(Constants::CLOSE)->stringWithAppendedShortcut(EditorManager::tr("Close")));
    m_goBackAction->setToolTip(am->command(Constants::GO_BACK)->action()->toolTip());
    m_goForwardAction->setToolTip(am->command(Constants::GO_FORWARD)->action()->toolTip());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
295
296
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
297
IEditor *EditorToolBar::currentEditor() const
Lasse Holmstedt's avatar
Lasse Holmstedt committed
298
{
Lasse Holmstedt's avatar
Lasse Holmstedt committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    return ICore::instance()->editorManager()->currentEditor();
}

void EditorToolBar::checkEditorStatus()
{
    IEditor *editor = qobject_cast<IEditor *>(sender());
    IEditor *current = currentEditor();

    if (current == editor)
        updateEditorStatus(editor);
}

void EditorToolBar::updateEditorStatus(IEditor *editor)
{
    m_lockButton->setVisible(editor != 0);
    m_closeButton->setEnabled(editor != 0);

    if (!editor || !editor->file()) {
        m_editorList->setToolTip(QString());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
318
        return;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
319
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
320

Lasse Holmstedt's avatar
Lasse Holmstedt committed
321
322
323
324
325
    m_editorList->setCurrentIndex(m_editorsListModel->indexOf(editor).row());

    if (editor->file()->isReadOnly()) {
        m_lockButton->setIcon(QIcon(m_editorsListModel->lockedIcon()));
        m_lockButton->setEnabled(!editor->file()->fileName().isEmpty());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
326
327
        m_lockButton->setToolTip(tr("Make writable"));
    } else {
Lasse Holmstedt's avatar
Lasse Holmstedt committed
328
        m_lockButton->setIcon(QIcon(m_editorsListModel->unlockedIcon()));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
329
330
331
        m_lockButton->setEnabled(false);
        m_lockButton->setToolTip(tr("File is writable"));
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    if (editor == currentEditor())
        m_editorList->setToolTip(
                currentEditor()->file()->fileName().isEmpty()
                ? currentEditor()->displayName()
                    : QDir::toNativeSeparators(editor->file()->fileName())
                    );

}

void EditorToolBar::setNavigationVisible(bool isVisible)
{
    m_goBackAction->setVisible(isVisible);
    m_goForwardAction->setVisible(isVisible);
    m_backButton->setVisible(isVisible);
    m_forwardButton->setVisible(isVisible);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
347
348
}

349
} // Core