Changeset View
Changeset View
Standalone View
Standalone View
wallpapers/image/image.cpp
Show First 20 Lines • Show All 176 Lines • ▼ Show 20 Line(s) | 176 | if (!package.isValid() || !package.filePath("preferred").isEmpty()) { | |||
---|---|---|---|---|---|
177 | return; | 177 | return; | ||
178 | } | 178 | } | ||
179 | 179 | | |||
180 | QStringList images = package.entryList("images"); | 180 | QStringList images = package.entryList("images"); | ||
181 | if (images.empty()) { | 181 | if (images.empty()) { | ||
182 | return; | 182 | return; | ||
183 | } | 183 | } | ||
184 | 184 | | |||
185 | //qDebug() << "wanted" << size; | 185 | //qDebug() << "wanted" << m_targetSize << "options" << images; | ||
186 | 186 | | |||
187 | // choose the nearest resolution | 187 | // choose the nearest resolution, always preferring images with the same aspect ratio | ||
188 | float best = FLT_MAX; | 188 | float best = FLT_MAX; | ||
189 | float bestWithSameAspectRatio = FLT_MAX; | ||||
190 | float targetAspectRatio = m_targetSize.width()/(float)m_targetSize.height(); | ||||
189 | 191 | | |||
190 | QString bestImage; | 192 | QString bestImage; | ||
193 | QString bestImageWithSameAspectRatio; | ||||
191 | foreach (const QString &entry, images) { | 194 | foreach (const QString &entry, images) { | ||
192 | QSize candidate = resSize(QFileInfo(entry).baseName()); | 195 | QSize candidate = resSize(QFileInfo(entry).baseName()); | ||
193 | if (candidate == QSize()) { | 196 | if (candidate == QSize()) { | ||
194 | continue; | 197 | continue; | ||
195 | } | 198 | } | ||
199 | float candidateAspectRatio = candidate.width()/(float)candidate.height(); | ||||
196 | 200 | | |||
197 | double dist = distance(candidate, m_targetSize); | 201 | double dist = distance(candidate, m_targetSize); | ||
198 | //qDebug() << "candidate" << candidate << "distance" << dist; | 202 | //qDebug() << "candidate" << candidate << "distance" << dist << "aspect ratio" << candidateAspectRatio; | ||
199 | if (bestImage.isEmpty() || dist < best) { | 203 | | ||
200 | bestImage = entry; | 204 | if ( candidateAspectRatio == targetAspectRatio && (bestImageWithSameAspectRatio.isEmpty() || dist < bestWithSameAspectRatio) ) { | ||
201 | best = dist; | 205 | bestImageWithSameAspectRatio = entry; | ||
202 | //qDebug() << "best" << bestImage; | 206 | bestWithSameAspectRatio = dist; | ||
207 | //qDebug() << "bestWithSameAspectRatio" << bestImageWithSameAspectRatio; | ||||
203 | if (dist == 0) { | 208 | if (dist == 0) { | ||
204 | break; | 209 | break; | ||
205 | } | 210 | } | ||
211 | } else if (bestImage.isEmpty() || dist < best) { | ||||
212 | bestImage = entry; | ||||
213 | best = dist; | ||||
214 | //qDebug() << "best" << bestImage; | ||||
206 | } | 215 | } | ||
207 | } | 216 | } | ||
208 | 217 | | |||
218 | if (!bestImageWithSameAspectRatio.isEmpty()) // Always prefer an image with the same aspect ratio as the target (if available) | ||||
219 | bestImage=bestImageWithSameAspectRatio; | ||||
220 | | ||||
209 | //qDebug() << "best image" << bestImage; | 221 | //qDebug() << "best image" << bestImage; | ||
210 | package.removeDefinition("preferred"); | 222 | package.removeDefinition("preferred"); | ||
211 | package.addFileDefinition("preferred", "images/" + bestImage, i18n("Recommended wallpaper file")); | 223 | package.addFileDefinition("preferred", "images/" + bestImage, i18n("Recommended wallpaper file")); | ||
212 | } | 224 | } | ||
213 | 225 | | |||
214 | QSize Image::targetSize() const | 226 | QSize Image::targetSize() const | ||
215 | { | 227 | { | ||
216 | return m_targetSize; | 228 | return m_targetSize; | ||
▲ Show 20 Lines • Show All 632 Lines • Show Last 20 Lines |