Commit f674af8b authored by Daniel Teske's avatar Daniel Teske

Introduce Utils::TreeView/TreeWidget

The only view that is sane is my view. Adds handling of return/enter
for macintosh. Allows for overriding whether single or double click
activates items via setActivationMode.

Change-Id: Id8f9001f99162833c682dfc5b2b1abac04ce18a3
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent b5c95ed0
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "itemviews.h"
/*!
\class Utils::TreeView
\brief The TreeView adds setActivationMode to QTreeView
to allow for single click/double click behavior on
platforms where the default is different. Use with care.
Also adds sane keyboard navigation for mac.
*/
/*!
\class Utils::TreeWidget
\brief The TreeWidget adds setActivationMode to QTreeWidget
to allow for single click/double click behavior on
platforms where the default is different. Use with care.
Also adds sane keyboard navigation for mac.
*/
/*!
\class Utils::ListView
\brief The ListView adds setActivationMode to QListView
to allow for single click/double click behavior on
platforms where the default is different. Use with care.
Also adds sane keyboard navigation for mac.
*/
/*!
\class Utils::ListWidget
\brief The ListWidget adds setActivationMode to QListWidget
to allow for single click/double click behavior on
platforms where the default is different. Use with care.
Also adds sane keyboard navigation for mac.
*/
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef TREEVIEW_H
#define TREEVIEW_H
#include "utils_global.h"
static const char activationModeC[] = "ActivationMode";
#include <QListView>
#include <QListWidget>
#include <QTreeView>
#include <QTreeWidget>
#include <QKeyEvent>
namespace Utils {
enum ActivationMode {
DoubleClickActivation = 0,
SingleClickActivation = 1,
PlatformDefaultActivation = 2
};
template<class BaseT>
class View : public BaseT
{
public:
View(QWidget *parent = 0)
: BaseT(parent)
{}
void setActivationMode(ActivationMode mode)
{
if (mode == PlatformDefaultActivation)
BaseT::setProperty(activationModeC, QVariant());
else
BaseT::setProperty(activationModeC, QVariant(bool(mode)));
}
ActivationMode activationMode() const
{
QVariant v = BaseT::property(activationModeC);
if (!v.isValid())
return PlatformDefaultActivation;
return v.toBool() ? SingleClickActivation : DoubleClickActivation;
}
void keyPressEvent(QKeyEvent *event)
{
// Note: This always eats the event
// whereas QAbstractItemView never eats it
if ((event->key() == Qt::Key_Return
|| event->key() == Qt::Key_Enter)
&& event->modifiers() == 0
&& BaseT::currentIndex().isValid()
&& BaseT::state() != QAbstractItemView::EditingState) {
emit BaseT::activated(BaseT::currentIndex());
return;
}
BaseT::keyPressEvent(event);
}
};
class QTCREATOR_UTILS_EXPORT TreeView : public View<QTreeView>
{
Q_OBJECT
public:
TreeView(QWidget *parent = 0)
: View<QTreeView>(parent)
{}
};
class QTCREATOR_UTILS_EXPORT TreeWidget : public View<QTreeWidget>
{
Q_OBJECT
public:
TreeWidget(QWidget *parent = 0)
: View<QTreeWidget>(parent)
{}
};
class QTCREATOR_UTILS_EXPORT ListView : public View<QListView>
{
Q_OBJECT
public:
ListView(QWidget *parent = 0)
: View<QListView>(parent)
{}
};
class QTCREATOR_UTILS_EXPORT ListWidget : public View<QListWidget>
{
Q_OBJECT
public:
ListWidget(QWidget *parent = 0)
: View<QListWidget>(parent)
{}
};
}
#endif // TREEVIEW_H
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
namespace Utils { namespace Utils {
NavigationTreeView::NavigationTreeView(QWidget *parent) NavigationTreeView::NavigationTreeView(QWidget *parent)
: QTreeView(parent) : TreeView(parent)
{ {
setFrameStyle(QFrame::NoFrame); setFrameStyle(QFrame::NoFrame);
setIndentation(indentation() * 9/10); setIndentation(indentation() * 9/10);
...@@ -70,34 +70,19 @@ NavigationTreeView::NavigationTreeView(QWidget *parent) ...@@ -70,34 +70,19 @@ NavigationTreeView::NavigationTreeView(QWidget *parent)
void NavigationTreeView::focusInEvent(QFocusEvent *event) void NavigationTreeView::focusInEvent(QFocusEvent *event)
{ {
if (event->reason() != Qt::PopupFocusReason) if (event->reason() != Qt::PopupFocusReason)
QTreeView::focusInEvent(event); TreeView::focusInEvent(event);
} }
void NavigationTreeView::focusOutEvent(QFocusEvent *event) void NavigationTreeView::focusOutEvent(QFocusEvent *event)
{ {
if (event->reason() != Qt::PopupFocusReason) if (event->reason() != Qt::PopupFocusReason)
QTreeView::focusOutEvent(event); TreeView::focusOutEvent(event);
} }
void NavigationTreeView::resizeEvent(QResizeEvent *event) void NavigationTreeView::resizeEvent(QResizeEvent *event)
{ {
header()->setMinimumSectionSize(viewport()->width()); header()->setMinimumSectionSize(viewport()->width());
QTreeView::resizeEvent(event); TreeView::resizeEvent(event);
} }
#ifdef Q_OS_MAC
void NavigationTreeView::keyPressEvent(QKeyEvent *event)
{
if ((event->key() == Qt::Key_Return
|| event->key() == Qt::Key_Enter)
&& event->modifiers() == 0
&& currentIndex().isValid()
&& state() != QAbstractItemView::EditingState) {
emit activated(currentIndex());
return;
}
QTreeView::keyPressEvent(event);
}
#endif
} // namespace Utils } // namespace Utils
...@@ -32,11 +32,11 @@ ...@@ -32,11 +32,11 @@
#include "utils_global.h" #include "utils_global.h"
#include <QTreeView> #include "itemviews.h"
namespace Utils { namespace Utils {
class QTCREATOR_UTILS_EXPORT NavigationTreeView : public QTreeView class QTCREATOR_UTILS_EXPORT NavigationTreeView : public Utils::TreeView
{ {
Q_OBJECT Q_OBJECT
public: public:
...@@ -46,10 +46,6 @@ protected: ...@@ -46,10 +46,6 @@ protected:
void focusInEvent(QFocusEvent *event); void focusInEvent(QFocusEvent *event);
void focusOutEvent(QFocusEvent *event); void focusOutEvent(QFocusEvent *event);
void resizeEvent(QResizeEvent *event); void resizeEvent(QResizeEvent *event);
#ifdef Q_OS_MAC
void keyPressEvent(QKeyEvent *event);
#endif
}; };
} // Utils } // Utils
......
...@@ -87,7 +87,8 @@ SOURCES += $$PWD/environment.cpp \ ...@@ -87,7 +87,8 @@ SOURCES += $$PWD/environment.cpp \
$$PWD/ansiescapecodehandler.cpp \ $$PWD/ansiescapecodehandler.cpp \
$$PWD/execmenu.cpp \ $$PWD/execmenu.cpp \
$$PWD/completinglineedit.cpp \ $$PWD/completinglineedit.cpp \
$$PWD/winutils.cpp $$PWD/winutils.cpp \
$$PWD/itemviews.cpp
win32:SOURCES += $$PWD/consoleprocess_win.cpp win32:SOURCES += $$PWD/consoleprocess_win.cpp
else:SOURCES += $$PWD/consoleprocess_unix.cpp else:SOURCES += $$PWD/consoleprocess_unix.cpp
...@@ -178,7 +179,8 @@ HEADERS += \ ...@@ -178,7 +179,8 @@ HEADERS += \
$$PWD/execmenu.h \ $$PWD/execmenu.h \
$$PWD/completinglineedit.h \ $$PWD/completinglineedit.h \
$$PWD/logging.h \ $$PWD/logging.h \
$$PWD/winutils.h $$PWD/winutils.h \
$$PWD/itemviews.h
FORMS += $$PWD/filewizardpage.ui \ FORMS += $$PWD/filewizardpage.ui \
$$PWD/projectintropage.ui \ $$PWD/projectintropage.ui \
......
...@@ -99,6 +99,8 @@ QtcLibrary { ...@@ -99,6 +99,8 @@ QtcLibrary {
"htmldocextractor.h", "htmldocextractor.h",
"ipaddresslineedit.cpp", "ipaddresslineedit.cpp",
"ipaddresslineedit.h", "ipaddresslineedit.h",
"itemviews.cpp",
"itemviews.h",
"iwelcomepage.cpp", "iwelcomepage.cpp",
"iwelcomepage.h", "iwelcomepage.h",
"json.cpp", "json.cpp",
......
...@@ -360,6 +360,13 @@ int ManhattanStyle::styleHint(StyleHint hint, const QStyleOption *option, const ...@@ -360,6 +360,13 @@ int ManhattanStyle::styleHint(StyleHint hint, const QStyleOption *option, const
case QStyle::SH_ItemView_ArrowKeysNavigateIntoChildren: case QStyle::SH_ItemView_ArrowKeysNavigateIntoChildren:
ret = true; ret = true;
break; break;
case QStyle::SH_ItemView_ActivateItemOnSingleClick:
// default depends on the style
if (widget) {
QVariant activationMode = widget->property("ActivationMode");
if (activationMode.isValid())
ret = activationMode.toBool();
}
default: default:
break; break;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment