Changeset View
Changeset View
Standalone View
Standalone View
wallpapers/image/image.cpp
Show First 20 Lines • Show All 150 Lines • ▼ Show 20 Line(s) | 150 | } else { | |||
---|---|---|---|---|---|
151 | // we need to reset the prefered image | 151 | // we need to reset the prefered image | ||
152 | setSingleImage(); | 152 | setSingleImage(); | ||
153 | } | 153 | } | ||
154 | } | 154 | } | ||
155 | 155 | | |||
156 | float distance(const QSize& size, const QSize& desired) | 156 | float distance(const QSize& size, const QSize& desired) | ||
157 | { | 157 | { | ||
158 | // compute difference of areas | 158 | // compute difference of areas | ||
159 | float delta = size.width() * size.height() - | 159 | float desiredAspectRatio = ( desired.height() > 0 ) ? desired.width() / (float)desired.height() : 0; | ||
160 | desired.width() * desired.height(); | 160 | float candidateAspectRatio = ( size.height() > 0 ) ? size.width() / (float)size.height() : FLT_MAX; | ||
161 | // scale down to about 1.0 | 161 | | ||
162 | delta /= ((desired.width() * desired.height())+(size.width() * size.height()))/2; | 162 | float delta = size.width() - desired.width(); | ||
163 | delta = (delta >= 0.0 ? delta : -delta*2 ); // Penalize for scaling up | ||||
164 | | ||||
165 | return qAbs(candidateAspectRatio - desiredAspectRatio)*25000 + delta; | ||||
163 | 166 | | |||
164 | // Difference of areas, slight preference to scale down | | |||
165 | return delta >= 0.0 ? delta : -delta + 2.0; | | |||
166 | } | 167 | } | ||
167 | 168 | | |||
168 | QSize resSize(const QString &str) | 169 | QSize resSize(const QString &str) | ||
169 | { | 170 | { | ||
170 | int index = str.indexOf('x'); | 171 | int index = str.indexOf('x'); | ||
171 | if (index != -1) { | 172 | if (index != -1) { | ||
172 | return QSize(str.leftRef(index).toInt(), | 173 | return QSize(str.leftRef(index).toInt(), | ||
173 | str.midRef(index + 1).toInt()); | 174 | str.midRef(index + 1).toInt()); | ||
174 | } | 175 | } | ||
175 | 176 | | |||
176 | return QSize(); | 177 | return QSize(); | ||
177 | } | 178 | } | ||
178 | 179 | | |||
179 | void Image::findPreferedImageInPackage(KPackage::Package &package) | 180 | QString Image::findPreferedImage(const QStringList &images) | ||
180 | { | 181 | { | ||
181 | if (!package.isValid() || !package.filePath("preferred").isEmpty()) { | | |||
182 | return; | | |||
183 | } | | |||
184 | | ||||
185 | QStringList images = package.entryList("images"); | | |||
186 | if (images.empty()) { | 182 | if (images.empty()) { | ||
187 | return; | 183 | return QString(); | ||
188 | } | 184 | } | ||
189 | 185 | | |||
190 | //qDebug() << "wanted" << m_targetSize << "options" << images; | 186 | //float targetAspectRatio = (m_targetSize.height() > 0 ) ? m_targetSize.width() / (float)m_targetSize.height() : 0; | ||
191 | 187 | //qDebug() << "wanted" << m_targetSize << "options" << images << "aspect ratio" << targetAspectRatio; | |||
mart: remove dead code | |||||
192 | // choose the nearest resolution, always preferring images with the same aspect ratio | | |||
193 | float best = FLT_MAX; | 188 | float best = FLT_MAX; | ||
194 | float bestWithSameAspectRatio = FLT_MAX; | | |||
195 | float targetAspectRatio = m_targetSize.width()/(float)m_targetSize.height(); | | |||
196 | 189 | | |||
197 | QString bestImage; | 190 | QString bestImage; | ||
198 | QString bestImageWithSameAspectRatio; | | |||
199 | foreach (const QString &entry, images) { | 191 | foreach (const QString &entry, images) { | ||
200 | QSize candidate = resSize(QFileInfo(entry).baseName()); | 192 | QSize candidate = resSize(QFileInfo(entry).baseName()); | ||
201 | if (candidate == QSize()) { | 193 | if (candidate == QSize()) { | ||
202 | continue; | 194 | continue; | ||
203 | } | 195 | } | ||
204 | float candidateAspectRatio = candidate.width()/(float)candidate.height(); | 196 | //float candidateAspectRatio = (candidate.height() > 0 ) ? candidate.width() / (float)candidate.height() : FLT_MAX; | ||
mart: dead code | |||||
The commented out qDebug a couple of lines below shows that variable (when uncommented). Should I remove it from the qDebug line below? or just keep both? antlarr: The commented out qDebug a couple of lines below shows that variable (when uncommented). Should… | |||||
205 | 197 | | |||
206 | double dist = distance(candidate, m_targetSize); | 198 | float dist = distance(candidate, m_targetSize); | ||
207 | //qDebug() << "candidate" << candidate << "distance" << dist << "aspect ratio" << candidateAspectRatio; | 199 | //qDebug() << "candidate" << candidate << "distance" << dist << "aspect ratio" << candidateAspectRatio; | ||
208 | 200 | | |||
209 | if ( candidateAspectRatio == targetAspectRatio && (bestImageWithSameAspectRatio.isEmpty() || dist < bestWithSameAspectRatio) ) { | 201 | if (bestImage.isEmpty() || dist < best) { | ||
210 | bestImageWithSameAspectRatio = entry; | | |||
211 | bestWithSameAspectRatio = dist; | | |||
212 | //qDebug() << "bestWithSameAspectRatio" << bestImageWithSameAspectRatio; | | |||
213 | if (dist == 0) { | | |||
214 | break; | | |||
215 | } | | |||
216 | } else if (bestImage.isEmpty() || dist < best) { | | |||
217 | bestImage = entry; | 202 | bestImage = entry; | ||
218 | best = dist; | 203 | best = dist; | ||
219 | //qDebug() << "best" << bestImage; | 204 | //qDebug() << "best" << bestImage; | ||
220 | } | 205 | } | ||
221 | } | 206 | } | ||
222 | 207 | | |||
223 | if (!bestImageWithSameAspectRatio.isEmpty()) // Always prefer an image with the same aspect ratio as the target (if available) | | |||
224 | bestImage=bestImageWithSameAspectRatio; | | |||
225 | | ||||
226 | //qDebug() << "best image" << bestImage; | 208 | //qDebug() << "best image" << bestImage; | ||
209 | return bestImage; | ||||
210 | } | ||||
211 | | ||||
212 | void Image::findPreferedImageInPackage(KPackage::Package &package) | ||||
213 | { | ||||
214 | if (!package.isValid() || !package.filePath("preferred").isEmpty()) { | ||||
215 | return; | ||||
216 | } | ||||
217 | | ||||
218 | QString preferred = findPreferedImage( package.entryList("images") ); | ||||
219 | | ||||
227 | package.removeDefinition("preferred"); | 220 | package.removeDefinition("preferred"); | ||
228 | package.addFileDefinition("preferred", "images/" + bestImage, i18n("Recommended wallpaper file")); | 221 | package.addFileDefinition("preferred", "images/" + preferred, i18n("Recommended wallpaper file")); | ||
229 | } | 222 | } | ||
230 | 223 | | |||
231 | QSize Image::targetSize() const | 224 | QSize Image::targetSize() const | ||
232 | { | 225 | { | ||
233 | return m_targetSize; | 226 | return m_targetSize; | ||
234 | } | 227 | } | ||
235 | 228 | | |||
236 | void Image::setTargetSize(const QSize &size) | 229 | void Image::setTargetSize(const QSize &size) | ||
▲ Show 20 Lines • Show All 629 Lines • Show Last 20 Lines |
remove dead code