1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "openeditorsview.h"
35 #include "editormanager.h"
36 #include "editorview.h"
37 #include "openeditorsmodel.h"
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>
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>
55 #include <qmacstyle_mac.h>
59 using namespace Core::Internal;
62 OpenEditorsDelegate::OpenEditorsDelegate(QObject *parent)
63 : QStyledItemDelegate(parent)
67 void OpenEditorsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
68 const QModelIndex &index) const
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);
80 QStyledItemDelegate::paint(painter, option, index);
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));
86 QRect iconRect(option.rect.right() - option.rect.height(),
89 option.rect.height());
91 icon.paint(painter, iconRect, Qt::AlignRight | Qt::AlignVCenter);
100 OpenEditorsWidget::OpenEditorsWidget()
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);
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)));
131 connect(m_ui.editorList, SIGNAL(customContextMenuRequested(QPoint)),
132 this, SLOT(contextMenuRequested(QPoint)));
135 OpenEditorsWidget::~OpenEditorsWidget()
139 void OpenEditorsWidget::updateCurrentItem(Core::IEditor *editor)
142 m_ui.editorList->clearSelection();
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());
152 bool OpenEditorsWidget::eventFilter(QObject *obj, QEvent *event)
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());
162 } else if ((ke->key() == Qt::Key_Delete
163 || ke->key() == Qt::Key_Backspace)
164 && ke->modifiers() == 0) {
165 closeEditor(m_ui.editorList->currentIndex());
171 void OpenEditorsWidget::handlePressed(const QModelIndex &index)
173 if (index.column() == 0) {
174 activateEditor(index);
175 } else if (index.column() == 1) {
176 m_delegate->pressedIndex = index;
180 void OpenEditorsWidget::handleClicked(const QModelIndex &index)
182 if (index.column() == 1) { // the funky close button
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);
193 void OpenEditorsWidget::activateEditor(const QModelIndex &index)
195 m_ui.editorList->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
196 EditorManager::instance()->activateEditorForIndex(index, EditorManager::ModeSwitch);
199 void OpenEditorsWidget::closeEditor(const QModelIndex &index)
201 EditorManager::instance()->closeEditor(index);
202 // work around selection changes
203 updateCurrentItem(EditorManager::instance()->currentEditor());
206 void OpenEditorsWidget::contextMenuRequested(QPoint pos)
208 const QModelIndex index = m_ui.editorList->indexAt(pos);
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"));
218 if (!index.isValid()) {
219 closeEditor->setEnabled(false);
220 closeOtherEditors->setEnabled(false);
223 if (EditorManager::instance()->openedEditors().isEmpty())
224 closeAllEditors->setEnabled(false);
226 QAction *action = contextMenu.exec(m_ui.editorList->mapToGlobal(pos));
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*>());
238 // OpenEditorsViewFactory
241 NavigationView OpenEditorsViewFactory::createWidget()
244 n.widget = new OpenEditorsWidget();
248 QString OpenEditorsViewFactory::displayName() const
250 return OpenEditorsWidget::tr("Open Documents");
253 int OpenEditorsViewFactory::priority() const
258 QString OpenEditorsViewFactory::id() const
260 return QLatin1String("Open Documents");
263 QKeySequence OpenEditorsViewFactory::activationSequence() const
265 return QKeySequence(Qt::ALT + Qt::Key_O);
268 OpenEditorsViewFactory::OpenEditorsViewFactory()
272 OpenEditorsViewFactory::~OpenEditorsViewFactory()