|
| 1 | +// Copyright (c) 2025 Kirill Gavrilov |
| 2 | + |
| 3 | +#include "OcctQtTools.h" |
| 4 | + |
| 5 | +// ================================================================ |
| 6 | +// Function : qtColorToOcct |
| 7 | +// ================================================================ |
| 8 | +Quantity_Color OcctQtTools::qtColorToOcct(const QColor& theColor) |
| 9 | +{ |
| 10 | + return Quantity_Color(theColor.red() / 255.0, theColor.green() / 255.0, theColor.blue() / 255.0, Quantity_TOC_sRGB); |
| 11 | +} |
| 12 | + |
| 13 | +// ================================================================ |
| 14 | +// Function : qtColorFromOcct |
| 15 | +// ================================================================ |
| 16 | +QColor OcctQtTools::qtColorFromOcct(const Quantity_Color& theColor) |
| 17 | +{ |
| 18 | + NCollection_Vec3<double> anRgb; theColor.Values(anRgb.r(), anRgb.g(), anRgb.b(), Quantity_TOC_sRGB); |
| 19 | + return QColor((int)Round(anRgb.r() * 255.0), (int)Round(anRgb.g() * 255.0), (int)Round(anRgb.b() * 255.0)); |
| 20 | +} |
| 21 | + |
| 22 | +// ================================================================ |
| 23 | +// Function : qtGlSurfaceFormat |
| 24 | +// ================================================================ |
| 25 | +QSurfaceFormat OcctQtTools::qtGlSurfaceFormat(QSurfaceFormat::OpenGLContextProfile theProfile, |
| 26 | + bool theToDebug) |
| 27 | +{ |
| 28 | + const bool isDeepColor = false; |
| 29 | + QSurfaceFormat::OpenGLContextProfile aProfile = theProfile; |
| 30 | + if (theProfile == QSurfaceFormat::NoProfile) |
| 31 | + { |
| 32 | + aProfile = QSurfaceFormat::CompatibilityProfile; |
| 33 | +#ifdef __APPLE__ |
| 34 | + // suppress Qt warning "QCocoaGLContext: Falling back to unshared context" |
| 35 | + aProfile = QSurfaceFormat::CoreProfile; |
| 36 | +#endif |
| 37 | + } |
| 38 | + |
| 39 | + QSurfaceFormat aGlFormat; |
| 40 | + if (isDeepColor) |
| 41 | + { |
| 42 | + aGlFormat.setRedBufferSize(10); |
| 43 | + aGlFormat.setGreenBufferSize(10); |
| 44 | + aGlFormat.setBlueBufferSize(10); |
| 45 | + aGlFormat.setAlphaBufferSize(2); |
| 46 | + } |
| 47 | + aGlFormat.setDepthBufferSize(24); |
| 48 | + aGlFormat.setStencilBufferSize(8); |
| 49 | + aGlFormat.setProfile(theProfile); |
| 50 | + if (theProfile == QSurfaceFormat::CoreProfile) |
| 51 | + aGlFormat.setVersion(4, 5); |
| 52 | + |
| 53 | + // request sRGBColorSpace colorspace to meet OCCT expectations or use OcctQtFrameBuffer fallback. |
| 54 | + /*#if (QT_VERSION_MAJOR > 5) || (QT_VERSION_MAJOR == 5 && QT_VERSION_MINOR >= 10) |
| 55 | + aGlFormat.setColorSpace(QSurfaceFormat::sRGBColorSpace); |
| 56 | + #endif*/ |
| 57 | + |
| 58 | + if (theToDebug) |
| 59 | + aGlFormat.setOption(QSurfaceFormat::DebugContext, true); |
| 60 | + |
| 61 | + return aGlFormat; |
| 62 | +} |
| 63 | + |
| 64 | +// ================================================================ |
| 65 | +// Function : qtMouseButtons2VKeys |
| 66 | +// ================================================================ |
| 67 | +Aspect_VKeyMouse OcctQtTools::qtMouseButtons2VKeys(Qt::MouseButtons theButtons) |
| 68 | +{ |
| 69 | + Aspect_VKeyMouse aButtons = Aspect_VKeyMouse_NONE; |
| 70 | + if ((theButtons & Qt::LeftButton) != 0) |
| 71 | + aButtons |= Aspect_VKeyMouse_LeftButton; |
| 72 | + |
| 73 | + if ((theButtons & Qt::MiddleButton) != 0) |
| 74 | + aButtons |= Aspect_VKeyMouse_MiddleButton; |
| 75 | + |
| 76 | + if ((theButtons & Qt::RightButton) != 0) |
| 77 | + aButtons |= Aspect_VKeyMouse_RightButton; |
| 78 | + |
| 79 | + return aButtons; |
| 80 | +} |
| 81 | + |
| 82 | +// ================================================================ |
| 83 | +// Function : qtMouseModifiers2VKeys |
| 84 | +// ================================================================ |
| 85 | +Aspect_VKeyFlags OcctQtTools::qtMouseModifiers2VKeys(Qt::KeyboardModifiers theModifiers) |
| 86 | +{ |
| 87 | + Aspect_VKeyFlags aFlags = Aspect_VKeyFlags_NONE; |
| 88 | + if ((theModifiers & Qt::ShiftModifier) != 0) |
| 89 | + aFlags |= Aspect_VKeyFlags_SHIFT; |
| 90 | + |
| 91 | + if ((theModifiers & Qt::ControlModifier) != 0) |
| 92 | + aFlags |= Aspect_VKeyFlags_CTRL; |
| 93 | + |
| 94 | + if ((theModifiers & Qt::AltModifier) != 0) |
| 95 | + aFlags |= Aspect_VKeyFlags_ALT; |
| 96 | + |
| 97 | + return aFlags; |
| 98 | +} |
| 99 | + |
| 100 | +// ================================================================ |
| 101 | +// Function : qtKey2VKey |
| 102 | +// ================================================================ |
| 103 | +Aspect_VKey OcctQtTools::qtKey2VKey(int theKey) |
| 104 | +{ |
| 105 | + switch (theKey) |
| 106 | + { |
| 107 | + case 1060: // ru |
| 108 | + case Qt::Key_A: |
| 109 | + return Aspect_VKey_A; |
| 110 | + case 1048: // ru |
| 111 | + case Qt::Key_B: |
| 112 | + return Aspect_VKey_B; |
| 113 | + case 1057: // ru |
| 114 | + case Qt::Key_C: |
| 115 | + return Aspect_VKey_C; |
| 116 | + case 1042: // ru |
| 117 | + case Qt::Key_D: |
| 118 | + return Aspect_VKey_D; |
| 119 | + case 1059: // ru |
| 120 | + case Qt::Key_E: |
| 121 | + return Aspect_VKey_E; |
| 122 | + case 1040: // ru |
| 123 | + case Qt::Key_F: |
| 124 | + return Aspect_VKey_F; |
| 125 | + case Qt::Key_G: |
| 126 | + return Aspect_VKey_G; |
| 127 | + case Qt::Key_H: |
| 128 | + return Aspect_VKey_H; |
| 129 | + case Qt::Key_I: |
| 130 | + return Aspect_VKey_I; |
| 131 | + case Qt::Key_J: |
| 132 | + return Aspect_VKey_J; |
| 133 | + case Qt::Key_K: |
| 134 | + return Aspect_VKey_K; |
| 135 | + case 1044: // ru |
| 136 | + case Qt::Key_L: |
| 137 | + return Aspect_VKey_L; |
| 138 | + case Qt::Key_M: |
| 139 | + return Aspect_VKey_M; |
| 140 | + case Qt::Key_N: |
| 141 | + return Aspect_VKey_N; |
| 142 | + case Qt::Key_O: |
| 143 | + return Aspect_VKey_O; |
| 144 | + case Qt::Key_P: |
| 145 | + return Aspect_VKey_P; |
| 146 | + case 1049: // ru |
| 147 | + case Qt::Key_Q: |
| 148 | + return Aspect_VKey_Q; |
| 149 | + case 1050: // ru |
| 150 | + case Qt::Key_R: |
| 151 | + return Aspect_VKey_R; |
| 152 | + case 1067: // ru |
| 153 | + case Qt::Key_S: |
| 154 | + return Aspect_VKey_S; |
| 155 | + case 1045: // ru |
| 156 | + case Qt::Key_T: |
| 157 | + return Aspect_VKey_T; |
| 158 | + case Qt::Key_U: |
| 159 | + return Aspect_VKey_U; |
| 160 | + case 1052: // ru |
| 161 | + case Qt::Key_V: |
| 162 | + return Aspect_VKey_V; |
| 163 | + case 1062: // ru |
| 164 | + case Qt::Key_W: |
| 165 | + return Aspect_VKey_W; |
| 166 | + case 1063: // ru |
| 167 | + case Qt::Key_X: |
| 168 | + return Aspect_VKey_X; |
| 169 | + case Qt::Key_Y: |
| 170 | + return Aspect_VKey_Y; |
| 171 | + case 1071: // ru |
| 172 | + case Qt::Key_Z: |
| 173 | + return Aspect_VKey_Z; |
| 174 | + // |
| 175 | + case Qt::Key_0: |
| 176 | + return Aspect_VKey_0; |
| 177 | + case Qt::Key_1: |
| 178 | + return Aspect_VKey_1; |
| 179 | + case Qt::Key_2: |
| 180 | + return Aspect_VKey_2; |
| 181 | + case Qt::Key_3: |
| 182 | + return Aspect_VKey_3; |
| 183 | + case Qt::Key_4: |
| 184 | + return Aspect_VKey_4; |
| 185 | + case Qt::Key_5: |
| 186 | + return Aspect_VKey_5; |
| 187 | + case Qt::Key_6: |
| 188 | + return Aspect_VKey_6; |
| 189 | + case Qt::Key_7: |
| 190 | + return Aspect_VKey_7; |
| 191 | + case Qt::Key_8: |
| 192 | + return Aspect_VKey_8; |
| 193 | + case Qt::Key_9: |
| 194 | + return Aspect_VKey_9; |
| 195 | + // |
| 196 | + case Qt::Key_F1: |
| 197 | + return Aspect_VKey_F1; |
| 198 | + case Qt::Key_F2: |
| 199 | + return Aspect_VKey_F2; |
| 200 | + case Qt::Key_F3: |
| 201 | + return Aspect_VKey_F3; |
| 202 | + case Qt::Key_F4: |
| 203 | + return Aspect_VKey_F4; |
| 204 | + case Qt::Key_F5: |
| 205 | + return Aspect_VKey_F5; |
| 206 | + case Qt::Key_F6: |
| 207 | + return Aspect_VKey_F6; |
| 208 | + case Qt::Key_F7: |
| 209 | + return Aspect_VKey_F7; |
| 210 | + case Qt::Key_F8: |
| 211 | + return Aspect_VKey_F8; |
| 212 | + case Qt::Key_F9: |
| 213 | + return Aspect_VKey_F9; |
| 214 | + case Qt::Key_F10: |
| 215 | + return Aspect_VKey_F10; |
| 216 | + case Qt::Key_F11: |
| 217 | + return Aspect_VKey_F11; |
| 218 | + case Qt::Key_F12: |
| 219 | + return Aspect_VKey_F12; |
| 220 | + // |
| 221 | + case Qt::Key_Up: |
| 222 | + return Aspect_VKey_Up; |
| 223 | + case Qt::Key_Left: |
| 224 | + return Aspect_VKey_Left; |
| 225 | + case Qt::Key_Right: |
| 226 | + return Aspect_VKey_Right; |
| 227 | + case Qt::Key_Down: |
| 228 | + return Aspect_VKey_Down; |
| 229 | + case Qt::Key_Plus: |
| 230 | + return Aspect_VKey_Plus; |
| 231 | + case Qt::Key_Minus: |
| 232 | + return Aspect_VKey_Minus; |
| 233 | + case Qt::Key_Equal: |
| 234 | + return Aspect_VKey_Equal; |
| 235 | + case Qt::Key_PageDown: |
| 236 | + return Aspect_VKey_PageDown; |
| 237 | + case Qt::Key_PageUp: |
| 238 | + return Aspect_VKey_PageUp; |
| 239 | + case Qt::Key_Home: |
| 240 | + return Aspect_VKey_Home; |
| 241 | + case Qt::Key_End: |
| 242 | + return Aspect_VKey_End; |
| 243 | + case Qt::Key_Escape: |
| 244 | + return Aspect_VKey_Escape; |
| 245 | + case Qt::Key_Back: |
| 246 | + return Aspect_VKey_Back; |
| 247 | + case Qt::Key_Enter: |
| 248 | + return Aspect_VKey_Enter; |
| 249 | + case Qt::Key_Backspace: |
| 250 | + return Aspect_VKey_Backspace; |
| 251 | + case Qt::Key_Space: |
| 252 | + return Aspect_VKey_Space; |
| 253 | + case Qt::Key_Delete: |
| 254 | + return Aspect_VKey_Delete; |
| 255 | + case Qt::Key_Tab: |
| 256 | + return Aspect_VKey_Tab; |
| 257 | + case 1025: |
| 258 | + case Qt::Key_QuoteLeft: |
| 259 | + return Aspect_VKey_Tilde; |
| 260 | + // |
| 261 | + case Qt::Key_Shift: |
| 262 | + return Aspect_VKey_Shift; |
| 263 | + case Qt::Key_Control: |
| 264 | + return Aspect_VKey_Control; |
| 265 | + case Qt::Key_Alt: |
| 266 | + return Aspect_VKey_Alt; |
| 267 | + case Qt::Key_Menu: |
| 268 | + return Aspect_VKey_Menu; |
| 269 | + case Qt::Key_Meta: |
| 270 | + return Aspect_VKey_Meta; |
| 271 | + default: |
| 272 | + return Aspect_VKey_UNKNOWN; |
| 273 | + } |
| 274 | +} |
0 commit comments