| 1 | // Copyright (C) 2016 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 | |
| 4 | #include "deviceskin_p.h" |
| 5 | |
| 6 | #include <QtCore/qnamespace.h> |
| 7 | #include <QtWidgets/QApplication> |
| 8 | #include <QtGui/QBitmap> |
| 9 | #include <QtGui/QPixmap> |
| 10 | #include <QtGui/QPainter> |
| 11 | #include <QtCore/QTextStream> |
| 12 | #include <QtCore/QFile> |
| 13 | #include <QtCore/QFileInfo> |
| 14 | #include <QtGui/QImage> |
| 15 | #include <QtCore/QTimer> |
| 16 | #include <QtCore/QDir> |
| 17 | #include <QtCore/QRegularExpression> |
| 18 | #include <QtGui/QMouseEvent> |
| 19 | #include <QtCore/QDebug> |
| 20 | |
| 21 | #ifdef TEST_SKIN |
| 22 | # include <QtWidgets/QMainWindow> |
| 23 | # include <QtWidgets/QDialog> |
| 24 | # include <QtWidgets/QDialogButtonBox> |
| 25 | # include <QtWidgets/QHBoxLayout> |
| 26 | #endif |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | using namespace Qt::StringLiterals; |
| 31 | |
| 32 | namespace { |
| 33 | enum { joydistance = 10, key_repeat_period = 50, key_repeat_delay = 500 }; |
| 34 | enum { debugDeviceSkin = 0 }; |
| 35 | } |
| 36 | |
| 37 | static void parseRect(const QString &value, QRect *rect) { |
| 38 | const auto l = QStringView{value}.split(sep: QLatin1Char(' ')); |
| 39 | rect->setRect(ax: l[0].toInt(), ay: l[1].toInt(), aw: l[2].toInt(), ah: l[3].toInt()); |
| 40 | } |
| 41 | |
| 42 | static QString msgImageNotLoaded(const QString &f) { |
| 43 | return DeviceSkin::tr(s: "The image file '%1' could not be loaded." ).arg(a: f); |
| 44 | } |
| 45 | |
| 46 | // ------------ DeviceSkinButtonArea |
| 47 | QDebug &operator<<(QDebug &str, const DeviceSkinButtonArea &a) |
| 48 | { |
| 49 | |
| 50 | str << "Area: " << a.name << " keyCode=" << a.keyCode << " area=" << a.area |
| 51 | << " text=" << a.text << " activeWhenClosed=" << a.activeWhenClosed; |
| 52 | return str; |
| 53 | } |
| 54 | |
| 55 | // ------------ DeviceSkinParameters |
| 56 | |
| 57 | QDebug operator<<(QDebug str, const DeviceSkinParameters &p) |
| 58 | { |
| 59 | str << "Images " << p.skinImageUpFileName << ',' |
| 60 | << p.skinImageDownFileName<< ',' << p.skinImageClosedFileName |
| 61 | << ',' << p.skinCursorFileName <<"\nScreen: " << p.screenRect |
| 62 | << " back: " << p.backScreenRect << " closed: " << p.closedScreenRect |
| 63 | << " cursor: " << p.cursorHot << " Prefix: " << p.prefix |
| 64 | << " Joystick: " << p.joystick << " MouseHover" << p.hasMouseHover; |
| 65 | const int numAreas = p.buttonAreas.size(); |
| 66 | for (int i = 0; i < numAreas; i++) |
| 67 | str << p.buttonAreas[i]; |
| 68 | return str; |
| 69 | } |
| 70 | |
| 71 | QSize DeviceSkinParameters::secondaryScreenSize() const |
| 72 | { |
| 73 | return backScreenRect.isNull() ? closedScreenRect .size(): backScreenRect.size(); |
| 74 | } |
| 75 | |
| 76 | bool DeviceSkinParameters::hasSecondaryScreen() const |
| 77 | { |
| 78 | return secondaryScreenSize() != QSize(0, 0); |
| 79 | } |
| 80 | |
| 81 | bool DeviceSkinParameters::read(const QString &skinDirectory, ReadMode rm, QString *errorMessage) |
| 82 | { |
| 83 | // Figure out the name. remove ending '/' if present |
| 84 | QString skinFile = skinDirectory; |
| 85 | if (skinFile.endsWith(c: QLatin1Char('/'))) |
| 86 | skinFile.truncate(pos: skinFile.size() - 1); |
| 87 | |
| 88 | QFileInfo fi(skinFile); |
| 89 | QString fn; |
| 90 | if ( fi.isDir() ) { |
| 91 | prefix = skinFile; |
| 92 | prefix += QLatin1Char('/'); |
| 93 | fn = prefix; |
| 94 | fn += fi.baseName(); |
| 95 | fn += ".skin"_L1 ; |
| 96 | } else if (fi.isFile()){ |
| 97 | fn = skinFile; |
| 98 | prefix = fi.path(); |
| 99 | prefix += QLatin1Char('/'); |
| 100 | } else { |
| 101 | *errorMessage = DeviceSkin::tr(s: "The skin directory '%1' does not contain a configuration file." ).arg(a: skinDirectory); |
| 102 | return false; |
| 103 | } |
| 104 | QFile f(fn); |
| 105 | if (!f.open(flags: QIODevice::ReadOnly )) { |
| 106 | *errorMessage = DeviceSkin::tr(s: "The skin configuration file '%1' could not be opened." ).arg(a: fn); |
| 107 | return false; |
| 108 | } |
| 109 | QTextStream ts(&f); |
| 110 | const bool rc = read(ts, rm, errorMessage); |
| 111 | if (!rc) |
| 112 | *errorMessage = DeviceSkin::tr(s: "The skin configuration file '%1' could not be read: %2" ) |
| 113 | .arg(args&: fn, args&: *errorMessage); |
| 114 | return rc; |
| 115 | } |
| 116 | bool DeviceSkinParameters::read(QTextStream &ts, ReadMode rm, QString *errorMessage) |
| 117 | { |
| 118 | QStringList closedAreas; |
| 119 | QStringList toggleAreas; |
| 120 | QStringList toggleActiveAreas; |
| 121 | int nareas = 0; |
| 122 | screenDepth = 0; |
| 123 | QString mark; |
| 124 | ts >> mark; |
| 125 | hasMouseHover = true; // historical default |
| 126 | if (mark == "[SkinFile]"_L1 ) { |
| 127 | const QString UpKey = "Up"_L1 ; |
| 128 | const QString DownKey = "Down"_L1 ; |
| 129 | const QString ClosedKey = "Closed"_L1 ; |
| 130 | const QString ClosedAreasKey = "ClosedAreas"_L1 ; |
| 131 | const QString ScreenKey = "Screen"_L1 ; |
| 132 | const QString ScreenDepthKey = "ScreenDepth"_L1 ; |
| 133 | const QString BackScreenKey = "BackScreen"_L1 ; |
| 134 | const QString ClosedScreenKey = "ClosedScreen"_L1 ; |
| 135 | const QString CursorKey = "Cursor"_L1 ; |
| 136 | const QString AreasKey = "Areas"_L1 ; |
| 137 | const QString ToggleAreasKey = "ToggleAreas"_L1 ; |
| 138 | const QString ToggleActiveAreasKey = "ToggleActiveAreas"_L1 ; |
| 139 | const QString HasMouseHoverKey = "HasMouseHover"_L1 ; |
| 140 | // New |
| 141 | while (!nareas) { |
| 142 | QString line = ts.readLine(); |
| 143 | if ( line.isNull() ) |
| 144 | break; |
| 145 | if (!line.isEmpty() && line.at(i: 0) != u'#') { |
| 146 | int eq = line.indexOf(ch: QLatin1Char('=')); |
| 147 | if ( eq >= 0 ) { |
| 148 | const QString key = line.left(n: eq); |
| 149 | eq++; |
| 150 | while (eq<line.size()-1 && line[eq].isSpace()) |
| 151 | eq++; |
| 152 | const QString value = line.mid(position: eq); |
| 153 | if ( key == UpKey ) { |
| 154 | skinImageUpFileName = value; |
| 155 | } else if ( key == DownKey ) { |
| 156 | skinImageDownFileName = value; |
| 157 | } else if ( key == ClosedKey ) { |
| 158 | skinImageClosedFileName = value; |
| 159 | } else if ( key == ClosedAreasKey ) { |
| 160 | closedAreas = value.split(sep: QLatin1Char(' ')); |
| 161 | } else if ( key == ScreenKey ) { |
| 162 | parseRect( value, rect: &screenRect); |
| 163 | } else if ( key == ScreenDepthKey ) { |
| 164 | screenDepth = value.toInt(); |
| 165 | } else if ( key == BackScreenKey ) { |
| 166 | parseRect(value, rect: &backScreenRect); |
| 167 | } else if ( key == ClosedScreenKey ) { |
| 168 | parseRect( value, rect: &closedScreenRect ); |
| 169 | } else if ( key == CursorKey ) { |
| 170 | QStringList l = value.split(sep: QLatin1Char(' ')); |
| 171 | skinCursorFileName = l[0]; |
| 172 | cursorHot = QPoint(l[1].toInt(),l[2].toInt()); |
| 173 | } else if ( key == AreasKey ) { |
| 174 | nareas = value.toInt(); |
| 175 | } else if ( key == ToggleAreasKey ) { |
| 176 | toggleAreas = value.split(sep: QLatin1Char(' ')); |
| 177 | } else if ( key == ToggleActiveAreasKey ) { |
| 178 | toggleActiveAreas = value.split(sep: QLatin1Char(' ')); |
| 179 | } else if ( key == HasMouseHoverKey ) { |
| 180 | hasMouseHover = value == "true"_L1 || value == "1"_L1 ; |
| 181 | } |
| 182 | } else { |
| 183 | *errorMessage = DeviceSkin::tr(s: "Syntax error: %1" ).arg(a: line); |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } else { |
| 189 | // Old |
| 190 | skinImageUpFileName = mark; |
| 191 | QString s; |
| 192 | int x,y,w,h,na; |
| 193 | ts >> s >> x >> y >> w >> h >> na; |
| 194 | skinImageDownFileName = s; |
| 195 | screenRect.setRect(ax: x, ay: y, aw: w, ah: h); |
| 196 | nareas = na; |
| 197 | } |
| 198 | // Done for short mode |
| 199 | if (rm == ReadSizeOnly) |
| 200 | return true; |
| 201 | // verify skin files exist |
| 202 | skinImageUpFileName.insert(i: 0, s: prefix); |
| 203 | if (!QFile(skinImageUpFileName).exists()) { |
| 204 | *errorMessage = DeviceSkin::tr(s: "The skin \"up\" image file '%1' does not exist." ).arg(a: skinImageUpFileName); |
| 205 | return false; |
| 206 | } |
| 207 | if (!skinImageUp.load(fileName: skinImageUpFileName)) { |
| 208 | *errorMessage = msgImageNotLoaded(f: skinImageUpFileName); |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | skinImageDownFileName.insert(i: 0, s: prefix); |
| 213 | if (!QFile(skinImageDownFileName).exists()) { |
| 214 | *errorMessage = DeviceSkin::tr(s: "The skin \"down\" image file '%1' does not exist." ).arg(a: skinImageDownFileName); |
| 215 | return false; |
| 216 | } |
| 217 | if (!skinImageDown.load(fileName: skinImageDownFileName)) { |
| 218 | *errorMessage = msgImageNotLoaded(f: skinImageDownFileName); |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | if (!skinImageClosedFileName.isEmpty()) { |
| 223 | skinImageClosedFileName.insert(i: 0, s: prefix); |
| 224 | if (!QFile(skinImageClosedFileName).exists()) { |
| 225 | *errorMessage = DeviceSkin::tr(s: "The skin \"closed\" image file '%1' does not exist." ).arg(a: skinImageClosedFileName); |
| 226 | return false; |
| 227 | } |
| 228 | if (!skinImageClosed.load(fileName: skinImageClosedFileName)) { |
| 229 | *errorMessage = msgImageNotLoaded(f: skinImageClosedFileName); |
| 230 | return false; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | if (!skinCursorFileName.isEmpty()) { |
| 235 | skinCursorFileName.insert(i: 0, s: prefix); |
| 236 | if (!QFile(skinCursorFileName).exists()) { |
| 237 | *errorMessage = DeviceSkin::tr(s: "The skin cursor image file '%1' does not exist." ).arg(a: skinCursorFileName); |
| 238 | return false; |
| 239 | } |
| 240 | if (!skinCursor.load(fileName: skinCursorFileName)) { |
| 241 | *errorMessage = msgImageNotLoaded(f: skinCursorFileName); |
| 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // read areas |
| 247 | if (!nareas) |
| 248 | return true; |
| 249 | buttonAreas.reserve(asize: nareas); |
| 250 | |
| 251 | int i = 0; |
| 252 | ts.readLine(); // eol |
| 253 | joystick = -1; |
| 254 | const QString Joystick = "Joystick"_L1 ; |
| 255 | const QRegularExpression splitRe("[ \t][ \t]*"_L1 ); |
| 256 | Q_ASSERT(splitRe.isValid()); |
| 257 | while (i < nareas && !ts.atEnd() ) { |
| 258 | buttonAreas.push_back(t: DeviceSkinButtonArea()); |
| 259 | DeviceSkinButtonArea &area = buttonAreas.back(); |
| 260 | const QString line = ts.readLine(); |
| 261 | if ( !line.isEmpty() && line[0] != QLatin1Char('#') ) { |
| 262 | const QStringList tok = line.split(sep: splitRe); |
| 263 | if ( tok.size()<6 ) { |
| 264 | *errorMessage = DeviceSkin::tr(s: "Syntax error in area definition: %1" ).arg(a: line); |
| 265 | return false; |
| 266 | } else { |
| 267 | area.name = tok[0]; |
| 268 | QString k = tok[1]; |
| 269 | if ( k.left(n: 2).toLower() == "0x"_L1 ) { |
| 270 | area.keyCode = k.mid(position: 2).toInt(ok: 0,base: 16); |
| 271 | } else { |
| 272 | area.keyCode = k.toInt(); |
| 273 | } |
| 274 | |
| 275 | int p=0; |
| 276 | for (int j=2; j < tok.size() - 1; ) { |
| 277 | const int x = tok[j++].toInt(); |
| 278 | const int y = tok[j++].toInt(); |
| 279 | area.area.putPoints(index: p++,nPoints: 1,firstx: x,firsty: y); |
| 280 | } |
| 281 | |
| 282 | const QChar doubleQuote = QLatin1Char('"'); |
| 283 | if ( area.name[0] == doubleQuote && area.name.endsWith(c: doubleQuote)) { |
| 284 | area.name.truncate(pos: area.name.size() - 1); |
| 285 | area.name.remove(i: 0, len: 1); |
| 286 | } |
| 287 | if ( area.name.size() == 1 ) |
| 288 | area.text = area.name; |
| 289 | if ( area.name == Joystick) |
| 290 | joystick = i; |
| 291 | area.activeWhenClosed = closedAreas.contains(str: area.name) |
| 292 | || area.keyCode == Qt::Key_Flip; // must be to work |
| 293 | area.toggleArea = toggleAreas.contains(str: area.name); |
| 294 | area.toggleActiveArea = toggleActiveAreas.contains(str: area.name); |
| 295 | if (area.toggleArea) |
| 296 | toggleAreaList += i; |
| 297 | i++; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | if (i != nareas) { |
| 302 | qWarning() << DeviceSkin::tr(s: "Mismatch in number of areas, expected %1, got %2." ) |
| 303 | .arg(a: nareas).arg(a: i); |
| 304 | } |
| 305 | if (debugDeviceSkin) |
| 306 | qDebug() << *this; |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | // --------- CursorWindow declaration |
| 311 | |
| 312 | namespace qvfb_internal { |
| 313 | |
| 314 | class CursorWindow : public QWidget |
| 315 | { |
| 316 | public: |
| 317 | explicit CursorWindow(const QImage &cursor, QPoint hot, QWidget *sk); |
| 318 | |
| 319 | void setView(QWidget*); |
| 320 | void setPos(QPoint); |
| 321 | bool handleMouseEvent(QEvent *ev); |
| 322 | |
| 323 | protected: |
| 324 | bool event(QEvent *) override; |
| 325 | bool eventFilter(QObject*, QEvent *) override; |
| 326 | |
| 327 | private: |
| 328 | QWidget *mouseRecipient; |
| 329 | QWidget *m_view; |
| 330 | QWidget *skin; |
| 331 | QPoint hotspot; |
| 332 | }; |
| 333 | } |
| 334 | |
| 335 | // --------- Skin |
| 336 | |
| 337 | DeviceSkin::DeviceSkin(const DeviceSkinParameters ¶meters, QWidget *p ) : |
| 338 | QWidget(p), |
| 339 | m_parameters(parameters), |
| 340 | buttonRegions(parameters.buttonAreas.size(), QRegion()), |
| 341 | parent(p), |
| 342 | t_skinkey(new QTimer(this)), |
| 343 | t_parentmove(new QTimer(this)) |
| 344 | { |
| 345 | Q_ASSERT(p); |
| 346 | setMouseTracking(true); |
| 347 | setAttribute(Qt::WA_NoSystemBackground); |
| 348 | |
| 349 | setZoom(1.0); |
| 350 | connect(sender: t_skinkey, signal: &QTimer::timeout, context: this, slot: &DeviceSkin::skinKeyRepeat ); |
| 351 | t_parentmove->setSingleShot( true ); |
| 352 | connect(sender: t_parentmove, signal: &QTimer::timeout, context: this, slot: &DeviceSkin::moveParent ); |
| 353 | } |
| 354 | |
| 355 | void DeviceSkin::skinKeyRepeat() |
| 356 | { |
| 357 | if ( m_view ) { |
| 358 | const DeviceSkinButtonArea &area = m_parameters.buttonAreas[buttonIndex]; |
| 359 | emit skinKeyReleaseEvent(code: area.keyCode,text: area.text, autorep: true); |
| 360 | emit skinKeyPressEvent(code: area.keyCode, text: area.text, autorep: true); |
| 361 | t_skinkey->start(msec: key_repeat_period); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | void DeviceSkin::calcRegions() |
| 366 | { |
| 367 | const int numAreas = m_parameters.buttonAreas.size(); |
| 368 | for (int i=0; i<numAreas; i++) { |
| 369 | QPolygon xa(m_parameters.buttonAreas[i].area.size()); |
| 370 | int n = m_parameters.buttonAreas[i].area.size(); |
| 371 | for (int p = 0; p < n; p++) { |
| 372 | xa.setPoint(index: p,pt: transform.map(p: m_parameters.buttonAreas[i].area[p])); |
| 373 | } |
| 374 | if (n == 2) { |
| 375 | buttonRegions[i] = QRegion(xa.boundingRect()); |
| 376 | } else { |
| 377 | buttonRegions[i] = QRegion(xa); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void DeviceSkin::loadImages() |
| 383 | { |
| 384 | QImage iup = m_parameters.skinImageUp; |
| 385 | QImage idown = m_parameters.skinImageDown; |
| 386 | |
| 387 | QImage iclosed; |
| 388 | const bool hasClosedImage = !m_parameters.skinImageClosed.isNull(); |
| 389 | |
| 390 | if (hasClosedImage) |
| 391 | iclosed = m_parameters.skinImageClosed; |
| 392 | QImage icurs; |
| 393 | const bool hasCursorImage = !m_parameters.skinCursor.isNull(); |
| 394 | if (hasCursorImage) |
| 395 | icurs = m_parameters.skinCursor; |
| 396 | |
| 397 | if (!transform.isIdentity()) { |
| 398 | iup = iup.transformed(matrix: transform, mode: Qt::SmoothTransformation); |
| 399 | idown = idown.transformed(matrix: transform, mode: Qt::SmoothTransformation); |
| 400 | if (hasClosedImage) |
| 401 | iclosed = iclosed.transformed(matrix: transform, mode: Qt::SmoothTransformation); |
| 402 | if (hasCursorImage) |
| 403 | icurs = icurs.transformed(matrix: transform, mode: Qt::SmoothTransformation); |
| 404 | } |
| 405 | const Qt::ImageConversionFlags conv = Qt::ThresholdAlphaDither|Qt::AvoidDither; |
| 406 | skinImageUp = QPixmap::fromImage(image: iup); |
| 407 | skinImageDown = QPixmap::fromImage(image: idown, flags: conv); |
| 408 | if (hasClosedImage) |
| 409 | skinImageClosed = QPixmap::fromImage(image: iclosed, flags: conv); |
| 410 | if (hasCursorImage) |
| 411 | skinCursor = QPixmap::fromImage(image: icurs, flags: conv); |
| 412 | |
| 413 | setFixedSize( skinImageUp.size() ); |
| 414 | if (!skinImageUp.mask()) |
| 415 | skinImageUp.setMask(skinImageUp.createHeuristicMask()); |
| 416 | if (!skinImageClosed.mask()) |
| 417 | skinImageClosed.setMask(skinImageClosed.createHeuristicMask()); |
| 418 | |
| 419 | QWidget* parent = parentWidget(); |
| 420 | parent->setMask( skinImageUp.mask() ); |
| 421 | parent->setFixedSize( skinImageUp.size() ); |
| 422 | |
| 423 | delete cursorw; |
| 424 | cursorw = 0; |
| 425 | if (hasCursorImage) { |
| 426 | cursorw = new qvfb_internal::CursorWindow(m_parameters.skinCursor, m_parameters.cursorHot, this); |
| 427 | if (m_view) |
| 428 | cursorw->setView(m_view); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | DeviceSkin::~DeviceSkin( ) |
| 433 | { |
| 434 | delete cursorw; |
| 435 | } |
| 436 | |
| 437 | void DeviceSkin::setTransform(const QTransform &wm) |
| 438 | { |
| 439 | transform = QImage::trueMatrix(wm,w: m_parameters.skinImageUp.width(),h: m_parameters.skinImageUp.height()); |
| 440 | calcRegions(); |
| 441 | loadImages(); |
| 442 | if ( m_view ) { |
| 443 | QPoint p = transform.map(a: QPolygon(m_parameters.screenRect)).boundingRect().topLeft(); |
| 444 | m_view->move(p); |
| 445 | } |
| 446 | updateSecondaryScreen(); |
| 447 | } |
| 448 | |
| 449 | void DeviceSkin::setZoom( double z ) |
| 450 | { |
| 451 | setTransform(QTransform().scale(sx: z,sy: z)); |
| 452 | } |
| 453 | |
| 454 | void DeviceSkin::updateSecondaryScreen() |
| 455 | { |
| 456 | if (!m_secondaryView) |
| 457 | return; |
| 458 | if (flipped_open) { |
| 459 | if (m_parameters.backScreenRect.isNull()) { |
| 460 | m_secondaryView->hide(); |
| 461 | } else { |
| 462 | m_secondaryView->move(transform.map(a: QPolygon(m_parameters.backScreenRect)).boundingRect().topLeft()); |
| 463 | m_secondaryView->show(); |
| 464 | } |
| 465 | } else { |
| 466 | if (m_parameters.closedScreenRect.isNull()) { |
| 467 | m_secondaryView->hide(); |
| 468 | } else { |
| 469 | m_secondaryView->move(transform.map(a: QPolygon(m_parameters.closedScreenRect)).boundingRect().topLeft()); |
| 470 | m_secondaryView->show(); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | void DeviceSkin::setView( QWidget *v ) |
| 476 | { |
| 477 | m_view = v; |
| 478 | m_view->setFocus(); |
| 479 | m_view->move(transform.map(a: QPolygon(m_parameters.screenRect)).boundingRect().topLeft()); |
| 480 | if ( cursorw ) |
| 481 | cursorw->setView(v); |
| 482 | } |
| 483 | |
| 484 | void DeviceSkin::setSecondaryView( QWidget *v ) |
| 485 | { |
| 486 | m_secondaryView = v; |
| 487 | updateSecondaryScreen(); |
| 488 | } |
| 489 | |
| 490 | void DeviceSkin::paintEvent( QPaintEvent *) |
| 491 | { |
| 492 | QPainter p( this ); |
| 493 | if ( flipped_open ) { |
| 494 | p.drawPixmap(x: 0, y: 0, pm: skinImageUp); |
| 495 | } else { |
| 496 | p.drawPixmap(x: 0, y: 0, pm: skinImageClosed); |
| 497 | } |
| 498 | QList<int> toDraw; |
| 499 | if ( buttonPressed == true ) { |
| 500 | toDraw += buttonIndex; |
| 501 | } |
| 502 | for (int toggle : std::as_const(t: m_parameters.toggleAreaList)) { |
| 503 | const DeviceSkinButtonArea &ba = m_parameters.buttonAreas[toggle]; |
| 504 | if (flipped_open || ba.activeWhenClosed) { |
| 505 | if (ba.toggleArea && ba.toggleActiveArea) |
| 506 | toDraw += toggle; |
| 507 | } |
| 508 | } |
| 509 | for (int button : std::as_const(t&: toDraw)) { |
| 510 | const DeviceSkinButtonArea &ba = m_parameters.buttonAreas[button]; |
| 511 | const QRect r = buttonRegions[button].boundingRect(); |
| 512 | if ( ba.area.size() > 2 ) |
| 513 | p.setClipRegion(buttonRegions[button]); |
| 514 | p.drawPixmap( p: r.topLeft(), pm: skinImageDown, sr: r); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | void DeviceSkin::mousePressEvent( QMouseEvent *e ) |
| 519 | { |
| 520 | if (e->button() == Qt::RightButton) { |
| 521 | emit popupMenu(); |
| 522 | } else { |
| 523 | buttonPressed = false; |
| 524 | |
| 525 | onjoyrelease = -1; |
| 526 | const int numAreas = m_parameters.buttonAreas.size(); |
| 527 | for (int i = 0; i < numAreas ; i++) { |
| 528 | const DeviceSkinButtonArea &ba = m_parameters.buttonAreas[i]; |
| 529 | if ( buttonRegions[i].contains( p: e->position().toPoint() ) ) { |
| 530 | if ( flipped_open || ba.activeWhenClosed ) { |
| 531 | if ( m_parameters.joystick == i ) { |
| 532 | joydown = true; |
| 533 | } else { |
| 534 | if ( joydown ) |
| 535 | onjoyrelease = i; |
| 536 | else |
| 537 | startPress(i); |
| 538 | break; |
| 539 | if (debugDeviceSkin)// Debug message to be sure we are clicking the right areas |
| 540 | qDebug()<< m_parameters.buttonAreas[i].name << " clicked" ; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | clickPos = e->position().toPoint(); |
| 546 | // This is handy for finding the areas to define rectangles for new skins |
| 547 | if (debugDeviceSkin) |
| 548 | qDebug()<< "Clicked in " << e->position().toPoint().x() << ',' << e->position().toPoint().y(); |
| 549 | clickPos = e->position().toPoint(); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | void DeviceSkin::flip(bool open) |
| 554 | { |
| 555 | if ( flipped_open == open ) |
| 556 | return; |
| 557 | if ( open ) { |
| 558 | parent->setMask(skinImageUp.mask()); |
| 559 | emit skinKeyReleaseEvent(code: Qt::Key(Qt::Key_Flip), text: QString(), autorep: false); |
| 560 | } else { |
| 561 | parent->setMask(skinImageClosed.mask()); |
| 562 | emit skinKeyPressEvent(code: Qt::Key(Qt::Key_Flip), text: QString(), autorep: false); |
| 563 | } |
| 564 | flipped_open = open; |
| 565 | updateSecondaryScreen(); |
| 566 | repaint(); |
| 567 | } |
| 568 | |
| 569 | void DeviceSkin::startPress(int i) |
| 570 | { |
| 571 | buttonPressed = true; |
| 572 | buttonIndex = i; |
| 573 | if (m_view) { |
| 574 | const DeviceSkinButtonArea &ba = m_parameters.buttonAreas[buttonIndex]; |
| 575 | if (ba.keyCode == Qt::Key_Flip) { |
| 576 | flip(open: !flipped_open); |
| 577 | } else if (ba.toggleArea) { |
| 578 | bool active = !ba.toggleActiveArea; |
| 579 | const_cast<DeviceSkinButtonArea &>(ba).toggleActiveArea = active; |
| 580 | if (active) |
| 581 | emit skinKeyPressEvent(code: ba.keyCode, text: ba.text, autorep: false); |
| 582 | else |
| 583 | emit skinKeyReleaseEvent(code: ba.keyCode, text: ba.text, autorep: false); |
| 584 | } else { |
| 585 | emit skinKeyPressEvent(code: ba.keyCode, text: ba.text, autorep: false); |
| 586 | t_skinkey->start(msec: key_repeat_delay); |
| 587 | } |
| 588 | repaint(buttonRegions[buttonIndex].boundingRect()); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | void DeviceSkin::endPress() |
| 593 | { |
| 594 | const DeviceSkinButtonArea &ba = m_parameters.buttonAreas[buttonIndex]; |
| 595 | if (m_view && ba.keyCode != Qt::Key_Flip && !ba.toggleArea ) |
| 596 | emit skinKeyReleaseEvent(code: ba.keyCode, text: ba.text, autorep: false); |
| 597 | t_skinkey->stop(); |
| 598 | buttonPressed = false; |
| 599 | repaint( buttonRegions[buttonIndex].boundingRect() ); |
| 600 | } |
| 601 | |
| 602 | void DeviceSkin::mouseMoveEvent( QMouseEvent *e ) |
| 603 | { |
| 604 | if ( e->buttons() & Qt::LeftButton ) { |
| 605 | const int joystick = m_parameters.joystick; |
| 606 | QPoint newpos = e->globalPosition().toPoint() - clickPos; |
| 607 | if (joydown) { |
| 608 | int k1=0, k2=0; |
| 609 | if (newpos.x() < -joydistance) { |
| 610 | k1 = joystick+1; |
| 611 | } else if (newpos.x() > +joydistance) { |
| 612 | k1 = joystick+3; |
| 613 | } |
| 614 | if (newpos.y() < -joydistance) { |
| 615 | k2 = joystick+2; |
| 616 | } else if (newpos.y() > +joydistance) { |
| 617 | k2 = joystick+4; |
| 618 | } |
| 619 | if (k1 || k2) { |
| 620 | if (!buttonPressed) { |
| 621 | onjoyrelease = -1; |
| 622 | if (k1 && k2) { |
| 623 | startPress(i: k2); |
| 624 | endPress(); |
| 625 | } |
| 626 | startPress(i: k1 ? k1 : k2); |
| 627 | } |
| 628 | } else if (buttonPressed) { |
| 629 | endPress(); |
| 630 | } |
| 631 | } else if (buttonPressed == false) { |
| 632 | parentpos = newpos; |
| 633 | if (!t_parentmove->isActive()) |
| 634 | t_parentmove->start(msec: 50); |
| 635 | } |
| 636 | } |
| 637 | if ( cursorw ) |
| 638 | cursorw->setPos(e->globalPosition().toPoint()); |
| 639 | } |
| 640 | |
| 641 | void DeviceSkin::moveParent() |
| 642 | { |
| 643 | parent->move( parentpos ); |
| 644 | } |
| 645 | |
| 646 | void DeviceSkin::mouseReleaseEvent( QMouseEvent * ) |
| 647 | { |
| 648 | if ( buttonPressed ) |
| 649 | endPress(); |
| 650 | if ( joydown ) { |
| 651 | joydown = false; |
| 652 | if (onjoyrelease >= 0) { |
| 653 | startPress(i: onjoyrelease); |
| 654 | endPress(); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | bool DeviceSkin::hasCursor() const |
| 660 | { |
| 661 | return !skinCursor.isNull(); |
| 662 | } |
| 663 | |
| 664 | // ------------------ CursorWindow implementation |
| 665 | |
| 666 | namespace qvfb_internal { |
| 667 | |
| 668 | bool CursorWindow::eventFilter( QObject *, QEvent *ev) |
| 669 | { |
| 670 | handleMouseEvent(ev); |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | bool CursorWindow::event( QEvent *ev ) |
| 675 | { |
| 676 | if (handleMouseEvent(ev)) |
| 677 | return true; |
| 678 | return QWidget::event(event: ev); |
| 679 | } |
| 680 | |
| 681 | bool CursorWindow::handleMouseEvent(QEvent *ev) |
| 682 | { |
| 683 | bool handledEvent = false; |
| 684 | static int inhere=0; |
| 685 | if ( !inhere ) { |
| 686 | inhere++; |
| 687 | if (m_view) { |
| 688 | if (ev->type() >= QEvent::MouseButtonPress && ev->type() <= QEvent::MouseMove) { |
| 689 | QMouseEvent *e = (QMouseEvent*)ev; |
| 690 | QPoint gp = e->globalPosition().toPoint(); |
| 691 | QPoint vp = m_view->mapFromGlobal(gp); |
| 692 | QPoint sp = skin->mapFromGlobal(gp); |
| 693 | if (e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonDblClick) { |
| 694 | if (m_view->rect().contains(p: vp)) |
| 695 | mouseRecipient = m_view; |
| 696 | else if (skin->parentWidget()->geometry().contains(p: gp)) |
| 697 | mouseRecipient = skin; |
| 698 | else |
| 699 | mouseRecipient = 0; |
| 700 | } |
| 701 | if (mouseRecipient) { |
| 702 | setPos(gp); |
| 703 | QMouseEvent me(e->type(),mouseRecipient==skin ? sp : vp,gp,e->button(),e->buttons(),e->modifiers()); |
| 704 | QApplication::sendEvent(receiver: mouseRecipient, event: &me); |
| 705 | } else if (!skin->parentWidget()->geometry().contains(p: gp)) { |
| 706 | hide(); |
| 707 | } else { |
| 708 | setPos(gp); |
| 709 | } |
| 710 | if (e->type() == QEvent::MouseButtonRelease) |
| 711 | mouseRecipient = 0; |
| 712 | handledEvent = true; |
| 713 | } |
| 714 | } |
| 715 | inhere--; |
| 716 | } |
| 717 | return handledEvent; |
| 718 | } |
| 719 | |
| 720 | void CursorWindow::setView(QWidget* v) |
| 721 | { |
| 722 | if ( m_view ) { |
| 723 | m_view->removeEventFilter(obj: this); |
| 724 | m_view->removeEventFilter(obj: this); |
| 725 | } |
| 726 | m_view = v; |
| 727 | m_view->installEventFilter(filterObj: this); |
| 728 | m_view->installEventFilter(filterObj: this); |
| 729 | mouseRecipient = 0; |
| 730 | } |
| 731 | |
| 732 | CursorWindow::CursorWindow(const QImage &img, QPoint hot, QWidget* sk) |
| 733 | : QWidget(0), |
| 734 | m_view(0), |
| 735 | skin(sk), |
| 736 | hotspot(hot) |
| 737 | { |
| 738 | setWindowFlags( Qt::FramelessWindowHint ); |
| 739 | mouseRecipient = 0; |
| 740 | setMouseTracking(true); |
| 741 | #ifndef QT_NO_CURSOR |
| 742 | setCursor(Qt::BlankCursor); |
| 743 | #endif |
| 744 | QPixmap p; |
| 745 | p = QPixmap::fromImage(image: img); |
| 746 | if (!p.mask()) { |
| 747 | QBitmap bm = img.hasAlphaChannel() ? QBitmap::fromImage(image: img.createAlphaMask()) |
| 748 | : QBitmap::fromImage(image: img.createHeuristicMask()); |
| 749 | p.setMask(bm); |
| 750 | } |
| 751 | QPalette palette; |
| 752 | palette.setBrush(acr: backgroundRole(), abrush: QBrush(p)); |
| 753 | setPalette(palette); |
| 754 | setFixedSize( p.size() ); |
| 755 | if ( !p.mask().isNull() ) |
| 756 | setMask(p.mask()); |
| 757 | } |
| 758 | |
| 759 | void CursorWindow::setPos(QPoint p) |
| 760 | { |
| 761 | move(p-hotspot); |
| 762 | show(); |
| 763 | raise(); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | #ifdef TEST_SKIN |
| 768 | |
| 769 | int main(int argc,char *argv[]) |
| 770 | { |
| 771 | if (argc < 1) |
| 772 | return 1; |
| 773 | const QString skinFile = QString::fromUtf8(argv[1]); |
| 774 | QApplication app(argc,argv); |
| 775 | QMainWindow mw; |
| 776 | |
| 777 | DeviceSkinParameters params; |
| 778 | QString errorMessage; |
| 779 | if (!params.read(skinFile, DeviceSkinParameters::ReadAll, &errorMessage)) { |
| 780 | qWarning() << errorMessage; |
| 781 | return 1; |
| 782 | } |
| 783 | DeviceSkin ds(params, &mw); |
| 784 | // View Dialog |
| 785 | QDialog *dialog = new QDialog(); |
| 786 | QHBoxLayout *dialogLayout = new QHBoxLayout(); |
| 787 | dialog->setLayout(dialogLayout); |
| 788 | QDialogButtonBox *dialogButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); |
| 789 | QObject::connect(dialogButtonBox, &QDialogButtonBox::rejected, dialog, &QDialog::reject); |
| 790 | QObject::connect(dialogButtonBox, &QDialogButtonBox::accepted, dialog, &QDialog::accept); |
| 791 | dialogLayout->addWidget(dialogButtonBox); |
| 792 | dialog->setFixedSize(params.screenSize()); |
| 793 | dialog->setParent(&ds, Qt::SubWindow); |
| 794 | dialog->setAutoFillBackground(true); |
| 795 | ds.setView(dialog); |
| 796 | |
| 797 | QObject::connect(&ds, &DeviceSkin::popupMenu, &mw, &QWidget::close); |
| 798 | QObject::connect(&ds, &DeviceSkin::skinKeyPressEvent, &mw, &QWidget::close); |
| 799 | mw.show(); |
| 800 | return app.exec(); |
| 801 | } |
| 802 | |
| 803 | #endif |
| 804 | |
| 805 | QT_END_NAMESPACE |
| 806 | |
| 807 | |