From 5af6f15f7c6897b65e494838866e01f984093cb7 Mon Sep 17 00:00:00 2001
From: Denis Mingulov <denis.mingulov@gmail.com>
Date: Fri, 16 Jul 2010 11:18:30 +0200
Subject: [PATCH] Utils::NavigationTreeView is created - as a base class for
 any Navigation Widget (Side Bar) tree view

Merge-request: 2167
Reviewed-by: Kai Koehne <kai.koehne@nokia.com>
---
 src/libs/utils/navigationtreeview.cpp         | 79 +++++++++++++++++++
 src/libs/utils/navigationtreeview.h           | 63 +++++++++++++++
 src/libs/utils/utils.pro                      |  6 +-
 .../classview/classviewnavigationwidget.cpp   |  1 -
 .../classview/classviewnavigationwidget.ui    | 18 ++---
 src/plugins/cppeditor/cppoutline.cpp          |  7 +-
 src/plugins/cppeditor/cppoutline.h            |  3 +-
 .../projectexplorer/projecttreewidget.cpp     | 38 +--------
 src/plugins/qmljseditor/qmljsoutline.cpp      |  7 +-
 src/plugins/qmljseditor/qmljsoutline.h        |  3 +-
 10 files changed, 162 insertions(+), 63 deletions(-)
 create mode 100644 src/libs/utils/navigationtreeview.cpp
 create mode 100644 src/libs/utils/navigationtreeview.h

diff --git a/src/libs/utils/navigationtreeview.cpp b/src/libs/utils/navigationtreeview.cpp
new file mode 100644
index 00000000000..0974bccaaa0
--- /dev/null
+++ b/src/libs/utils/navigationtreeview.cpp
@@ -0,0 +1,79 @@
+/**************************************************************************
+**
+** This file is part of Qt Creator
+**
+** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
+**
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** Commercial Usage
+**
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Nokia.
+**
+** GNU Lesser General Public License Usage
+**
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at http://qt.nokia.com/contact.
+**
+**************************************************************************/
+
+#include "navigationtreeview.h"
+
+#include <QtGui/QFocusEvent>
+
+#ifdef Q_WS_MAC
+#include <QtGui/QKeyEvent>
+#endif
+
+namespace Utils {
+
+NavigationTreeView::NavigationTreeView(QWidget *parent)
+    : QTreeView(parent)
+{
+    setFrameStyle(QFrame::NoFrame);
+    setIndentation(indentation() * 9/10);
+    setUniformRowHeights(true);
+    setTextElideMode(Qt::ElideNone);
+    setAttribute(Qt::WA_MacShowFocusRect, false);
+}
+
+// This is a workaround to stop Qt from redrawing the project tree every
+// time the user opens or closes a menu when it has focus. Would be nicer to
+// fix it in Qt.
+void NavigationTreeView::focusInEvent(QFocusEvent *event)
+{
+    if (event->reason() != Qt::PopupFocusReason)
+        QTreeView::focusInEvent(event);
+}
+
+void NavigationTreeView::focusOutEvent(QFocusEvent *event)
+{
+    if (event->reason() != Qt::PopupFocusReason)
+        QTreeView::focusOutEvent(event);
+}
+
+#ifdef Q_WS_MAC
+void NavigationTreeView::keyPressEvent(QKeyEvent *event)
+{
+    if ((event->key() == Qt::Key_Return
+            || event->key() == Qt::Key_Enter)
+            && event->modifiers() == 0
+            && currentIndex().isValid()) {
+        emit activated(currentIndex());
+        return;
+    }
+    QTreeView::keyPressEvent(event);
+}
+#endif
+
+} // namespace Utils
diff --git a/src/libs/utils/navigationtreeview.h b/src/libs/utils/navigationtreeview.h
new file mode 100644
index 00000000000..697fed10124
--- /dev/null
+++ b/src/libs/utils/navigationtreeview.h
@@ -0,0 +1,63 @@
+/**************************************************************************
+**
+** This file is part of Qt Creator
+**
+** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
+**
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** Commercial Usage
+**
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Nokia.
+**
+** GNU Lesser General Public License Usage
+**
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at http://qt.nokia.com/contact.
+**
+**************************************************************************/
+
+#ifndef NAVIGATIONTREEVIEW_H
+#define NAVIGATIONTREEVIEW_H
+
+#include "utils_global.h"
+
+#include <QtGui/QTreeView>
+
+namespace Utils {
+
+/*!
+   \class NavigationTreeView
+   \sa Core::NavigationView, Core::INavigationWidgetFactory
+
+   General TreeView for any Side Bar widget. Common initialization etc, e.g. Mac specific behaviour.
+ */
+
+class QTCREATOR_UTILS_EXPORT NavigationTreeView : public QTreeView
+{
+    Q_OBJECT
+public:
+    NavigationTreeView(QWidget *parent = 0);
+
+protected:
+    void focusInEvent(QFocusEvent *event);
+    void focusOutEvent(QFocusEvent *event);
+
+#ifdef Q_WS_MAC
+    void keyPressEvent(QKeyEvent *event);
+#endif
+};
+
+} // Utils
+
+#endif // NAVIGATIONTREEVIEW_H
diff --git a/src/libs/utils/utils.pro b/src/libs/utils/utils.pro
index a189b672508..582286884c3 100644
--- a/src/libs/utils/utils.pro
+++ b/src/libs/utils/utils.pro
@@ -41,7 +41,8 @@ SOURCES += reloadpromptutils.cpp \
     changeset.cpp \
     filterlineedit.cpp \
     faketooltip.cpp \
-    htmldocextractor.cpp
+    htmldocextractor.cpp \
+    navigationtreeview.cpp
 win32 {
     SOURCES += abstractprocess_win.cpp \
         consoleprocess_win.cpp \
@@ -95,7 +96,8 @@ HEADERS += utils_global.h \
     changeset.h \
     filterlineedit.h \
     faketooltip.h \
-    htmldocextractor.h
+    htmldocextractor.h \
+    navigationtreeview.h
 FORMS += filewizardpage.ui \
     projectintropage.ui \
     newclasswidget.ui \
diff --git a/src/plugins/classview/classviewnavigationwidget.cpp b/src/plugins/classview/classviewnavigationwidget.cpp
index a90a58fa823..8420284d818 100644
--- a/src/plugins/classview/classviewnavigationwidget.cpp
+++ b/src/plugins/classview/classviewnavigationwidget.cpp
@@ -76,7 +76,6 @@ NavigationWidget::NavigationWidget(QWidget *parent) :
 {
     d_ptr->ui = new Ui::NavigationWidget;
     d_ptr->ui->setupUi(this);
-    d_ptr->ui->treeView->setIndentation(d_ptr->ui->treeView->indentation() * 9/10);
 
     // tree model
     d_ptr->treeModel = new TreeItemModel(this);
diff --git a/src/plugins/classview/classviewnavigationwidget.ui b/src/plugins/classview/classviewnavigationwidget.ui
index d9ba960515e..0b0bf55a50f 100644
--- a/src/plugins/classview/classviewnavigationwidget.ui
+++ b/src/plugins/classview/classviewnavigationwidget.ui
@@ -21,19 +21,10 @@
     <number>0</number>
    </property>
    <item>
-    <widget class="QTreeView" name="treeView">
-     <property name="frameShape">
-      <enum>QFrame::NoFrame</enum>
-     </property>
+    <widget class="Utils::NavigationTreeView" name="treeView">
      <property name="editTriggers">
       <set>QAbstractItemView::NoEditTriggers</set>
      </property>
-     <property name="textElideMode">
-      <enum>Qt::ElideNone</enum>
-     </property>
-     <property name="uniformRowHeights">
-      <bool>true</bool>
-     </property>
      <property name="headerHidden">
       <bool>true</bool>
      </property>
@@ -41,6 +32,13 @@
    </item>
   </layout>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>Utils::NavigationTreeView</class>
+   <extends>QTreeView</extends>
+   <header>utils/navigationtreeview.h</header>
+  </customwidget>
+ </customwidgets>
  <resources/>
  <connections/>
 </ui>
diff --git a/src/plugins/cppeditor/cppoutline.cpp b/src/plugins/cppeditor/cppoutline.cpp
index 4e9fcfdbdaf..14d8b0b9e93 100644
--- a/src/plugins/cppeditor/cppoutline.cpp
+++ b/src/plugins/cppeditor/cppoutline.cpp
@@ -18,16 +18,11 @@ enum {
 };
 
 CppOutlineTreeView::CppOutlineTreeView(QWidget *parent) :
-    QTreeView(parent)
+    Utils::NavigationTreeView(parent)
 {
     // see also QmlJSOutlineTreeView
     setFocusPolicy(Qt::NoFocus);
-    setFrameStyle(QFrame::NoFrame);
-    setAttribute(Qt::WA_MacShowFocusRect, false);
-    setUniformRowHeights(true);
     setHeaderHidden(true);
-    setTextElideMode(Qt::ElideNone);
-    setIndentation(20);
     setExpandsOnDoubleClick(false);
 }
 
diff --git a/src/plugins/cppeditor/cppoutline.h b/src/plugins/cppeditor/cppoutline.h
index d007b9689de..a9f2e65e0fb 100644
--- a/src/plugins/cppeditor/cppoutline.h
+++ b/src/plugins/cppeditor/cppoutline.h
@@ -3,6 +3,7 @@
 
 #include "cppeditor.h"
 
+#include <utils/navigationtreeview.h>
 #include <texteditor/ioutlinewidget.h>
 
 #include <QtGui/QSortFilterProxyModel>
@@ -11,7 +12,7 @@
 namespace CppEditor {
 namespace Internal {
 
-class CppOutlineTreeView : public QTreeView
+class CppOutlineTreeView : public Utils::NavigationTreeView
 {
     Q_OBJECT
 public:
diff --git a/src/plugins/projectexplorer/projecttreewidget.cpp b/src/plugins/projectexplorer/projecttreewidget.cpp
index 56495cf6394..d6fd7a5f62c 100644
--- a/src/plugins/projectexplorer/projecttreewidget.cpp
+++ b/src/plugins/projectexplorer/projecttreewidget.cpp
@@ -38,6 +38,7 @@
 #include <coreplugin/icore.h>
 #include <coreplugin/editormanager/editormanager.h>
 #include <utils/qtcassert.h>
+#include <utils/navigationtreeview.h>
 
 #include <QtCore/QDebug>
 #include <QtCore/QSettings>
@@ -57,14 +58,12 @@ namespace {
     bool debug = false;
 }
 
-class ProjectTreeView : public QTreeView
+class ProjectTreeView : public Utils::NavigationTreeView
 {
 public:
     ProjectTreeView()
     {
         setEditTriggers(QAbstractItemView::EditKeyPressed);
-        setFrameStyle(QFrame::NoFrame);
-        setIndentation(indentation() * 9/10);
         {
             QHeaderView *treeHeader = header();
             treeHeader->setVisible(false);
@@ -72,41 +71,8 @@ public:
             treeHeader->setStretchLastSection(true);
         }
         setContextMenuPolicy(Qt::CustomContextMenu);
-        setUniformRowHeights(true);
-        setTextElideMode(Qt::ElideNone);
 //        setExpandsOnDoubleClick(false);
-        setAttribute(Qt::WA_MacShowFocusRect, false);
     }
-
-protected:
-    // This is a workaround to stop Qt from redrawing the project tree every
-    // time the user opens or closes a menu when it has focus. Would be nicer to
-    // fix it in Qt.
-    void focusInEvent(QFocusEvent *event)
-    {
-        if (event->reason() != Qt::PopupFocusReason)
-            QTreeView::focusInEvent(event);
-    }
-
-    void focusOutEvent(QFocusEvent *event)
-    {
-        if (event->reason() != Qt::PopupFocusReason)
-            QTreeView::focusOutEvent(event);
-    }
-
-#ifdef Q_WS_MAC
-    void keyPressEvent(QKeyEvent *event)
-    {
-        if ((event->key() == Qt::Key_Return
-                || event->key() == Qt::Key_Enter)
-                && event->modifiers() == 0
-                && currentIndex().isValid()) {
-            emit activated(currentIndex());
-            return;
-        }
-        QTreeView::keyPressEvent(event);
-    }
-#endif
 };
 
 /*!
diff --git a/src/plugins/qmljseditor/qmljsoutline.cpp b/src/plugins/qmljseditor/qmljsoutline.cpp
index 7831670eec7..c349dfc9ed9 100644
--- a/src/plugins/qmljseditor/qmljsoutline.cpp
+++ b/src/plugins/qmljseditor/qmljsoutline.cpp
@@ -17,16 +17,11 @@ namespace QmlJSEditor {
 namespace Internal {
 
 QmlJSOutlineTreeView::QmlJSOutlineTreeView(QWidget *parent) :
-    QTreeView(parent)
+    Utils::NavigationTreeView(parent)
 {
     // see also CppOutlineTreeView
     setFocusPolicy(Qt::NoFocus);
-    setFrameStyle(QFrame::NoFrame);
-    setAttribute(Qt::WA_MacShowFocusRect, false);
-    setUniformRowHeights(true);
     setHeaderHidden(true);
-    setTextElideMode(Qt::ElideNone);
-    setIndentation(20);
     setExpandsOnDoubleClick(false);
 }
 
diff --git a/src/plugins/qmljseditor/qmljsoutline.h b/src/plugins/qmljseditor/qmljsoutline.h
index 15221210cb9..27c2e4f8d40 100644
--- a/src/plugins/qmljseditor/qmljsoutline.h
+++ b/src/plugins/qmljseditor/qmljsoutline.h
@@ -3,6 +3,7 @@
 
 #include "qmljseditor.h"
 
+#include <utils/navigationtreeview.h>
 #include <texteditor/ioutlinewidget.h>
 
 #include <QtGui/QTreeView>
@@ -19,7 +20,7 @@ class Editor;
 namespace QmlJSEditor {
 namespace Internal {
 
-class QmlJSOutlineTreeView : public QTreeView
+class QmlJSOutlineTreeView : public Utils::NavigationTreeView
 {
     Q_OBJECT
 public:
-- 
GitLab