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 : #pragma once
24 :
25 : #include "modularity/ioc.h"
26 :
27 : #include "draw/types/pixmap.h"
28 :
29 : #include "bsymbol.h"
30 :
31 : namespace muse::draw {
32 : class IImageProvider;
33 : class Pixmap;
34 : class SvgRenderer;
35 : }
36 :
37 : namespace muse::io {
38 : struct path_t;
39 : }
40 :
41 : namespace mu::engraving {
42 : class ImageStoreItem;
43 :
44 : enum class ImageType : char {
45 : NONE, RASTER, SVG
46 : };
47 :
48 : //---------------------------------------------------------
49 : // @@ Image
50 : //---------------------------------------------------------
51 :
52 : class Image final : public BSymbol
53 : {
54 60 : OBJECT_ALLOCATOR(engraving, Image)
55 1625 : DECLARE_CLASSOF(ElementType::IMAGE)
56 :
57 : muse::GlobalInject<muse::draw::IImageProvider> imageProvider;
58 :
59 : public:
60 : Image(DummyParentOr<EngravingItem> parent);
61 : Image(const Image&);
62 : ~Image();
63 :
64 6 : Image* clone() const override { return new Image(*this); }
65 :
66 : bool loadFromStore(const std::string& storePath);
67 : bool loadFromFile(muse::io::path_t path);
68 : bool loadFromData(const std::string& suffix, const muse::ByteArray&);
69 :
70 : void init();
71 :
72 : bool isImageFramed() const;
73 : double imageAspectRatio() const;
74 81 : void setSize(const SizeF& s) { m_size = s; }
75 111 : const SizeF& size() const { return m_size; }
76 : void updateImageHeight(const double& height);
77 : void updateImageWidth(const double& width);
78 : double imageHeight() const;
79 : double imageWidth() const;
80 23 : bool lockAspectRatio() const { return m_lockAspectRatio; }
81 0 : void setLockAspectRatio(bool v) { m_lockAspectRatio = v; }
82 83 : bool autoScale() const { return m_autoScale; }
83 0 : void setAutoScale(bool v) { m_autoScale = v; }
84 42 : ImageStoreItem* storeItem() const { return m_storeItem; }
85 23 : bool sizeIsSpatium() const { return m_sizeIsSpatium; }
86 0 : void setSizeIsSpatium(bool val) { m_sizeIsSpatium = val; }
87 :
88 : const std::string& storePath() const { return m_storePath; }
89 : void setStorePath(const std::string& p) { m_storePath = p; }
90 :
91 : PropertyValue getProperty(Pid) const override;
92 : bool setProperty(Pid propertyId, const PropertyValue&) override;
93 : PropertyValue propertyDefault(Pid id) const override;
94 :
95 : SizeF imageSize() const;
96 :
97 : void setImageType(ImageType);
98 0 : ImageType imageType() const { return m_imageType; }
99 34 : bool isValid() const { return m_rasterDoc || m_svgDoc; }
100 :
101 0 : muse::draw::SvgRenderer* svgRenderer() const { return m_svgDoc; }
102 0 : const std::shared_ptr<muse::draw::Pixmap>& rasterImage() const { return m_rasterDoc; }
103 :
104 0 : bool needStartEditingAfterSelecting() const override { return true; }
105 0 : int gripsCount() const override { return 2; }
106 0 : Grip initialEditModeGrip() const override { return Grip(1); }
107 0 : Grip defaultGrip() const override { return Grip(1); }
108 : std::vector<PointF> gripsPositions(const EditData&) const override;
109 :
110 : SizeF pixel2size(const SizeF& s) const;
111 : SizeF size2pixel(const SizeF& s) const;
112 :
113 : const muse::draw::Pixmap& buffer() const { return m_buffer; }
114 : void setBuffer(const muse::draw::Pixmap& p) const { m_buffer = p; }
115 : bool dirty() const { return m_dirty; }
116 : void setDirty(bool val) const { m_dirty = val; }
117 :
118 : private:
119 :
120 0 : bool isEditable() const override { return true; }
121 : void startDragGrip(EditData&) override;
122 : void dragGrip(EditData& ed) override;
123 0 : std::vector<LineF> gripAnchorLines(Grip) const override { return std::vector<LineF>(); }
124 :
125 : ImageStoreItem* m_storeItem = nullptr;
126 : std::string m_storePath; // the path of the img in the ImageStore
127 : mutable muse::draw::Pixmap m_buffer; // cached rendering
128 : SizeF m_size; // in absolute or spatium units
129 : bool m_lockAspectRatio = false;
130 : bool m_autoScale = false; // fill parent frame
131 : bool m_sizeIsSpatium = false;
132 : mutable bool m_dirty = false;
133 :
134 : std::shared_ptr<muse::draw::Pixmap> m_rasterDoc;
135 : muse::draw::SvgRenderer* m_svgDoc = nullptr;
136 :
137 : ImageType m_imageType = ImageType::NONE;
138 : };
139 : }
|