Skip to content

Commit

Permalink
added AccessibilityProxy for GUI testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lexxmark committed Mar 5, 2015
1 parent 7bf867e commit 9f0adc0
Show file tree
Hide file tree
Showing 17 changed files with 383 additions and 33 deletions.
26 changes: 1 addition & 25 deletions Core/PropertyBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,13 @@
#include <QtScript/QScriptEngine>

static int qtnPropertyChangeReasonMetaId = qRegisterMetaType<QtnPropertyChangeReason>("QtnPropertyChangeReason");
static int qtnPropertyStateMetaId = qRegisterMetaType<QtnPropertyState>("QtnPropertyState");
static int qtnPropertyPtrId = qRegisterMetaType<QtnPropertyBase*>("QtnPropertyBase*");
static quint16 qtnPropertyMagicNumber = 0x1984;

// extern declaration
void qtnAddPropertyAsChild(QObject* parent, QtnPropertyBase* child, bool moveOwnership);
void qtnRemovePropertyAsChild(QObject* parent, QtnPropertyBase* child);

static QScriptValue qtnPropertyIdToScriptValue(QScriptEngine* engine, const QtnPropertyID& val)
{
QScriptValue obj((int)val);
return obj;
}

static void qtnPropertyIdFromScriptValue(const QScriptValue& obj, QtnPropertyID& val)
{
val = obj.toInt32();
}

static QScriptValue qtnPropertyStateToScriptValue(QScriptEngine* engine, const QtnPropertyState& val)
{
QScriptValue obj((QtnPropertyState::Int)val);
return obj;
}

static void qtnPropertyStateFromScriptValue(const QScriptValue& obj, QtnPropertyState& val)
{
val = (QtnPropertyState::enum_type)obj.toInt32();
}

static QScriptValue qtnPropertyChangeReasonToScriptValue(QScriptEngine* engine, const QtnPropertyChangeReason& val)
{
QScriptValue obj((QtnPropertyChangeReason::Int)val);
Expand Down Expand Up @@ -85,8 +63,6 @@ static void qtnPropertyBasePtrFromScriptValue(const QScriptValue& obj, QtnProper

void qtnScriptRegisterPropertyTypes(QScriptEngine* engine)
{
qScriptRegisterMetaType(engine, qtnPropertyIdToScriptValue, qtnPropertyIdFromScriptValue);
qScriptRegisterMetaType(engine, qtnPropertyStateToScriptValue, qtnPropertyStateFromScriptValue);
qScriptRegisterMetaType(engine, qtnPropertyChangeReasonToScriptValue, qtnPropertyChangeReasonFromScriptValue);
qScriptRegisterMetaType(engine, qtnPropertyValuePtrToScriptValue, qtnPropertyValuePtrFromScriptValue);
qScriptRegisterMetaType(engine, qtnPropertyBasePtrToScriptValue, qtnPropertyBasePtrFromScriptValue);
Expand Down
5 changes: 3 additions & 2 deletions Core/PropertyBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ class QTN_PE_CORE_EXPORT QtnPropertyBase: public QObject
public: // properties for scripting
Q_PROPERTY(QString name READ name)
Q_PROPERTY(QString description READ description)
Q_PROPERTY(QtnPropertyID id READ id)
Q_PROPERTY(qint32 id READ id)
Q_PROPERTY(bool isEditable READ isEditableByUser)
Q_PROPERTY(QtnPropertyState state READ state)
Q_PROPERTY(quint32 state READ state)
Q_PROPERTY(QVariant value READ valueAsVariant WRITE setValueAsVariant)

Q_SIGNALS:
Expand Down Expand Up @@ -132,5 +132,6 @@ QTN_PE_CORE_EXPORT QDataStream& operator>> (QDataStream& stream, QtnPropertyBase
QTN_PE_CORE_EXPORT void qtnScriptRegisterPropertyTypes(QScriptEngine* engine);

Q_DECLARE_METATYPE(const QtnPropertyBase*)
Q_DECLARE_METATYPE(QtnPropertyBase*)

#endif // QTN_PROPERTY_BASE_H
3 changes: 3 additions & 0 deletions Core/PropertySet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "PropertySet.h"
#include <QRegularExpression>

static int qtnPropertySetPtrId = qRegisterMetaType<QtnPropertySet*>("QtnPropertySet*");


void qtnAddPropertyAsChild(QObject *parent, QtnPropertyBase* child, bool moveOwnership)
{
QtnPropertySet* propertySet = qobject_cast<QtnPropertySet*>(parent);
Expand Down
3 changes: 3 additions & 0 deletions Core/PropertySet.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class QTN_PE_CORE_EXPORT QtnPropertySet: public QtnPropertyBase
explicit QtnPropertySet(QObject* parent);
virtual ~QtnPropertySet();

public slots:
// sub properties
bool hasChildProperties() const { return !m_childProperties.empty(); }
const QList<QtnPropertyBase*>& childProperties() const { return m_childProperties; }
Expand Down Expand Up @@ -85,4 +86,6 @@ class QTN_PE_CORE_EXPORT QtnPropertySet: public QtnPropertyBase
friend void qtnDisconnectChildProperty(QtnPropertySet* masterProperty, QtnPropertyBase* childProperty);
};

Q_DECLARE_METATYPE(QtnPropertySet*)

#endif // QTN_PROPERTY_SET_H
3 changes: 3 additions & 0 deletions Demo/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ui_MainWindow.h"
#include "mydialog.h"
#include <QMessageBox>
#include <QDesktopWidget>

#include "QObjectPropertySet.h"
#include "Demo.peg.h"
Expand All @@ -21,6 +22,8 @@ MainWindow::MainWindow(QWidget *parent) :
jsEngine.globalObject().setProperty("samplePS", jsEngine.newQObject(ps));

dbg.attachTo(&jsEngine);

move(QApplication::desktop()->availableGeometry().center() - rect().center());
}

MainWindow::~MainWindow()
Expand Down
4 changes: 2 additions & 2 deletions Demo/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<x>0</x>
<y>0</y>
<width>742</width>
<height>417</height>
<height>468</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -51,7 +51,7 @@
<item>
<widget class="QPushButton" name="editButton">
<property name="text">
<string>Edit in modal dialog</string>
<string>Edit in modal dialog...</string>
</property>
</widget>
</item>
Expand Down
7 changes: 6 additions & 1 deletion PropertyWidget/Delegates/PropertyDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ bool QtnPropertyDelegate::acceptKeyPressedForInplaceEdit(QKeyEvent* keyEvent) co

QWidget* QtnPropertyDelegate::createValueEditor(QWidget* parent, const QRect& rect, QtnInplaceInfo* inplaceInfo)
{
return createValueEditorImpl(parent, rect, inplaceInfo);
QWidget* valueEditor = createValueEditorImpl(parent, rect, inplaceInfo);
if (!valueEditor)
return valueEditor;

valueEditor->setObjectName("QtnPropertyValueEditor");
return valueEditor;
}

void QtnPropertyDelegate::drawValueImpl(QStylePainter& painter, const QRect& rect, const QStyle::State& state, bool* needTooltip) const
Expand Down
12 changes: 11 additions & 1 deletion PropertyWidget/PropertyView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ QtnPropertyView::QtnPropertyView(QWidget* parent, QtnPropertySet* propertySet)
m_itemHeightSpacing(6),
m_leadMargin(0),
m_splitRatio(0.5f),
m_rubberBand(nullptr)
m_rubberBand(nullptr),
m_accessibilityProxy(nullptr)
{
setFocusPolicy(Qt::StrongFocus);
viewport()->setMouseTracking(true);
Expand All @@ -93,6 +94,15 @@ QtnPropertyView::~QtnPropertyView()
{
}

QtnAccessibilityProxy *QtnPropertyView::accessibilityProxy()
{
if (!m_accessibilityProxy)
m_accessibilityProxy = new QtnAccessibilityProxy(this);

return m_accessibilityProxy;
}


void QtnPropertyView::setPropertySet(QtnPropertySet* newPropertySet)
{
if (m_propertySet)
Expand Down
7 changes: 7 additions & 0 deletions PropertyWidget/PropertyView.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "PropertyWidgetAPI.h"
#include "Delegates/PropertyDelegateFactory.h"
#include "../Core/PropertySet.h"
#include "Utils/AccessibilityProxy.h"

#include <QAbstractScrollArea>
#include <QScopedPointer>
Expand Down Expand Up @@ -77,6 +78,9 @@ class QTN_PW_EXPORT QtnPropertyView: public QAbstractScrollArea
void addPropertyViewStyle(QtnPropertyViewStyle style);
void removePropertyViewStyle(QtnPropertyViewStyle style);

public slots:
QtnAccessibilityProxy* accessibilityProxy();

Q_SIGNALS:
// emits when active property has changed
void activePropertyChanged(QtnPropertyBase* activeProperty);
Expand Down Expand Up @@ -190,6 +194,9 @@ class QTN_PW_EXPORT QtnPropertyView: public QAbstractScrollArea

float m_splitRatio;
QRubberBand* m_rubberBand;

friend class QtnAccessibilityProxy;
QtnAccessibilityProxy* m_accessibilityProxy;
};

#endif // QTN_PROPERTYVIEW_H
6 changes: 4 additions & 2 deletions PropertyWidget/PropertyWidget.pro
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ SOURCES += PropertyWidget.cpp \
Delegates/PropertyEditorAux.cpp \
Delegates/Core/PropertyDelegateQSize.cpp \
Delegates/Core/PropertyDelegateQPoint.cpp \
Delegates/GUI/PropertyDelegateQFont.cpp
Delegates/GUI/PropertyDelegateQFont.cpp \
Utils/AccessibilityProxy.cpp

HEADERS += PropertyWidgetAPI.h \
PropertyWidget.h \
Expand All @@ -49,7 +50,8 @@ HEADERS += PropertyWidgetAPI.h \
Delegates/PropertyEditorAux.h \
Delegates/Core/PropertyDelegateQSize.h \
Delegates/Core/PropertyDelegateQPoint.h \
Delegates/GUI/PropertyDelegateQFont.h
Delegates/GUI/PropertyDelegateQFont.h \
Utils/AccessibilityProxy.h

LIBS += -L$$BIN_DIR -lQtnPropertyCore

Expand Down
146 changes: 146 additions & 0 deletions PropertyWidget/Utils/AccessibilityProxy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
Copyright (c) 2012-1015 Alex Zhondin <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "AccessibilityProxy.h"
#include "../PropertyView.h"
#include <QMessageBox>

static int typeID = qRegisterMetaType<QtnAccessibilityProxy*>("QtnAccessibilityProxy*");

QtnAccessibilityProxy::QtnAccessibilityProxy(QtnPropertyView* owner)
: QObject(owner),
m_owner(owner)
{
}

QtnAccessibilityProxy::~QtnAccessibilityProxy()
{
}

QtnPropertyView* QtnAccessibilityProxy::owner()
{
return m_owner;
}

QtnPropertySet* QtnAccessibilityProxy::propertySet()
{
return m_owner->propertySet();
}

QtnPropertyBase* QtnAccessibilityProxy::activeProperty()
{
return m_owner->activeProperty();
}

QtnPropertyBase* QtnAccessibilityProxy::findProperty(QString nameOrPath)
{
auto root = m_owner->propertySet();
if (!root)
return nullptr;

auto properties = root->findChildProperties(nameOrPath);
if (properties.size() != 1)
return nullptr;

return properties[0];
}

QtnPropertyBase* QtnAccessibilityProxy::propertyUnderPoint(QPoint point)
{
int index = m_owner->visibleItemIndexByPoint(point);
if (index < 0)
return nullptr;

return m_owner->m_visibleItems[index].item->property;
}

void QtnAccessibilityProxy::ensureVisibleProperty(QtnPropertyBase* property)
{
if (!property)
return;

auto propertySet = qobject_cast<QtnPropertySet*>(property->parent());
while (propertySet)
{
propertySet->removeState(QtnPropertyStateCollapsed);
propertySet = qobject_cast<QtnPropertySet*>(propertySet->parent());
}

m_owner->ensureVisible(property);
}

QRect QtnAccessibilityProxy::propertyNameRect(QtnPropertyBase* property)
{
if (!property)
return QRect();

int index = m_owner->visibleItemIndexByProperty(property);
if (index < 0)
return QRect();

QRect rect = m_owner->visibleItemRect(index);
rect.setRight(m_owner->splitPosition());
return rect;
}

QRect QtnAccessibilityProxy::propertyValueRect(QtnPropertyBase* property)
{
if (!property)
return QRect();

int index = m_owner->visibleItemIndexByProperty(property);
if (index < 0)
return QRect();

QRect rect = m_owner->visibleItemRect(index);
rect.setLeft(m_owner->splitPosition() + 1);
return rect;
}

QRect QtnAccessibilityProxy::propertyActionRect(QtnPropertyBase* property, int actionIndex)
{
if (!property)
return QRect();

int index = m_owner->visibleItemIndexByProperty(property);
if (index < 0)
return QRect();

const auto& item = m_owner->m_visibleItems[index];
if (!item.actionsValid)
return QRect();

if (actionIndex < 0 || actionIndex >= item.actions.size())
return QRect();

return item.actions[actionIndex].rect;
}

QString QtnAccessibilityProxy::propertyDelegateName(QtnPropertyBase* property)
{
if (!property)
return QString();

auto theProperty = property->asProperty();
if (!theProperty)
return QString();

if (!theProperty->delegate())
return QString("default");

return theProperty->delegate()->name;
}

Loading

0 comments on commit 9f0adc0

Please sign in to comment.