openeditorsview.cpp 6.41 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
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
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
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 "icore.h"
con's avatar
con committed
34
35
36
37

#include <coreplugin/coreconstants.h>
#include <coreplugin/filemanager.h>
#include <coreplugin/uniqueidmanager.h>
hjk's avatar
hjk committed
38
#include <utils/qtcassert.h>
con's avatar
con committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#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;

OpenEditorsWidget::OpenEditorsWidget()
{
    m_ui.setupUi(this);
    setWindowTitle(tr("Open Documents"));
    setWindowIcon(QIcon(Constants::ICON_DIR));
    setFocusProxy(m_ui.editorList);
    m_ui.editorList->header()->hide();
    m_ui.editorList->setIndentation(0);
    m_ui.editorList->setSelectionMode(QAbstractItemView::ExtendedSelection);
    m_ui.editorList->setTextElideMode(Qt::ElideMiddle);
    m_ui.editorList->installEventFilter(this);
    m_ui.editorList->setFrameStyle(QFrame::NoFrame);
con's avatar
con committed
68
    m_ui.editorList->setAttribute(Qt::WA_MacShowFocusRect, false);
con's avatar
con committed
69
    EditorManager *em = EditorManager::instance();
70
    m_ui.editorList->setModel(em->openedEditorsModel());
con's avatar
con committed
71
    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
72
            this, SLOT(updateCurrentItem(Core::IEditor*)));
73
74
    connect(m_ui.editorList, SIGNAL(clicked(QModelIndex)),
            this, SLOT(selectEditor(QModelIndex)));
75
    //updateEditorList();
con's avatar
con committed
76
77
78
79
80
81
}

OpenEditorsWidget::~OpenEditorsWidget()
{
}

82
void OpenEditorsWidget::updateCurrentItem(Core::IEditor *editor)
con's avatar
con committed
83
{
84
85
86
    if (!editor) {
        m_ui.editorList->clearSelection();
        return;
con's avatar
con committed
87
88
    }
    EditorManager *em = EditorManager::instance();
89
    m_ui.editorList->clearSelection(); //we are in extended selectionmode
90
91
    m_ui.editorList->setCurrentIndex(em->openedEditorsModel()->indexOf(editor));
    m_ui.editorList->scrollTo(m_ui.editorList->currentIndex());
con's avatar
con committed
92
93
}

94
void OpenEditorsWidget::selectEditor(const QModelIndex &index)
con's avatar
con committed
95
{
96
    IEditor *editor = index.data(Qt::UserRole).value<IEditor*>();
97
    EditorManager::instance()->activateEditor(editor);
con's avatar
con committed
98
99
}

100
101

void OpenEditorsWidget::selectEditor()
con's avatar
con committed
102
{
103
    selectEditor(m_ui.editorList->currentIndex());
con's avatar
con committed
104
105
106
107
}

void OpenEditorsWidget::closeEditors()
{
108
    /* ### TODO
con's avatar
con committed
109
110
111
112
113
114
    QList<IFile *> selectedFiles;
    QList<IEditor *> selectedEditors;
    foreach (QTreeWidgetItem *item, m_ui.editorList->selectedItems()) {
        selectedEditors.append(item->data(0, Qt::UserRole).value<IEditor *>());
        selectedFiles.append(item->data(0, Qt::UserRole).value<IEditor *>()->file());
    }
115
    ICore *core = ICore::instance();
con's avatar
con committed
116
117
118
119
    bool cancelled = false;
    core->fileManager()->saveModifiedFiles(selectedFiles, &cancelled);
    if (cancelled)
        return;
120
    core->editorManager()->closeEditors(selectedEditors);
con's avatar
con committed
121
    updateEditorList();
122
    */
con's avatar
con committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
}

void OpenEditorsWidget::closeAllEditors()
{
    m_ui.editorList->selectAll();
    closeEditors();
}


bool OpenEditorsWidget::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == m_ui.editorList) {
        if (event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            switch (keyEvent->key()) {
                case Qt::Key_Return:
139
                    selectEditor(m_ui.editorList->currentIndex());
con's avatar
con committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
                    return true;
                case Qt::Key_Delete: //fall through
                case Qt::Key_Backspace:
                    if (keyEvent->modifiers() == Qt::NoModifier) {
                        closeEditors();
                        return true;
                    }
                    break;
                default:
                    break;
            }
        }
        if (event->type() == QEvent::ContextMenu) {
            QContextMenuEvent *contextMenuEvent = static_cast<QContextMenuEvent *>(event);
            QMenu menu;
            menu.addAction(tr("&Select"), this, SLOT(selectEditor()));
156
157
158
//todo      menu.addAction(tr("&Close"), this, SLOT(closeEditors()));
//todo      menu.addAction(tr("Close &All"), this, SLOT(closeAllEditors()));
            if (m_ui.editorList->selectionModel()->selectedIndexes().isEmpty())
con's avatar
con committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
                menu.setEnabled(false);
            menu.exec(contextMenuEvent->globalPos());
            return true;
        }
    } else if (obj == m_widget) {
        if (event->type() == QEvent::FocusIn) {
            QFocusEvent *e = static_cast<QFocusEvent *>(event);
            if (e->reason() != Qt::MouseFocusReason) {
                // we should not set the focus in a event filter for a focus event,
                // so do it when the focus event is processed
                QTimer::singleShot(0, this, SLOT(putFocusToEditorList()));
            }
        }
    }
    return false;
}

void OpenEditorsWidget::putFocusToEditorList()
{
    m_ui.editorList->setFocus();
}

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

QString OpenEditorsViewFactory::displayName()
{
    return "Open Documents";
}

QKeySequence OpenEditorsViewFactory::activationSequence()
{
    return QKeySequence(Qt::ALT + Qt::Key_O);
}

OpenEditorsViewFactory::OpenEditorsViewFactory()
{
}

OpenEditorsViewFactory::~OpenEditorsViewFactory()
{
}