Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
format_qt.h
1// SPDX-FileCopyrightText: © 2024-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "utils/qt.h"
7
8#include <QObject>
9#include <QString>
10#include <QUuid>
11#include <QVariant>
12
13#include <fmt/format.h>
14
15// Formatter for QPointer
16template <typename T>
17struct fmt::formatter<QPointer<T>> : fmt::formatter<std::string_view>
18{
19 auto format (const QPointer<T> &opt, fmt::format_context &ctx) const
20 -> format_context::iterator
21 {
22 if (!opt.isNull ())
23 {
24 return fmt::formatter<std::string_view>::format (
25 fmt::format ("{}", *opt), ctx);
26 }
27 return fmt::formatter<std::string_view>::format ("(null)", ctx);
28 }
29};
30
31// Formatter for utils::QObjectUniquePtr
32template <typename T>
33struct fmt::formatter<zrythm::utils::QObjectUniquePtr<T>>
34 : fmt::formatter<std::string_view>
35{
36 auto format (
38 fmt::format_context &ctx) const -> format_context::iterator
39 {
40 if (opt)
41 {
42 return fmt::formatter<std::string_view>::format (
43 fmt::format ("{}", *opt), ctx);
44 }
45 return fmt::formatter<std::string_view>::format ("(null)", ctx);
46 }
47};
48
49// Formatter for QString
50template <> struct fmt::formatter<QString> : fmt::formatter<std::string_view>
51{
52 auto format (const QString &s, fmt::format_context &ctx) const
53 -> format_context::iterator
54 {
55 return fmt::formatter<std::string_view>::format (s.toUtf8 (), ctx);
56 }
57};
58static_assert (fmt::formattable<QString>);
59
60// Formatter for QStringList
61template <>
62struct fmt::formatter<QStringList> : fmt::formatter<std::string_view>
63{
64 auto format (const QStringList &s, fmt::format_context &ctx) const
65 -> format_context::iterator
66 {
67 return fmt::formatter<std::string_view>::format (
68 s.join (QString::fromUtf8 (", ")).toUtf8 (), ctx);
69 }
70};
71static_assert (fmt::formattable<QStringList>);
72
73template <typename... Args>
74QString
75format_qstr (const QString &format, Args &&... args)
76{
77 return QString::fromUtf8 (
78 fmt::vformat (
79 std::string_view (format.toUtf8 ()), fmt::make_format_args (args...)));
80}
81
82// Formatter for QVariant
83template <> struct fmt::formatter<QVariant> : fmt::formatter<std::string_view>
84{
85 auto format (const QVariant &v, fmt::format_context &ctx) const
86 -> format_context::iterator
87 {
88 return fmt::formatter<std::string_view>::format (
89 v.toString ().toUtf8 (), ctx);
90 }
91};
92static_assert (fmt::formattable<QVariant>);
93
94// Formatter for QUuid
95inline auto
96format_as (const QUuid &uuid)
97{
98 return uuid.toString (QUuid::WithoutBraces);
99}
100static_assert (fmt::formattable<QUuid>);
A unique pointer for QObject objects that also works with QObject-based ownership.
Definition qt.h:36