openeditorsview.cpp 8.38 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
hjk's avatar
hjk committed
3 4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30 31
#include "openeditorsview.h"
#include "editormanager.h"
32
#include "editorview.h"
33
#include "openeditorsmodel.h"
34
#include "icore.h"
con's avatar
con committed
35 36

#include <coreplugin/coreconstants.h>
37
#include <coreplugin/editormanager/ieditor.h>
38
#include <coreplugin/documentmanager.h>
39
#include <coreplugin/id.h>
40
#include <coreplugin/actionmanager/actionmanager.h>
41
#include <coreplugin/actionmanager/command.h>
hjk's avatar
hjk committed
42
#include <utils/qtcassert.h>
con's avatar
con committed
43

44 45
#include <QApplication>
#include <QGridLayout>
46 47 48 49 50 51 52
#include <QTimer>
#include <QMenu>
#include <QPainter>
#include <QStyle>
#include <QStyleOption>
#include <QHeaderView>
#include <QKeyEvent>
53
#include <QTreeView>
54
#ifdef Q_OS_MAC
con's avatar
con committed
55 56 57 58 59 60
#include <qmacstyle_mac.h>
#endif

using namespace Core;
using namespace Core::Internal;

61 62 63 64 65 66 67 68 69 70

OpenEditorsDelegate::OpenEditorsDelegate(QObject *parent)
 : QStyledItemDelegate(parent)
{
}

void OpenEditorsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
           const QModelIndex &index) const
{
    if (option.state & QStyle::State_MouseOver) {
71 72 73 74 75 76
        if ((QApplication::mouseButtons() & Qt::LeftButton) == 0)
            pressedIndex = QModelIndex();
        QBrush brush = option.palette.alternateBase();
        if (index == pressedIndex)
            brush = option.palette.dark();
        painter->fillRect(option.rect, brush);
77 78
    }

mae's avatar
mae committed
79

80 81 82
    QStyledItemDelegate::paint(painter, option, index);

    if (index.column() == 1 && option.state & QStyle::State_MouseOver) {
83 84
        const QIcon icon(QLatin1String((option.state & QStyle::State_Selected) ?
                                       Constants::ICON_CLOSE : Constants::ICON_CLOSE_DARK));
85 86 87 88 89 90

        QRect iconRect(option.rect.right() - option.rect.height(),
                       option.rect.top(),
                       option.rect.height(),
                       option.rect.height());

91
        icon.paint(painter, iconRect, Qt::AlignRight | Qt::AlignVCenter);
92 93 94 95
    }

}

96 97 98 99
////
// OpenEditorsWidget
////

con's avatar
con committed
100 101 102
OpenEditorsWidget::OpenEditorsWidget()
{
    setWindowTitle(tr("Open Documents"));
103
    setWindowIcon(QIcon(QLatin1String(Constants::ICON_DIR)));
104 105 106 107 108 109 110 111
    setUniformRowHeights(true);
    viewport()->setAttribute(Qt::WA_Hover);
    setItemDelegate((m_delegate = new OpenEditorsDelegate(this)));
    header()->hide();
    setIndentation(0);
    setTextElideMode(Qt::ElideMiddle);
    setFrameStyle(QFrame::NoFrame);
    setAttribute(Qt::WA_MacShowFocusRect, false);
con's avatar
con committed
112
    EditorManager *em = EditorManager::instance();
113 114 115 116 117 118 119 120 121 122
    setModel(em->openedEditorsModel());
    setSelectionMode(QAbstractItemView::SingleSelection);
    setSelectionBehavior(QAbstractItemView::SelectRows);
    header()->setStretchLastSection(false);
    header()->setResizeMode(0, QHeaderView::Stretch);
    header()->setResizeMode(1, QHeaderView::Fixed);
    header()->resizeSection(1, 16);
    setContextMenuPolicy(Qt::CustomContextMenu);
    installEventFilter(this);
    viewport()->installEventFilter(this);
123

con's avatar
con committed
124
    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
125
            this, SLOT(updateCurrentItem(Core::IEditor*)));
126
    connect(this, SIGNAL(clicked(QModelIndex)),
127
            this, SLOT(handleClicked(QModelIndex)));
128
    connect(this, SIGNAL(pressed(QModelIndex)),
129
            this, SLOT(handlePressed(QModelIndex)));
130

131
    connect(this, SIGNAL(customContextMenuRequested(QPoint)),
132
            this, SLOT(contextMenuRequested(QPoint)));
con's avatar
con committed
133 134 135 136 137 138
}

OpenEditorsWidget::~OpenEditorsWidget()
{
}

139
void OpenEditorsWidget::updateCurrentItem(Core::IEditor *editor)
con's avatar
con committed
140
{
141
    if (!editor) {
142
        clearSelection();
143
        return;
con's avatar
con committed
144 145
    }
    EditorManager *em = EditorManager::instance();
146 147
    setCurrentIndex(em->openedEditorsModel()->indexOf(editor));
    selectionModel()->select(currentIndex(),
148
                                              QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
149
    scrollTo(currentIndex());
con's avatar
con committed
150 151
}

152 153
bool OpenEditorsWidget::eventFilter(QObject *obj, QEvent *event)
{
154 155
    if (obj == this && event->type() == QEvent::KeyPress
            && currentIndex().isValid()) {
156 157 158 159
        QKeyEvent *ke = static_cast<QKeyEvent*>(event);
        if ((ke->key() == Qt::Key_Return
                || ke->key() == Qt::Key_Enter)
                && ke->modifiers() == 0) {
160
            activateEditor(currentIndex());
161 162 163 164
            return true;
        } else if ((ke->key() == Qt::Key_Delete
                   || ke->key() == Qt::Key_Backspace)
                && ke->modifiers() == 0) {
165
            closeEditor(currentIndex());
166
        }
167
    } else if (obj == viewport()
168 169 170 171
             && event->type() == QEvent::MouseButtonRelease) {
        QMouseEvent * me = static_cast<QMouseEvent*>(event);
        if (me->button() == Qt::MiddleButton
                && me->modifiers() == Qt::NoModifier) {
172
            QModelIndex index = indexAt(me->pos());
173 174 175 176 177
            if (index.isValid()) {
                closeEditor(index);
                return true;
            }
        }
178 179 180 181
    }
    return false;
}

182 183 184
void OpenEditorsWidget::handlePressed(const QModelIndex &index)
{
    if (index.column() == 0) {
185
        activateEditor(index);
186 187 188 189 190 191
    } else if (index.column() == 1) {
        m_delegate->pressedIndex = index;
    }
}

void OpenEditorsWidget::handleClicked(const QModelIndex &index)
con's avatar
con committed
192
{
193
    if (index.column() == 1) { // the funky close button
194
        closeEditor(index);
195 196 197

        // work around a bug in itemviews where the delegate wouldn't get the QStyle::State_MouseOver
        QPoint cursorPos = QCursor::pos();
198
        QWidget *vp = viewport();
199 200
        QMouseEvent e(QEvent::MouseMove, vp->mapFromGlobal(cursorPos), cursorPos, Qt::NoButton, 0, 0);
        QCoreApplication::sendEvent(vp, &e);
con's avatar
con committed
201 202 203
    }
}

204 205
void OpenEditorsWidget::activateEditor(const QModelIndex &index)
{
206
    selectionModel()->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
207
    EditorManager::instance()->activateEditorForIndex(index, EditorManager::ModeSwitch);
208 209 210 211 212 213
}

void OpenEditorsWidget::closeEditor(const QModelIndex &index)
{
    EditorManager::instance()->closeEditor(index);
    // work around selection changes
hjk's avatar
hjk committed
214
    updateCurrentItem(EditorManager::currentEditor());
215 216
}

217 218 219
void OpenEditorsWidget::contextMenuRequested(QPoint pos)
{
    QMenu contextMenu;
220
    QModelIndex editorIndex = indexAt(pos);
Robert Loehning's avatar
Robert Loehning committed
221 222 223
    EditorManager::instance()->addCloseEditorActions(&contextMenu, editorIndex);
    contextMenu.addSeparator();
    EditorManager::instance()->addNativeDirActions(&contextMenu, editorIndex);
224
    contextMenu.exec(mapToGlobal(pos));
225 226 227 228 229
}

///
// OpenEditorsViewFactory
///
con's avatar
con committed
230 231 232 233 234 235 236 237

NavigationView OpenEditorsViewFactory::createWidget()
{
    NavigationView n;
    n.widget = new OpenEditorsWidget();
    return n;
}

238
QString OpenEditorsViewFactory::displayName() const
con's avatar
con committed
239
{
240
    return OpenEditorsWidget::tr("Open Documents");
con's avatar
con committed
241 242
}

243 244 245 246 247
int OpenEditorsViewFactory::priority() const
{
    return 200;
}

248
Core::Id OpenEditorsViewFactory::id() const
249
{
250
    return Core::Id("Open Documents");
251 252 253
}

QKeySequence OpenEditorsViewFactory::activationSequence() const
con's avatar
con committed
254
{
255
    return QKeySequence(Core::UseMacShortcuts ? tr("Meta+O") : tr("Alt+O"));
con's avatar
con committed
256 257 258 259 260 261 262 263 264
}

OpenEditorsViewFactory::OpenEditorsViewFactory()
{
}

OpenEditorsViewFactory::~OpenEditorsViewFactory()
{
}