PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/textedit.cpp

https://github.com/proton/zNotes
C++ | 148 lines | 127 code | 11 blank | 10 comment | 18 complexity | 5e4a8df12aa3728f46eef0f3918a8cb8 MD5 | raw file
Possible License(s): GPL-3.0
  1. #include "textedit.h"
  2. #include "settings.h"
  3. #include <QRegExp>
  4. #include <QMouseEvent>
  5. #include <QUrl>
  6. #include <QDesktopServices>
  7. //Checking note's text for link
  8. inline bool isOnLink(const QTextDocument& document, const QTextCursor& cursor, int& pos_start, int& pos_end)
  9. {
  10. const int position = cursor.position();
  11. if(!document.characterAt(position).isSpace())
  12. {
  13. pos_start = position;
  14. while(pos_start>0 && !document.characterAt(pos_start-1).isSpace()) --pos_start; //Detecting start on the word
  15. const int characterCount = document.characterCount();
  16. pos_end = position;
  17. while(pos_end<characterCount && !document.characterAt(pos_end).isSpace()) ++pos_end; //Detecting end on the word
  18. //
  19. QStringList signlist;
  20. signlist << "http://" << "https://" << "ftp://";
  21. const int link_lenght = pos_end-pos_start;
  22. QStringList::const_iterator sign_it;
  23. for(sign_it=signlist.constBegin(); sign_it!=signlist.constEnd(); ++sign_it)
  24. {
  25. if(link_lenght>sign_it->size())
  26. {
  27. bool isLink = true;
  28. int i = 0;
  29. while(i<sign_it->size())
  30. {
  31. if(document.characterAt(pos_start+i)!=sign_it->at(i))
  32. {
  33. isLink = false;
  34. break;
  35. }
  36. ++i;
  37. }
  38. if(isLink) return true;
  39. }
  40. }
  41. }
  42. return false;
  43. }
  44. //------------------------------------------------------------------------------
  45. TextEdit::TextEdit()
  46. : QTextEdit()
  47. {
  48. highlighter = new Highlighter(document());
  49. connect(&settings, SIGNAL(NoteHighlightChanged()), highlighter, SLOT(rehighlight()));
  50. setFont(settings.getNoteFont());
  51. connect(&settings, SIGNAL(NoteFontChanged()), this, SLOT(fontChanged()));
  52. setMouseTracking(settings.getNoteLinksOpen());
  53. connect(&settings, SIGNAL(NoteLinkOpenChanged()), this, SLOT(linkOpenChanged()));
  54. this->setWordWrapMode(QTextOption::WordWrap);
  55. }
  56. //Mouse pressing
  57. void TextEdit::mousePressEvent(QMouseEvent *e)
  58. {
  59. if(settings.getNoteLinksOpen() && e->buttons()==Qt::LeftButton && e->modifiers()&Qt::ControlModifier) //Ctrl+LeftMouseButton
  60. {
  61. bool onLink = false;
  62. int position_start, position_end;
  63. const QTextCursor cursor = cursorForPosition(e->pos());
  64. onLink = isOnLink(*document(), cursor, position_start, position_end);
  65. //
  66. if(onLink) //if link under cursor
  67. {
  68. const QUrl link(toPlainText().mid(position_start, position_end-position_start));
  69. QDesktopServices::openUrl(link);
  70. }
  71. }
  72. else QTextEdit::mousePressEvent(e);
  73. }
  74. //Mouse moving
  75. void TextEdit::mouseMoveEvent(QMouseEvent *e)
  76. {
  77. bool onLink = false;
  78. int position_start, position_end;
  79. if(settings.getNoteLinksOpen() && e->buttons()==Qt::NoButton && e->modifiers()&Qt::ControlModifier) //Ctrl+NoMouseButton
  80. {
  81. QTextCursor cursor = cursorForPosition(e->pos());
  82. onLink = isOnLink(*document(), cursor, position_start, position_end);
  83. }
  84. if(onLink) //if link under cursor
  85. {
  86. QTextEdit::ExtraSelection sel;
  87. sel.cursor = textCursor();
  88. sel.cursor.setPosition(position_start);
  89. sel.cursor.setPosition(position_end, QTextCursor::KeepAnchor);
  90. //sel.format = highlighter->linkFormat;
  91. sel.format.setFontUnderline(true);
  92. setExtraSelections(QList<QTextEdit::ExtraSelection>() << sel); //Underlining link
  93. viewport()->setCursor(Qt::PointingHandCursor);
  94. }
  95. else
  96. {
  97. setExtraSelections(QList<QTextEdit::ExtraSelection>()); //Clearing
  98. viewport()->setCursor(Qt::IBeamCursor);
  99. }
  100. QTextEdit::mouseMoveEvent(e);
  101. }
  102. //Focus out
  103. void TextEdit::focusOutEvent(QFocusEvent*)
  104. {
  105. setExtraSelections(QList<QTextEdit::ExtraSelection>()); //Clearing
  106. }
  107. //If notes' font changed in the preferences, applying font to note
  108. void TextEdit::fontChanged()
  109. {
  110. setFont(settings.getNoteFont());
  111. }
  112. void TextEdit::linkOpenChanged()
  113. {
  114. bool is_link_open = settings.getNoteLinksOpen();
  115. setMouseTracking(is_link_open);
  116. if(!is_link_open)
  117. {
  118. setExtraSelections(QList<QTextEdit::ExtraSelection>());
  119. viewport()->setCursor(Qt::IBeamCursor);
  120. }
  121. }
  122. //Searching
  123. bool TextEdit::search(const QString& text, bool from_start)
  124. {
  125. if(from_start)
  126. {
  127. QTextCursor cursor(textCursor());
  128. cursor.movePosition(QTextCursor::Start);
  129. setTextCursor(cursor);
  130. }
  131. else
  132. {
  133. QTextCursor cursor(textCursor().block().next());
  134. setTextCursor(cursor);
  135. }
  136. return find(text);
  137. }