1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "openeditorsview.h"
34 #include "editormanager.h"
35 #include "editorview.h"
36 #include "openeditorsmodel.h"
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>
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>
54 #include <qmacstyle_mac.h>
58 using namespace Core::Internal;
61 OpenEditorsDelegate::OpenEditorsDelegate(QObject *parent)
62 : QStyledItemDelegate(parent)
66 void OpenEditorsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
67 const QModelIndex &index) const
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);
79 QStyledItemDelegate::paint(painter, option, index);
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));
85 QRect iconRect(option.rect.right() - option.rect.height(),
88 option.rect.height());
90 icon.paint(painter, iconRect, Qt::AlignRight | Qt::AlignVCenter);
99 OpenEditorsWidget::OpenEditorsWidget()
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);
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)));
130 connect(m_ui.editorList, SIGNAL(customContextMenuRequested(QPoint)),
131 this, SLOT(contextMenuRequested(QPoint)));
134 OpenEditorsWidget::~OpenEditorsWidget()
138 void OpenEditorsWidget::updateCurrentItem(Core::IEditor *editor)
141 m_ui.editorList->clearSelection();
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());
151 bool OpenEditorsWidget::eventFilter(QObject *obj, QEvent *event)
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());
161 } else if ((ke->key() == Qt::Key_Delete
162 || ke->key() == Qt::Key_Backspace)
163 && ke->modifiers() == 0) {
164 closeEditor(m_ui.editorList->currentIndex());
170 void OpenEditorsWidget::handlePressed(const QModelIndex &index)
172 if (index.column() == 0) {
173 activateEditor(index);
174 } else if (index.column() == 1) {
175 m_delegate->pressedIndex = index;
179 void OpenEditorsWidget::handleClicked(const QModelIndex &index)
181 if (index.column() == 1) { // the funky close button
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);
192 void OpenEditorsWidget::activateEditor(const QModelIndex &index)
194 m_ui.editorList->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
195 EditorManager::instance()->activateEditorForIndex(index, EditorManager::ModeSwitch);
198 void OpenEditorsWidget::closeEditor(const QModelIndex &index)
200 EditorManager::instance()->closeEditor(index);
201 // work around selection changes
202 updateCurrentItem(EditorManager::instance()->currentEditor());
205 void OpenEditorsWidget::contextMenuRequested(QPoint pos)
207 const QModelIndex index = m_ui.editorList->indexAt(pos);
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"));
217 if (!index.isValid()) {
218 closeEditor->setEnabled(false);
219 closeOtherEditors->setEnabled(false);
222 if (EditorManager::instance()->openedEditors().isEmpty())
223 closeAllEditors->setEnabled(false);
225 QAction *action = contextMenu.exec(m_ui.editorList->mapToGlobal(pos));
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*>());
237 // OpenEditorsViewFactory
240 NavigationView OpenEditorsViewFactory::createWidget()
243 n.widget = new OpenEditorsWidget();
247 QString OpenEditorsViewFactory::displayName() const
249 return OpenEditorsWidget::tr("Open Documents");
252 int OpenEditorsViewFactory::priority() const
257 QString OpenEditorsViewFactory::id() const
259 return QLatin1String("Open Documents");
262 QKeySequence OpenEditorsViewFactory::activationSequence() const
264 return QKeySequence(Qt::ALT + Qt::Key_O);
267 OpenEditorsViewFactory::OpenEditorsViewFactory()
271 OpenEditorsViewFactory::~OpenEditorsViewFactory()