| 1 |
// Copyright (C) 2025 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QCoreApplication> |
| 5 |
#include <QTranslator> |
| 6 |
|
| 7 |
namespace Outer { |
| 8 |
namespace Inner { |
| 9 |
namespace Deep { |
| 10 |
class MyClass |
| 11 |
{ |
| 12 |
public: |
| 13 |
static int getValue() { return 42; } |
| 14 |
static int count; |
| 15 |
}; |
| 16 |
} // namespace Deep |
| 17 |
} // namespace Inner |
| 18 |
} // namespace Outer |
| 19 |
|
| 20 |
template <typename T> |
| 21 |
class TemplateClass |
| 22 |
{ |
| 23 |
public: |
| 24 |
static T value; |
| 25 |
static int getCount() { return 10; } |
| 26 |
}; |
| 27 |
|
| 28 |
template <typename T, typename U> |
| 29 |
class DoubleTemplate |
| 30 |
{ |
| 31 |
public: |
| 32 |
static int getValue() { return 20; } |
| 33 |
}; |
| 34 |
|
| 35 |
class Dialog |
| 36 |
{ |
| 37 |
public: |
| 38 |
static int getCount() { return 5; } |
| 39 |
int value; |
| 40 |
int getValue() { return 42; } |
| 41 |
}; |
| 42 |
|
| 43 |
int func() |
| 44 |
{ |
| 45 |
QTranslator trans; |
| 46 |
int n = 5; |
| 47 |
int array[5] = { 1, 2, 3, 4, 5 }; |
| 48 |
Dialog obj; |
| 49 |
int *intPtr = &n; |
| 50 |
|
| 51 |
// Parenthesized expressions |
| 52 |
|
| 53 |
trans.translate("Context", "Single parens around identifier", "comment", (n)); |
| 54 |
trans.translate("Context", "Double parens", "comment", ((n))); |
| 55 |
trans.translate("Context", "Triple parens", "comment", (((n)))); |
| 56 |
trans.translate("Context", "Many nested parens", "comment", ((((((n))))))); |
| 57 |
trans.translate("Context", "Parens around number", "comment", (42)); |
| 58 |
trans.translate("Context", "Parens around expression", "comment", (n + 1)); |
| 59 |
trans.translate("Context", "Parens around complex expr", "comment", (n * 2 + 3)); |
| 60 |
trans.translate("Context", "Parens with operators", "comment", ((n + 1) * (n - 1))); |
| 61 |
trans.translate("Context", "Nested parens with operators", "comment", (n + 1) * (n - 1)); |
| 62 |
trans.translate("Context", "Empty parens case", "comment", (int)n); |
| 63 |
|
| 64 |
// Scope resolution operator :: |
| 65 |
|
| 66 |
trans.translate("Context", "Simple scope resolution", "comment", Dialog::getCount()); |
| 67 |
trans.translate("Context", "Nested namespace scope", "comment", |
| 68 |
Outer::Inner::Deep::MyClass::getValue()); |
| 69 |
trans.translate("Context", "Scope with static member", "comment", |
| 70 |
Outer::Inner::Deep::MyClass::count); |
| 71 |
trans.translate("Context", "Global scope", "comment", ::Dialog::getCount()); |
| 72 |
trans.translate("Context", "Long scope chain", "comment", |
| 73 |
Outer::Inner::Deep::MyClass::getValue()); |
| 74 |
|
| 75 |
// Combined: Scope resolution + Parentheses |
| 76 |
|
| 77 |
trans.translate("Context", "Parens around scope resolution", "comment", (Dialog::getCount())); |
| 78 |
trans.translate("Context", "Double parens around scope", "comment", |
| 79 |
((Outer::Inner::Deep::MyClass::getValue()))); |
| 80 |
trans.translate("Context", "Scope in paren expr", "comment", (Dialog::getCount() + 1)); |
| 81 |
|
| 82 |
// Templates (angle brackets) |
| 83 |
|
| 84 |
trans.translate("Context", "Simple template", "comment", TemplateClass<int>::getCount()); |
| 85 |
trans.translate("Context", "Template with scope", "comment", TemplateClass<Dialog>::getCount()); |
| 86 |
trans.translate("Context", "Double template", "comment", |
| 87 |
DoubleTemplate<int, double>::getValue()); |
| 88 |
trans.translate("Context", "Nested template", "comment", |
| 89 |
TemplateClass<TemplateClass<int>>::getCount()); |
| 90 |
trans.translate("Context", "Template with parens", "comment", (TemplateClass<int>::getCount())); |
| 91 |
|
| 92 |
// Cast expressions |
| 93 |
|
| 94 |
trans.translate("Context", "static_cast with scope resolution", "comment", |
| 95 |
static_cast<long>(Dialog::getCount())); |
| 96 |
trans.translate("Context", "static_cast in parens", "comment", (static_cast<int>(n))); |
| 97 |
Dialog *dptr = nullptr; |
| 98 |
trans.translate("Context", "reinterpret_cast", "comment", reinterpret_cast<long>(dptr)); |
| 99 |
const int *cptr = &n; |
| 100 |
trans.translate("Context", "const_cast", "comment", *const_cast<int *>(cptr)); |
| 101 |
trans.translate("Context", "C-style cast", "comment", (int)Dialog::getCount()); |
| 102 |
trans.translate("Context", "Functional cast", "comment", int(Dialog::getCount())); |
| 103 |
|
| 104 |
// Combinations |
| 105 |
|
| 106 |
trans.translate("Context", "Triple combo", "comment", |
| 107 |
static_cast<long>(TemplateClass<int>::getCount())); |
| 108 |
trans.translate("Context", "Nested cast with scope", "comment", |
| 109 |
static_cast<int>(static_cast<long>(Dialog::getCount()))); |
| 110 |
trans.translate("Context", "Template with multi-scope", "comment", |
| 111 |
TemplateClass<Outer::Inner::Deep::MyClass>::getCount()); |
| 112 |
trans.translate("Context", "Parens around cast with template", "comment", |
| 113 |
(static_cast<long>(TemplateClass<int>::getCount()))); |
| 114 |
trans.translate("Context", "Double nested template", "comment", |
| 115 |
DoubleTemplate<TemplateClass<int>, Dialog>::getValue()); |
| 116 |
trans.translate("Context", "Mixed angle and scope", "comment", |
| 117 |
TemplateClass<Outer::Inner::Deep::MyClass>::getCount()); |
| 118 |
|
| 119 |
// Arrow operator |
| 120 |
|
| 121 |
Dialog *ptr = nullptr; |
| 122 |
trans.translate("Context", "Arrow operator", "comment", ptr->getCount()); |
| 123 |
trans.translate("Context", "Arrow with scope", "comment", |
| 124 |
Outer::Inner::Deep::MyClass::getValue()); |
| 125 |
trans.translate("Context", "Parens around arrow", "comment", (ptr->getCount())); |
| 126 |
|
| 127 |
// Comparison operators |
| 128 |
|
| 129 |
trans.translate("Context", "Greater than", "comment", (n > 5)); |
| 130 |
trans.translate("Context", "Less than", "comment", (n < 5)); |
| 131 |
trans.translate("Context", "Greater or equal", "comment", (n >= 5)); |
| 132 |
trans.translate("Context", "Less or equal", "comment", (n <= 5)); |
| 133 |
trans.translate("Context", "Equal", "comment", (n == 5)); |
| 134 |
trans.translate("Context", "Not equal", "comment", (n != 5)); |
| 135 |
|
| 136 |
// Array subscripts |
| 137 |
|
| 138 |
trans.translate("Context", "Array subscript", "comment", array[0]); |
| 139 |
trans.translate("Context", "Array with expression 1", "comment", array[0] * 2); |
| 140 |
trans.translate("Context", "Array with expression 2", "comment", array[n - 5]); |
| 141 |
|
| 142 |
// Dot operator |
| 143 |
|
| 144 |
trans.translate("Context", "Dot operator", "comment", obj.getValue()); |
| 145 |
trans.translate("Context", "Dot member access", "comment", obj.value); |
| 146 |
|
| 147 |
// Unary operators |
| 148 |
|
| 149 |
trans.translate("Context", "Dereference", "comment", *intPtr); |
| 150 |
trans.translate("Context", "Address of", "comment", (long)&n); |
| 151 |
trans.translate("Context", "Logical not", "comment", !n); |
| 152 |
trans.translate("Context", "Bitwise not", "comment", ~n); |
| 153 |
trans.translate("Context", "Unary minus", "comment", -n); |
| 154 |
trans.translate("Context", "Unary plus", "comment", +n); |
| 155 |
|
| 156 |
// More binary operators |
| 157 |
|
| 158 |
trans.translate("Context", "Division", "comment", (n / 2)); |
| 159 |
trans.translate("Context", "Modulo", "comment", (n % 3)); |
| 160 |
trans.translate("Context", "Bitwise AND", "comment", (n & 0xFF)); |
| 161 |
trans.translate("Context", "Bitwise OR", "comment", (n | 0xFF)); |
| 162 |
trans.translate("Context", "Bitwise XOR", "comment", (n ^ 0xFF)); |
| 163 |
trans.translate("Context", "Bitwise right shift", "comment", (n >> 2)); |
| 164 |
|
| 165 |
// Logical operators |
| 166 |
|
| 167 |
trans.translate("Context", "Logical AND", "comment", (n && 5)); |
| 168 |
trans.translate("Context", "Logical OR", "comment", (n || 5)); |
| 169 |
|
| 170 |
// Special cases |
| 171 |
|
| 172 |
trans.translate("Context", "Ternary operator", "comment", (n > 0 ? n : 0)); |
| 173 |
trans.translate("Context", "Bitwise left shift", "comment", (n << 1)); |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |