1
0
mirror of https://github.com/QtExcel/QXlsx.git synced 2025-01-16 04:42:53 +08:00

Make more use of std::shared

This commit is contained in:
Daniel Nicoletti 2022-12-14 15:07:55 -03:00
parent 971843da44
commit 5488bedb65
7 changed files with 28 additions and 29 deletions

View File

@ -10,6 +10,8 @@
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <memory>
#include "xlsxglobal.h"
QT_BEGIN_NAMESPACE_XLSX
@ -86,7 +88,7 @@ protected:
Drawing *m_drawing;
ObjectType m_objectType;
QSharedPointer<MediaFile> m_pictureFile;
std::shared_ptr<MediaFile> m_pictureFile;
QSharedPointer<Chart> m_chartFile;
int m_id;

View File

@ -9,6 +9,8 @@
#include <QSharedPointer>
#include <QIODevice>
#include <memory>
#include "xlsxglobal.h"
#include "xlsxabstractooxmlfile.h"
#include "xlsxabstractsheet.h"
@ -61,8 +63,8 @@ public:
void setDefaultDateFormat(const QString &format);
//internal used member
void addMediaFile(QSharedPointer<MediaFile> media, bool force=false);
QList<QSharedPointer<MediaFile> > mediaFiles() const;
void addMediaFile(std::shared_ptr<MediaFile> media, bool force=false);
QList<std::shared_ptr<MediaFile> > mediaFiles() const;
void addChartFile(QSharedPointer<Chart> chartFile);
QList<QSharedPointer<Chart> > chartFiles() const;

View File

@ -44,7 +44,7 @@ public:
QStringList sheetNames;
QSharedPointer<Styles> styles;
QSharedPointer<Theme> theme;
QList<QSharedPointer<MediaFile> > mediaFiles;
QList<std::shared_ptr<MediaFile> > mediaFiles;
QList<QSharedPointer<Chart> > chartFiles;
QList<XlsxDefineNameData> definedNamesList;

View File

@ -135,10 +135,6 @@ struct XlsxColumnInfo
bool collapsed;
};
// #ifndef QMapIntSharedPointerCell
// typedef QMap<int, QSharedPointer<Cell> > QMapIntSharedPointerCell;
// #endif
class WorksheetPrivate : public AbstractSheetPrivate
{
Q_DECLARE_PUBLIC(Worksheet)

View File

@ -308,9 +308,8 @@ bool DocumentPrivate::loadPackage(QIODevice *device)
}
//load media files
QList<QSharedPointer<MediaFile> > mediaFileToLoad = workbook->mediaFiles();
for (int i=0; i<mediaFileToLoad.size(); ++i) {
QSharedPointer<MediaFile> mf = mediaFileToLoad[i];
const auto mediaFileToLoad = workbook->mediaFiles();
for (const auto &mf : mediaFileToLoad) {
const QString path = mf->fileName();
const QString suffix = path.mid(path.lastIndexOf(QLatin1Char('.'))+1);
mf->set(zipReader.fileData(path), suffix);
@ -433,9 +432,10 @@ bool DocumentPrivate::savePackage(QIODevice *device) const
}
// save image files
for (int i=0; i<workbook->mediaFiles().size(); ++i)
const auto mfs = workbook->mediaFiles();
for (int i=0; i < mfs.size(); ++i)
{
QSharedPointer<MediaFile> mf = workbook->mediaFiles()[i];
auto mf = mfs[i];
if (!mf->mimeType().isEmpty())
contentTypes->addDefault(mf->suffix(), mf->mimeType());
@ -1288,12 +1288,11 @@ Document::~Document()
bool Document::changeimage(int filenoinmidea, QString newfile)
{
Q_D(const Document);
QImage newpic;
QImage newpic(newfile);
newpic=QImage(newfile);
QList<QSharedPointer<MediaFile> > mediaFileToLoad = d->workbook->mediaFiles();
QSharedPointer<MediaFile> mf = mediaFileToLoad[filenoinmidea];
auto mediaFileToLoad = d->workbook->mediaFiles();
const auto mf = mediaFileToLoad[filenoinmidea];
const QString suffix = newfile.mid(newfile.lastIndexOf(QLatin1Char('.'))+1);
QString mimetypemy;

View File

@ -68,7 +68,7 @@ void DrawingAnchor::setObjectPicture(const QImage &img)
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "PNG");
m_pictureFile = QSharedPointer<MediaFile>(new MediaFile(ba, QStringLiteral("png"), QStringLiteral("image/png")));
m_pictureFile = std::make_shared<MediaFile>(ba, QStringLiteral("png"), QStringLiteral("image/png"));
m_drawing->workbook->addMediaFile(m_pictureFile);
m_objectType = Picture;
@ -91,7 +91,7 @@ void DrawingAnchor::setObjectShape(const QImage &img)
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "PNG");
m_pictureFile = QSharedPointer<MediaFile>(new MediaFile(ba, QStringLiteral("png"), QStringLiteral("image/png")));
m_pictureFile = std::make_shared<MediaFile>(ba, QStringLiteral("png"), QStringLiteral("image/png"));
m_drawing->workbook->addMediaFile(m_pictureFile);
m_objectType = Shape;
@ -385,16 +385,16 @@ void DrawingAnchor::loadXmlObjectPicture(QXmlStreamReader &reader)
QString path = QDir::cleanPath(parts.first() + QLatin1String("/") + name);
bool exist = false;
QList<QSharedPointer<MediaFile> > mfs = m_drawing->workbook->mediaFiles();
for (int i=0; i<mfs.size(); ++i) {
if (mfs[i]->fileName() == path) {
const auto mfs = m_drawing->workbook->mediaFiles();
for (const auto &mf : mfs) {
if (mf->fileName() == path) {
//already exist
exist = true;
m_pictureFile = mfs[i];
m_pictureFile = mf;
}
}
if (!exist) {
m_pictureFile = QSharedPointer<MediaFile> (new MediaFile(path));
m_pictureFile = std::make_shared<MediaFile>(path);
m_drawing->workbook->addMediaFile(m_pictureFile, true);
}
}
@ -794,7 +794,7 @@ void DrawingAnchor::saveXmlObjectGroupShape(QXmlStreamWriter &writer) const
void DrawingAnchor::saveXmlObjectPicture(QXmlStreamWriter &writer) const
{
Q_ASSERT(m_objectType == Picture && !m_pictureFile.isNull());
Q_ASSERT(m_objectType == Picture && m_pictureFile);
writer.writeStartElement(QStringLiteral("xdr:pic"));
@ -878,7 +878,7 @@ void DrawingAnchor::saveXmlObjectShape(QXmlStreamWriter &writer) const
writer.writeEmptyElement(QStringLiteral("a:avLst"));
writer.writeEndElement(); //a:prstGeom
if(!m_pictureFile.isNull()){
if(m_pictureFile){
m_drawing->relationships()->addDocumentRelationship(QStringLiteral("/image"), QStringLiteral("../media/image%1.%2").arg(m_pictureFile->index()+1).arg(m_pictureFile->suffix()));
writer.writeStartElement(QStringLiteral("a:blipFill"));
writer.writeAttribute(QStringLiteral("dpi"), QString::number(dpiTA));

View File

@ -686,7 +686,7 @@ bool Workbook::loadFromXmlFile(QIODevice *device)
/*!
* \internal
*/
QList<QSharedPointer<MediaFile> > Workbook::mediaFiles() const
QList<std::shared_ptr<MediaFile> > Workbook::mediaFiles() const
{
Q_D(const Workbook);
@ -696,7 +696,7 @@ QList<QSharedPointer<MediaFile> > Workbook::mediaFiles() const
/*!
* \internal
*/
void Workbook::addMediaFile(QSharedPointer<MediaFile> media, bool force)
void Workbook::addMediaFile(std::shared_ptr<MediaFile> media, bool force)
{
Q_D(Workbook);