Make the fibonacci number list look similar across styles
parent
0a143b6bad
commit
d544db5a1b
|
@ -81,9 +81,13 @@
|
|||
"FibonacciList": {
|
||||
"type": "List",
|
||||
"itemProperties": {
|
||||
"result": {
|
||||
"row": {
|
||||
"type": "quint64",
|
||||
"roles": [ [ "display" ] ]
|
||||
},
|
||||
"fibonacciNumber": {
|
||||
"type": "quint64",
|
||||
"roles": [ [], [ "display" ] ]
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,5 +7,8 @@
|
|||
<file>qml/Fibonacci.qml</file>
|
||||
<file>qml/FibonacciList.qml</file>
|
||||
<file>qml/StyleSwitcher.qml</file>
|
||||
<file>qml/StyleSwitcher2.qml</file>
|
||||
<file>qml/Fibonacci2.qml</file>
|
||||
<file>qml/FibonacciList2.qml</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {}
|
||||
|
|
|
@ -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<int, QByteArray> FibonacciList::roleNames() const {
|
||||
QHash<int, QByteArray> 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),
|
||||
|
|
|
@ -102,7 +102,8 @@ public:
|
|||
QHash<int, QByteArray> 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()
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue