Browse Source

completed tabbar sample (quick)

pull/2/head
Skycoder42 7 years ago
parent
commit
1ef15ae73d
  1. 5
      examples/mvvmcore/SampleCore/drawerviewmodel.cpp
  2. 35
      examples/mvvmcore/SampleCore/tabviewmodel.cpp
  3. 25
      examples/mvvmcore/SampleCore/tabviewmodel.h
  4. 7
      examples/mvvmquick/SampleQuick/SampleView.qml
  5. 14
      examples/mvvmquick/SampleQuick/TabItemView.qml
  6. 42
      examples/mvvmquick/SampleQuick/TabView.qml
  7. 1
      examples/mvvmquick/SampleQuick/main.cpp
  8. 1
      examples/mvvmquick/SampleQuick/qml.qrc
  9. 5
      src/imports/mvvmquick/InputDialog.qml
  10. 2
      src/imports/mvvmquick/PresentingStackView.qml
  11. 14
      src/mvvmquick/quickpresenter.cpp

5
examples/mvvmcore/SampleCore/drawerviewmodel.cpp

@ -1,12 +1,14 @@
#include "drawerviewmodel.h" #include "drawerviewmodel.h"
#include "sampleviewmodel.h" #include "sampleviewmodel.h"
#include "tabviewmodel.h"
DrawerViewModel::DrawerViewModel(QObject *parent) : DrawerViewModel::DrawerViewModel(QObject *parent) :
ViewModel(parent), ViewModel(parent),
_navModel(new QStandardItemModel(0, 1, this)) _navModel(new QStandardItemModel(0, 1, this))
{ {
_navModel->appendRow(new QStandardItem(tr("Main Sample"))); _navModel->appendRow(new QStandardItem(tr("Main Sample")));
_navModel->appendRow(new QStandardItem(tr("Tab Sample")));
} }
DrawerViewModel::~DrawerViewModel() DrawerViewModel::~DrawerViewModel()
@ -25,6 +27,9 @@ void DrawerViewModel::open(int index)
case 0: case 0:
show<SampleViewModel>(); show<SampleViewModel>();
break; break;
case 1:
show<TabViewModel>();
break;
default: default:
break; break;
} }

35
examples/mvvmcore/SampleCore/tabviewmodel.cpp

@ -1,4 +1,5 @@
#include "tabviewmodel.h" #include "tabviewmodel.h"
#include <QtMvvmCore/Messages>
TabViewModel::TabViewModel(QObject *parent) : TabViewModel::TabViewModel(QObject *parent) :
ViewModel(parent) ViewModel(parent)
@ -8,3 +9,37 @@ TabViewModel::~TabViewModel()
{ {
qInfo(Q_FUNC_INFO); qInfo(Q_FUNC_INFO);
} }
void TabViewModel::addTab()
{
QtMvvm::getInput<QString>(tr("New Tab"), tr("Enter a tab title:"), this, [this](QString res, bool ok) {
if(ok) {
show<TabItemViewModel>({
{QStringLiteral("title"), res}
});
}
});
}
TabItemViewModel::TabItemViewModel(QObject *parent) :
ViewModel(parent),
_title(tr("No Title"))
{}
TabItemViewModel::~TabItemViewModel()
{
qInfo(Q_FUNC_INFO);
}
QString TabItemViewModel::title() const
{
return _title;
}
void TabItemViewModel::onInit(const QVariantHash &params)
{
_title = params.value(QStringLiteral("title"), _title).toString();
emit titleChanged(_title);
}

25
examples/mvvmcore/SampleCore/tabviewmodel.h

@ -10,6 +10,31 @@ class TabViewModel : public QtMvvm::ViewModel
public: public:
Q_INVOKABLE explicit TabViewModel(QObject *parent = nullptr); Q_INVOKABLE explicit TabViewModel(QObject *parent = nullptr);
~TabViewModel(); ~TabViewModel();
public Q_SLOTS:
void addTab();
};
class TabItemViewModel : public QtMvvm::ViewModel
{
Q_OBJECT
Q_PROPERTY(QString title READ title NOTIFY titleChanged)
public:
Q_INVOKABLE explicit TabItemViewModel(QObject *parent = nullptr);
~TabItemViewModel();
QString title() const;
Q_SIGNALS:
void titleChanged(QString title);
protected:
void onInit(const QVariantHash &params) override;
private:
QString _title;
}; };
#endif // TABVIEWMODEL_H #endif // TABVIEWMODEL_H

7
examples/mvvmquick/SampleQuick/SampleView.qml

@ -18,13 +18,6 @@ Page {
onMenuButtonClicked: QuickPresenter.toggleDrawer() onMenuButtonClicked: QuickPresenter.toggleDrawer()
moreMenu: Menu { moreMenu: Menu {
MenuItem {
text: qsTr("Show Tabs")
onTriggered: viewModel.showTabs()
}
MenuSeparator {}
MenuItem { MenuItem {
text: qsTr("Another Input") text: qsTr("Another Input")
onTriggered: viewModel.getInput() onTriggered: viewModel.getInput()

14
examples/mvvmquick/SampleQuick/TabItemView.qml

@ -0,0 +1,14 @@
import QtQuick 2.10
import QtQuick.Controls 2.3
import de.skycoder42.QtMvvm.Core 1.0
import de.skycoder42.QtMvvm.Quick 1.0
import de.skycoder42.QtMvvm.Sample 1.0
Pane {
property TabItemViewModel viewModel: null
Switch {
anchors.centerIn: parent
text: viewModel.title
}
}

42
examples/mvvmquick/SampleQuick/TabView.qml

@ -20,13 +20,11 @@ Page {
tabBar: TabBar { tabBar: TabBar {
id: tabBar id: tabBar
currentIndex: swipe.currentIndex currentIndex: 0
TabButton { TabButton {
text: "Test 1" text: "+"
} onClicked: viewModel.addTab();
TabButton {
text: "Test 2"
} }
} }
} }
@ -37,23 +35,33 @@ Page {
id: swipe id: swipe
anchors.fill: parent anchors.fill: parent
currentIndex: bar.tabBarItem.currentIndex currentIndex: bar.tabBarItem.currentIndex
}
Pane { Component {
id: firstPage id: _newTab
TabButton {
property TabItemViewModel viewModel: null
Switch { text: viewModel.title
anchors.centerIn: parent onClicked: swipe.setCurrentIndex(bar.tabBarItem.currentIndex)
checked: true
text: qsTr("Click me!")
} }
} }
Pane {
id: secondPage
Switch { function presentTab(item) {
anchors.centerIn: parent console.log("here");
text: qsTr("Click me, too!") var tabBar = bar.tabBarItem;
} tabBar.insertItem(tabBar.count - 1, _newTab.createObject(tabBar, {viewModel: item.viewModel}));
item.parent = swipe;
swipe.addItem(item);
tabBar.setCurrentIndex(tabBar.count - 2);
return true;
} }
function afterPop() {
var tabBar = bar.tabBarItem;
while(tabBar.count > 0)
tabBar.takeItem(0).destroy();
while(swipe.count > 0)
swipe.takeItem(0).destroy();
} }
} }

1
examples/mvvmquick/SampleQuick/main.cpp

@ -33,6 +33,7 @@ int main(int argc, char *argv[])
qmlRegisterUncreatableType<ResultViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "ResultViewModel", QStringLiteral("ViewModels cannot be created")); qmlRegisterUncreatableType<ResultViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "ResultViewModel", QStringLiteral("ViewModels cannot be created"));
qmlRegisterUncreatableType<DrawerViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "DrawerViewModel", QStringLiteral("ViewModels cannot be created")); qmlRegisterUncreatableType<DrawerViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "DrawerViewModel", QStringLiteral("ViewModels cannot be created"));
qmlRegisterUncreatableType<TabViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "TabViewModel", QStringLiteral("ViewModels cannot be created")); qmlRegisterUncreatableType<TabViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "TabViewModel", QStringLiteral("ViewModels cannot be created"));
qmlRegisterUncreatableType<TabItemViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "TabItemViewModel", QStringLiteral("ViewModels cannot be created"));
QtMvvm::QuickPresenter::registerAsPresenter(); QtMvvm::QuickPresenter::registerAsPresenter();

1
examples/mvvmquick/SampleQuick/qml.qrc

@ -7,5 +7,6 @@
<file>ResultView.qml</file> <file>ResultView.qml</file>
<file>DrawerView.qml</file> <file>DrawerView.qml</file>
<file>TabView.qml</file> <file>TabView.qml</file>
<file>TabItemView.qml</file>
</qresource> </qresource>
</RCC> </RCC>

5
src/imports/mvvmquick/InputDialog.qml

@ -45,9 +45,12 @@ MsgBoxBase {
Layout.fillHeight: true Layout.fillHeight: true
Layout.fillWidth: true Layout.fillWidth: true
onLoaded: msgResult.result = Qt.binding(function() { onLoaded: {
msgResult.result = Qt.binding(function() {
return item.inputValue; return item.inputValue;
}) })
item.forceActiveFocus();
}
} }
} }

2
src/imports/mvvmquick/PresentingStackView.qml

@ -65,6 +65,8 @@ StackView {
function clearWaitingItems() { function clearWaitingItems() {
_clearItems.forEach(function(item) { _clearItems.forEach(function(item) {
if(typeof item.afterPop == "function")
item.afterPop();
item.destroy(); item.destroy();
}); });
_clearItems = []; _clearItems = [];

14
src/mvvmquick/quickpresenter.cpp

@ -1,6 +1,8 @@
#include "quickpresenter.h" #include "quickpresenter.h"
#include "quickpresenter_p.h" #include "quickpresenter_p.h"
#include <limits>
#include <QtCore/QDir> #include <QtCore/QDir>
#include <QtCore/QDirIterator> #include <QtCore/QDirIterator>
#include <QtCore/QMetaMethod> #include <QtCore/QMetaMethod>
@ -90,27 +92,33 @@ QUrl QuickPresenter::findViewUrl(const QMetaObject *viewModelType)
if(lIndex > 0) if(lIndex > 0)
cName.truncate(lIndex); cName.truncate(lIndex);
QUrl resUrl;
auto shortest = std::numeric_limits<int>::max();
for(auto dir : qAsConst(d->searchDirs)) { for(auto dir : qAsConst(d->searchDirs)) {
QDir searchDir(dir, QDir searchDir(dir,
QStringLiteral("%1*.qml").arg(QString::fromLatin1(cName)), QStringLiteral("%1*.qml").arg(QString::fromLatin1(cName)),
QDir::NoSort, QDir::NoSort,
QDir::Files | QDir::NoDotAndDotDot | QDir::Readable); QDir::Files | QDir::NoDotAndDotDot | QDir::Readable);
QDirIterator iterator(searchDir, QDirIterator::Subdirectories); QDirIterator iterator(searchDir, QDirIterator::Subdirectories);
if(iterator.hasNext()) { while(iterator.hasNext()) {
iterator.next(); iterator.next();
QUrl resUrl; if(iterator.fileName().size() < shortest) {
if(dir.startsWith(QStringLiteral(":"))) { if(dir.startsWith(QStringLiteral(":"))) {
resUrl.clear();
resUrl.setScheme(QStringLiteral("qrc")); resUrl.setScheme(QStringLiteral("qrc"));
resUrl.setPath(iterator.filePath().mid(1)); //skip the beginning colon resUrl.setPath(iterator.filePath().mid(1)); //skip the beginning colon
} else } else
resUrl = QUrl::fromLocalFile(iterator.filePath()); resUrl = QUrl::fromLocalFile(iterator.filePath());
}
}
}
if(resUrl.isValid()) {
logDebug() << "Found URL for viewmodel" logDebug() << "Found URL for viewmodel"
<< viewModelType->className() << viewModelType->className()
<< "as:" << resUrl; << "as:" << resUrl;
return resUrl; return resUrl;
} }
} }
}
currentMeta = currentMeta->superClass(); currentMeta = currentMeta->superClass();
} }

Loading…
Cancel
Save