Debian Codegraph @

GUIWidgets/QtWidgets/QtUtilities.h

raw
1 /* Copyright (c) 2023, Dyssol Development Team.
2 * Copyright (c) 2023, DyssolTEC GmbH.
3 * All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */
4
5 #pragma once
6 #include <QApplication>
7 #include <QClipboard>
8 #include <QMessageBox>
9 #include "DyssolFilesystem.h"
10 #include <vector>
11 #include <locale>
12
13 /**
14 * Parses pasted text as a table of doubles.
15 * Each row contains at least one element.
16 */
17 inline std::vector<std::vector<double>> ParseClipboardAsDoubles()
18 {
19 QString regExp = "[\t; ]";
20
21 // parse pasted text
22 const QString text = QApplication::clipboard()->text();
23 // split by rows
24 QStringList rows = text.split(QRegExp(QLatin1String("\n")));
25 // remove trailing whitespaces
26 while (!rows.empty() && rows.back().size() == 0)
27 rows.pop_back();
28 std::vector<std::vector<double>> res(rows.length());
29 for (int irow = 0; irow < rows.length(); ++irow)
30 {
31 #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 9)
32 constexpr auto skipEmptyParts = Qt::SkipEmptyParts;
33 #else
34 constexpr auto skipEmptyParts = QString::SkipEmptyParts;
35 #endif
36 // split by columns
37 QStringList data = rows[irow].split(QRegExp(regExp), skipEmptyParts);
38 // remove trailing whitespaces
39 while (!data.empty() && data.back().size() == 0)
40 data.pop_back();
41 // parse data
42 for (int icol = 0; icol < data.length(); ++icol)
43 res[irow].push_back(data[icol].toDouble());
44 }
45 return res;
46 }
47
48 inline QMessageBox::StandardButton AskYesNoCancel(QWidget* _parent, const std::string& _title, const std::string& _text)
49 {
50 return QMessageBox::question(_parent, _title.c_str(), _text.c_str(), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
51 }
52
53 inline QMessageBox::StandardButton AskYesAllNoAllCancel(QWidget* _parent, const std::string& _title, const std::string& _text)
54 {
55 return QMessageBox::question(_parent, _title.c_str(), _text.c_str(), QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel);
56 }
57
58 inline QMessageBox::StandardButton Notify(QWidget* _parent, const std::string& _title, const std::string& _text)
59 {
60 return QMessageBox::information(_parent, _title.c_str(), _text.c_str(), QMessageBox::Ok);
61 }
62
63 inline std::filesystem::path QString2Path(const QString& _str)
64 {
65 #ifdef _WIN32
66 auto* ptr = reinterpret_cast<const wchar_t*>(_str.utf16());
67 return{ ptr, ptr + _str.size() };
68 #else
69 return{ _str.toStdString() };
70 #endif
71 }
72
73 inline QString Path2QString(const std::filesystem::path& _str)
74 {
75 #ifdef _WIN32
76 return QString::fromStdWString(_str.generic_wstring());
77 #else
78 return QString::fromStdString(_str.native());
79 #endif
80 }