|
1 #include "mailtreeviewitem.h" |
|
2 |
|
3 #include <hbpushbutton.h> |
|
4 #include <hbabstractitemview.h> |
|
5 #include <hbtextitem.h> |
|
6 #include <hbstyle.h> |
|
7 #include <hblabel.h> |
|
8 #include <hbanchorlayout.h> |
|
9 #include <hbframedrawer.h> |
|
10 #include <hbframeitem.h> |
|
11 #include <hbicon.h> |
|
12 #include <hbstyle_p.h> |
|
13 |
|
14 #include <QPainter> |
|
15 #include <QDebug> |
|
16 |
|
17 /*! |
|
18 \class NmMessageListViewItem |
|
19 \brief list view item for message list view |
|
20 */ |
|
21 |
|
22 /*! |
|
23 Constructor |
|
24 */ |
|
25 MailTreeViewItem::MailTreeViewItem(QGraphicsItem *parent): |
|
26 HbTreeViewItem(parent), |
|
27 mSender(0), |
|
28 mSubject(0), |
|
29 mTime(0), |
|
30 mDividerTitle(0), |
|
31 mNewMsgIcon(0), |
|
32 mFrom(0) |
|
33 { |
|
34 } |
|
35 |
|
36 MailTreeViewItem::~MailTreeViewItem() |
|
37 { |
|
38 } |
|
39 |
|
40 |
|
41 int MailTreeViewItem::type() const |
|
42 { |
|
43 return MailTreeViewItem::Type; |
|
44 } |
|
45 |
|
46 HbAbstractViewItem *MailTreeViewItem::createItem() |
|
47 { |
|
48 return new MailTreeViewItem(*this); |
|
49 } |
|
50 |
|
51 /*! |
|
52 boolean value indicating model index availability |
|
53 */ |
|
54 bool MailTreeViewItem::canSetModelIndex(const QModelIndex &index) const |
|
55 { |
|
56 Q_UNUSED(index); |
|
57 // This item class can handle all items in message list |
|
58 return true; |
|
59 } |
|
60 |
|
61 /*! |
|
62 update child items |
|
63 */ |
|
64 void MailTreeViewItem::updateChildItems() |
|
65 { |
|
66 // to create expand icon, if it doesn't exist |
|
67 HbTreeViewItem::updateChildItems(); |
|
68 |
|
69 // Shared data with ModelFactory. |
|
70 int messageRole = Qt::UserRole+1; |
|
71 int dateRole = Qt::UserRole+2; |
|
72 |
|
73 mLayout = 0; |
|
74 setLayout(0); |
|
75 |
|
76 // Create layout |
|
77 mLayout = new HbAnchorLayout(); |
|
78 setLayout(mLayout); // mLayout ownership is passed to QGraphicsWidget |
|
79 |
|
80 QVariant dateData = modelIndex().data(dateRole); |
|
81 QVariant messageData = modelIndex().data(messageRole); |
|
82 |
|
83 // Create fonts |
|
84 HbFontSpec primaryFont = HbFontSpec(HbFontSpec::Primary); |
|
85 HbFontSpec secondaryFont = HbFontSpec(HbFontSpec::Secondary); |
|
86 |
|
87 // Check whether item is message item or title divider |
|
88 // and set the layout accordingly |
|
89 if (messageData.isValid()) { |
|
90 QStringList stringList; |
|
91 if ( messageData.canConvert<QStringList>()) { |
|
92 stringList = messageData.toStringList(); |
|
93 } |
|
94 |
|
95 if (stringList.count() < 3) { |
|
96 // to avoid crash |
|
97 return; |
|
98 } |
|
99 |
|
100 delete mDividerTitle; |
|
101 mDividerTitle = 0; |
|
102 |
|
103 if (!mTime) { |
|
104 mTime = new HbLabel(); |
|
105 } |
|
106 mTime->setObjectName("ListViewItemMessageTime"); |
|
107 mTime->setFontSpec(HbFontSpec(HbFontSpec::Primary)); |
|
108 // Create subject label |
|
109 if (!mSubject) { |
|
110 mSubject = new HbLabel(); |
|
111 } |
|
112 mSubject->setObjectName("ListViewItemMessageSubject"); |
|
113 mSubject->setFontSpec(HbFontSpec(HbFontSpec::Secondary)); |
|
114 |
|
115 if (!mFrom) { |
|
116 mFrom = new HbLabel(stringList.at(0)); |
|
117 } |
|
118 mFrom->setObjectName("ListViewItemMessageSender"); |
|
119 mFrom->setFontSpec(primaryFont); |
|
120 |
|
121 // Create default locale |
|
122 /*QLocale locale; |
|
123 mTime->setText(locale.toString( |
|
124 msgModelItem->metaData().sentTime().toLocalTime().time(), |
|
125 QLocale::ShortFormat));*/ |
|
126 mTime->setPlainText(stringList.at(1)); |
|
127 |
|
128 // mTime->setAlignment(Qt::AlignVCenter | Qt::AlignRight); |
|
129 mSubject->setPlainText(stringList.at(2)); |
|
130 |
|
131 // Create new message icon |
|
132 if (!mNewMsgIcon) { |
|
133 HbFrameDrawer *drawer = new HbFrameDrawer(":/resources/qtg_fr_list_new_item", HbFrameDrawer::ThreePiecesVertical); |
|
134 drawer->setFillWholeRect(false); |
|
135 mNewMsgIcon = new HbFrameItem(drawer, this); |
|
136 } |
|
137 mNewMsgIcon->setObjectName("ListViewItemMessageIcon"); |
|
138 |
|
139 static const int iconWidth = 8; |
|
140 |
|
141 // Set message item layout |
|
142 // Place new message icon to layout |
|
143 mLayout->setAnchor(mLayout, Hb::TopEdge, mNewMsgIcon, Hb::TopEdge, 0); |
|
144 mLayout->setAnchor(mLayout, Hb::LeftEdge, mNewMsgIcon, Hb::LeftEdge, 0); |
|
145 mLayout->setAnchor(mNewMsgIcon, Hb::RightEdge, mFrom, Hb::LeftEdge, 0); |
|
146 |
|
147 // Set from & subject 10 pixels from left |
|
148 mLayout->setAnchor(mLayout, Hb::TopEdge, mFrom, Hb::TopEdge, 0); |
|
149 mLayout->setAnchor(mLayout, Hb::LeftEdge, mFrom, Hb::LeftEdge, iconWidth); |
|
150 mLayout->setAnchor(mLayout, Hb::RightEdge, mSubject, Hb::RightEdge, iconWidth); |
|
151 |
|
152 mLayout->setAnchor(mLayout, Hb::LeftEdge, mSubject, Hb::LeftEdge, iconWidth); |
|
153 mLayout->setAnchor(mFrom, Hb::BottomEdge, mSubject, Hb::TopEdge, 0); |
|
154 // Set Time label to correct place |
|
155 mLayout->setAnchor(mTime, Hb::LeftEdge, mLayout, Hb::RightEdge, 100); |
|
156 mLayout->setAnchor(mFrom, Hb::RightEdge, mTime, Hb::LeftEdge, 0); |
|
157 // Set subject right edge alignment |
|
158 mLayout->setAnchor(mSubject, Hb::RightEdge, mLayout, Hb::RightEdge, iconWidth); |
|
159 |
|
160 mLayout->setPreferredHeight(50); |
|
161 setObjectName("ListViewItemMessage"); |
|
162 } |
|
163 else if (dateData.isValid()) { |
|
164 QString date; |
|
165 if (dateData.canConvert<QString>()) { |
|
166 date = dateData.toString(); |
|
167 } |
|
168 else { |
|
169 return; |
|
170 } |
|
171 |
|
172 // NOTE: Layout data will be read from xml once orbit supports it |
|
173 // Create divider icon |
|
174 delete mTime; |
|
175 mTime = 0; |
|
176 |
|
177 delete mSubject; |
|
178 mSubject = 0; |
|
179 |
|
180 delete mNewMsgIcon; |
|
181 mNewMsgIcon = 0; |
|
182 |
|
183 delete mFrom; |
|
184 mFrom = 0; |
|
185 |
|
186 // Create divider title |
|
187 if (!mDividerTitle) { |
|
188 mDividerTitle = new HbLabel(); |
|
189 } |
|
190 mDividerTitle->setObjectName("ListViewItemDividerTitle"); |
|
191 |
|
192 mDividerTitle->setPlainText(date); |
|
193 mDividerTitle->setFontSpec(primaryFont); |
|
194 mDividerTitle->setAlignment(Qt::AlignVCenter | Qt::AlignLeft); |
|
195 |
|
196 // Add title divider text |
|
197 mLayout->setAnchor(mLayout, Hb::TopEdge, mDividerTitle, Hb::TopEdge, 1); |
|
198 mLayout->setAnchor(mLayout, Hb::LeftEdge, mDividerTitle, Hb::LeftEdge, 0); |
|
199 |
|
200 QGraphicsItem *graphicsItem = primitive(QLatin1String("subitem-indicator")); |
|
201 if ( graphicsItem |
|
202 && graphicsItem->isWidget()) { |
|
203 HbLabel *dividerIcon = qobject_cast<HbLabel*>(static_cast<QGraphicsWidget*>(graphicsItem)); |
|
204 if (dividerIcon) { |
|
205 mLayout->setAnchor(mLayout, Hb::TopEdge, dividerIcon, Hb::TopEdge, 5); |
|
206 mLayout->setAnchor(dividerIcon, Hb::RightEdge, mLayout, Hb::RightEdge, 20); |
|
207 } |
|
208 } |
|
209 |
|
210 mLayout->setPreferredHeight(32); |
|
211 setObjectName("ListViewItemDivider"); |
|
212 } |
|
213 else { |
|
214 qDebug() <<"MailTreeViewItem: Invalid message meta data when drawing message list"; |
|
215 } |
|
216 |
|
217 const QSizeF reso = HbDeviceProfile::current().logicalSize(); |
|
218 mLayout->setPreferredWidth(reso.width()-20); |
|
219 } |
|
220 |
|
221 void MailTreeViewItem::polishEvent() |
|
222 { |
|
223 QGraphicsWidget::polishEvent(); |
|
224 } |
|
225 |
|
226 |
|
227 HbWidgetBase *MailTreeViewItem::updateExpandItem() |
|
228 { |
|
229 HbLabel *dividerIcon = 0; |
|
230 QGraphicsItem *graphicsItem = primitive(QLatin1String("subitem-indicator")); |
|
231 if ( graphicsItem |
|
232 && graphicsItem->isWidget()) { |
|
233 dividerIcon = qobject_cast<HbLabel*>(static_cast<QGraphicsWidget*>(graphicsItem)); |
|
234 } |
|
235 |
|
236 if (!dividerIcon) { |
|
237 dividerIcon = new HbLabel(); |
|
238 HbIcon icon; |
|
239 icon.setIconName(":/resources/qtg_nmailui_minus_sign", QIcon::Normal, QIcon::On); |
|
240 icon.setIconName(":/resources/qtg_nmailui_plus_sign", QIcon::Normal, QIcon::Off); |
|
241 dividerIcon->setIcon(icon); |
|
242 } |
|
243 |
|
244 if (isExpanded()) { |
|
245 dividerIcon->setObjectName("ListViewItemDividerIconMinus"); |
|
246 } |
|
247 else { |
|
248 dividerIcon->setObjectName("ListViewItemDividerIconPlus"); |
|
249 } |
|
250 return dividerIcon; |
|
251 } |
|
252 |
|
253 void MailTreeViewItem::paint( |
|
254 QPainter *painter, |
|
255 const QStyleOptionGraphicsItem *option, |
|
256 QWidget *widget) |
|
257 { |
|
258 Q_UNUSED(option); |
|
259 Q_UNUSED(widget); |
|
260 if (painter){ |
|
261 painter->save(); |
|
262 painter->setOpacity(0.15); |
|
263 QLineF line1( rect().topLeft().x(), rect().bottomRight().y(), |
|
264 rect().bottomRight().x(), rect().bottomRight().y()); |
|
265 painter->drawLine(line1); |
|
266 // Draw line before each item |
|
267 QLineF line2( rect().topLeft().x(), rect().topLeft().y(), |
|
268 rect().bottomRight().x(), rect().topLeft().y()); |
|
269 painter->drawLine(line2); |
|
270 painter->restore(); |
|
271 } |
|
272 } |
|
273 |
|
274 |