File: tourhandler.cpp

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-21
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 219,852 kB
  • sloc: cpp: 291,412; xml: 200,226; sh: 3,779; ansic: 1,447; python: 393; makefile: 246; perl: 82; pascal: 79
file content (492 lines) | stat: -rw-r--r-- 17,726 bytes parent folder | download | duplicates (5)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include "tourhandler.h"
#include "musescore.h"
#include "preferences.h"

namespace Ms {

QHash<QString, Tour*> TourHandler::allTours;
QHash<QString, Tour*> TourHandler::shortcutToTour;
QMap<QString, QMap<QString, QString>*> TourHandler::eventNameLookup;

//---------------------------------------------------------
//   OverlayWidget
//---------------------------------------------------------

OverlayWidget::OverlayWidget(QList<QWidget*> widgetList, QWidget* parent)
      : QWidget{parent}
      {
      widgets = widgetList;
      newParent();
      }

//---------------------------------------------------------
//   newParent
//---------------------------------------------------------

void OverlayWidget::newParent()
      {
      if (!parent())
            return;
      parent()->installEventFilter(this);
      resize(qobject_cast<QWidget*>(parent())->size());
      raise();
      }

//---------------------------------------------------------
//   eventFilter
//---------------------------------------------------------

bool OverlayWidget::eventFilter(QObject* obj, QEvent* ev)
      {
      if (obj == parent()) {
            if (ev->type() == QEvent::Resize)
                  resize(static_cast<QResizeEvent*>(ev)->size());
            else if (ev->type() == QEvent::ChildAdded)
                  raise();
            }
      return QWidget::eventFilter(obj, ev);
      }

//---------------------------------------------------------
//   event
//---------------------------------------------------------

bool OverlayWidget::event(QEvent* ev)
      {
      if (ev->type() == QEvent::ParentAboutToChange) {
            if (parent()) parent()->removeEventFilter(this);
            }
      else if (ev->type() == QEvent::ParentChange)
            newParent();
      return QWidget::event(ev);
      }

//---------------------------------------------------------
//   paintEvent
//---------------------------------------------------------

void OverlayWidget::paintEvent(QPaintEvent *)
      {
      QPainterPath painterPath = QPainterPath();
      QPainter p(this);
      QWidget* parentWindow = qobject_cast<QWidget*>(parent());
      if (parentWindow)
            painterPath.addRect(parentWindow->rect());

      QPainterPath subPath = QPainterPath();
      for (QWidget* w : widgets) {
            if (w->isVisible()) {
                  // Add widget and children visible region mapped to the parentWindow
                  QRegion region = w->visibleRegion() + w->childrenRegion();
                  region.translate(w->mapTo(parentWindow, QPoint(0, 0)));
                  subPath.addRegion(region);
                  }
            }
      painterPath -= subPath;

      QColor overlayColor = QApplication::palette().color(QPalette::Shadow);
      overlayColor.setAlpha(128);
      p.fillPath(painterPath, overlayColor);
      }

//---------------------------------------------------------
//   showWelcomeTour
//---------------------------------------------------------

void TourHandler::showWelcomeTour()
      {
      if (!delayedWelcomeTour)
            startTour("welcome");
      }

//---------------------------------------------------------
//   showDelayedWelcomeTour
//   delays showing the welcome tour when the user
//   attempts to open a score or create a new score
//---------------------------------------------------------

void TourHandler::showDelayedWelcomeTour()
      {
      if (delayedWelcomeTour)
            startTour("welcome");
      delayedWelcomeTour = false;
      }

//---------------------------------------------------------
//   loadTours
//---------------------------------------------------------

void TourHandler::loadTours()
      {
      QStringList nameFilters;
      nameFilters << "*.tour";
      QString path = mscoreGlobalShare + "tours";
      QDirIterator it(path, nameFilters, QDir::Files, QDirIterator::Subdirectories);

      while (it.hasNext()) {
            QFile* tourFile = new QFile(it.next());
            tourFile->open(QIODevice::ReadOnly);
            XmlReader tourXml(tourFile);
            while (tourXml.readNextStartElement()) {
                  if (tourXml.name() == "Tour")
                        loadTour(tourXml);
                  else
                        tourXml.unknown();
                  }
            }

      readCompletedTours();
      }

//---------------------------------------------------------
//   loadTours
//---------------------------------------------------------

void TourHandler::loadTour(XmlReader& tourXml)
      {
      QString tourName = tourXml.attribute("name");
      QList<QString> shortcuts;
      Tour* tour = new Tour(tourName);
      while (tourXml.readNextStartElement()) {
            if (tourXml.name() == "Message") {
                  QString text;
                  QList<QString> objectNames;
                  while (tourXml.readNextStartElement()) {
                        if (tourXml.name() == "Text") {
                              QTextDocument doc;
                              QString rawText = tourXml.readElementText();
                              QString ttext = qApp->translate("TourXML", rawText.toUtf8().constData());
                              doc.setHtml(ttext);
                              text = doc.toPlainText().replace("\\n", "\n");
                              }
                        else if (tourXml.name() == "Widget")
                              objectNames.append(tourXml.readXml());
                        else
                              tourXml.unknown();
                        }
                  tour->addMessage(text, objectNames);
                  }
            else if (tourXml.name() == "Event") {
                  QString name = tourXml.attribute("objectName");
                  QString event = tourXml.readXml();
                  if (!eventNameLookup.contains(name))
                        eventNameLookup.insert(name, new QMap<QString, QString>);
                  eventNameLookup.value(name)->insert(event, tourName);
                  }
            else if (tourXml.name() == "Shortcut") {
                  shortcuts.append(tourXml.readXml());
                  }
            else
                  tourXml.unknown();
            }

      allTours[tourName] = tour;
      for (QString s : shortcuts)
            shortcutToTour[s] = tour;
      }

//---------------------------------------------------------
//   resetCompletedTours
//---------------------------------------------------------

void TourHandler::resetCompletedTours()
      {
      for (auto tour : allTours)
            tour->setCompleted(false);
      }

//---------------------------------------------------------
//   readCompletedTours
//---------------------------------------------------------

void TourHandler::readCompletedTours()
      {
      QFile completedToursFile(dataPath + "/tours/completedTours.list");
      if (!completedToursFile.open(QIODevice::ReadOnly))
            return;

      QDataStream in(&completedToursFile);
      QList<QString> completedTours;
      in >> completedTours;

      for (QString tourName : completedTours)
            if (allTours.contains(tourName))
                  allTours.value(tourName)->setCompleted(true);
      }

//---------------------------------------------------------
//   writeCompletedTours
//---------------------------------------------------------

void TourHandler::writeCompletedTours()
      {
      QDir dir;
      dir.mkpath(dataPath);
      QString path = dataPath + "/tours";
      dir.mkpath(path);
      QFile completedToursFile(path + "/completedTours.list");
      completedToursFile.open(QIODevice::WriteOnly);

      QList<QString> completedTours;

      for (Tour* t : allTours.values())
            if (t->completed())
                  completedTours.append(t->tourName());

      QDataStream out(&completedToursFile);
      out << completedTours;
      }

//---------------------------------------------------------
//   eventFilter
//---------------------------------------------------------

bool TourHandler::eventFilter(QObject* obj, QEvent* event)
      {
      QString eventString = QVariant::fromValue(event->type()).value<QString>();

      if (eventNameLookup.contains(obj->objectName()) &&
          eventNameLookup.value(obj->objectName())->contains(eventString))
            startTour(eventNameLookup.value(obj->objectName())->value(eventString));
      else if (eventHandler.contains(obj) && eventHandler.value(obj)->contains(event->type()))
            startTour(eventHandler.value(obj)->value(event->type()));

      return QObject::eventFilter(obj, event);
      }

//---------------------------------------------------------
//   attachTour
//---------------------------------------------------------

void TourHandler::attachTour(QObject* obj, QEvent::Type eventType, QString tourName)
      {
      obj->installEventFilter(this);
      if (!eventHandler.contains(obj))
            eventHandler.insert(obj, new QMap<QEvent::Type, QString>);
      eventHandler.value(obj)->insert(eventType, tourName);
      }

//---------------------------------------------------------
//   addWidgetToTour
//---------------------------------------------------------

void TourHandler::addWidgetToTour(QString tourName, QWidget* widget, QString widgetName)
      {
      if (allTours.contains(tourName)) {
            Tour* tour = allTours.value(tourName);
            tour->addNameAndWidget(widgetName, widget);
            }
      else
            qDebug() << tourName << "does not have a tour.";
      }

//---------------------------------------------------------
//   clearWidgetsFromTour
//---------------------------------------------------------

void TourHandler::clearWidgetsFromTour(QString tourName)
      {
      if (allTours.contains(tourName))
            allTours.value(tourName)->clearWidgets();
      else
            qDebug() << tourName << "does not have a tour.";
      }

//---------------------------------------------------------
//   startTour
//   lookup string can be a tour name or a shortcut name
//---------------------------------------------------------

void TourHandler::startTour(QString lookupString)
      {
      if (!preferences.getBool(PREF_UI_APP_STARTUP_SHOWTOURS) || MScore::noGui)
            return;

      Tour* tour = nullptr;
      if (allTours.contains(lookupString))
            tour = allTours.value(lookupString);
      else if (shortcutToTour.contains(lookupString))
            tour = shortcutToTour.value(lookupString);

      if (tour) {
            if (tour->completed())
                  return;
            displayTour(tour);
            tour->setCompleted(true);
            }
      }

//---------------------------------------------------------
//   getDisplayPoints
//---------------------------------------------------------

void TourHandler::positionMessage(QList<QWidget*> widgets, QMessageBox* mbox)
      {
      // Loads some information into the size of the mbox, a bit of a hack
      mbox->show();

      // Create a "box" to see where the msgbox should go
      bool set = false;
      QRect widgetsBox;

      for (QWidget* w : widgets) {
            if (w->visibleRegion().isEmpty())
                  continue;
            QRect boundingRect = w->visibleRegion().boundingRect();
            QPoint topLeft = w->mapToGlobal(QPoint(0, 0));
            QPoint bottomRight = w->mapToGlobal(boundingRect.bottomRight());

            if (!set) {
                  widgetsBox.setTopLeft(topLeft);
                  widgetsBox.setBottomRight(bottomRight);
                  set = true;
                  }
            else {
                  widgetsBox.setTop(qMin(widgetsBox.top(), topLeft.y()));
                  widgetsBox.setLeft(qMin(widgetsBox.left(), topLeft.x()));
                  widgetsBox.setBottom(qMax(widgetsBox.bottom(), bottomRight.y()));
                  widgetsBox.setRight(qMax(widgetsBox.right(), bottomRight.x()));
                  }
            }
      if (!set)
            return; // Should display in center

      // Next find where the mbox goes around the widgetsBox
      QWidget* mainWindow = widgets.at(0)->window();
      int midX = mainWindow->mapToGlobal(QPoint(mainWindow->frameGeometry().width() / 2, 0)).x();
      int midY = mainWindow->mapToGlobal(QPoint(0, mainWindow->frameGeometry().height() / 2)).y();

      // The longer side decides which side the mbox goes on.
      bool topBottom = (widgetsBox.height() < widgetsBox.width());

      // Calculate the topLeft point for the mbox
      QPoint displayPoint(0, 0);
      if (topBottom) {
            bool displayAbove = (widgetsBox.center().y() > midY);
            if (displayAbove) {
                  int mBoxHeight = mbox->size().height() + 15; // hack
                  int y = widgetsBox.top();
                  displayPoint.setY(y - mBoxHeight);
                  }
            else
                  displayPoint.setY(widgetsBox.bottom());

            int x = (int) (widgetsBox.width() - mbox->size().width()) / 2 + widgetsBox.left();
            displayPoint.setX(x);
            }
      else {
            bool displayLeft = (widgetsBox.center().x() > midX);
            if (displayLeft) {
                  int mBoxWidth = mbox->size().width();
                  int x = widgetsBox.left();
                  displayPoint.setX(x - mBoxWidth);
                  }
            else
                  displayPoint.setX(widgetsBox.right());

            int y = (widgetsBox.height() - mbox->size().height()) / 2 + widgetsBox.top();
            displayPoint.setY(y);
            }

      // Make sure the box is within the screen
      QRect screenGeometry = QGuiApplication::primaryScreen()->geometry();
      displayPoint.setX(qMax(displayPoint.x(), 0));
      displayPoint.setY(qMax(displayPoint.y(), 0));
      displayPoint.setX(qMin(displayPoint.x(), screenGeometry.width() - mbox->size().width()));
      displayPoint.setY(qMin(displayPoint.y(), screenGeometry.height() - mbox->size().height() - 15));

      mbox->move(displayPoint);
      }

//---------------------------------------------------------
//   displayTour
//---------------------------------------------------------

QList<QWidget*> TourHandler::getWidgetsByNames(Tour* tour, QList<QString> names)
      {
      QList<QWidget*> widgets;
      for (QString name : names) {
            // First check internal storage for widget
            if (tour->hasNameForWidget(name))
                  widgets.append(tour->getWidgetsByName(name));
            else {
                  // If not found, check all widgets by object name
                  auto foundWidgets = mscore->findChildren<QWidget*>(name);
                  widgets.append(foundWidgets);
                  }
            }
      return widgets;
      }

//---------------------------------------------------------
//   displayTour
//---------------------------------------------------------

void TourHandler::displayTour(Tour* tour)
      {
      int i = 0;
      bool next = true;
      bool showTours = true;
      QList<TourMessage> tourMessages = tour->messages();
      while (i != tourMessages.size()) {
            // Set up the message box buttons
            QMessageBox* mbox = new QMessageBox(mscore);
            mbox->setWindowTitle(tr("Tour"));
            QPushButton* backButton = nullptr;
            QPushButton* nextButton = nullptr;
            QPushButton* closeButton = nullptr;

            //QMessageBox doesn't support next/back semantic for various OS styles. QWizard does.
            closeButton = mbox->addButton(tr("Close"), QMessageBox::RejectRole);
            if (i != 0)
                  backButton = mbox->addButton(tr("Back"), QMessageBox::NoRole); //Explicit text is bad since it varies depending on the OS. MacOS uses "Go back"
            if (i != tourMessages.size() - 1)
                  nextButton = mbox->addButton(tr("Next"), QMessageBox::YesRole); //MacOS uses "Continue"
            else
                  nextButton = mbox->addButton(tr("End"), QMessageBox::YesRole); // MacOS uses "Done"

            // Sets default to last pressed button
            if (next)
                  mbox->setDefaultButton(nextButton);
            else
                  mbox->setDefaultButton(backButton);
            mbox->setEscapeButton(closeButton);

            // Add text (translation?)
            mbox->setText(tourMessages[i].message);

            // Add checkbox to show tours
            QCheckBox* showToursBox = new QCheckBox(tr("Continue showing tours"), mbox);
            showToursBox->setChecked(showTours);
            mbox->setCheckBox(showToursBox);

            // Display the message box, position it if needed
            QList<QWidget*> tourWidgets = getWidgetsByNames(tour, tourMessages[i].widgetNames);
            OverlayWidget* overlay = new OverlayWidget(tourWidgets);
            if (tourWidgets.isEmpty())
                  overlay->setParent(mscore);
            else {
                  overlay->setParent(tourWidgets.at(0)->window());
                  positionMessage(tourWidgets, mbox);
                  }
            overlay->show();
            mbox->exec();
            overlay->hide();
            showTours = showToursBox->isChecked();

            // Handle the button presses
            if (mbox->clickedButton() == nextButton) {
                  i++;
                  next = true;
                  }
            else if (mbox->clickedButton() == backButton) {
                  i--;
                  next = false;
                  }
            else
                  break;
            }
      preferences.setPreference(PREF_UI_APP_STARTUP_SHOWTOURS, showTours);
      }

}