FlowArithmeticCalculator 1.0.0
Flow Arithmetic Calculator
Loading...
Searching...
No Matches
TextInputDialog.cpp
Go to the documentation of this file.
1#include "TextInputDialog.h"
2
3namespace gui::window::prompt {
4 std::optional<std::string> TextInputDialog::promptForTextInput(
5 const std::string& title,
6 const std::string& prompt,
7 const std::string& defaultValue,
9 const auto app = createTempQApplication();
10
11 bool ok = false;
12 const QString text = QInputDialog::getText(nullptr,
13 QString::fromStdString(title),
14 QString::fromStdString(prompt),
15 QLineEdit::Normal,
16 QString::fromStdString(defaultValue),
17 &ok);
18
19 // restore focus to the window
20 windowDelegate->focusWindow();
21
22 if (ok && !text.isEmpty()) {
23 return text.toStdString();
24 } else {
25 return std::nullopt;
26 }
27
28 return std::nullopt;
29 }
30
32 const std::string& title,
33 const std::string& prompt,
34 const std::optional<FloatingPoint>& defaultValue,
36 auto input = promptForTextInput(
37 title, prompt, defaultValue ? (defaultValue.value()).str() : "", windowDelegate);
38
39 if (!input.has_value()) {
40 return std::nullopt;
41 }
42
43 auto strValue = input.value();
44 // replace , with .
45 std::ranges::replace(strValue, ',', '.');
46
47 try {
48 return FloatingPoint(strValue);
49 } catch (const std::runtime_error&) {
50 return std::nullopt;
51 }
52 }
53
54} // namespace gui::window::prompt
virtual void focusWindow()=0
Focus the window.
static std::unique_ptr< QApplication > createTempQApplication()
Create a temporary QApplication instance.
static std::optional< std::string > promptForTextInput(const std::string &title, const std::string &prompt, const std::string &defaultValue, business_logic::delegate::IWindowDelegate *windowDelegate)
static std::optional< FloatingPoint > promptForFloatingPointInput(const std::string &title, const std::string &prompt, const std::optional< FloatingPoint > &defaultValue, business_logic::delegate::IWindowDelegate *windowDelegate)
GUI user prompt utilities.
boost::multiprecision::cpp_dec_float_50 FloatingPoint
Definition typenames.h:6