From ecdecd4e97397afeeb83af0f5c9e3fef9a1ba522 Mon Sep 17 00:00:00 2001 From: Aurindam Jana Date: Mon, 2 Apr 2012 13:09:37 +0200 Subject: [PATCH] DebuggerTreeView: Move base class to utils Change-Id: I3313789be5f835d218cad5ed5f3143aee18e9f5f Reviewed-by: hjk --- src/libs/utils/basetreeview.cpp | 137 ++++++++++++++++++ src/libs/utils/basetreeview.h | 76 ++++++++++ src/libs/utils/utils-lib.pri | 6 +- src/plugins/debugger/basewindow.cpp | 96 +----------- src/plugins/debugger/basewindow.h | 27 +--- src/plugins/debugger/registerwindow.cpp | 1 - .../qmljsinspector/qmljspropertyinspector.cpp | 21 +-- .../qmljsinspector/qmljspropertyinspector.h | 7 +- 8 files changed, 228 insertions(+), 143 deletions(-) create mode 100644 src/libs/utils/basetreeview.cpp create mode 100644 src/libs/utils/basetreeview.h diff --git a/src/libs/utils/basetreeview.cpp b/src/libs/utils/basetreeview.cpp new file mode 100644 index 00000000000..b1307bc0083 --- /dev/null +++ b/src/libs/utils/basetreeview.cpp @@ -0,0 +1,137 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** 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. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "basetreeview.h" + +#include +#include +#include + +namespace Utils { + +BaseTreeView::BaseTreeView(QWidget *parent) + : QTreeView(parent) +{ + setAttribute(Qt::WA_MacShowFocusRect, false); + setFrameStyle(QFrame::NoFrame); + setRootIsDecorated(false); + setIconSize(QSize(10, 10)); + setSelectionMode(QAbstractItemView::ExtendedSelection); + setUniformRowHeights(true); + + header()->setDefaultAlignment(Qt::AlignLeft); + header()->setClickable(true); + + connect(this, SIGNAL(activated(QModelIndex)), + SLOT(rowActivatedHelper(QModelIndex))); + connect(header(), SIGNAL(sectionClicked(int)), + SLOT(headerSectionClicked(int))); + + m_adjustColumnsAction = new QAction(tr("Adjust Column Widths to Contents"), 0); + m_alwaysAdjustColumnsAction = 0; +} + +void BaseTreeView::setAlwaysAdjustColumnsAction(QAction *action) +{ + m_alwaysAdjustColumnsAction = action; + connect(action, SIGNAL(toggled(bool)), + SLOT(setAlwaysResizeColumnsToContents(bool))); +} + +void BaseTreeView::addBaseContextActions(QMenu *menu) +{ + menu->addSeparator(); + if (m_alwaysAdjustColumnsAction) + menu->addAction(m_alwaysAdjustColumnsAction); + menu->addAction(m_adjustColumnsAction); + menu->addSeparator(); +} + +bool BaseTreeView::handleBaseContextAction(QAction *act) +{ + if (act == 0) + return true; + if (act == m_adjustColumnsAction) { + resizeColumnsToContents(); + return true; + } + if (act == m_alwaysAdjustColumnsAction) { + if (act->isChecked()) + resizeColumnsToContents(); + // Action triggered automatically. + return true; + } + return false; +} + +void BaseTreeView::setModel(QAbstractItemModel *model) +{ + QTreeView::setModel(model); + if (header() && m_alwaysAdjustColumnsAction) + setAlwaysResizeColumnsToContents(m_alwaysAdjustColumnsAction->isChecked()); +} + +void BaseTreeView::mousePressEvent(QMouseEvent *ev) +{ + QTreeView::mousePressEvent(ev); + if (!indexAt(ev->pos()).isValid()) + resizeColumnsToContents(); +} + +void BaseTreeView::resizeColumnsToContents() +{ + const int columnCount = model()->columnCount(); + for (int c = 0 ; c != columnCount; ++c) + resizeColumnToContents(c); +} + +void BaseTreeView::setAlwaysResizeColumnsToContents(bool on) +{ + QHeaderView::ResizeMode mode = on + ? QHeaderView::ResizeToContents : QHeaderView::Interactive; + header()->setResizeMode(0, mode); +} + +void BaseTreeView::headerSectionClicked(int logicalIndex) +{ + resizeColumnToContents(logicalIndex); +} + +void BaseTreeView::reset() +{ + QTreeView::reset(); + if (header() && m_alwaysAdjustColumnsAction + && m_alwaysAdjustColumnsAction->isChecked()) + resizeColumnsToContents(); +} + +} // namespace Utils diff --git a/src/libs/utils/basetreeview.h b/src/libs/utils/basetreeview.h new file mode 100644 index 00000000000..c78860ebd0f --- /dev/null +++ b/src/libs/utils/basetreeview.h @@ -0,0 +1,76 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** 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. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef BASETREEVIEW_H +#define BASETREEVIEW_H + +#include "utils_global.h" + +#include + +namespace Utils { + +class QTCREATOR_UTILS_EXPORT BaseTreeView : public QTreeView +{ + Q_OBJECT + +public: + BaseTreeView(QWidget *parent = 0); + + void setAlwaysAdjustColumnsAction(QAction *action); + virtual void addBaseContextActions(QMenu *menu); + bool handleBaseContextAction(QAction *action); + + void setModel(QAbstractItemModel *model); + virtual void rowActivated(const QModelIndex &) {} + void mousePressEvent(QMouseEvent *ev); + +public slots: + void resizeColumnsToContents(); + void setAlwaysResizeColumnsToContents(bool on); + void reset(); + +protected slots: + void setAlternatingRowColorsHelper(bool on) { setAlternatingRowColors(on); } + +private slots: + void rowActivatedHelper(const QModelIndex &index) { rowActivated(index); } + void headerSectionClicked(int logicalIndex); + +private: + QAction *m_alwaysAdjustColumnsAction; + QAction *m_adjustColumnsAction; +}; + +} // namespace Utils + +#endif // BASETREEVIEW_H diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri index 152c0ad47d9..6327f3fbdcc 100644 --- a/src/libs/utils/utils-lib.pri +++ b/src/libs/utils/utils-lib.pri @@ -97,7 +97,8 @@ SOURCES += $$PWD/environment.cpp \ $$PWD/json.cpp \ $$PWD/portlist.cpp \ $$PWD/tcpportsgatherer.cpp \ - $$PWD/appmainwindow.cpp + $$PWD/appmainwindow.cpp \ + $$PWD/basetreeview.cpp win32 { SOURCES += \ @@ -211,7 +212,8 @@ HEADERS += \ $$PWD/runextensions.h \ $$PWD/portlist.h \ $$PWD/tcpportsgatherer.h \ - $$PWD/appmainwindow.h + $$PWD/appmainwindow.h \ + $$PWD/basetreeview.h FORMS += $$PWD/filewizardpage.ui \ $$PWD/projectintropage.ui \ diff --git a/src/plugins/debugger/basewindow.cpp b/src/plugins/debugger/basewindow.cpp index bf77f3f37d8..92fdcd0d913 100644 --- a/src/plugins/debugger/basewindow.cpp +++ b/src/plugins/debugger/basewindow.cpp @@ -40,9 +40,6 @@ #include #include -#include -#include -#include #include #include @@ -50,107 +47,20 @@ namespace Debugger { namespace Internal { BaseTreeView::BaseTreeView(QWidget *parent) - : QTreeView(parent) + : Utils::BaseTreeView(parent) { QAction *act = debuggerCore()->action(UseAlternatingRowColors); - - setAttribute(Qt::WA_MacShowFocusRect, false); - setFrameStyle(QFrame::NoFrame); setAlternatingRowColors(act->isChecked()); - setRootIsDecorated(false); - setIconSize(QSize(10, 10)); - setSelectionMode(QAbstractItemView::ExtendedSelection); - setUniformRowHeights(true); - - header()->setDefaultAlignment(Qt::AlignLeft); - header()->setClickable(true); - connect(act, SIGNAL(toggled(bool)), - SLOT(setAlternatingRowColorsHelper(bool))); - connect(this, SIGNAL(activated(QModelIndex)), - SLOT(rowActivatedHelper(QModelIndex))); - connect(header(), SIGNAL(sectionClicked(int)), - SLOT(headerSectionClicked(int))); - - m_adjustColumnsAction = new QAction(tr("Adjust Column Widths to Contents"), 0); - m_alwaysAdjustColumnsAction = 0; -} - -void BaseTreeView::setAlwaysAdjustColumnsAction(QAction *action) -{ - m_alwaysAdjustColumnsAction = action; - connect(action, SIGNAL(toggled(bool)), - SLOT(setAlwaysResizeColumnsToContents(bool))); + SLOT(setAlternatingRowColorsHelper(bool))); } void BaseTreeView::addBaseContextActions(QMenu *menu) { - menu->addSeparator(); - if (m_alwaysAdjustColumnsAction) - menu->addAction(m_alwaysAdjustColumnsAction); - menu->addAction(m_adjustColumnsAction); - menu->addSeparator(); + Utils::BaseTreeView::addBaseContextActions(menu); menu->addAction(debuggerCore()->action(SettingsDialog)); } -bool BaseTreeView::handleBaseContextAction(QAction *act) -{ - if (act == 0) - return true; - if (act == m_adjustColumnsAction) { - resizeColumnsToContents(); - return true; - } - if (act == m_alwaysAdjustColumnsAction) { - if (act->isChecked()) - resizeColumnsToContents(); - // Action triggered automatically. - return true; - } - return false; -} - -void BaseTreeView::setModel(QAbstractItemModel *model) -{ - QTreeView::setModel(model); - if (header() && m_alwaysAdjustColumnsAction) - setAlwaysResizeColumnsToContents(m_alwaysAdjustColumnsAction->isChecked()); -} - -void BaseTreeView::mousePressEvent(QMouseEvent *ev) -{ - QTreeView::mousePressEvent(ev); - if (!indexAt(ev->pos()).isValid()) - resizeColumnsToContents(); -} - -void BaseTreeView::resizeColumnsToContents() -{ - const int columnCount = model()->columnCount(); - for (int c = 0 ; c != columnCount; ++c) - resizeColumnToContents(c); -} - -void BaseTreeView::setAlwaysResizeColumnsToContents(bool on) -{ - QHeaderView::ResizeMode mode = on - ? QHeaderView::ResizeToContents : QHeaderView::Interactive; - header()->setResizeMode(0, mode); -} - -void BaseTreeView::headerSectionClicked(int logicalIndex) -{ - resizeColumnToContents(logicalIndex); -} - -void BaseTreeView::reset() -{ - QTreeView::reset(); - if (header() && m_alwaysAdjustColumnsAction - && m_alwaysAdjustColumnsAction->isChecked()) - resizeColumnsToContents(); -} - BaseWindow::BaseWindow(QTreeView *treeView, QWidget *parent) : QWidget(parent), m_treeView(treeView) { diff --git a/src/plugins/debugger/basewindow.h b/src/plugins/debugger/basewindow.h index f4f79f7e329..4a0b95d8b86 100644 --- a/src/plugins/debugger/basewindow.h +++ b/src/plugins/debugger/basewindow.h @@ -33,39 +33,18 @@ #ifndef DEBUGGER_BASEWINDOW_H #define DEBUGGER_BASEWINDOW_H -#include +#include namespace Debugger { namespace Internal { -class BaseTreeView : public QTreeView +class BaseTreeView : public Utils::BaseTreeView { Q_OBJECT public: - BaseTreeView(QWidget *parent = 0); - - void setAlwaysAdjustColumnsAction(QAction *action); + explicit BaseTreeView(QWidget *parent = 0); void addBaseContextActions(QMenu *menu); - bool handleBaseContextAction(QAction *action); - - void setModel(QAbstractItemModel *model); - virtual void rowActivated(const QModelIndex &) {} - void mousePressEvent(QMouseEvent *ev); - -public slots: - void resizeColumnsToContents(); - void setAlwaysResizeColumnsToContents(bool on); - -private slots: - void setAlternatingRowColorsHelper(bool on) { setAlternatingRowColors(on); } - void rowActivatedHelper(const QModelIndex &index) { rowActivated(index); } - void headerSectionClicked(int logicalIndex); - void reset(); - -private: - QAction *m_alwaysAdjustColumnsAction; - QAction *m_adjustColumnsAction; }; class BaseWindow : public QWidget diff --git a/src/plugins/debugger/registerwindow.cpp b/src/plugins/debugger/registerwindow.cpp index 4314cf6fbd6..bf796bb43dd 100644 --- a/src/plugins/debugger/registerwindow.cpp +++ b/src/plugins/debugger/registerwindow.cpp @@ -166,7 +166,6 @@ public: RegisterTreeView::RegisterTreeView(QWidget *parent) : BaseTreeView(parent) { - setAlwaysAdjustColumnsAction(debuggerCore()->action(UseAlternatingRowColors)); setItemDelegate(new RegisterDelegate(this)); } diff --git a/src/plugins/qmljsinspector/qmljspropertyinspector.cpp b/src/plugins/qmljsinspector/qmljspropertyinspector.cpp index 36d2db1bcc8..9f4c325c48c 100644 --- a/src/plugins/qmljsinspector/qmljspropertyinspector.cpp +++ b/src/plugins/qmljsinspector/qmljspropertyinspector.cpp @@ -292,28 +292,13 @@ bool QmlJSPropertyInspectorModel::contentsValid() const } QmlJSPropertyInspector::QmlJSPropertyInspector(QWidget *parent) - : QTreeView(parent) + : Utils::BaseTreeView(parent) { - setAttribute(Qt::WA_MacShowFocusRect, false); - setFrameStyle(QFrame::NoFrame); - setExpandsOnDoubleClick(true); - - header()->setDefaultAlignment(Qt::AlignLeft); - header()->setClickable(true); - setRootIsDecorated(false); - setItemDelegateForColumn(PROPERTY_VALUE_COLUMN, new PropertyEditDelegate(this)); setModel(&m_model); //Add an empty Row to make the headers visible! addRow(QString(), QString(), QString(), -1, false); - connect(header(), SIGNAL(sectionClicked(int)), - SLOT(headerSectionClicked(int))); -} - -void QmlJSPropertyInspector::headerSectionClicked(int logicalIndex) -{ - resizeColumnToContents(logicalIndex); } void QmlJSPropertyInspector::clear() @@ -491,8 +476,6 @@ void QmlJSPropertyInspector::contextMenuEvent(QContextMenuEvent *ev) { QMenu menu; QModelIndex itemIndex = indexAt(ev->pos()); - if (!itemIndex.isValid()) - return; bool isEditable = false; bool isColor = false; if (itemIndex.isValid()) { @@ -507,6 +490,7 @@ void QmlJSPropertyInspector::contextMenuEvent(QContextMenuEvent *ev) QAction colorAction(tr("Choose color"), this); if (isColor) menu.addAction(&colorAction); + addBaseContextActions(&menu); QAction *action = menu.exec(ev->globalPos()); if (action == 0) @@ -516,6 +500,7 @@ void QmlJSPropertyInspector::contextMenuEvent(QContextMenuEvent *ev) openExpressionEditor(itemIndex); if (action == &colorAction) openColorSelector(itemIndex); + handleBaseContextAction(action); } void QmlJSPropertyInspector::openExpressionEditor(const QModelIndex &itemIndex) diff --git a/src/plugins/qmljsinspector/qmljspropertyinspector.h b/src/plugins/qmljsinspector/qmljspropertyinspector.h index 568f28624bb..3080c646527 100644 --- a/src/plugins/qmljsinspector/qmljspropertyinspector.h +++ b/src/plugins/qmljsinspector/qmljspropertyinspector.h @@ -33,7 +33,7 @@ #define PROPERTYINSPECTOR_H #include -#include +#include #include #include @@ -108,7 +108,7 @@ private: bool m_contentsValid; }; -class QmlJSPropertyInspector : public QTreeView +class QmlJSPropertyInspector : public Utils::BaseTreeView { Q_OBJECT public: @@ -138,9 +138,6 @@ public slots: void openExpressionEditor(const QModelIndex &itemIndex); void openColorSelector(const QModelIndex &itemIndex); -private slots: - void headerSectionClicked(int logicalIndex); - private: friend class PropertyEditDelegate; void buildPropertyTree(const QmlDebugObjectReference &);