lmms
Cross-platform music production software
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
48inline QPoint position(const QDropEvent* de)
49{
50#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
51 return de->position().toPoint();
52#else
53 return de->pos();
54#endif
55}
56
65inline QPoint position(const QMouseEvent* me)
66{
67#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
68 return me->position().toPoint();
69#else
70 return me->pos();
71#endif
72}
73
82inline QPointF positionF(const QMouseEvent* me)
83{
84#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
85 return me->position();
86#else
87 return me->localPos();
88#endif
89}
90
99inline QPoint globalPosition(const QMouseEvent* me)
100{
101#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
102 return me->globalPosition().toPoint();
103#else
104 return me->globalPos();
105#endif
106}
107
108namespace detail
109{
110
111template<typename T>
112inline constexpr bool IsKeyOrModifier = std::is_same_v<T, Qt::Key>
113 || std::is_same_v<T, Qt::Modifier> || std::is_same_v<T, Qt::KeyboardModifier>;
114
115} // namespace detail
116
126template<typename... Args> requires (detail::IsKeyOrModifier<Args> && ...)
127inline QKeySequence keySequence(Args... args)
128{
129 return (0 | ... | static_cast<int>(args));
130}
131
138inline QMetaType::Type typeId(const QVariant& variant)
139{
140#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
141 return static_cast<QMetaType::Type>(variant.typeId());
142#else
143 return static_cast<QMetaType::Type>(variant.type());
144#endif
145}
146
151inline bool setContent(QDomDocument& doc, const QByteArray& text,
152 QString* errorMsg = nullptr, int* errorLine = nullptr, int* errorColumn = nullptr)
153{
154#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
155 auto result = doc.setContent(text, QDomDocument::ParseOption::Default);
156 if (result) { return true; }
157 if (errorMsg) { *errorMsg = std::move(result.errorMessage); }
158 if (errorLine) { *errorLine = static_cast<int>(result.errorLine); }
159 if (errorColumn) { *errorColumn = static_cast<int>(result.errorColumn); }
160 return false;
161#else
162 return doc.setContent(text, errorMsg, errorLine, errorColumn);
163#endif
164}
165
170inline bool setContent(QDomDocument& doc, QIODevice* dev, bool namespaceProcessing,
171 QString* errorMsg = nullptr, int* errorLine = nullptr, int* errorColumn = nullptr)
172{
173#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
174 const auto options = namespaceProcessing
175 ? QDomDocument::ParseOption::UseNamespaceProcessing
176 : QDomDocument::ParseOption::Default;
177 auto result = doc.setContent(dev, options);
178 if (result) { return true; }
179 if (errorMsg) { *errorMsg = std::move(result.errorMessage); }
180 if (errorLine) { *errorLine = static_cast<int>(result.errorLine); }
181 if (errorColumn) { *errorColumn = static_cast<int>(result.errorColumn); }
182 return false;
183#else
184 return doc.setContent(dev, namespaceProcessing, errorMsg, errorLine, errorColumn);
185#endif
186}
187
188} // namespace lmms
189
190#endif // LMMS_DEPRECATIONHELPER_H
constexpr bool IsKeyOrModifier
Definition DeprecationHelper.h:112
Definition RemotePluginBase.cpp:34
QPointF positionF(const QMouseEvent *me)
Backwards-compatible adapter for QMouseEvent's new position() to the old localPos() method.
Definition DeprecationHelper.h:82
QPoint position(const QDropEvent *de)
Backwards-compatible adapter for QDropEvent's new position() to the old pos() method.
Definition DeprecationHelper.h:48
QMetaType::Type typeId(const QVariant &variant)
Backwards-compatible adapter for QVariant's new typeId() to the old type() method.
Definition DeprecationHelper.h:138
bool setContent(QDomDocument &doc, const QByteArray &text, QString *errorMsg=nullptr, int *errorLine=nullptr, int *errorColumn=nullptr)
Backwards-compatible adapter for QDomDocument::setContent() for QByteArray input.
Definition DeprecationHelper.h:151
QPoint globalPosition(const QMouseEvent *me)
Backwards-compatible adapter for QMouseEvent's new globalPosition() to the old globalPos() method.
Definition DeprecationHelper.h:99
QKeySequence keySequence(Args... args)
Combines Qt key and modifier arguments together.
Definition DeprecationHelper.h:127