fixed displaying images in the score view

This commit is contained in:
Roman Pudashkin 2021-09-23 14:13:24 +02:00
parent 9b5c0f2e12
commit 20743f0da0
6 changed files with 60 additions and 51 deletions

View file

@ -37,13 +37,16 @@ namespace mu::draw {
class IImageProvider : MODULE_EXPORT_INTERFACE
{
INTERFACE_ID(IImageProvider)
public:
virtual ~IImageProvider() = default;
virtual std::shared_ptr<Pixmap> createPixmap(const QByteArray& data) const = 0;
virtual std::shared_ptr<Pixmap> createPixmap(int w, int h, int dpm, const Color& color) const = 0;
virtual Pixmap scaled(const Pixmap& origin, const Size& s) const = 0;
virtual IPaintProviderPtr painterForImage(std::shared_ptr<Pixmap> pixmap) = 0;
virtual std::shared_ptr<Pixmap> createPixmap(int w, int h, int dpm, const Color& color) = 0;
virtual void saveAsPng(std::shared_ptr<Pixmap> px, QIODevice* device) = 0;
virtual std::shared_ptr<Pixmap> pixmapFromQVariant(const QVariant& val) = 0;

View file

@ -47,12 +47,6 @@ public:
bool isNull() const { return m_data.isNull(); }
void setData(const QByteArray& data)
{
m_data = data;
m_key = qHash(data);
}
uint key() const { return m_key; }
#ifndef NO_QT_SUPPORT
@ -80,6 +74,12 @@ public:
#endif
private:
void setData(const QByteArray& data)
{
m_data = data;
m_key = qHash(data);
}
Size m_size;
QByteArray m_data; //! usually png
uint m_key;

View file

@ -10,6 +10,24 @@ using namespace mu::draw;
static const char FILE_FORMAT[] = "PNG";
std::shared_ptr<Pixmap> QImageProvider::createPixmap(const QByteArray& data) const
{
QImage image;
image.loadFromData(data);
return std::make_shared<Pixmap>(Pixmap::fromQPixmap(QPixmap::fromImage(image)));
}
std::shared_ptr<Pixmap> QImageProvider::createPixmap(int w, int h, int dpm, const Color& color) const
{
QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
image.setDotsPerMeterX(dpm);
image.setDotsPerMeterY(dpm);
image.fill(color.toQColor());
return std::make_shared<Pixmap>(Pixmap::fromQPixmap(QPixmap::fromImage(image)));
}
Pixmap QImageProvider::scaled(const Pixmap& origin, const Size& s) const
{
QPixmap qtPixmap = Pixmap::toQPixmap(origin);
@ -41,13 +59,3 @@ void QImageProvider::saveAsPng(std::shared_ptr<Pixmap> px, QIODevice* device)
{
Pixmap::toQPixmap(*px).save(device, FILE_FORMAT);
}
std::shared_ptr<Pixmap> QImageProvider::createPixmap(int w, int h, int dpm, const Color& color)
{
QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
image.setDotsPerMeterX(dpm);
image.setDotsPerMeterY(dpm);
image.fill(color.toQColor());
return std::make_shared<Pixmap>(Pixmap::fromQPixmap(QPixmap::fromImage(image)));
}

View file

@ -28,9 +28,10 @@ namespace mu::draw {
class QImageProvider : public IImageProvider
{
public:
Pixmap scaled(const Pixmap& origin, const Size& s) const override;
std::shared_ptr<Pixmap> createPixmap(const QByteArray& data) const override;
std::shared_ptr<Pixmap> createPixmap(int w, int h, int dpm, const Color& color) const override;
std::shared_ptr<Pixmap> createPixmap(int w, int h, int dpm, const Color& color) override;
Pixmap scaled(const Pixmap& origin, const Size& s) const override;
IPaintProviderPtr painterForImage(std::shared_ptr<Pixmap> pixmap) override;
void saveAsPng(std::shared_ptr<Pixmap> px, QIODevice* device) override;

View file

@ -53,7 +53,6 @@ Image::Image(EngravingItem* parent)
: BSymbol(ElementType::IMAGE, parent, ElementFlag::MOVABLE)
{
imageType = ImageType::NONE;
rasterDoc = 0;
_size = SizeF(0.0, 0.0);
_storeItem = 0;
_dirty = false;
@ -80,7 +79,7 @@ Image::Image(const Image& img)
_linkPath = img._linkPath;
_linkIsValid = img._linkIsValid;
if (imageType == ImageType::RASTER) {
rasterDoc = img.rasterDoc ? new Pixmap(*img.rasterDoc) : 0;
rasterDoc = img.rasterDoc ? std::make_shared<Pixmap>(*img.rasterDoc) : nullptr;
} else if (imageType == ImageType::SVG) {
svgDoc = img.svgDoc ? new SvgRenderer(_storeItem->buffer()) : 0;
}
@ -98,8 +97,6 @@ Image::~Image()
}
if (imageType == ImageType::SVG) {
delete svgDoc;
} else if (imageType == ImageType::RASTER) {
delete rasterDoc;
}
}
@ -113,7 +110,7 @@ void Image::setImageType(ImageType t)
if (imageType == ImageType::SVG) {
svgDoc = 0;
} else if (imageType == ImageType::RASTER) {
rasterDoc = 0;
rasterDoc.reset();
} else {
qDebug("illegal image type");
}
@ -551,8 +548,7 @@ void Image::layout()
}
} else if (imageType == ImageType::RASTER && !rasterDoc) {
if (_storeItem) {
rasterDoc = new Pixmap;
rasterDoc->setData(_storeItem->buffer());
rasterDoc = imageProvider()->createPixmap(_storeItem->buffer());
if (!rasterDoc->isNull()) {
_dirty = true;
}

View file

@ -47,31 +47,6 @@ enum class ImageType : char {
class Image final : public BSymbol
{
INJECT(engraving, mu::draw::IImageProvider, imageProvider)
union {
mu::draw::Pixmap* rasterDoc;
mu::draw::SvgRenderer* svgDoc;
};
ImageType imageType;
mu::SizeF pixel2size(const mu::SizeF& s) const;
mu::SizeF size2pixel(const mu::SizeF& s) const;
protected:
ImageStoreItem* _storeItem;
QString _storePath; // the path of the img in the ImageStore
QString _linkPath; // the path of an external linked img
bool _linkIsValid; // whether _linkPath file exists or not
mutable mu::draw::Pixmap buffer; ///< cached rendering
mu::SizeF _size; // in mm or spatium units
bool _lockAspectRatio;
bool _autoScale; ///< fill parent frame
bool _sizeIsSpatium;
mutable bool _dirty;
bool isEditable() const override { return true; }
void startEditDrag(EditData&) override;
void editDrag(EditData& ed) override;
QVector<mu::LineF> gripAnchorLines(Grip) const override { return QVector<mu::LineF>(); }
public:
Image(EngravingItem* parent = 0);
@ -118,6 +93,32 @@ public:
Grip initialEditModeGrip() const override { return Grip(1); }
Grip defaultGrip() const override { return Grip(1); }
std::vector<mu::PointF> gripsPositions(const EditData&) const override;
protected:
ImageStoreItem* _storeItem;
QString _storePath; // the path of the img in the ImageStore
QString _linkPath; // the path of an external linked img
bool _linkIsValid; // whether _linkPath file exists or not
mutable mu::draw::Pixmap buffer; ///< cached rendering
mu::SizeF _size; // in mm or spatium units
bool _lockAspectRatio;
bool _autoScale; ///< fill parent frame
bool _sizeIsSpatium;
mutable bool _dirty;
bool isEditable() const override { return true; }
void startEditDrag(EditData&) override;
void editDrag(EditData& ed) override;
QVector<mu::LineF> gripAnchorLines(Grip) const override { return QVector<mu::LineF>(); }
private:
mu::SizeF pixel2size(const mu::SizeF& s) const;
mu::SizeF size2pixel(const mu::SizeF& s) const;
std::shared_ptr<mu::draw::Pixmap> rasterDoc;
mu::draw::SvgRenderer* svgDoc = nullptr;
ImageType imageType = ImageType::NONE;
};
} // namespace Ms
#endif