OSDN Git Service

a90d37024811f8de2567b51c6cb7a503f27fe185
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / editormanager / openeditorsview.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "openeditorsview.h"
35 #include "editormanager.h"
36 #include "editorview.h"
37 #include "openeditorsmodel.h"
38 #include "icore.h"
39
40 #include <coreplugin/coreconstants.h>
41 #include <coreplugin/editormanager/ieditor.h>
42 #include <coreplugin/filemanager.h>
43 #include <coreplugin/uniqueidmanager.h>
44 #include <coreplugin/actionmanager/actionmanager.h>
45 #include <utils/qtcassert.h>
46
47 #include <QtCore/QTimer>
48 #include <QtGui/QMenu>
49 #include <QtGui/QPainter>
50 #include <QtGui/QStyle>
51 #include <QtGui/QStyleOption>
52 #include <QtGui/QHeaderView>
53 #include <QtGui/QKeyEvent>
54 #ifdef Q_WS_MAC
55 #include <qmacstyle_mac.h>
56 #endif
57
58 using namespace Core;
59 using namespace Core::Internal;
60
61
62 OpenEditorsDelegate::OpenEditorsDelegate(QObject *parent)
63  : QStyledItemDelegate(parent)
64 {
65 }
66
67 void OpenEditorsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
68            const QModelIndex &index) const
69 {
70     if (option.state & QStyle::State_MouseOver) {
71         if ((QApplication::mouseButtons() & Qt::LeftButton) == 0)
72             pressedIndex = QModelIndex();
73         QBrush brush = option.palette.alternateBase();
74         if (index == pressedIndex)
75             brush = option.palette.dark();
76         painter->fillRect(option.rect, brush);
77     }
78
79
80     QStyledItemDelegate::paint(painter, option, index);
81
82     if (index.column() == 1 && option.state & QStyle::State_MouseOver) {
83         const QIcon icon(QLatin1String((option.state & QStyle::State_Selected) ?
84                                        Constants::ICON_CLOSE : Constants::ICON_CLOSE_DARK));
85
86         QRect iconRect(option.rect.right() - option.rect.height(),
87                        option.rect.top(),
88                        option.rect.height(),
89                        option.rect.height());
90
91         icon.paint(painter, iconRect, Qt::AlignRight | Qt::AlignVCenter);
92     }
93
94 }
95
96 ////
97 // OpenEditorsWidget
98 ////
99
100 OpenEditorsWidget::OpenEditorsWidget()
101 {
102     m_ui.setupUi(this);
103     setWindowTitle(tr("Open Documents"));
104     setWindowIcon(QIcon(Constants::ICON_DIR));
105     setFocusProxy(m_ui.editorList);
106     m_ui.editorList->viewport()->setAttribute(Qt::WA_Hover);
107     m_ui.editorList->setItemDelegate((m_delegate = new OpenEditorsDelegate(this)));
108     m_ui.editorList->header()->hide();
109     m_ui.editorList->setIndentation(0);
110     m_ui.editorList->setTextElideMode(Qt::ElideMiddle);
111     m_ui.editorList->setFrameStyle(QFrame::NoFrame);
112     m_ui.editorList->setAttribute(Qt::WA_MacShowFocusRect, false);
113     EditorManager *em = EditorManager::instance();
114     m_ui.editorList->setModel(em->openedEditorsModel());
115     m_ui.editorList->setSelectionMode(QAbstractItemView::SingleSelection);
116     m_ui.editorList->setSelectionBehavior(QAbstractItemView::SelectRows);
117     m_ui.editorList->header()->setStretchLastSection(false);
118     m_ui.editorList->header()->setResizeMode(0, QHeaderView::Stretch);
119     m_ui.editorList->header()->setResizeMode(1, QHeaderView::Fixed);
120     m_ui.editorList->header()->resizeSection(1, 16);
121     m_ui.editorList->setContextMenuPolicy(Qt::CustomContextMenu);
122     m_ui.editorList->installEventFilter(this);
123
124     connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
125             this, SLOT(updateCurrentItem(Core::IEditor*)));
126     connect(m_ui.editorList, SIGNAL(clicked(QModelIndex)),
127             this, SLOT(handleClicked(QModelIndex)));
128     connect(m_ui.editorList, SIGNAL(pressed(QModelIndex)),
129             this, SLOT(handlePressed(QModelIndex)));
130
131     connect(m_ui.editorList, SIGNAL(customContextMenuRequested(QPoint)),
132             this, SLOT(contextMenuRequested(QPoint)));
133 }
134
135 OpenEditorsWidget::~OpenEditorsWidget()
136 {
137 }
138
139 void OpenEditorsWidget::updateCurrentItem(Core::IEditor *editor)
140 {
141     if (!editor) {
142         m_ui.editorList->clearSelection();
143         return;
144     }
145     EditorManager *em = EditorManager::instance();
146     m_ui.editorList->setCurrentIndex(em->openedEditorsModel()->indexOf(editor));
147     m_ui.editorList->selectionModel()->select(m_ui.editorList->currentIndex(),
148                                               QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
149     m_ui.editorList->scrollTo(m_ui.editorList->currentIndex());
150 }
151
152 bool OpenEditorsWidget::eventFilter(QObject *obj, QEvent *event)
153 {
154     if (obj == m_ui.editorList && event->type() == QEvent::KeyPress
155             && m_ui.editorList->currentIndex().isValid()) {
156         QKeyEvent *ke = static_cast<QKeyEvent*>(event);
157         if ((ke->key() == Qt::Key_Return
158                 || ke->key() == Qt::Key_Enter)
159                 && ke->modifiers() == 0) {
160             activateEditor(m_ui.editorList->currentIndex());
161             return true;
162         } else if ((ke->key() == Qt::Key_Delete
163                    || ke->key() == Qt::Key_Backspace)
164                 && ke->modifiers() == 0) {
165             closeEditor(m_ui.editorList->currentIndex());
166         }
167     }
168     return false;
169 }
170
171 void OpenEditorsWidget::handlePressed(const QModelIndex &index)
172 {
173     if (index.column() == 0) {
174         activateEditor(index);
175     } else if (index.column() == 1) {
176         m_delegate->pressedIndex = index;
177     }
178 }
179
180 void OpenEditorsWidget::handleClicked(const QModelIndex &index)
181 {
182     if (index.column() == 1) { // the funky close button
183         closeEditor(index);
184
185         // work around a bug in itemviews where the delegate wouldn't get the QStyle::State_MouseOver
186         QPoint cursorPos = QCursor::pos();
187         QWidget *vp = m_ui.editorList->viewport();
188         QMouseEvent e(QEvent::MouseMove, vp->mapFromGlobal(cursorPos), cursorPos, Qt::NoButton, 0, 0);
189         QCoreApplication::sendEvent(vp, &e);
190     }
191 }
192
193 void OpenEditorsWidget::activateEditor(const QModelIndex &index)
194 {
195     m_ui.editorList->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
196     EditorManager::instance()->activateEditorForIndex(index, EditorManager::ModeSwitch);
197 }
198
199 void OpenEditorsWidget::closeEditor(const QModelIndex &index)
200 {
201     EditorManager::instance()->closeEditor(index);
202     // work around selection changes
203     updateCurrentItem(EditorManager::instance()->currentEditor());
204 }
205
206 void OpenEditorsWidget::contextMenuRequested(QPoint pos)
207 {
208     const QModelIndex index = m_ui.editorList->indexAt(pos);
209     QMenu contextMenu;
210     QAction *closeEditor = contextMenu.addAction(
211             index.isValid() ?  tr("Close \"%1\"").arg(index.data().toString())
212                             :  tr("Close Editor"));
213     QAction *closeOtherEditors = contextMenu.addAction(
214             index.isValid() ? tr("Close All Except \"%1\"").arg(index.data().toString())
215                             : tr("Close Other Editors"));
216     QAction *closeAllEditors = contextMenu.addAction(tr("Close All Editors"));
217
218     if (!index.isValid()) {
219         closeEditor->setEnabled(false);
220         closeOtherEditors->setEnabled(false);
221     }
222
223     if (EditorManager::instance()->openedEditors().isEmpty())
224         closeAllEditors->setEnabled(false);
225
226     QAction *action = contextMenu.exec(m_ui.editorList->mapToGlobal(pos));
227     if (action == 0)
228         return;
229     if (action == closeEditor)
230         EditorManager::instance()->closeEditor(index);
231     else if (action == closeAllEditors)
232         EditorManager::instance()->closeAllEditors();
233     else if (action == closeOtherEditors)
234         EditorManager::instance()->closeOtherEditors(index.data(Qt::UserRole).value<Core::IEditor*>());
235 }
236
237 ///
238 // OpenEditorsViewFactory
239 ///
240
241 NavigationView OpenEditorsViewFactory::createWidget()
242 {
243     NavigationView n;
244     n.widget = new OpenEditorsWidget();
245     return n;
246 }
247
248 QString OpenEditorsViewFactory::displayName() const
249 {
250     return OpenEditorsWidget::tr("Open Documents");
251 }
252
253 int OpenEditorsViewFactory::priority() const
254 {
255     return 200;
256 }
257
258 QString OpenEditorsViewFactory::id() const
259 {
260     return QLatin1String("Open Documents");
261 }
262
263 QKeySequence OpenEditorsViewFactory::activationSequence() const
264 {
265     return QKeySequence(Qt::ALT + Qt::Key_O);
266 }
267
268 OpenEditorsViewFactory::OpenEditorsViewFactory()
269 {
270 }
271
272 OpenEditorsViewFactory::~OpenEditorsViewFactory()
273 {
274 }