From d544db5a1b8c8de26602a965ccffb4ea6bc6fc8d Mon Sep 17 00:00:00 2001 From: Jos van den Oever Date: Sat, 2 Sep 2017 16:58:04 +0200 Subject: [PATCH] Make the fibonacci number list look similar across styles --- demo/bindings.json | 6 +- demo/qml/FibonacciList.qml | 11 +-- demo/qml/StyleSwitcher.qml | 8 +- demo/qml/demo-qtquick2.qml | 92 ++++++----------------- demo/resource_file.qrc | 3 + demo/rust/src/implementation/fibonacci.rs | 5 +- demo/rust/src/interface.rs | 12 ++- demo/src/Bindings.cpp | 38 +++++++--- demo/src/Bindings.h | 3 +- demo/src/main.cpp | 13 +++- 10 files changed, 91 insertions(+), 100 deletions(-) diff --git a/demo/bindings.json b/demo/bindings.json index 71ba97f..91f0fcc 100644 --- a/demo/bindings.json +++ b/demo/bindings.json @@ -81,9 +81,13 @@ "FibonacciList": { "type": "List", "itemProperties": { - "result": { + "row": { "type": "quint64", "roles": [ [ "display" ] ] + }, + "fibonacciNumber": { + "type": "quint64", + "roles": [ [], [ "display" ] ] } } }, diff --git a/demo/qml/FibonacciList.qml b/demo/qml/FibonacciList.qml index 1b6e77a..8599a4f 100644 --- a/demo/qml/FibonacciList.qml +++ b/demo/qml/FibonacciList.qml @@ -4,19 +4,16 @@ import QtQuick.Controls 1.5 import QtQuick.Layouts 1.3 TableView { - id: listView model: demo.fibonacciList TableViewColumn { - delegate: Text { - text: styleData.row + 1 - } - title: "Row" + role: "row" + title: qsTr("Row") width: 100 Component.onCompleted: resizeToContents() } TableViewColumn { - role: "display" - title: "Fibonacci Number" + role: "fibonacciNumber" + title: qsTr("Fibonacci number") width: 100 Component.onCompleted: resizeToContents() } diff --git a/demo/qml/StyleSwitcher.qml b/demo/qml/StyleSwitcher.qml index 7a9b667..f7c4daa 100644 --- a/demo/qml/StyleSwitcher.qml +++ b/demo/qml/StyleSwitcher.qml @@ -3,8 +3,6 @@ import QtQuick.Controls 1.5 import QtQuick.Layouts 1.3 RowLayout { - id: content - Layout.fillWidth: true Image { sourceSize.height: 2* box.height fillMode: Image.PreserveAspectFit @@ -15,9 +13,9 @@ RowLayout { currentIndex: qtquickIndex model: styles textRole: "display" - onCurrentIndexChanged: { - if (currentIndex !== qtquickIndex) { - widgets.currentIndex = currentIndex; + onActivated: { + if (index !== qtquickIndex) { + widgets.currentIndex = index; application.close(); } } diff --git a/demo/qml/demo-qtquick2.qml b/demo/qml/demo-qtquick2.qml index a4f1c12..b20d0b4 100644 --- a/demo/qml/demo-qtquick2.qml +++ b/demo/qml/demo-qtquick2.qml @@ -7,76 +7,30 @@ ApplicationWindow { property string initialTab: "style" property int qtquickIndex: 0 visible: true - footer: StyleSwitcher { - anchors.fill: parent - } - contentItem { - TabBar { - id: bar - width: parent.width - TabButton { - text: "object" - } - TabButton { - text: "list" - } - TabButton { - text: "tree" - } + height: 500 + header: TabBar { + id: bar + width: parent.width + TabButton { + text: "object" } - StackLayout { - width: parent.width - anchors.top: bar.bottom - anchors.bottom: parent.bottom - currentIndex: bar.currentIndex - ColumnLayout { - ComboBox { - currentIndex: qtquickIndex - model: styles - textRole: "display" - onCurrentIndexChanged: { - if (currentIndex != qtquickIndex) { - widgets.currentIndex = currentIndex - application.close() - } - } - } - Image { - source: "../logo.svg" - } - } - RowLayout { - TextField { - id: fibonacciInput - placeholderText: "Your number" - validator: IntValidator { - bottom: 0 - top: 100 - } - Component.onCompleted: { - text = demo.fibonacci.input - } - onTextChanged: { - demo.fibonacci.input = parseInt(text, 10) - } - } - Text { - text: "The Fibonacci number: " + demo.fibonacci.result - } - } - ListView { - id: listView - model: demo.fibonacciList - delegate: Row { - Text { - text: result - } - } - } - Text { - id: treeView - text: "No TreeView in QtQuick Controls 2" - } + TabButton { + text: "list" + } + TabButton { + text: "tree" + } + } + footer: StyleSwitcher2 { + } + StackLayout { + anchors.fill: parent + currentIndex: bar.currentIndex + Fibonacci2 {} + FibonacciList2 {} + Text { + id: treeView + text: "No TreeView in QtQuick Controls 2" } } } diff --git a/demo/resource_file.qrc b/demo/resource_file.qrc index dc79c44..971ab9a 100644 --- a/demo/resource_file.qrc +++ b/demo/resource_file.qrc @@ -7,5 +7,8 @@ qml/Fibonacci.qml qml/FibonacciList.qml qml/StyleSwitcher.qml + qml/StyleSwitcher2.qml + qml/Fibonacci2.qml + qml/FibonacciList2.qml diff --git a/demo/rust/src/implementation/fibonacci.rs b/demo/rust/src/implementation/fibonacci.rs index f3c1d46..b134408 100644 --- a/demo/rust/src/implementation/fibonacci.rs +++ b/demo/rust/src/implementation/fibonacci.rs @@ -75,7 +75,10 @@ impl FibonacciListTrait for FibonacciList { fn row_count(&self) -> usize { 93 } - fn result(&self, row: usize) -> u64 { + fn row(&self, row: usize) -> u64 { + row as u64 + 1 + } + fn fibonacci_number(&self, row: usize) -> u64 { fibonacci(row as u32 + 1) as u64 } } diff --git a/demo/rust/src/interface.rs b/demo/rust/src/interface.rs index 7332f51..a0b03bf 100644 --- a/demo/rust/src/interface.rs +++ b/demo/rust/src/interface.rs @@ -437,7 +437,8 @@ pub trait FibonacciListTrait { } fn fetch_more(&mut self) {} fn sort(&mut self, u8, SortOrder) {} - fn result(&self, item: usize) -> u64; + fn fibonacci_number(&self, item: usize) -> u64; + fn row(&self, item: usize) -> u64; } #[no_mangle] @@ -497,8 +498,13 @@ pub unsafe extern "C" fn fibonacci_list_sort( } #[no_mangle] -pub unsafe extern "C" fn fibonacci_list_data_result(ptr: *const FibonacciList, row: c_int) -> u64 { - (&*ptr).result(row as usize).into() +pub unsafe extern "C" fn fibonacci_list_data_fibonacci_number(ptr: *const FibonacciList, row: c_int) -> u64 { + (&*ptr).fibonacci_number(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn fibonacci_list_data_row(ptr: *const FibonacciList, row: c_int) -> u64 { + (&*ptr).row(row as usize).into() } pub struct FileSystemTreeQObject {} diff --git a/demo/src/Bindings.cpp b/demo/src/Bindings.cpp index 45e8f65..f0fcc98 100644 --- a/demo/src/Bindings.cpp +++ b/demo/src/Bindings.cpp @@ -147,7 +147,8 @@ extern "C" { }; extern "C" { - quint64 fibonacci_list_data_result(const FibonacciList::Private*, int); + quint64 fibonacci_list_data_fibonacci_number(const FibonacciList::Private*, int); + quint64 fibonacci_list_data_row(const FibonacciList::Private*, int); void fibonacci_list_sort(FibonacciList::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); int fibonacci_list_row_count(const FibonacciList::Private*); @@ -156,7 +157,7 @@ extern "C" { } int FibonacciList::columnCount(const QModelIndex &parent) const { - return (parent.isValid()) ? 0 : 1; + return (parent.isValid()) ? 0 : 2; } bool FibonacciList::hasChildren(const QModelIndex &parent) const @@ -171,7 +172,7 @@ int FibonacciList::rowCount(const QModelIndex &parent) const QModelIndex FibonacciList::index(int row, int column, const QModelIndex &parent) const { - if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 1) { + if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 2) { return createIndex(row, column, (quintptr)row); } return QModelIndex(); @@ -204,10 +205,17 @@ Qt::ItemFlags FibonacciList::flags(const QModelIndex &i) const return flags; } -QVariant FibonacciList::result(int row) const +QVariant FibonacciList::fibonacciNumber(int row) const { QVariant v; - v = fibonacci_list_data_result(m_d, row); + v = fibonacci_list_data_fibonacci_number(m_d, row); + return v; +} + +QVariant FibonacciList::row(int row) const +{ + QVariant v; + v = fibonacci_list_data_row(m_d, row); return v; } @@ -216,10 +224,18 @@ QVariant FibonacciList::data(const QModelIndex &index, int role) const Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { case 0: + switch (role) { + case Qt::UserRole + 0: + return fibonacciNumber(index.row()); + case Qt::DisplayRole: + case Qt::UserRole + 1: + return row(index.row()); + } + case 1: switch (role) { case Qt::DisplayRole: case Qt::UserRole + 0: - return result(index.row()); + return fibonacciNumber(index.row()); } } return QVariant(); @@ -227,7 +243,8 @@ QVariant FibonacciList::data(const QModelIndex &index, int role) const QHash FibonacciList::roleNames() const { QHash names = QAbstractItemModel::roleNames(); - names.insert(Qt::UserRole + 0, "result"); + names.insert(Qt::UserRole + 0, "fibonacciNumber"); + names.insert(Qt::UserRole + 1, "row"); return names; } QVariant FibonacciList::headerData(int section, Qt::Orientation orientation, int role) const @@ -906,7 +923,7 @@ Demo::Demo(QObject *parent): }, [](FibonacciList* o, quintptr first, quintptr last) { o->dataChanged(o->createIndex(first, 0, first), - o->createIndex(last, 0, last)); + o->createIndex(last, 1, last)); }, [](FibonacciList* o) { o->beginResetModel(); @@ -1155,7 +1172,7 @@ FibonacciList::FibonacciList(QObject *parent): }, [](FibonacciList* o, quintptr first, quintptr last) { o->dataChanged(o->createIndex(first, 0, first), - o->createIndex(last, 0, last)); + o->createIndex(last, 1, last)); }, [](FibonacciList* o) { o->beginResetModel(); @@ -1190,7 +1207,8 @@ FibonacciList::~FibonacciList() { } } void FibonacciList::initHeaderData() { - m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("result")); + m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("row")); + m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("fibonacciNumber")); } FileSystemTree::FileSystemTree(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), diff --git a/demo/src/Bindings.h b/demo/src/Bindings.h index 8a10c5b..7ea5372 100644 --- a/demo/src/Bindings.h +++ b/demo/src/Bindings.h @@ -102,7 +102,8 @@ public: QHash roleNames() const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override; - Q_INVOKABLE QVariant result(int row) const; + Q_INVOKABLE QVariant fibonacciNumber(int row) const; + Q_INVOKABLE QVariant row(int row) const; signals: // new data is ready to be made available to the model with fetchMore() diff --git a/demo/src/main.cpp b/demo/src/main.cpp index 48e19e8..d324f54 100644 --- a/demo/src/main.cpp +++ b/demo/src/main.cpp @@ -84,6 +84,8 @@ void createQtQuick(const QString& name, const QString& qml, Model* model, QWindow* window = getWindow(widgets); if (window) { geometry = window->geometry(); + } else { + geometry = QRect(0, 0, 500, 500); } engine->load(QUrl(qml)); QObject* root = engine->rootObjects().first(); @@ -193,8 +195,13 @@ QWidget* createObjectTab(Model* model) { } QWidget* createListTab(Model* model) { - QListView* view = new QListView(); + QTableView* view = new QTableView(); + model->demo.fibonacciList()->setHeaderData(0, Qt::Horizontal, + view->tr("Row"), Qt::DisplayRole); + model->demo.fibonacciList()->setHeaderData(1, Qt::Horizontal, + view->tr("Fibonacci number"), Qt::DisplayRole); view->setModel(model->demo.fibonacciList()); + view->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); return view; } @@ -268,7 +275,7 @@ QWidget* createChartTab(Model* model) { } #endif -QTabWidget* createTabs(Model* model, QComboBox* box) { +QTabWidget* createTabs(Model* model) { QTabWidget* tabs = new QTabWidget(); tabs->addTab(createObjectTab(model), "object"); tabs->addTab(createListTab(model), "list"); @@ -292,7 +299,7 @@ void createMainWindow(Model* model, const QString& initialStyle, QComboBox* box = createStyleComboBox(model); QStatusBar* statusBar = createStatusBar(model, main, box, initialTab); main->setStatusBar(statusBar); - QTabWidget* tabs = createTabs(model, box); + QTabWidget* tabs = createTabs(model); main->setCentralWidget(tabs); main->show(); box->setCurrentText(initialStyle);