editortoolbar.cpp 17.6 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Lasse Holmstedt's avatar
Lasse Holmstedt committed
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Lasse Holmstedt's avatar
Lasse Holmstedt committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Lasse Holmstedt's avatar
Lasse Holmstedt committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Lasse Holmstedt's avatar
Lasse Holmstedt committed
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
Lasse Holmstedt's avatar
Lasse Holmstedt committed
25

26
#include "editortoolbar.h"
Lasse Holmstedt's avatar
Lasse Holmstedt committed
27

28
#include <coreplugin/actionmanager/actionmanager.h>
29
#include <coreplugin/actionmanager/command.h>
30
#include <coreplugin/coreicons.h>
31
#include <coreplugin/editormanager/documentmodel.h>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
32
#include <coreplugin/editormanager/editormanager.h>
33
#include <coreplugin/editormanager/editormanager_p.h>
34 35 36
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/icore.h>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
37

38
#include <utils/fileutils.h>
39
#include <utils/hostosinfo.h>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
40
#include <utils/qtcassert.h>
41
#include <utils/utilsicons.h>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
42

43 44
#include <QApplication>
#include <QComboBox>
45 46 47
#include <QDir>
#include <QDrag>
#include <QLabel>
48
#include <QMenu>
49 50 51 52 53 54 55
#include <QMimeData>
#include <QMouseEvent>
#include <QTimer>
#include <QToolButton>
#include <QVBoxLayout>

#include <QDebug>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
56 57 58 59 60

enum {
    debug = false
};

61
namespace Core {
Lasse Holmstedt's avatar
Lasse Holmstedt committed
62

hjk's avatar
hjk committed
63 64
struct EditorToolBarPrivate
{
65 66 67
    explicit EditorToolBarPrivate(QWidget *parent, EditorToolBar *q);

    QComboBox *m_editorList;
68
    QToolButton *m_closeEditorButton;
69
    QToolButton *m_lockButton;
70
    QToolButton *m_dragHandle;
71 72
    QMenu *m_dragHandleMenu;
    EditorToolBar::MenuProvider m_menuProvider;
73 74 75 76
    QAction *m_goBackAction;
    QAction *m_goForwardAction;
    QToolButton *m_backButton;
    QToolButton *m_forwardButton;
77 78 79
    QToolButton *m_splitButton;
    QAction *m_horizontalSplitAction;
    QAction *m_verticalSplitAction;
80
    QAction *m_splitNewWindowAction;
81
    QToolButton *m_closeSplitButton;
82 83 84 85 86

    QWidget *m_activeToolBar;
    QWidget *m_toolBarPlaceholder;
    QWidget *m_defaultToolBar;

87 88
    QPoint m_dragStartPosition;

89 90 91 92 93
    bool m_isStandalone;
};

EditorToolBarPrivate::EditorToolBarPrivate(QWidget *parent, EditorToolBar *q) :
    m_editorList(new QComboBox(q)),
94 95 96
    m_closeEditorButton(new QToolButton(q)),
    m_lockButton(new QToolButton(q)),
    m_dragHandle(new QToolButton(q)),
97
    m_dragHandleMenu(0),
98 99
    m_goBackAction(new QAction(Utils::Icons::PREV_TOOLBAR.icon(), EditorManager::tr("Go Back"), parent)),
    m_goForwardAction(new QAction(Utils::Icons::NEXT_TOOLBAR.icon(), EditorManager::tr("Go Forward"), parent)),
100 101
    m_backButton(new QToolButton(q)),
    m_forwardButton(new QToolButton(q)),
102
    m_splitButton(new QToolButton(q)),
103 104
    m_horizontalSplitAction(new QAction(Icons::SPLIT_HORIZONTAL.icon(), EditorManager::tr("Split"), parent)),
    m_verticalSplitAction(new QAction(Icons::SPLIT_VERTICAL.icon(), EditorManager::tr("Split Side by Side"), parent)),
105
    m_splitNewWindowAction(new QAction(EditorManager::tr("Open in New Window"), parent)),
106
    m_closeSplitButton(new QToolButton(q)),
107
    m_activeToolBar(0),
108
    m_toolBarPlaceholder(new QWidget(q)),
109 110 111 112 113
    m_defaultToolBar(new QWidget(q)),
    m_isStandalone(false)
{
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
114 115 116
/*!
  Mimic the look of the text editor toolbar as defined in e.g. EditorView::EditorView
  */
Lasse Holmstedt's avatar
Lasse Holmstedt committed
117
EditorToolBar::EditorToolBar(QWidget *parent) :
118
        Utils::StyledBar(parent), d(new EditorToolBarPrivate(parent, this))
Lasse Holmstedt's avatar
Lasse Holmstedt committed
119
{
Lasse Holmstedt's avatar
Lasse Holmstedt committed
120 121 122
    QHBoxLayout *toolBarLayout = new QHBoxLayout(this);
    toolBarLayout->setMargin(0);
    toolBarLayout->setSpacing(0);
123 124 125
    toolBarLayout->addWidget(d->m_defaultToolBar);
    d->m_toolBarPlaceholder->setLayout(toolBarLayout);
    d->m_toolBarPlaceholder->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
126

127 128
    d->m_defaultToolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    d->m_activeToolBar = d->m_defaultToolBar;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
129

130 131 132
    d->m_lockButton->setAutoRaise(true);
    d->m_lockButton->setEnabled(false);

133
    d->m_dragHandle->setProperty("noArrow", true);
134 135
    d->m_dragHandle->setToolTip(tr("Drag to drag documents between splits"));
    d->m_dragHandle->installEventFilter(this);
136 137
    d->m_dragHandleMenu = new QMenu(d->m_dragHandle);
    d->m_dragHandle->setMenu(d->m_dragHandleMenu);
138

Orgad Shaneh's avatar
Orgad Shaneh committed
139 140
    connect(d->m_goBackAction, &QAction::triggered, this, &EditorToolBar::goBackClicked);
    connect(d->m_goForwardAction, &QAction::triggered, this, &EditorToolBar::goForwardClicked);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
141

142
    d->m_editorList->setProperty("hideicon", true);
143
    d->m_editorList->setProperty("notelideasterisk", true);
144 145
    d->m_editorList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    d->m_editorList->setMinimumContentsLength(20);
hjk's avatar
hjk committed
146
    d->m_editorList->setModel(DocumentModel::model());
147 148
    d->m_editorList->setMaxVisibleItems(40);
    d->m_editorList->setContextMenuPolicy(Qt::CustomContextMenu);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
149

150
    d->m_closeEditorButton->setAutoRaise(true);
151
    d->m_closeEditorButton->setIcon(Icons::CLOSE_TOOLBAR.icon());
152
    d->m_closeEditorButton->setEnabled(false);
153
    d->m_closeEditorButton->setProperty("showborder", true);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
154

155
    d->m_toolBarPlaceholder->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
156

157
    d->m_backButton->setDefaultAction(d->m_goBackAction);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
158

159
    d->m_forwardButton->setDefaultAction(d->m_goForwardAction);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
160

161 162 163
    if (Utils::HostOsInfo::isMacHost()) {
        d->m_horizontalSplitAction->setIconVisibleInMenu(false);
        d->m_verticalSplitAction->setIconVisibleInMenu(false);
164
        d->m_splitNewWindowAction->setIconVisibleInMenu(false);
165
    }
166

167
    d->m_splitButton->setIcon(Icons::SPLIT_HORIZONTAL_TOOLBAR.icon());
168 169
    d->m_splitButton->setToolTip(tr("Split"));
    d->m_splitButton->setPopupMode(QToolButton::InstantPopup);
Eike Ziller's avatar
Eike Ziller committed
170
    d->m_splitButton->setProperty("noArrow", true);
171 172 173
    QMenu *splitMenu = new QMenu(d->m_splitButton);
    splitMenu->addAction(d->m_horizontalSplitAction);
    splitMenu->addAction(d->m_verticalSplitAction);
174
    splitMenu->addAction(d->m_splitNewWindowAction);
175 176 177
    d->m_splitButton->setMenu(splitMenu);

    d->m_closeSplitButton->setAutoRaise(true);
178
    d->m_closeSplitButton->setIcon(Icons::CLOSE_SPLIT_BOTTOM.icon());
179

Lasse Holmstedt's avatar
Lasse Holmstedt committed
180 181 182
    QHBoxLayout *toplayout = new QHBoxLayout(this);
    toplayout->setSpacing(0);
    toplayout->setMargin(0);
183 184
    toplayout->addWidget(d->m_backButton);
    toplayout->addWidget(d->m_forwardButton);
185
    toplayout->addWidget(d->m_lockButton);
186
    toplayout->addWidget(d->m_dragHandle);
187
    toplayout->addWidget(d->m_editorList);
188
    toplayout->addWidget(d->m_closeEditorButton);
189 190 191
    toplayout->addWidget(d->m_toolBarPlaceholder, 1); // Custom toolbar stretches
    toplayout->addWidget(d->m_splitButton);
    toplayout->addWidget(d->m_closeSplitButton);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
192

Lasse Holmstedt's avatar
Lasse Holmstedt committed
193 194
    setLayout(toplayout);

195 196
    // this signal is disconnected for standalone toolbars and replaced with
    // a private slot connection
Orgad Shaneh's avatar
Orgad Shaneh committed
197 198
    connect(d->m_editorList, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
            this, &EditorToolBar::listSelectionActivated);
199

200 201 202 203 204 205 206 207 208
    connect(d->m_editorList, &QComboBox::customContextMenuRequested, [this](QPoint p) {
       QMenu menu;
       fillListContextMenu(&menu);
       menu.exec(d->m_editorList->mapToGlobal(p));
    });
    connect(d->m_dragHandleMenu, &QMenu::aboutToShow, [this]() {
       d->m_dragHandleMenu->clear();
       fillListContextMenu(d->m_dragHandleMenu);
    });
Orgad Shaneh's avatar
Orgad Shaneh committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    connect(d->m_lockButton, &QAbstractButton::clicked, this, &EditorToolBar::makeEditorWritable);
    connect(d->m_closeEditorButton, &QAbstractButton::clicked,
            this, &EditorToolBar::closeEditor, Qt::QueuedConnection);
    connect(d->m_horizontalSplitAction, &QAction::triggered,
            this, &EditorToolBar::horizontalSplitClicked, Qt::QueuedConnection);
    connect(d->m_verticalSplitAction, &QAction::triggered,
            this, &EditorToolBar::verticalSplitClicked, Qt::QueuedConnection);
    connect(d->m_splitNewWindowAction, &QAction::triggered,
            this, &EditorToolBar::splitNewWindowClicked, Qt::QueuedConnection);
    connect(d->m_closeSplitButton, &QAbstractButton::clicked,
            this, &EditorToolBar::closeSplitClicked, Qt::QueuedConnection);


    connect(ActionManager::command(Constants::CLOSE), &Command::keySequenceChanged,
            this, &EditorToolBar::updateActionShortcuts);
    connect(ActionManager::command(Constants::GO_BACK), &Command::keySequenceChanged,
            this, &EditorToolBar::updateActionShortcuts);
    connect(ActionManager::command(Constants::GO_FORWARD), &Command::keySequenceChanged,
            this, &EditorToolBar::updateActionShortcuts);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
228

229
    updateActionShortcuts();
Lasse Holmstedt's avatar
Lasse Holmstedt committed
230 231
}

232 233
EditorToolBar::~EditorToolBar()
{
hjk's avatar
hjk committed
234
    delete d;
235 236
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
237
void EditorToolBar::removeToolbarForEditor(IEditor *editor)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
238
{
239
    QTC_ASSERT(editor, return);
Orgad Shaneh's avatar
Orgad Shaneh committed
240
    disconnect(editor->document(), &IDocument::changed, this, &EditorToolBar::checkDocumentStatus);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
241 242 243

    QWidget *toolBar = editor->toolBar();
    if (toolBar != 0) {
244 245 246
        if (d->m_activeToolBar == toolBar) {
            d->m_activeToolBar = d->m_defaultToolBar;
            d->m_activeToolBar->setVisible(true);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
247
        }
248
        d->m_toolBarPlaceholder->layout()->removeWidget(toolBar);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
249 250 251 252 253
        toolBar->setVisible(false);
        toolBar->setParent(0);
    }
}

254 255 256 257 258
void EditorToolBar::setCloseSplitEnabled(bool enable)
{
    d->m_closeSplitButton->setVisible(enable);
}

Eike Ziller's avatar
Eike Ziller committed
259 260 261 262 263
void EditorToolBar::setCloseSplitIcon(const QIcon &icon)
{
    d->m_closeSplitButton->setIcon(icon);
}

264
void EditorToolBar::closeEditor()
Lasse Holmstedt's avatar
Lasse Holmstedt committed
265
{
hjk's avatar
hjk committed
266
    if (d->m_isStandalone)
267
        EditorManager::slotCloseCurrentEditorOrDocument();
Lasse Holmstedt's avatar
Lasse Holmstedt committed
268 269 270
    emit closeClicked();
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
271
void EditorToolBar::addEditor(IEditor *editor)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
272
{
273
    QTC_ASSERT(editor, return);
Orgad Shaneh's avatar
Orgad Shaneh committed
274
    connect(editor->document(), &IDocument::changed, this, &EditorToolBar::checkDocumentStatus);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
275 276
    QWidget *toolBar = editor->toolBar();

277
    if (toolBar && !d->m_isStandalone)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
278
        addCenterToolBar(toolBar);
279

280
    updateDocumentStatus(editor->document());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
281 282 283 284
}

void EditorToolBar::addCenterToolBar(QWidget *toolBar)
{
285
    QTC_ASSERT(toolBar, return);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
286
    toolBar->setVisible(false); // will be made visible in setCurrentEditor
287
    d->m_toolBarPlaceholder->layout()->addWidget(toolBar);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
288 289 290 291 292 293 294

    updateToolBar(toolBar);
}

void EditorToolBar::updateToolBar(QWidget *toolBar)
{
    if (!toolBar)
295 296
        toolBar = d->m_defaultToolBar;
    if (d->m_activeToolBar == toolBar)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
297 298
        return;
    toolBar->setVisible(true);
299 300
    d->m_activeToolBar->setVisible(false);
    d->m_activeToolBar = toolBar;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
301 302
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
303 304
void EditorToolBar::setToolbarCreationFlags(ToolbarCreationFlags flags)
{
305 306
    d->m_isStandalone = flags & FlagsStandalone;
    if (d->m_isStandalone) {
Orgad Shaneh's avatar
Orgad Shaneh committed
307 308
        connect(EditorManager::instance(), &EditorManager::currentEditorChanged,
                this, &EditorToolBar::updateEditorListSelection);
309

Orgad Shaneh's avatar
Orgad Shaneh committed
310 311 312 313
        disconnect(d->m_editorList, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
                   this, &EditorToolBar::listSelectionActivated);
        connect(d->m_editorList, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
                this, &EditorToolBar::changeActiveEditor);
314 315
        d->m_splitButton->setVisible(false);
        d->m_closeSplitButton->setVisible(false);
316
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
317
}
Lasse Holmstedt's avatar
Lasse Holmstedt committed
318

319 320 321 322 323
void EditorToolBar::setMenuProvider(const EditorToolBar::MenuProvider &provider)
{
    d->m_menuProvider = provider;
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
324
void EditorToolBar::setCurrentEditor(IEditor *editor)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
325
{
326
    IDocument *document = editor ? editor->document() : 0;
hjk's avatar
hjk committed
327
    d->m_editorList->setCurrentIndex(DocumentModel::rowOfDocument(document));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
328 329 330

    // 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.
331
    if (!d->m_isStandalone)
332
        updateToolBar(editor ? editor->toolBar() : 0);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
333

334
    updateDocumentStatus(document);
335
}
Lasse Holmstedt's avatar
Lasse Holmstedt committed
336

Lasse Holmstedt's avatar
Lasse Holmstedt committed
337
void EditorToolBar::updateEditorListSelection(IEditor *newSelection)
338
{
339
    if (newSelection)
hjk's avatar
hjk committed
340
        d->m_editorList->setCurrentIndex(DocumentModel::rowOfDocument(newSelection->document()));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
341 342
}

343
void EditorToolBar::changeActiveEditor(int row)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
344
{
345
    EditorManager::activateEditorForEntry(DocumentModel::entryAtRow(row));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
346 347
}

348
void EditorToolBar::fillListContextMenu(QMenu *menu)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
349
{
350 351
    if (d->m_menuProvider) {
        d->m_menuProvider(menu);
352
    } else {
353 354 355 356 357 358
        IEditor *editor = EditorManager::currentEditor();
        DocumentModel::Entry *entry = editor ? DocumentModel::entryForDocument(editor->document())
                                             : 0;
        EditorManager::addSaveAndCloseEditorActions(menu, entry, editor);
        menu->addSeparator();
        EditorManager::addNativeDirAndOpenWithActions(menu, entry);
359
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
360 361
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
362 363
void EditorToolBar::makeEditorWritable()
{
364
    if (IDocument *current = EditorManager::currentDocument())
365
        Internal::EditorManagerPrivate::makeFileWritable(current);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
366 367 368 369
}

void EditorToolBar::setCanGoBack(bool canGoBack)
{
370
    d->m_goBackAction->setEnabled(canGoBack);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
371 372 373 374
}

void EditorToolBar::setCanGoForward(bool canGoForward)
{
375
    d->m_goForwardAction->setEnabled(canGoForward);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
376 377 378
}

void EditorToolBar::updateActionShortcuts()
Lasse Holmstedt's avatar
Lasse Holmstedt committed
379
{
Eike Ziller's avatar
Eike Ziller committed
380 381 382 383
    d->m_closeEditorButton->setToolTip(ActionManager::command(Constants::CLOSE)->stringWithAppendedShortcut(EditorManager::tr("Close Document")));
    d->m_goBackAction->setToolTip(ActionManager::command(Constants::GO_BACK)->action()->toolTip());
    d->m_goForwardAction->setToolTip(ActionManager::command(Constants::GO_FORWARD)->action()->toolTip());
    d->m_closeSplitButton->setToolTip(ActionManager::command(Constants::REMOVE_CURRENT_SPLIT)->stringWithAppendedShortcut(tr("Remove Split")));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
384 385
}

386
void EditorToolBar::checkDocumentStatus()
Lasse Holmstedt's avatar
Lasse Holmstedt committed
387
{
388 389
    IDocument *document = qobject_cast<IDocument *>(sender());
    QTC_ASSERT(document, return);
390
    DocumentModel::Entry *entry = DocumentModel::entryAtRow(
391
                d->m_editorList->currentIndex());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
392

393 394
    if (entry && entry->document && entry->document == document)
        updateDocumentStatus(document);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
395 396
}

397
void EditorToolBar::updateDocumentStatus(IDocument *document)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
398
{
399
    d->m_closeEditorButton->setEnabled(document != 0);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
400

401
    if (!document) {
402 403 404
        d->m_lockButton->setIcon(QIcon());
        d->m_lockButton->setEnabled(false);
        d->m_lockButton->setToolTip(QString());
405
        d->m_dragHandle->setIcon(QIcon());
406
        d->m_editorList->setToolTip(QString());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
407
        return;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
408
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
409

hjk's avatar
hjk committed
410
    d->m_editorList->setCurrentIndex(DocumentModel::rowOfDocument(document));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
411

412
    if (document->filePath().isEmpty()) {
413 414 415
        d->m_lockButton->setIcon(QIcon());
        d->m_lockButton->setEnabled(false);
        d->m_lockButton->setToolTip(QString());
416
    } else if (document->isFileReadOnly()) {
417
        const static QIcon locked = Utils::Icons::LOCKED_TOOLBAR.icon();
418
        d->m_lockButton->setIcon(locked);
419
        d->m_lockButton->setEnabled(true);
Leena Miettinen's avatar
Leena Miettinen committed
420
        d->m_lockButton->setToolTip(tr("Make Writable"));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
421
    } else {
422
        const static QIcon unlocked = Utils::Icons::UNLOCKED_TOOLBAR.icon();
423
        d->m_lockButton->setIcon(unlocked);
424 425
        d->m_lockButton->setEnabled(false);
        d->m_lockButton->setToolTip(tr("File is writable"));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
426
    }
427 428 429 430

    if (document->filePath().isEmpty())
        d->m_dragHandle->setIcon(QIcon());
    else
431
        d->m_dragHandle->setIcon(FileIconProvider::icon(document->filePath().toFileInfo()));
432

433 434 435
    d->m_editorList->setToolTip(document->filePath().isEmpty()
                                ? document->displayName()
                                : document->filePath().toUserOutput());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
436 437
}

438 439 440 441 442
bool EditorToolBar::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == d->m_dragHandle) {
        if (event->type() == QEvent::MouseButtonPress) {
            auto me = static_cast<QMouseEvent *>(event);
443
            if (me->buttons() == Qt::LeftButton)
444
                d->m_dragStartPosition = me->pos();
445 446 447 448
            return true; // do not pop up menu already on press
        } else if (event->type() == QEvent::MouseButtonRelease) {
            d->m_dragHandle->showMenu();
            return true;
449 450 451 452 453 454 455 456 457 458 459
        } else if (event->type() == QEvent::MouseMove) {
            auto me = static_cast<QMouseEvent *>(event);
            if (me->buttons() != Qt::LeftButton)
                return Utils::StyledBar::eventFilter(obj, event);
            if ((me->pos() - d->m_dragStartPosition).manhattanLength()
                    < QApplication::startDragDistance())
                return Utils::StyledBar::eventFilter(obj, event);
            DocumentModel::Entry *entry = DocumentModel::entryAtRow(
                        d->m_editorList->currentIndex());
            if (!entry) // no document
                return Utils::StyledBar::eventFilter(obj, event);
460
            auto drag = new QDrag(this);
461
            auto data = new Utils::DropMimeData;
462
            data->addFile(entry->fileName().toString());
463
            drag->setMimeData(data);
464 465 466 467 468 469 470 471 472
            Qt::DropAction action = drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::MoveAction);
            if (action == Qt::MoveAction)
                emit currentDocumentMoved();
            return true;
        }
    }
    return Utils::StyledBar::eventFilter(obj, event);
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
473 474
void EditorToolBar::setNavigationVisible(bool isVisible)
{
475 476 477 478
    d->m_goBackAction->setVisible(isVisible);
    d->m_goForwardAction->setVisible(isVisible);
    d->m_backButton->setVisible(isVisible);
    d->m_forwardButton->setVisible(isVisible);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
479 480
}

481
} // Core