Browse Source

added progress dialog code to core

pull/2/head
Skycoder42 7 years ago
parent
commit
1dd2e12e19
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 148
      src/imports/mvvmcore/plugins.qmltypes
  2. 26
      src/imports/mvvmcore/qqmlmvvmmessage.cpp
  3. 13
      src/imports/mvvmcore/qqmlmvvmmessage.h
  4. 2
      src/imports/mvvmcore/qtmvvmcore_plugin.cpp
  5. 143
      src/mvvmcore/message.cpp
  6. 83
      src/mvvmcore/message.h
  7. 9
      src/mvvmcore/message_p.h

148
src/imports/mvvmcore/plugins.qmltypes

@ -91,6 +91,58 @@ Module {
Parameter { name: "result"; type: "QVariant" }
}
}
Component {
name: "QtMvvm::ProgressControl"
prototype: "QObject"
exports: ["de.skycoder42.QtMvvm.Core/ProgressControl 1.1"]
exportMetaObjectRevisions: [0]
Property { name: "indeterminate"; type: "bool" }
Property { name: "minimum"; type: "int" }
Property { name: "maximum"; type: "int" }
Property { name: "progress"; type: "int" }
Signal {
name: "indeterminateChanged"
Parameter { name: "indeterminate"; type: "bool" }
}
Signal {
name: "minimumChanged"
Parameter { name: "minimum"; type: "int" }
}
Signal {
name: "maximumChanged"
Parameter { name: "maximum"; type: "int" }
}
Signal {
name: "progressChanged"
Parameter { name: "progress"; type: "int" }
}
Signal { name: "closeRequested" }
Signal { name: "canceled" }
Signal { name: "closed" }
Method { name: "close" }
Method {
name: "setIndeterminate"
Parameter { name: "indeterminate"; type: "bool" }
}
Method {
name: "setMinimum"
Parameter { name: "minimum"; type: "int" }
}
Method {
name: "setMaximum"
Parameter { name: "maximum"; type: "int" }
}
Method {
name: "setProgress"
Parameter { name: "progress"; type: "int" }
}
Method {
name: "setProgress"
Parameter { name: "progressPercent"; type: "double" }
}
Method { name: "requestCancel" }
Method { name: "notifyClosed" }
}
Component {
name: "QtMvvm::QQmlCoreApp"
prototype: "QObject"
@ -490,6 +542,102 @@ Module {
Parameter { name: "onResult"; type: "QJSValue" }
}
Method { name: "getColor"; revision: 1 }
Method {
name: "showProgress"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
Parameter { name: "label"; type: "string" }
Parameter { name: "maximum"; type: "int" }
Parameter { name: "minimum"; type: "int" }
Parameter { name: "allowCancel"; type: "bool" }
Parameter { name: "value"; type: "int" }
}
Method {
name: "showProgress"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
Parameter { name: "label"; type: "string" }
Parameter { name: "maximum"; type: "int" }
Parameter { name: "minimum"; type: "int" }
Parameter { name: "allowCancel"; type: "bool" }
}
Method {
name: "showProgress"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
Parameter { name: "label"; type: "string" }
Parameter { name: "maximum"; type: "int" }
Parameter { name: "minimum"; type: "int" }
}
Method {
name: "showProgress"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
Parameter { name: "label"; type: "string" }
Parameter { name: "maximum"; type: "int" }
}
Method {
name: "showProgress"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
Parameter { name: "label"; type: "string" }
}
Method {
name: "showProgress"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
}
Method { name: "showProgress"; revision: 1; type: "QtMvvm::ProgressControl*" }
Method {
name: "showIndeterminateProgress"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
Parameter { name: "label"; type: "string" }
Parameter { name: "allowCancel"; type: "bool" }
}
Method {
name: "showIndeterminateProgress"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
Parameter { name: "label"; type: "string" }
}
Method {
name: "showIndeterminateProgress"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
}
Method { name: "showIndeterminateProgress"; revision: 1; type: "QtMvvm::ProgressControl*" }
Method {
name: "showBusy"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
Parameter { name: "label"; type: "string" }
Parameter { name: "allowCancel"; type: "bool" }
}
Method {
name: "showBusy"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
Parameter { name: "label"; type: "string" }
}
Method {
name: "showBusy"
revision: 1
type: "QtMvvm::ProgressControl*"
Parameter { name: "title"; type: "string" }
}
Method { name: "showBusy"; revision: 1; type: "QtMvvm::ProgressControl*" }
}
Component {
name: "QtMvvm::QQmlServiceRegistry"

26
src/imports/mvvmcore/qqmlmvvmmessage.cpp

@ -1,4 +1,5 @@
#include "qqmlmvvmmessage.h"
#include <QtQml/QQmlEngine>
using namespace QtMvvm;
QQmlMvvmMessage::QQmlMvvmMessage(QJSEngine *engine, QObject *parent) :
@ -110,9 +111,30 @@ void QtMvvm::QQmlMvvmMessage::getColor(const QJSValue &onResult, const QString &
{
if(onResult.isCallable()) {
auto engine = _engine;
QtMvvm::getColor(this, [engine, onResult](const QColor &color){
QJSValue(onResult).call({engine->toScriptValue(color)});
QtMvvm::getColor(this, [engine, onResult](const QColor &resColor){
QJSValue(onResult).call({engine->toScriptValue(resColor)});
}, title, color, argb);
} else
QtMvvm::getColor(title, color, argb);
}
ProgressControl *QtMvvm::QQmlMvvmMessage::showProgress(const QString &title, const QString &label, int maximum, int minimum, bool allowCancel, int value)
{
auto control = QtMvvm::showProgress(title, label, maximum, minimum, allowCancel, value);
QQmlEngine::setObjectOwnership(control, QQmlEngine::JavaScriptOwnership);
return control;
}
ProgressControl *QtMvvm::QQmlMvvmMessage::showIndeterminateProgress(const QString &title, const QString &label, bool allowCancel)
{
auto control = QtMvvm::showIndeterminateProgress(title, label, allowCancel);
QQmlEngine::setObjectOwnership(control, QQmlEngine::JavaScriptOwnership);
return control;
}
ProgressControl *QtMvvm::QQmlMvvmMessage::showBusy(const QString &title, const QString &label, bool allowCancel)
{
auto control = QtMvvm::showBusy(title, label, allowCancel);
QQmlEngine::setObjectOwnership(control, QQmlEngine::JavaScriptOwnership);
return control;
}

13
src/imports/mvvmcore/qqmlmvvmmessage.h

@ -115,6 +115,19 @@ public Q_SLOTS:
const QColor &color = {},
bool argb = false);
Q_REVISION(1) static QtMvvm::ProgressControl *showProgress(const QString &title = {},
const QString &label = {},
int maximum = 100,
int minimum = 0,
bool allowCancel = true,
int value = 0);
Q_REVISION(1) static QtMvvm::ProgressControl *showIndeterminateProgress(const QString &title = {},
const QString &label = {},
bool allowCancel = true);
Q_REVISION(1) static QtMvvm::ProgressControl *showBusy(const QString &title = {},
const QString &label = {},
bool allowCancel = true);
#ifndef DOXYGEN_RUN
#undef static
#endif

2
src/imports/mvvmcore/qtmvvmcore_plugin.cpp

@ -51,6 +51,8 @@ void QtMvvmCoreDeclarativeModule::registerTypes(const char *uri)
qmlRegisterSingletonType<QtMvvm::QQmlMvvmMessage>(uri, 1, 0, "Message", createMessageSingleton);
//Version 1.1
qmlRegisterType<QtMvvm::ProgressControl>(uri, 1, 1, "ProgressControl");
qmlRegisterSingletonType<QtMvvm::QQmlServiceRegistry>(uri, 1, 1, "ServiceRegistry", createRegistrySingleton);
qmlRegisterSingletonType<QtMvvm::QQmlCoreApp>(uri, 1, 1, "CoreApp", createCoreAppSingleton);

143
src/mvvmcore/message.cpp

@ -3,6 +3,8 @@
#include "coreapp.h"
#include "qtmvvm_logging_p.h"
#include <cmath>
#include <QtGui/QGuiApplication>
using namespace QtMvvm;
@ -11,6 +13,7 @@ const QByteArray MessageConfig::TypeMessageBox = "msgbox";
const QByteArray MessageConfig::TypeInputDialog = "input";
const QByteArray MessageConfig::TypeFileDialog = "file";
const QByteArray MessageConfig::TypeColorDialog = "color";
const QByteArray MessageConfig::TypeProgressDialog = "progress";
const QByteArray MessageConfig::SubTypeInformation = "information";
const QByteArray MessageConfig::SubTypeWarning = "warning";
@ -26,6 +29,9 @@ const QByteArray MessageConfig::SubTypeSaveFile = "save";
const QByteArray MessageConfig::SubTypeRgb = "rgb";
const QByteArray MessageConfig::SubTypeArgb = "argb";
const QByteArray MessageConfig::SubTypeProgress = "progress";
const QByteArray MessageConfig::SubTypeBusy = "busy";
MessageConfig::MessageConfig(const QByteArray &type, const QByteArray &subType) :
d(new MessageConfigPrivate(type, subType))
{
@ -274,6 +280,92 @@ void MessageResult::setAutoDelete(bool autoDelete)
emit autoDeleteChanged(autoDelete, {});
}
ProgressControl::ProgressControl(QObject *parent) :
QObject{parent},
d{new ProgressControlPrivate{}}
{}
bool ProgressControl::isIndeterminate() const
{
return d->indeterminate;
}
ProgressControl::~ProgressControl() = default;
int ProgressControl::minimum() const
{
return d->minimum;
}
int ProgressControl::maximum() const
{
return d->maximum;
}
int ProgressControl::progress() const
{
return d->progress;
}
void ProgressControl::requestCancel()
{
emit canceled({});
}
void ProgressControl::notifyClosed()
{
emit closed({});
}
void ProgressControl::close()
{
emit closeRequested({});
}
void ProgressControl::setIndeterminate(bool indeterminate)
{
if (d->indeterminate == indeterminate)
return;
d->indeterminate = indeterminate;
emit indeterminateChanged(d->indeterminate);
}
void ProgressControl::setMinimum(int minimum)
{
if (d->minimum == minimum)
return;
d->minimum = minimum;
emit minimumChanged(d->minimum);
}
void ProgressControl::setMaximum(int maximum)
{
if (d->maximum == maximum)
return;
d->maximum = maximum;
emit maximumChanged(d->maximum);
}
void ProgressControl::setProgress(int progress)
{
if (d->progress == progress)
return;
d->progress = progress;
emit progressChanged(d->progress);
}
void ProgressControl::setProgress(double progressPercent)
{
Q_ASSERT_X(progressPercent >= 0.0 && progressPercent <= 1.0, Q_FUNC_INFO, "progressPercent must be in the range [0.0,1.0]");
setProgress(static_cast<int>(std::round((d->maximum - d->minimum) * progressPercent + d->minimum)));
}
// ------------- Private Implementation -------------
QtMvvm::MessageConfigPrivate::MessageConfigPrivate(QByteArray type, QByteArray subType) :
@ -605,3 +697,54 @@ void QtMvvm::getColor(const std::function<void (QColor)> &onResult, const QStrin
{
getColor(CoreApp::instance(), onResult, title, color, argb);
}
MessageResult *QtMvvm::showProgress(const QString &title, const QString &label, ProgressControl *control, bool allowCancel, bool isBusy)
{
MessageConfig config(MessageConfig::TypeProgressDialog, isBusy ? MessageConfig::SubTypeBusy : MessageConfig::SubTypeProgress);
config.setTitle(title);
config.setText(label);
config.setDefaultValue(QVariant::fromValue<QPointer<ProgressControl>>(control));
config.setViewProperty(QStringLiteral("allowCancel"), allowCancel);
return CoreApp::showDialog(config);
}
ProgressControl *QtMvvm::showProgress(QObject *scope, const QString &title, const QString &label, int maximum, int minimum, bool allowCancel, int value)
{
auto control = new ProgressControl{scope};
control->setMaximum(maximum);
control->setMinimum(minimum);
control->setProgress(value);
showProgress(title, label, control, allowCancel, false);
return control;
}
ProgressControl *QtMvvm::showProgress(const QString &title, const QString &label, int maximum, int minimum, bool allowCancel, int value)
{
return showProgress(nullptr, title, label, maximum, minimum, allowCancel, value);
}
ProgressControl *QtMvvm::showIndeterminateProgress(QObject *scope, const QString &title, const QString &label, bool allowCancel)
{
auto control = new ProgressControl{scope};
control->setIndeterminate(true);
showProgress(title, label, control, allowCancel, false);
return control;
}
ProgressControl *QtMvvm::showIndeterminateProgress(const QString &title, const QString &label, bool allowCancel)
{
return showIndeterminateProgress(nullptr, title, label, allowCancel);
}
ProgressControl *QtMvvm::showBusy(QObject *scope, const QString &title, const QString &label, bool allowCancel)
{
auto control = new ProgressControl{scope};
control->setIndeterminate(true);
showProgress(title, label, control, allowCancel, true);
return control;
}
ProgressControl *QtMvvm::showBusy(const QString &title, const QString &label, bool allowCancel)
{
return showBusy(nullptr, title, label, allowCancel);
}

83
src/mvvmcore/message.h

@ -80,6 +80,7 @@ public:
static const QByteArray TypeFileDialog;
//! @}
static const QByteArray TypeColorDialog;
static const QByteArray TypeProgressDialog;
/**
* @name Possible values for MessageConfig::subType when using the type MessageConfig::TypeMessageBox
@ -115,6 +116,9 @@ public:
static const QByteArray SubTypeRgb;
static const QByteArray SubTypeArgb;
static const QByteArray SubTypeProgress;
static const QByteArray SubTypeBusy;
//! Default constructor, can take a type and a subtype
MessageConfig(const QByteArray &type = TypeMessageBox, const QByteArray &subType = {});
//! Copy constructor
@ -236,6 +240,52 @@ private:
QScopedPointer<MessageResultPrivate> d;
};
class ProgressControlPrivate;
class Q_MVVMCORE_EXPORT ProgressControl : public QObject
{
Q_OBJECT
Q_PROPERTY(bool indeterminate READ isIndeterminate WRITE setIndeterminate NOTIFY indeterminateChanged)
Q_PROPERTY(int minimum READ minimum WRITE setMinimum NOTIFY minimumChanged)
Q_PROPERTY(int maximum READ maximum WRITE setMaximum NOTIFY maximumChanged)
Q_PROPERTY(int progress READ progress WRITE setProgress NOTIFY progressChanged)
public:
explicit ProgressControl(QObject *parent = nullptr);
~ProgressControl() override;
bool isIndeterminate() const;
int minimum() const;
int maximum() const;
int progress() const;
Q_INVOKABLE void requestCancel();
Q_INVOKABLE void notifyClosed();
public Q_SLOTS:
void close();
void setIndeterminate(bool indeterminate);
void setMinimum(int minimum);
void setMaximum(int maximum);
void setProgress(int progress);
void setProgress(double progressPercent);
Q_SIGNALS:
void indeterminateChanged(bool indeterminate);
void minimumChanged(int minimum);
void maximumChanged(int maximum);
void progressChanged(int progress);
void closeRequested(QPrivateSignal);
void canceled(QPrivateSignal);
void closed(QPrivateSignal);
private:
QScopedPointer<ProgressControlPrivate> d;
};
/**
* @name Methods to show simple messageboxes (MessageConfig::TypeMessageBox)
* @{
@ -468,6 +518,39 @@ Q_MVVMCORE_EXPORT void getColor(const std::function<void(QColor)> &onResult,
const QColor &color = {},
bool argb = false);
Q_MVVMCORE_EXPORT MessageResult *showProgress(const QString &title,
const QString &label,
ProgressControl *control,
bool allowCancel = true,
bool isBusy = false);
Q_MVVMCORE_EXPORT ProgressControl *showProgress(QObject *scope,
const QString &title = {},
const QString &label = {},
int maximum = 100,
int minimum = 0,
bool allowCancel = true,
int value = 0);
Q_MVVMCORE_EXPORT ProgressControl *showProgress(const QString &title = {},
const QString &label = {},
int maximum = 100,
int minimum = 0,
bool allowCancel = true,
int value = 0);
Q_MVVMCORE_EXPORT ProgressControl *showIndeterminateProgress(QObject *scope,
const QString &title = {},
const QString &label = {},
bool allowCancel = true);
Q_MVVMCORE_EXPORT ProgressControl *showIndeterminateProgress(const QString &title = {},
const QString &label = {},
bool allowCancel = true);
Q_MVVMCORE_EXPORT ProgressControl *showBusy(QObject *scope,
const QString &title = {},
const QString &label = {},
bool allowCancel = true);
Q_MVVMCORE_EXPORT ProgressControl *showBusy(const QString &title = {},
const QString &label = {},
bool allowCancel = true);
}
Q_DECLARE_METATYPE(QtMvvm::MessageConfig)

9
src/mvvmcore/message_p.h

@ -38,6 +38,15 @@ public:
bool autoDelete = true;
};
class ProgressControlPrivate
{
public:
bool indeterminate = false;
int minimum = 0;
int maximum = 100;
int progress = -1;
};
}
#endif // QTMVVM_MESSAGE_P_H

Loading…
Cancel
Save