fixed drawing of icons in the palettes panel

This commit is contained in:
RomanPudashkin 2021-03-03 17:30:15 +02:00
parent b4de3cec4d
commit 9b7d9802fd
10 changed files with 173 additions and 117 deletions

View file

@ -178,6 +178,13 @@ public:
GRADUATION_CAP = 0xF19D,
AUTO_TEXT = 0xF329,
NOTE_HEAD_EIGHTH = 0xF33A,
BEAM_START = 0xF33B,
BEAM_MIDDLE = 0xF33D,
BEAM_32 = 0xF33E,
BEAM_64 = 0xF33F,
QUESTION_MARK = 0xF340,
NOTE_HEAD_QUARTER = 0xF341,

View file

@ -15,6 +15,53 @@
#include "property.h"
namespace Ms {
Icon::Icon(Score* score)
: Element(score)
{
}
Icon *Icon::clone() const
{
return new Icon(*this);
}
ElementType Icon::type() const
{
return ElementType::ICON;
}
IconType Icon::iconType() const
{
return _iconType;
}
QRectF Icon::boundingBox() const
{
return QRectF(0, 0, _extent, _extent);
}
void Icon::setAction(const std::string& actionCode, char16_t icon)
{
_actionCode = actionCode;
_icon = icon;
}
const std::string& Icon::actionCode() const
{
return _actionCode;
}
void Icon::setIconType(IconType val)
{
_iconType = val;
}
void Icon::setExtent(int extent)
{
_extent = extent;
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
@ -23,8 +70,8 @@ void Icon::write(XmlWriter& xml) const
{
xml.stag(this);
xml.tag("subtype", int(_iconType));
if (!_action.isEmpty()) {
xml.tag("action", _action.data());
if (!_actionCode.empty()) {
xml.tag("action", QString::fromStdString(_actionCode));
}
xml.etag();
}
@ -38,7 +85,7 @@ void Icon::read(XmlReader& e)
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "action") {
_action = e.readElementText().toLocal8Bit();
_actionCode = e.readElementText().toStdString();
} else if (tag == "subtype") {
_iconType = IconType(e.readInt());
} else {
@ -53,7 +100,7 @@ void Icon::read(XmlReader& e)
void Icon::layout()
{
setbbox(QRectF(0, 0, _extent, _extent));
setbbox(boundingBox());
}
//---------------------------------------------------------
@ -63,8 +110,7 @@ void Icon::layout()
void Icon::draw(mu::draw::Painter* painter) const
{
TRACE_OBJ_DRAW;
QPixmap pm(_icon.pixmap(_extent, QIcon::Normal, QIcon::On));
painter->drawPixmap(QPointF(0, 0), pm);
painter->drawText(boundingBox(), Qt::AlignCenter, QChar(_icon));
}
//---------------------------------------------------------
@ -75,7 +121,7 @@ QVariant Icon::getProperty(Pid pid) const
{
switch (pid) {
case Pid::ACTION:
return action();
return QString::fromStdString(actionCode());
default:
break;
}
@ -90,7 +136,7 @@ bool Icon::setProperty(Pid pid, const QVariant& v)
{
switch (pid) {
case Pid::ACTION:
_action = v.toString().toLatin1();
_actionCode = v.toString().toStdString();
triggerLayout();
break;
default:

View file

@ -24,25 +24,19 @@ namespace Ms {
class Icon final : public Element
{
IconType _iconType { IconType::NONE };
QByteArray _action;
QIcon _icon;
int _extent { 40 };
public:
Icon(Score* s)
: Element(s) { }
virtual ~Icon() {}
Icon(Score* score);
~Icon() override = default;
Icon* clone() const override;
ElementType type() const override;
IconType iconType() const;
const std::string& actionCode() const;
void setIconType(IconType val);
void setAction(const std::string& actionCode, char16_t icon);
void setExtent(int extent);
Icon* clone() const override { return new Icon(*this); }
ElementType type() const override { return ElementType::ICON; }
IconType iconType() const { return _iconType; }
void setIconType(IconType val) { _iconType = val; }
void setAction(const QByteArray& a, const QIcon& i) { _action = a; _icon = i; }
const QByteArray& action() const { return _action; }
QIcon icon() const { return _icon; }
void setExtent(int v) { _extent = v; }
int extent() const { return _extent; }
void write(XmlWriter&) const override;
void read(XmlReader&) override;
void draw(mu::draw::Painter*) const override;
@ -50,6 +44,14 @@ public:
QVariant getProperty(Pid) const override;
bool setProperty(Pid, const QVariant&) override;
private:
QRectF boundingBox() const;
IconType _iconType { IconType::NONE };
std::string _actionCode;
char16_t _icon = 0;
int _extent { 40 };
};
} // namespace Ms
#endif

View file

@ -350,17 +350,20 @@ const ActionList NotationActions::m_actions = {
ActionItem("insert-hbox",
ShortcutContext::NotationHasSelection,
QT_TRANSLATE_NOOP("action", "Insert Horizontal Frame"),
QT_TRANSLATE_NOOP("action", "Insert horizontal frame")
QT_TRANSLATE_NOOP("action", "Insert horizontal frame"),
IconCode::Code::HORIZONTAL_FRAME
),
ActionItem("insert-vbox",
ShortcutContext::NotationHasSelection,
QT_TRANSLATE_NOOP("action", "Insert Vertical Frame"),
QT_TRANSLATE_NOOP("action", "Insert vertical frame")
QT_TRANSLATE_NOOP("action", "Insert vertical frame"),
IconCode::Code::VERTICAL_FRAME
),
ActionItem("insert-textframe",
ShortcutContext::NotationHasSelection,
QT_TRANSLATE_NOOP("action", "Insert Text Frame"),
QT_TRANSLATE_NOOP("action", "Insert text frame")
QT_TRANSLATE_NOOP("action", "Insert text frame"),
IconCode::Code::TEXT_FRAME
),
ActionItem("append-hbox",
ShortcutContext::NotationHasSelection,
@ -412,27 +415,38 @@ const ActionList NotationActions::m_actions = {
ActionItem("beam-start",
ShortcutContext::NotationActive,
QT_TRANSLATE_NOOP("action", "Beam Start"),
QT_TRANSLATE_NOOP("action", "Beam start")
QT_TRANSLATE_NOOP("action", "Beam start"),
IconCode::Code::BEAM_START
),
ActionItem("beam-mid",
ShortcutContext::NotationActive,
QT_TRANSLATE_NOOP("action", "Beam Middle"),
QT_TRANSLATE_NOOP("action", "Beam middle")
QT_TRANSLATE_NOOP("action", "Beam middle"),
IconCode::Code::BEAM_MIDDLE
),
ActionItem("no-beam",
ShortcutContext::NotationActive,
QT_TRANSLATE_NOOP("action", "No Beam"),
QT_TRANSLATE_NOOP("action", "No beam")
QT_TRANSLATE_NOOP("action", "No beam"),
IconCode::Code::NOTE_HEAD_EIGHTH
),
ActionItem("beam-32",
ShortcutContext::NotationActive,
QT_TRANSLATE_NOOP("action", "Beam 16th Sub"),
QT_TRANSLATE_NOOP("action", "Beam 16th sub")
QT_TRANSLATE_NOOP("action", "Beam 16th sub"),
IconCode::Code::BEAM_32
),
ActionItem("beam-64",
ShortcutContext::NotationActive,
QT_TRANSLATE_NOOP("action", "Beam 32th Sub"),
QT_TRANSLATE_NOOP("action", "Beam 32th sub")
QT_TRANSLATE_NOOP("action", "Beam 32th sub"),
IconCode::Code::BEAM_64
),
ActionItem("auto-beam",
ShortcutContext::NotationActive,
QT_TRANSLATE_NOOP("action", "Auto beam"),
QT_TRANSLATE_NOOP("action", "Auto beam"),
IconCode::Code::AUTO_TEXT
),
ActionItem("interval1",
ShortcutContext::NotationHasSelection,

View file

@ -35,20 +35,9 @@ MU4PaletteAdapter::MU4PaletteAdapter()
m_paletteEnabled.val = true;
}
QAction* MU4PaletteAdapter::getAction(const char* id_) const
actions::ActionItem MU4PaletteAdapter::getAction(const actions::ActionCode& code) const
{
QString id(id_);
QAction* a = m_actions.value(id, nullptr);
if (!a) {
a = new QAction();
m_actions.insert(id, a);
}
return a;
}
QString MU4PaletteAdapter::actionHelp(const char* id) const
{
return QString(id);
return actionsRegister()->action(code);
}
void MU4PaletteAdapter::showMasterPalette(const QString& arg)

View file

@ -25,18 +25,19 @@
#include "modularity/ioc.h"
#include "context/iglobalcontext.h"
#include "iinteractive.h"
#include "actions/iactionsregister.h"
namespace mu::palette {
class MU4PaletteAdapter : public IPaletteAdapter
{
INJECT(palette, context::IGlobalContext, globalContext)
INJECT(palette, framework::IInteractive, interactive)
INJECT(palette, actions::IActionsRegister, actionsRegister)
public:
MU4PaletteAdapter();
QAction* getAction(const char* id) const override;
QString actionHelp(const char* id) const override;
actions::ActionItem getAction(const actions::ActionCode& code) const override;
void showMasterPalette(const QString&) override;
bool isSelected() const override;
@ -55,10 +56,8 @@ public:
mu::async::Notification elementDraggedToScoreView() const override;
private:
ValCh<bool> m_paletteEnabled;
mutable Ms::PaletteWorkspace* m_paletteWorkspace = nullptr;
mutable QHash<QString, QAction*> m_actions;
mu::async::Notification m_paletteSearchRequested;
mu::async::Notification m_elementDraggedToScoreView;
};

View file

@ -24,6 +24,8 @@
#include <QToolTip>
#include <QBuffer>
#include "actions/actiontypes.h"
#include "libmscore/element.h"
#include "libmscore/style.h"
#include "libmscore/sym.h"
@ -61,6 +63,7 @@
using namespace mu::framework;
using namespace mu::palette;
using namespace mu::actions;
namespace Ms {
//---------------------------------------------------------
@ -922,7 +925,7 @@ PaletteCellPtr Palette::add(int idx, ElementPtr element, const QString& name, QS
if (element && element->isIcon()) {
const Icon* icon = toIcon(element.get());
connect(adapter()->getAction(icon->action()), SIGNAL(toggled(bool)), SLOT(actionToggled(bool)));
//connect(adapter()->getAction(icon->action()), SIGNAL(toggled(bool)), SLOT(actionToggled(bool)));
}
updateGeometry();
@ -1517,14 +1520,15 @@ void Palette::read(XmlReader& e)
} else {
cell->element->read(e);
cell->element->styleChanged();
if (cell->element->type() == ElementType::ICON) {
Icon* icon = static_cast<Icon*>(cell->element.get());
QAction* ac = adapter()->getAction(icon->action());
if (ac) {
QIcon qicon(ac->icon());
icon->setAction(icon->action(), qicon);
ActionItem actionItem = adapter()->getAction(icon->actionCode());
if (actionItem.isValid()) {
icon->setAction(icon->actionCode(), static_cast<char16_t>(actionItem.iconCode));
} else {
add = false; // action is not valid, don't add it to the palette.
add = false;
}
}
}
@ -1625,11 +1629,12 @@ void Palette::actionToggled(bool /*val*/)
for (int n = 0; n < nn; ++n) {
const ElementPtr element = cellAt(n)->element;
if (element && element->type() == ElementType::ICON) {
/*
QAction* a = adapter()->getAction(std::dynamic_pointer_cast<Icon>(element)->action());
if (a->isChecked()) {
m_selectedIdx = n;
break;
}
}*/
}
}
update();
@ -1768,15 +1773,13 @@ void Palette::dropEvent(QDropEvent* event)
if (element) {
element->read(xml);
element->setTrack(0);
if (element->isIcon()) {
Icon* i = toIcon(element.get());
const QByteArray& action = i->action();
if (!action.isEmpty()) {
QAction* a = adapter()->getAction(action);
if (a) {
QIcon icon(a->icon());
i->setAction(action, icon);
}
Icon* icon = toIcon(element.get());
ActionItem actionItem = adapter()->getAction(icon->actionCode());
if (actionItem.isValid()) {
icon->setAction(icon->actionCode(), static_cast<char16_t>(actionItem.iconCode));
}
}
}

View file

@ -23,6 +23,8 @@
#include <QAction>
#include "actions/actiontypes.h"
#include "libmscore/score.h"
#include "libmscore/note.h"
#include "libmscore/chordrest.h"
@ -83,6 +85,8 @@
#include "palette/palette.h"
#include "translation.h"
using namespace mu::actions;
namespace Ms {
extern bool useFactorySettings;
@ -101,10 +105,9 @@ void populateIconPalette(Palette* p, const IconAction* a)
while (a->subtype != IconType::NONE) {
std::shared_ptr<Icon> ik = std::make_shared<Icon>(gscore);
ik->setIconType(a->subtype);
QAction* action = adapter->getAction(a->action);
QIcon icon(action->icon());
ik->setAction(a->action, icon);
p->append(ik, adapter->actionHelp(a->action));
ActionItem action = adapter->getAction(codeFromQString(a->action));
ik->setAction(a->action, static_cast<char16_t>(action.iconCode));
p->append(ik, QString::fromStdString(action.title));
++a;
}
}
@ -419,10 +422,9 @@ static void populateIconPalettePanel(PalettePanel* p, const IconAction* a)
while (a->subtype != IconType::NONE) {
Icon* ik = new Icon(gscore);
ik->setIconType(a->subtype);
QAction* action = adapter->getAction(a->action);
QIcon icon(action->icon());
ik->setAction(a->action, icon);
p->append(ik, adapter->actionHelp(a->action));
ActionItem action = adapter->getAction(codeFromQString(a->action));
ik->setAction(a->action, static_cast<char16_t>(action.iconCode));
p->append(ik, QString::fromStdString(action.title));
++a;
}
}
@ -568,21 +570,21 @@ PalettePanel* PaletteCreator::newAccidentalsPalettePanel(bool defaultPalettePane
Icon* ik = new Icon(gscore);
ik->setIconType(IconType::BRACKETS);
QAction* action = adapter()->getAction("add-brackets");
ik->setAction(QByteArray("add-brackets"), action->icon());
sp->append(ik, adapter()->actionHelp("add-brackets"));
ActionItem action = adapter()->getAction("add-brackets");
ik->setAction("add-brackets", static_cast<char16_t>(action.iconCode));
sp->append(ik, QString::fromStdString(action.title));
ik = new Icon(gscore);
ik->setIconType(IconType::PARENTHESES);
action = adapter()->getAction("add-parentheses");
ik->setAction(QByteArray("add-parentheses"), action->icon());
sp->append(ik, adapter()->actionHelp("add-parentheses"));
ik->setAction("add-parentheses", static_cast<char16_t>(action.iconCode));
sp->append(ik, QString::fromStdString(action.title));
ik = new Icon(gscore);
ik->setIconType(IconType::BRACES);
action = adapter()->getAction("add-braces");
ik->setAction(QByteArray("add-braces"), action->icon());
sp->append(ik, adapter()->actionHelp("add-braces"));
ik->setAction("add-braces", static_cast<char16_t>(action.iconCode));
sp->append(ik, QString::fromStdString(action.title));
return sp;
}
@ -851,12 +853,13 @@ PalettePanel* PaletteCreator::newNoteHeadsPalettePanel()
nh->setSym(sym);
sp->append(nh, NoteHead::group2userName(NoteHead::Group(i)));
}
Icon* ik = new Icon(gscore);
ik->setIconType(IconType::PARENTHESES);
QAction* action = adapter()->getAction("add-parentheses");
QIcon icon(action->icon());
ik->setAction("add-parentheses", icon);
sp->append(ik, adapter()->actionHelp("add-parentheses"));
ActionItem action = adapter()->getAction("add-parentheses");
ik->setAction("add-parentheses", static_cast<char16_t>(action.iconCode));
sp->append(ik, QString::fromStdString(action.title));
return sp;
}

View file

@ -24,6 +24,8 @@
#include "palette.h"
#include "palettetree.h"
#include "actions/actiontypes.h"
#include "libmscore/articulation.h"
#include "libmscore/fret.h"
#include "libmscore/icon.h"
@ -48,6 +50,7 @@
using namespace mu::palette;
using namespace mu::framework;
using namespace mu::actions;
namespace Ms {
//---------------------------------------------------------
@ -307,14 +310,15 @@ bool PaletteCell::read(XmlReader& e)
} else {
element->read(e);
element->styleChanged();
if (element->type() == ElementType::ICON) {
Icon* icon = static_cast<Icon*>(element.get());
QAction* ac = adapter()->getAction(icon->action());
if (ac) {
QIcon qicon(ac->icon());
icon->setAction(icon->action(), qicon);
ActionItem actionItem = adapter()->getAction(icon->actionCode());
if (actionItem.isValid()) {
icon->setAction(icon->actionCode(), static_cast<char16_t>(actionItem.iconCode));
} else {
add = false; // action is not valid, don't add it to the palette.
add = false;
}
}
}
@ -343,31 +347,27 @@ PaletteCellPtr PaletteCell::readElementMimeData(const QByteArray& data)
{
QPointF dragOffset;
Fraction duration(1, 4);
std::shared_ptr<Element> e(Element::readMimeData(gscore, data, &dragOffset, &duration));
ElementPtr element(Element::readMimeData(gscore, data, &dragOffset, &duration));
if (!e) {
if (!element) {
return nullptr;
}
if (!e->isSymbol()) { // not sure this check is necessary, it was so in the old palette
e->setTrack(0);
if (!element->isSymbol()) { // not sure this check is necessary, it was so in the old palette
element->setTrack(0);
}
if (e->isIcon()) {
Icon* i = toIcon(e.get());
const QByteArray& action = i->action();
if (!action.isEmpty()) {
QAction* a = adapter()->getAction(action);
if (a) {
QIcon icon(a->icon());
i->setAction(action, icon);
}
if (element->isIcon()) {
Icon* icon = toIcon(element.get());
ActionItem actionItem = adapter()->getAction(icon->actionCode());
if (actionItem.isValid()) {
icon->setAction(icon->actionCode(), static_cast<char16_t>(actionItem.iconCode));
}
}
const QString name = (e->isFretDiagram()) ? toFretDiagram(e.get())->harmonyText() : e->userName();
const QString name = (element->isFretDiagram()) ? toFretDiagram(element.get())->harmonyText() : element->userName();
return PaletteCellPtr(new PaletteCell(e, name));
return std::make_shared<PaletteCell>(element, name);
}
//---------------------------------------------------------
@ -916,8 +916,9 @@ PalettePanel::Type PalettePanel::guessType() const
case ElementType::SYMBOL:
return Type::Accordion;
case ElementType::ICON: {
const Icon* i = toIcon(e);
const QByteArray& action = i->action();
const Icon* icon = toIcon(e);
QString action = QString::fromStdString(icon->actionCode());
if (action.contains("beam")) {
return Type::Beam;
}

View file

@ -22,20 +22,13 @@
#include "modularity/imoduleexport.h"
#include <QString>
#include <QPointF>
#include "retval.h"
#include "async/notification.h"
class QAction;
#include "async/notification.h"
#include "actions/actiontypes.h"
namespace Ms {
class InstrumentChange;
class Score;
class ScriptRecorder;
enum class ViewState;
class Slur;
class Score;
class Element;
class PaletteWorkspace;
}
@ -50,8 +43,7 @@ class IPaletteAdapter : MODULE_EXPORT_INTERFACE
public:
virtual ~IPaletteAdapter() = default;
virtual QAction* getAction(const char* id) const = 0;
virtual QString actionHelp(const char* id) const = 0;
virtual actions::ActionItem getAction(const actions::ActionCode& code) const = 0;
virtual void showMasterPalette(const QString&) = 0;