13 changed files with 623 additions and 10 deletions
@ -0,0 +1,131 @@ |
|||
#include "changeremoteviewmodel_p.h" |
|||
using namespace QtMvvm; |
|||
using namespace QtDataSync; |
|||
|
|||
std::tuple<RemoteConfig, bool> ChangeRemoteViewModel::result(const QVariant &data) |
|||
{ |
|||
auto map = data.toHash(); |
|||
return std::make_tuple(map.value(QStringLiteral("config")).value<RemoteConfig>(), |
|||
map.value(QStringLiteral("keepData")).toBool()); |
|||
} |
|||
|
|||
ChangeRemoteViewModel::ChangeRemoteViewModel(QObject *parent) : |
|||
ViewModel(parent), |
|||
_url(), |
|||
_accessKey(), |
|||
_keepAlive(), |
|||
_keepData(true), |
|||
_headerModel(new QStandardItemModel(0, 2, this)) |
|||
{ |
|||
_headerModel->setHorizontalHeaderLabels({tr("Key"), tr("Value")}); |
|||
_headerModel->setItemRoleNames({ |
|||
{KeyRole, "key"}, |
|||
{ValueRole, "value"} |
|||
}); |
|||
|
|||
connect(this, &ChangeRemoteViewModel::urlChanged, |
|||
this, &ChangeRemoteViewModel::validChanged); |
|||
} |
|||
|
|||
QUrl ChangeRemoteViewModel::url() const |
|||
{ |
|||
return _url; |
|||
} |
|||
|
|||
QString ChangeRemoteViewModel::accessKey() const |
|||
{ |
|||
return _accessKey; |
|||
} |
|||
|
|||
int ChangeRemoteViewModel::keepAlive() const |
|||
{ |
|||
return _keepAlive; |
|||
} |
|||
|
|||
bool ChangeRemoteViewModel::keepData() const |
|||
{ |
|||
return _keepData; |
|||
} |
|||
|
|||
QStandardItemModel *ChangeRemoteViewModel::headerModel() const |
|||
{ |
|||
return _headerModel; |
|||
} |
|||
|
|||
bool ChangeRemoteViewModel::isValid() const |
|||
{ |
|||
return _url.isValid(); |
|||
} |
|||
|
|||
bool ChangeRemoteViewModel::completeSetup() |
|||
{ |
|||
if(!isValid()) |
|||
return false; |
|||
|
|||
RemoteConfig::HeaderHash headers; |
|||
for(auto i = 0; i < _headerModel->rowCount(); i++) { |
|||
auto item = _headerModel->item(i); |
|||
headers.insert(item->text().toUtf8(), |
|||
item->data(ValueRole).toString().toUtf8()); |
|||
} |
|||
|
|||
QVariantHash hash; |
|||
hash[QStringLiteral("config")] = QVariant::fromValue<RemoteConfig>({ |
|||
_url, |
|||
_accessKey, |
|||
headers, |
|||
_keepAlive |
|||
}); |
|||
hash[QStringLiteral("keepData")] = _keepData; |
|||
emit resultReady(hash); |
|||
return true; |
|||
} |
|||
|
|||
void ChangeRemoteViewModel::setUrl(const QUrl &url) |
|||
{ |
|||
if (_url == url) |
|||
return; |
|||
|
|||
_url = url; |
|||
emit urlChanged(_url); |
|||
} |
|||
|
|||
void ChangeRemoteViewModel::setAccessKey(const QString &accessKey) |
|||
{ |
|||
if (_accessKey == accessKey) |
|||
return; |
|||
|
|||
_accessKey = accessKey; |
|||
emit accessKeyChanged(_accessKey); |
|||
} |
|||
|
|||
void ChangeRemoteViewModel::setKeepAlive(int keepAlive) |
|||
{ |
|||
if (_keepAlive == keepAlive) |
|||
return; |
|||
|
|||
_keepAlive = keepAlive; |
|||
emit keepAliveChanged(_keepAlive); |
|||
} |
|||
|
|||
void ChangeRemoteViewModel::setKeepData(bool keepData) |
|||
{ |
|||
if (_keepData == keepData) |
|||
return; |
|||
|
|||
_keepData = keepData; |
|||
emit keepDataChanged(_keepData); |
|||
} |
|||
|
|||
void ChangeRemoteViewModel::addHeaderConfig(const QString &key, const QString &value) |
|||
{ |
|||
auto item0 = new QStandardItem(key); |
|||
item0->setData(value, ValueRole); |
|||
auto item1 = new QStandardItem(value); |
|||
_headerModel->appendRow({item0, item1}); |
|||
} |
|||
|
|||
void ChangeRemoteViewModel::removeHeaderConfig(int index) |
|||
{ |
|||
_headerModel->removeRow(index); |
|||
} |
@ -0,0 +1,73 @@ |
|||
#ifndef QTMVVM_CHANGEREMOTEVIEWMODEL_P_H |
|||
#define QTMVVM_CHANGEREMOTEVIEWMODEL_P_H |
|||
|
|||
#include <QtMvvmCore/ViewModel> |
|||
|
|||
#include <QtDataSync/RemoteConfig> |
|||
|
|||
#include <QtGui/QStandardItemModel> |
|||
|
|||
#include "qtmvvmdatasynccore_global.h" |
|||
|
|||
namespace QtMvvm { |
|||
|
|||
class Q_MVVMDATASYNCCORE_EXPORT ChangeRemoteViewModel : public ViewModel |
|||
{ |
|||
Q_OBJECT |
|||
|
|||
Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged) |
|||
Q_PROPERTY(QString accessKey READ accessKey WRITE setAccessKey NOTIFY accessKeyChanged) |
|||
Q_PROPERTY(int keepAlive READ keepAlive WRITE setKeepAlive NOTIFY keepAliveChanged) |
|||
Q_PROPERTY(bool keepData READ keepData WRITE setKeepData NOTIFY keepDataChanged) |
|||
Q_PROPERTY(QStandardItemModel* headerModel READ headerModel CONSTANT) |
|||
|
|||
Q_PROPERTY(bool valid READ isValid NOTIFY validChanged) |
|||
|
|||
public: |
|||
enum Roles { |
|||
KeyRole = Qt::DisplayRole, |
|||
ValueRole = Qt::UserRole + 1 |
|||
}; |
|||
Q_ENUM(Roles) |
|||
|
|||
static std::tuple<QtDataSync::RemoteConfig, bool> result(const QVariant &data); |
|||
|
|||
Q_INVOKABLE explicit ChangeRemoteViewModel(QObject *parent = nullptr); |
|||
|
|||
QUrl url() const; |
|||
QString accessKey() const; |
|||
int keepAlive() const; |
|||
bool keepData() const; |
|||
QStandardItemModel* headerModel() const; |
|||
bool isValid() const; |
|||
|
|||
public Q_SLOTS: |
|||
bool completeSetup(); |
|||
|
|||
void setUrl(const QUrl &url); |
|||
void setAccessKey(const QString &accessKey); |
|||
void setKeepAlive(int keepAlive); |
|||
void setKeepData(bool keepData); |
|||
|
|||
void addHeaderConfig(const QString &key, const QString &value); |
|||
void removeHeaderConfig(int index); |
|||
|
|||
Q_SIGNALS: |
|||
void urlChanged(const QUrl &url); |
|||
void accessKeyChanged(const QString &accessKey); |
|||
void keepAliveChanged(int keepAlive); |
|||
void keepDataChanged(bool keepData); |
|||
void validChanged(); |
|||
|
|||
private: |
|||
QUrl _url; |
|||
QString _accessKey; |
|||
int _keepAlive; |
|||
bool _keepData; |
|||
|
|||
QStandardItemModel *_headerModel; |
|||
}; |
|||
|
|||
} |
|||
|
|||
#endif // QTMVVM_CHANGEREMOTEVIEWMODEL_P_H
|
@ -0,0 +1,74 @@ |
|||
#include "changeremotedialog_p.h" |
|||
#include "ui_changeremotedialog.h" |
|||
#include <QtMvvmCore/Binding> |
|||
#include <QtMvvmWidgets/private/widgetspresenter_p.h> |
|||
using namespace QtMvvm; |
|||
|
|||
ChangeRemoteDialog::ChangeRemoteDialog(ViewModel *viewModel, QWidget *parent) : |
|||
QDialog(parent), |
|||
_viewModel(static_cast<ChangeRemoteViewModel*>(viewModel)), |
|||
ui(new Ui::ChangeRemoteDialog) |
|||
{ |
|||
ui->setupUi(this); |
|||
if(parentWidget()) { |
|||
setWindowModality(Qt::WindowModal); |
|||
setWindowFlags(Qt::Sheet | Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint); |
|||
} else { |
|||
setWindowModality(Qt::ApplicationModal); |
|||
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint); |
|||
} |
|||
|
|||
ui->addHeaderButton->setDefaultAction(ui->actionA_dd_Header); |
|||
ui->removeHeaderButton->setDefaultAction(ui->action_Remove_Header); |
|||
ui->treeView->addActions({ |
|||
ui->actionA_dd_Header, |
|||
ui->action_Remove_Header |
|||
}); |
|||
ui->urlLineEdit->setValidator(createUrlValidator({QStringLiteral("ws"), QStringLiteral("wss")}, ui->urlLineEdit)); |
|||
|
|||
bind(_viewModel, "url", |
|||
ui->urlLineEdit, "text", |
|||
Binding::OneWayToViewModel); |
|||
bind(_viewModel, "accessKey", |
|||
ui->accessKeyLineEdit, "text", |
|||
Binding::OneWayToViewModel); |
|||
bind(_viewModel, "keepAlive", |
|||
ui->keepAliveTimeoutSpinBox, "value"); |
|||
bind(_viewModel, "keepData", |
|||
ui->keepDataCheckBox, "checked"); |
|||
bind(_viewModel, "valid", |
|||
ui->buttonBox->button(QDialogButtonBox::Ok), "enabled", |
|||
Binding::OneWayToView); |
|||
|
|||
connect(ui->actionA_dd_Header, &QAction::triggered, |
|||
this, &ChangeRemoteDialog::addHeader); |
|||
connect(ui->action_Remove_Header, &QAction::triggered, |
|||
this, &ChangeRemoteDialog::removeHeader); |
|||
|
|||
ui->treeView->setModel(_viewModel->headerModel()); |
|||
ui->treeView->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents); |
|||
} |
|||
|
|||
ChangeRemoteDialog::~ChangeRemoteDialog() {} |
|||
|
|||
void ChangeRemoteDialog::accept() |
|||
{ |
|||
if(_viewModel->completeSetup()) |
|||
QDialog::accept(); |
|||
} |
|||
|
|||
void ChangeRemoteDialog::addHeader() |
|||
{ |
|||
if(ui->keyEdit->text().isEmpty()) |
|||
return; |
|||
_viewModel->addHeaderConfig(ui->keyEdit->text(), ui->valueEdit->text()); |
|||
ui->keyEdit->clear(); |
|||
ui->valueEdit->clear(); |
|||
} |
|||
|
|||
void ChangeRemoteDialog::removeHeader() |
|||
{ |
|||
auto index = ui->treeView->currentIndex(); |
|||
if(index.isValid()) |
|||
_viewModel->removeHeaderConfig(index.row()); |
|||
} |
@ -0,0 +1,263 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<ui version="4.0"> |
|||
<class>ChangeRemoteDialog</class> |
|||
<widget class="QDialog" name="ChangeRemoteDialog"> |
|||
<property name="geometry"> |
|||
<rect> |
|||
<x>0</x> |
|||
<y>0</y> |
|||
<width>487</width> |
|||
<height>376</height> |
|||
</rect> |
|||
</property> |
|||
<property name="windowTitle"> |
|||
<string>Dialog</string> |
|||
</property> |
|||
<property name="sizeGripEnabled"> |
|||
<bool>true</bool> |
|||
</property> |
|||
<property name="modal"> |
|||
<bool>true</bool> |
|||
</property> |
|||
<layout class="QFormLayout" name="formLayout"> |
|||
<item row="0" column="0"> |
|||
<widget class="QLabel" name="urlLabel"> |
|||
<property name="text"> |
|||
<string>&Url:</string> |
|||
</property> |
|||
<property name="buddy"> |
|||
<cstring>urlLineEdit</cstring> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item row="0" column="1"> |
|||
<widget class="QLineEdit" name="urlLineEdit"> |
|||
<property name="placeholderText"> |
|||
<string>wss://example.org/qdsapp/</string> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item row="1" column="0"> |
|||
<widget class="QLabel" name="accessKeyLabel"> |
|||
<property name="text"> |
|||
<string>&Access Key:</string> |
|||
</property> |
|||
<property name="buddy"> |
|||
<cstring>accessKeyLineEdit</cstring> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item row="1" column="1"> |
|||
<widget class="QLineEdit" name="accessKeyLineEdit"> |
|||
<property name="echoMode"> |
|||
<enum>QLineEdit::Password</enum> |
|||
</property> |
|||
<property name="placeholderText"> |
|||
<string>Optional access secret</string> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item row="2" column="0"> |
|||
<widget class="QLabel" name="keepAliveTimeoutLabel"> |
|||
<property name="text"> |
|||
<string>Keep-&Alive Timeout:</string> |
|||
</property> |
|||
<property name="buddy"> |
|||
<cstring>keepAliveTimeoutSpinBox</cstring> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item row="2" column="1"> |
|||
<widget class="QSpinBox" name="keepAliveTimeoutSpinBox"> |
|||
<property name="buttonSymbols"> |
|||
<enum>QAbstractSpinBox::PlusMinus</enum> |
|||
</property> |
|||
<property name="accelerated"> |
|||
<bool>true</bool> |
|||
</property> |
|||
<property name="correctionMode"> |
|||
<enum>QAbstractSpinBox::CorrectToNearestValue</enum> |
|||
</property> |
|||
<property name="showGroupSeparator" stdset="0"> |
|||
<bool>true</bool> |
|||
</property> |
|||
<property name="suffix"> |
|||
<string> min</string> |
|||
</property> |
|||
<property name="minimum"> |
|||
<number>1</number> |
|||
</property> |
|||
<property name="maximum"> |
|||
<number>1440</number> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item row="3" column="0"> |
|||
<widget class="QLabel" name="keepDataLabel"> |
|||
<property name="text"> |
|||
<string>&Keep data:</string> |
|||
</property> |
|||
<property name="buddy"> |
|||
<cstring>keepDataCheckBox</cstring> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item row="3" column="1"> |
|||
<widget class="QCheckBox" name="keepDataCheckBox"> |
|||
<property name="checked"> |
|||
<bool>true</bool> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item row="5" column="0" colspan="2"> |
|||
<widget class="QTreeView" name="treeView"> |
|||
<property name="contextMenuPolicy"> |
|||
<enum>Qt::ActionsContextMenu</enum> |
|||
</property> |
|||
<property name="alternatingRowColors"> |
|||
<bool>true</bool> |
|||
</property> |
|||
<property name="rootIsDecorated"> |
|||
<bool>false</bool> |
|||
</property> |
|||
<property name="itemsExpandable"> |
|||
<bool>false</bool> |
|||
</property> |
|||
<property name="sortingEnabled"> |
|||
<bool>true</bool> |
|||
</property> |
|||
<property name="animated"> |
|||
<bool>true</bool> |
|||
</property> |
|||
<property name="expandsOnDoubleClick"> |
|||
<bool>false</bool> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item row="4" column="0" colspan="2"> |
|||
<widget class="QLabel" name="extraHeadersLabel"> |
|||
<property name="text"> |
|||
<string>Extra &Headers:</string> |
|||
</property> |
|||
<property name="buddy"> |
|||
<cstring>treeView</cstring> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item row="6" column="0" colspan="2"> |
|||
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,0,0,0,0"> |
|||
<item> |
|||
<widget class="QLineEdit" name="keyEdit"> |
|||
<property name="placeholderText"> |
|||
<string>Header Key</string> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item> |
|||
<widget class="QLineEdit" name="valueEdit"> |
|||
<property name="placeholderText"> |
|||
<string>Header Value</string> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item> |
|||
<widget class="QToolButton" name="addHeaderButton"> |
|||
<property name="toolButtonStyle"> |
|||
<enum>Qt::ToolButtonFollowStyle</enum> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
<item> |
|||
<spacer name="horizontalSpacer"> |
|||
<property name="orientation"> |
|||
<enum>Qt::Horizontal</enum> |
|||
</property> |
|||
<property name="sizeHint" stdset="0"> |
|||
<size> |
|||
<width>40</width> |
|||
<height>20</height> |
|||
</size> |
|||
</property> |
|||
</spacer> |
|||
</item> |
|||
<item> |
|||
<widget class="QToolButton" name="removeHeaderButton"> |
|||
<property name="toolButtonStyle"> |
|||
<enum>Qt::ToolButtonFollowStyle</enum> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
</layout> |
|||
</item> |
|||
<item row="7" column="0" colspan="2"> |
|||
<widget class="QDialogButtonBox" name="buttonBox"> |
|||
<property name="orientation"> |
|||
<enum>Qt::Horizontal</enum> |
|||
</property> |
|||
<property name="standardButtons"> |
|||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> |
|||
</property> |
|||
</widget> |
|||
</item> |
|||
</layout> |
|||
<action name="actionA_dd_Header"> |
|||
<property name="icon"> |
|||
<iconset theme="list-add"> |
|||
<normaloff>.</normaloff>.</iconset> |
|||
</property> |
|||
<property name="text"> |
|||
<string>A&dd Header</string> |
|||
</property> |
|||
<property name="shortcut"> |
|||
<string>Ctrl+Ins</string> |
|||
</property> |
|||
</action> |
|||
<action name="action_Remove_Header"> |
|||
<property name="icon"> |
|||
<iconset theme="list-remove"> |
|||
<normaloff>.</normaloff>.</iconset> |
|||
</property> |
|||
<property name="text"> |
|||
<string>&Remove Selected Header</string> |
|||
</property> |
|||
<property name="shortcut"> |
|||
<string>Del</string> |
|||
</property> |
|||
</action> |
|||
</widget> |
|||
<resources/> |
|||
<connections> |
|||
<connection> |
|||
<sender>buttonBox</sender> |
|||
<signal>accepted()</signal> |
|||
<receiver>ChangeRemoteDialog</receiver> |
|||
<slot>accept()</slot> |
|||
<hints> |
|||
<hint type="sourcelabel"> |
|||
<x>227</x> |
|||
<y>279</y> |
|||
</hint> |
|||
<hint type="destinationlabel"> |
|||
<x>157</x> |
|||
<y>274</y> |
|||
</hint> |
|||
</hints> |
|||
</connection> |
|||
<connection> |
|||
<sender>buttonBox</sender> |
|||
<signal>rejected()</signal> |
|||
<receiver>ChangeRemoteDialog</receiver> |
|||
<slot>reject()</slot> |
|||
<hints> |
|||
<hint type="sourcelabel"> |
|||
<x>295</x> |
|||
<y>285</y> |
|||
</hint> |
|||
<hint type="destinationlabel"> |
|||
<x>286</x> |
|||
<y>274</y> |
|||
</hint> |
|||
</hints> |
|||
</connection> |
|||
</connections> |
|||
</ui> |
@ -0,0 +1,40 @@ |
|||
#ifndef QTMVVM_CHANGEREMOTEDIALOG_P_H |
|||
#define QTMVVM_CHANGEREMOTEDIALOG_P_H |
|||
|
|||
#include <QtCore/QScopedPointer> |
|||
|
|||
#include <QtWidgets/QDialog> |
|||
|
|||
#include <QtMvvmDataSyncCore/private/changeremoteviewmodel_p.h> |
|||
|
|||
#include "qtmvvmdatasyncwidgets_global.h" |
|||
|
|||
namespace Ui { |
|||
class ChangeRemoteDialog; |
|||
} |
|||
|
|||
namespace QtMvvm { |
|||
|
|||
class Q_MVVMDATASYNCWIDGETS_EXPORT ChangeRemoteDialog : public QDialog |
|||
{ |
|||
Q_OBJECT |
|||
|
|||
public: |
|||
Q_INVOKABLE explicit ChangeRemoteDialog(QtMvvm::ViewModel *viewModel, QWidget *parent = nullptr); |
|||
~ChangeRemoteDialog(); |
|||
|
|||
public Q_SLOTS: |
|||
void accept() override; |
|||
|
|||
private Q_SLOTS: |
|||
void addHeader(); |
|||
void removeHeader(); |
|||
|
|||
private: |
|||
ChangeRemoteViewModel *_viewModel; |
|||
QScopedPointer<Ui::ChangeRemoteDialog> ui; |
|||
}; |
|||
|
|||
} |
|||
|
|||
#endif // QTMVVM_CHANGEREMOTEDIALOG_P_H
|
Loading…
Reference in new issue