21 ** If you have questions regarding the use of this file, please contact |
21 ** If you have questions regarding the use of this file, please contact |
22 ** Nokia at developer.feedback@nokia.com. |
22 ** Nokia at developer.feedback@nokia.com. |
23 ** |
23 ** |
24 ****************************************************************************/ |
24 ****************************************************************************/ |
25 |
25 |
26 #include <QGraphicsSceneResizeEvent> |
|
27 #include <QPainter> |
|
28 #include <QObject> |
|
29 #include <QDebug> |
|
30 |
|
31 #include "hbbackgrounditem_p.h" |
26 #include "hbbackgrounditem_p.h" |
32 #include "hbwidget_p.h" |
27 #include "hbwidget_p.h" |
33 #include "hbinstance.h" |
28 #include "hbinstance.h" |
34 #include "hbdeviceprofile.h" |
29 #include "hbdeviceprofile.h" |
35 #include "hbevent.h" |
30 #include "hbevent.h" |
36 #include "hbmainwindow_p.h" |
31 #include "hbmainwindow_p.h" |
37 |
32 #include <QGraphicsSceneResizeEvent> |
38 #ifndef HB_NVG_CS_ICON |
33 #include <QPainter> |
39 #define ENABLE_FAST_PAINT_ |
34 #include <QObject> |
40 #endif |
35 #include <QDebug> |
41 |
36 |
42 /* |
37 /* |
43 \class HbBackgroundItem |
38 \class HbBackgroundItem |
44 |
39 |
45 \brief HbBackgroundItem draws background |
40 \brief Draws the background. |
46 |
41 |
47 \internal |
42 \internal |
48 */ |
43 */ |
49 |
44 |
50 HbBackgroundItem::HbBackgroundItem(HbMainWindow *mainWindow, QGraphicsWidget *parent) : |
45 HbBackgroundItem::HbBackgroundItem(HbMainWindow *mainWindow, QGraphicsWidget *parent) : |
51 HbWidget(parent), |
46 HbWidget(parent), |
52 mMainWindow(mainWindow) |
47 mMainWindow(mainWindow), |
|
48 mImageMode(Hb::ScaleBackgroundToFit) |
53 { |
49 { |
54 #ifdef ENABLE_FAST_PAINT_ |
50 setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); |
55 setAttribute(Qt::WA_NoSystemBackground); // Disable clearing of background |
|
56 #endif |
|
57 setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored ); |
|
58 |
|
59 mPrtImageName = defaultImageName(Qt::Vertical); |
51 mPrtImageName = defaultImageName(Qt::Vertical); |
60 mLscImageName = defaultImageName(Qt::Horizontal); |
52 mLscImageName = defaultImageName(Qt::Horizontal); |
61 updateBackgroundImage(); |
53 updateBackgroundImage(); |
62 } |
54 } |
63 |
55 |
90 } |
82 } |
91 |
83 |
92 QString HbBackgroundItem::defaultImageName(Qt::Orientation orientation) const |
84 QString HbBackgroundItem::defaultImageName(Qt::Orientation orientation) const |
93 { |
85 { |
94 return orientation == Qt::Vertical |
86 return orientation == Qt::Vertical |
95 ? QLatin1String("qtg_graf_screen_bg_prt") |
87 ? QLatin1String("qtg_graf_screen_bg_prt") |
96 : QLatin1String("qtg_graf_screen_bg_lsc"); |
88 : QLatin1String("qtg_graf_screen_bg_lsc"); |
|
89 } |
|
90 |
|
91 void HbBackgroundItem::setImageMode(Hb::BackgroundImageMode mode) |
|
92 { |
|
93 if (mode != mImageMode) { |
|
94 mImageMode = mode; |
|
95 updateBackgroundImage(); |
|
96 } |
|
97 } |
|
98 |
|
99 Hb::BackgroundImageMode HbBackgroundItem::imageMode() const |
|
100 { |
|
101 return mImageMode; |
97 } |
102 } |
98 |
103 |
99 void HbBackgroundItem::updateBackgroundImage() |
104 void HbBackgroundItem::updateBackgroundImage() |
100 { |
105 { |
101 prepareGeometryChange(); |
106 prepareGeometryChange(); |
102 if (mMainWindow) { |
107 if (mMainWindow) { |
103 QSizeF size(HbDeviceProfile::profile(mMainWindow).logicalSize()); |
108 QSizeF size(HbDeviceProfile::profile(mMainWindow).logicalSize()); |
104 mBoundingRect.setWidth(size.width()); |
109 mBoundingRect.setWidth(size.width()); |
105 mBoundingRect.setHeight(size.height()); |
110 mBoundingRect.setHeight(size.height()); |
106 mBackground.setSize(size); |
|
107 if (mMainWindow->orientation() == Qt::Vertical) { |
111 if (mMainWindow->orientation() == Qt::Vertical) { |
108 mBackground.setIconName(mPrtImageName); |
112 mBackground.setIconName(mPrtImageName); |
109 } else { |
113 } else { |
110 mBackground.setIconName(mLscImageName); |
114 mBackground.setIconName(mLscImageName); |
111 } |
115 } |
|
116 if (mImageMode == Hb::KeepOriginalBackgroundSize |
|
117 || mImageMode == Hb::KeepOriginalBackgroundSizeIfSmaller) { |
|
118 QSizeF imageSize = mBackground.defaultSize(); |
|
119 if (mImageMode == Hb::KeepOriginalBackgroundSize |
|
120 || (imageSize.width() <= size.width() && imageSize.height() <= size.height())) { |
|
121 size = imageSize; |
|
122 } |
|
123 } |
|
124 mBackground.setSize(size); |
112 } |
125 } |
113 } |
126 } |
114 |
127 |
115 void HbBackgroundItem::resizeEvent(QGraphicsSceneResizeEvent *event) |
128 void HbBackgroundItem::resizeEvent(QGraphicsSceneResizeEvent *event) |
116 { |
129 { |
145 return mBoundingRect; |
158 return mBoundingRect; |
146 } |
159 } |
147 |
160 |
148 void HbBackgroundItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
161 void HbBackgroundItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
149 { |
162 { |
150 Q_UNUSED(widget) |
163 Q_UNUSED(widget); |
151 Q_UNUSED(option) |
164 Q_UNUSED(option); |
152 |
165 |
153 #ifdef ENABLE_FAST_PAINT_ |
166 if (mImageMode == Hb::DoNotDrawBackground) { |
154 QPainter::CompositionMode compositionMode = painter->compositionMode(); |
167 return; |
155 painter->setCompositionMode( QPainter::CompositionMode_Source ); // Do not use alpha blending.. |
168 } |
156 #endif |
|
157 |
169 |
158 mBackground.paint(painter, mBoundingRect, Qt::KeepAspectRatioByExpanding); |
170 // Note: No optimizations to disable alpha blending etc. The background |
|
171 // image may be anything, it can have transparent parts too. |
159 |
172 |
160 #ifdef ENABLE_FAST_PAINT_ |
173 Qt::AspectRatioMode aspRatMode; |
161 painter->setCompositionMode( compositionMode ); // restore old composition mode |
174 switch (mImageMode) { |
162 #endif |
175 case Hb::ScaleBackgroundToFitWithoutExpanding: |
|
176 aspRatMode = Qt::KeepAspectRatio; |
|
177 break; |
|
178 case Hb::StretchBackgroundToFit: |
|
179 aspRatMode = Qt::IgnoreAspectRatio; |
|
180 break; |
|
181 default: |
|
182 aspRatMode = Qt::KeepAspectRatioByExpanding; |
|
183 break; |
|
184 } |
|
185 |
|
186 mBackground.paint(painter, mBoundingRect, aspRatMode, Qt::AlignCenter); |
163 } |
187 } |