openeditorsview.cpp 6.2 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
** 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
26
** contact the sales department at http://www.qtsoftware.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 "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

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

56
57
58
59
60
61
62
63
64
65

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

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

mae's avatar
mae committed
74

75
76
77
78
79
80
81
82
83
84
85
    QStyledItemDelegate::paint(painter, option, index);

    if (index.column() == 1 && option.state & QStyle::State_MouseOver) {
        QIcon icon((option.state & QStyle::State_Selected) ? ":/core/images/closebutton.png"
               : ":/core/images/darkclosebutton.png");

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

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

}

con's avatar
con committed
91
92
93
94
95
96
OpenEditorsWidget::OpenEditorsWidget()
{
    m_ui.setupUi(this);
    setWindowTitle(tr("Open Documents"));
    setWindowIcon(QIcon(Constants::ICON_DIR));
    setFocusProxy(m_ui.editorList);
97
98
    m_ui.editorList->setFocusPolicy(Qt::NoFocus);
    m_ui.editorList->viewport()->setAttribute(Qt::WA_Hover);
99
    m_ui.editorList->setItemDelegate((m_delegate = new OpenEditorsDelegate(this)));
con's avatar
con committed
100
101
102
103
    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
104
    m_ui.editorList->setAttribute(Qt::WA_MacShowFocusRect, false);
con's avatar
con committed
105
    EditorManager *em = EditorManager::instance();
106
    m_ui.editorList->setModel(em->openedEditorsModel());
107
108
109
110
111
112
    m_ui.editorList->setSelectionMode(QAbstractItemView::NoSelection);
    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);
con's avatar
con committed
113
    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
114
            this, SLOT(updateCurrentItem(Core::IEditor*)));
115
    connect(m_ui.editorList, SIGNAL(clicked(QModelIndex)),
116
117
118
            this, SLOT(handleClicked(QModelIndex)));
    connect(m_ui.editorList, SIGNAL(pressed(QModelIndex)),
            this, SLOT(handlePressed(QModelIndex)));
con's avatar
con committed
119
120
121
122
123
124
}

OpenEditorsWidget::~OpenEditorsWidget()
{
}

125
void OpenEditorsWidget::updateCurrentItem(Core::IEditor *editor)
con's avatar
con committed
126
{
127
128
129
    if (!editor) {
        m_ui.editorList->clearSelection();
        return;
con's avatar
con committed
130
131
    }
    EditorManager *em = EditorManager::instance();
132
    m_ui.editorList->setCurrentIndex(em->openedEditorsModel()->indexOf(editor));
133
134
    m_ui.editorList->selectionModel()->select(m_ui.editorList->currentIndex(),
                                              QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
135
    m_ui.editorList->scrollTo(m_ui.editorList->currentIndex());
con's avatar
con committed
136
137
}

138
139
140
141
142
143
144
145
146
147
148
void OpenEditorsWidget::handlePressed(const QModelIndex &index)
{
    if (index.column() == 0) {
        m_ui.editorList->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
        EditorManager::instance()->activateEditor(index);
    } else if (index.column() == 1) {
        m_delegate->pressedIndex = index;
    }
}

void OpenEditorsWidget::handleClicked(const QModelIndex &index)
con's avatar
con committed
149
{
150
151
152
153
154
155
156
157
    if (index.column() == 1) { // the funky close button
        EditorManager::instance()->closeEditor(index);

        // 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
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
    }
}


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()
{
}