PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/ceb/src/url_textedit.cpp

http://cebmtpchat.googlecode.com/
C++ | 124 lines | 79 code | 21 blank | 24 comment | 22 complexity | 298903376aa53f454a1af0a44f624378 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* This file is part of CeB.
  2. * Copyright (C) 2005 Guillaume Denry
  3. *
  4. * CeB is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * any later version.
  8. *
  9. * CeB is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with CeB; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "common.h"
  19. #include "profile.h"
  20. #include "url_textedit.h"
  21. #ifdef DEBUG_NEW
  22. #define new DEBUG_NEW
  23. #endif
  24. QColor UrlTextEdit::_textBackgroundColor = QColor(0, 0, 0, 0);
  25. UrlTextEdit::UrlTextEdit(QWidget *parent) : QTextBrowser(parent)
  26. {
  27. connect(this, SIGNAL(anchorClicked(const QUrl &)),
  28. this, SLOT(myAnchorClicked(const QUrl &)));
  29. isAway = false;
  30. }
  31. void UrlTextEdit::openUrl(const QUrl &link, bool forcesystem)
  32. {
  33. // Custom browser?
  34. if (Profile::instance().linksCustomBrowser.isEmpty() || forcesystem)
  35. QDesktopServices::openUrl(link);
  36. else
  37. {
  38. QStringList args;
  39. QProcess process;
  40. args << link.toString();
  41. process.startDetached(Profile::instance().linksCustomBrowser, args);
  42. }
  43. }
  44. void UrlTextEdit::myAnchorClicked(const QUrl &link)
  45. {
  46. if (link.scheme().isEmpty() && link.host().isEmpty() && link.toString().contains('@'))
  47. openUrl(QUrl("mailto:" + link.toString()), true);
  48. if (link.scheme().isEmpty() && link.toString().contains("www."))
  49. openUrl(QUrl("http://" + link.toString()));
  50. else if (link.scheme() == "http" || link.scheme() == "https" || link.scheme() == "ftp")
  51. openUrl(link);
  52. else
  53. openUrl(link, true);
  54. }
  55. void UrlTextEdit::setSource(const QUrl &)
  56. {
  57. }
  58. void UrlTextEdit::insertLine(QTextCursor &cursor, const QString &line, const QFont *font, const QColor *color)
  59. {
  60. // Normal format
  61. QTextCharFormat charFormat;
  62. if (font) charFormat.setFont(*font);
  63. if (color) charFormat.setForeground(*color);
  64. if (Profile::instance().textSkin().isForcedBackgroundColor())
  65. charFormat.setBackground(_textBackgroundColor);
  66. // Url format
  67. QTextCharFormat urlFormat;
  68. if (font) urlFormat.setFont(*font);
  69. urlFormat.setForeground(Qt::blue);
  70. urlFormat.setFontUnderline(true);
  71. urlFormat.setAnchor(true);
  72. urlFormat.setAnchorHref("");
  73. // Search for URLs
  74. QList<RenderSegmentList::TextRange> urlRanges = _segments.urlRanges(line);
  75. // Format work
  76. int pos = 0;
  77. for (int i = 0; i < urlRanges.count(); i++)
  78. {
  79. RenderSegmentList::TextRange range = urlRanges[i];
  80. // Normal format
  81. if (pos < range.start)
  82. cursor.insertText(line.mid(pos, range.start - pos), charFormat);
  83. // Url format
  84. urlFormat.setAnchorHref(line.mid(range.start, range.length));
  85. cursor.insertText(line.mid(range.start, range.length), urlFormat);
  86. pos = range.start + range.length;
  87. }
  88. if (pos < line.length()) // remains a piece of normal format
  89. cursor.insertText(line.mid(pos, line.length() - pos), charFormat);
  90. }
  91. void UrlTextEdit::mouseReleaseEvent(QMouseEvent *e)
  92. {
  93. QTextBrowser::mouseReleaseEvent(e);
  94. if (Profile::instance().copyOnSelection)
  95. copy();
  96. }
  97. void UrlTextEdit::setText(const QString &text)
  98. {
  99. clear();
  100. QTextCursor cursor(document());
  101. cursor.movePosition(QTextCursor::Start);
  102. insertLine(cursor, text);
  103. }