1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#ifndef QQMLDEBUGTRANSLATIONPROTOCOL_P_H
4#define QQMLDEBUGTRANSLATIONPROTOCOL_P_H
5//
6// W A R N I N G
7// -------------
8//
9// This file is not part of the Qt API. It exists purely as an
10// implementation detail. This header file may change from version to
11// version without notice, or even be removed.
12//
13// We mean it.
14//
15#include <QtCore/qdatastream.h>
16#include <QtCore/qbuffer.h>
17#include <QtCore/qurl.h>
18#include <QtCore/qobjectdefs.h>
19#include <QtCore/qmetaobject.h>
20#include <QtCore/private/qglobal_p.h>
21#include <tuple>
22
23QT_BEGIN_NAMESPACE
24
25namespace QQmlDebugTranslation {
26
27enum class Request {
28 ChangeLanguage = 1,
29 StateList,
30 ChangeState,
31 TranslationIssues,
32 TranslatableTextOccurrences,
33 WatchTextElides,
34 DisableWatchTextElides,
35 // following are obsolete, just provided for compilation compatibility
36 MissingTranslations
37};
38
39enum class Reply {
40 LanguageChanged = 101,
41 StateList,
42 StateChanged,
43 TranslationIssues,
44 TranslatableTextOccurrences,
45 // following are obsolete, just provided for compilation compatibility
46 MissingTranslations,
47 TextElided
48};
49
50inline QDataStream &operator<<(QDataStream &ds, Request r)
51{
52 return ds << int(r);
53}
54
55inline QDataStream &operator>>(QDataStream &ds, Request &r)
56{
57 int i;
58 ds >> i;
59 r = Request(i);
60 return ds;
61}
62
63inline QDataStream &operator<<(QDataStream &ds, Reply r)
64{
65 return ds << int(r);
66}
67
68inline QDataStream &operator>>(QDataStream &ds, Reply &r)
69{
70 int i;
71 ds >> i;
72 r = Reply(i);
73 return ds;
74}
75
76inline QByteArray createChangeLanguageRequest(QDataStream &packet, const QUrl &url,
77 const QString &locale)
78{
79 packet << Request::ChangeLanguage << url << locale;
80 return qobject_cast<QBuffer *>(object: packet.device())->data();
81}
82
83inline QByteArray createChangeStateRequest(QDataStream &packet, const QString &state)
84{
85 packet << Request::ChangeState << state;
86 return qobject_cast<QBuffer *>(object: packet.device())->data();
87}
88
89inline QByteArray createMissingTranslationsRequest(QDataStream &packet)
90{
91 packet << Request::MissingTranslations;
92 return qobject_cast<QBuffer *>(object: packet.device())->data();
93}
94
95inline QByteArray createTranslationIssuesRequest(QDataStream &packet)
96{
97 packet << Request::TranslationIssues;
98 return qobject_cast<QBuffer *>(object: packet.device())->data();
99}
100
101inline QByteArray createTranslatableTextOccurrencesRequest(QDataStream &packet)
102{
103 packet << Request::TranslatableTextOccurrences;
104 return qobject_cast<QBuffer *>(object: packet.device())->data();
105}
106
107inline QByteArray createStateListRequest(QDataStream &packet)
108{
109 packet << Request::StateList;
110 return qobject_cast<QBuffer *>(object: packet.device())->data();
111}
112
113inline QByteArray createWatchTextElidesRequest(QDataStream &packet)
114{
115 packet << Request::WatchTextElides;
116 return qobject_cast<QBuffer *>(object: packet.device())->data();
117}
118
119inline QByteArray createDisableWatchTextElidesRequest(QDataStream &packet)
120{
121 packet << Request::DisableWatchTextElides;
122 return qobject_cast<QBuffer *>(object: packet.device())->data();
123}
124
125class CodeMarker
126{
127public:
128 friend QDataStream &operator>>(QDataStream &stream, CodeMarker &codeMarker)
129 {
130 return stream >> codeMarker.url
131 >> codeMarker.line
132 >> codeMarker.column;
133 }
134
135 friend QDataStream &operator<<(QDataStream &stream, const CodeMarker &codeMarker)
136 {
137 return stream << codeMarker.url
138 << codeMarker.line
139 << codeMarker.column;
140 }
141
142 friend bool operator<(const CodeMarker &first, const CodeMarker &second)
143 {
144 return std::tie(args: first.url, args: first.line, args: first.column)
145 < std::tie(args: second.url, args: second.line, args: second.column);
146 }
147
148 friend bool operator==(const CodeMarker &first, const CodeMarker &second)
149 {
150 return first.line == second.line
151 && first.column == second.column
152 && first.url == second.url;
153 }
154
155 QUrl url;
156 int line = -1;
157 int column = -1;
158};
159class TranslationIssue
160{
161public:
162 enum class Type{
163 Missing,
164 Elided
165 };
166
167 friend QDataStream &operator>>(QDataStream &stream, TranslationIssue &issue)
168 {
169 int t;
170 stream >> issue.codeMarker
171 >> issue.language
172 >> t;
173 issue.type = Type(t);
174 return stream;
175 }
176
177 friend QDataStream &operator<<(QDataStream &stream, const TranslationIssue &issue)
178 {
179 return stream << issue.codeMarker
180 << issue.language
181 << int(issue.type);
182 }
183
184 friend bool operator==(const TranslationIssue &first, const TranslationIssue &second)
185 {
186 return first.type == second.type
187 && first.language == second.language
188 && first.codeMarker == second.codeMarker;
189 }
190
191 QString toDebugString() const
192 {
193 QString debugString(QLatin1String(
194 "TranslationIssue(type=%1, line=%2, column=%3, url=%4, language=%5)"));
195 return debugString.arg(args: type == TranslationIssue::Type::Missing ? QLatin1String("Missing")
196 : QLatin1String("Elided"),
197 args: QString::number(codeMarker.line), args: QString::number(codeMarker.column),
198 args: codeMarker.url.toString(), args: language);
199 }
200
201 QString language;
202 Type type = Type::Missing;
203 CodeMarker codeMarker;
204};
205class QmlElement
206{
207public:
208 QmlElement() = default;
209
210 friend QDataStream &operator>>(QDataStream &stream, QmlElement &qmlElement)
211 {
212 return stream >> qmlElement.codeMarker >> qmlElement.elementId >> qmlElement.elementType
213 >> qmlElement.propertyName >> qmlElement.translationId >> qmlElement.translatedText
214 >> qmlElement.fontFamily >> qmlElement.fontPointSize >> qmlElement.fontPixelSize
215 >> qmlElement.fontStyleName >> qmlElement.horizontalAlignment
216 >> qmlElement.verticalAlignment >> qmlElement.stateName;
217 }
218
219 friend QDataStream &operator<<(QDataStream &stream, const QmlElement &qmlElement)
220 {
221 return stream << qmlElement.codeMarker << qmlElement.elementId << qmlElement.elementType
222 << qmlElement.propertyName << qmlElement.translationId
223 << qmlElement.translatedText << qmlElement.fontFamily
224 << qmlElement.fontPointSize << qmlElement.fontPixelSize
225 << qmlElement.fontStyleName << qmlElement.horizontalAlignment
226 << qmlElement.verticalAlignment << qmlElement.stateName;
227 }
228
229 CodeMarker codeMarker;
230 QString propertyName;
231 QString translationId;
232 QString translatedText;
233 QString fontFamily;
234 QString fontStyleName;
235 QString elementId;
236 QString elementType;
237 qreal fontPointSize = 0.0;
238 QString stateName;
239 int fontPixelSize = 0;
240 int horizontalAlignment = 0;
241 int verticalAlignment = 0;
242};
243
244class QmlState
245{
246public:
247 QmlState() = default;
248
249 friend QDataStream &operator>>(QDataStream &stream, QmlState &qmlState)
250 {
251 return stream >> qmlState.name;
252 }
253
254 friend QDataStream &operator<<(QDataStream &stream, const QmlState &qmlState)
255 {
256 return stream << qmlState.name;
257 }
258
259 QString name;
260};
261}
262
263QT_END_NAMESPACE
264
265#endif // QQMLDEBUGTRANSLATIONPROTOCOL_P_H
266

source code of qtdeclarative/src/qml/debugger/qqmldebugtranslationprotocol_p.h