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.7 kB

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

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

Contributors (1)