Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit 93a620b

Browse files
committed
wip update imports
1 parent 0404a63 commit 93a620b

File tree

5 files changed

+185
-158
lines changed

5 files changed

+185
-158
lines changed

doc/qtmvvm.dox

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ The following list shows which classes belong to which Qt module, in alphabetica
103103
- @ref QtMvvm::ExchangeDevicesModel "ExchangeDevicesModel"
104104
- @ref QtMvvm::DataSyncViewModel "DataSyncViewModel" (uncreatable)
105105
- @ref QtMvvm::NetworkExchangeViewModel "NetworkExchangeViewModel" (uncreatable)
106+
- @ref QtMvvm::DataSyncSettingsViewModel "DataSyncSettingsViewModel" (uncreatable)
107+
- @ref QtMvvm::DataSyncSettingsEntry "DataSyncSettingsEntry" (uncreatable)
106108
- PChangeRemoteViewModel (uncreatable, internal)
107109
- PExportSetupViewModel (uncreatable, internal)
108110
- PIdentityEditViewModel (uncreatable, internal)

examples/mvvmquick/SampleQuick/ContainerView.qml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,13 @@ Page {
3535
ViewPlaceholder {
3636
id: viewPlaceholder
3737

38-
viewModelType: containerView.viewModel.vmType
39-
parentViewModel: containerView.viewModel
40-
autoPresent: false
41-
4238
Layout.fillWidth: true
4339
Layout.fillHeight: true
4440

4541
BusyIndicator {
4642
anchors.centerIn: parent
47-
anchors.verticalCenterOffset: -(hookButton.height/2)
4843
running: !viewPlaceholder.loadedView
4944
}
50-
51-
Button {
52-
id: hookButton
53-
Layout.fillWidth: true
54-
text: qsTr("Load from QML")
55-
onClicked: viewPlaceholder.presentView();
56-
enabled: !viewPlaceholder.loadedView
57-
58-
anchors.left: parent.left
59-
anchors.right: parent.right
60-
anchors.bottom: parent.bottom
61-
}
6245
}
6346

6447
Button {

src/imports/mvvmquick/plugins.qmltypes

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,10 @@ Module {
130130
prototype: "QQuickItem"
131131
exports: ["de.skycoder42.QtMvvm.Quick/ViewPlaceholder 1.1"]
132132
exportMetaObjectRevisions: [0]
133-
Property { name: "viewModelType"; type: "string" }
134-
Property { name: "showParams"; type: "QVariantHash" }
135-
Property { name: "parentViewModel"; type: "QtMvvm::ViewModel"; isPointer: true }
136-
Property { name: "autoPresent"; type: "bool" }
137133
Property { name: "autoResizeView"; type: "bool" }
138134
Property { name: "replaceViews"; type: "bool" }
139135
Property { name: "closeViewOnAction"; type: "bool" }
140136
Property { name: "loadedView"; type: "QQuickItem"; isReadonly: true; isPointer: true }
141-
Signal {
142-
name: "viewModelTypeChanged"
143-
Parameter { name: "viewModelType"; type: "string" }
144-
}
145-
Signal {
146-
name: "showParamsChanged"
147-
Parameter { name: "showParams"; type: "QVariantHash" }
148-
}
149-
Signal {
150-
name: "parentViewModelChanged"
151-
Parameter { name: "parentViewModel"; type: "QtMvvm::ViewModel"; isPointer: true }
152-
}
153-
Signal {
154-
name: "autoPresentChanged"
155-
Parameter { name: "autoPresent"; type: "bool" }
156-
}
157137
Signal {
158138
name: "autoResizeViewChanged"
159139
Parameter { name: "autoResizeView"; type: "bool" }
@@ -170,12 +150,11 @@ Module {
170150
name: "loadedViewChanged"
171151
Parameter { name: "loadedView"; type: "QQuickItem"; isPointer: true }
172152
}
173-
Method { name: "presentView" }
174153
Method { name: "discardView" }
175154
Method {
176155
name: "presentItem"
177156
type: "bool"
178-
Parameter { name: "item"; type: "QVariant" }
157+
Parameter { name: "item"; type: "QQuickItem"; isPointer: true }
179158
}
180159
Method { name: "closeAction"; type: "bool" }
181160
}

src/imports/mvvmquick/qqmlviewplaceholder.cpp

Lines changed: 35 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ using namespace QtMvvm;
88
QQmlViewPlaceholder::QQmlViewPlaceholder(QQuickItem *parent) :
99
QQuickItem{parent}
1010
{
11-
// auto presenting
12-
connect(this, &QQmlViewPlaceholder::viewModelTypeChanged,
13-
this, &QQmlViewPlaceholder::presentIfReady);
14-
connect(this, &QQmlViewPlaceholder::autoPresentChanged,
15-
this, &QQmlViewPlaceholder::presentIfReady);
11+
setImplicitSize(0, 0); // init to 0
1612

1713
// size changes
1814
connect(this, &QQmlViewPlaceholder::widthChanged,
@@ -23,18 +19,12 @@ QQmlViewPlaceholder::QQmlViewPlaceholder(QQuickItem *parent) :
2319
this, &QQmlViewPlaceholder::resizeView);
2420
}
2521

26-
bool QQmlViewPlaceholder::presentItem(const QVariant &item)
22+
bool QQmlViewPlaceholder::presentItem(QQuickItem *item)
2723
{
28-
// check if the parameter is valid
29-
auto quickItem = item.value<QQuickItem*>();
30-
if(!quickItem) {
31-
qmlWarning(this) << "presentItem called with invalid item of type: " << item.typeName();
32-
return false;
33-
}
34-
3524
// handle already existing view case
3625
if(_loadedView) {
3726
if(_replaceViews) { // quick discard without reenableing all children
27+
disconnectSizeChanges(false);
3828
_loadedView->setVisible(false);
3929
_loadedView->deleteLater();
4030
_loadedView = nullptr;
@@ -45,11 +35,12 @@ bool QQmlViewPlaceholder::presentItem(const QVariant &item)
4535
}
4636

4737
// add
48-
_loadedView = quickItem;
49-
quickItem->setParent(this);
50-
quickItem->setParentItem(this);
38+
_loadedView = item;
39+
_loadedView->setParent(this);
40+
_loadedView->setParentItem(this);
41+
connectSizeChanges();
5142
resizeView();
52-
quickItem->setVisible(true);
43+
_loadedView->setVisible(true);
5344

5445
// hide all children
5546
for(auto child : childItems()) {
@@ -87,52 +78,11 @@ bool QQmlViewPlaceholder::closeAction()
8778
return false;
8879
}
8980

90-
void QQmlViewPlaceholder::setParentViewModel(ViewModel *parentViewModel)
91-
{
92-
// first: clear the auto-connected viewmodel, if required
93-
if(_clearParentVmCon && _parentVmCon)
94-
disconnect(_parentVmCon);
95-
96-
// then: set property as usual
97-
if(_parentViewModel == parentViewModel)
98-
return;
99-
100-
_parentViewModel = parentViewModel;
101-
emit parentViewModelChanged(_parentViewModel);
102-
103-
// check the vm parent for a presenter method
104-
auto view = qobject_cast<QQuickItem*>(_parentViewModel->parent());
105-
if(view) {
106-
if(view->metaObject()->indexOfMethod("presentItem(QVariant)") == -1)
107-
qmlWarning(this) << R"(Parent item of "parentViewModel" does not have a "presentItem" method. Check the ViewPlaceholder documentation!)";
108-
} else
109-
qmlWarning(this) << R"(Parent item of "parentViewModel" is not an Item)";
110-
111-
presentIfReady();
112-
}
113-
11481
QQuickItem *QQmlViewPlaceholder::loadedView() const
11582
{
11683
return _loadedView;
11784
}
11885

119-
void QQmlViewPlaceholder::componentComplete()
120-
{
121-
// auto-set the vm if not already set
122-
if(!_parentViewModel)
123-
getParentViewModel();
124-
125-
// last step: call base implementation, then present
126-
QQuickItem::componentComplete();
127-
_isReady = true;
128-
presentIfReady();
129-
}
130-
131-
void QQmlViewPlaceholder::presentView()
132-
{
133-
CoreApp::show(qUtf8Printable(_viewModelType), _showParams, _parentViewModel);
134-
}
135-
13686
void QQmlViewPlaceholder::discardView()
13787
{
13888
// hide view
@@ -145,57 +95,46 @@ void QQmlViewPlaceholder::discardView()
14595
}
14696

14797
// now delete it
98+
disconnectSizeChanges(true);
14899
_loadedView->deleteLater();
149100
_loadedView = nullptr;
150101
emit loadedViewChanged(nullptr);
151102
}
152103

153-
void QQmlViewPlaceholder::parentItemVmChanged(ViewModel *viewModel)
104+
void QQmlViewPlaceholder::resizeView()
154105
{
155-
// set vm without clearing the connection
156-
_clearParentVmCon = false;
157-
setParentViewModel(viewModel);
158-
_clearParentVmCon = true;
106+
if(_loadedView && _autoResizeView) {
107+
_loadedView->setWidth(width());
108+
_loadedView->setHeight(height());
109+
}
159110
}
160111

161-
void QQmlViewPlaceholder::presentIfReady()
112+
void QQmlViewPlaceholder::updateImpHeight()
162113
{
163-
if(_isReady &&
164-
_autoPresent &&
165-
!_loadedView &&
166-
_parentViewModel &&
167-
!_viewModelType.isEmpty())
168-
presentView();
114+
setImplicitHeight(_loadedView->implicitHeight());
169115
}
170116

171-
void QQmlViewPlaceholder::resizeView()
117+
void QQmlViewPlaceholder::updateImpWidth()
172118
{
173-
if(_loadedView && _autoResizeView) {
174-
_loadedView->setWidth(width());
175-
_loadedView->setHeight(height());
176-
}
119+
setImplicitWidth(_loadedView->implicitWidth());
120+
}
121+
122+
void QQmlViewPlaceholder::connectSizeChanges()
123+
{
124+
connect(_loadedView, &QQuickItem::implicitWidthChanged,
125+
this, &QQmlViewPlaceholder::updateImpWidth);
126+
connect(_loadedView, &QQuickItem::implicitHeightChanged,
127+
this, &QQmlViewPlaceholder::updateImpHeight);
128+
setImplicitSize(_loadedView->implicitWidth(),
129+
_loadedView->implicitHeight());
177130
}
178131

179-
void QQmlViewPlaceholder::getParentViewModel()
132+
void QQmlViewPlaceholder::disconnectSizeChanges(bool resetSize)
180133
{
181-
auto pItem = parentItem();
182-
if(!pItem)
183-
return;
184-
185-
QQmlProperty vmProp{pItem, QStringLiteral("viewModel")};
186-
if(!vmProp.isValid())
187-
return;
188-
189-
// set the vm from the property
190-
auto vm = vmProp.read().value<ViewModel*>();
191-
if(vm)
192-
setParentViewModel(vm); // no warning here - might be ok for lazy presented vms
193-
194-
// connect to further changes, via helper slot
195-
auto cSlotIndex = metaObject()->indexOfSlot("parentItemVmChanged(QtMvvm::ViewModel*)");
196-
Q_ASSERT(cSlotIndex != -1);
197-
if(_parentVmCon)
198-
disconnect(_parentVmCon);
199-
_parentVmCon = connect(pItem, vmProp.property().notifySignal(),
200-
this, metaObject()->method(cSlotIndex));
134+
disconnect(_loadedView, &QQuickItem::implicitWidthChanged,
135+
this, &QQmlViewPlaceholder::updateImpWidth);
136+
disconnect(_loadedView, &QQuickItem::implicitHeightChanged,
137+
this, &QQmlViewPlaceholder::updateImpHeight);
138+
if(resetSize)
139+
setImplicitSize(0, 0);
201140
}

0 commit comments

Comments
 (0)