You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

recvtextedit.cpp 5.6 kB

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "recvtextedit.h"
  2. #include <QDate>
  3. #include "emoji.h"
  4. #include <QMouseEvent>
  5. RecvTextEdit::RecvTextEdit(QWidget *parent)
  6. :QTextEdit(parent)
  7. {
  8. setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
  9. }
  10. void RecvTextEdit::mousePressEvent(QMouseEvent *e)
  11. {
  12. mPressedAnchor = (e->button() & Qt::LeftButton) ? anchorAt(e->pos()) : "";
  13. QTextEdit::mousePressEvent(e);
  14. }
  15. void RecvTextEdit::mouseReleaseEvent(QMouseEvent *e)
  16. {
  17. if (e->button() & Qt::LeftButton)
  18. {
  19. if (anchorAt(e->pos()) == mPressedAnchor && !mPressedAnchor.isEmpty())
  20. parseLink(mPressedAnchor);
  21. }
  22. QTextEdit::mouseReleaseEvent(e);
  23. }
  24. void RecvTextEdit::addFellowContent(const Content *content, long long msSinceEpoch)
  25. {
  26. drawDaySeperatorIfNewDay(msSinceEpoch);
  27. QString hint = "";
  28. if (mFellow)
  29. hint = mFellow->getName().c_str();
  30. else
  31. hint = "匿名";
  32. hint = hint+" <font color=gray>"+ timeStr(msSinceEpoch)+"</font>";
  33. moveCursor(QTextCursor::End);
  34. insertHtml(hint);
  35. append("");
  36. showContent(content, false);
  37. append("\n");
  38. moveCursor(QTextCursor::End);
  39. }
  40. void RecvTextEdit::addMyContent(const Content *content, long long msSinceEpoch)
  41. {
  42. drawDaySeperatorIfNewDay(msSinceEpoch);
  43. QString hint = "我 <font color=gray>"+timeStr(msSinceEpoch)+"</font>";
  44. moveCursor(QTextCursor::End);
  45. insertHtml(hint);
  46. append("");
  47. showContent(content, true);
  48. append("\n");
  49. moveCursor(QTextCursor::End);
  50. }
  51. void RecvTextEdit::setCurFellow(const Fellow *fellow)
  52. {
  53. if (mFellow)
  54. mDocs[mFellow] = document()->clone();//document将被清除或删除了,需clone
  55. auto it = mDocs.find(fellow);
  56. if (it != mDocs.end())
  57. {
  58. setDocument((*it).second);
  59. moveCursor(QTextCursor::End);
  60. }
  61. else
  62. {
  63. clear();
  64. }
  65. mFellow = fellow;
  66. }
  67. void RecvTextEdit::addWarning(const QString &warning)
  68. {
  69. auto align = alignment();
  70. setAlignment(Qt::AlignCenter);
  71. auto color = textColor();
  72. setTextColor(QColor(128,128,128));
  73. append(warning);
  74. append("");//结束当前段落,否则下一行恢复对齐方式时会将刚append的内容左对齐
  75. setAlignment(align);
  76. setTextColor(color);
  77. }
  78. const Fellow *RecvTextEdit::curFellow()
  79. {
  80. return mFellow;
  81. }
  82. void RecvTextEdit::parseLink(const QString &link)
  83. {
  84. QStringList parts = link.split("_");
  85. if (parts.count()<3)
  86. return;
  87. auto packetNo = parts.at(0).toLongLong();
  88. auto fileId = parts.at(1).toLongLong();
  89. bool upload = parts.at(2) == "up";
  90. emit navigateToFileTask(packetNo, fileId, upload);
  91. }
  92. QString RecvTextEdit::timeStr(long long msSinceEpoch)
  93. {
  94. QDateTime time;
  95. time.setMSecsSinceEpoch(msSinceEpoch);
  96. return time.toString("MM-dd HH:mm:ss");
  97. }
  98. void RecvTextEdit::showContent(const Content *content, bool mySelf)
  99. {
  100. switch (content->type())
  101. {
  102. case ContentType::File:
  103. showFile(static_cast<const FileContent*>(content), mySelf);
  104. break;
  105. case ContentType::Knock:
  106. showKnock(static_cast<const KnockContent*>(content), mySelf);
  107. break;
  108. case ContentType::Image:
  109. showImage(static_cast<const ImageContent*>(content));
  110. break;
  111. case ContentType::Text:
  112. showText(static_cast<const TextContent*>(content));
  113. break;
  114. default:
  115. showUnSupport();
  116. break;
  117. }
  118. }
  119. void RecvTextEdit::showFile(const FileContent *content, bool fromMySelf)
  120. {
  121. if (content->fileType == IPMSG_FILE_REGULAR)
  122. {
  123. stringstream ss;
  124. ss<<"<a href="<<content->packetNo<<"_"<<content->fileId<<"_"<<(fromMySelf?"up":"down")<<">"
  125. <<content->filename<<"("<<content->size<<")"
  126. <<"</a>";
  127. insertHtml(ss.str().c_str());
  128. }
  129. else
  130. {
  131. showUnSupport("对方发来非普通文件(可能是文件夹),收不来……");
  132. }
  133. }
  134. void RecvTextEdit::showImage(const ImageContent *content)
  135. {
  136. showUnSupport("对方发来图片,来图片,图片,片……额~还不支持!");
  137. }
  138. void RecvTextEdit::showText(const TextContent *content)
  139. {
  140. insertHtml(textHtmlStr(content));
  141. }
  142. void RecvTextEdit::showKnock(const KnockContent *content, bool mySelf)
  143. {
  144. if (mySelf)
  145. insertHtml("[发送了一个窗口抖动]");
  146. else
  147. insertHtml("[发来窗口抖动]");
  148. }
  149. void RecvTextEdit::showUnSupport(const QString& text)
  150. {
  151. QString t = text;
  152. if (t.isEmpty())
  153. t = "对方发来尚未支持的内容,无法显示";
  154. insertHtml("<font color=\"red\">"+t+"</font>");
  155. }
  156. void RecvTextEdit::drawDaySeperatorIfNewDay(long long sinceEpoch)
  157. {
  158. QDateTime cur;
  159. cur.setMSecsSinceEpoch(sinceEpoch);
  160. if (mLastEdit > 0)
  161. {
  162. QDateTime last;
  163. last.setMSecsSinceEpoch(mLastEdit);
  164. if (last.daysTo(cur)>0)
  165. {
  166. addWarning("-----------------------------");
  167. }
  168. }
  169. mLastEdit = sinceEpoch;
  170. }
  171. QString RecvTextEdit::textHtmlStr(const TextContent *content)
  172. {
  173. auto str = QString(content->text.c_str());
  174. auto htmlStr = str.toHtmlEscaped();
  175. htmlStr.replace("\r\n", "<br>");
  176. htmlStr.replace("\r", "<br>");
  177. htmlStr.replace("\n", "<br>");
  178. for (auto i = 0; i < EMOJI_LEN; i++)
  179. {
  180. auto resName = QString(":/default/res/face/")+QString::number(i+1)+".gif";
  181. auto emojiStr = QString(g_emojis[i]).toHtmlEscaped();
  182. QString imgTag = "<img src=\""+resName+"\"/>";
  183. htmlStr.replace(emojiStr, imgTag);
  184. }
  185. return htmlStr;
  186. }

mac下的“飞秋”大多数只是飞鸽传书协议,而且未发现令人满意的开源项目,所以基于c++与qt实现了基础的飞秋协议。

Contributors (1)