Commit 3aa8cef3 authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Much better version, using a Flickable as parent and configuring it

parent 241473a2
......@@ -62,7 +62,7 @@ public:
return (idx >= 0 && idx < count () ? m_itemsList.at (idx) : Q_NULLPTR);
}
void append (T * item) {
void append (T * item) {
if (item != Q_NULLPTR && !contains (item)) {
const int idx { m_itemsList.count () };
m_itemsList.append (item);
......@@ -70,6 +70,19 @@ public:
emit countChanged ();
}
}
void appendList (const QList<T *> itemsList) {
if (!itemsList.isEmpty ()) {
for (T * item : itemsList) {
int idx { m_itemsList.count () };
if (item != Q_NULLPTR && !contains (item)) {
m_itemsList.append (item);
emit itemInserted (item, idx);
++idx;
}
}
emit countChanged ();
}
}
void prepend (T * item) {
if (item != Q_NULLPTR && !contains (item)) {
const int idx { 0 };
......
This diff is collapsed.
......@@ -16,6 +16,12 @@ class QQuickFastObjectListView : public QQuickItem {
Q_PROPERTY (QObject * current READ getCurrent WRITE setCurrent NOTIFY currentChanged)
Q_PROPERTY (int behavior READ getBehavior WRITE setBehavior NOTIFY behaviorChanged)
struct Holder {
QObject * modelItem { Q_NULLPTR };
QQuickItem * delegateInstance { Q_NULLPTR };
QQmlContext * context { Q_NULLPTR };
};
public:
explicit QQuickFastObjectListView (QQuickItem * parent = Q_NULLPTR);
virtual ~QQuickFastObjectListView (void);
......@@ -45,38 +51,43 @@ signals:
void behaviorChanged (void);
protected slots:
void doMarkDirty (void);
void doResetInstances (void);
void doPrepareInstances (void);
void doAttachToModel (void);
void doDetachFromModel (void);
void onItemsCleared (void);
void doChangePositionY (const int posY);
void doInstantiate (Holder * holder);
void doEliminate (Holder * holder);
void onItemInserted (QObject * item, const int idx);
void onItemRemoved (QObject * item, const int idx);
void onItemsCleared (void);
void onUserInteraction (void);
protected:
void classBegin (void) Q_DECL_FINAL;
void componentComplete (void) Q_DECL_FINAL;
void updatePolish (void) Q_DECL_FINAL;
//void mousePressEvent (QMouseEvent * event) Q_DECL_FINAL;
//void mouseMoveEvent (QMouseEvent * event) Q_DECL_FINAL;
//void mouseReleaseEvent (QMouseEvent * event) Q_DECL_FINAL;
//void wheelEvent (QWheelEvent * event) Q_DECL_FINAL;
bool childMouseEventFilter (QQuickItem * childItem, QEvent * event) Q_DECL_FINAL;
void classBegin (void) Q_DECL_FINAL;
void componentComplete (void) Q_DECL_FINAL;
void updatePolish (void) Q_DECL_FINAL;
private:
QQmlFastObjectListModelBase * m_model { Q_NULLPTR };
QQmlComponent * m_delegate { Q_NULLPTR };
QQuickItem * m_reference { new QQuickItem { this } };
QObject * m_current { Q_NULLPTR };
QPropertyAnimation * m_animScroll { Q_NULLPTR };
int m_behavior { FREE_MOVE };
bool m_pressed { false };
QList<QQuickItem *> m_instances { };
QPoint m_prevPos { };
QMap<qint64, qreal> m_moveCurve { };
const QString MODEL_ITEM { "modelItem" };
const char * FLICKABLE { "QQuickFlickable" };
const char * FLICKING_V { "flickingVertically" };
const char * DRAGGING_V { "draggingVertically" };
const char * CONTENT_Y { "contentY" };
const char * CONTENT_W { "contentWidth" };
const char * CONTENT_H { "contentHeight" };
const char * ANCHORS { "anchors" };
const char * TOP { "top" };
const char * LEFT { "left" };
const char * RIGHT { "right" };
QQuickItem * instantiate (QObject * item);
void eliminate (QQuickItem * instance);
int m_behavior { FREE_MOVE };
QObject * m_current { Q_NULLPTR };
QQuickItem * m_flickable { Q_NULLPTR };
QQmlComponent * m_delegate { Q_NULLPTR };
QQmlFastObjectListModelBase * m_model { Q_NULLPTR };
QList<Holder *> m_holdersList { };
};
#endif // QQUICKFASTOBJECTLISTVIEW_H
......@@ -13,15 +13,23 @@ class Test : public QObject {
public:
Q_INVOKABLE void insert (const int idx) {
QObject * item { new QObject { this } };
qWarning () << "INSERT" << item << idx;
//qWarning () << "INSERT" << item << idx;
m_testModel.insert (item, idx);
}
Q_INVOKABLE void remove (const int idx) {
QObject * item { m_testModel.getAt (idx) };
qWarning () << "REMOVE" << item << idx;
//qWarning () << "REMOVE" << item << idx;
m_testModel.remove (item);
}
Q_INVOKABLE QObject * first (void) const {
return m_testModel.getFirst ();
}
Q_INVOKABLE QObject * last (void) const {
return m_testModel.getLast ();
}
QQmlFastObjectListModel<QObject> & testModel (void) { return m_testModel; }
QQmlFastObjectListModelBase * getTestModel (void) { return &testModel (); }
......
......@@ -13,9 +13,13 @@ int main (int argc, char * argv []) {
qmlRegisterType<QQmlFastObjectListModelBase> ("QtQmlTricks", 3, 0, "FastObjectListModel");
qmlRegisterType<QQuickFastObjectListView> ("QtQmlTricks", 3, 0, "FastObjectListView");
Test test { };
for (int idx { 0 }; idx < 50; ++idx) {
test.insert (idx);
QList<QObject *> tmp { };
const int nb { 1000 };
tmp.reserve (nb);
for (int idx { 0 }; idx < nb; ++idx) {
tmp.append (new QObject { &test });
}
test.testModel ().appendList (tmp);
QGuiApplication app { argc, argv };
QQmlApplicationEngine engine { };
engine.rootContext ()->setContextProperty ("test", QVariant::fromValue (&test));
......
......@@ -3,7 +3,7 @@ import QtQuick.Window 2.1;
import QtQmlTricks 3.0;
Window {
title: qsTr ("Fast Object List %1").arg (test.testModel.count);
title: qsTr ("Fast Object List %1 - %2 - %3").arg (test.testModel.count).arg (flicker.atYBeginning ? "AT START" : "").arg (flicker.atYEnd ? "AT END" : "");
color: "gray";
width: 800;
height: 640;
......@@ -11,69 +11,60 @@ Window {
visibility = Window.Windowed;
}
//Timer {
// repeat: true;
// running: true;
// interval: 10000;
// onTriggered: {
// if (test.testModel.count > 7) {
// test.remove (0);
// }
// else {
// test.insert (7);
// }
// }
//}
FastObjectListView {
model: test.testModel;
delegate: Component {
MouseArea {
implicitHeight: 50;
Flickable {
id: flicker;
anchors.fill: parent;
Rectangle {
color: "steelblue";
opacity: 0.15;
visible: parent.pressed;
anchors.fill: parent;
}
Row {
spacing: 12;
anchors.verticalCenter: parent.verticalCenter;
anchors.left: parent.left;
anchors.margins: 12;
FastObjectListView {
id: view;
model: test.testModel;
delegate: Component {
MouseArea {
implicitHeight: (row.height + row.anchors.margins * 2);
onPressAndHold: {
console.log ("LONG PRESS", modelItem);
}
MouseArea {
id: button;
implicitWidth: 42;
implicitHeight: 42;
anchors.verticalCenter: parent.verticalCenter;
onPressed: { console.log ("SUB-PRESSED"); }
onReleased: { console.log ("SUB-RELEASED"); }
Rectangle {
color: "steelblue";
opacity: (modelItem === view.current ? 0.35 : 0.15);
visible: (parent.pressed || modelItem === view.current);
anchors.fill: parent;
}
Row {
id: row;
spacing: 12;
anchors.top: parent.top;
anchors.left: parent.left;
anchors.margins: 12;
MouseArea {
id: button;
implicitWidth: 42;
implicitHeight: 42;
anchors.verticalCenter: parent.verticalCenter;
onClicked: {
view.current = modelItem;
view.behavior = FastObjectListView.KEEP_CENTERED;
}
Rectangle {
color: Qt.hsla (Math.random (), 0.85, 0.65);
border {
width: (button.pressed ? 2 : 0);
color: "black";
Rectangle {
color: Qt.hsla (Math.random (), 0.85, 0.65);
border {
width: (button.pressed ? 2 : 0);
color: "black";
}
anchors.fill: parent;
}
anchors.fill: parent;
}
}
Text {
text: "Hello World ! %1".arg (modelItem);
font.pixelSize: 42;
anchors.verticalCenter: parent.verticalCenter;
Component.onCompleted: { console.log ("CREATED", this); }
Component.onDestruction: { console.log ("DESTROYED", this); }
Text {
text: "Hello World ! %1".arg (modelItem);
font.pixelSize: 42;
anchors.verticalCenter: parent.verticalCenter;
}
}
}
}
}
anchors.fill: parent;
MouseArea {
z: -1;
anchors.fill: parent;
}
}
}
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