Skip to content

Commit

Permalink
Introduce showKiwixMessageBox()
Browse files Browse the repository at this point in the history
Added a function to show custom choices instead of yes/no. The chosen option is returned
A closeButton is also added to the dialog.
  • Loading branch information
juuz0 committed Jun 14, 2024
1 parent b3065a9 commit f708fc5
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 38 deletions.
9 changes: 9 additions & 0 deletions resources/css/confirmBox.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ QPushButton:hover {
color: white;
}

#closeButton {
border: 0;
}

#closeButton:hover {
background-color: transparent;
}



23 changes: 20 additions & 3 deletions src/kiwixmessagebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <QFile>
#include "kiwixapp.h"

KiwixMessageBox::KiwixMessageBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent) :
KiwixMessageBox::KiwixMessageBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent,
QString leftAction, QString rightAction) :
QDialog(parent), m_confirmTitle(confirmTitle), m_confirmText(confirmText),
ui(new Ui::kiwixmessagebox)
{
Expand All @@ -12,17 +13,26 @@ KiwixMessageBox::KiwixMessageBox(QString confirmTitle, QString confirmText, bool
setStyleSheet(KiwixApp::instance()->parseStyleFromFile(":/css/confirmBox.css"));
connect(ui->yesButton, &QPushButton::clicked, [=]() {
emit yesClicked();
m_result = YesClicked;
accept();
});
connect(ui->noButton, &QPushButton::clicked, [=]() {
emit noClicked();
m_result = NoClicked;
reject();
});
connect(ui->okButton, &QPushButton::clicked, [=]() {
emit okClicked();
m_result = OkClicked;
});
connect(ui->closeButton, &QPushButton::clicked, [=]() {
this->close();
m_result = CloseClicked;
});
ui->confirmText->setText(confirmText);
ui->confirmTitle->setText(confirmTitle);
ui->yesButton->setText(gt("yes"));
ui->noButton->setText(gt("no"));
ui->yesButton->setText(leftAction);
ui->noButton->setText(rightAction);
ui->okButton->setText(gt("ok"));
ui->okButton->hide();
if (okDialog) {
Expand All @@ -45,3 +55,10 @@ void showInfoBox(QString title, QString text, QWidget *parent)
dialog->deleteLater();
});
}

KiwixMessageBox::Result showKiwixMessageBox(QString title, QString text, QWidget *parent, QString leftTitle, QString rightTitle)
{
KiwixMessageBox *dialog = new KiwixMessageBox(title, text, false, parent, leftTitle, rightTitle);
return dialog->execDialog();
}

13 changes: 12 additions & 1 deletion src/kiwixmessagebox.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define KIWIXCONFIRMBOX_H

#include <QDialog>
#include "kiwixapp.h"

namespace Ui {
class kiwixmessagebox;
Expand All @@ -12,8 +13,16 @@ class KiwixMessageBox : public QDialog
Q_OBJECT

public:
KiwixMessageBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent = nullptr);
KiwixMessageBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent = nullptr,
QString leftAction = gt("yes"), QString rightAction = gt("no"));
~KiwixMessageBox();
enum Result {
YesClicked,
NoClicked,
OkClicked,
CloseClicked
};
Result execDialog() { QDialog::exec(); return m_result; }

signals:
void yesClicked();
Expand All @@ -24,10 +33,12 @@ class KiwixMessageBox : public QDialog
QString m_confirmTitle;
QString m_confirmText;
Ui::kiwixmessagebox *ui;
Result m_result;
};


void showInfoBox(QString title, QString text, QWidget *parent = nullptr);
KiwixMessageBox::Result showKiwixMessageBox(QString title, QString text, QWidget *parent, QString leftTitle, QString rightTitle);

template<class YesAction>
void showConfirmBox(QString title, QString text, QWidget *parent,
Expand Down
122 changes: 88 additions & 34 deletions ui/kiwixmessagebox.ui
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,7 @@
<string>Dialog</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<widget class="QLabel" name="confirmText">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>600</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Would you like to confirm doing xyz?</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="confirmTitle">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Confirm title</string>
</property>
</widget>
</item>
<item row="2" column="0">
<item row="3" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
Expand Down Expand Up @@ -90,6 +58,9 @@
<height>0</height>
</size>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>Yes</string>
</property>
Expand All @@ -112,6 +83,9 @@
<height>0</height>
</size>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>Ok</string>
</property>
Expand All @@ -134,6 +108,9 @@
<height>0</height>
</size>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>No</string>
</property>
Expand All @@ -144,8 +121,85 @@
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="confirmText">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>600</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Would you like to confirm doing xyz?</string>
</property>
</widget>
</item>
<item row="1" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="confirmTitle">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Confirm title</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<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="QPushButton" name="closeButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>20</width>
<height>16777215</height>
</size>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../resources/kiwix.qrc">
<normaloff>:/icons/close.svg</normaloff>:/icons/close.svg</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<resources>
<include location="../resources/kiwix.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit f708fc5

Please sign in to comment.