/textedit.cpp

https://github.com/sithereal/zNotes · C++ · 142 lines · 121 code · 11 blank · 10 comment · 22 complexity · ef362b1fc5e03230e08d05a0d668f34b MD5 · raw file

  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. }
  55. //Mouse pressing
  56. void TextEdit::mousePressEvent(QMouseEvent *e)
  57. {
  58. if(settings.getNoteLinksOpen() && e->buttons()==Qt::LeftButton && e->modifiers()&Qt::ControlModifier) //Ctrl+LeftMouseButton
  59. {
  60. bool onLink = false;
  61. int position_start, position_end;
  62. const QTextCursor cursor = cursorForPosition(e->pos());
  63. onLink = isOnLink(*document(), cursor, position_start, position_end);
  64. //
  65. if(onLink) //if link under cursor
  66. {
  67. const QUrl link(toPlainText().mid(position_start, position_end-position_start));
  68. QDesktopServices::openUrl(link);
  69. }
  70. }
  71. else QTextEdit::mousePressEvent(e);
  72. }
  73. //Mouse moving
  74. void TextEdit::mouseMoveEvent(QMouseEvent *e)
  75. {
  76. bool onLink = false;
  77. int position_start, position_end;
  78. if(settings.getNoteLinksOpen() && e->buttons()==Qt::NoButton && e->modifiers()&Qt::ControlModifier) //Ctrl+NoMouseButton
  79. {
  80. QTextCursor cursor = cursorForPosition(e->pos());
  81. onLink = isOnLink(*document(), cursor, position_start, position_end);
  82. }
  83. if(onLink) //if link under cursor
  84. {
  85. QTextEdit::ExtraSelection sel;
  86. sel.cursor = textCursor();
  87. sel.cursor.setPosition(position_start);
  88. sel.cursor.setPosition(position_end, QTextCursor::KeepAnchor);
  89. //sel.format = highlighter->linkFormat;
  90. sel.format.setFontUnderline(true);
  91. setExtraSelections(QList<QTextEdit::ExtraSelection>() << sel); //Underlining link
  92. viewport()->setCursor(Qt::PointingHandCursor);
  93. }
  94. else
  95. {
  96. setExtraSelections(QList<QTextEdit::ExtraSelection>()); //Clearing
  97. viewport()->setCursor(Qt::IBeamCursor);
  98. }
  99. QTextEdit::mouseMoveEvent(e);
  100. }
  101. //Focus out
  102. void TextEdit::focusOutEvent(QFocusEvent*)
  103. {
  104. setExtraSelections(QList<QTextEdit::ExtraSelection>()); //Clearing
  105. }
  106. //If notes' font changed in the preferences, applying font to note
  107. void TextEdit::fontChanged()
  108. {
  109. setFont(settings.getNoteFont());
  110. }
  111. void TextEdit::linkOpenChanged()
  112. {
  113. bool is_link_open = settings.getNoteLinksOpen();
  114. setMouseTracking(is_link_open);
  115. if(!is_link_open)
  116. {
  117. setExtraSelections(QList<QTextEdit::ExtraSelection>());
  118. viewport()->setCursor(Qt::IBeamCursor);
  119. }
  120. }
  121. //Searching
  122. bool TextEdit::search(const QString& text, bool next)
  123. {
  124. if(next) //search next
  125. {
  126. QTextCursor cursor(textCursor().block().next());
  127. setTextCursor(cursor);
  128. }
  129. else unsetCursor(); //new search
  130. return find(text);
  131. }