LMMS
Loading...
Searching...
No Matches
DeprecationHelper.h
Go to the documentation of this file.
1/*
2 * DeprecationHelper.h - This file contains the declarations of helper functions
3 * which helps centralize the #ifdefs preprocessors regarding deprecation based on Qt versions.
4 * The functions are defined differently based on the callers' Qt versions.
5 *
6 * Copyright (c) 2020 Tien Dat Nguyen <ntd.bk.k56/at/gmail.com>
7 *
8 * This file is part of LMMS - https://lmms.io
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program (see COPYING); if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301 USA.
24 *
25 */
26
27#ifndef LMMS_DEPRECATIONHELPER_H
28#define LMMS_DEPRECATIONHELPER_H
29
30#include <type_traits>
31
32#include <QDomDocument>
33#include <QFontMetrics>
34#include <QKeySequence>
35#include <QVariant>
36#include <QWheelEvent>
37
38namespace lmms
39{
40
47inline QPoint position(const QDropEvent* de)
48{
49#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
50 return de->position().toPoint();
51#else
52 return de->pos();
53#endif
54}
55
62inline QPoint position(const QMouseEvent* me)
63{
64#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
65 return me->position().toPoint();
66#else
67 return me->pos();
68#endif
69}
70
77inline QPointF positionF(const QMouseEvent* me)
78{
79#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
80 return me->position();
81#else
82 return me->localPos();
83#endif
84}
85
92inline QPoint globalPosition(const QMouseEvent* me)
93{
94#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
95 return me->globalPosition().toPoint();
96#else
97 return me->globalPos();
98#endif
99}
100
101namespace detail
102{
103
104template<typename T>
105inline constexpr bool IsKeyOrModifier = std::is_same_v<T, Qt::Key>
106 || std::is_same_v<T, Qt::Modifier> || std::is_same_v<T, Qt::KeyboardModifier>;
107
108} // namespace detail
109
117template<typename... Args> requires (detail::IsKeyOrModifier<Args> && ...)
118inline QKeySequence keySequence(Args... args)
119{
120 return (0 | ... | static_cast<int>(args));
121}
122
129inline QMetaType::Type typeId(const QVariant& variant)
130{
131#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
132 return static_cast<QMetaType::Type>(variant.typeId());
133#else
134 return static_cast<QMetaType::Type>(variant.type());
135#endif
136}
137
139inline bool setContent(QDomDocument& doc, const QByteArray& text,
140 QString* errorMsg = nullptr, int* errorLine = nullptr, int* errorColumn = nullptr)
141{
142#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
143 auto result = doc.setContent(text, QDomDocument::ParseOption::Default);
144 if (result) { return true; }
145 if (errorMsg) { *errorMsg = std::move(result.errorMessage); }
146 if (errorLine) { *errorLine = static_cast<int>(result.errorLine); }
147 if (errorColumn) { *errorColumn = static_cast<int>(result.errorColumn); }
148 return false;
149#else
150 return doc.setContent(text, errorMsg, errorLine, errorColumn);
151#endif
152}
153
155inline bool setContent(QDomDocument& doc, QIODevice* dev, bool namespaceProcessing,
156 QString* errorMsg = nullptr, int* errorLine = nullptr, int* errorColumn = nullptr)
157{
158#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
159 const auto options = namespaceProcessing
160 ? QDomDocument::ParseOption::UseNamespaceProcessing
161 : QDomDocument::ParseOption::Default;
162 auto result = doc.setContent(dev, options);
163 if (result) { return true; }
164 if (errorMsg) { *errorMsg = std::move(result.errorMessage); }
165 if (errorLine) { *errorLine = static_cast<int>(result.errorLine); }
166 if (errorColumn) { *errorColumn = static_cast<int>(result.errorColumn); }
167 return false;
168#else
169 return doc.setContent(dev, namespaceProcessing, errorMsg, errorLine, errorColumn);
170#endif
171}
172
173} // namespace lmms
174
175#endif // LMMS_DEPRECATIONHELPER_H
constexpr bool IsKeyOrModifier
Definition DeprecationHelper.h:105
Definition AudioAlsa.cpp:35
QPointF positionF(const QMouseEvent *me)
positionF is a backwards-compatible adapter for QMouseEvent::position and localPos functions.
Definition DeprecationHelper.h:77
QPoint position(const QDropEvent *de)
position is a backwards-compatible adapter for QDropEvent::position and pos functions.
Definition DeprecationHelper.h:47
QKeySequence keySequence(Args... args)
Combines Qt key and modifier arguments together, replacing A | B which was deprecated in C++20 due to...
Definition DeprecationHelper.h:118
QMetaType::Type typeId(const QVariant &variant)
typeId is a backwards-compatible adapter for QVariant::typeId and type functions.
Definition DeprecationHelper.h:129
bool setContent(QDomDocument &doc, const QByteArray &text, QString *errorMsg=nullptr, int *errorLine=nullptr, int *errorColumn=nullptr)
Backwards-compatible adapter for QDomDocument::setContent.
Definition DeprecationHelper.h:139
QPoint globalPosition(const QMouseEvent *me)
globalPosition is a backwards-compatible adapter for QMouseEvent::globalPosition and globalPos functi...
Definition DeprecationHelper.h:92
const char * text
Definition swell-functions.h:167
int result
Definition process.c:1455