PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mscore/help.cpp

https://gitlab.com/Red54/MuseScore
C++ | 229 lines | 164 code | 30 blank | 35 comment | 33 complexity | da521370ebdbe3a7dbfa74bacd2b4fe8 MD5 | raw file
  1. //=============================================================================
  2. // MuseScore
  3. // Music Composition & Notation
  4. //
  5. // Copyright (C) 2015 Werner Schweer
  6. //
  7. // This program is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License version 2
  9. // as published by the Free Software Foundation and appearing in
  10. // the file LICENCE.GPL
  11. //=============================================================================
  12. #include "help.h"
  13. #include "helpBrowser.h"
  14. #include "musescore.h"
  15. #include "scoreview.h"
  16. namespace Ms {
  17. //---------------------------------------------------------
  18. // HelpQuery
  19. //---------------------------------------------------------
  20. HelpQuery::HelpQuery(QWidget* parent)
  21. : QWidgetAction(parent)
  22. {
  23. mapper = new QSignalMapper(this);
  24. w = new QWidget(parent);
  25. QHBoxLayout* layout = new QHBoxLayout;
  26. QLabel* label = new QLabel;
  27. label->setText(tr("Search for: "));
  28. layout->addWidget(label);
  29. entry = new QLineEdit;
  30. layout->addWidget(entry);
  31. QToolButton* button = new QToolButton;
  32. button->setText("x");
  33. layout->addWidget(button);
  34. w->setLayout(layout);
  35. setDefaultWidget(w);
  36. emptyState = true;
  37. connect(button, SIGNAL(clicked()), entry, SLOT(clear()));
  38. connect(entry, SIGNAL(textChanged(const QString&)), SLOT(textChanged(const QString&)));
  39. connect(entry, SIGNAL(returnPressed()), SLOT(returnPressed()));
  40. connect(mapper, SIGNAL(mapped(QObject*)), SLOT(actionTriggered(QObject*)));
  41. }
  42. //---------------------------------------------------------
  43. // setFocus
  44. //---------------------------------------------------------
  45. void HelpQuery::setFocus()
  46. {
  47. entry->clear();
  48. entry->setFocus();
  49. }
  50. //---------------------------------------------------------
  51. // textChanged
  52. //---------------------------------------------------------
  53. void HelpQuery::textChanged(const QString& ss)
  54. {
  55. QString s = ss.toLower();
  56. QWidget* menu = static_cast<QWidget*>(parent());
  57. if (s.isEmpty()) {
  58. if (!emptyState) { // restore old menu entries
  59. QList<QAction*> al = menu->actions();
  60. for (QAction* a : al) {
  61. if (a != this)
  62. menu->removeAction(a);
  63. }
  64. for (QAction* a : actions) {
  65. if (a != this)
  66. menu->addAction(a);
  67. }
  68. }
  69. emptyState = true;
  70. return;
  71. }
  72. if (emptyState)
  73. actions = menu->actions();
  74. for (QAction* a : menu->actions()) {
  75. if (a != this)
  76. menu->removeAction(a);
  77. }
  78. emptyState = false;
  79. if (!mscore->helpEngine())
  80. return;
  81. QMap<QString,QUrl>list = mscore->helpEngine()->linksForIdentifier(s);
  82. // QMap<QString,QUrl>list = mscore->helpEngine()->indexModel()->linksForKeyword(s);
  83. int k = 0;
  84. for (auto i = list.begin(); i != list.end(); ++i) {
  85. QAction* action = new QAction(i.key(), this);
  86. action->setData(i.value());
  87. // printf("add action <%s> <%s>\n", qPrintable(i.key()), qPrintable(i.value().toString()));
  88. menu->addAction(action);
  89. connect(action, SIGNAL(triggered()), mapper, SLOT(map()));
  90. mapper->setMapping(action, action);
  91. if (++k > 10)
  92. break;
  93. }
  94. }
  95. //---------------------------------------------------------
  96. // actionTriggered
  97. //---------------------------------------------------------
  98. void HelpQuery::actionTriggered(QObject* obj)
  99. {
  100. QAction* action = static_cast<QAction*>(obj);
  101. if (action->data().isNull())
  102. return;
  103. QUrl url = action->data().toUrl();
  104. if (url.isValid())
  105. mscore->showHelp(url);
  106. else
  107. qDebug("actionTriggered: bad url");
  108. entry->clear();
  109. }
  110. //---------------------------------------------------------
  111. // return pressed
  112. //---------------------------------------------------------
  113. void HelpQuery::returnPressed()
  114. {
  115. QHelpEngine* he = mscore->helpEngine();
  116. if (!he)
  117. return;
  118. QMap<QString,QUrl>list = he->linksForIdentifier(entry->text().toLower());
  119. if (!list.isEmpty()) {
  120. mscore->showHelp(list.begin().value());
  121. }
  122. entry->clear();
  123. }
  124. //---------------------------------------------------------
  125. // showHelp
  126. // show local help
  127. //---------------------------------------------------------
  128. void MuseScore::showHelp(const QUrl& url)
  129. {
  130. qDebug("showHelp <%s>", qPrintable(url.toString()));
  131. if (!_helpEngine)
  132. return;
  133. QAction* a = getAction("local-help");
  134. a->blockSignals(true);
  135. a->setChecked(true);
  136. a->blockSignals(false);
  137. if (!helpBrowser) {
  138. helpBrowser = new HelpBrowser;
  139. manualDock = new QDockWidget(tr("Manual"), 0);
  140. manualDock->setObjectName("Manual");
  141. manualDock->setWidget(helpBrowser);
  142. Qt::DockWidgetArea area = Qt::RightDockWidgetArea;
  143. addDockWidget(area, manualDock);
  144. }
  145. manualDock->show();
  146. helpBrowser->setContent(url);
  147. }
  148. void MuseScore::showHelp(QString s)
  149. {
  150. qDebug("showHelp <%s>", qPrintable(s));
  151. s = s.toLower();
  152. if (!s.isEmpty()) {
  153. QString help = QString("https://musescore.org/redirect/help?tag=%1&locale=%2").arg(s).arg(getLocaleISOCode());
  154. help += QString("&utm_source=desktop&utm_medium=contextual&utm_content=%1&utm_term=%2&utm_campaign=MuseScore%3").arg(rev.trimmed()).arg(s).arg(QString(VERSION));
  155. QDesktopServices::openUrl(QUrl(help));
  156. }
  157. #if 0
  158. if (!_helpEngine) {
  159. qDebug("no help available");
  160. return;
  161. }
  162. s = s.toLower();
  163. qDebug("showHelp <%s>", qPrintable(s));
  164. QMap<QString,QUrl>list = _helpEngine->linksForIdentifier(s);
  165. if (!list.isEmpty())
  166. showHelp(*list.begin());
  167. else {
  168. qDebug("help for <%s> not found", qPrintable(s));
  169. QMap<QString,QUrl>list = _helpEngine->linksForIdentifier("manual");
  170. if (!list.isEmpty())
  171. showHelp(*list.begin());
  172. }
  173. #endif
  174. }
  175. //---------------------------------------------------------
  176. // showContextHelp
  177. //---------------------------------------------------------
  178. void MuseScore::showContextHelp()
  179. {
  180. QString s;
  181. QWidget* w = qApp->widgetAt(globalX, globalY);
  182. while (w) {
  183. if (!w->statusTip().isEmpty()) {
  184. s = w->statusTip();
  185. break;
  186. }
  187. w = w->parentWidget();
  188. }
  189. if (w && s == "scoreview") {
  190. ScoreView* canvas = static_cast<ScoreView*>(w);
  191. QPoint pt = w->mapFromGlobal(QPoint(globalX, globalY));
  192. QPointF p = canvas->toLogical(pt);
  193. Element* e = canvas->elementNear(p);
  194. if (e)
  195. s = QString("element:%1").arg(e->name());
  196. }
  197. showHelp(s);
  198. }
  199. } // end namespace Ms