OSDN Git Service

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