From f1302b3b209517d19613913fc3269b6da51eaa8d Mon Sep 17 00:00:00 2001
From: mae <qt-info@nokia.com>
Date: Thu, 26 Mar 2009 13:58:29 +0100
Subject: [PATCH] Add close-document functionality to the "Open Documents"
 view. The close button is enabled with mouse-over. The change makes the list
 behave more like tabs.

---
 src/plugins/coreplugin/core.qrc               |   1 +
 .../editormanager/editormanager.cpp           |   9 ++
 .../coreplugin/editormanager/editormanager.h  |   3 +-
 .../coreplugin/editormanager/editorview.cpp   |  17 ++-
 .../coreplugin/editormanager/editorview.h     |   2 +
 .../editormanager/openeditorsview.cpp         | 136 +++++++-----------
 .../editormanager/openeditorsview.h           |  19 ++-
 .../coreplugin/images/darkclosebutton.png     | Bin 0 -> 319 bytes
 8 files changed, 93 insertions(+), 94 deletions(-)
 create mode 100644 src/plugins/coreplugin/images/darkclosebutton.png

diff --git a/src/plugins/coreplugin/core.qrc b/src/plugins/coreplugin/core.qrc
index bd36c4f1c8d..bd8f3ad11f4 100644
--- a/src/plugins/coreplugin/core.qrc
+++ b/src/plugins/coreplugin/core.qrc
@@ -72,5 +72,6 @@
         <file>images/unknownfile.png</file>
         <file>images/unlocked.png</file>
         <file>images/extension.png</file>
+        <file>images/darkclosebutton.png</file>
     </qresource>
 </RCC>
diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp
index 722f925b263..972de1582aa 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.cpp
+++ b/src/plugins/coreplugin/editormanager/editormanager.cpp
@@ -744,6 +744,15 @@ IEditor *EditorManager::pickUnusedEditor() const
 }
 
 
+void EditorManager::closeEditor(const QModelIndex &index)
+{
+    IEditor *editor = index.data(Qt::UserRole).value<Core::IEditor*>();
+    if (editor)
+        closeEditor(editor);
+    else
+        m_d->m_editorModel->removeEditor(index);
+}
+
 void EditorManager::activateEditor(const QModelIndex &index, Internal::EditorView *view)
 {
     IEditor *editor = index.data(Qt::UserRole).value<IEditor*>();
diff --git a/src/plugins/coreplugin/editormanager/editormanager.h b/src/plugins/coreplugin/editormanager/editormanager.h
index 75c7e28cc34..9fd783de8b2 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.h
+++ b/src/plugins/coreplugin/editormanager/editormanager.h
@@ -127,6 +127,7 @@ public:
 
     Internal::EditorModel *openedEditorsModel() const;
     void activateEditor(const QModelIndex &index, Internal::EditorView *view = 0);
+    void closeEditor(const QModelIndex &index);
 
 
     QList<IEditor*> editorsForFiles(QList<IFile*> files) const;
@@ -151,8 +152,6 @@ public:
     Internal::OpenEditorsWindow *windowPopup() const;
     void showWindowPopup() const;
 
-//    Internal::EditorSplitter *editorSplitter() const;
-
     void showEditorInfoBar(const QString &kind,
                            const QString &infoText,
                            const QString &buttonText = QString(),
diff --git a/src/plugins/coreplugin/editormanager/editorview.cpp b/src/plugins/coreplugin/editormanager/editorview.cpp
index 1399269c6a6..26411c8fc79 100644
--- a/src/plugins/coreplugin/editormanager/editorview.cpp
+++ b/src/plugins/coreplugin/editormanager/editorview.cpp
@@ -76,7 +76,7 @@ QByteArray EditorModel::Entry::kind() const
 int EditorModel::columnCount(const QModelIndex &parent) const
 {
     Q_UNUSED(parent);
-    return 1;
+    return 2;
 }
 
 int EditorModel::rowCount(const QModelIndex &parent) const
@@ -180,6 +180,19 @@ void EditorModel::removeEditor(IEditor *editor)
     disconnect(editor, SIGNAL(changed()), this, SLOT(itemChanged()));
 }
 
+void EditorModel::removeEditor(const QModelIndex &index)
+{
+    int idx = index.row();
+    if (idx < 0)
+        return;
+    IEditor *editor= m_editors.at(idx).editor;
+    beginRemoveRows(QModelIndex(), idx, idx);
+    m_editors.removeAt(idx);
+    endRemoveRows();
+    if (editor)
+        disconnect(editor, SIGNAL(changed()), this, SLOT(itemChanged()));
+}
+
 void EditorModel::removeAllRestoredEditors()
 {
     for (int i = m_editors.count()-1; i >= 0; --i) {
@@ -227,7 +240,7 @@ void EditorModel::emitDataChanged(IEditor *editor)
 QModelIndex EditorModel::index(int row, int column, const QModelIndex &parent) const
 {
     Q_UNUSED(parent);
-    if (column != 0 || row < 0 || row >= m_editors.count())
+    if (column < 0 || column > 1 || row < 0 || row >= m_editors.count())
         return QModelIndex();
     return createIndex(row, column);
 }
diff --git a/src/plugins/coreplugin/editormanager/editorview.h b/src/plugins/coreplugin/editormanager/editorview.h
index 364be827f94..995a6e86cc6 100644
--- a/src/plugins/coreplugin/editormanager/editorview.h
+++ b/src/plugins/coreplugin/editormanager/editorview.h
@@ -86,6 +86,8 @@ public:
     QList<Entry> entries() const { return m_editors; }
 
     void removeEditor(IEditor *editor);
+    void removeEditor(const QModelIndex &index);
+
     void removeAllRestoredEditors();
     void emitDataChanged(IEditor *editor);
 
diff --git a/src/plugins/coreplugin/editormanager/openeditorsview.cpp b/src/plugins/coreplugin/editormanager/openeditorsview.cpp
index af10e6096f1..d04d82b3995 100644
--- a/src/plugins/coreplugin/editormanager/openeditorsview.cpp
+++ b/src/plugins/coreplugin/editormanager/openeditorsview.cpp
@@ -53,26 +53,62 @@ Q_DECLARE_METATYPE(Core::IEditor*)
 using namespace Core;
 using namespace Core::Internal;
 
+
+OpenEditorsDelegate::OpenEditorsDelegate(QObject *parent)
+ : QStyledItemDelegate(parent)
+{
+}
+
+void OpenEditorsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
+           const QModelIndex &index) const
+{
+
+    if (option.state & QStyle::State_MouseOver) {
+        painter->fillRect(option.rect, option.palette.alternateBase());
+    }
+
+    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());
+
+        icon.paint(painter, iconRect, Qt::AlignRight | Qt::AlignVCenter);
+    }
+
+}
+
 OpenEditorsWidget::OpenEditorsWidget()
 {
     m_ui.setupUi(this);
     setWindowTitle(tr("Open Documents"));
     setWindowIcon(QIcon(Constants::ICON_DIR));
     setFocusProxy(m_ui.editorList);
+    m_ui.editorList->setFocusPolicy(Qt::NoFocus);
+    m_ui.editorList->viewport()->setAttribute(Qt::WA_Hover);
+    m_ui.editorList->setItemDelegate(new OpenEditorsDelegate(this));
     m_ui.editorList->header()->hide();
     m_ui.editorList->setIndentation(0);
-    m_ui.editorList->setSelectionMode(QAbstractItemView::ExtendedSelection);
     m_ui.editorList->setTextElideMode(Qt::ElideMiddle);
-    m_ui.editorList->installEventFilter(this);
     m_ui.editorList->setFrameStyle(QFrame::NoFrame);
     m_ui.editorList->setAttribute(Qt::WA_MacShowFocusRect, false);
     EditorManager *em = EditorManager::instance();
     m_ui.editorList->setModel(em->openedEditorsModel());
+    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);
     connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
             this, SLOT(updateCurrentItem(Core::IEditor*)));
     connect(m_ui.editorList, SIGNAL(clicked(QModelIndex)),
             this, SLOT(selectEditor(QModelIndex)));
-    //updateEditorList();
 }
 
 OpenEditorsWidget::~OpenEditorsWidget()
@@ -86,96 +122,28 @@ void OpenEditorsWidget::updateCurrentItem(Core::IEditor *editor)
         return;
     }
     EditorManager *em = EditorManager::instance();
-    m_ui.editorList->clearSelection(); //we are in extended selectionmode
     m_ui.editorList->setCurrentIndex(em->openedEditorsModel()->indexOf(editor));
+    m_ui.editorList->selectionModel()->select(m_ui.editorList->currentIndex(),
+                                              QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
     m_ui.editorList->scrollTo(m_ui.editorList->currentIndex());
 }
 
 void OpenEditorsWidget::selectEditor(const QModelIndex &index)
 {
-    EditorManager::instance()->activateEditor(index);
-}
-
-
-void OpenEditorsWidget::selectEditor()
-{
-    selectEditor(m_ui.editorList->currentIndex());
-}
-
-void OpenEditorsWidget::closeEditors()
-{
-    /* ### TODO
-    QList<IFile *> selectedFiles;
-    QList<IEditor *> selectedEditors;
-    foreach (QTreeWidgetItem *item, m_ui.editorList->selectedItems()) {
-        selectedEditors.append(item->data(0, Qt::UserRole).value<IEditor *>());
-        selectedFiles.append(item->data(0, Qt::UserRole).value<IEditor *>()->file());
-    }
-    ICore *core = ICore::instance();
-    bool cancelled = false;
-    core->fileManager()->saveModifiedFiles(selectedFiles, &cancelled);
-    if (cancelled)
-        return;
-    core->editorManager()->closeEditors(selectedEditors);
-    updateEditorList();
-    */
-}
-
-void OpenEditorsWidget::closeAllEditors()
-{
-    m_ui.editorList->selectAll();
-    closeEditors();
-}
-
-
-bool OpenEditorsWidget::eventFilter(QObject *obj, QEvent *event)
-{
-    if (obj == m_ui.editorList) {
-        if (event->type() == QEvent::KeyPress) {
-            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
-            switch (keyEvent->key()) {
-                case Qt::Key_Return:
-                    selectEditor(m_ui.editorList->currentIndex());
-                    return true;
-                case Qt::Key_Delete: //fall through
-                case Qt::Key_Backspace:
-                    if (keyEvent->modifiers() == Qt::NoModifier) {
-                        closeEditors();
-                        return true;
-                    }
-                    break;
-                default:
-                    break;
-            }
-        }
-        if (event->type() == QEvent::ContextMenu) {
-            QContextMenuEvent *contextMenuEvent = static_cast<QContextMenuEvent *>(event);
-            QMenu menu;
-            menu.addAction(tr("&Select"), this, SLOT(selectEditor()));
-//todo      menu.addAction(tr("&Close"), this, SLOT(closeEditors()));
-//todo      menu.addAction(tr("Close &All"), this, SLOT(closeAllEditors()));
-            if (m_ui.editorList->selectionModel()->selectedIndexes().isEmpty())
-                menu.setEnabled(false);
-            menu.exec(contextMenuEvent->globalPos());
-            return true;
-        }
-    } else if (obj == m_widget) {
-        if (event->type() == QEvent::FocusIn) {
-            QFocusEvent *e = static_cast<QFocusEvent *>(event);
-            if (e->reason() != Qt::MouseFocusReason) {
-                // we should not set the focus in a event filter for a focus event,
-                // so do it when the focus event is processed
-                QTimer::singleShot(0, this, SLOT(putFocusToEditorList()));
-            }
-        }
+    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);
+    } else {
+        m_ui.editorList->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
+        EditorManager::instance()->activateEditor(index);
     }
-    return false;
 }
 
-void OpenEditorsWidget::putFocusToEditorList()
-{
-    m_ui.editorList->setFocus();
-}
 
 NavigationView OpenEditorsViewFactory::createWidget()
 {
diff --git a/src/plugins/coreplugin/editormanager/openeditorsview.h b/src/plugins/coreplugin/editormanager/openeditorsview.h
index 90305516990..45cfc62a010 100644
--- a/src/plugins/coreplugin/editormanager/openeditorsview.h
+++ b/src/plugins/coreplugin/editormanager/openeditorsview.h
@@ -40,6 +40,7 @@
 #include <QtGui/QKeySequence>
 #include <QtGui/QAbstractButton>
 #include <QtGui/QTreeWidgetItem>
+#include <QtGui/QStyledItemDelegate>
 
 namespace Core {
 namespace Internal {
@@ -52,15 +53,9 @@ public:
     OpenEditorsWidget();
     ~OpenEditorsWidget();
 
-    bool eventFilter(QObject *obj, QEvent *event);
-
 private slots:
     void selectEditor(const QModelIndex &);
-    void selectEditor();
-    void closeEditors();
-    void closeAllEditors();
     void updateCurrentItem(Core::IEditor*);
-    void putFocusToEditorList();
 
 private:
     Ui::OpenEditorsView m_ui;
@@ -80,4 +75,16 @@ public:
 } // namespace Internal
 } // namespace Core
 
+class OpenEditorsDelegate : public QStyledItemDelegate
+{
+    Q_OBJECT
+
+public:
+    OpenEditorsDelegate(QObject *parent = 0);
+
+    void paint(QPainter *painter, const QStyleOptionViewItem &option,
+               const QModelIndex &index) const;
+};
+
+
 #endif // OPENEDITORSVIEW_H
diff --git a/src/plugins/coreplugin/images/darkclosebutton.png b/src/plugins/coreplugin/images/darkclosebutton.png
new file mode 100644
index 0000000000000000000000000000000000000000..1077663b28e6a08098b1a460c8ee0d1a89ad5d88
GIT binary patch
literal 319
zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|*pj^6T^Rm@
z;DWu&Cj&(|3p^r=85p>QL70(Y)*K0-AbW|YuPggaW=TF~rkIzr(t$$zJzX3_EKaYz
zY}mzQDAN9M{^@V?93u8MvkSae*nA@*`Yo@_-iBgD?Ttd+t{dkZ6FyNE-QnBfI62})
zwv)nt{XOqh*KX3;8zdjMW;?T$|AM_T3!1s?IXxOxe`y}z%;r&=z?3ZW@it4%y}LoJ
zr8jQ$JN;;o{8!vi?I9k&vLKI3^p${JvK;qw23LcB?PkkXZq~fLeV%%R>Jc|t5y3>m
zjngF-w7+lJpq3(i>@UNLjR))$*Kj@bUDtYd^78$azteXyz1g8{uX_5^exO$vJYD@<
J);T3K0RTJbcB=pY

literal 0
HcmV?d00001

-- 
GitLab