LCOV - code coverage report
Current view: top level - muse/framework/ui/api - navigationapi.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 0.0 % 142 0
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2021 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : #include "navigationapi.h"
      23              : 
      24              : #include <QElapsedTimer>
      25              : #include <QEventLoop>
      26              : #include <QTimer>
      27              : 
      28              : #include "navigationcommands.h"
      29              : 
      30              : #include "log.h"
      31              : 
      32              : using namespace muse::api;
      33              : using namespace muse::ui;
      34              : 
      35            0 : NavigationApi::NavigationApi(api::IApiEngine* e)
      36            0 :     : api::ApiObject(e)
      37              : {
      38            0 : }
      39              : 
      40            0 : NavigationApi::~NavigationApi()
      41              : {
      42            0 : }
      43              : 
      44            0 : void NavigationApi::nextPanel()
      45              : {
      46            0 :     dispatcher()->dispatch(NEXT_PANEL_COMMAND);
      47            0 : }
      48              : 
      49            0 : void NavigationApi::prevPanel()
      50              : {
      51            0 :     dispatcher()->dispatch(PREV_PANEL_COMMAND);
      52            0 : }
      53              : 
      54            0 : void NavigationApi::right()
      55              : {
      56            0 :     dispatcher()->dispatch(RIGHT_COMMAND);
      57            0 : }
      58              : 
      59            0 : void NavigationApi::left()
      60              : {
      61            0 :     dispatcher()->dispatch(LEFT_COMMAND);
      62            0 : }
      63              : 
      64            0 : void NavigationApi::up()
      65              : {
      66            0 :     dispatcher()->dispatch(UP_COMMAND);
      67            0 : }
      68              : 
      69            0 : void NavigationApi::down()
      70              : {
      71            0 :     dispatcher()->dispatch(DOWN_COMMAND);
      72            0 : }
      73              : 
      74            0 : void NavigationApi::escape()
      75              : {
      76            0 :     dispatcher()->dispatch(ESCAPE_COMMAND);
      77            0 : }
      78              : 
      79              : static constexpr int WAIT_POLL_INTERVAL_MSEC = 50;
      80              : 
      81            0 : bool NavigationApi::waitFor(const std::function<bool()>& condition, int timeoutMsec) const
      82              : {
      83            0 :     QElapsedTimer elapsed;
      84            0 :     elapsed.start();
      85              : 
      86              :     while (true) {
      87            0 :         if (condition()) {
      88            0 :             return true;
      89              :         }
      90              : 
      91            0 :         if (elapsed.elapsed() >= timeoutMsec) {
      92            0 :             return false;
      93              :         }
      94              : 
      95              :         //! NOTE Let the UI update (load QML, fill models) while waiting
      96            0 :         QEventLoop loop;
      97            0 :         bool polled = false;
      98            0 :         QTimer::singleShot(WAIT_POLL_INTERVAL_MSEC, &loop, [&loop, &polled]() {
      99            0 :             polled = true;
     100            0 :             loop.quit();
     101            0 :         });
     102            0 :         loop.exec();
     103              : 
     104              :         //! NOTE The loop was interrupted, for example, the application is exiting
     105            0 :         if (!polled) {
     106            0 :             return false;
     107              :         }
     108            0 :     }
     109              : }
     110              : 
     111            0 : bool NavigationApi::waitControl(const QString& section, const QString& panel, const QString& controlName, int timeoutMsec)
     112              : {
     113            0 :     const std::string sectionName = section.toStdString();
     114            0 :     const std::string panelName = panel.toStdString();
     115            0 :     const std::string name = controlName.toStdString();
     116              : 
     117            0 :     return waitFor([this, &sectionName, &panelName, &name]() {
     118            0 :         const INavigationControl* control = navigation()->findControl(sectionName, panelName, name);
     119            0 :         return control && control->enabled();
     120            0 :     }, timeoutMsec);
     121            0 : }
     122              : 
     123            0 : bool NavigationApi::goToControl(const QString& section, const QString& panel, const QJSValue& controlNameOrIndex, int timeoutMsec)
     124              : {
     125            0 :     if (timeoutMsec > 0) {
     126            0 :         bool waited = false;
     127            0 :         if (controlNameOrIndex.isString()) {
     128            0 :             waited = waitControl(section, panel, controlNameOrIndex.toString(), timeoutMsec);
     129              :         } else {
     130            0 :             const std::string sectionName = section.toStdString();
     131            0 :             const std::string panelName = panel.toStdString();
     132            0 :             waited = waitFor([this, &sectionName, &panelName]() {
     133            0 :                 const INavigationPanel* p = navigation()->findPanel(sectionName, panelName);
     134            0 :                 return p && p->enabled();
     135              :             }, timeoutMsec);
     136            0 :         }
     137              : 
     138            0 :         if (!waited) {
     139            0 :             LOGW() << "timeout " << timeoutMsec << " msec, waited for an enabled control: " << controlNameOrIndex.toString()
     140            0 :                    << ", panel: " << panel << ", section: " << section;
     141              :         }
     142              :     }
     143              : 
     144            0 :     bool ok = false;
     145            0 :     if (controlNameOrIndex.isString()) {
     146            0 :         ok = navigation()->requestActivateByName(section.toStdString(), panel.toStdString(), controlNameOrIndex.toString().toStdString());
     147            0 :     } else if (controlNameOrIndex.isArray()) {
     148            0 :         if (controlNameOrIndex.property("length").toInt() == 2) {
     149            0 :             INavigation::Index idx;
     150            0 :             idx.row = controlNameOrIndex.property(0).toInt();
     151            0 :             idx.column = controlNameOrIndex.property(1).toInt();
     152            0 :             ok = navigation()->requestActivateByIndex(section.toStdString(), panel.toStdString(), idx);
     153              :         } else {
     154            0 :             LOGE() << "bad argument `control`: " << controlNameOrIndex.toString();
     155            0 :             ok = false;
     156              :         }
     157              :     } else {
     158            0 :         LOGE() << "bad argument `control`: " << controlNameOrIndex.toString();
     159            0 :         ok = false;
     160              :     }
     161              : 
     162            0 :     return ok;
     163              : }
     164              : 
     165            0 : void NavigationApi::trigger()
     166              : {
     167            0 :     dispatcher()->dispatch(TRIGGER_CONTROL_COMMAND);
     168            0 : }
     169              : 
     170            0 : bool NavigationApi::triggerControl(const QString& section, const QString& panel, const QJSValue& controlNameOrIndex,
     171              :                                    int timeoutMsec)
     172              : {
     173            0 :     bool ok = goToControl(section, panel, controlNameOrIndex, timeoutMsec);
     174            0 :     if (ok) {
     175            0 :         trigger();
     176              :     }
     177            0 :     return ok;
     178              : }
     179              : 
     180            0 : QString NavigationApi::activeSection() const
     181              : {
     182            0 :     INavigationSection* sec = navigation()->activeSection();
     183            0 :     return sec ? sec->name() : QString();
     184              : }
     185              : 
     186            0 : QString NavigationApi::activePanel() const
     187              : {
     188            0 :     INavigationPanel* p = navigation()->activePanel();
     189            0 :     return p ? p->name() : QString();
     190              : }
     191              : 
     192            0 : QString NavigationApi::activeControl() const
     193              : {
     194            0 :     INavigationControl* c = navigation()->activeControl();
     195            0 :     return c ? c->name() : QString();
     196              : }
     197              : 
     198            0 : static QJSValue toQJSValue(const INavigation* n, IApiEngine* e)
     199              : {
     200            0 :     QJSValue o = e->newObject();
     201            0 :     o.setProperty("name", n->name());
     202              : 
     203            0 :     QJSValue io = e->newObject();
     204            0 :     io.setProperty("column", n->index().column);
     205            0 :     io.setProperty("row", n->index().row);
     206            0 :     o.setProperty("index", io);
     207              : 
     208            0 :     o.setProperty("enabled", n->enabled());
     209            0 :     o.setProperty("active", n->active());
     210              : 
     211            0 :     return o;
     212            0 : }
     213              : 
     214            0 : QJSValue NavigationApi::sections() const
     215              : {
     216            0 :     const std::set<INavigationSection*>& sects = navigation()->sections();
     217            0 :     QJSValue arr = engine()->newArray(sects.size());
     218            0 :     quint32 i = 0;
     219            0 :     for (const INavigationSection* s : sects) {
     220            0 :         arr.setProperty(i, toQJSValue(s, engine()));
     221            0 :         ++i;
     222              :     }
     223              : 
     224            0 :     return arr;
     225            0 : }
     226              : 
     227            0 : QJSValue NavigationApi::panels(const QString& sectionName) const
     228              : {
     229            0 :     const INavigationSection* s = navigation()->findSection(sectionName.toStdString());
     230            0 :     if (!s) {
     231            0 :         return engine()->newArray(0);
     232              :     }
     233              : 
     234            0 :     QJSValue arr = engine()->newArray(s->panels().size());
     235            0 :     quint32 i = 0;
     236            0 :     for (const INavigationPanel* p : s->panels()) {
     237            0 :         arr.setProperty(i, toQJSValue(p, engine()));
     238            0 :         ++i;
     239              :     }
     240              : 
     241            0 :     return arr;
     242            0 : }
     243              : 
     244            0 : QJSValue NavigationApi::controls(const QString& sectionName, const QString& panelName) const
     245              : {
     246            0 :     const INavigationPanel* p = navigation()->findPanel(sectionName.toStdString(), panelName.toStdString());
     247            0 :     if (!p) {
     248            0 :         return engine()->newArray(0);
     249              :     }
     250              : 
     251            0 :     QJSValue arr = engine()->newArray(p->controls().size());
     252            0 :     quint32 i = 0;
     253            0 :     for (const INavigationControl* c : p->controls()) {
     254            0 :         arr.setProperty(i, toQJSValue(c, engine()));
     255            0 :         ++i;
     256              :     }
     257              : 
     258            0 :     return arr;
     259            0 : }
     260              : 
     261            0 : void NavigationApi::dump() const
     262              : {
     263            0 :     navigation()->dump();
     264            0 : }
        

Generated by: LCOV version 2.0-1