openeditorsview.cpp 10.2 KB
Newer Older
con's avatar
con committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
** 
** Non-Open Source Usage  
** 
** 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
** agreement between you and Nokia.  
** 
** GNU General Public License Usage 
** 
** 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
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.  
** 
***************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34
35
36
37
38
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "openeditorsview.h"
#include "editorgroup.h"
#include "editormanager.h"
#include "coreimpl.h"

#include <coreplugin/coreconstants.h>
#include <coreplugin/filemanager.h>
#include <coreplugin/uniqueidmanager.h>

#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->setColumnCount(1);
    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();
    foreach(IEditor *editor, em->openedEditors()) {
        registerEditor(editor);
    }
    connect(em, SIGNAL(editorOpened(Core::IEditor*)),
            this, SLOT(registerEditor(Core::IEditor*)));
    connect(em, SIGNAL(editorsClosed(QList<Core::IEditor*>)),
            this, SLOT(unregisterEditors(QList<Core::IEditor*>)));
    connect(em, SIGNAL(editorGroupsChanged()),
            this, SLOT(updateEditorList()));
    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
            this, SLOT(updateCurrentItem()));
    connect(m_ui.editorList, SIGNAL(itemActivated(QTreeWidgetItem*, int)),
            this, SLOT(selectEditor(QTreeWidgetItem*)));
    updateEditorList();
}

OpenEditorsWidget::~OpenEditorsWidget()
{
    
}

void OpenEditorsWidget::registerEditor(IEditor *editor)
{
    connect(editor, SIGNAL(changed()), this, SLOT(updateEditor()));
    updateEditorList();
}

void OpenEditorsWidget::unregisterEditors(QList<IEditor *> editors)
{
    foreach (IEditor *editor, editors)
        disconnect(editor, SIGNAL(changed()), this, SLOT(updateEditor()));
    updateEditorList();
}

void OpenEditorsWidget::updateEditorList()
{
    EditorManager *em = EditorManager::instance();
    QList<EditorGroup *> groups = em->editorGroups();
    IEditor *curEditor = em->currentEditor();
    int oldNum = m_ui.editorList->topLevelItemCount();
    QTreeWidgetItem *currentItem = 0;
    int currItemIndex = 0;
    for (int i = 0; i < groups.count(); ++i) {
        QTreeWidgetItem *item;
        if (groups.count() > 1) {
            if (currItemIndex < oldNum) {
                item = m_ui.editorList->topLevelItem(currItemIndex);
            } else {
                item = new QTreeWidgetItem(QStringList()<<"");
                m_ui.editorList->addTopLevelItem(item);
            }
            currItemIndex++;
            item->setIcon(0, QIcon());
            item->setText(0, tr("---Group %1---").arg(i));
            item->setFlags(0);
            item->setToolTip(0, "");
            item->setData(0, Qt::UserRole, QVariant());
            item->setTextAlignment(0, Qt::AlignLeft);
        }
        foreach (IEditor *editor, groups.at(i)->editors()) {
            if (currItemIndex < oldNum) {
                item = m_ui.editorList->topLevelItem(currItemIndex);
            } else {
                item = new QTreeWidgetItem(QStringList()<<"");
                m_ui.editorList->addTopLevelItem(item);
            }
            currItemIndex++;
            updateItem(item, editor);
            if (editor == curEditor)
                currentItem = item;
        }
    }
    for (int i = oldNum-1; i >= currItemIndex; --i) {
        delete m_ui.editorList->takeTopLevelItem(i);
    }
    updateCurrentItem(currentItem);
}

void OpenEditorsWidget::updateCurrentItem(QTreeWidgetItem *currentItem)
{
    EditorManager *em = EditorManager::instance();
    IEditor *curEditor = em->currentEditor();
    m_ui.editorList->clearSelection();
    if (!currentItem && curEditor) {
        int count = m_ui.editorList->topLevelItemCount();
        for (int i = 0; i < count; ++i) {
            if (m_ui.editorList->topLevelItem(i)->data(0, Qt::UserRole).value<IEditor *>()
                    == curEditor) {
                currentItem = m_ui.editorList->topLevelItem(i);
                break;
            }
        }
    }
    m_ui.editorList->setCurrentItem(currentItem);
    if (currentItem)
        m_ui.editorList->scrollTo(m_ui.editorList->currentIndex());
}

//todo: this is almost duplicated in openeditorswindow
void OpenEditorsWidget::updateItem(QTreeWidgetItem *item, IEditor *editor)
{
    static const QIcon lockedIcon(QLatin1String(":/qworkbench/images/locked.png"));
    static const QIcon emptyIcon(QLatin1String(":/qworkbench/images/empty14.png"));
    QString title = editor->displayName();
    if (editor->file()->isModified())
        title += tr("*");
    item->setIcon(0, editor->file()->isReadOnly() ? lockedIcon : emptyIcon);
    item->setText(0, title);
    item->setToolTip(0, editor->file()->fileName());
    item->setData(0, Qt::UserRole, QVariant::fromValue(editor));
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    item->setTextAlignment(0, Qt::AlignLeft);
}

void OpenEditorsWidget::selectEditor(QTreeWidgetItem *item)
{
    if (item == 0)
        item = m_ui.editorList->currentItem();
    if (item == 0)
        return;
    IEditor *editor = item->data(0, Qt::UserRole).value<IEditor*>();
    EditorManager::instance()->setCurrentEditor(editor);
}

void OpenEditorsWidget::updateEditor()
{
    IEditor *editor = qobject_cast<IEditor *>(sender());
    Q_ASSERT(editor);
    int num = m_ui.editorList->topLevelItemCount();
    for (int i = 0; i < num; ++i) {
        QTreeWidgetItem *item = m_ui.editorList->topLevelItem(i);
        if (item->data(0, Qt::UserRole).value<IEditor *>()
                == editor) {
            updateItem(item, editor);
            return;
        }
    }
}

void OpenEditorsWidget::closeEditors()
{
    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());
    }
    ICore *core = CoreImpl::instance();
    bool cancelled = false;
    core->fileManager()->saveModifiedFiles(selectedFiles, &cancelled);
    if (cancelled)
        return;
    core->editorManager()->
            closeEditors(selectedEditors);
    updateEditorList();
}

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:
                    selectEditor(m_ui.editorList->currentItem());
                    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()));
            menu.addAction(tr("&Close"), this, SLOT(closeEditors()));
            menu.addAction(tr("Close &All"), this, SLOT(closeAllEditors()));
            if (m_ui.editorList->selectedItems().isEmpty())
                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()
{
}