LCOV - code coverage report
Current view: top level - src/palette/widgets - palettewidget.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 0.0 % 785 0
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-Studio-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              : 
      23              : #include "palettewidget.h"
      24              : 
      25              : #include <cmath>
      26              : 
      27              : #include <QAccessible>
      28              : #include <QAccessibleEvent>
      29              : #include <QAction>
      30              : #include <QApplication>
      31              : #include <QContextMenuEvent>
      32              : #include <QDrag>
      33              : #include <QFileInfo>
      34              : #include <QMenu>
      35              : #include <QMimeData>
      36              : #include <QResizeEvent>
      37              : #include <QToolTip>
      38              : #include <QWindow>
      39              : 
      40              : #include "modularity/ioc.h"
      41              : #include "translation.h"
      42              : #include "types/bytearray.h"
      43              : 
      44              : #include "draw/types/color.h"
      45              : #include "draw/types/pen.h"
      46              : 
      47              : #include "engraving/rw/rwregister.h"
      48              : 
      49              : #include "engraving/dom/actionicon.h"
      50              : #include "engraving/dom/chord.h"
      51              : #include "engraving/dom/engravingitem.h"
      52              : #include "engraving/dom/fret.h"
      53              : #include "engraving/dom/image.h"
      54              : #include "engraving/dom/masterscore.h"
      55              : #include "engraving/dom/note.h"
      56              : #include "engraving/dom/symbol.h"
      57              : #include "engraving/dom/factory.h"
      58              : #include "engraving/style/defaultstyle.h"
      59              : #include "engraving/style/style.h"
      60              : 
      61              : #include "notation/inotation.h"
      62              : #include "notation/inotationinteraction.h"
      63              : 
      64              : #include "notationscene/utilities/engravingitempreviewpainter.h"
      65              : 
      66              : #include "internal/palettecelliconengine.h"
      67              : 
      68              : #include "log.h"
      69              : 
      70              : using namespace mu;
      71              : using namespace muse;
      72              : using namespace muse::io;
      73              : using namespace mu::palette;
      74              : using namespace mu::engraving;
      75              : using namespace muse::draw;
      76              : 
      77            0 : PaletteWidget::PaletteWidget(QWidget* parent, bool setIocContext)
      78            0 :     : QWidget(parent), muse::Contextable(muse::iocCtxForQWidget(this))
      79              : {
      80            0 :     if (setIocContext) {
      81            0 :         m_palette = std::make_shared<Palette>(iocContext());
      82              :     } else {
      83            0 :         m_palette = std::make_shared<Palette>(nullptr);
      84              :     }
      85              : 
      86              :     //! NOTE: need for accessibility
      87            0 :     m_palette->setParent(this);
      88              : 
      89            0 :     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Ignored);
      90              : 
      91            0 :     setReadOnly(false);
      92            0 :     setMouseTracking(true);
      93            0 : }
      94              : 
      95            0 : void PaletteWidget::setIocContext()
      96              : {
      97            0 :     m_palette->setContext(iocContext());
      98            0 : }
      99              : 
     100            0 : void PaletteWidget::setPalette(PalettePtr palette)
     101              : {
     102            0 :     if (!palette) {
     103            0 :         return;
     104              :     }
     105              : 
     106            0 :     m_palette = palette;
     107            0 :     setObjectName("PaletteWidget_" + name());
     108              : 
     109              :     //! NOTE: need for accessibility
     110            0 :     m_palette->setParent(this);
     111              : 
     112            0 :     update();
     113              : }
     114              : 
     115            0 : QAccessibleInterface* PaletteWidget::accessibleInterface(QObject* object)
     116              : {
     117            0 :     PaletteWidget* widget = qobject_cast<PaletteWidget*>(object);
     118            0 :     IF_ASSERT_FAILED(widget) {
     119            0 :         return nullptr;
     120              :     }
     121              : 
     122            0 :     return static_cast<QAccessibleInterface*>(new AccessiblePaletteWidget(widget));
     123              : }
     124              : 
     125            0 : QString PaletteWidget::name() const
     126              : {
     127            0 :     return m_palette->name();
     128              : }
     129              : 
     130            0 : void PaletteWidget::setName(const QString& name)
     131              : {
     132            0 :     m_palette->setName(name);
     133            0 : }
     134              : 
     135              : // ====================================================
     136              : // Cells & Elements
     137              : // ====================================================
     138              : 
     139            0 : const std::vector<PaletteCellPtr>& PaletteWidget::cells() const
     140              : {
     141            0 :     return m_palette->cells();
     142              : }
     143              : 
     144            0 : const std::vector<PaletteCellPtr>& PaletteWidget::actualCellsList() const
     145              : {
     146            0 :     return m_isFilterActive ? m_filteredCells : cells();
     147              : }
     148              : 
     149            0 : int PaletteWidget::actualCellCount() const
     150              : {
     151            0 :     return static_cast<int>(m_isFilterActive ? m_filteredCells.size() : cells().size());
     152              : }
     153              : 
     154            0 : PaletteCellPtr PaletteWidget::cellAt(size_t index) const
     155              : {
     156            0 :     if (index >= actualCellsList().size()) {
     157            0 :         return nullptr;
     158              :     }
     159              : 
     160            0 :     return actualCellsList().at(index);
     161              : }
     162              : 
     163            0 : ElementPtr PaletteWidget::elementForCellAt(int idx) const
     164              : {
     165            0 :     PaletteCellPtr cell = cellAt(idx);
     166            0 :     return cell ? cell->element : nullptr;
     167            0 : }
     168              : 
     169            0 : PaletteCellPtr PaletteWidget::insertElement(int idx, ElementPtr element, const QString& name, qreal mag, const QPointF offset,
     170              :                                             const QString& tag)
     171              : {
     172            0 :     PaletteCellPtr cell = m_palette->insertElement(idx, element, name, mag, offset, tag);
     173              : 
     174            0 :     update();
     175            0 :     updateGeometry();
     176              : 
     177            0 :     return cell;
     178            0 : }
     179              : 
     180            0 : PaletteCellPtr PaletteWidget::appendElement(ElementPtr element, const QString& name, qreal mag, const QPointF offset, const QString& tag)
     181              : {
     182            0 :     PaletteCellPtr cell = m_palette->appendElement(element, name, mag, offset, tag);
     183              : 
     184            0 :     setFixedHeight(heightForWidth(width()));
     185            0 :     updateGeometry();
     186            0 :     update();
     187              : 
     188            0 :     return cell;
     189            0 : }
     190              : 
     191            0 : PaletteCellPtr PaletteWidget::appendActionIcon(mu::engraving::ActionIconType type)
     192              : {
     193            0 :     PaletteCellPtr cell = m_palette->appendActionIcon(type);
     194              : 
     195            0 :     setFixedHeight(heightForWidth(width()));
     196            0 :     updateGeometry();
     197            0 :     update();
     198              : 
     199            0 :     return cell;
     200            0 : }
     201              : 
     202            0 : void PaletteWidget::clear()
     203              : {
     204            0 :     m_palette->clear();
     205            0 : }
     206              : 
     207              : // ====================================================
     208              : // Drawing
     209              : // ====================================================
     210              : 
     211            0 : qreal PaletteWidget::mag() const
     212              : {
     213            0 :     return m_palette->mag();
     214              : }
     215              : 
     216            0 : void PaletteWidget::setMag(qreal val)
     217              : {
     218            0 :     m_palette->setMag(val);
     219            0 : }
     220              : 
     221            0 : qreal PaletteWidget::yOffset() const
     222              : {
     223            0 :     return m_palette->yOffset();
     224              : }
     225              : 
     226            0 : void PaletteWidget::setYOffset(qreal val)
     227              : {
     228            0 :     m_palette->setYOffset(val);
     229            0 : }
     230              : 
     231              : // ====================================================
     232              : // Grid
     233              : // ====================================================
     234              : 
     235            0 : bool PaletteWidget::drawGrid() const
     236              : {
     237            0 :     return m_palette->drawGrid();
     238              : }
     239              : 
     240            0 : void PaletteWidget::setDrawGrid(bool val)
     241              : {
     242            0 :     m_palette->setDrawGrid(val);
     243            0 : }
     244              : 
     245            0 : int PaletteWidget::gridWidth() const
     246              : {
     247            0 :     return m_palette->gridSize().width();
     248              : }
     249              : 
     250            0 : int PaletteWidget::gridHeight() const
     251              : {
     252            0 :     return m_palette->gridSize().height();
     253              : }
     254              : 
     255            0 : void PaletteWidget::setGridSize(int width, int height)
     256              : {
     257            0 :     m_palette->setGridSize(width, height);
     258            0 :     QSize s = QSize(width, height) * paletteScaling();
     259            0 :     setSizeIncrement(s);
     260            0 :     setBaseSize(s);
     261            0 :     setMinimumSize(s);
     262            0 :     updateGeometry();
     263            0 : }
     264              : 
     265            0 : int PaletteWidget::gridWidthScaled() const
     266              : {
     267            0 :     return gridWidth() * paletteScaling();
     268              : }
     269              : 
     270            0 : int PaletteWidget::gridHeightScaled() const
     271              : {
     272            0 :     return gridHeight() * paletteScaling();
     273              : }
     274              : 
     275            0 : qreal PaletteWidget::paletteScaling() const
     276              : {
     277            0 :     return configuration()->paletteScaling();
     278              : }
     279              : 
     280              : // ====================================================
     281              : // Filter
     282              : // ====================================================
     283              : 
     284            0 : bool PaletteWidget::isFilterActive()
     285              : {
     286            0 :     return m_isFilterActive;
     287              : }
     288              : 
     289              : /// Returns `true` if all cells are filtered.
     290            0 : bool PaletteWidget::setFilterText(const QString& text)
     291              : {
     292            0 :     m_isFilterActive = false;
     293            0 :     setMouseTracking(true);
     294            0 :     QString t = text.toLower();
     295            0 :     bool res = true;
     296            0 :     m_filteredCells.clear();
     297              :     // if palette name is searched for, display all elements in the palette
     298            0 :     if (name().startsWith(t, Qt::CaseInsensitive)) {
     299            0 :         const PaletteCellPtr& c = cells().front();
     300            0 :         for (const PaletteCellPtr& cell : cells()) {
     301            0 :             m_filteredCells.push_back(cell);
     302              :         }
     303              : 
     304            0 :         const bool contains = t.isEmpty() || c;
     305            0 :         if (!contains) {
     306            0 :             m_isFilterActive = true;
     307              :         }
     308            0 :         if (contains && res) {
     309            0 :             res = false;
     310              :         }
     311              :     }
     312              : 
     313            0 :     for (const PaletteCellPtr& cell : cells()) {
     314            0 :         const QStringList h = cell->name.toLower().split(" ");
     315            0 :         const QStringList n = t.split(" ");
     316            0 :         bool c = false;
     317            0 :         for (const QString& hs : h) {
     318            0 :             for (const QString& ns : n) {
     319            0 :                 if (!ns.trimmed().isEmpty()) {
     320            0 :                     c = hs.trimmed().startsWith(ns.trimmed());
     321              :                 }
     322              :             }
     323            0 :             if (c) {
     324            0 :                 break;
     325              :             }
     326              :         }
     327            0 :         if (t.isEmpty() || c) {
     328            0 :             m_filteredCells.push_back(cell);
     329              :         }
     330            0 :         const bool contains = t.isEmpty() || c;
     331            0 :         if (!contains) {
     332            0 :             m_isFilterActive = true;
     333              :         }
     334            0 :         if (contains && res) {
     335            0 :             res = false;
     336              :         }
     337            0 :     }
     338            0 :     setFixedHeight(heightForWidth(width()));
     339            0 :     updateGeometry();
     340            0 :     return res;
     341            0 : }
     342              : 
     343              : // ====================================================
     344              : // Selection
     345              : // ====================================================
     346              : 
     347            0 : bool PaletteWidget::isSelectable() const
     348              : {
     349            0 :     return m_selectable;
     350              : }
     351              : 
     352            0 : void PaletteWidget::setSelectable(bool val)
     353              : {
     354            0 :     m_selectable = val;
     355            0 : }
     356              : 
     357            0 : int PaletteWidget::selectedIdx() const
     358              : {
     359            0 :     return m_selectedIdx;
     360              : }
     361              : 
     362            0 : void PaletteWidget::setSelected(int idx)
     363              : {
     364            0 :     if (m_selectedIdx == idx) {
     365            0 :         return;
     366              :     }
     367              : 
     368            0 :     int previous = m_selectedIdx;
     369            0 :     m_selectedIdx = idx;
     370            0 :     emit selectedChanged(idx, previous);
     371              : }
     372              : 
     373            0 : int PaletteWidget::currentIdx() const
     374              : {
     375            0 :     return m_currentIdx;
     376              : }
     377              : 
     378            0 : void PaletteWidget::setCurrentIdx(int idx)
     379              : {
     380            0 :     m_currentIdx = idx;
     381            0 : }
     382              : 
     383            0 : void PaletteWidget::nextPaletteElement()
     384              : {
     385            0 :     int i = m_currentIdx;
     386            0 :     if (i == -1) {
     387            0 :         return;
     388              :     }
     389            0 :     i++;
     390            0 :     if (i < actualCellCount() && cellAt(i)) {
     391            0 :         m_currentIdx = i;
     392              :     }
     393              : 
     394              :     // TODO: screenreader support
     395              :     //QString name = qApp->translate("palette", cellAt(i)->name.toUtf8());
     396              :     //ScoreAccessibility::makeReadable(name);
     397              : 
     398            0 :     update();
     399              : }
     400              : 
     401            0 : void PaletteWidget::prevPaletteElement()
     402              : {
     403            0 :     int i = m_currentIdx;
     404            0 :     if (i == -1) {
     405            0 :         return;
     406              :     }
     407            0 :     i--;
     408            0 :     if (i >= 0 && cellAt(i)) {
     409            0 :         m_currentIdx = i;
     410              :     }
     411              : 
     412              :     // TODO: screenreader support
     413              :     //QString name = qApp->translate("palette", cellAt(i)->name.toUtf8());
     414              :     //ScoreAccessibility::makeReadable(name);
     415              : 
     416            0 :     update();
     417              : }
     418              : 
     419              : // ====================================================
     420              : // Applying elements
     421              : // ====================================================
     422              : 
     423            0 : bool PaletteWidget::isApplyingElementsDisabled() const
     424              : {
     425            0 :     return m_isApplyingElementsDisabled;
     426              : }
     427              : 
     428            0 : void PaletteWidget::setApplyingElementsDisabled(bool val)
     429              : {
     430            0 :     m_isApplyingElementsDisabled = val;
     431            0 : }
     432              : 
     433            0 : bool PaletteWidget::useDoubleClickForApplyingElements() const
     434              : {
     435            0 :     return m_useDoubleClickForApplyingElements;
     436              : }
     437              : 
     438            0 : void PaletteWidget::setUseDoubleClickForApplyingElements(bool val)
     439              : {
     440            0 :     m_useDoubleClickForApplyingElements = val;
     441            0 : }
     442              : 
     443            0 : bool PaletteWidget::ignoreInputEvents() const
     444              : {
     445            0 :     return m_ignoreInputEvents;
     446              : }
     447              : 
     448            0 : void PaletteWidget::setIgnoreInputEvents(bool val)
     449              : {
     450            0 :     m_ignoreInputEvents = val;
     451            0 : }
     452              : 
     453            0 : void PaletteWidget::applyCurrentElementToScore()
     454              : {
     455            0 :     applyElementAtIndex(m_currentIdx);
     456            0 : }
     457              : 
     458            0 : void PaletteWidget::applyElementAtPosition(const QPointF& pos, Qt::KeyboardModifiers modifiers)
     459              : {
     460            0 :     applyElementAtIndex(cellIndexForPoint(pos), modifiers);
     461            0 : }
     462              : 
     463            0 : void PaletteWidget::applyElementAtIndex(int index, Qt::KeyboardModifiers modifiers)
     464              : {
     465            0 :     if (m_isApplyingElementsDisabled) {
     466            0 :         return;
     467              :     }
     468              : 
     469            0 :     notation::INotationPtr notation = projectSession()->currentNotation();
     470            0 :     if (!notation) {
     471            0 :         return;
     472              :     }
     473              : 
     474            0 :     const PaletteCellPtr cell = cellAt(index);
     475            0 :     if (!cell || !cell->element) {
     476            0 :         return;
     477              :     }
     478              : 
     479            0 :     mu::notation::INotationInteractionPtr interaction = notation->interaction();
     480            0 :     mu::engraving::ElementPtr element = cell->element;
     481              : 
     482            0 :     Ret ret = interaction->canApplyPaletteElement(element.get());
     483            0 :     if (!ret) {
     484              :         //! NOTE The element is applied once the user agrees, unless the notation is closed or changed meanwhile
     485            0 :         std::weak_ptr<mu::notation::INotation> weakNotation = notation;
     486            0 :         interactive()->askToContinue(ret).onResolve(this, [this, weakNotation, element, modifiers](bool proceed) {
     487            0 :             mu::notation::INotationPtr notation = weakNotation.lock();
     488            0 :             if (proceed && notation && notation == projectSession()->currentNotation()) {
     489            0 :                 notation->interaction()->applyPaletteElement(element.get(), modifiers);
     490              :             }
     491            0 :         });
     492            0 :         return;
     493            0 :     }
     494              : 
     495            0 :     interaction->applyPaletteElement(element.get(), modifiers);
     496            0 :     mainWindow()->qWindow()->requestActivate();
     497            0 : }
     498              : 
     499              : // ====================================================
     500              : // Settings
     501              : // ====================================================
     502              : 
     503            0 : bool PaletteWidget::readOnly() const
     504              : {
     505            0 :     return m_isReadOnly;
     506              : }
     507              : 
     508            0 : void PaletteWidget::setReadOnly(bool val)
     509              : {
     510            0 :     m_isReadOnly = val;
     511            0 :     setAcceptDrops(!val);
     512            0 : }
     513              : 
     514            0 : void PaletteWidget::setCellReadOnly(int cellIndex, bool readonly)
     515              : {
     516            0 :     cells()[cellIndex]->readOnly = readonly;
     517            0 : }
     518              : 
     519            0 : void PaletteWidget::setShowContextMenu(bool val)
     520              : {
     521            0 :     m_showContextMenu = val;
     522            0 : }
     523              : 
     524              : // ====================================================
     525              : // Helpers
     526              : // ====================================================
     527              : 
     528            0 : int PaletteWidget::columns() const
     529              : {
     530            0 :     return width() / gridWidthScaled();
     531              : }
     532              : 
     533            0 : int PaletteWidget::rows() const
     534              : {
     535            0 :     int c = columns();
     536            0 :     if (c == 0) {
     537            0 :         return 0;
     538              :     }
     539            0 :     return (actualCellCount() + c - 1) / c;
     540              : }
     541              : 
     542            0 : int PaletteWidget::cellIndexForPoint(const QPointF& p) const
     543              : {
     544            0 :     int idx = theoreticalCellIndexForPoint(p);
     545            0 :     if (idx < 0 || idx >= actualCellCount()) {
     546            0 :         return -1;
     547              :     }
     548            0 :     return idx;
     549              : }
     550              : 
     551            0 : int PaletteWidget::theoreticalCellIndexForPoint(const QPointF& p) const
     552              : {
     553            0 :     int hgridM = gridWidthScaled();
     554            0 :     int vgridM = gridHeightScaled();
     555            0 :     if (columns() == 0) {
     556            0 :         return -1;
     557              :     }
     558            0 :     int rightBorder = width() % hgridM;
     559            0 :     int hhgrid      = hgridM + (rightBorder / columns());
     560              : 
     561            0 :     int col = p.x() / hhgrid;
     562            0 :     int row = p.y() / vgridM;
     563              : 
     564            0 :     int nc = columns();
     565            0 :     if (col > nc) {
     566            0 :         return -1;
     567              :     }
     568              : 
     569            0 :     int idx = row * nc + col;
     570            0 :     if (idx < 0 || idx > columns() * rows()) {
     571            0 :         return -1;
     572              :     }
     573            0 :     return idx;
     574              : }
     575              : 
     576            0 : QRect PaletteWidget::rectForCellAt(int idx) const
     577              : {
     578            0 :     int hgridM = gridWidthScaled();
     579            0 :     int vgridM = gridHeightScaled();
     580            0 :     if (idx == -1) {
     581            0 :         return QRect();
     582              :     }
     583            0 :     if (columns() == 0) {
     584            0 :         return QRect();
     585              :     }
     586              : 
     587            0 :     int rightBorder = width() % hgridM;
     588            0 :     int hhgrid = hgridM + (rightBorder / columns());
     589              : 
     590            0 :     int cc = idx % columns();
     591            0 :     int cr = idx / columns();
     592            0 :     return QRect(cc * hhgrid, cr * vgridM, hhgrid, vgridM);
     593              : }
     594              : 
     595            0 : QPixmap PaletteWidget::pixmapForCellAt(int paletteIdx) const
     596              : {
     597            0 :     qreal _spatium = paletteScoreProvider()->paletteScore()->style().spatium();
     598            0 :     qreal magS     = configuration()->paletteSpatium() * mag() * paletteScaling();
     599            0 :     qreal mag      = magS / _spatium;
     600              : 
     601            0 :     PaletteCellPtr cell = cellAt(paletteIdx);
     602            0 :     if (!cell || !cell->element) {
     603            0 :         return QPixmap();
     604              :     }
     605              : 
     606            0 :     qreal cellMag = cell->mag * mag;
     607            0 :     mu::engraving::ElementPtr element = cell->element;
     608              : 
     609            0 :     engravingRender()->layoutItem(element.get());
     610              : 
     611            0 :     RectF r = element->ldata()->bbox();
     612            0 :     int w = lrint(r.width() * cellMag);
     613            0 :     int h = lrint(r.height() * cellMag);
     614              : 
     615            0 :     if (w * h == 0) {
     616            0 :         LOGD("zero pixmap %d %d %s", w, h, element->typeName());
     617            0 :         return QPixmap();
     618              :     }
     619              : 
     620            0 :     QPixmap pm(w, h);
     621            0 :     pm.fill(configuration()->elementsBackgroundColor());
     622              : 
     623            0 :     muse::draw::Painter painter(&pm, "palette");
     624            0 :     painter.setAntialiasing(true);
     625              : 
     626            0 :     painter.scale(cellMag, cellMag);
     627              : 
     628            0 :     painter.translate(-r.topLeft());
     629            0 :     PointF pos = element->ldata()->pos();
     630            0 :     element->setPos(0, 0);
     631              : 
     632            0 :     QColor color;
     633              :     // show voice colors for notes
     634            0 :     if (element->isChord()) {
     635            0 :         const Chord* chord = toChord(element.get());
     636            0 :         for (Note* note : chord->notes()) {
     637            0 :             note->setSelected(true);
     638              :         }
     639            0 :         color = element->curColor({}).toQColor();
     640              :     } else {
     641            0 :         color = palette().color(QPalette::Normal, QPalette::Text);
     642              :     }
     643              : 
     644            0 :     painter.setPen(Pen(color));
     645              : 
     646            0 :     notation::EngravingItemPreviewPainter::PaintParams params;
     647            0 :     params.painter = &painter;
     648            0 :     params.color = configuration()->elementsColor();
     649              : 
     650            0 :     notation::EngravingItemPreviewPainter::paintItem(engravingRender(), element.get(), params);
     651              : 
     652            0 :     element->setPos(pos);
     653            0 :     return pm;
     654            0 : }
     655              : 
     656              : // ====================================================
     657              : // Sizing
     658              : // ====================================================
     659              : 
     660            0 : int PaletteWidget::heightForWidth(int w) const
     661              : {
     662            0 :     int hgridM = gridWidthScaled();
     663            0 :     int vgridM = gridHeightScaled();
     664            0 :     int c = w / hgridM;
     665            0 :     if (c <= 0) {
     666            0 :         c = 1;
     667              :     }
     668            0 :     int rows = (actualCellCount() + c - 1) / c;
     669            0 :     if (rows <= 0) {
     670            0 :         rows = 1;
     671              :     }
     672            0 :     qreal magS = configuration()->paletteSpatium() * mag() * paletteScaling();
     673            0 :     int h = lrint(yOffset() * 2 * magS);
     674            0 :     return rows * vgridM + h;
     675              : }
     676              : 
     677            0 : QSize PaletteWidget::sizeHint() const
     678              : {
     679            0 :     int h = heightForWidth(width());
     680            0 :     int hgridM = gridWidthScaled();
     681            0 :     return QSize((width() / hgridM) * hgridM, h);
     682              : }
     683              : 
     684              : // ====================================================
     685              : // Event handling
     686              : // ====================================================
     687              : 
     688            0 : bool PaletteWidget::event(QEvent* ev)
     689              : {
     690            0 :     if (!m_palette) {
     691            0 :         return false;
     692              :     }
     693              : 
     694            0 :     if (m_ignoreInputEvents && ev->isInputEvent()) {
     695            0 :         return false;
     696              :     }
     697              : 
     698            0 :     int hgridM = gridWidthScaled();
     699            0 :     int vgridM = gridHeightScaled();
     700              :     // disable mouse hover when keyboard navigation is enabled
     701            0 :     if (m_isFilterActive && (ev->type() == QEvent::MouseMove || ev->type() == QEvent::ToolTip
     702            0 :                              || ev->type() == QEvent::WindowDeactivate)) {
     703            0 :         return true;
     704            0 :     } else if (columns() && (ev->type() == QEvent::ToolTip)) {
     705            0 :         int rightBorder = width() % hgridM;
     706            0 :         int hhgrid = hgridM + (rightBorder / columns());
     707            0 :         QHelpEvent* he = (QHelpEvent*)ev;
     708            0 :         int x = he->pos().x();
     709            0 :         int y = he->pos().y();
     710              : 
     711            0 :         int row = y / vgridM;
     712            0 :         int col = x / hhgrid;
     713              : 
     714            0 :         if (row < 0 || row >= rows()) {
     715            0 :             return false;
     716              :         }
     717            0 :         if (col < 0 || col >= columns()) {
     718            0 :             return false;
     719              :         }
     720            0 :         int idx = row * columns() + col;
     721            0 :         if (idx >= int(cells().size())) {
     722            0 :             return false;
     723              :         }
     724            0 :         if (cellAt(idx) == 0) {
     725            0 :             return false;
     726              :         }
     727            0 :         QToolTip::showText(he->globalPos(), cellAt(idx)->translatedName(), this);
     728            0 :         return false;
     729              :     }
     730            0 :     return QWidget::event(ev);
     731              : }
     732              : 
     733            0 : void PaletteWidget::mousePressEvent(QMouseEvent* ev)
     734              : {
     735            0 :     m_dragStartPosition = ev->pos();
     736            0 :     m_dragIdx           = cellIndexForPoint(m_dragStartPosition);
     737              : 
     738            0 :     m_pressedIndex = m_dragIdx;
     739              : 
     740            0 :     if (m_dragIdx == -1) {
     741            0 :         return;
     742              :     }
     743              : 
     744            0 :     if (m_selectable) {
     745            0 :         if (m_dragIdx != m_selectedIdx) {
     746            0 :             update(rectForCellAt(m_dragIdx) | rectForCellAt(m_selectedIdx));
     747              :         }
     748              : 
     749            0 :         emit boxClicked(m_dragIdx);
     750            0 :         m_selectedIdx = m_dragIdx;
     751              :     }
     752              : 
     753            0 :     update();
     754              : }
     755              : 
     756            0 : void PaletteWidget::mouseDoubleClickEvent(QMouseEvent* ev)
     757              : {
     758            0 :     if (m_useDoubleClickForApplyingElements) {
     759            0 :         applyElementAtPosition(ev->pos(), ev->modifiers());
     760              :     }
     761            0 : }
     762              : 
     763            0 : void PaletteWidget::mouseMoveEvent(QMouseEvent* ev)
     764              : {
     765            0 :     if ((m_currentIdx != -1) && (m_dragIdx == m_currentIdx) && (ev->buttons() & Qt::LeftButton)
     766            0 :         && (ev->pos() - m_dragStartPosition).manhattanLength() > QApplication::startDragDistance()) {
     767            0 :         PaletteCellPtr cell = cellAt(m_currentIdx);
     768            0 :         if (cell && cell->element) {
     769            0 :             QDrag* drag         = new QDrag(this);
     770            0 :             QMimeData* mimeData = new QMimeData;
     771            0 :             const ElementPtr el   = cell->element;
     772              : 
     773            0 :             mimeData->setData(mimeSymbolFormat, el->mimeData().toQByteArray());
     774            0 :             drag->setMimeData(mimeData);
     775              : 
     776            0 :             drag->setPixmap(pixmapForCellAt(m_currentIdx));
     777              : 
     778            0 :             QPoint hotsp(drag->pixmap().rect().bottomRight());
     779            0 :             drag->setHotSpot(hotsp);
     780              : 
     781            0 :             Qt::DropActions da;
     782            0 :             if (!(m_isReadOnly || m_isFilterActive) && (ev->modifiers() & Qt::ShiftModifier)) {
     783            0 :                 m_filteredCells = cells(); // backup
     784            0 :                 da = Qt::MoveAction;
     785              :             } else {
     786            0 :                 da = Qt::CopyAction;
     787              :             }
     788            0 :             Qt::DropAction a = drag->exec(da);
     789            0 :             if (da == Qt::MoveAction && a != da) {
     790              :                 // restore on a failed move action
     791            0 :                 m_palette->clear();
     792            0 :                 m_palette->insertCells(0, m_filteredCells);
     793              :             }
     794            0 :             update();
     795            0 :         }
     796            0 :     } else {
     797            0 :         m_currentIdx = cellIndexForPoint(ev->pos());
     798            0 :         if (m_currentIdx != -1 && cellAt(m_currentIdx) == 0) {
     799            0 :             m_currentIdx = -1;
     800              :         }
     801            0 :         update();
     802              :     }
     803            0 : }
     804              : 
     805            0 : void PaletteWidget::mouseReleaseEvent(QMouseEvent* event)
     806              : {
     807            0 :     m_pressedIndex = -1;
     808              : 
     809            0 :     update();
     810              : 
     811            0 :     if (!m_useDoubleClickForApplyingElements) {
     812            0 :         applyElementAtPosition(event->position(), event->modifiers());
     813              :     }
     814            0 : }
     815              : 
     816            0 : void PaletteWidget::leaveEvent(QEvent*)
     817              : {
     818            0 :     if (m_currentIdx != -1) {
     819            0 :         QRect r = rectForCellAt(m_currentIdx);
     820            0 :         if (!(QGuiApplication::keyboardModifiers() & Qt::ShiftModifier)) {
     821            0 :             m_currentIdx = -1;
     822              :         }
     823            0 :         update(r);
     824              :     }
     825            0 : }
     826              : 
     827            0 : void PaletteWidget::dragEnterEvent(QDragEnterEvent* event)
     828              : {
     829            0 :     const QMimeData* dta = event->mimeData();
     830            0 :     if (dta->hasUrls()) {
     831            0 :         QList<QUrl> ul = event->mimeData()->urls();
     832            0 :         QUrl u = ul.front();
     833              : 
     834            0 :         if (u.scheme() == "file") {
     835            0 :             QFileInfo fi(u.path());
     836            0 :             QString suffix(fi.suffix().toLower());
     837            0 :             if (suffix == "svg"
     838            0 :                 || suffix == "svgz"
     839            0 :                 || suffix == "jpg"
     840            0 :                 || suffix == "jpeg"
     841            0 :                 || suffix == "png"
     842            0 :                 || suffix == "bmp"
     843            0 :                 || suffix == "tif"
     844            0 :                 || suffix == "tiff"
     845              :                 ) {
     846            0 :                 event->acceptProposedAction();
     847              :             }
     848            0 :         }
     849            0 :     } else if (dta->hasFormat(mimeSymbolFormat)) {
     850            0 :         event->accept();
     851            0 :         update();
     852              :     } else {
     853            0 :         event->ignore();
     854              : #ifndef NDEBUG
     855            0 :         LOGD("dragEnterEvent: formats:");
     856            0 :         for (const QString& s : event->mimeData()->formats()) {
     857            0 :             LOGD("   %s", qPrintable(s));
     858            0 :         }
     859              : #endif
     860              :     }
     861            0 : }
     862              : 
     863            0 : void PaletteWidget::dragMoveEvent(QDragMoveEvent* event)
     864              : {
     865            0 :     if (event->source() == this) {
     866            0 :         if (m_currentIdx != -1 && event->proposedAction() == Qt::MoveAction) {
     867            0 :             int targetIdx = cellIndexForPoint(event->position());
     868            0 :             if (targetIdx != -1 && targetIdx != m_currentIdx) {
     869            0 :                 PaletteCellPtr cell = m_palette->takeCell(m_currentIdx);
     870            0 :                 m_palette->insertCell(targetIdx, cell);
     871            0 :                 m_currentIdx = targetIdx;
     872            0 :                 update();
     873            0 :             }
     874            0 :             event->accept();
     875            0 :             return;
     876              :         }
     877            0 :         event->ignore();
     878              :     } else {
     879            0 :         event->accept();
     880              :     }
     881              : }
     882              : 
     883            0 : void PaletteWidget::dropEvent(QDropEvent* event)
     884              : {
     885            0 :     ElementPtr element = nullptr;
     886            0 :     QString name;
     887              : 
     888            0 :     const QMimeData* datap = event->mimeData();
     889            0 :     if (datap->hasUrls()) {
     890            0 :         QList<QUrl> ul = event->mimeData()->urls();
     891            0 :         QUrl u = ul.front();
     892            0 :         if (u.scheme() == "file") {
     893            0 :             auto image = std::make_shared<Image>(paletteScoreProvider()->paletteScore()->dummy());
     894            0 :             QString filePath(u.toLocalFile());
     895            0 :             image->loadFromFile(filePath);
     896            0 :             element = image;
     897            0 :             QFileInfo file(filePath);
     898            0 :             name = file.completeBaseName();
     899            0 :         }
     900            0 :     } else if (datap->hasFormat(mimeSymbolFormat)) {
     901            0 :         QByteArray dta(event->mimeData()->data(mimeSymbolFormat));
     902            0 :         muse::ByteArray ba = muse::ByteArray::fromQByteArrayNoCopy(dta);
     903            0 :         XmlReader xml(ba);
     904            0 :         PointF dragOffset;
     905            0 :         Fraction duration;
     906            0 :         ElementType type = EngravingItem::readType(xml, &dragOffset, &duration);
     907              : 
     908            0 :         if (type == ElementType::SYMBOL) {
     909            0 :             auto symbol = std::make_shared<Symbol>(paletteScoreProvider()->paletteScore()->dummy());
     910            0 :             rw::RWRegister::reader()->readItem(symbol.get(), xml);
     911            0 :             element = symbol;
     912            0 :         } else {
     913            0 :             element = std::shared_ptr<EngravingItem>(Factory::createItem(type, paletteScoreProvider()->paletteScore()->dummy()));
     914            0 :             if (element) {
     915            0 :                 rw::RWRegister::reader()->readItem(element.get(), xml);
     916            0 :                 element->setTrack(0);
     917              :             }
     918              :         }
     919            0 :     }
     920              : 
     921            0 :     if (!element) {
     922            0 :         event->ignore();
     923            0 :         return;
     924              :     }
     925              : 
     926            0 :     if (event->source() == this) {
     927            0 :         if (event->proposedAction() == Qt::MoveAction) {
     928            0 :             event->accept();
     929            0 :             emit changed();
     930            0 :             return;
     931              :         }
     932            0 :         event->ignore();
     933            0 :         return;
     934              :     }
     935              : 
     936            0 :     if (element->isFretDiagram()) {
     937            0 :         name = toFretDiagram(element.get())->harmonyPlainText();
     938              :     }
     939              : 
     940            0 :     element->setSelected(false);
     941              : 
     942            0 :     int i = cellIndexForPoint(event->position());
     943            0 :     if (i == -1 || cells()[i]) {
     944            0 :         appendElement(element, name);
     945              :     } else {
     946            0 :         insertElement(i, element, name);
     947              :     }
     948            0 :     event->accept();
     949              : 
     950            0 :     setFixedHeight(heightForWidth(width()));
     951            0 :     updateGeometry();
     952            0 :     update();
     953            0 :     emit changed();
     954            0 : }
     955              : 
     956            0 : void PaletteWidget::resizeEvent(QResizeEvent* e)
     957              : {
     958            0 :     setFixedHeight(heightForWidth(e->size().width()));
     959            0 : }
     960              : 
     961            0 : void PaletteWidget::paintEvent(QPaintEvent* /*event*/)
     962              : {
     963            0 :     auto paletteScore = paletteScoreProvider()->paletteScore();
     964            0 :     qreal _spatium = paletteScore->style().spatium();
     965            0 :     qreal magS     = configuration()->paletteSpatium() * mag() * paletteScaling();
     966            0 :     qreal mag      = magS / _spatium;
     967            0 :     paletteScore->style().setSpatium(paletteScore->style().defaultSpatium());
     968              : 
     969            0 :     muse::draw::Painter painter(this, "palette");
     970            0 :     painter.setAntialiasing(true);
     971              : 
     972            0 :     if (m_paintOptions.backgroundColor.isValid()) {
     973            0 :         painter.setBrush(m_paintOptions.backgroundColor);
     974              :     }
     975              : 
     976            0 :     painter.setPen(configuration()->gridColor());
     977            0 :     painter.drawRoundedRect(RectF(0, 0, width(), height()), 2, 2);
     978              : 
     979              :     //
     980              :     // draw grid
     981              :     //
     982            0 :     int hgridM = gridWidthScaled();
     983            0 :     int vgridM = gridHeightScaled();
     984              : 
     985            0 :     if (columns() == 0) {
     986            0 :         return;
     987              :     }
     988            0 :     int rightBorder = width() % hgridM;
     989            0 :     int hhgrid = hgridM + (rightBorder / columns());
     990              : 
     991            0 :     if (drawGrid()) {
     992            0 :         for (int row = 1; row < rows(); ++row) {
     993            0 :             int x2 = row < rows() - 1 ? columns() * hhgrid : width();
     994            0 :             int y  = row * vgridM;
     995            0 :             painter.drawLine(0, y, x2, y);
     996              :         }
     997            0 :         for (int column = 1; column < columns(); ++column) {
     998            0 :             int x = hhgrid * column;
     999            0 :             painter.drawLine(x, 0, x, rows() * vgridM);
    1000              :         }
    1001              :     }
    1002              : 
    1003            0 :     qreal dy = lrint(2 * magS);
    1004              : 
    1005            0 :     Color linesColor = m_paintOptions.linesColor.isValid() ? m_paintOptions.linesColor
    1006            0 :                        : configuration()->elementsColor();
    1007              : 
    1008            0 :     Color selectionColor = m_paintOptions.selectionColor.isValid() ? m_paintOptions.selectionColor
    1009            0 :                            : configuration()->accentColor();
    1010              : 
    1011              :     //
    1012              :     // draw symbols
    1013              :     //
    1014            0 :     for (int idx = 0; idx < int(actualCellsList().size()); ++idx) {
    1015            0 :         int yoffset = _spatium * yOffset();
    1016            0 :         RectF r = RectF::fromQRectF(rectForCellAt(idx));
    1017            0 :         RectF rShift = r.translated(0, yoffset);
    1018            0 :         QColor c = selectionColor.toQColor();
    1019              : 
    1020            0 :         PaletteCellPtr currentCell = actualCellsList().at(idx);
    1021            0 :         if (!currentCell) {
    1022            0 :             continue;
    1023              :         }
    1024              : 
    1025            0 :         if (currentCell->focused) {
    1026            0 :             painter.setPen(QColor(uiConfiguration()->currentTheme().values[muse::ui::FONT_PRIMARY_COLOR].toString()));
    1027            0 :             painter.setBrush(QColor(Qt::transparent));
    1028              : 
    1029            0 :             int borderWidth = uiConfiguration()->currentTheme().values[muse::ui::NAVIGATION_CONTROL_BORDER_WIDTH].toInt();
    1030            0 :             qreal border = borderWidth / 2;
    1031              : 
    1032            0 :             painter.drawRoundedRect(r.adjusted(border, border, -border, -border), borderWidth, borderWidth);
    1033            0 :             r.adjust(borderWidth, borderWidth, -borderWidth, -borderWidth);
    1034              :         }
    1035              : 
    1036            0 :         if (idx == m_selectedIdx) {
    1037            0 :             c.setAlphaF(0.5f);
    1038            0 :             painter.fillRect(r, c);
    1039            0 :         } else if (idx == m_pressedIndex) {
    1040            0 :             c.setAlphaF(0.75f);
    1041            0 :             painter.fillRect(r, c);
    1042            0 :         } else if (idx == m_currentIdx) {
    1043            0 :             c.setAlphaF(0.2f);
    1044            0 :             painter.fillRect(r, c);
    1045              :         }
    1046              : 
    1047            0 :         QString tag = currentCell->tag;
    1048            0 :         if (!tag.isEmpty()) {
    1049            0 :             painter.setPen(QColor(Qt::darkGray));
    1050            0 :             Font font(painter.font());
    1051            0 :             font.setPixelSize(uiConfiguration()->fontSize(muse::ui::FontSizeType::BODY));
    1052            0 :             painter.setFont(font);
    1053            0 :             painter.drawText(rShift, AlignLeft | AlignTop, {}, tag);
    1054            0 :         }
    1055              : 
    1056            0 :         muse::draw::Pen pen(linesColor);
    1057            0 :         pen.setWidthF(engraving::DefaultStyle::defaultStyle().styleS(Sid::staffLineWidth).val() * magS);
    1058            0 :         painter.setPen(pen);
    1059              : 
    1060            0 :         ElementPtr el = currentCell->element;
    1061            0 :         if (!el) {
    1062            0 :             continue;
    1063              :         }
    1064              : 
    1065            0 :         bool drawStaff = currentCell->drawStaff;
    1066            0 :         int row    = idx / columns();
    1067            0 :         int column = idx % columns();
    1068              : 
    1069            0 :         qreal cellMag = currentCell->mag * mag;
    1070              : 
    1071            0 :         engravingRender()->layoutItem(el.get());
    1072              : 
    1073            0 :         if (drawStaff) {
    1074            0 :             qreal y = r.y() + vgridM * .5 - dy + yOffset() * _spatium * cellMag;
    1075            0 :             qreal x = r.x() + 3;
    1076            0 :             qreal w = hhgrid - 6;
    1077            0 :             for (int i = 0; i < 5; ++i) {
    1078            0 :                 qreal yy = y + i * magS;
    1079            0 :                 painter.setPen(linesColor);
    1080            0 :                 painter.drawLine(LineF(x, yy, x + w, yy));
    1081              :             }
    1082              :         }
    1083            0 :         painter.save();
    1084            0 :         painter.scale(cellMag, cellMag);
    1085              : 
    1086            0 :         double gw = hhgrid / cellMag;
    1087            0 :         double gh = vgridM / cellMag;
    1088            0 :         double gx = column * gw + currentCell->xoffset * _spatium;
    1089            0 :         double gy = row * gh + currentCell->yoffset * _spatium;
    1090              : 
    1091            0 :         double sw = el->width();
    1092            0 :         double sh = el->height();
    1093            0 :         double sy = 0;
    1094              : 
    1095            0 :         if (drawStaff) {
    1096            0 :             sy = gy + gh * .5 - 2.0 * _spatium;
    1097              :         } else {
    1098            0 :             sy  = gy + (gh - sh) * .5 - el->ldata()->bbox().y();
    1099              :         }
    1100            0 :         double sx  = gx + (gw - sw) * .5 - el->ldata()->bbox().x();
    1101              : 
    1102            0 :         sy += yOffset() * _spatium;
    1103              : 
    1104            0 :         painter.translate(sx, sy);
    1105              : 
    1106            0 :         QColor color;
    1107            0 :         if (idx != m_selectedIdx) {
    1108              :             // show voice colors for notes
    1109            0 :             if (el->isChord()) {
    1110            0 :                 color = el->curColor({}).toQColor();
    1111              :             } else {
    1112            0 :                 color = palette().color(QPalette::Normal, QPalette::Text);
    1113              :             }
    1114              :         } else {
    1115            0 :             color = palette().color(QPalette::Normal, QPalette::HighlightedText);
    1116              :         }
    1117              : 
    1118            0 :         painter.setPen(Pen(color));
    1119              : 
    1120            0 :         notation::EngravingItemPreviewPainter::PaintParams params;
    1121            0 :         params.painter = &painter;
    1122            0 :         params.color = configuration()->elementsColor();
    1123              : 
    1124            0 :         params.useElementColors = m_paintOptions.useElementColors;
    1125            0 :         params.colorsInversionEnabled = m_paintOptions.colorsInversionEnabled;
    1126              : 
    1127            0 :         notation::EngravingItemPreviewPainter::paintItem(engravingRender(), el.get(), params);
    1128              : 
    1129            0 :         painter.restore();
    1130            0 :     }
    1131            0 : }
    1132              : 
    1133            0 : void PaletteWidget::contextMenuEvent(QContextMenuEvent* event)
    1134              : {
    1135            0 :     if (!m_showContextMenu) {
    1136            0 :         return;
    1137              :     }
    1138            0 :     int i = cellIndexForPoint(event->pos());
    1139            0 :     if (i == -1) {
    1140              :         // palette context menu
    1141            0 :         return;
    1142              :     }
    1143              : 
    1144            0 :     QMenu menu;
    1145            0 :     QAction* deleteCellAction = menu.addAction(muse::qtrc("palette", "Delete"));
    1146            0 :     QAction* contextAction = menu.addAction(muse::qtrc("palette", "Properties…"));
    1147            0 :     deleteCellAction->setEnabled(!m_isReadOnly);
    1148            0 :     contextAction->setEnabled(!m_isReadOnly);
    1149              : 
    1150            0 :     if (m_isFilterActive || (cellAt(i) && cellAt(i)->readOnly)) {
    1151            0 :         deleteCellAction->setEnabled(false);
    1152              :     }
    1153              : 
    1154            0 :     if (!deleteCellAction->isEnabled() && !contextAction->isEnabled()) {
    1155            0 :         return;
    1156              :     }
    1157              : 
    1158            0 :     const QAction* action = menu.exec(mapToGlobal(event->pos()));
    1159              : 
    1160            0 :     if (action == deleteCellAction) {
    1161            0 :         PaletteCellPtr cell = cellAt(i);
    1162            0 :         if (cell) {
    1163            0 :             std::string title = muse::trc("palette", "Delete palette cell");
    1164            0 :             std::string question = muse::qtrc("palette", "Are you sure you want to delete palette cell “%1”?")
    1165            0 :                                    .arg(cell->name).toStdString();
    1166              : 
    1167            0 :             auto promise = interactive()->question(title, question, {
    1168              :                 muse::IInteractive::Button::Yes,
    1169              :                 muse::IInteractive::Button::No
    1170            0 :             }, muse::IInteractive::Button::Yes);
    1171              : 
    1172            0 :             promise.onResolve(this, [this, i](const IInteractive::Result& res) {
    1173            0 :                 if (res.isButton(IInteractive::Button::Yes)) {
    1174            0 :                     m_palette->takeCell(i);
    1175            0 :                     emit changed();
    1176              :                 }
    1177            0 :             });
    1178            0 :         }
    1179            0 :     } else {
    1180            0 :         bool sizeChanged = false;
    1181            0 :         for (size_t j = 0; j < cells().size(); ++j) {
    1182            0 :             if (!cellAt(j)) {
    1183            0 :                 m_palette->takeCells(i, 1);
    1184            0 :                 sizeChanged = true;
    1185              :             }
    1186              :         }
    1187            0 :         if (sizeChanged) {
    1188            0 :             setFixedHeight(heightForWidth(width()));
    1189            0 :             updateGeometry();
    1190              :         }
    1191            0 :         update();
    1192              :     }
    1193            0 : }
    1194              : 
    1195              : // ====================================================
    1196              : // Read/write
    1197              : // ====================================================
    1198              : 
    1199            0 : void PaletteWidget::read(XmlReader& e, bool pasteMode)
    1200              : {
    1201            0 :     m_palette->read(e, pasteMode);
    1202            0 : }
    1203              : 
    1204            0 : void PaletteWidget::write(XmlWriter& xml, bool pasteMode) const
    1205              : {
    1206            0 :     m_palette->write(xml, pasteMode);
    1207            0 : }
    1208              : 
    1209            0 : bool PaletteWidget::readFromFile(const QString& path)
    1210              : {
    1211            0 :     return m_palette->readFromFile(path);
    1212              : }
    1213              : 
    1214              : /// Write as compressed zip file and include images as needed
    1215            0 : void PaletteWidget::writeToFile(const QString& path) const
    1216              : {
    1217            0 :     m_palette->writeToFile(path);
    1218            0 : }
    1219              : 
    1220            0 : bool PaletteWidget::handleEvent(QEvent* event)
    1221              : {
    1222            0 :     return QWidget::event(event);
    1223              : }
    1224              : 
    1225            0 : const PaletteWidget::PaintOptions& PaletteWidget::paintOptions() const
    1226              : {
    1227            0 :     return m_paintOptions;
    1228              : }
    1229              : 
    1230            0 : void PaletteWidget::setPaintOptions(const PaintOptions& options)
    1231              : {
    1232            0 :     m_paintOptions = options;
    1233            0 : }
    1234              : 
    1235              : // ====================================================
    1236              : // PaletteScrollArea
    1237              : // ====================================================
    1238              : 
    1239            0 : PaletteScrollArea::PaletteScrollArea(PaletteWidget* w, QWidget* parent)
    1240            0 :     : QScrollArea(parent)
    1241              : {
    1242            0 :     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    1243            0 :     setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    1244            0 :     setWidget(w);
    1245            0 :     setWidgetResizable(false);
    1246            0 :     m_restrictHeight = true;
    1247            0 :     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
    1248            0 : }
    1249              : 
    1250            0 : bool PaletteScrollArea::restrictHeight() const
    1251              : {
    1252            0 :     return m_restrictHeight;
    1253              : }
    1254              : 
    1255            0 : void PaletteScrollArea::setRestrictHeight(bool val)
    1256              : {
    1257            0 :     m_restrictHeight = val;
    1258            0 : }
    1259              : 
    1260            0 : void PaletteScrollArea::keyPressEvent(QKeyEvent* event)
    1261              : {
    1262            0 :     PaletteWidget* p = qobject_cast<PaletteWidget*>(widget());
    1263            0 :     int pressedKey = event->key();
    1264            0 :     switch (pressedKey) {
    1265            0 :     case Qt::Key_Right:
    1266              :     case Qt::Key_Left:
    1267              :     case Qt::Key_Up:
    1268              :     case Qt::Key_Down:
    1269              :     {
    1270            0 :         int idx = p->selectedIdx();
    1271            0 :         if (pressedKey == Qt::Key_Left || pressedKey == Qt::Key_Up) {
    1272            0 :             idx--;
    1273              :         } else {
    1274            0 :             idx++;
    1275              :         }
    1276            0 :         if (idx < 0) {
    1277            0 :             idx = p->actualCellCount() - 1;
    1278            0 :         } else if (idx >= p->actualCellCount()) {
    1279            0 :             idx = 0;
    1280              :         }
    1281              : 
    1282            0 :         p->setSelected(idx);
    1283            0 :         p->setCurrentIdx(idx);
    1284              : 
    1285            0 :         p->update();
    1286            0 :         return;
    1287              :     }
    1288            0 :     case Qt::Key_Enter:
    1289              :     case Qt::Key_Return:
    1290            0 :         if (!p->isApplyingElementsDisabled()) {
    1291            0 :             p->applyCurrentElementToScore();
    1292              :         }
    1293            0 :         return;
    1294            0 :     default:
    1295            0 :         break;
    1296              :     }
    1297            0 :     QScrollArea::keyPressEvent(event);
    1298              : }
    1299              : 
    1300            0 : void PaletteScrollArea::resizeEvent(QResizeEvent* re)
    1301              : {
    1302            0 :     QScrollArea::resizeEvent(re);
    1303              : 
    1304            0 :     PaletteWidget* palette = qobject_cast<PaletteWidget*>(widget());
    1305            0 :     int h = palette->heightForWidth(width());
    1306            0 :     palette->resize(QSize(width() - 6, h));
    1307            0 :     if (m_restrictHeight) {
    1308            0 :         setMaximumHeight(h + 6);
    1309              :     }
    1310            0 : }
    1311              : 
    1312            0 : AccessiblePaletteWidget::AccessiblePaletteWidget(PaletteWidget* palette)
    1313            0 :     : QAccessibleWidget(palette)
    1314              : {
    1315            0 :     m_palette = palette;
    1316              : 
    1317            0 :     connect(m_palette, &PaletteWidget::selectedChanged, this, [this](int index, int previous) {
    1318            0 :         PaletteCellPtr curCell = m_palette->cellAt(index);
    1319            0 :         PaletteCellPtr previousCell = m_palette->cellAt(previous);
    1320              : 
    1321            0 :         if (previousCell) {
    1322            0 :             previousCell->focused = false;
    1323              : 
    1324            0 :             QAccessible::State qstate;
    1325            0 :             qstate.active = false;
    1326            0 :             qstate.focused = false;
    1327            0 :             qstate.selected = false;
    1328              : 
    1329            0 :             QAccessibleEvent ev(previousCell.get(), QAccessible::Focus);
    1330            0 :             QAccessible::updateAccessibility(&ev);
    1331            0 :         }
    1332              : 
    1333            0 :         if (curCell) {
    1334            0 :             curCell->focused = true;
    1335              : 
    1336            0 :             QAccessible::State qstate;
    1337            0 :             qstate.active = true;
    1338            0 :             qstate.focused = true;
    1339            0 :             qstate.selected = true;
    1340              : 
    1341            0 :             QAccessibleEvent ev(curCell.get(), QAccessible::Focus);
    1342            0 :             QAccessible::updateAccessibility(&ev);
    1343            0 :         }
    1344            0 :     });
    1345            0 : }
    1346              : 
    1347            0 : QObject* AccessiblePaletteWidget::object() const
    1348              : {
    1349            0 :     return m_palette;
    1350              : }
    1351              : 
    1352            0 : QAccessibleInterface* AccessiblePaletteWidget::child(int index) const
    1353              : {
    1354            0 :     PaletteCellPtr cell = m_palette->cellAt(index);
    1355            0 :     if (!cell) {
    1356            0 :         return nullptr;
    1357              :     }
    1358              : 
    1359            0 :     return QAccessible::queryAccessibleInterface(cell.get());
    1360            0 : }
    1361              : 
    1362            0 : int AccessiblePaletteWidget::childCount() const
    1363              : {
    1364            0 :     return m_palette->actualCellCount();
    1365              : }
    1366              : 
    1367            0 : int AccessiblePaletteWidget::indexOfChild(const QAccessibleInterface* child) const
    1368              : {
    1369            0 :     for (int i = 0; i < childCount(); ++i) {
    1370            0 :         auto childAccessible = QAccessible::queryAccessibleInterface(m_palette->cellAt(i).get());
    1371            0 :         if (childAccessible == child) {
    1372            0 :             return i;
    1373              :         }
    1374              :     }
    1375              : 
    1376            0 :     return -1;
    1377              : }
    1378              : 
    1379            0 : QAccessible::Role AccessiblePaletteWidget::role() const
    1380              : {
    1381            0 :     return QAccessible::StaticText;
    1382              : }
    1383              : 
    1384            0 : QAccessible::State AccessiblePaletteWidget::state() const
    1385              : {
    1386            0 :     QAccessible::State state = QAccessibleWidget::state();
    1387            0 :     state.selectable = true;
    1388            0 :     state.active = true;
    1389            0 :     state.focusable = true;
    1390            0 :     state.focused = m_palette->hasFocus();
    1391              : 
    1392            0 :     return state;
    1393              : }
    1394              : 
    1395            0 : QAccessibleInterface* AccessiblePaletteWidget::focusChild() const
    1396              : {
    1397            0 :     int selectIndex = m_palette->selectedIdx();
    1398            0 :     return QAccessible::queryAccessibleInterface(m_palette->cellAt(selectIndex).get());
    1399              : }
        

Generated by: LCOV version 2.0-1