37 #include "hbabstractedit_p.h" |
37 #include "hbabstractedit_p.h" |
38 #include "hbabstractedit.h" |
38 #include "hbabstractedit.h" |
39 #include "hbstyleoption_p.h" |
39 #include "hbstyleoption_p.h" |
40 #include "hbscrollarea.h" |
40 #include "hbscrollarea.h" |
41 #include "hbvalidator.h" |
41 #include "hbvalidator.h" |
42 #include "hbmeshlayout_p.h" |
|
43 #include "hbmenu.h" |
42 #include "hbmenu.h" |
44 #include "hbselectioncontrol_p.h" |
43 #include "hbselectioncontrol_p.h" |
45 #include "hbcolorscheme.h" |
44 #include "hbcolorscheme.h" |
46 #include "hbsmileyengine_p.h" |
45 #include "hbsmileyengine_p.h" |
47 #include "hbtextmeasurementutility_p.h" |
46 #include "hbtextmeasurementutility_p.h" |
48 #include "hbfeaturemanager_p.h" |
47 #include "hbfeaturemanager_r.h" |
49 #include "hbinputeditorinterface.h" |
48 #include "hbinputeditorinterface.h" |
50 #include "hbinputvkbhost.h" |
49 #include "hbinputvkbhost.h" |
|
50 #include "hbinputmethod.h" |
|
51 #include "hbinputfocusobject.h" |
|
52 |
51 |
53 |
52 #include <QValidator> |
54 #include <QValidator> |
53 #include <QTextLayout> |
55 #include <QTextLayout> |
54 #include <QTextBlock> |
56 #include <QTextBlock> |
55 #include <QTextList> |
57 #include <QTextList> |
93 { |
95 { |
94 public: |
96 public: |
95 |
97 |
96 HbEditItem(HbAbstractEdit *parent) : HbWidget(parent), edit(parent) |
98 HbEditItem(HbAbstractEdit *parent) : HbWidget(parent), edit(parent) |
97 { |
99 { |
|
100 setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true); |
98 }; |
101 }; |
99 |
102 |
100 virtual ~HbEditItem() {}; |
103 virtual ~HbEditItem() {}; |
101 |
104 |
102 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) |
105 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) |
209 void HbAbstractEditPrivate::init() |
213 void HbAbstractEditPrivate::init() |
210 { |
214 { |
211 Q_Q(HbAbstractEdit); |
215 Q_Q(HbAbstractEdit); |
212 |
216 |
213 canvas = new HbEditItem(q); |
217 canvas = new HbEditItem(q); |
|
218 canvas->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored); |
|
219 |
214 |
220 |
215 setContent(Qt::RichText, QString()); |
221 setContent(Qt::RichText, QString()); |
216 |
222 |
217 QTextOption textOption = doc->defaultTextOption(); |
223 QTextOption textOption = doc->defaultTextOption(); |
218 textOption.setTextDirection(q->layoutDirection()); |
224 textOption.setTextDirection(q->layoutDirection()); |
236 // These are the default values which are then overridden in subclasses |
242 // These are the default values which are then overridden in subclasses |
237 // and when different options are enabled. |
243 // and when different options are enabled. |
238 q->setFlag(QGraphicsItem::ItemIsFocusable); |
244 q->setFlag(QGraphicsItem::ItemIsFocusable); |
239 q->setFlag(QGraphicsItem::ItemAcceptsInputMethod); |
245 q->setFlag(QGraphicsItem::ItemAcceptsInputMethod); |
240 q->setFlag(QGraphicsItem::ItemSendsScenePositionChanges); |
246 q->setFlag(QGraphicsItem::ItemSendsScenePositionChanges); |
|
247 q->setFlag(QGraphicsItem::ItemHasNoContents, false); |
241 q->setFocusPolicy(Qt::StrongFocus); |
248 q->setFocusPolicy(Qt::StrongFocus); |
242 q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
249 q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
243 |
250 |
244 contextMenuShownOn = Hb::ShowTextContextMenuOnSelectionClicked | Hb::ShowTextContextMenuOnLongPress; |
251 contextMenuShownOn = Hb::ShowTextContextMenuOnSelectionClicked | Hb::ShowTextContextMenuOnLongPress; |
245 |
252 |
246 q->grabGesture(Qt::TapGesture); |
253 q->grabGesture(Qt::TapGesture); |
247 } |
254 } |
248 |
255 |
249 void HbAbstractEditPrivate::updatePaletteFromTheme() |
256 void HbAbstractEditPrivate::updatePaletteFromTheme() |
250 { |
257 { |
251 Q_Q(HbAbstractEdit); |
|
252 |
|
253 // TODO: remove once these color dissapear from hbcolorgroup.css |
|
254 QColor textColor = HbColorScheme::color("qtc_editor_normal"); |
|
255 QColor selectedColor = HbColorScheme::color("qtc_editor_selected"); |
|
256 QColor selectedBackground = HbColorScheme::color("qtc_editor_marker_normal"); |
|
257 QPalette pal = q->palette(); |
|
258 |
|
259 if (textColor.isValid()) { |
|
260 pal.setColor(QPalette::Text, textColor); |
|
261 } |
|
262 |
|
263 if (selectedColor.isValid()) { |
|
264 pal.setColor(QPalette::HighlightedText, selectedColor); |
|
265 } |
|
266 |
|
267 if (selectedBackground.isValid()) { |
|
268 pal.setColor(QPalette::Highlight, selectedBackground); |
|
269 } |
|
270 q->setPalette(pal); |
|
271 |
|
272 |
|
273 // The link color is used from application's palette |
258 // The link color is used from application's palette |
274 QColor linkColor = HbColorScheme::color("qtc_view_link_normal"); |
259 QColor linkColor = HbColorScheme::color("qtc_view_link_normal"); |
275 QColor linkVisitedColor = HbColorScheme::color("qtc_view_visited_normal"); |
260 QColor linkVisitedColor = HbColorScheme::color("qtc_view_visited_normal"); |
276 QPalette appPal = qApp->palette(); |
261 QPalette appPal = qApp->palette(); |
277 if (linkColor.isValid()) { |
262 if (linkColor.isValid()) { |
388 return false; |
373 return false; |
389 } |
374 } |
390 |
375 |
391 void HbAbstractEditPrivate::setCursorPosition(int pos, QTextCursor::MoveMode mode) |
376 void HbAbstractEditPrivate::setCursorPosition(int pos, QTextCursor::MoveMode mode) |
392 { |
377 { |
|
378 if (cursor.isNull()) { |
|
379 cursor = QTextCursor(doc); |
|
380 } |
|
381 |
393 cursor.setPosition(pos, mode); |
382 cursor.setPosition(pos, mode); |
394 |
383 ensureCursorVisible(); |
395 cursorChanged(HbValidator::CursorChangeFromMouse); |
384 cursorChanged(HbValidator::CursorChangeFromMouse); |
396 } |
385 } |
397 |
386 |
398 bool HbAbstractEditPrivate::cursorMoveKeyEvent(QKeyEvent *e) |
387 bool HbAbstractEditPrivate::cursorMoveKeyEvent(QKeyEvent *e) |
399 { |
388 { |
508 #endif // QT_NO_SHORTCUT |
497 #endif // QT_NO_SHORTCUT |
509 else { |
498 else { |
510 return false; |
499 return false; |
511 } |
500 } |
512 |
501 |
513 const QTextCursor oldCursor = cursor; |
502 //const QTextCursor oldCursor = cursor; |
514 bool visualNavigation = cursor.visualNavigation(); |
503 bool visualNavigation = cursor.visualNavigation(); |
515 cursor.setVisualNavigation(true); |
504 cursor.setVisualNavigation(true); |
516 cursor.movePosition(op, mode); |
505 cursor.movePosition(op, mode); |
517 cursor.setVisualNavigation(visualNavigation); |
506 cursor.setVisualNavigation(visualNavigation); |
518 cursorChanged(HbValidator::CursorChangeFromOperation); |
507 cursorChanged(HbValidator::CursorChangeFromOperation); |
519 repaintOldAndNewSelection(oldCursor); |
|
520 |
508 |
521 return true; |
509 return true; |
522 } |
510 } |
523 |
511 |
524 void HbAbstractEditPrivate::repaintOldAndNewSelection(const QTextCursor &oldSelection) |
512 void HbAbstractEditPrivate::repaintOldAndNewSelection(const QTextCursor &oldSelection) |
594 } |
582 } |
595 } |
583 } |
596 |
584 |
597 void HbAbstractEditPrivate::setTextInteractionFlags(Qt::TextInteractionFlags flags) |
585 void HbAbstractEditPrivate::setTextInteractionFlags(Qt::TextInteractionFlags flags) |
598 { |
586 { |
599 Q_Q(HbAbstractEdit); |
|
600 |
|
601 if (flags == interactionFlags) |
587 if (flags == interactionFlags) |
602 return; |
588 return; |
603 interactionFlags = flags; |
589 interactionFlags = flags; |
604 |
590 |
605 if (q->hasFocus()) { |
591 if (hasInputFocus()) { |
606 setBlinkingCursorEnabled(flags & Qt::TextEditable); |
592 setBlinkingCursorEnabled(flags & Qt::TextEditable); |
607 } |
593 } |
608 } |
594 } |
609 |
595 |
610 void HbAbstractEditPrivate::_q_updateRequest(QRectF rect) |
596 void HbAbstractEditPrivate::_q_updateRequest(QRectF rect) |
647 void HbAbstractEditPrivate::_q_selectionChanged() |
633 void HbAbstractEditPrivate::_q_selectionChanged() |
648 { |
634 { |
649 Q_Q(HbAbstractEdit); |
635 Q_Q(HbAbstractEdit); |
650 |
636 |
651 if (cursor.hasSelection()) { |
637 if (cursor.hasSelection()) { |
652 if (selectionControl) { |
638 selectionControl = HbSelectionControl::attachEditor(q); |
653 selectionControl->showHandles(); |
639 selectionControl->showHandles(); |
654 q->update(); |
|
655 } |
|
656 } else if (selectionControl){ |
640 } else if (selectionControl){ |
657 selectionControl->hideHandles(); |
641 selectionControl->hideHandles(); |
658 q->update(); |
642 } |
659 } |
643 |
|
644 QTextCursor oldSelection(selectionCursor); |
|
645 selectionCursor = cursor; |
|
646 repaintOldAndNewSelection(oldSelection); |
660 } |
647 } |
661 |
648 |
662 void HbAbstractEditPrivate::_q_scrollStarted() |
649 void HbAbstractEditPrivate::_q_scrollStarted() |
663 { |
650 { |
664 if (selectionControl) { |
651 if (selectionControl) { |
790 } |
776 } |
791 |
777 |
792 if (cursor.hasSelection()) { |
778 if (cursor.hasSelection()) { |
793 QAbstractTextDocumentLayout::Selection selection; |
779 QAbstractTextDocumentLayout::Selection selection; |
794 selection.cursor = cursor; |
780 selection.cursor = cursor; |
795 QPalette::ColorGroup cg = q->hasFocus() ? QPalette::Active : QPalette::Inactive; |
781 QPalette::ColorGroup cg = hasInputFocus() ? QPalette::Active : QPalette::Inactive; |
796 selection.format.setBackground(ctx.palette.brush(cg, QPalette::Highlight)); |
782 selection.format.setBackground(ctx.palette.brush(cg, QPalette::Highlight)); |
797 selection.format.setForeground(ctx.palette.brush(cg, QPalette::HighlightedText)); |
783 selection.format.setForeground(ctx.palette.brush(cg, QPalette::HighlightedText)); |
798 |
784 |
799 HbStyleOption opt; |
785 HbStyleOption opt; |
800 q->initStyleOption(&opt); |
786 q->initStyleOption(&opt); |
993 Q_Q(const HbAbstractEdit); |
979 Q_Q(const HbAbstractEdit); |
994 |
980 |
995 QTextLayout *layout = cursor.block().layout(); |
981 QTextLayout *layout = cursor.block().layout(); |
996 int cursorPos = hitTest(tapPos, Qt::ExactHit); |
982 int cursorPos = hitTest(tapPos, Qt::ExactHit); |
997 |
983 |
998 if (cursorPos == -1) { |
984 if (layout && cursorPos == -1) { |
999 cursorPos = cursor.position() + layout->preeditAreaText().length(); |
985 cursorPos = cursor.position() + layout->preeditAreaText().length(); |
1000 } |
986 } |
1001 |
987 |
1002 if (layout && !layout->preeditAreaText().isEmpty()) { |
988 if (layout && !layout->preeditAreaText().isEmpty()) { |
1003 QInputContext *ctx = qApp->inputContext(); |
989 QInputContext *ctx = qApp->inputContext(); |
1076 bool removeSelection = (hitTest(point, Qt::ExactHit) == -1); |
1062 bool removeSelection = (hitTest(point, Qt::ExactHit) == -1); |
1077 |
1063 |
1078 if (removeSelection && cursor.hasSelection()) { |
1064 if (removeSelection && cursor.hasSelection()) { |
1079 const QTextCursor oldCursor = cursor; |
1065 const QTextCursor oldCursor = cursor; |
1080 cursor.clearSelection(); |
1066 cursor.clearSelection(); |
1081 repaintOldAndNewSelection(oldCursor); |
|
1082 emit q->selectionChanged(oldCursor, cursor); |
1067 emit q->selectionChanged(oldCursor, cursor); |
1083 } |
1068 } |
1084 |
1069 |
1085 int newCursorPos = hitTest(point, Qt::FuzzyHit); |
1070 int newCursorPos = hitTest(point, Qt::FuzzyHit); |
1086 |
1071 |
1092 q->showContextMenu(q->mapToScene(point)); |
1077 q->showContextMenu(q->mapToScene(point)); |
1093 } |
1078 } |
1094 } else { |
1079 } else { |
1095 // Currently focused widget to listen to InputContext before updating the cursor position |
1080 // Currently focused widget to listen to InputContext before updating the cursor position |
1096 sendMouseEventToInputContext(point); |
1081 sendMouseEventToInputContext(point); |
|
1082 |
|
1083 // translate the point to have the Y ccordinate inside the viewport |
|
1084 QPointF translatedPoint(point); |
|
1085 if(translatedPoint.y() < viewPortRect().top()) { |
|
1086 translatedPoint.setY(viewPortRect().top() + 1); |
|
1087 } else if(translatedPoint.y() > viewPortRect().bottom()){ |
|
1088 translatedPoint.setY(viewPortRect().bottom() - 1); |
|
1089 } |
|
1090 |
1097 // need to get the cursor position again since input context can change the document |
1091 // need to get the cursor position again since input context can change the document |
1098 newCursorPos = hitTest(point, Qt::FuzzyHit); |
1092 newCursorPos = hitTest(translatedPoint, Qt::FuzzyHit); |
1099 setCursorPosition(newCursorPos); |
1093 setCursorPosition(newCursorPos); |
1100 |
1094 |
1101 if (interactionFlags & Qt::TextEditable) { |
1095 if (interactionFlags & Qt::TextEditable) { |
1102 updateCurrentCharFormat(); |
1096 updateCurrentCharFormat(); |
1103 } |
1097 } |
1104 cursorChanged(HbValidator::CursorChangeFromMouse); |
1098 |
|
1099 QString anchor(q->anchorAt(translatedPoint)); |
|
1100 if(!anchor.isEmpty()) { |
|
1101 emit q->anchorTapped(anchor); |
|
1102 } |
1105 } |
1103 } |
1106 } |
1104 } |
1107 |
1105 |
1108 |
1106 |
1109 |
1107 |
1169 placeholderDoc->setDefaultTextOption(doc->defaultTextOption()); |
1167 placeholderDoc->setDefaultTextOption(doc->defaultTextOption()); |
1170 placeholderDoc->setDefaultFont(doc->defaultFont()); |
1168 placeholderDoc->setDefaultFont(doc->defaultFont()); |
1171 placeholderDoc->setTextWidth(doc->textWidth()); |
1169 placeholderDoc->setTextWidth(doc->textWidth()); |
1172 } |
1170 } |
1173 } |
1171 } |
|
1172 |
|
1173 void HbAbstractEditPrivate::filterInputText(QString &text) const |
|
1174 { |
|
1175 Q_Q(const HbAbstractEdit); |
|
1176 HbEditorInterface editorInterface(const_cast<HbAbstractEdit*>(q)); |
|
1177 HbInputFilter *inputFilter = editorInterface.filter(); |
|
1178 |
|
1179 if (!text.isEmpty() && inputFilter) { |
|
1180 for(int i(text.length() - 1); i>=0; --i) { |
|
1181 if (!inputFilter->filter(text.at(i))) { |
|
1182 text.remove(i,1); |
|
1183 } |
|
1184 } |
|
1185 } |
|
1186 } |
|
1187 |
1174 |
1188 |
1175 Qt::Alignment HbAbstractEditPrivate::alignmentFromString(const QString &text) |
1189 Qt::Alignment HbAbstractEditPrivate::alignmentFromString(const QString &text) |
1176 { |
1190 { |
1177 Qt::Alignment align(0); |
1191 Qt::Alignment align(0); |
1178 static const struct { |
1192 static const struct { |
1227 void HbAbstractEditPrivate::closeInputPanel() |
1241 void HbAbstractEditPrivate::closeInputPanel() |
1228 { |
1242 { |
1229 sendInputPanelEvent(QEvent::CloseSoftwareInputPanel); |
1243 sendInputPanelEvent(QEvent::CloseSoftwareInputPanel); |
1230 } |
1244 } |
1231 |
1245 |
1232 #include "hbinputeditorinterface.h" |
1246 bool HbAbstractEditPrivate::hasInputFocus() const |
1233 #include "hbinputvkbhost.h" |
1247 { |
1234 |
1248 Q_Q(const HbAbstractEdit); |
1235 void HbAbstractEditPrivate::minimizeInputPanel() |
1249 |
1236 { |
1250 HbInputMethod* inputMethod = HbInputMethod::activeInputMethod(); |
1237 Q_Q(HbAbstractEdit); |
1251 if (inputMethod && inputMethod->focusObject() && |
1238 |
1252 qobject_cast<HbAbstractEdit*>(inputMethod->focusObject()->object()) == q) { |
1239 HbEditorInterface ei(q); |
1253 return true; |
1240 HbVkbHost* vkbHost = ei.vkbHost(); |
1254 } |
1241 vkbHost->minimizeKeypad(); |
1255 return false; |
|
1256 } |
|
1257 |
|
1258 void HbAbstractEditPrivate::setInputFocusEnabled(bool enable) |
|
1259 { |
|
1260 QGraphicsItem *focusItem = focusPrimitive(HbWidget::FocusHighlightActive); |
|
1261 if (focusItem) { |
|
1262 focusItem->setVisible(enable); |
|
1263 } |
|
1264 |
|
1265 setBlinkingCursorEnabled(enable); |
|
1266 repaintOldAndNewSelection(selectionCursor); |
1242 } |
1267 } |
1243 |
1268 |
1244 #include "moc_hbabstractedit.cpp" |
1269 #include "moc_hbabstractedit.cpp" |