openeditorsview.cpp 8.9 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
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
37
38

#include <coreplugin/coreconstants.h>
#include <coreplugin/filemanager.h>
#include <coreplugin/uniqueidmanager.h>
39
#include <coreplugin/actionmanager/actionmanager.h>
hjk's avatar
hjk committed
40
#include <utils/qtcassert.h>
con's avatar
con committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#include <QtCore/QTimer>
#include <QtGui/QMenu>
#include <QtGui/QPainter>
#include <QtGui/QStyle>
#include <QtGui/QStyleOption>
#include <QtGui/QHeaderView>
#include <QtGui/QKeyEvent>
#ifdef Q_WS_MAC
#include <qmacstyle_mac.h>
#endif

Q_DECLARE_METATYPE(Core::IEditor*)

using namespace Core;
using namespace Core::Internal;

58
59
60
61
62
63
64
65
66
67

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

void OpenEditorsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
           const QModelIndex &index) const
{
    if (option.state & QStyle::State_MouseOver) {
68
69
70
71
72
73
        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);
74
75
    }

mae's avatar
mae committed
76

77
78
79
    QStyledItemDelegate::paint(painter, option, index);

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

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

88
        icon.paint(painter, iconRect, Qt::AlignRight | Qt::AlignVCenter);
89
90
91
92
    }

}

93
94
95
96
////
// OpenEditorsWidget
////

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

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

    connect(m_ui.editorList, SIGNAL(customContextMenuRequested(QPoint)),
            this, SLOT(contextMenuRequested(QPoint)));
con's avatar
con committed
130
131
132
133
134
135
}

OpenEditorsWidget::~OpenEditorsWidget()
{
}

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

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
bool OpenEditorsWidget::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == m_ui.editorList && event->type() == QEvent::KeyPress
            && m_ui.editorList->currentIndex().isValid()) {
        QKeyEvent *ke = static_cast<QKeyEvent*>(event);
        if ((ke->key() == Qt::Key_Return
                || ke->key() == Qt::Key_Enter)
                && ke->modifiers() == 0) {
            activateEditor(m_ui.editorList->currentIndex());
            return true;
        } else if ((ke->key() == Qt::Key_Delete
                   || ke->key() == Qt::Key_Backspace)
                && ke->modifiers() == 0) {
            closeEditor(m_ui.editorList->currentIndex());
        }
    }
    return false;
}

168
169
170
void OpenEditorsWidget::handlePressed(const QModelIndex &index)
{
    if (index.column() == 0) {
171
        activateEditor(index);
172
173
174
175
176
177
    } else if (index.column() == 1) {
        m_delegate->pressedIndex = index;
    }
}

void OpenEditorsWidget::handleClicked(const QModelIndex &index)
con's avatar
con committed
178
{
179
    if (index.column() == 1) { // the funky close button
180
        closeEditor(index);
181
182
183
184
185
186

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

190
191
192
193
194
195
196
197
198
199
200
201
202
void OpenEditorsWidget::activateEditor(const QModelIndex &index)
{
    m_ui.editorList->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    EditorManager::instance()->activateEditor(index);
}

void OpenEditorsWidget::closeEditor(const QModelIndex &index)
{
    EditorManager::instance()->closeEditor(index);
    // work around selection changes
    updateCurrentItem(EditorManager::instance()->currentEditor());
}

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
void OpenEditorsWidget::contextMenuRequested(QPoint pos)
{
    const QModelIndex index = m_ui.editorList->indexAt(pos);
    QMenu contextMenu;
    QAction *closeEditor = contextMenu.addAction(
            index.isValid() ?  tr("Close %1").arg(index.data().toString())
                            :  tr("Close Editor"));
    QAction *closeOtherEditors = contextMenu.addAction(
            index.isValid() ? tr("Close All Except %1").arg(index.data().toString())
                            : tr("Close Other Editors"));
    QAction *closeAllEditors = contextMenu.addAction(tr("Close All Editors"));

    if (!index.isValid()) {
        closeEditor->setEnabled(false);
        closeOtherEditors->setEnabled(false);
    }

    if (EditorManager::instance()->openedEditors().isEmpty())
        closeAllEditors->setEnabled(false);

    QAction *action = contextMenu.exec(m_ui.editorList->mapToGlobal(pos));
    if (action == 0)
        return;
    if (action == closeEditor)
        EditorManager::instance()->closeEditor(index);
    else if (action == closeAllEditors)
        EditorManager::instance()->closeAllEditors();
    else if (action == closeOtherEditors)
        EditorManager::instance()->closeOtherEditors(index.data(Qt::UserRole).value<Core::IEditor*>());
}

///
// OpenEditorsViewFactory
///
con's avatar
con committed
237
238
239
240
241
242
243
244

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

245
QString OpenEditorsViewFactory::displayName() const
con's avatar
con committed
246
{
247
    return OpenEditorsWidget::tr("Open Documents");
con's avatar
con committed
248
249
}

250
251
252
253
254
255
QString OpenEditorsViewFactory::id() const
{
    return QLatin1String("Open Documents");
}

QKeySequence OpenEditorsViewFactory::activationSequence() const
con's avatar
con committed
256
257
258
259
260
261
262
263
264
265
266
{
    return QKeySequence(Qt::ALT + Qt::Key_O);
}

OpenEditorsViewFactory::OpenEditorsViewFactory()
{
}

OpenEditorsViewFactory::~OpenEditorsViewFactory()
{
}