From 54b766b0e70087858c725a2676ca7e010c4588b9 Mon Sep 17 00:00:00 2001 From: Nav Date: Wed, 12 Apr 2023 21:52:37 +0100 Subject: [PATCH] New Dialog widget base class and ConfirmationDialog widget --- src/Insight/CMakeLists.txt | 9 ++ .../Widgets/ConfirmationDialog.cpp | 32 +++++++ .../Widgets/ConfirmationDialog.hpp | 32 +++++++ .../InsightWindow/Widgets/Dialog/Dialog.cpp | 74 +++++++++++++++ .../InsightWindow/Widgets/Dialog/Dialog.hpp | 32 +++++++ .../Widgets/Dialog/Images/icon.svg | 94 +++++++++++++++++++ .../Widgets/Dialog/Stylesheets/Dialog.qss | 7 ++ .../Widgets/Dialog/UiFiles/Dialog.ui | 88 +++++++++++++++++ .../Widgets/ErrorDialogue/ErrorDialogue.hpp | 4 + 9 files changed, 372 insertions(+) create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/ConfirmationDialog.cpp create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/ConfirmationDialog.hpp create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Dialog.cpp create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Dialog.hpp create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Images/icon.svg create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Stylesheets/Dialog.qss create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/UiFiles/Dialog.ui diff --git a/src/Insight/CMakeLists.txt b/src/Insight/CMakeLists.txt index ed895837..f0d76b88 100755 --- a/src/Insight/CMakeLists.txt +++ b/src/Insight/CMakeLists.txt @@ -51,6 +51,10 @@ target_sources( # Error dialogue window ${CMAKE_CURRENT_SOURCE_DIR}/UserInterfaces/InsightWindow/Widgets/ErrorDialogue/ErrorDialogue.cpp + # Dialogue window + ${CMAKE_CURRENT_SOURCE_DIR}/UserInterfaces/InsightWindow/Widgets/Dialog/Dialog.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/UserInterfaces/InsightWindow/Widgets/ConfirmationDialog.cpp + # Target package widgets ${CMAKE_CURRENT_SOURCE_DIR}/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPackageWidgetContainer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPackageWidget.cpp @@ -201,4 +205,9 @@ qt_add_resources( "./UserInterfaces/InsightWindow/Widgets/ErrorDialogue/UiFiles/ErrorDialogue.ui" "./UserInterfaces/InsightWindow/Widgets/ErrorDialogue/Stylesheets/ErrorDialogue.qss" "./UserInterfaces/InsightWindow/Widgets/ErrorDialogue/Images/icon.svg" + + # Dialogue + "./UserInterfaces/InsightWindow/Widgets/Dialog/UiFiles/Dialog.ui" + "./UserInterfaces/InsightWindow/Widgets/Dialog/Stylesheets/Dialog.qss" + "./UserInterfaces/InsightWindow/Widgets/Dialog/Images/icon.svg" ) diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/ConfirmationDialog.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/ConfirmationDialog.cpp new file mode 100644 index 00000000..b6a0db88 --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/ConfirmationDialog.cpp @@ -0,0 +1,32 @@ +#include "ConfirmationDialog.hpp" + +namespace Bloom::Widgets +{ + ConfirmationDialog::ConfirmationDialog( + const QString& windowTitle, + const QString& text, + const std::optional& confirmationButtonText, + const std::optional& cancelButtonText, + QWidget* parent + ) + : Dialog(windowTitle, text, parent) + { + this->confirmButton->setStyleName("primary"); + + this->addActionButton(this->confirmButton); + this->addActionButton(this->cancelButton); + + this->confirmButton->setText(confirmationButtonText.value_or("Proceed")); + this->cancelButton->setText(cancelButtonText.value_or("Cancel")); + + QObject::connect(this->confirmButton, &QPushButton::clicked, this, [this] { + this->close(); + emit this->confirmed(); + }); + + QObject::connect(this->cancelButton, &QPushButton::clicked, this, [this] { + this->close(); + emit this->aborted(); + }); + } +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/ConfirmationDialog.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/ConfirmationDialog.hpp new file mode 100644 index 00000000..da006edc --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/ConfirmationDialog.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include + +#include "Dialog/Dialog.hpp" +#include "PushButton.hpp" + +namespace Bloom::Widgets +{ + class ConfirmationDialog: public Dialog + { + Q_OBJECT + + public: + explicit ConfirmationDialog( + const QString& windowTitle, + const QString& text, + const std::optional& confirmationButtonText, + const std::optional& cancelButtonText, + QWidget* parent = nullptr + ); + + signals: + void confirmed(); + void aborted(); + + protected: + PushButton* confirmButton = new PushButton(this); + PushButton* cancelButton = new PushButton(this); + }; +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Dialog.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Dialog.cpp new file mode 100644 index 00000000..3796095b --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Dialog.cpp @@ -0,0 +1,74 @@ +#include "Dialog.hpp" + +#include + +#include "src/Insight/UserInterfaces/InsightWindow/UiLoader.hpp" +#include "src/Services/PathService.hpp" +#include "src/Exceptions/Exception.hpp" + +namespace Bloom::Widgets +{ + using Bloom::Exceptions::Exception; + + Dialog::Dialog( + const QString& windowTitle, + const QString& text, + QWidget* parent + ) + : QDialog(parent) + { + this->setObjectName("dialog"); + this->setAttribute(Qt::WA_DeleteOnClose, true); + this->setWindowTitle(windowTitle); + + auto dialogUiFile = QFile( + QString::fromStdString(Services::PathService::compiledResourcesPath() + + "/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/UiFiles/Dialog.ui" + ) + ); + + auto dialogStylesheet = QFile( + QString::fromStdString(Services::PathService::compiledResourcesPath() + + "/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Stylesheets/Dialog.qss" + ) + ); + + if (!dialogUiFile.open(QFile::ReadOnly)) { + throw Exception("Failed to open Dialog UI file"); + } + + if (!dialogStylesheet.open(QFile::ReadOnly)) { + throw Exception("Failed to open Dialog stylesheet file"); + } + + this->setStyleSheet(dialogStylesheet.readAll()); + + auto uiLoader = UiLoader(this); + this->container = uiLoader.load(&dialogUiFile, this); + + this->textLabel = this->container->findChild("text-label"); + this->actionLayout = this->container->findChild("actions-layout"); + + this->container->setContentsMargins(15, 10, 15, 15); + + this->textLabel->setTextFormat(Qt::TextFormat::RichText); + this->textLabel->setText(text); + this->setMinimumSize(500, 150); + } + + void Dialog::showEvent(QShowEvent* event) { + const auto containerSize = this->container->sizeHint(); + const auto windowSize = QSize( + std::max(containerSize.width(), 500), + std::max(containerSize.height(), 100) + ); + + this->setFixedSize(windowSize); + this->container->setFixedSize(windowSize); + } + + void Dialog::addActionButton(QPushButton* button) { + button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + this->actionLayout->insertWidget(1, button); + } +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Dialog.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Dialog.hpp new file mode 100644 index 00000000..dcbc9ac0 --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Dialog.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include +#include + +#include "src/Insight/UserInterfaces/InsightWindow/Widgets/Label.hpp" + +namespace Bloom::Widgets +{ + class Dialog: public QDialog + { + Q_OBJECT + + public: + Dialog( + const QString& windowTitle, + const QString& text, + QWidget* parent + ); + + protected: + void showEvent(QShowEvent* event) override; + void addActionButton(QPushButton* button); + + private: + QWidget* container = nullptr; + Label* textLabel = nullptr; + QHBoxLayout* actionLayout = nullptr; + }; +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Images/icon.svg b/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Images/icon.svg new file mode 100644 index 00000000..33bd5216 --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Images/icon.svg @@ -0,0 +1,94 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + + diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Stylesheets/Dialog.qss b/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Stylesheets/Dialog.qss new file mode 100644 index 00000000..74daf966 --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Stylesheets/Dialog.qss @@ -0,0 +1,7 @@ +#dialog { + background-color: #373835; +} + +#text-label { + font-size: 14px; +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/UiFiles/Dialog.ui b/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/UiFiles/Dialog.ui new file mode 100644 index 00000000..395d46ae --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/UiFiles/Dialog.ui @@ -0,0 +1,88 @@ + + + + + + 0 + + + + + 0 + + + + + :/compiled/src/Insight/UserInterfaces/InsightWindow/Widgets/Dialog/Images/icon.svg + + + 44 + + + 40 + + + + + + + + 10 + + + + QSizePolicy::Fixed + + + + + + + + + + true + + + Qt::AlignLeft + + + + + + + + + + 20 + + + + QSizePolicy::Fixed + + + + + + + Qt::Vertical + + + + + + + 15 + + + + + Qt::Horizontal + + + + + + + + diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/ErrorDialogue/ErrorDialogue.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/ErrorDialogue/ErrorDialogue.hpp index 104f6c31..e4bc000f 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/ErrorDialogue/ErrorDialogue.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/ErrorDialogue/ErrorDialogue.hpp @@ -8,6 +8,10 @@ namespace Bloom::Widgets { + /** + * @deprecated + * TODO: Bin this. Replace all usages with Bloom::Widgets::Dialog. + */ class ErrorDialogue: public QDialog { Q_OBJECT