|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <QImageReader> |
|
19 #include <QTime> |
|
20 |
|
21 #include "hswallpaperimagereader.h" |
|
22 |
|
23 /*! |
|
24 Constructor. |
|
25 */ |
|
26 HsWallpaperImageReader::HsWallpaperImageReader(QObject *parent): |
|
27 QObject(parent), |
|
28 mCenterTarget(false) |
|
29 { |
|
30 |
|
31 } |
|
32 |
|
33 /*! |
|
34 Destructor. |
|
35 */ |
|
36 HsWallpaperImageReader::~HsWallpaperImageReader() |
|
37 { |
|
38 |
|
39 } |
|
40 |
|
41 /*! |
|
42 Saves image source path |
|
43 */ |
|
44 void HsWallpaperImageReader::setSourcePath(const QString &sourcePath) |
|
45 { |
|
46 mSourcePath = sourcePath; |
|
47 } |
|
48 |
|
49 /*! |
|
50 Returns image source path |
|
51 */ |
|
52 QString HsWallpaperImageReader::getSourcePath() const |
|
53 { |
|
54 return mSourcePath; |
|
55 } |
|
56 |
|
57 /*! |
|
58 Saves image source rect |
|
59 */ |
|
60 void HsWallpaperImageReader::setSourceRect(const QRect &sourceRect) |
|
61 { |
|
62 mSourceRect = sourceRect; |
|
63 } |
|
64 |
|
65 /*! |
|
66 Returns source rect |
|
67 */ |
|
68 QRect HsWallpaperImageReader::getSourceRect() const |
|
69 { |
|
70 return mSourceRect; |
|
71 } |
|
72 |
|
73 /*! |
|
74 Saves image target rect |
|
75 */ |
|
76 void HsWallpaperImageReader::setTargetRect(const QRect &targetRect) |
|
77 { |
|
78 mTargetRect = targetRect; |
|
79 } |
|
80 |
|
81 /*! |
|
82 Returns target rect |
|
83 */ |
|
84 QRect HsWallpaperImageReader::getTargetRect() const |
|
85 { |
|
86 return mTargetRect; |
|
87 } |
|
88 |
|
89 /*! |
|
90 Centers image target rect |
|
91 */ |
|
92 void HsWallpaperImageReader::setCenterTarget(bool center) |
|
93 { |
|
94 mCenterTarget = center; |
|
95 } |
|
96 |
|
97 /*! |
|
98 Returns target centering |
|
99 */ |
|
100 bool HsWallpaperImageReader::getCenterTarget() |
|
101 { |
|
102 return mCenterTarget; |
|
103 } |
|
104 |
|
105 /*! |
|
106 Returns processed image |
|
107 */ |
|
108 QImage HsWallpaperImageReader::getProcessedImage() const |
|
109 { |
|
110 return mProcessedImage; |
|
111 } |
|
112 |
|
113 /*! |
|
114 \internal |
|
115 Scales and crops (if needed) image using target rect. |
|
116 Centers target rect automatically if mCenterTarget is true. |
|
117 Pass empty set sourceRect to empty to use full size source image as starting point. |
|
118 Returns processed image or null image if operation fails. |
|
119 */ |
|
120 void HsWallpaperImageReader::processImage() |
|
121 { |
|
122 QImageReader imageReader(mSourcePath); |
|
123 |
|
124 QRect tempTargetRect = mTargetRect; |
|
125 QRect tempSourceRect = mSourceRect; |
|
126 |
|
127 if (imageReader.canRead()) { |
|
128 QSize sourceSize = imageReader.size(); |
|
129 if (tempSourceRect.isEmpty()) { |
|
130 // If sourceRect not defined, uses full size image as source. |
|
131 tempSourceRect.setRect(0, 0, sourceSize.width(), sourceSize.height()); |
|
132 } |
|
133 sourceSize.scale(tempTargetRect.width(), tempTargetRect.height(), |
|
134 Qt::KeepAspectRatioByExpanding); |
|
135 imageReader.setScaledSize(sourceSize); |
|
136 |
|
137 if (mCenterTarget) { |
|
138 tempTargetRect.moveCenter(QPoint(sourceSize.width() / 2, sourceSize.height() / 2)); |
|
139 } |
|
140 imageReader.setScaledClipRect(tempTargetRect); |
|
141 mProcessedImage = imageReader.read(); |
|
142 } else { |
|
143 mProcessedImage = QImage(); |
|
144 } |
|
145 emit processingFinished(); |
|
146 } |