| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "contextdialog.h" |
| 6 | |
| 7 | #include <QAbstractButton> |
| 8 | #include <QMessageBox> |
| 9 | #include <QHBoxLayout> |
| 10 | #include <QFile> |
| 11 | #include <QCheckBox> |
| 12 | #include <QPushButton> |
| 13 | #include <QGroupBox> |
| 14 | #include <QDebug> |
| 15 | |
| 16 | void ContextDialog::okCancel(QString text, QString title, |
| 17 | QMessageBox::Icon icon, |
| 18 | std::function<void (bool)> okCallBack, |
| 19 | std::function<void (bool)> cancelCallBack) |
| 20 | { |
| 21 | if (text.isEmpty()) |
| 22 | return; |
| 23 | |
| 24 | QMessageBox messageBox; |
| 25 | messageBox.setWindowTitle(title); |
| 26 | messageBox.setText(text); |
| 27 | messageBox.setIcon(icon); |
| 28 | messageBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); |
| 29 | messageBox.button(QMessageBox::Ok)->setText(QObject::tr("Ok" )); |
| 30 | messageBox.button(QMessageBox::Cancel)->setText(QObject::tr("Cancel" )); |
| 31 | if (okCallBack) |
| 32 | QObject::connect(messageBox.button(QMessageBox::Ok), |
| 33 | &QAbstractButton::clicked, okCallBack); |
| 34 | if (cancelCallBack) |
| 35 | QObject::connect(messageBox.button(QMessageBox::Cancel), |
| 36 | &QAbstractButton::clicked, cancelCallBack); |
| 37 | messageBox.exec(); |
| 38 | } |
| 39 | |
| 40 | void ContextDialog::ok(QString text, QString title, |
| 41 | QMessageBox::Icon icon, |
| 42 | std::function<void (bool)> okCallBack) |
| 43 | { |
| 44 | if (text.isEmpty()) |
| 45 | return; |
| 46 | |
| 47 | QMessageBox messageBox; |
| 48 | messageBox.setWindowTitle(title); |
| 49 | messageBox.setText(text); |
| 50 | messageBox.setIcon(icon); |
| 51 | messageBox.setStandardButtons(QMessageBox::Ok); |
| 52 | messageBox.button(QMessageBox::Ok)->setText(QObject::tr("Ok" )); |
| 53 | if (okCallBack) |
| 54 | QObject::connect(messageBox.button(QMessageBox::Ok), |
| 55 | &QAbstractButton::clicked, okCallBack); |
| 56 | messageBox.exec(); |
| 57 | } |
| 58 | |
| 59 | void ContextDialog::question(QString text, QString title, QMessageBox::Icon icon, std::function<void (bool)> okCallBack, std::function<void (bool)> noCallBack, std::function<void (bool)> cancelCallBack) |
| 60 | { |
| 61 | if (text.isEmpty()) |
| 62 | return; |
| 63 | |
| 64 | QMessageBox messageBox; |
| 65 | messageBox.setWindowTitle(title); |
| 66 | messageBox.setText(text); |
| 67 | messageBox.setIcon(icon); |
| 68 | messageBox.setStandardButtons(QMessageBox::Ok | QMessageBox::No | QMessageBox::Cancel); |
| 69 | messageBox.button(QMessageBox::Ok)->setText(QObject::tr("Ok" )); |
| 70 | messageBox.button(QMessageBox::No)->setText(QObject::tr("No" )); |
| 71 | messageBox.button(QMessageBox::Cancel)->setText(QObject::tr("Cancel" )); |
| 72 | if (okCallBack) |
| 73 | QObject::connect(messageBox.button(QMessageBox::Ok), |
| 74 | &QAbstractButton::clicked, okCallBack); |
| 75 | if (noCallBack) |
| 76 | QObject::connect(messageBox.button(QMessageBox::No), |
| 77 | &QAbstractButton::clicked, noCallBack); |
| 78 | if (cancelCallBack) |
| 79 | QObject::connect(messageBox.button(QMessageBox::Cancel), |
| 80 | &QAbstractButton::clicked, cancelCallBack); |
| 81 | messageBox.exec(); |
| 82 | } |
| 83 | |
| 84 | void ContextDialog::singleChoice(QSet<SingleChoiceBox::Info> infos, |
| 85 | QString windowTitle, QString choiceTitle, |
| 86 | std::function<void (const SingleChoiceBox::Info&)> okCallBack, |
| 87 | std::function<void (const SingleChoiceBox::Info&)> cancelCallBack) |
| 88 | { |
| 89 | QDialog dialog; |
| 90 | dialog.setWindowTitle(windowTitle); |
| 91 | |
| 92 | QHBoxLayout *hLayoutButton = new QHBoxLayout; |
| 93 | QVBoxLayout *vLayout = new QVBoxLayout; |
| 94 | QPushButton *okPbt = new QPushButton(QPushButton::tr("Ok" )); |
| 95 | QPushButton *cancelPbt = new QPushButton(QPushButton::tr("Cancel" )); |
| 96 | |
| 97 | SingleChoiceBox::Info cacheInfo; |
| 98 | SingleChoiceBox *box = new SingleChoiceBox; |
| 99 | box->setChoiceTitle(choiceTitle); |
| 100 | box->setInfos(infos); |
| 101 | QObject::connect(box, &SingleChoiceBox::selected, |
| 102 | [&cacheInfo](const SingleChoiceBox::Info &info){ |
| 103 | cacheInfo = info; |
| 104 | }); |
| 105 | |
| 106 | vLayout->addWidget(box); |
| 107 | vLayout->addLayout(hLayoutButton); |
| 108 | dialog.setLayout(vLayout); |
| 109 | |
| 110 | hLayoutButton->addWidget(okPbt); |
| 111 | hLayoutButton->addWidget(cancelPbt); |
| 112 | |
| 113 | QObject::connect(okPbt, &QPushButton::clicked, [=, &cacheInfo, &dialog](){ |
| 114 | if (okCallBack) { |
| 115 | okCallBack(cacheInfo); |
| 116 | } |
| 117 | dialog.close(); |
| 118 | }); |
| 119 | |
| 120 | QObject::connect(cancelPbt, &QPushButton::clicked, [=, &cacheInfo, &dialog](){ |
| 121 | if (cancelCallBack) { |
| 122 | cancelCallBack(cacheInfo); |
| 123 | } |
| 124 | dialog.close(); |
| 125 | }); |
| 126 | |
| 127 | dialog.exec(); |
| 128 | } |
| 129 | |