Commit 3e977b38 authored by Yuya Nishihara's avatar Yuya Nishihara
Browse files

manage tile layout parameters by C++ TileListModel

parent 556406f9
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQml 2.15 // Binding.restoreMode
import MyTileExample 1.0
Control {
id: root
property int majorOrientation: Qt.Horizontal
property int majorCount: 2
property var minorCounts: [5, 5]
readonly property int majorOrientation: model.majorOrientation
readonly property int majorCount: model.majorCount
readonly property var minorCounts: model.minorCounts
property real handleWidth: 5
property real handleHeight: 5
......@@ -15,7 +16,7 @@ Control {
property var _majorPositions: _makeDefaultPositionsArray(majorCount)
property var _minorPositions: _makeDefaultPositionsArray(Math.max(...minorCounts))
property alias model: contentRepeater.model
property TileListModel model
property Component delegate
function _makeDefaultPositionsArray(count) {
......@@ -51,6 +52,8 @@ Control {
Repeater {
id: contentRepeater
model: root.model
Item {
id: tile
......
......@@ -19,6 +19,7 @@ Window {
TileListModel {
id: tileListModel
Component.onCompleted: {
resetLayout(Qt.Horizontal, [5, 5]);
for (let i = 0; i < 10; ++i) {
assignTileItem(String.fromCharCode(65 + i));
}
......@@ -32,7 +33,6 @@ Window {
id: tileView
Layout.fillWidth: true
Layout.fillHeight: true
majorOrientation: Qt.Horizontal
model: tileListModel
delegate: GridLayout {
id: tile
......@@ -85,40 +85,41 @@ Window {
SpinBox {
from: 0
to: 1
value: tileView.majorOrientation
value: tileListModel.majorOrientation
onValueModified: {
tileView.majorOrientation = value;
tileListModel.resetLayout(value, tileListModel.minorCounts);
}
}
Label { text: "majorCount:" }
SpinBox {
enabled: false
editable: true
value: tileView.majorCount
value: tileListModel.majorCount
onValueModified: {
tileView.majorCount = value;
// TODO
}
}
Label { text: "minorCount[0]:" }
SpinBox {
editable: true
value: tileView.minorCounts[0]
value: tileListModel.minorCounts[0]
onValueModified: {
let counts = tileView.minorCounts;
let counts = tileListModel.minorCounts.slice();
counts[0] = value;
tileView.minorCounts = counts;
tileListModel.resetLayout(tileListModel.majorOrientation, counts);
}
}
Label { text: "minorCount[1]:" }
SpinBox {
editable: true
value: tileView.minorCounts[1]
value: tileListModel.minorCounts[1]
onValueModified: {
let counts = tileView.minorCounts;
let counts = tileListModel.minorCounts.slice();
counts[1] = value;
tileView.minorCounts = counts;
tileListModel.resetLayout(tileListModel.majorOrientation, counts);
}
}
......
......@@ -8,7 +8,10 @@ enum Role : int {
};
}
TileListModel::TileListModel(QObject *parent) : QAbstractListModel(parent), m_tiles() { }
TileListModel::TileListModel(QObject *parent)
: QAbstractListModel(parent), m_majorOrientation(Qt::Horizontal), m_minorCounts(), m_tiles()
{
}
QHash<int, QByteArray> TileListModel::roleNames() const
{
......@@ -44,6 +47,28 @@ QVariant TileListModel::data(const QModelIndex &index, int role) const
return {};
}
Qt::Orientation TileListModel::majorOrientation() const
{
return m_majorOrientation;
}
int TileListModel::majorCount() const
{
return m_minorCounts.size();
}
QVector<int> TileListModel::minorCounts() const
{
return m_minorCounts;
}
void TileListModel::resetLayout(Qt::Orientation majorOrientation, const QVector<int> &minorCounts)
{
m_majorOrientation = majorOrientation;
m_minorCounts = minorCounts;
emit layoutReset();
}
void TileListModel::assignTileItem(const QString &name)
{
const int row = static_cast<int>(m_tiles.size());
......
......@@ -2,11 +2,15 @@
#define TILELISTMODEL_H
#include <QAbstractListModel>
#include <QVector>
#include <vector>
class TileListModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(int majorCount READ majorCount NOTIFY layoutReset)
Q_PROPERTY(QVector<int> minorCounts READ minorCounts NOTIFY layoutReset)
Q_PROPERTY(Qt::Orientation majorOrientation READ majorOrientation NOTIFY layoutReset)
public:
explicit TileListModel(QObject *parent = nullptr);
......@@ -16,9 +20,17 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
Qt::Orientation majorOrientation() const;
int majorCount() const;
QVector<int> minorCounts() const;
Q_INVOKABLE void resetLayout(Qt::Orientation majorOrientation, const QVector<int> &minorCounts);
Q_INVOKABLE void assignTileItem(const QString &name);
Q_INVOKABLE void setTileIndex(int row, int tileIndex);
signals:
void layoutReset();
private:
struct Tile
{
......@@ -26,6 +38,8 @@ private:
int tileIndex;
};
Qt::Orientation m_majorOrientation;
QVector<int> m_minorCounts;
std::vector<Tile> m_tiles;
};
......
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