diff -r 1a4342d80de7 -r 654661eab7f3 src/main.h --- a/src/main.h Wed Jun 08 20:41:21 2022 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,274 +0,0 @@ -/* - * LDForge: LDraw parts authoring CAD - * Copyright (C) 2013 - 2020 Teemu Piippo - * - * 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 . - */ - -#pragma once -#include -#include -#include -#include -#include -#include -#include "basics.h" -#include "utility.h" -#include "geometry.h" -#include "functional.h" - -namespace settingGroups -{ - // List of setting groups - constexpr char mainwindow[] = "mainwindow"; -} - -constexpr std::size_t operator""_z(const unsigned long long int x) -{ - return static_cast(x); -} - -inline QString operator""_q(const char* string, const unsigned long int length) -{ - Q_UNUSED(length) - return QString{string}; -} - -inline QPointF pointToPointF(const QPoint& point) -{ - return {static_cast(point.x()), static_cast(point.y())}; -} - -inline QPoint pointFToPoint(const QPointF& point) -{ - return {static_cast(std::round(point.x())), static_cast(std::round(point.y()))}; -} - -inline QSizeF sizeToSizeF(const QSize& size) -{ - return {static_cast(size.width()), static_cast(size.height())}; -} - -/** - * \brief Hints to the specified vector that a certain amount of new elements are going to be added. - * \param vector vector to consider - * \param amount amount of new elements to expect - */ -template -void reserveMore(std::vector& vector, std::size_t amount) -{ - vector.reserve(vector.size() + amount); -} - -inline QString vectorToString(const glm::vec2& vec) -{ - return "(%1, %2)"_q - .arg(toDouble(vec.x)) - .arg(toDouble(vec.y)); -} - -inline QString vectorToString(const glm::vec3& vec) -{ - return "(%1, %2, %3)"_q - .arg(toDouble(vec.x)) - .arg(toDouble(vec.y)) - .arg(toDouble(vec.z)); -} - -inline QString vectorToString(const glm::vec4& vec) -{ - return "(%1, %2, %3, %4)"_q - .arg(toDouble(vec.x)) - .arg(toDouble(vec.y)) - .arg(toDouble(vec.z)) - .arg(toDouble(vec.w)); -} - -template -struct KeyValuePair -{ - K key; - V value; -}; - -template -struct MapItemsIterator : IteratorType -{ - template - MapItemsIterator(Ts&&... args) : IteratorType{args...} {} - auto operator*() const - { - return KeyValuePair{this->key(), this->value()}; - } -}; - -template -struct MapItems -{ - MapType& map; - IteratorType begin() - { - return IteratorType(this->map.begin()); - } - - IteratorType end() - { - return IteratorType(this->map.end()); - } -}; - -/* - * Python's dict.items for QMap: use in a for loop to iterate a map to - * get both keys and values. Iteration yields KeyValuePairs. - */ -template -auto items(const QMap& map) -{ - return MapItems< - const K&, - const V&, - const QMap, - MapItemsIterator::const_iterator> - >{map}; -} - -template -auto items(QMap& map) -{ - return MapItems< - const K&, - V&, - QMap, - MapItemsIterator::iterator> - >{map}; -} - -template -struct TypeValue -{ - T value; - bool operator==(TypeValue other) const - { - return value == other.value; - } - bool operator!=(TypeValue other) const - { - return value != other.value; - } - bool operator<(TypeValue other) const - { - return value < other.value; - } - bool operator>(TypeValue other) const - { - return value > other.value; - } - bool operator<=(TypeValue other) const - { - return value <= other.value; - } - bool operator>=(TypeValue other) const - { - return value >= other.value; - } -}; - -template -int qHash(TypeValue value) -{ - return qHash(value.value); -} - -/** - * Iterates a @c glm::mat - */ -template -void iter_matrix(const glm::mat& matrix, Fn&& fn) -{ - for (int i = 0; i < X; ++i) - { - for (int j = 0; j < Y; ++j) - { - fn(i, j, matrix[i][j]); - } - } -} - -inline QDataStream& operator<<(QDataStream& stream, const glm::vec3& vec) -{ - return stream << vec.x << vec.y << vec.z; -} - -inline QDataStream& operator>>(QDataStream& stream, glm::vec3& vec) -{ - return stream >> vec.x >> vec.y >> vec.z; -} - -template -QDataStream& operator<<(QDataStream& stream, const glm::mat& mat) -{ - iter_matrix(mat, [&stream](int, int, float x) - { - stream << x; - }); - return stream; -} - -template -QDataStream& operator>>(QDataStream& stream, glm::mat& mat) -{ - iter_matrix(mat, [&stream](int, int, float x) - { - stream >> x; - }); - return stream; -} - -template -std::array vectorToArray(const std::vector& x) -{ - std::array result; - for (std::size_t i = 0; i < x.size() and i < N; i += 1) - { - result[i] = x[i]; - } - return result; -} - -template -std::optional pointerToOptional(const T* p) -{ - std::optional result; - if (p != nullptr) { - result = *p; - } - return result; -} - -template -void removeFromMap(std::map& map, T&& key) -{ - const auto it = map.find(key); - if (it != map.end()) { - map.erase(it); - } -} - -template -using opt = std::optional; - -// some magic code from https://en.cppreference.com/w/cpp/utility/variant/visit -// for use with std::visit -template struct overloaded : Ts... { using Ts::operator()...; }; -template overloaded(Ts...) -> overloaded;