openeditorsview.cpp 6.45 KB
Newer Older
con's avatar
con committed
1
2
3
4
/***************************************************************************
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2008-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
10
11
**
** Non-Open Source Usage
**
con's avatar
con committed
12
13
14
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
15
16
17
18
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
con's avatar
con committed
19
20
21
22
23
24
25
26
27
28
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
29
** rights. These rights are described in the Nokia Qt GPL Exception
30
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
31
**
con's avatar
con committed
32
***************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34
35
#include "openeditorsview.h"
#include "editormanager.h"
36
#include "editorview.h"
37
#include "icore.h"
con's avatar
con committed
38
39
40
41

#include <coreplugin/coreconstants.h>
#include <coreplugin/filemanager.h>
#include <coreplugin/uniqueidmanager.h>
hjk's avatar
hjk committed
42
#include <utils/qtcassert.h>
con's avatar
con committed
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
68
69
70
71
72

#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);
    EditorManager *em = EditorManager::instance();
73
    m_ui.editorList->setModel(em->openedEditorsModel());
con's avatar
con committed
74
    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
75
76
77
            this, SLOT(updateCurrentItem(Core::IEditor*)));
    connect(m_ui.editorList, SIGNAL(activated(QModelIndex)),
            this, SLOT(selectEditor(QModelIndex)));
con's avatar
con committed
78
79
80
81
82
83
84
}

OpenEditorsWidget::~OpenEditorsWidget()
{
    
}

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

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

103
104

void OpenEditorsWidget::selectEditor()
con's avatar
con committed
105
{
106
    selectEditor(m_ui.editorList->currentIndex());
con's avatar
con committed
107
108
109
110
}

void OpenEditorsWidget::closeEditors()
{
111
    /* ### TODO
con's avatar
con committed
112
113
114
115
116
117
    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());
    }
118
    ICore *core = ICore::instance();
con's avatar
con committed
119
120
121
122
123
124
125
    bool cancelled = false;
    core->fileManager()->saveModifiedFiles(selectedFiles, &cancelled);
    if (cancelled)
        return;
    core->editorManager()->
            closeEditors(selectedEditors);
    updateEditorList();
126
    */
con's avatar
con committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
}

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:
143
                    selectEditor(m_ui.editorList->currentIndex());
con's avatar
con committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
                    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()));
160
161
162
//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
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
205
206
207
208
                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()
{
}