summaryrefslogtreecommitdiffstats
path: root/examples/tools
diff options
context:
space:
mode:
authorRhys Weatherley <rhys.weatherley@nokia.com>2009-03-24 11:07:00 +1000
committerRhys Weatherley <rhys.weatherley@nokia.com>2009-03-24 11:07:00 +1000
commit0d00798f6bdd098dbb59c6f1da5be5efd6c283fa (patch)
tree53537f94a9df8092a6e9ca3b7e2212bb8318e9c3 /examples/tools
parent738e8391bc6037f7949d59781a6b2c64f5ca6a8a (diff)
Squashed commit of the following:
commit 39de3862f5678b3226b4932eeb342c4a023d2f2b Author: Ian Walters <ian.walters@nokia.com> Date: Thu Feb 19 14:16:05 2009 +1000 Fixes: Test runs (and passes), doc links. Task: QT-308 Details: Minor changes related to the code having moved. commit 5a8910dd1018fb228d0e2e2819ea429577bfa834 Author: Ian Walters <ian.walters@nokia.com> Date: Thu Feb 19 09:47:20 2009 +1000 Fixes: Checkin of QOffsetVector stuff for branch Task: QT-308 Details: Files originally from research/qcircularbuffer This checkin likely won't compile. Just a copy for now.
Diffstat (limited to 'examples/tools')
-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
-rw-r--r--examples/tools/tools.pro1
5 files changed, 107 insertions, 0 deletions
diff --git a/examples/tools/offsetvector/main.cpp b/examples/tools/offsetvector/main.cpp
new file mode 100644
index 0000000000..bdeb3f3f8f
--- /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 0000000000..f329bb961b
--- /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 0000000000..5c0953b4f9
--- /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 0000000000..e102255cbb
--- /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
diff --git a/examples/tools/tools.pro b/examples/tools/tools.pro
index 79f0faab0c..424f2863c2 100644
--- a/examples/tools/tools.pro
+++ b/examples/tools/tools.pro
@@ -5,6 +5,7 @@ SUBDIRS = codecs \
customcompleter \
echoplugin \
i18n \
+ offsetvector \
plugandpaintplugins \
plugandpaint \
regexp \