9 * Initial Contributors: |
9 * Initial Contributors: |
10 * Nokia Corporation - initial contribution. |
10 * Nokia Corporation - initial contribution. |
11 * |
11 * |
12 * Contributors: |
12 * Contributors: |
13 * |
13 * |
14 * Description: |
14 * Description:Custom widget derived from HbTextEdit which provides rich text |
|
15 * processing |
15 * |
16 * |
16 */ |
17 */ |
17 |
18 |
18 #include "univieweraddresswidget.h" |
19 #include "univieweraddresswidget.h" |
19 #include "univiewerfeeder_p.h" |
20 |
20 // SYSTEM INCLUDES |
21 // SYSTEM INCLUDES |
21 #include <HbApplication> |
|
22 #include <QString> |
|
23 #include <QTextCursor> |
22 #include <QTextCursor> |
|
23 #include <QTextBlock> |
|
24 #include <QApplication> |
|
25 #include <QClipboard> |
|
26 |
24 #include <HbMenu> |
27 #include <HbMenu> |
25 |
28 #include <HbAction> |
26 // USER INCLUDES |
29 #include <HbTapGesture> |
27 #include "unitexteditor.h" |
30 |
|
31 #include <xqservicerequest.h> |
|
32 #include <xqappmgr.h> |
|
33 #include <cntservicescontact.h> |
|
34 #include <qtcontacts.h> |
|
35 #include <xqservicerequest.h> |
|
36 #include <xqaiwrequest.h> |
|
37 #include "msgcontacthandler.h" |
28 |
38 |
29 // LOCAL CONSTANTS |
39 // LOCAL CONSTANTS |
30 const QString ADDRESS_SEPARATOR("; "); |
40 const QString ADDRESS_SEPARATOR("; "); |
31 const QString ADDRESS_OPEN(" ("); |
41 const QString ADDRESS_OPEN(" ("); |
32 const QString ADDRESS_CLOSE(")"); |
42 const QString ADDRESS_CLOSE(")"); |
33 |
43 const QString SPACE(" "); |
34 //---------------------------------------------------------------------------- |
44 |
|
45 //localization |
|
46 #define LOC_OPEN_CONTACT_INFO hbTrId("txt_messaging_menu_open_contact_info") |
|
47 #define LOC_CALL hbTrId("txt_common_menu_call_verb") |
|
48 #define LOC_SEND_MESSAGE hbTrId("txt_common_menu_send_message") |
|
49 #define LOC_SAVE_TO_CONTACTS hbTrId("txt_common_menu_save_to_contacts") |
|
50 #define LOC_COPY hbTrId("txt_common_menu_copy") |
|
51 |
|
52 const QString BG_FRAME_GRAPHICS("qtg_fr_lineedit_normal"); |
|
53 |
|
54 //--------------------------------------------------------------- |
35 // UniViewerAddressWidget::UniViewerAddressWidget |
55 // UniViewerAddressWidget::UniViewerAddressWidget |
36 // @see header file |
56 // @see header file |
37 //---------------------------------------------------------------------------- |
57 //--------------------------------------------------------------- |
38 UniViewerAddressWidget::UniViewerAddressWidget(QGraphicsItem *parent) : |
58 UniViewerAddressWidget::UniViewerAddressWidget(QGraphicsItem * parent) : |
39 HbWidget(parent), mAddress(NULL) |
59 HbTextEdit(parent), |
40 { |
60 mCursorPos(-1) |
41 mAddress = new UniTextEditor(this); |
61 { |
42 HbStyle::setItemName(mAddress, "addressField"); |
62 this->setReadOnly(true); |
43 |
63 this->setScrollable(false); |
44 connect(mAddress, SIGNAL(aboutToShowContextMenu(HbMenu *,QPointF)), this, |
64 this->setFlag(QGraphicsItem::ItemIsFocusable,false); |
45 SLOT(handleAboutToShowContextMenu(HbMenu *,QPointF))); |
65 this->setCursorVisibility(Hb::TextCursorHidden); |
46 |
66 this->setBackgroundItem(0); |
47 // TODO: Wrapping fix breaking normal scenarios. |
67 |
48 // this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); |
68 HbFontSpec fontSpec(HbFontSpec::Secondary); |
49 } |
69 QFont font = fontSpec.font(); |
50 |
70 this->setFont(font); |
51 //---------------------------------------------------------------------------- |
71 |
|
72 mFormatNormal.setForeground(palette().link()); |
|
73 mFormatNormal.setBackground(Qt::transparent); |
|
74 |
|
75 mFormatHighlight.setBackground(palette().highlight()); |
|
76 mFormatHighlight.setForeground(palette().highlightedText()); |
|
77 |
|
78 connect(this, SIGNAL(aboutToShowContextMenu(HbMenu*,QPointF)), |
|
79 this, SLOT(aboutToShowContextMenu(HbMenu*,QPointF))); |
|
80 } |
|
81 |
|
82 //--------------------------------------------------------------- |
52 // UniViewerAddressWidget::~UniViewerAddressWidget |
83 // UniViewerAddressWidget::~UniViewerAddressWidget |
53 // @see header file |
84 // @see header file |
54 //---------------------------------------------------------------------------- |
85 //--------------------------------------------------------------- |
55 UniViewerAddressWidget::~UniViewerAddressWidget() |
86 UniViewerAddressWidget::~UniViewerAddressWidget() |
56 { |
87 { |
57 } |
88 } |
|
89 |
|
90 //--------------------------------------------------------------- |
|
91 //UniViewerAddressWidget :: gestureEvent |
|
92 // @see header file |
|
93 //--------------------------------------------------------------- |
|
94 void UniViewerAddressWidget::gestureEvent(QGestureEvent* event) |
|
95 { |
|
96 //handle gesture to highlight and dehighlight find item. |
|
97 |
|
98 if(HbTapGesture *tap = qobject_cast<HbTapGesture*>(event->gesture(Qt::TapGesture))) |
|
99 { |
|
100 //capturing gesture position, and map to local co-ordinates. |
|
101 QPointF pos = mapFromScene(tap->scenePosition()); |
|
102 |
|
103 switch (tap->state()) |
|
104 { |
|
105 case Qt::GestureStarted: |
|
106 { |
|
107 //highlight find item. |
|
108 QTextDocument* doc = this->document(); |
|
109 mCursorPos = doc->documentLayout()->hitTest(pos, Qt::ExactHit); |
|
110 highlightText(true); |
|
111 break; |
|
112 } |
|
113 |
|
114 case Qt::GestureFinished: |
|
115 { |
|
116 if (HbTapGesture::Tap == tap->tapStyleHint()) |
|
117 { |
|
118 //gesture is finshed dehighlight text. |
|
119 highlightText(false); |
|
120 |
|
121 QString anchor = this->anchorAt(pos); |
|
122 |
|
123 //do short tap action. |
|
124 if (!anchor.isEmpty() && !this->textCursor().hasSelection()) |
|
125 { |
|
126 shortTapAction(anchor); |
|
127 } |
|
128 } |
|
129 break; |
|
130 } |
|
131 |
|
132 case Qt::GestureCanceled: |
|
133 { |
|
134 //gesture is canceled due to pan or swipe, dehighlight text. |
|
135 if (HbTapGesture::Tap == tap->tapStyleHint()) |
|
136 { |
|
137 highlightText(false); |
|
138 break; |
|
139 } |
|
140 } |
|
141 default: |
|
142 break; |
|
143 } |
|
144 |
|
145 event->accept(); |
|
146 } |
|
147 else |
|
148 { |
|
149 event->ignore(); |
|
150 } |
|
151 |
|
152 //passing gesture event to base class. |
|
153 HbTextEdit::gestureEvent(event); |
|
154 } |
|
155 |
58 |
156 |
59 //---------------------------------------------------------------------------- |
157 //---------------------------------------------------------------------------- |
60 // UniViewerAddressWidget::populate |
158 // UniViewerAddressWidget::populate |
61 // @see header file |
159 // @see header file |
62 //---------------------------------------------------------------------------- |
160 //---------------------------------------------------------------------------- |
63 void UniViewerAddressWidget::populate(const QString &label, |
161 void UniViewerAddressWidget::populate(const QString &label, |
64 const QString &address, |
162 const QString &address, |
65 const QString &alias) |
163 const QString &alias) |
66 { |
164 { |
67 |
165 QString labelText = label; |
68 QTextCursor cursor(mAddress->document()); |
166 labelText.trimmed(); |
69 |
167 labelText += SPACE; |
70 QTextCharFormat addressFormat = cursor.charFormat(); |
168 |
71 addressFormat.setFontWeight(QFont::Bold); |
169 //Font. |
72 addressFormat.setForeground(QApplication::palette().link()); |
170 HbFontSpec fontSpec(HbFontSpec::Secondary); |
73 addressFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline); |
171 qreal fontHeight = 0.0; |
|
172 style()->parameter("hb-param-text-height-tiny", fontHeight); |
|
173 fontSpec.setTextHeight(fontHeight); |
|
174 QFont font = fontSpec.font(); |
|
175 |
|
176 QTextCharFormat labelFormat; |
|
177 labelFormat.setFont(font); |
|
178 |
|
179 QTextCharFormat addressFormat; |
|
180 addressFormat.setForeground(palette().link()); |
|
181 addressFormat.setFontUnderline(true); |
74 |
182 |
75 // Insert the label then the addresses |
183 // Insert the label then the addresses |
76 cursor.insertText(label); |
184 QTextCursor cursor(this->textCursor()); |
|
185 cursor.insertText(labelText,labelFormat); |
|
186 |
77 QString address1 = QString(); |
187 QString address1 = QString(); |
78 if (!(alias.isEmpty())) |
188 if (!(alias.isEmpty())) |
79 { |
189 { |
80 address1.append(alias); |
190 address1.append(alias); |
81 QString alias1 = QString(); |
191 QString alias1 = QString(); |
82 if (UniViewerFeederPrivate::GetNameFromContacts(address, alias1) > 1) |
192 |
|
193 int totalNumbers = 0; |
|
194 MsgContactHandler::resolveContactDisplayName( |
|
195 address, |
|
196 alias1, |
|
197 totalNumbers); |
|
198 if (totalNumbers > 1) |
83 { |
199 { |
84 address1.append(ADDRESS_OPEN); |
200 address1.append(ADDRESS_OPEN); |
85 address1.append(address); |
201 address1.append(address); |
86 address1.append(ADDRESS_CLOSE); |
202 address1.append(ADDRESS_CLOSE); |
87 } |
203 } |
90 { |
206 { |
91 address1.append(address); |
207 address1.append(address); |
92 } |
208 } |
93 addressFormat.setAnchorHref(address); |
209 addressFormat.setAnchorHref(address); |
94 cursor.insertText(address1, addressFormat); |
210 cursor.insertText(address1, addressFormat); |
95 repolish(); |
|
96 } |
211 } |
97 |
212 |
98 //---------------------------------------------------------------------------- |
213 //---------------------------------------------------------------------------- |
99 // UniViewerAddressWidget::populate |
214 // UniViewerAddressWidget::populate |
100 // @see header file |
215 // @see header file |
101 //---------------------------------------------------------------------------- |
216 //---------------------------------------------------------------------------- |
102 void UniViewerAddressWidget::populate(const QString &label, |
217 void UniViewerAddressWidget::populate(const QString &label, |
103 ConvergedMessageAddressList addressList) |
218 ConvergedMessageAddressList addressList) |
104 { |
219 { |
105 QTextCursor cursor(mAddress->document()); |
220 QString labelText = label; |
106 |
221 labelText.trimmed(); |
107 QTextCharFormat defaultFormat = cursor.charFormat(); |
222 labelText += SPACE; |
108 defaultFormat.setFontWeight(QFont::Bold); |
223 |
109 defaultFormat.setForeground(QApplication::palette().link()); |
224 //Font. |
110 |
225 HbFontSpec fontSpec(HbFontSpec::Secondary); |
111 QTextCharFormat addressFormat = cursor.charFormat(); |
226 qreal fontHeight = 0.0; |
112 addressFormat.setFontWeight(QFont::Bold); |
227 style()->parameter("hb-param-text-height-tiny", fontHeight); |
113 addressFormat.setForeground(QApplication::palette().link()); |
228 fontSpec.setTextHeight(fontHeight); |
114 addressFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline); |
229 QFont font = fontSpec.font(); |
|
230 |
|
231 QTextCharFormat labelFormat; |
|
232 labelFormat.setFont(font); |
|
233 |
|
234 QTextCharFormat defaultFormat; |
|
235 defaultFormat.setForeground(palette().link()); |
|
236 |
|
237 QTextCharFormat addressFormat; |
|
238 addressFormat.setForeground(palette().link()); |
|
239 addressFormat.setFontUnderline(true); |
115 |
240 |
116 // Insert the label then the addresses |
241 // Insert the label then the addresses |
117 cursor.insertText(label); |
242 QTextCursor cursor(this->document()); |
|
243 cursor.insertText(labelText,labelFormat); |
118 |
244 |
119 int addressCount = addressList.count(); |
245 int addressCount = addressList.count(); |
120 |
246 |
121 for (int i = 0; i < addressCount; ++i) |
247 for (int i = 0; i < addressCount; ++i) |
122 { |
248 { |
146 { |
277 { |
147 cursor.insertText(ADDRESS_SEPARATOR, defaultFormat); |
278 cursor.insertText(ADDRESS_SEPARATOR, defaultFormat); |
148 } |
279 } |
149 |
280 |
150 } |
281 } |
151 repolish(); |
|
152 } |
282 } |
153 |
283 |
154 //---------------------------------------------------------------------------- |
284 //---------------------------------------------------------------------------- |
155 // UniViewerAddressWidget::clearContent |
285 // UniViewerAddressWidget::clearContent |
156 // @see header file |
286 // @see header file |
157 //---------------------------------------------------------------------------- |
287 //---------------------------------------------------------------------------- |
158 void UniViewerAddressWidget::clearContent() |
288 void UniViewerAddressWidget::clearContent() |
159 { |
289 { |
160 mAddress->document()->clear(); |
290 this->document()->clear(); |
161 } |
291 } |
162 |
292 |
163 //---------------------------------------------------------------------------- |
293 //---------------------------------------------------------------------------- |
164 // UniViewerAddressWidget::handleAboutToShowContextMenu |
294 // UniViewerAddressWidget::menuClosed |
165 // @see header file |
295 // @see header file |
166 //---------------------------------------------------------------------------- |
296 //---------------------------------------------------------------------------- |
167 void UniViewerAddressWidget::handleAboutToShowContextMenu(HbMenu *contextMenu, const QPointF &pos) |
297 void UniViewerAddressWidget::menuClosed() |
168 { |
298 { |
169 Q_UNUSED(pos) |
299 highlightText(false); |
|
300 } |
|
301 |
|
302 //---------------------------------------------------------------------------- |
|
303 // UniViewerAddressWidget::highlightText |
|
304 // @see header file |
|
305 //---------------------------------------------------------------------------- |
|
306 void UniViewerAddressWidget::highlightText(bool highlight) |
|
307 { |
|
308 QTextBlock textBlock = this->document()->findBlock(mCursorPos); |
|
309 |
|
310 QTextBlock::iterator it; |
|
311 |
|
312 for (it = textBlock.begin(); !(it.atEnd()); ++it) |
|
313 { |
|
314 QTextFragment currentFragment = it.fragment(); |
|
315 |
|
316 if (currentFragment.isValid() && currentFragment.contains(mCursorPos) |
|
317 && currentFragment.charFormat().fontUnderline()) |
|
318 { |
|
319 int start = currentFragment.position(); |
|
320 int length = currentFragment.length(); |
|
321 |
|
322 QTextCursor cursor = this->textCursor(); |
|
323 cursor.clearSelection(); |
|
324 cursor.setPosition(start); |
|
325 cursor.setPosition(start + length,QTextCursor::KeepAnchor); |
|
326 |
|
327 if(highlight) |
|
328 { |
|
329 cursor.mergeCharFormat(mFormatHighlight); |
|
330 } |
|
331 else |
|
332 { |
|
333 cursor.mergeCharFormat(mFormatNormal); |
|
334 } |
|
335 |
|
336 cursor.clearSelection(); |
|
337 break; |
|
338 } |
|
339 } |
|
340 } |
|
341 |
|
342 void UniViewerAddressWidget::aboutToShowContextMenu(HbMenu *contextMenu, const QPointF &pos) |
|
343 { |
|
344 //remove default actions. |
170 contextMenu->clearActions(); |
345 contextMenu->clearActions(); |
|
346 |
|
347 // Check if there is an anchor at this pos |
|
348 QString anchor = this->anchorAt(pos); |
|
349 |
|
350 if(!anchor.isEmpty() && !this->textCursor().hasSelection()) |
|
351 { |
|
352 |
|
353 HbAction* action = NULL; |
|
354 |
|
355 action = contextMenu->addAction(LOC_OPEN_CONTACT_INFO, this, SLOT(openContactInfo())); |
|
356 action->setData(anchor); |
|
357 |
|
358 action = contextMenu->addAction(LOC_CALL, this, SLOT(call())); |
|
359 action->setData(anchor); |
|
360 |
|
361 action = contextMenu->addAction(LOC_SEND_MESSAGE, this, SLOT(sendMessage())); |
|
362 action->setData(anchor); |
|
363 |
|
364 action = contextMenu->addAction(LOC_SAVE_TO_CONTACTS, this, SLOT(saveToContacts())); |
|
365 action->setData(anchor); |
|
366 |
|
367 action = contextMenu->addAction(LOC_COPY, this, SLOT(copyToClipboard())); |
|
368 action->setData(anchor); |
|
369 |
|
370 } |
|
371 |
|
372 connect(contextMenu,SIGNAL(aboutToClose()),this,SLOT(menuClosed())); |
|
373 } |
|
374 |
|
375 void UniViewerAddressWidget::shortTapAction(QString anchor) |
|
376 { |
|
377 HbAction action; |
|
378 action.setData(anchor); |
|
379 connect(&action,SIGNAL(triggered()),this,SLOT(openContactInfo())); |
|
380 action.trigger(); |
|
381 } |
|
382 |
|
383 void UniViewerAddressWidget::copyToClipboard() |
|
384 { |
|
385 HbAction* action = qobject_cast<HbAction*>(sender()); |
|
386 |
|
387 if(action) |
|
388 { |
|
389 QMimeData* data = new QMimeData(); |
|
390 QString str = action->data().toString(); |
|
391 data->setText(str); |
|
392 QApplication::clipboard()->setMimeData(data); |
|
393 } |
|
394 } |
|
395 |
|
396 void UniViewerAddressWidget::call() |
|
397 { |
|
398 HbAction* action = qobject_cast<HbAction*>(sender()); |
|
399 |
|
400 if(action) |
|
401 { |
|
402 QString phoneNumber = action->data().toString(); |
|
403 |
|
404 //invoke dialer service and pass phoneNumber. |
|
405 QString serviceName("com.nokia.symbian.ICallDial"); |
|
406 QString operation("dial(QString)"); |
|
407 |
|
408 XQServiceRequest* serviceRequest = new XQServiceRequest(serviceName,operation,false); |
|
409 |
|
410 connect(serviceRequest, SIGNAL(requestCompleted(QVariant)), |
|
411 this, SLOT(onServiceRequestCompleted())); |
|
412 |
|
413 connect(serviceRequest, SIGNAL(requestError(int)), |
|
414 this, SLOT(onServiceRequestCompleted())); |
|
415 |
|
416 *serviceRequest << phoneNumber; |
|
417 serviceRequest->send(); |
|
418 } |
|
419 } |
|
420 |
|
421 void UniViewerAddressWidget::onServiceRequestCompleted() |
|
422 { |
|
423 //service request is now complete. delete it. |
|
424 XQServiceRequest* request = qobject_cast<XQServiceRequest*>(sender()); |
|
425 |
|
426 if(request) |
|
427 { |
|
428 delete request; |
|
429 } |
|
430 } |
|
431 |
|
432 |
|
433 void UniViewerAddressWidget::openContactInfo() |
|
434 { |
|
435 HbAction* action = qobject_cast<HbAction*>(sender()); |
|
436 |
|
437 if(action) |
|
438 { |
|
439 QList<QVariant> args; |
|
440 QString operation; |
|
441 |
|
442 QString data = action->data().toString(); |
|
443 |
|
444 int contactId = MsgContactHandler::resolveContactDisplayName( |
|
445 data, |
|
446 QContactPhoneNumber::DefinitionName, |
|
447 QContactPhoneNumber::FieldNumber); |
|
448 |
|
449 // if contact is unresolved on phone number field |
|
450 // then, try resolving it on email address field |
|
451 if(contactId <= 0) |
|
452 { |
|
453 contactId = MsgContactHandler::resolveContactDisplayName( |
|
454 data, |
|
455 QContactEmailAddress::DefinitionName, |
|
456 QContactEmailAddress::FieldEmailAddress); |
|
457 } |
|
458 |
|
459 if(contactId > 0) |
|
460 { |
|
461 //open contact card |
|
462 operation = QString("open(int)"); |
|
463 args << contactId; |
|
464 } |
|
465 else |
|
466 { |
|
467 //save to contacts with phone number field prefilled. |
|
468 |
|
469 operation = QString("editCreateNew(QString,QString)"); |
|
470 QString type = QContactPhoneNumber::DefinitionName; |
|
471 |
|
472 args << type; |
|
473 args << data; |
|
474 } |
|
475 |
|
476 //service stuff. |
|
477 QString serviceName("com.nokia.services.phonebookservices"); |
|
478 |
|
479 XQAiwRequest* request; |
|
480 XQApplicationManager appManager; |
|
481 request = appManager.create(serviceName, "Fetch", operation, true); // embedded |
|
482 if ( request == NULL ) |
|
483 { |
|
484 return; |
|
485 } |
|
486 |
|
487 // Result handlers |
|
488 connect (request, SIGNAL(requestOk(const QVariant&)), |
|
489 this, SLOT(handleOk(const QVariant&))); |
|
490 connect (request, SIGNAL(requestError(const QVariant&)), |
|
491 this, SLOT(handleError(const QVariant&))); |
|
492 |
|
493 request->setArguments(args); |
|
494 request->send(); |
|
495 delete request; |
|
496 } |
|
497 } |
|
498 |
|
499 void UniViewerAddressWidget::handleOk(const QVariant& result) |
|
500 { |
|
501 Q_UNUSED(result) |
|
502 } |
|
503 |
|
504 void UniViewerAddressWidget::handleError(int errorCode, const QString& errorMessage) |
|
505 { |
|
506 Q_UNUSED(errorMessage) |
|
507 Q_UNUSED(errorCode) |
|
508 } |
|
509 |
|
510 void UniViewerAddressWidget::saveToContacts() |
|
511 { |
|
512 //handler for save to contacts. |
|
513 } |
|
514 |
|
515 void UniViewerAddressWidget::sendMessage() |
|
516 { |
|
517 HbAction* action = qobject_cast<HbAction*>(sender()); |
|
518 |
|
519 if(action) |
|
520 { |
|
521 QString phoneNumber = action->data().toString(); |
|
522 QString alias; |
|
523 |
|
524 QTextBlock textBlock = this->document()->findBlock(mCursorPos); |
|
525 |
|
526 QTextBlock::iterator it; |
|
527 |
|
528 for (it = textBlock.begin(); !(it.atEnd()); ++it) |
|
529 { |
|
530 QTextFragment currentFragment = it.fragment(); |
|
531 |
|
532 if (currentFragment.isValid() && currentFragment.contains(mCursorPos) |
|
533 && currentFragment.charFormat().fontUnderline()) |
|
534 { |
|
535 QString txt = currentFragment.text(); |
|
536 if(txt != phoneNumber) |
|
537 { |
|
538 alias = txt; |
|
539 } |
|
540 break; |
|
541 } |
|
542 } |
|
543 |
|
544 |
|
545 //invoke editor & pass phoneNumber. |
|
546 emit sendMessage(phoneNumber,alias); |
|
547 } |
171 } |
548 } |
172 |
549 |
173 // EOF |
550 // EOF |