summaryrefslogtreecommitdiffstats
path: root/examples/tools/offsetvector
diff options
context:
space:
mode:
Diffstat (limited to 'examples/tools/offsetvector')
-rw-r--r--examples/tools/offsetvector/main.cpp15
-rw-r--r--examples/tools/offsetvector/offsetvector.pro9
-rw-r--r--examples/tools/offsetvector/randomlistmodel.cpp56
-rw-r--r--examples/tools/offsetvector/randomlistmodel.h26
4 files changed, 106 insertions, 0 deletions
diff --git a/examples/tools/offsetvector/main.cpp b/examples/tools/offsetvector/main.cpp
new file mode 100644
index 0000000..bdeb3f3
--- /dev/null
+++ b/examples/tools/offsetvector/main.cpp
@@ -0,0 +1,15 @@
+#include "randomlistmodel.h"
+#include <QListView>
+#include <QApplication>
+
+int main(int c, char **v)
+{
+ QApplication a(c, v);
+
+ QListView view;
+ view.setUniformItemSizes(true);
+ view.setModel(new RandomListModel(&view));
+ view.show();
+
+ return a.exec();
+}
diff --git a/examples/tools/offsetvector/offsetvector.pro b/examples/tools/offsetvector/offsetvector.pro
new file mode 100644
index 0000000..f329bb9
--- /dev/null
+++ b/examples/tools/offsetvector/offsetvector.pro
@@ -0,0 +1,9 @@
+HEADERS = randomlistmodel.h
+SOURCES = randomlistmodel.cpp \
+ main.cpp
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/tools/offsetvector
+sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS offsetvector.pro
+sources.path = $$[QT_INSTALL_EXAMPLES]/tools/offsetvector
+INSTALLS += target sources
diff --git a/examples/tools/offsetvector/randomlistmodel.cpp b/examples/tools/offsetvector/randomlistmodel.cpp
new file mode 100644
index 0000000..5c0953b
--- /dev/null
+++ b/examples/tools/offsetvector/randomlistmodel.cpp
@@ -0,0 +1,56 @@
+#include "randomlistmodel.h"
+
+static const int bufferSize(500);
+static const int lookAhead(100);
+static const int halfLookAhead(lookAhead/2);
+
+RandomListModel::RandomListModel(QObject *parent)
+: QAbstractListModel(parent), m_rows(bufferSize), m_count(10000)
+{
+}
+
+RandomListModel::~RandomListModel()
+{
+}
+
+int RandomListModel::rowCount(const QModelIndex &) const
+{
+ return m_count;
+}
+
+//! [0]
+QVariant RandomListModel::data(const QModelIndex &index, int role) const
+{
+ if (role != Qt::DisplayRole)
+ return QVariant();
+
+ int row = index.row();
+
+ if (row > m_rows.lastIndex()) {
+ if (row - m_rows.lastIndex() > lookAhead)
+ cacheRows(row-halfLookAhead, qMin(m_count, row+halfLookAhead));
+ else while (row > m_rows.lastIndex())
+ m_rows.append(fetchRow(m_rows.lastIndex()+1));
+ } else if (row < m_rows.firstIndex()) {
+ if (m_rows.firstIndex() - row > lookAhead)
+ cacheRows(qMax(0, row-halfLookAhead), row+halfLookAhead);
+ else while (row < m_rows.firstIndex())
+ m_rows.prepend(fetchRow(m_rows.firstIndex()-1));
+ }
+
+ return m_rows.at(row);
+}
+
+void RandomListModel::cacheRows(int from, int to) const
+{
+ for (int i = from; i <= to; ++i)
+ m_rows.insert(i, fetchRow(i));
+}
+//![0]
+
+//![1]
+QString RandomListModel::fetchRow(int position) const
+{
+ return QString::number(rand() % ++position);
+}
+//![1]
diff --git a/examples/tools/offsetvector/randomlistmodel.h b/examples/tools/offsetvector/randomlistmodel.h
new file mode 100644
index 0000000..e102255
--- /dev/null
+++ b/examples/tools/offsetvector/randomlistmodel.h
@@ -0,0 +1,26 @@
+#ifndef RANDOMLISTMODEL_H
+#define RANDOMLISTMODEL_H
+
+#include <QOffsetVector>
+#include <QAbstractListModel>
+
+class QTimer;
+class RandomListModel : public QAbstractListModel
+{
+ Q_OBJECT
+public:
+ RandomListModel(QObject *parent = 0);
+ ~RandomListModel();
+
+ int rowCount(const QModelIndex & = QModelIndex()) const;
+ QVariant data(const QModelIndex &, int) const;
+
+private:
+ void cacheRows(int, int) const;
+ QString fetchRow(int) const;
+
+ mutable QOffsetVector<QString> m_rows;
+ const int m_count;
+};
+
+#endif