Refactored using new GridView approach

This commit is contained in:
vladest 2017-12-23 17:44:26 +01:00
parent dc22b814c2
commit a497047666
3 changed files with 123 additions and 133 deletions

View file

@ -3,6 +3,8 @@ import QtQuick.Controls 2.2
import QtGraphicalEffects 1.0 import QtGraphicalEffects 1.0
import QtQuick.Layouts 1.3 import QtQuick.Layouts 1.3
import TabletScriptingInterface 1.0
import "." import "."
import "../../styles-uit" import "../../styles-uit"
import "../audio" as HifiAudio import "../audio" as HifiAudio
@ -14,12 +16,10 @@ Item {
property int rowIndex: 6 // by default property int rowIndex: 6 // by default
property int columnIndex: 1 // point to 'go to location' property int columnIndex: 1 // point to 'go to location'
readonly property int buttonsOnPage: 12
readonly property int buttonsRowsOnPage: 4
readonly property int buttonsColumnsOnPage: 3
property var currentGridItems: null property var currentGridItems: null
property var gridPages: [];
focus: true
Rectangle { Rectangle {
id: bgTopBar id: bgTopBar
@ -116,20 +116,21 @@ Item {
clip: false clip: false
currentIndex: -1 currentIndex: -1
property int previousIndex: -1 property int previousIndex: -1
Repeater { Repeater {
id: pageRepeater id: pageRepeater
model: Math.ceil(tabletProxy.buttons.rowCount() / buttonsOnPage) model: Math.ceil(tabletProxy.buttons.rowCount() / TabletEnums.ButtonsOnPage)
onCountChanged: console.warn("repeat count", count, tabletProxy.buttons.rowCount())
onItemAdded: { onItemAdded: {
item.pageIndex = index item.proxyModel.sourceModel = tabletProxy.buttons;
item.proxyModel.pageIndex = index;
} }
delegate: Item { delegate: Item {
id: page id: page
property int pageIndex property TabletButtonsProxyModel proxyModel: TabletButtonsProxyModel {}
onPageIndexChanged: console.warn("page index", pageIndex)
Grid { GridView {
id: flickable
keyNavigationEnabled: false
anchors { anchors {
fill: parent fill: parent
topMargin: 20 topMargin: 20
@ -137,71 +138,57 @@ Item {
rightMargin: 30 rightMargin: 30
bottomMargin: 0 bottomMargin: 0
} }
rows: 4; columns: 3; rowSpacing: 16; columnSpacing: 16; cellWidth: width/3
Repeater { cellHeight: cellWidth
id: buttonsRepeater flow: GridView.LeftToRight
model: tabletProxy.buttons.rowCount() - (((page.pageIndex + 1) * buttonsOnPage) % buttonsOnPage) model: page.proxyModel
delegate: Item {
id: wrapper
width: 129
height: 129
property var proxy: modelData
TabletButton { delegate: Item {
id: tabletButton id: wrapper
anchors.centerIn: parent width: flickable.cellWidth
onClicked: modelData.clicked() height: flickable.cellHeight
state: wrapper.GridView.isCurrentItem ? "hover state" : "base state"
}
Connections { property var proxy: modelData
target: modelData;
onPropertiesChanged: {
updateProperties();
}
}
Component.onCompleted: updateProperties() TabletButton {
id: tabletButton
anchors.centerIn: parent
onClicked: modelData.clicked()
state: wrapper.GridView.isCurrentItem ? "hover state" : "base state"
}
function updateProperties() { Connections {
var keys = Object.keys(modelData.properties).forEach(function (key) { target: modelData;
if (tabletButton[key] !== modelData.properties[key]) { onPropertiesChanged: {
tabletButton[key] = modelData.properties[key]; updateProperties();
}
});
} }
} }
}
Component.onCompleted: { Component.onCompleted: updateProperties()
// tabletProxy.buttonsOnPage.setCurrentPage(page.index);
// buttonsRepeater.model = tabletProxy.buttonsOnPage; function updateProperties() {
// console.warn("buttons on page:", page.index, tabletProxy.buttonsOnPage.rowCount()) var keys = Object.keys(modelData.properties).forEach(function (key) {
if (tabletButton[key] !== modelData.properties[key]) {
tabletButton[key] = modelData.properties[key];
}
});
}
} }
} }
} }
} }
onCurrentIndexChanged: { onCurrentIndexChanged: {
// if (swipeView.currentIndex < 0 if (swipeView.currentIndex < 0
// || swipeView.itemAt(swipeView.currentIndex) === null || swipeView.itemAt(swipeView.currentIndex) === null
// || swipeView.itemAt(swipeView.currentIndex).children[0] === null || swipeView.itemAt(swipeView.currentIndex).children[0] === null) {
// || swipeView.itemAt(swipeView.currentIndex).children[0].children === null) { return;
// return; }
// }
// currentGridItems = swipeView.itemAt(swipeView.currentIndex).children[0].children; currentGridItems = swipeView.itemAt(swipeView.currentIndex).children[0];
// var row = rowIndex < 0 ? 0 : rowIndex; currentGridItems.currentIndex = (previousIndex > swipeView.currentIndex ? currentGridItems.count - 1 : 0);
// var column = previousIndex > swipeView.currentIndex ? buttonsColumnsOnPage - 1 : 0; previousIndex = currentIndex;
// var index = row * buttonsColumnsOnPage + column;
// if (index < 0 || index >= currentGridItems.length) {
// column = 0;
// row = 0;
// }
// rowIndex = row;
// columnIndex = column;
// setCurrentItemState("hover state");
// previousIndex = currentIndex;
} }
hoverEnabled: true hoverEnabled: true
@ -241,70 +228,61 @@ Item {
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
count: swipeView.count count: swipeView.count
} }
// GridView { }
// id: flickable
// anchors.top: parent.top
// anchors.topMargin: 15
// anchors.bottom: parent.bottom
// anchors.horizontalCenter: parent.horizontalCenter
// width: cellWidth * 3
// cellHeight: 145
// cellWidth: 145
// flow: GridView.LeftToRight
// model: tabletProxy.buttons
// delegate: Item {
// width: flickable.cellWidth
// height: flickable.cellHeight
// property var proxy: modelData
// TabletButton { function setCurrentItemState(state) {
// id: tabletButton // var buttonIndex = rowIndex * TabletEnums.ButtonsColumnsOnPage + columnIndex;
// anchors.centerIn: parent // if (currentGridItems !== null && buttonIndex >= 0 && buttonIndex < currentGridItems.length) {
// onClicked: modelData.clicked() // if (currentGridItems[buttonIndex].isActive) {
// state: wrapper.GridView.isCurrentItem ? "hover state" : "base state" // currentGridItems[buttonIndex].state = "active state";
// } // } else {
// currentGridItems[buttonIndex].state = state;
// Connections {
// target: modelData;
// onPropertiesChanged: {
// updateProperties();
// }
// }
// Component.onCompleted: updateProperties()
// function updateProperties() {
// var keys = Object.keys(modelData.properties).forEach(function (key) {
// if (tabletButton[key] !== modelData.properties[key]) {
// tabletButton[key] = modelData.properties[key];
// }
// });
// }
// } // }
// } // }
} }
function setCurrentItemState(state) { Component.onCompleted: {
var buttonIndex = rowIndex * buttonsColumnsOnPage + columnIndex; focus = true;
if (currentGridItems !== null && buttonIndex >= 0 && buttonIndex < currentGridItems.length) { forceActiveFocus();
if (currentGridItems[buttonIndex].isActive) { }
currentGridItems[buttonIndex].state = "active state";
} else { Keys.onRightPressed: {
currentGridItems[buttonIndex].state = state; if (!currentGridItems) {
return;
}
var index = currentGridItems.currentIndex;
currentGridItems.moveCurrentIndexRight();
console.warn("onRightPressed", index, currentGridItems.currentIndex, currentGridItems.count)
if (index === currentGridItems.count - 1 && index === currentGridItems.currentIndex) {
if (swipeView.currentIndex < swipeView.count - 1) {
swipeView.incrementCurrentIndex();
} }
} }
} }
// Keys.onRightPressed: flickable.moveCurrentIndexRight(); Keys.onLeftPressed: {
// Keys.onLeftPressed: flickable.moveCurrentIndexLeft(); if (!currentGridItems) {
// Keys.onDownPressed: flickable.moveCurrentIndexDown(); return;
// Keys.onUpPressed: flickable.moveCurrentIndexUp(); }
// Keys.onReturnPressed: {
// if (flickable.currentItem) { var index = currentGridItems.currentIndex;
// flickable.currentItem.proxy.clicked(); currentGridItems.moveCurrentIndexLeft();
// if (tabletRoot) { console.warn("onLeftPressed", index, currentGridItems.currentIndex, currentGridItems.count)
// tabletRoot.playButtonClickSound(); if (index === 0 && index === currentGridItems.currentIndex) {
// } if (swipeView.currentIndex > 0) {
// } swipeView.decrementCurrentIndex();
// } }
}
}
Keys.onDownPressed: currentGridItems.moveCurrentIndexDown();
Keys.onUpPressed: currentGridItems.moveCurrentIndexUp();
Keys.onReturnPressed: {
if (currentGridItems.currentItem) {
currentGridItems.currentItem.proxy.clicked();
if (tabletRoot) {
tabletRoot.playButtonClickSound();
}
}
}
} }

View file

@ -93,21 +93,28 @@ void TabletButtonListModel::removeButton(TabletButtonProxy* button) {
} }
TabletButtonsProxyModel::TabletButtonsProxyModel(QObject *parent) TabletButtonsProxyModel::TabletButtonsProxyModel(QObject *parent)
: QSortFilterProxyModel(parent){ : QSortFilterProxyModel(parent), _pageIndex(-1) {
} }
int TabletButtonsProxyModel::currentPage() const { int TabletButtonsProxyModel::pageIndex() const {
return _currentPage; return _pageIndex;
} }
void TabletButtonsProxyModel::setCurrentPage(int currentPage) { void TabletButtonsProxyModel::setPageIndex(int pageIndex)
_currentPage = currentPage; {
if (_pageIndex == pageIndex)
return;
_pageIndex = pageIndex;
invalidateFilter(); invalidateFilter();
emit pageIndexChanged(_pageIndex);
} }
bool TabletButtonsProxyModel::filterAcceptsRow(int sourceRow, bool TabletButtonsProxyModel::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const { const QModelIndex &sourceParent) const {
return (sourceRow >= _currentPage*12 && sourceRow < _currentPage*12); Q_UNUSED(sourceParent);
return (sourceRow >= _pageIndex*TabletScriptingInterface::ButtonsOnPage
&& sourceRow < (_pageIndex + 1)*TabletScriptingInterface::ButtonsOnPage);
} }
bool TabletButtonsProxyModel::lessThan(const QModelIndex &left, bool TabletButtonsProxyModel::lessThan(const QModelIndex &left,
@ -300,7 +307,6 @@ TabletProxy::TabletProxy(QObject* parent, const QString& name) : QObject(parent)
qCWarning(uiLogging) << "Creating tablet proxy on wrong thread " << _name; qCWarning(uiLogging) << "Creating tablet proxy on wrong thread " << _name;
} }
connect(this, &TabletProxy::tabletShownChanged, this, &TabletProxy::onTabletShown); connect(this, &TabletProxy::tabletShownChanged, this, &TabletProxy::onTabletShown);
_buttonsProxy.setSourceModel(&_buttons);
} }
TabletProxy::~TabletProxy() { TabletProxy::~TabletProxy() {

View file

@ -47,6 +47,10 @@ public:
enum TabletAudioEvents { ButtonClick, ButtonHover, TabletOpen, TabletHandsIn, TabletHandsOut, Last}; enum TabletAudioEvents { ButtonClick, ButtonHover, TabletOpen, TabletHandsIn, TabletHandsOut, Last};
Q_ENUM(TabletAudioEvents) Q_ENUM(TabletAudioEvents)
//Different useful constants
enum TabletConstants { ButtonsColumnsOnPage = 3, ButtonsRowsOnPage = 4, ButtonsOnPage = 12 };
Q_ENUM(TabletConstants)
TabletScriptingInterface(); TabletScriptingInterface();
virtual ~TabletScriptingInterface(); virtual ~TabletScriptingInterface();
static const QString QML; static const QString QML;
@ -123,18 +127,23 @@ class TabletButtonsProxyModel : public QSortFilterProxyModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(int pageIndex READ pageIndex WRITE setPageIndex NOTIFY pageIndexChanged)
public: public:
TabletButtonsProxyModel(QObject *parent = 0); TabletButtonsProxyModel(QObject *parent = 0);
int pageIndex() const;
int currentPage() const; public slots:
Q_INVOKABLE void setCurrentPage(int currentPage); void setPageIndex(int pageIndex);
signals:
void pageIndexChanged(int pageIndex);
protected: protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override; bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
private: private:
int _currentPage; int _pageIndex;
}; };
Q_DECLARE_METATYPE(TabletButtonsProxyModel*); Q_DECLARE_METATYPE(TabletButtonsProxyModel*);
@ -152,7 +161,6 @@ class TabletProxy : public QObject {
Q_PROPERTY(bool landscape READ getLandscape WRITE setLandscape) Q_PROPERTY(bool landscape READ getLandscape WRITE setLandscape)
Q_PROPERTY(bool tabletShown MEMBER _tabletShown NOTIFY tabletShownChanged) Q_PROPERTY(bool tabletShown MEMBER _tabletShown NOTIFY tabletShownChanged)
Q_PROPERTY(TabletButtonListModel* buttons READ getButtons CONSTANT) Q_PROPERTY(TabletButtonListModel* buttons READ getButtons CONSTANT)
Q_PROPERTY(TabletButtonsProxyModel* buttonsOnPage READ getButtonsOnPage CONSTANT)
public: public:
TabletProxy(QObject* parent, const QString& name); TabletProxy(QObject* parent, const QString& name);
~TabletProxy(); ~TabletProxy();
@ -256,7 +264,6 @@ public:
QQuickItem* getQmlMenu() const; QQuickItem* getQmlMenu() const;
TabletButtonListModel* getButtons() { return &_buttons; } TabletButtonListModel* getButtons() { return &_buttons; }
TabletButtonsProxyModel* getButtonsOnPage() { return &_buttonsProxy; }
signals: signals:
/**jsdoc /**jsdoc
@ -317,7 +324,6 @@ protected:
bool _showRunningScripts { false }; bool _showRunningScripts { false };
TabletButtonListModel _buttons; TabletButtonListModel _buttons;
TabletButtonsProxyModel _buttonsProxy;
}; };
Q_DECLARE_METATYPE(TabletProxy*); Q_DECLARE_METATYPE(TabletProxy*);