~adamreichold/qpdfview/trunk

2117 by Adam Reichold
First stab at making the application compatible with Qt versions 4, 5 and 6.
1
/*
2
3
Copyright 2021 Adam Reichold
4
5
This file is part of qpdfview.
6
7
qpdfview is free software: you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation, either version 2 of the License, or
10
(at your option) any later version.
11
12
qpdfview is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.
19
20
*/
21
22
#ifndef COMPATIBILITY_H
23
#define COMPATIBILITY_H
24
25
#include <QWheelEvent>
26
#include <QPrinter>
27
#include <QProcess>
28
29
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
30
31
#include <QRegularExpression>
32
33
#else
34
35
#include <QRegExp>
36
37
#endif // QT_VERSION
38
39
namespace qpdfview
40
{
41
42
inline bool rotatedForward(const QWheelEvent* event)
43
{
44
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
45
46
    return event->angleDelta().y() > 0;
47
48
#else
49
50
    return event->delta() > 0;
51
52
#endif // QT_VERSION
53
}
54
55
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
56
57
typedef QRegularExpression RegularExpression;
58
59
class Match
60
{
61
    QRegularExpressionMatch match;
62
63
public:
64
    Match(RegularExpression& expr, const QString& text) :
65
        match(expr.match(text))
66
    {
67
    }
68
69
    operator bool() const
70
    {
71
        return match.hasMatch();
72
    }
73
74
    QString captured(int nth) const
75
    {
76
        return match.captured(nth);
77
    }
78
79
};
80
81
#else
82
83
typedef QRegExp RegularExpression;
84
85
class Match
86
{
87
    QRegExp* expr;
88
89
public:
90
    Match(QRegExp& expr, const QString& text) :
91
        expr(expr.indexIn(text) != -1 ? &expr : 0)
92
    {
93
    }
94
95
    operator bool() const
96
    {
97
        return expr != 0;
98
    }
99
100
    QString captured(int nth) const
101
    {
102
        return expr->cap(nth);
103
    }
104
105
};
106
107
#endif // QT_VERSION
108
109
#if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
110
111
typedef Qt::SplitBehaviorFlags SplitBehavior;
2119 by Adam Reichold
Fix enum compatibility handling to support older GCC versions.
112
namespace SplitBehaviorValues = Qt;
2117 by Adam Reichold
First stab at making the application compatible with Qt versions 4, 5 and 6.
113
114
#else
115
116
typedef QString::SplitBehavior SplitBehavior;
2119 by Adam Reichold
Fix enum compatibility handling to support older GCC versions.
117
typedef QString SplitBehaviorValues;
2117 by Adam Reichold
First stab at making the application compatible with Qt versions 4, 5 and 6.
118
119
#endif // QT_VERSION
120
121
#if QT_VERSION >= QT_VERSION_CHECK(5,3,0)
122
123
typedef QPageLayout::Orientation PageOrientation;
2119 by Adam Reichold
Fix enum compatibility handling to support older GCC versions.
124
typedef QPageLayout PageOrientationValues;
2117 by Adam Reichold
First stab at making the application compatible with Qt versions 4, 5 and 6.
125
126
#else
127
128
typedef QPrinter::Orientation PageOrientation;
2119 by Adam Reichold
Fix enum compatibility handling to support older GCC versions.
129
typedef QPrinter PageOrientationValues;
2117 by Adam Reichold
First stab at making the application compatible with Qt versions 4, 5 and 6.
130
131
#endif // QT_VERSION
132
133
template< typename Iterator, typename Value >
134
Iterator binarySearch(Iterator first, Iterator last, const Value& value)
135
{
136
    first = std::lower_bound(first, last, value);
137
138
    return first == last || value < *first ? last : first;
139
}
140
141
inline bool startDetached(QString command)
142
{
143
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
144
145
        QStringList arguments = QProcess::splitCommand(command);
146
        QString program = arguments.takeFirst();
147
148
        return QProcess::startDetached(program, arguments);
149
150
#else
151
152
        return QProcess::startDetached(command);
153
154
#endif // QT_VERSION
155
}
156
157
} // qpdfview
158
159
#endif // MISCELLANEOUS_H