SILICON
Loading...
Searching...
No Matches
inputDialogUtils.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "inputDialogUtils.hpp"
19
20#include <ui/lifetime.hpp>
21
22#include <utility>
23
24#include <QGraphicsItem>
25#include <QGraphicsScene>
26#include <QGraphicsView>
27#include <QInputDialog>
28#include <QLineEdit>
29#include <QMessageBox>
30#include <QPushButton>
31#include <QWidget>
32
34
35QWidget* parentWidgetForGraphicsItem(const QGraphicsItem* item)
36{
37 if (!item || !item->scene() || item->scene()->views().empty())
38 return nullptr;
39
40 return item->scene()->views().front();
41}
42
43void getText(QWidget* parent, const QString& title, const QString& label,
44 const QString& defaultText, TextCallback callback, QObject* context)
45{
46 callback = guardCallback(context ? context : parent, std::move(callback));
47#ifdef __EMSCRIPTEN__
48 auto* dialog = new QInputDialog(parent);
49 dialog->setAttribute(Qt::WA_DeleteOnClose);
50 dialog->setWindowTitle(title);
51 dialog->setLabelText(label);
52 dialog->setInputMode(QInputDialog::TextInput);
53 dialog->setTextEchoMode(QLineEdit::Normal);
54 dialog->setTextValue(defaultText);
55
56 QObject::connect(
57 dialog, &QInputDialog::accepted, dialog,
58 [dialog, callback = std::move(callback)] { callback(dialog->textValue()); });
59 dialog->open();
60#else
61 bool ok = false;
62 const QString text =
63 QInputDialog::getText(parent, title, label, QLineEdit::Normal, defaultText, &ok);
64 if (ok)
65 callback(text);
66#endif
67}
68
69void getItem(QWidget* parent, const QString& title, const QString& label,
70 const QStringList& items, int current, bool editable, TextCallback callback,
71 QObject* context)
72{
73 callback = guardCallback(context ? context : parent, std::move(callback));
74#ifdef __EMSCRIPTEN__
75 auto* dialog = new QInputDialog(parent);
76 dialog->setAttribute(Qt::WA_DeleteOnClose);
77 dialog->setWindowTitle(title);
78 dialog->setLabelText(label);
79 dialog->setInputMode(QInputDialog::TextInput);
80 dialog->setComboBoxItems(items);
81 dialog->setComboBoxEditable(editable);
82 if (current >= 0 && current < items.size())
83 dialog->setTextValue(items[current]);
84
85 QObject::connect(
86 dialog, &QInputDialog::accepted, dialog,
87 [dialog, callback = std::move(callback)] { callback(dialog->textValue()); });
88 dialog->open();
89#else
90 bool ok = false;
91 const QString text =
92 QInputDialog::getItem(parent, title, label, items, current, editable, &ok);
93 if (ok)
94 callback(text);
95#endif
96}
97
98void warning(QWidget* parent, const QString& title, const QString& text)
99{
100#ifdef __EMSCRIPTEN__
101 auto* messageBox =
102 new QMessageBox(QMessageBox::Warning, title, text, QMessageBox::Ok, parent);
103 messageBox->setAttribute(Qt::WA_DeleteOnClose);
104 messageBox->open();
105#else
106 QMessageBox::warning(parent, title, text);
107#endif
108}
109
110void critical(QWidget* parent, const QString& title, const QString& text)
111{
112#ifdef __EMSCRIPTEN__
113 auto* messageBox =
114 new QMessageBox(QMessageBox::Critical, title, text, QMessageBox::Ok, parent);
115 messageBox->setAttribute(Qt::WA_DeleteOnClose);
116 messageBox->open();
117#else
118 QMessageBox::critical(parent, title, text);
119#endif
120}
121
122void question(QWidget* parent, const QString& title, const QString& text,
123 AcceptedCallback accepted, QObject* context)
124{
125 accepted = guardCallback(context ? context : parent, std::move(accepted));
126#ifdef __EMSCRIPTEN__
127 auto* messageBox = new QMessageBox(QMessageBox::Question, title, text,
128 QMessageBox::Yes | QMessageBox::No, parent);
129 messageBox->setDefaultButton(QMessageBox::No);
130 messageBox->setAttribute(Qt::WA_DeleteOnClose);
131 QObject::connect(messageBox, &QMessageBox::finished, messageBox,
132 [accepted = std::move(accepted)](const int result) {
133 if (result == QMessageBox::Yes)
134 accepted();
135 });
136 messageBox->open();
137#else
138 if (QMessageBox::question(parent, title, text, QMessageBox::Yes | QMessageBox::No,
139 QMessageBox::No)
140 == QMessageBox::Yes) {
141 accepted();
142 }
143#endif
144}
145
146void warningChoice(QWidget* parent, const QString& title, const QString& text,
147 const QString& primaryText, const QString& secondaryText,
148 ChoiceCallback callback, QObject* context)
149{
150 callback = guardCallback(context ? context : parent, std::move(callback));
151#ifdef __EMSCRIPTEN__
152 auto* messageBox =
153 new QMessageBox(QMessageBox::Warning, title, text, QMessageBox::NoButton, parent);
154 auto* primaryButton = messageBox->addButton(primaryText, QMessageBox::AcceptRole);
155 auto* secondaryButton =
156 messageBox->addButton(secondaryText, QMessageBox::DestructiveRole);
157 messageBox->addButton(QMessageBox::Cancel);
158 messageBox->setDefaultButton(primaryButton);
159 messageBox->setAttribute(Qt::WA_DeleteOnClose);
160 QObject::connect(messageBox, &QMessageBox::finished, messageBox,
161 [messageBox, primaryButton, secondaryButton,
162 callback = std::move(callback)](const int) {
163 if (messageBox->clickedButton() == primaryButton)
164 callback(Choice::Primary);
165 else if (messageBox->clickedButton() == secondaryButton)
166 callback(Choice::Secondary);
167 else
168 callback(Choice::Cancel);
169 });
170 messageBox->open();
171#else
172 QMessageBox messageBox(QMessageBox::Warning, title, text, QMessageBox::NoButton,
173 parent);
174 auto* primaryButton = messageBox.addButton(primaryText, QMessageBox::AcceptRole);
175 auto* secondaryButton =
176 messageBox.addButton(secondaryText, QMessageBox::DestructiveRole);
177 messageBox.addButton(QMessageBox::Cancel);
178 messageBox.setDefaultButton(primaryButton);
179 messageBox.exec();
180
181 if (messageBox.clickedButton() == primaryButton)
182 callback(Choice::Primary);
183 else if (messageBox.clickedButton() == secondaryButton)
184 callback(Choice::Secondary);
185 else
186 callback(Choice::Cancel);
187#endif
188}
189
190} // namespace SILICON::ui::inputDialog
std::function< void()> AcceptedCallback
void question(QWidget *parent, const QString &title, const QString &text, AcceptedCallback accepted, QObject *context)
std::function< void(const QString &)> TextCallback
QWidget * parentWidgetForGraphicsItem(const QGraphicsItem *item)
std::function< void(Choice)> ChoiceCallback
void critical(QWidget *parent, const QString &title, const QString &text)
void warningChoice(QWidget *parent, const QString &title, const QString &text, const QString &primaryText, const QString &secondaryText, ChoiceCallback callback, QObject *context)
Shows a three-way warning using caller-provided action labels.
void getItem(QWidget *parent, const QString &title, const QString &label, const QStringList &items, int current, bool editable, TextCallback callback, QObject *context)
void warning(QWidget *parent, const QString &title, const QString &text)
void getText(QWidget *parent, const QString &title, const QString &label, const QString &defaultText, TextCallback callback, QObject *context)
auto guardCallback(const QObject *owner, Callback callback)
Definition lifetime.hpp:20