LibrePCB Developers Documentation
Loading...
Searching...
No Matches
slinthelpers.h
Go to the documentation of this file.
1/*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBREPCB_EDITOR_SLINTHELPERS_H
21#define LIBREPCB_EDITOR_SLINTHELPERS_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
31
32#include <QtCore>
33#include <QtGui>
34
35#include <optional>
36#include <slint.h>
37
38/*******************************************************************************
39 * Namespace / Forward Declarations
40 ******************************************************************************/
41namespace librepcb {
42namespace editor {
43
44// q2s(): Qt to Slint conversion (only built-in types, not LibrePCB specific)
45// s2q(): Slint to Qt conversion (only built-in types, not LibrePCB specific)
46
47/*******************************************************************************
48 * Non-Member Functions
49 ******************************************************************************/
50
51slint::LogicalPosition q2s(const QPointF& p) noexcept;
52QPointF s2q(const slint::LogicalPosition& p) noexcept;
53
54slint::PhysicalPosition q2s(const QPoint& p) noexcept;
55QPoint s2q(const slint::PhysicalPosition& p) noexcept;
56
57slint::PhysicalSize q2s(const QSize& s) noexcept;
58QSize s2q(const slint::PhysicalSize& s) noexcept;
59
60slint::SharedString q2s(const QString& s) noexcept;
61QString s2q(const slint::SharedString& s) noexcept;
62bool operator==(const QString& s1, const slint::SharedString& s2) noexcept;
63bool operator!=(const QString& s1, const slint::SharedString& s2) noexcept;
64bool operator==(const slint::SharedString& s1, const QString& s2) noexcept;
65bool operator!=(const slint::SharedString& s1, const QString& s2) noexcept;
66
67std::shared_ptr<slint::VectorModel<slint::SharedString>> q2s(
68 const QStringList& s) noexcept;
69QStringList s2q(const slint::Model<slint::SharedString>& s) noexcept;
70
71slint::Image q2s(const QPixmap& p) noexcept;
72
73slint::Color q2s(const QColor& c) noexcept;
74
75slint::private_api::MouseCursor q2s(Qt::CursorShape s) noexcept;
76
77Qt::MouseButton s2q(const slint::private_api::PointerEventButton& b) noexcept;
78
79slint::private_api::KeyboardModifiers q2s(Qt::KeyboardModifiers m) noexcept;
80Qt::KeyboardModifiers s2q(
81 const slint::private_api::KeyboardModifiers& m) noexcept;
82
83slint::SharedString q2s(Qt::Key k) noexcept;
84Qt::Key s2key(const slint::SharedString& k) noexcept;
85
86// Bind property without type conversion
87template <typename TTarget, typename TClass, typename T>
88inline void bind(QObject* context, const TTarget& target,
89 void (TTarget::*setter)(const T&) const, TClass* source,
90 void (TClass::*signal)(T), const T& defaultValue) noexcept {
91 QObject::connect(source, signal, context, [&target, setter](const T& value) {
92 (target.*setter)(value);
93 });
94 (target.*setter)(defaultValue);
95}
96
97// Bind property with type conversion
98template <typename TTarget, typename TSlint, typename TClass, typename TQt>
99inline void bind(
100 QObject* context, const TTarget& target,
101 void (TTarget::*setter)(const TSlint&) const, TClass* source,
102 void (TClass::*signal)(TQt), const TQt& defaultValue,
103 std::function<TSlint(const TQt&)> convert = [](const TQt& value) {
104 return q2s(value);
105 }) noexcept {
106 QObject::connect(source, signal, context,
107 [&target, setter, convert](const TQt& value) {
108 (target.*setter)(convert(value));
109 });
110 (target.*setter)(convert(defaultValue));
111}
112
113std::optional<ElementName> validateElementName(
114 const QString& input, slint::SharedString& error) noexcept;
115
116std::optional<Version> validateVersion(const QString& input,
117 slint::SharedString& error) noexcept;
118
119std::optional<QString> validateFileName(
120 const QString& input, slint::SharedString& error,
121 FilePath::CleanFileNameOptions options, int maxLength,
122 const QString& requiredSuffix = QString()) noexcept;
123
124std::optional<AttributeKey> validateAttributeKey(const QString& input,
125 slint::SharedString& error,
126 bool isDuplicate) noexcept;
127
128std::optional<CircuitIdentifier> validateCircuitIdentifier(
129 const QString& input, slint::SharedString& error,
130 bool isDuplicate) noexcept;
131
132std::optional<QUrl> validateUrl(const QString& input,
133 slint::SharedString& error,
134 bool allowEmpty = false) noexcept;
135
136std::optional<ComponentPrefix> validateComponentPrefix(
137 const QString& input, slint::SharedString& error) noexcept;
138
139void validateComponentDefaultValue(const QString& input,
140 slint::SharedString& error) noexcept;
141
142/*******************************************************************************
143 * End of File
144 ******************************************************************************/
145
146} // namespace editor
147} // namespace librepcb
148
149#endif
std::optional< Version > validateVersion(const QString &input, slint::SharedString &error) noexcept
Definition slinthelpers.cpp:427
std::optional< QString > validateFileName(const QString &input, slint::SharedString &error, FilePath::CleanFileNameOptions options, int maxLength, const QString &requiredSuffix) noexcept
Definition slinthelpers.cpp:438
QPointF s2q(const slint::LogicalPosition &p) noexcept
Definition slinthelpers.cpp:43
Qt::Key s2key(const slint::SharedString &k) noexcept
Definition slinthelpers.cpp:290
slint::LogicalPosition q2s(const QPointF &p) noexcept
Definition slinthelpers.cpp:38
bool operator==(const QString &s1, const slint::SharedString &s2) noexcept
Definition slinthelpers.cpp:92
void bind(QObject *context, const TTarget &target, void(TTarget::*setter)(const T &) const, TClass *source, void(TClass::*signal)(T), const T &defaultValue) noexcept
Definition slinthelpers.h:88
std::optional< ElementName > validateElementName(const QString &input, slint::SharedString &error) noexcept
Definition slinthelpers.cpp:416
bool operator!=(const QString &s1, const slint::SharedString &s2) noexcept
Definition slinthelpers.cpp:96
Definition occmodel.cpp:77
type_safe::constrained_type< QString, AttributeKeyConstraint, AttributeKeyVerifier > AttributeKey
Definition attributekey.h:78
type_safe::constrained_type< QString, CircuitIdentifierConstraint, CircuitIdentifierVerifier > CircuitIdentifier
Definition circuitidentifier.h:88
type_safe::constrained_type< QString, ComponentPrefixConstraint, ComponentPrefixVerifier > ComponentPrefix
Definition componentprefix.h:81
Definition uuid.h:186