diff --git a/src/zloadingwidget.cpp b/src/zloadingwidget.cpp index 5266712..925a5b0 100644 --- a/src/zloadingwidget.cpp +++ b/src/zloadingwidget.cpp @@ -5,14 +5,12 @@ #include #include -ZLoadingWidget::ZLoadingWidget(bool start, QWidget *parent) - : QStackedWidget{parent}, m_loadingIndicator(new QProcessIndicator()) +ZLoadingWidget::ZLoadingWidget(bool retryEnabled, QWidget *parent) + : QStackedWidget{parent}, m_loadingIndicator(new QProcessIndicator()), + m_retryEnabled(retryEnabled) { m_loadingIndicator->setType(QProcessIndicator::line_rotate); m_loadingIndicator->setFixedSize(64, 32); - if (start) { - m_loadingIndicator->start(); - } // Create a proper container widget for the loading indicator QWidget *loadingWidget = new QWidget(this); @@ -22,7 +20,7 @@ ZLoadingWidget::ZLoadingWidget(bool start, QWidget *parent) loadingLayout->addWidget(m_loadingIndicator); loadingLayout->addStretch(); - m_errorWidget = new ZLoadingErrorWidget(this); + m_errorWidget = new ZLoadingErrorWidget(m_retryEnabled, this); connect(static_cast(m_errorWidget), &ZLoadingErrorWidget::retryClicked, this, [this]() { emit retryClicked(); }); @@ -65,9 +63,9 @@ void ZLoadingWidget::setupErrorWidget(QLayout *errorLayout) addWidget(m_errorWidget); } -void ZLoadingWidget::setupAditionalWidget(QWidget *customWidget) +int ZLoadingWidget::setupAditionalWidget(QWidget *customWidget) { - addWidget(customWidget); + return addWidget(customWidget); } void ZLoadingWidget::switchToWidget(QWidget *widget) diff --git a/src/zloadingwidget.h b/src/zloadingwidget.h index 5029bdd..cabe010 100644 --- a/src/zloadingwidget.h +++ b/src/zloadingwidget.h @@ -1,38 +1,43 @@ #ifndef ZLOADINGWIDGET_H #define ZLOADINGWIDGET_H +#include #include #include #include #include -#include class ZLoadingErrorWidget : public QWidget { Q_OBJECT public: - ZLoadingErrorWidget(QWidget *parent = nullptr) + ZLoadingErrorWidget(bool retryEnabled, QWidget *parent = nullptr) { - QHBoxLayout *layout = new QHBoxLayout(this); + QVBoxLayout *layout = new QVBoxLayout(this); layout->addStretch(); m_errorLabel = new QLabel("An error occurred.", this); m_errorLabel->setAlignment(Qt::AlignCenter); + m_errorLabel->setWordWrap(true); m_errorLabel->setStyleSheet("QLabel { color: red; }"); layout->addWidget(m_errorLabel); - m_retryButton = new QPushButton("Retry", this); - m_retryButton->setMaximumWidth(m_retryButton->sizeHint().width()); - layout->addWidget(m_retryButton); + + if (retryEnabled) { + layout->addSpacing(10); + m_retryButton = new QPushButton("Retry", this); + m_retryButton->setMaximumWidth(m_retryButton->sizeHint().width()); + layout->addWidget(m_retryButton, 0, Qt::AlignCenter); + connect(m_retryButton, &QPushButton::clicked, this, + [this]() { emit retryClicked(); }); + } layout->addStretch(); - connect(m_retryButton, &QPushButton::clicked, this, - [this]() { emit retryClicked(); }); } void setText(const QString &text) { m_errorLabel->setText(text); }; private: - QLabel *m_errorLabel; - QPushButton *m_retryButton; + QLabel *m_errorLabel = nullptr; + QPushButton *m_retryButton = nullptr; signals: void retryClicked(); }; @@ -41,7 +46,8 @@ class ZLoadingWidget : public QStackedWidget { Q_OBJECT public: - explicit ZLoadingWidget(bool start = true, QWidget *parent = nullptr); + explicit ZLoadingWidget(bool retryEnabled = false, + QWidget *parent = nullptr); ~ZLoadingWidget(); void stop(bool showContent = true); void showLoading(); @@ -49,7 +55,7 @@ public: void setupContentWidget(QLayout *contentLayout); void setupErrorWidget(QWidget *errorWidget); void setupErrorWidget(QLayout *errorLayout); - void setupAditionalWidget(QWidget *customWidget); + int setupAditionalWidget(QWidget *customWidget); void switchToWidget(QWidget *widget); void showError(); void showError(const QString &errorMessage); @@ -58,6 +64,7 @@ private: class QProcessIndicator *m_loadingIndicator = nullptr; QWidget *m_contentWidget = nullptr; QWidget *m_errorWidget = nullptr; + bool m_retryEnabled = false; signals: void retryClicked(); };