src/main.h

Tue, 07 Jun 2022 01:37:26 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 07 Jun 2022 01:37:26 +0300
changeset 201
5d201ee4a9c3
parent 200
ca23936b455b
child 205
1a4342d80de7
permissions
-rw-r--r--

Continue giant refactor

/*
 *  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 <http://www.gnu.org/licenses/>.
 */

#pragma once
#include <QString>
#include <QVector>
#include <QSet>
#include <QDebug>
#include <compare>
#include <memory>
#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<std::size_t>(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<qreal>(point.x()), static_cast<qreal>(point.y())};
}

inline QPoint pointFToPoint(const QPointF& point)
{
	return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))};
}

inline QSizeF sizeToSizeF(const QSize& size)
{
	return {static_cast<qreal>(size.width()), static_cast<qreal>(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<typename T>
void reserveMore(std::vector<T>& 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<typename K, typename V>
struct KeyValuePair
{
	K key;
	V value;
};

template<typename K, typename V, typename IteratorType>
struct MapItemsIterator : IteratorType
{
	template<typename... Ts>
	MapItemsIterator(Ts&&... args) : IteratorType{args...} {}
	auto operator*() const
	{
		return KeyValuePair<const K&, V&>{this->key(), this->value()};
	}
};

template<typename K, typename V, typename MapType, typename IteratorType>
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<typename K, typename V>
auto items(const QMap<K, V>& map)
{
	return MapItems<
		const K&,
		const V&,
		const QMap<K, V>,
		MapItemsIterator<K, const V, typename QMap<K, V>::const_iterator>
	>{map};
}

template<typename K, typename V>
auto items(QMap<K, V>& map)
{
	return MapItems<
		const K&,
		V&,
		QMap<K, V>,
		MapItemsIterator<K, const V, typename QMap<K, V>::iterator>
	>{map};
}

template<typename T, typename IdentifierType>
struct TypeValue
{
	T value;
	bool operator==(TypeValue<T, IdentifierType> other) const
	{
		return value == other.value;
	}
	bool operator!=(TypeValue<T, IdentifierType> other) const
	{
		return value != other.value;
	}
	bool operator<(TypeValue<T, IdentifierType> other) const
	{
		return value < other.value;
	}
	bool operator>(TypeValue<T, IdentifierType> other) const
	{
		return value > other.value;
	}
	bool operator<=(TypeValue<T, IdentifierType> other) const
	{
		return value <= other.value;
	}
	bool operator>=(TypeValue<T, IdentifierType> other) const
	{
		return value >= other.value;
	}
};

template<typename T, typename R>
int qHash(TypeValue<T, R> value)
{
	return qHash(value.value);
}

/**
 * Iterates a @c glm::mat
 */
template<int X, int Y, typename T, glm::qualifier Q, typename Fn>
void iter_matrix(const glm::mat<X, Y, T, Q>& 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<int X, int Y, typename T, glm::qualifier Q>
QDataStream& operator<<(QDataStream& stream, const glm::mat<X, Y, T, Q>& mat)
{
	iter_matrix(mat, [&stream](int, int, float x)
	{
		stream << x;
	});
	return stream;
}

template<int X, int Y, typename T, glm::qualifier Q>
QDataStream& operator>>(QDataStream& stream, glm::mat<X, Y, T, Q>& mat)
{
	iter_matrix(mat, [&stream](int, int, float x)
	{
		stream >> x;
	});
	return stream;
}

template<std::size_t N, typename T>
std::array<T, N> vectorToArray(const std::vector<T>& x)
{
	std::array<T, N> result;
	for (std::size_t i = 0; i < x.size() and i < N; i += 1)
	{
		result[i] = x[i];
	}
	return result;
}

template<typename T>
std::optional<T> pointerToOptional(const T* p)
{
	std::optional<T> result;
	if (p != nullptr) {
		result = *p;
	}
	return result;
}

template<typename T, typename R>
void removeFromMap(std::map<T, R>& map, T&& key)
{
	const auto it = map.find(key);
	if (it != map.end()) {
		map.erase(it);
	}
}

// some magic code from https://en.cppreference.com/w/cpp/utility/variant/visit
// for use with std::visit
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

mercurial