summaryrefslogtreecommitdiff
path: root/qt_helpers.hpp
blob: 83ae124306b7147d65771f09cc47e79955ad4aa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#ifndef QT_HELPERS_HPP_
#define QT_HELPERS_HPP_

#include <stdexcept>
#include <ostream>

#include <QString>
#include <QChar>
#include <QMetaObject>
#include <QHostAddress>
#include <QDataStream>
#include <QMetaType>
#include <QMetaEnum>

#pragma once
#include <QtGlobal>
#include <QOperatingSystemVersion>

class QDir;

// ----------------------
// Windows version macros
// ----------------------
#define IS_WINDOWS_VISTA_OR_LATER \
    (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::Windows, 6))

#define IS_WINDOWS_7_OR_LATER \
    (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::Windows, 6, 1))

#define IS_WINDOWS_10_OR_LATER \
    (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::Windows, 10))

// Windows 11 is identified by build number >= 22000
#define IS_WINDOWS_11_OR_LATER \
    (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::Windows, 10, 0, 22000))

// ----------------------
// macOS version macros
// ----------------------
#define IS_MACOS_CATALINA_OR_LATER \
    (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 10, 15))

#define IS_MACOS_BIGSUR_OR_LATER \
    (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 11))

#define IS_MACOS_MONTEREY_OR_LATER \
    (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 12))

#define IS_MACOS_VENTURA_OR_LATER \
    (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 13))

#define IS_MACOS_SONOMA_OR_LATER \
    (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 14))

#define IS_MACOS_SEQUOIA_OR_LATER \
    (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 15))

// macOS Tahoe (officially version 26, released Sept 15, 2025)
#define IS_MACOS_TAHOE_OR_LATER \
    (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 26))

#define ENUM_QDATASTREAM_OPS_DECL(CLASS, ENUM)				\
  QDataStream& operator << (QDataStream&, CLASS::ENUM const&);			\
  QDataStream& operator >> (QDataStream&, CLASS::ENUM&);

#define ENUM_QDATASTREAM_OPS_IMPL(CLASS, ENUM)				\
  QDataStream& operator << (QDataStream& os, CLASS::ENUM const& v)		\
  {									\
    auto const& mo = CLASS::staticMetaObject;				\
    return os << mo.enumerator (mo.indexOfEnumerator (#ENUM)).valueToKey (static_cast<int> (v)); \
  }									\
									\
  QDataStream& operator >> (QDataStream& is, CLASS::ENUM& v)		\
  {									\
    char * buffer;							\
    is >> buffer;							\
    bool ok {false};							\
    auto const& mo = CLASS::staticMetaObject;				\
    auto const& me = mo.enumerator (mo.indexOfEnumerator (#ENUM)); \
    if (buffer)								\
      {									\
        v = static_cast<CLASS::ENUM> (me.keyToValue (buffer, &ok));	\
        delete [] buffer;                                           \
      }									\
    if (!ok)								\
      {									\
        v = static_cast<CLASS::ENUM> (me.value (0));  \
      }									\
    return is;								\
  }

#define ENUM_CONVERSION_OPS_DECL(CLASS, ENUM)	\
  QString enum_to_qstring (CLASS::ENUM const&);

#define ENUM_CONVERSION_OPS_IMPL(CLASS, ENUM)				\
  QString enum_to_qstring (CLASS::ENUM const& m)				\
  {									\
    auto const& mo = CLASS::staticMetaObject;				\
    return QString {mo.enumerator (mo.indexOfEnumerator (#ENUM)).valueToKey (static_cast<int> (m))}; \
  }

#if QT_VERSION >= QT_VERSION_CHECK (5, 15, 0)
Qt::SplitBehaviorFlags const SkipEmptyParts = Qt::SkipEmptyParts;
#else
QString::SplitBehavior const SkipEmptyParts = QString::SkipEmptyParts;
#endif

inline
std::ostream& operator << (std::ostream& os, QByteArray const& b)
{
  return os << b.constData ();
}

inline
std::ostream& operator << (std::ostream& os, QString const& s)
{
  return os << s.toStdString ();
}

inline
void throw_qstring (QString const& qs)
{
  throw std::runtime_error {qs.toLocal8Bit ().constData ()};
}

QString font_as_stylesheet (QFont const&);

// Compare port-name prefixes lexically and trailing decimal numbers numerically.
bool serial_port_name_less (QString const& lhs, QString const& rhs);

QString application_style_sheet (QString const& base_style_sheet,
                                 QString const& dark_style_sheet,
                                 bool dark_style,
                                 QFont const& font);

// do what is necessary to change a dynamic property and trigger any
// conditional style sheet updates
void update_dynamic_property (QWidget *, char const * property, QVariant const& value);

// Invalid input, a nonpositive interval, or an unrepresentable result returns an invalid QDateTime.
// Round to the nearest interval, resolving midpoint ties toward the later interval.
QDateTime qt_round_date_time_to (QDateTime dt, int milliseconds);

// Invalid input, a nonpositive interval, or an unrepresentable result returns an invalid QDateTime.
// Truncate to the earlier interval.
QDateTime qt_truncate_date_time_to (QDateTime dt, int milliseconds);

QDir resolve_installed_data_directory (QString const& application_directory,
                                       QString const& configured_data_destination,
                                       QString const& relative_application_root);
QDir installed_data_directory ();
QDir preferred_sounds_directory (QDir const& canonical_directory, QDir const& legacy_directory);
QDir app_sounds_directory ();
QString voice_manifest_path (QDir const& canonical_directory, QDir const& legacy_directory);
QString app_voice_manifest_path ();
QDir sounds_subdirectory (QDir const& root, QString const& subdirectory);
bool app_sounds_subdirectory_is_safe (QString const& subdirectory);
bool parse_app_voice_entry (QString const& record, QString& subdirectory, QString& display_name);

// Empty lists have no valid index; -1 advances to the first item.
int next_cyclic_index (int current_index, int item_count);

QString writable_file_path (QDir const& writable_dir, QString const& file_name);
QString writable_override_or_installed_file_path (QDir const& writable_dir, QDir const& installed_dir,
                                                  QString const& file_name);
bool ensure_parent_directory (QString const& file_path);

template <class T>
class VPtr
{
public:
  static T * asPtr (QVariant v)
  {
    return reinterpret_cast<T *> (v.value<void *> ());
  }

  static QVariant asQVariant(T * ptr)
  {
    return QVariant::fromValue (reinterpret_cast<void *> (ptr));
  }
};

#if QT_VERSION < QT_VERSION_CHECK (5, 14, 0)
// The Qt  devs "fixed" this  in 5.14 to  specialize to use  their own
// qHash(), it doesn't  fix the problem we were  addressing as qHash()
// returns a  uint so is  still a  poorly distributed 32-bit  value on
// 64-bit platforms, but  we can't specialize ourselves  as Qt already
// has - sigh.
namespace std
{
  // std::hash<> specialization for QString based on the dbj2
  // algorithm http://www.cse.yorku.ca/~oz/hash.html because qHash()
  // is poor on 64-bit platforms due to being a 32-bit hash value
  template<>
  struct hash<QString>
  {
    std::size_t operator () (QString const& s) const noexcept
    {
      std::size_t hash {5381};
      for (int i = 0; i < s.size (); ++i)
        {
          hash = ((hash << 5) + hash) + ((s.at (i).row () << 8) | s.at (i).cell ());
        }
      return hash;
    }
  };
}
#endif

inline
bool is_broadcast_address (QHostAddress const& host_addr)
{
#if QT_VERSION >= QT_VERSION_CHECK (5, 11, 0)
  return host_addr.isBroadcast ();
#else
  bool ok;
  return host_addr.toIPv4Address (&ok) == 0xffffffffu && ok;
#endif
}

inline
bool is_multicast_address (QHostAddress const& host_addr)
{
#if QT_VERSION >= QT_VERSION_CHECK (5, 6, 0)
  return host_addr.isMulticast ();
#else
  bool ok;
  return (((host_addr.toIPv4Address (&ok) & 0xf0000000u) == 0xe0000000u) && ok)
    || host_addr.toIPv6Address ()[0] == 0xff;
#endif
}

inline
bool is_MAC_ambiguous_multicast_address (QHostAddress const& host_addr)
{
  // sub-ranges 224.128.0.0/24, 225.0.0.0/24, 225.128.0.0/24,
  // 226.0.0.0/24, 226.128.0.0/24, ..., 239.0.0.0/24, 239.128.0.0/24
  // are not supported as they are inefficient due to ambiguous
  // mappings to Ethernet MAC addresses. 224.0.0.0/24 alone is allowed
  // from these ranges
  bool ok;
  auto ipv4 = host_addr.toIPv4Address (&ok);
  return ok && !((ipv4 & 0xffffff00u) == 0xe0000000) && (ipv4 & 0xf07fff00) == 0xe0000000;
}

// Register some useful Qt types with QMetaType
Q_DECLARE_METATYPE (QHostAddress);

#endif