Browse Source

improve msg box presenting, added svg stuff

pull/2/head
Skycoder42 7 years ago
parent
commit
9590e00f4f
  1. 1
      examples/mvvmquick/SampleQuick/main.cpp
  2. 23
      src/imports/mvvmquick/DialogPresenter.qml
  3. 6
      src/imports/mvvmquick/MsgBox.qml
  4. 18
      src/imports/mvvmquick/PopupPresenter.qml
  5. 11
      src/imports/mvvmquick/QtMvvmApp.qml
  6. 4
      src/imports/mvvmquick/icons/ic_error_white_24px.svg
  7. 4
      src/imports/mvvmquick/icons/ic_help_white_24px.svg
  8. 4
      src/imports/mvvmquick/icons/ic_info_white_24px.svg
  9. 4
      src/imports/mvvmquick/icons/ic_warning_white_24px.svg
  10. 13
      src/imports/mvvmquick/mvvmquick.pro
  11. 14
      src/imports/mvvmquick/qtmvvmquick_plugin.cpp
  12. 8
      src/imports/mvvmquick/qtmvvmquick_plugin.qrc
  13. 39
      src/imports/mvvmquick/svgimageprovider.cpp
  14. 18
      src/imports/mvvmquick/svgimageprovider.h

1
examples/mvvmquick/SampleQuick/main.cpp

@ -33,7 +33,6 @@ int main(int argc, char *argv[])
QtMvvm::ServiceRegistry::instance()->registerInterface<IEventService, QuickEventService>();
QQmlApplicationEngine engine;
QuickExtras::setupEngine(&engine);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;

23
src/imports/mvvmquick/DialogPresenter.qml

@ -15,10 +15,31 @@ QtObject {
return false;
}
function closeAction() {
if(_popups.length > 0) {
_popups[_popups.length - 1].reject();
return true;
} else
return false;
}
property var _popups: []
property Component _msgBoxComponent: Component {
MsgBox {
id: __msgBox
Component.onCompleted: __msgBox.open()
onClosed: {
var index = _popups.indexOf(__msgBox);
if(index > -1) {
__msgBox.destroy();
_dialogPresenter._popups.splice(index, 1);
}
}
Component.onCompleted: {
_popups.push(__msgBox)
__msgBox.open()
}
}
}

6
src/imports/mvvmquick/MsgBox.qml

@ -11,14 +11,14 @@ Dialog {
property MessageResult msgResult
property real extraHeight: 0
property real baseWidth: 300
x: parent ? (parent.width - width) / 2 : 0
y: parent ? deltaY() : 0
width: parent ? Math.min(Math.max(implicitWidth, 300), parent.width - 28) : implicitWidth
width: parent ? Math.min(Math.max(implicitWidth, baseWidth), parent.width - 28) : implicitWidth
height: parent ? Math.min(implicitHeight, parent.height - 28) : implicitWidth
modal: true
focus: true
closePolicy: Popup.CloseOnEscape
function deltaY() {
var unscaled = Qt.inputMethod.keyboardRectangle.height / Screen.devicePixelRatio;
@ -42,7 +42,7 @@ Dialog {
visible: msgConfig.subType != "about"
source: {
var base = "image://svg/de/skycoder42/qtmvvm/quick/icons/ic_%1";
switch(msgConfig.subType) {
switch(String(msgConfig.subType)) {
case "information":
base = base.arg("info");
break;

18
src/imports/mvvmquick/PopupPresenter.qml

@ -4,25 +4,27 @@ import QtQuick.Controls 2.3
QtObject {
id: _popPresenter
property var popups: []
property Item rootItem: null
function presentPopup(root, popup) {
popup.parent = root;
property var _popups: []
function presentPopup(popup) {
popup.parent = rootItem;
popup.closed.connect(function() {
var index = popups.indexOf(popup);
var index = _popups.indexOf(popup);
if(index > -1) {
popup.destroy();
popups.splice(index, 1);
_popups.splice(index, 1);
}
});
_popups.push(popup);
popup.open();
popups.push(popup);
return true;
}
function closeAction() {
if(popups.length > 0) {
popups[popups.length - 1].close();
if(_popups.length > 0) {
_popups[_popups.length - 1].close();
return true;
} else
return false;

11
src/imports/mvvmquick/QtMvvmApp.qml

@ -20,10 +20,11 @@ ApplicationWindow {
PopupPresenter {
id: _rootPopup
rootItem: _root.contentItem
}
DialogPresenter {
id: _dialogs
id: _rootDialogs
rootItem: _root.contentItem
}
@ -36,18 +37,20 @@ ApplicationWindow {
}
function presentPopup(popup) {
return _rootPopup.presentPopup(contentItem, popup);
return _rootPopup.presentPopup(popup);
}
function showDialog(config, result) {
return _dialogs.showDialog(config, result);
return _rootDialogs.showDialog(config, result);
}
Component.onCompleted: QuickPresenter.qmlPresenter = _root
onClosing: {
var closed = false;//messageBox.closeAction();
var closed = false;
if(!closed)
closed = _rootDialogs.closeAction();
if(!closed)
closed = _rootPopup.closeAction();
if(!closed)

4
src/imports/mvvmquick/icons/ic_error_white_24px.svg

@ -0,0 +1,4 @@
<svg fill="#FFFFFF" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/>
</svg>

After

Width:  |  Height:  |  Size: 260 B

4
src/imports/mvvmquick/icons/ic_help_white_24px.svg

@ -0,0 +1,4 @@
<svg fill="#FFFFFF" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 17h-2v-2h2v2zm2.07-7.75l-.9.92C13.45 12.9 13 13.5 13 15h-2v-.5c0-1.1.45-2.1 1.17-2.83l1.24-1.26c.37-.36.59-.86.59-1.41 0-1.1-.9-2-2-2s-2 .9-2 2H8c0-2.21 1.79-4 4-4s4 1.79 4 4c0 .88-.36 1.68-.93 2.25z"/>
</svg>

After

Width:  |  Height:  |  Size: 432 B

4
src/imports/mvvmquick/icons/ic_info_white_24px.svg

@ -0,0 +1,4 @@
<svg fill="#FFFFFF" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"/>
</svg>

After

Width:  |  Height:  |  Size: 260 B

4
src/imports/mvvmquick/icons/ic_warning_white_24px.svg

@ -0,0 +1,4 @@
<svg fill="#FFFFFF" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z"/>
</svg>

After

Width:  |  Height:  |  Size: 214 B

13
src/imports/mvvmquick/mvvmquick.pro

@ -1,4 +1,4 @@
QT += core qml quick quickcontrols2 mvvmquick mvvmquick-private
QT += core qml quick svg quickcontrols2 mvvmquick mvvmquick-private
CXX_MODULE = mvvmquick
TARGETPATH = de/skycoder42/QtMvvm/Quick
TARGET = declarative_mvvmquick
@ -8,11 +8,13 @@ DEFINES += "VERSION_MINOR=$$MODULE_VERSION_MINOR"
HEADERS += \
qtmvvmquick_plugin.h \
qqmlquickpresenter.h
qqmlquickpresenter.h \
svgimageprovider.h
SOURCES += \
qtmvvmquick_plugin.cpp \
qqmlquickpresenter.cpp
qqmlquickpresenter.cpp \
svgimageprovider.cpp
QML_FILES += \
QtMvvmApp.qml \
@ -27,7 +29,7 @@ OTHER_FILES += qmldir
generate_qmltypes {
typeextra1.target = qmltypes
typeextra1.depends += export LD_LIBRARY_PATH := "$$shadowed($$dirname(_QMAKE_CONF_))/lib/:$(LD_LIBRARY_PATH)"
typeextra1.depends += export LD_LIBRARY_PATH := "$$shadowed($$dirname(_QMAKE_CONF_))/lib/:$$[QT_INSTALL_LIBS]:$(LD_LIBRARY_PATH)"
typeextra2.target = qmltypes
typeextra2.depends += export QML2_IMPORT_PATH := "$$shadowed($$dirname(_QMAKE_CONF_))/qml/"
QMAKE_EXTRA_TARGETS += typeextra1 typeextra2
@ -42,3 +44,6 @@ generate_qmltypes {
mfirst.depends += qmltypes
QMAKE_EXTRA_TARGETS += mfirst
}
RESOURCES += \
qtmvvmquick_plugin.qrc

14
src/imports/mvvmquick/qtmvvmquick_plugin.cpp

@ -3,16 +3,28 @@
#include <QtQml>
#include "qqmlquickpresenter.h"
#include "svgimageprovider.h"
static void initResources()
{
#ifdef QT_STATIC
Q_INIT_RESOURCE(qtmvvmquick_plugin);
#endif
}
static QObject *createQuickPresenterQmlSingleton(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
{
Q_UNUSED(jsEngine)
//image provider is created together with the singleton
qmlEngine->addImageProvider(QStringLiteral("svg"), new QtMvvm::SvgImageProvider());
return new QtMvvm::QQmlQuickPresenter(qmlEngine);
}
QtMvvmQuickDeclarativeModule::QtMvvmQuickDeclarativeModule(QObject *parent) :
QQmlExtensionPlugin(parent)
{}
{
initResources();
}
void QtMvvmQuickDeclarativeModule::registerTypes(const char *uri)
{

8
src/imports/mvvmquick/qtmvvmquick_plugin.qrc

@ -0,0 +1,8 @@
<RCC>
<qresource prefix="/de/skycoder42/qtmvvm/quick/icons">
<file alias="ic_error.svg">icons/ic_error_white_24px.svg</file>
<file alias="ic_help.svg">icons/ic_help_white_24px.svg</file>
<file alias="ic_info.svg">icons/ic_info_white_24px.svg</file>
<file alias="ic_warning.svg">icons/ic_warning_white_24px.svg</file>
</qresource>
</RCC>

39
src/imports/mvvmquick/svgimageprovider.cpp

@ -0,0 +1,39 @@
#include "svgimageprovider.h"
#include <QtGui/QGuiApplication>
#include <QtGui/QPainter>
#include <QtGui/QScreen>
#include <QtSvg/QSvgRenderer>
using namespace QtMvvm;
SvgImageProvider::SvgImageProvider() :
QQuickImageProvider(Image)
{}
QImage SvgImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
auto path = QStringLiteral(":/%1.svg").arg(id);
QScopedPointer<QSvgRenderer, QScopedPointerDeleteLater> renderer(new QSvgRenderer(path));
if(renderer->isValid()) {
QSize tSize;
if(requestedSize.isValid())
tSize = requestedSize;
else {
tSize = renderer->defaultSize();
tSize *= QGuiApplication::primaryScreen()->devicePixelRatio();
}
QImage image(tSize, QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
QPainter painter(&image);
renderer->render(&painter);
*size = image.size();
return image;
} else {
*size = QSize();
return QImage();
}
}

18
src/imports/mvvmquick/svgimageprovider.h

@ -0,0 +1,18 @@
#ifndef QTMVVM_SVGIMAGEPROVIDER_H
#define QTMVVM_SVGIMAGEPROVIDER_H
#include <QtQuick/QQuickImageProvider>
namespace QtMvvm {
class SvgImageProvider : public QQuickImageProvider
{
public:
explicit SvgImageProvider();
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override;
};
}
#endif // QTMVVM_SVGIMAGEPROVIDER_H
Loading…
Cancel
Save