openeditorswindow.cpp 8.18 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
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
#include "openeditorswindow.h"
#include "editormanager.h"

#include <QtGui/QHeaderView>

Q_DECLARE_METATYPE(Core::IEditor *)

using namespace Core;
using namespace Core::Internal;

const int OpenEditorsWindow::WIDTH = 300;
const int OpenEditorsWindow::HEIGHT = 200;
const int OpenEditorsWindow::MARGIN = 4;

OpenEditorsWindow::OpenEditorsWindow(QWidget *parent) :
    QWidget(parent, Qt::Popup),
    m_editorList(new QTreeWidget(this)),
    m_current(0)
{
    resize(QSize(WIDTH, HEIGHT));
    m_editorList->setColumnCount(1);
    m_editorList->header()->hide();
    m_editorList->setIndentation(0);
    m_editorList->setSelectionMode(QAbstractItemView::SingleSelection);
    m_editorList->setSelectionBehavior(QAbstractItemView::SelectItems);
    m_editorList->setTextElideMode(Qt::ElideMiddle);
#ifdef Q_WS_MAC
    m_editorList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
#endif
    m_editorList->installEventFilter(this);
    m_editorList->setGeometry(MARGIN, MARGIN, WIDTH-2*MARGIN, HEIGHT-2*MARGIN);

    connect(m_editorList, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
            this, SLOT(editorClicked(QTreeWidgetItem*)));

    m_autoHide.setSingleShot(true);
    connect(&m_autoHide, SIGNAL(timeout()), this, SLOT(selectAndHide()));
}

void OpenEditorsWindow::selectAndHide()
{
    selectEditor(m_editorList->currentItem());
    setVisible(false);
}

void OpenEditorsWindow::setVisible(bool visible)
{
    QWidget::setVisible(visible);
    if (visible) {
        m_autoHide.start(600);
        setFocus();
    }
}

bool OpenEditorsWindow::isCentering()
{
    int internalMargin = m_editorList->viewport()->mapTo(m_editorList, QPoint(0,0)).y();
    QRect rect0 = m_editorList->visualItemRect(m_editorList->topLevelItem(0));
    QRect rect1 = m_editorList->visualItemRect(m_editorList->topLevelItem(m_editorList->topLevelItemCount()-1));
    int height = rect1.y() + rect1.height() - rect0.y();
    height += 2*internalMargin + 2*MARGIN;
    if (height > HEIGHT)
        return true;
    return false;
}


bool OpenEditorsWindow::event(QEvent *e) {
    if (e->type() == QEvent::KeyRelease) {
        QKeyEvent *ke = static_cast<QKeyEvent*>(e);
        m_autoHide.stop();
        if (ke->modifiers() == 0
            /*HACK this is to overcome some event inconsistencies between platforms*/
            || (ke->modifiers() == Qt::AltModifier && (ke->key() == Qt::Key_Alt || ke->key() == -1))) {
            selectAndHide();
        }
    }
    return QWidget::event(e);
}

bool OpenEditorsWindow::eventFilter(QObject *obj, QEvent *e)
{
    if (obj == m_editorList) {
        if (e->type() == QEvent::KeyPress) {
            QKeyEvent *ke = static_cast<QKeyEvent*>(e);
            if (ke->key() == Qt::Key_Escape) {
                m_current = EditorManager::instance()->currentEditor();
                updateSelectedEditor();
                setVisible(false);
                return true;
            }
            if (ke->key() == Qt::Key_Return) {
                selectEditor(m_editorList->currentItem());
                return true;
            }
        }
    }
    return QWidget::eventFilter(obj, e);
}

void OpenEditorsWindow::focusInEvent(QFocusEvent *)
{
    m_editorList->setFocus();
}

void OpenEditorsWindow::selectUpDown(bool up)
{
    int itemCount = m_editorList->topLevelItemCount();
    if (itemCount < 2)
        return;
    int index = m_editorList->indexOfTopLevelItem(m_editorList->currentItem());
    if (index < 0)
        return;
    IEditor *editor = 0;
    int count = 0;
    while (!editor && count < itemCount) {
        if (up) {
            index--;
            if (index < 0)
                index = itemCount-1;
        } else {
            index++;
            if (index >= itemCount)
                index = 0;
        }
        editor = m_editorList->topLevelItem(index)
            ->data(0, Qt::UserRole).value<IEditor *>();
        count++;
    }
    if (editor)
164
        setSelectedEditor(editor);
con's avatar
con committed
165
166
167
168
}

void OpenEditorsWindow::selectPreviousEditor()
{
169
    selectUpDown(false);
con's avatar
con committed
170
171
172
173
}

void OpenEditorsWindow::selectNextEditor()
{
174
    selectUpDown(true);
con's avatar
con committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}

void OpenEditorsWindow::centerOnItem(int selectedIndex)
{
    if (selectedIndex >= 0) {
        QTreeWidgetItem *item;
        int num = m_editorList->topLevelItemCount();
        int rotate = selectedIndex-(num-1)/2;
        for (int i = 0; i < rotate; ++i) {
            item = m_editorList->takeTopLevelItem(0);
            m_editorList->addTopLevelItem(item);
        }
        rotate = -rotate;
        for (int i = 0; i < rotate; ++i) {
            item = m_editorList->takeTopLevelItem(num-1);
            m_editorList->insertTopLevelItem(0, item);
        }
    }
}

195
void OpenEditorsWindow::setEditors(const QList<IEditor *>&editors, IEditor *current)
con's avatar
con committed
196
{
197
198
    static const QIcon lockedIcon(QLatin1String(":/core/images/locked.png"));
    static const QIcon emptyIcon(QLatin1String(":/core/images/empty14.png"));
con's avatar
con committed
199

200
201
    m_editorList->clear();

202
203
    QList<IEditor *> doneList;
    QList<IFile *>doneFileList;
204
    foreach (IEditor *editor, editors) {
205
206
207
208
209
210
211
        if (doneList.contains(editor))
            continue;
        doneList += editor;
        if (!editor->widget()->isVisible() && doneFileList.contains(editor->file()))
            continue;
        doneFileList += editor->file();

212
213
214
215
216
217
218
219
220
        QTreeWidgetItem *item = new QTreeWidgetItem();

        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));
221
        //item->setFlags(Qt::ItemIsSelectable);
222
223
224
225
226
227
228

        item->setTextAlignment(0, Qt::AlignLeft);

        m_editorList->addTopLevelItem(item);

    }
    setSelectedEditor(current);
con's avatar
con committed
229
230
}

231

con's avatar
con committed
232
233
234
235
236
void OpenEditorsWindow::selectEditor(QTreeWidgetItem *item)
{
    IEditor *editor = 0;
    if (item)
        editor = item->data(0, Qt::UserRole).value<IEditor*>();
mae's avatar
mae committed
237
    if (editor)
238
        EditorManager::instance()->activateEditor(editor, EditorManager::ActivateInPlace);
con's avatar
con committed
239
240
241
242
243
244
245
246
}

void OpenEditorsWindow::editorClicked(QTreeWidgetItem *item)
{
    selectEditor(item);
    setFocus();
}

247
248
249
250
251
252
253

void OpenEditorsWindow::setSelectedEditor(IEditor *editor)
{
    m_current = editor;
    updateSelectedEditor();
}

con's avatar
con committed
254
255
256
257
258
259
260
261
262
263
void OpenEditorsWindow::updateSelectedEditor()
{
    if (m_current == 0 && m_editorList->currentItem()) {
        m_editorList->currentItem()->setSelected(false);
        return;
    }
    int num = m_editorList->topLevelItemCount();
    for (int i = 0; i < num; ++i) {
        IEditor *editor = m_editorList->topLevelItem(i)
                                  ->data(0, Qt::UserRole).value<IEditor *>();
264
        if (editor == m_current) {
con's avatar
con committed
265
266
267
268
269
270
271
            m_editorList->setCurrentItem(m_editorList->topLevelItem(i));
            break;
        }
    }
    m_editorList->scrollTo(m_editorList->currentIndex(), QAbstractItemView::PositionAtCenter);
}