Skip to content

Commit

Permalink
Extend accountmodalwidget to support more scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOneRing committed Apr 3, 2024
1 parent ea9e4b3 commit 1fde700
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
54 changes: 53 additions & 1 deletion src/gui/accountmodalwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,54 @@
#include "accountmodalwidget.h"
#include "ui_accountmodalwidget.h"

#include "resources/resources.h"

#include <QMessageBox>
#include <QPushButton>
#include <QQuickWidget>

namespace OCC {
AccountModalWidget::AccountModalWidget(const QString &title, QWidget *widget, QWidget *parent)

namespace {
auto *createQmlWidget(const QUrl &url, const QList<QQmlContext::PropertyPair> &properties)
{
auto *widget = new QQuickWidget;
widget->engine()->rootContext()->setContextProperties(properties);
widget->engine()->addImageProvider(QStringLiteral("ownCloud"), new Resources::CoreImageProvider());
widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
widget->setSource(url);
if (!widget->errors().isEmpty()) {
auto box = new QMessageBox(QMessageBox::Critical, QStringLiteral("QML Error"), QDebug::toString(widget->errors()));
box->setAttribute(Qt::WA_DeleteOnClose);
box->exec();
qFatal("A qml error occured %s", qPrintable(QDebug::toString(widget->errors())));
}
return widget;
}
}

AccountModalWidget::AccountModalWidget(
const QString &title, const QUrl &qmlSrc, const QList<QQmlContext::PropertyPair> &properties, const QList<Button> &buttons, QWidget *parent)
: AccountModalWidget(title, createQmlWidget(qmlSrc, properties), buttons, parent)
{
}


AccountModalWidget::AccountModalWidget(const QString &title, QWidget *widget, const QList<Button> &buttons, QWidget *parent)
: QWidget(parent)
, ui(new Ui::AccountModalWidget)
{
ui->setupUi(this);
ui->groupBox->setTitle(title);
ui->groupBox->layout()->addWidget(widget);

if (!buttons.isEmpty()) {
ui->buttonBox->clear();
for (auto [button, role] : buttons) {
ui->buttonBox->addButton(button, role);
}
}

connect(ui->buttonBox, &QDialogButtonBox::accepted, this, [this] {
Q_EMIT accepted();
Q_EMIT finished();
Expand All @@ -32,5 +71,18 @@ AccountModalWidget::AccountModalWidget(const QString &title, QWidget *widget, QW
Q_EMIT rejected();
Q_EMIT finished();
});

connect(ui->buttonBox, &QDialogButtonBox::clicked, this, [this](auto *button) { _clickedButton = button; });
}

QDialogButtonBox *AccountModalWidget::buttons()
{
return ui->buttonBox;
}

QAbstractButton *AccountModalWidget::clickedButton() const
{
return _clickedButton;
}

} // OCC
15 changes: 14 additions & 1 deletion src/gui/accountmodalwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
*/

#pragma once

#include <QDialogButtonBox>
#include <QQmlContext>
#include <QWidget>


namespace OCC {

namespace Ui {
Expand All @@ -25,7 +29,15 @@ class AccountModalWidget : public QWidget
{
Q_OBJECT
public:
AccountModalWidget(const QString &title, QWidget *widget, QWidget *parent);
using Button = QPair<QPushButton *, QDialogButtonBox::ButtonRole>;

AccountModalWidget(const QString &title, const QUrl &qmlSrc, const QList<QQmlContext::PropertyPair> &properties, const QList<Button> &buttons = {},
QWidget *parent = nullptr);
AccountModalWidget(const QString &title, QWidget *widget, const QList<Button> &buttons = {}, QWidget *parent = nullptr);

QDialogButtonBox *buttons();

QAbstractButton *clickedButton() const;

Q_SIGNALS:
void accepted();
Expand All @@ -34,6 +46,7 @@ class AccountModalWidget : public QWidget

private:
Ui::AccountModalWidget *ui;
QAbstractButton *_clickedButton = nullptr;
};

} // OCC
2 changes: 1 addition & 1 deletion src/gui/accountsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void AccountSettings::showSelectiveSyncDialog(Folder *folder)
folder->remotePath(), folder->displayName(), folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, &ok));
Q_ASSERT(ok);

auto *modalWidget = new AccountModalWidget(tr("Choose what to sync"), selectiveSync, this);
auto *modalWidget = new AccountModalWidget(tr("Choose what to sync"), selectiveSync, {}, this);
connect(modalWidget, &AccountModalWidget::accepted, this, [selectiveSync, folder, this] {
folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, selectiveSync->createBlackList());
Q_EMIT folderChanged();
Expand Down

0 comments on commit 1fde700

Please sign in to comment.