Browse Source

added loading error handling

pull/2/head
Skycoder42 7 years ago
parent
commit
c42dc486f7
  1. 3
      examples/mvvmcore/SampleCore/settings.xml
  2. 7
      src/mvvmquick/ErrorLabel.qml
  3. 2
      src/mvvmquick/inputviewfactory.cpp
  4. 1
      src/mvvmquick/qtmvvmquick_module.qrc
  5. 11
      src/mvvmwidgets/inputwidgetfactory.cpp
  6. 37
      src/mvvmwidgets/settingsdialog.cpp
  7. 5
      src/mvvmwidgets/widgetspresenter.cpp

3
examples/mvvmcore/SampleCore/settings.xml

@ -109,5 +109,8 @@
<Entry key="prop10"
type="QFont"
title="Choose a &amp;font" />
<Entry key="prop11"
type="non-existant-type"
title="Non existing type" />
</Category>
</SettingsConfig>

7
src/mvvmquick/ErrorLabel.qml

@ -0,0 +1,7 @@
import QtQuick 2.10
import QtQuick.Controls 2.3
Label {
text: qsTr("<i>Failed to load edit view!<i>")
color: "#800000"
}

2
src/mvvmquick/inputviewfactory.cpp

@ -50,7 +50,7 @@ QUrl InputViewFactory::getInputUrl(const QByteArray &type, const QVariantMap &vi
return QStringLiteral("qrc:/qtmvvm/inputs/RadioListEdit.qml");
else {
logCritical() << "Failed to find any input view for input type:" << type;
return QUrl();
return QStringLiteral("qrc:/qtmvvm/inputs/ErrorLabel.qml");
}
}

1
src/mvvmquick/qtmvvmquick_module.qrc

@ -9,6 +9,7 @@
<file>UrlField.qml</file>
<file>Switch.qml</file>
<file>RadioListEdit.qml</file>
<file>ErrorLabel.qml</file>
</qresource>
<qresource prefix="/qtmvvm/delegates">
<file>BoolDelegate.qml</file>

11
src/mvvmwidgets/inputwidgetfactory.cpp

@ -1,6 +1,7 @@
#include "inputwidgetfactory.h"
#include "inputwidgetfactory_p.h"
#include <QtMvvmCore/private/qtmvvm_logging_p.h>
#include <QtMvvmCore/IPresenter>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QLineEdit>
@ -13,6 +14,8 @@
#include <qurlvalidator.h>
#include <QtMvvmCore/private/qtmvvm_logging_p.h>
using namespace QtMvvm;
InputWidgetFactory::InputWidgetFactory() :
@ -64,10 +67,8 @@ QWidget *InputWidgetFactory::createInput(const QByteArray &type, QWidget *parent
widget = edit;
} else if(type == "selection" || type == "list")
widget = new SelectComboBox(parent);
else {
logCritical() << "Failed to find any input view for input type:" << type;
return nullptr; //TODO throw?
}
else
throw PresenterException("Unable to find an input view for type" + type);
for(auto it = viewProperties.constBegin(); it != viewProperties.constEnd(); it++)
widget->setProperty(qUtf8Printable(it.key()), it.value());

37
src/mvvmwidgets/settingsdialog.cpp

@ -186,23 +186,28 @@ void SettingsDialogPrivate::createEntry(const SettingsElements::Entry &entry, QW
});
content = btn;
} else {
auto widgetFactory = WidgetsPresenterPrivate::currentPresenter()->inputWidgetFactory();
content = widgetFactory->createInput(entry.type, sectionWidget, entry.properties);
if(!content) {
logWarning() << "Failed to create settings widget for type" << entry.type;
return;
try {
auto widgetFactory = WidgetsPresenterPrivate::currentPresenter()->inputWidgetFactory();
content = widgetFactory->createInput(entry.type, sectionWidget, entry.properties);
auto property = content->metaObject()->userProperty();
property.write(content, viewModel->loadValue(entry.key, entry.defaultValue));
if(property.hasNotifySignal()) {
auto changedSlot = metaObject()->method(metaObject()->indexOfSlot("propertyChanged()"));
connect(content, property.notifySignal(),
this, changedSlot);
} else
changedEntries.insert(content);
entryMap.insert(content, {entry, property});
} catch (PresenterException &e) {
logWarning() << "Failed to create settings widget for key"
<< entry.key
<< "with error:" << e.what();
content = new QLabel(tr("<i>Failed to load edit view!<i>"), sectionWidget);
auto pal = content->palette();
pal.setColor(QPalette::WindowText, Qt::darkRed);
content->setPalette(pal);
}
auto property = content->metaObject()->userProperty();
property.write(content, viewModel->loadValue(entry.key, entry.defaultValue));
if(property.hasNotifySignal()) {
auto changedSlot = metaObject()->method(metaObject()->indexOfSlot("propertyChanged()"));
connect(content, property.notifySignal(),
this, changedSlot);
} else
changedEntries.insert(content);
entryMap.insert(content, {entry, property});
}
auto label = new QLabel(entry.title + tr(":"), sectionWidget);

5
src/mvvmwidgets/widgetspresenter.cpp

@ -301,11 +301,6 @@ void WidgetsPresenter::presentMessageBox(const MessageConfig &config, QPointer<M
void WidgetsPresenter::presentInputDialog(const MessageConfig &config, QPointer<MessageResult> result)
{
auto input = d->inputViewFactory->createInput(config.subType(), nullptr, config.viewProperties());
if(!input) {
throw PresenterException(QByteArrayLiteral("Unable to find an input for type") +
config.subType());
}
QWidget *parent = nullptr;
if(!config.viewProperties().value(QStringLiteral("modal"), false).toBool())
parent = QApplication::activeWindow();

Loading…
Cancel
Save