Messagelib

imagescaling.cpp
1 /*
2  SPDX-FileCopyrightText: 2012-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "imagescaling.h"
8 #include "settings/messagecomposersettings.h"
9 #include <QBuffer>
10 #include <QImage>
11 using namespace MessageComposer;
12 
13 class MessageComposer::ImageScalingPrivate
14 {
15 public:
16  ImageScalingPrivate() = default;
17 
18  QImage mImage;
19  QBuffer mBuffer;
20  QString mName;
21  QByteArray mMimeType;
22 };
23 
24 ImageScaling::ImageScaling()
25  : d(new MessageComposer::ImageScalingPrivate)
26 {
27 }
28 
29 ImageScaling::~ImageScaling() = default;
30 
31 bool ImageScaling::loadImageFromData(const QByteArray &data)
32 {
33  if (!d->mImage.loadFromData(data)) {
34  return false;
35  }
36  return true;
37 }
38 
39 bool ImageScaling::resizeImage()
40 {
41  if (d->mImage.isNull()) {
42  return false;
43  }
44  const int width = d->mImage.width();
45  const int height = d->mImage.height();
46  int newWidth = -1;
47  int newHeight = -1;
48  if (MessageComposer::MessageComposerSettings::self()->reduceImageToMaximum()) {
49  int maximumWidth = MessageComposer::MessageComposerSettings::self()->maximumWidth();
50  if (maximumWidth == -1) {
51  maximumWidth = MessageComposer::MessageComposerSettings::self()->customMaximumWidth();
52  }
53  int maximumHeight = MessageComposer::MessageComposerSettings::self()->maximumHeight();
54  if (maximumHeight == -1) {
55  maximumHeight = MessageComposer::MessageComposerSettings::self()->customMaximumHeight();
56  }
57  if (width > maximumWidth) {
58  newWidth = maximumWidth;
59  } else {
60  newWidth = width;
61  }
62  if (height > maximumHeight) {
63  newHeight = maximumHeight;
64  } else {
65  newHeight = height;
66  }
67  } else {
68  newHeight = height;
69  newWidth = width;
70  }
71  if (MessageComposer::MessageComposerSettings::self()->enlargeImageToMinimum()) {
72  int minimumWidth = MessageComposer::MessageComposerSettings::self()->minimumWidth();
73  if (minimumWidth == -1) {
74  minimumWidth = MessageComposer::MessageComposerSettings::self()->customMinimumWidth();
75  }
76 
77  int minimumHeight = MessageComposer::MessageComposerSettings::self()->minimumHeight();
78  if (minimumHeight == -1) {
79  minimumHeight = MessageComposer::MessageComposerSettings::self()->customMinimumHeight();
80  }
81  if (newWidth < minimumWidth) {
82  newWidth = minimumWidth;
83  }
84  if (newHeight < minimumHeight) {
85  newHeight = minimumHeight;
86  }
87  }
88  if ((newHeight != height) || (newWidth != width)) {
89  d->mBuffer.open(QIODevice::WriteOnly);
90  d->mImage = d->mImage.scaled(newWidth,
91  newHeight,
92  MessageComposer::MessageComposerSettings::self()->keepImageRatio() ? Qt::KeepAspectRatio : Qt::IgnoreAspectRatio);
93 
94  QByteArray format;
95  if (d->mMimeType == "image/jpeg") {
96  format = QByteArrayLiteral("JPG");
97  } else if (d->mMimeType == "image/png") {
98  format = QByteArrayLiteral("PNG");
99  } else {
100  format = MessageComposer::MessageComposerSettings::self()->writeFormat().toLocal8Bit();
101  if (format.isEmpty()) {
102  format = QByteArrayLiteral("PNG");
103  }
104  }
105  const bool result = d->mImage.save(&d->mBuffer, format.constData());
106  d->mBuffer.close();
107  return result;
108  } else {
109  return false;
110  }
111  return true;
112 }
113 
114 QByteArray ImageScaling::mimetype() const
115 {
116  if (d->mMimeType.isEmpty()) {
117  return {};
118  }
119  if ((d->mMimeType == "image/jpeg") || (d->mMimeType == "image/png")) {
120  return d->mMimeType;
121  } else {
122  // Add more mimetype if a day we add more saving format.
123  const QString type = MessageComposer::MessageComposerSettings::self()->writeFormat();
124  if (type == QLatin1String("JPG")) {
125  return "image/jpeg";
126  } else {
127  return "image/png";
128  }
129  }
130 }
131 
132 void ImageScaling::setMimetype(const QByteArray &mimetype)
133 {
134  d->mMimeType = mimetype;
135 }
136 
137 void ImageScaling::setName(const QString &name)
138 {
139  d->mName = name;
140 }
141 
142 QByteArray ImageScaling::imageArray() const
143 {
144  return d->mBuffer.data();
145 }
146 
147 QString ImageScaling::generateNewName()
148 {
149  if (d->mName.isEmpty()) {
150  return {};
151  }
152 
153  // Don't rename it.
154  if ((d->mMimeType == "image/jpeg") || (d->mMimeType == "image/png")) {
155  return d->mName;
156  }
157  QString type = MessageComposer::MessageComposerSettings::self()->writeFormat();
158  if (type.isEmpty()) {
159  type = QStringLiteral("PNG");
160  }
161  if (d->mName.endsWith(QLatin1String(".png"))) {
162  if (type != QLatin1String("PNG")) {
163  d->mName.replace(QLatin1String(".png"), QLatin1String(".jpg"));
164  }
165  } else if (d->mName.endsWith(QLatin1String(".jpg"))) {
166  if (type != QLatin1String("JPG")) {
167  d->mName.replace(QLatin1String(".jpg"), QLatin1String(".png"));
168  }
169  } else {
170  if (type == QLatin1String("PNG")) {
171  d->mName += QLatin1String(".png");
172  } else {
173  d->mName += QLatin1String(".jpg");
174  }
175  }
176  return d->mName;
177 }
bool isEmpty() const const
const QLatin1String name
PartitionTable::TableType type
bool isEmpty() const const
const char * constData() const const
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
KeepAspectRatio
char * data()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:04:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.