Loading files
bae9c0e4eThis file is pinned to the selected release. Download the original to open it in your design tool.
/*
* LibrePCB - Professional EDA for everyone!
* Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
* https://librepcb.org/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBREPCB_EDITOR_SLINTHELPERS_H
#define LIBREPCB_EDITOR_SLINTHELPERS_H
/*******************************************************************************
* Includes
******************************************************************************/
#include <librepcb/core/attribute/attributekey.h>
#include <librepcb/core/library/cmp/componentprefix.h>
#include <librepcb/core/types/circuitidentifier.h>
#include <librepcb/core/types/elementname.h>
#include <librepcb/core/types/version.h>
#include <QtCore>
#include <QtGui>
#include <optional>
#include <slint.h>
/*******************************************************************************
* Namespace / Forward Declarations
******************************************************************************/
namespace librepcb {
namespace editor {
/*******************************************************************************
* Class SlintTranslator
******************************************************************************/
/**
* @brief Translator for Slint, using the Qt translation framework
*/
class SlintTranslator : public slint::Translator {
public:
slint::SharedString translate(std::string_view string,
std::string_view context) const override;
slint::SharedString ntranslate(uint64_t n, std::string_view singular,
std::string_view plural,
std::string_view context) const override;
private:
void setContext(std::string_view context) const noexcept;
void setString(std::string_view string) const noexcept;
slint::SharedString convertStringToSlint() const noexcept;
static QByteArray buildSlintPlaceholder(int i) noexcept;
static QByteArray buildQtPlaceholder(int i) noexcept;
// Buffers/caches to reduce the number of memory allocations.
mutable std::string mContextBuffer;
mutable QByteArray mStringBuffer;
mutable QByteArray mPlaceholderBuffer;
};
/*******************************************************************************
* Non-Member Functions
******************************************************************************/
// q2s(): Qt to Slint conversion (only built-in types, not LibrePCB specific)
// s2q(): Slint to Qt conversion (only built-in types, not LibrePCB specific)
slint::LogicalPosition q2s(const QPointF& p) noexcept;
QPointF s2q(const slint::LogicalPosition& p) noexcept;
slint::PhysicalPosition q2s(const QPoint& p) noexcept;
QPoint s2q(const slint::PhysicalPosition& p) noexcept;
slint::PhysicalSize q2s(const QSize& s) noexcept;
QSize s2q(const slint::PhysicalSize& s) noexcept;
slint::SharedString q2s(const QString& s) noexcept;
QString s2q(const slint::SharedString& s) noexcept;
bool operator==(const QString& s1, const slint::SharedString& s2) noexcept;
bool operator!=(const QString& s1, const slint::SharedString& s2) noexcept;
bool operator==(const slint::SharedString& s1, const QString& s2) noexcept;
bool operator!=(const slint::SharedString& s1, const QString& s2) noexcept;
std::shared_ptr<slint::VectorModel<slint::SharedString>> q2s(
const QStringList& s) noexcept;
QStringList s2q(const slint::Model<slint::SharedString>& s) noexcept;
slint::Image q2s(const QPixmap& p) noexcept;
slint::Image q2s(QImage img) noexcept;
QImage s2image(const slint::Image& img) noexcept;
slint::Color q2s(const QColor& c) noexcept;
slint::private_api::MouseCursorInner q2s(Qt::CursorShape s) noexcept;
Qt::MouseButton s2q(const slint::private_api::PointerEventButton& b) noexcept;
slint::private_api::KeyboardModifiers q2s(Qt::KeyboardModifiers m) noexcept;
Qt::KeyboardModifiers s2q(
const slint::private_api::KeyboardModifiers& m) noexcept;
slint::SharedString q2s(Qt::Key k) noexcept;
Qt::Key s2key(const slint::SharedString& k) noexcept;
// Bind property without type conversion
template <typename TTarget, typename TClass, typename T>
inline void bind(QObject* context, const TTarget& target,
void (TTarget::*setter)(const T&) const, TClass* source,
void (TClass::*signal)(T), const T& defaultValue) noexcept {
QObject::connect(source, signal, context, [&target, setter](const T& value) {
(target.*setter)(value);
});
(target.*setter)(defaultValue);
}
// Bind property with type conversion
template <typename TTarget, typename TSlint, typename TClass, typename TQt>
inline void bind(
QObject* context, const TTarget& target,
void (TTarget::*setter)(const TSlint&) const, TClass* source,
void (TClass::*signal)(TQt), const TQt& defaultValue,
std::function<TSlint(const TQt&)> convert = [](const TQt& value) {
return q2s(value);
}) noexcept {
QObject::connect(source, signal, context,
[&target, setter, convert](const TQt& value) {
(target.*setter)(convert(value));
});
(target.*setter)(convert(defaultValue));
}
std::optional<ElementName> validateElementName(
const QString& input, slint::SharedString& error) noexcept;
std::optional<Version> validateVersion(const QString& input,
slint::SharedString& error) noexcept;
std::optional<QString> validateFileName(
const QString& input, slint::SharedString& error,
FilePath::CleanFileNameOptions options, int maxLength,
const QString& requiredSuffix = QString()) noexcept;
std::optional<AttributeKey> validateAttributeKey(const QString& input,
slint::SharedString& error,
bool isDuplicate) noexcept;
std::optional<CircuitIdentifier> validateCircuitIdentifier(
const QString& input, slint::SharedString& error,
bool isDuplicate) noexcept;
std::optional<QUrl> validateUrl(const QString& input,
slint::SharedString& error,
bool allowEmpty = false) noexcept;
std::optional<ComponentPrefix> validateComponentPrefix(
const QString& input, slint::SharedString& error) noexcept;
void validateComponentDefaultValue(const QString& input,
slint::SharedString& error) noexcept;
/*******************************************************************************
* End of File
******************************************************************************/
} // namespace editor
} // namespace librepcb
#endif