工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 3978|回复: 0

[Qt] 可显示表情的编辑器

[复制链接]
发表于 2009-6-7 18:50 | 显示全部楼层 |阅读模式
似乎很久没发过新贴了。
libfetion-gui 项目里有这样一个任务:信息编辑框表情图片显示。目前的显示方式是直接显示代码,如 :) , :-p 等


实现如下:
fxeditor.h

  1. #ifndef FXEDITOR_H
  2. #define FXEDITOR_H

  3. #include <QTextEdit>
  4. typedef QPair<QString,QString> mypair;
  5. class FxEditor : public QTextEdit
  6. {
  7.     Q_OBJECT
  8. public:
  9.     FxEditor(QWidget*parent = 0);
  10.     QString toPlainText () const;
  11. public slots:
  12.     void onTextChanged();
  13. private:
  14.     // do not use map
  15.     // because it sort the key by char order
  16.     QMap<QString,QString> strToHtml;
  17.     QList<mypair> strToHtmlList;
  18. };

  19. #endif // FXEDITOR_H

复制代码
fxeditor.cpp

  1. #include "fxeditor.h"
  2. #include <QDebug>

  3. FxEditor::FxEditor(QWidget*parent):QTextEdit(parent)
  4. {
  5.     // setup the list
  6.     // this list should not be a class alone or a global variable?
  7.     mypair a("o:)","<img src="./faces/47.gif" />");
  8.     mypair b(":)","<img src="./faces/1.gif" />");
  9.     strToHtmlList<<a<<b;
  10.     strToHtml["&gt;:)"]="<img src="%path%/50.gif" />";
  11.     strToHtml["o:)"]="<img src="%path%/47.gif" />"; // error :)
  12.     strToHtml["*-:)"]="<img src="%path%/37.gif" />";
  13.     strToHtml[":-d"]="<img src="%path%/2.gif" />";
  14.     strToHtml[":-o"]="<img src="%path%/4.gif" />";
  15.     strToHtml[":-p"]="<img src="%path%/5.gif" />";
  16.     strToHtml["(h)"]="<img src="%path%/6.gif" />";
  17.     strToHtml[":-@"]="<img src="%path%/7.gif" />";
  18.     strToHtml[":("]="<img src="%path%/8.gif" />";
  19.     strToHtml[":'("]="<img src="%path%/9.gif" />";
  20.     strToHtml[":"&gt;"]="<img src="%path%/10.gif" />"; // work delay
  21.     strToHtml["^o)"]="<img src="%path%/11.gif" />";
  22.     strToHtml[":&amp;"]="<img src="%path%/12.gif" />";  // work delay
  23.     strToHtml["8o|"]="<img src="%path%/13.gif" />";
  24.     strToHtml["|-)"]="<img src="%path%/14.gif" />";
  25.     strToHtml[":-#"]="<img src="%path%/15.gif" />";
  26.     strToHtml["8-)"]="<img src="%path%/16.gif" />";
  27.     strToHtml["(s)"]="<img src="%path%/17.gif" />";
  28.     strToHtml["(st)"]="<img src="%path%/18.gif" />";
  29.     strToHtml["(o)"]="<img src="%path%/19.gif" />";
  30.     strToHtml["(l)"]="<img src="%path%/20.gif" />";
  31.     strToHtml["(u)"]="<img src="%path%/21.gif" />";
  32.     strToHtml["(@)"]="<img src="%path%/22.gif" />";
  33.     strToHtml["(&amp;)"]="<img src="%path%/23.gif" />";  // work delay
  34.     strToHtml["(sn)"]="<img src="%path%/24.gif" />";
  35.     strToHtml["(*)"]="<img src="%path%/25.gif" />";
  36.     strToHtml["(#)"]="<img src="%path%/26.gif" />";
  37.     strToHtml["(r)"]="<img src="%path%/27.gif" />";
  38.     strToHtml["(})"]="<img src="%path%/28.gif" />";
  39.     strToHtml["({)"]="<img src="%path%/29.gif" />";
  40.     strToHtml["(k)"]="<img src="%path%/30.gif" />";
  41.     strToHtml["(f)"]="<img src="%path%/31.gif" />";
  42.     strToHtml["(w)"]="<img src="%path%/32.gif" />";
  43.     strToHtml["(g)"]="<img src="%path%/33.gif" />";
  44.     strToHtml["(^)"]="<img src="%path%/34.gif" />";
  45.     strToHtml["-8"]="<img src="%path%/35.gif" />";
  46.     strToHtml["(i)"]="<img src="%path%/36.gif" />";
  47.     strToHtml["(c)"]="<img src="%path%/38.gif" />";
  48.     strToHtml["(um)"]="<img src="%path%/39.gif" />";
  49.     strToHtml["(mp)"]="<img src="%path%/40.gif" />";
  50.     strToHtml["(co)"]="<img src="%path%/41.gif" />";
  51.     strToHtml[":-|"]="<img src="%path%/42.gif" />";
  52.     strToHtml[":-/"]="<img src="%path%/43.gif" />";
  53.     strToHtml[":-s"]="<img src="%path%/44.gif" />";
  54.     strToHtml[")-|"]="<img src="%path%/45.gif" />";
  55.     strToHtml["(d)"]="<img src="%path%/46.gif" />";
  56.     strToHtml[":-?"]="<img src="%path%/48.gif" />";
  57.     strToHtml["(y)"]="<img src="%path%/49.gif" />";
  58.     strToHtml[":-b"]="<img src="%path%/51.gif" />";
  59.     strToHtml["b)"]="<img src="%path%/52.gif" />";
  60.     strToHtml[";)"]="<img src="%path%/3.gif" />";
  61.     strToHtml[":)"]="<img src="%path%/1.gif" />";
  62.     //strToHtml[""]="<img src="%path%/.gif" />";

  63.     connect(this,SIGNAL(textChanged()),this,SLOT(onTextChanged()));
  64. }
  65. QString FxEditor::toPlainText()const
  66. {
  67.     QString oristr = toHtml();
  68.     //qDebug()<<oristr;
  69.     // replace all <img ..> to str
  70.     QMap<QString, QString>::const_iterator i = strToHtml.constBegin();
  71.     while (i != strToHtml.end()) {
  72.         QString k = i.value();
  73.         oristr.replace(k.replace("%path%","./faces"),i.key());
  74.         ++i;
  75.     }
  76.     // replace all html tags
  77.     // remove <!Do..
  78.     oristr.remove(QRegExp("<!(^>)+>"));
  79.     // remove stylesheet tag and its content
  80.     oristr.remove(QRegExp("<[^>]+css">[^<]*</style>"));
  81.     // remove other tags
  82.     oristr.remove(QRegExp("<[^>]+>"));
  83.     oristr.replace("&lt;","<");
  84.     oristr.replace("&gt;",">");

  85.     // remove the newline
  86.     oristr.remove(0,2);
  87.     //qDebug()<<"\n orient plainText return :"<<QTextEdit::toPlainText()<<"\n";
  88.     //return QTextEdit::toPlainText();
  89.     return oristr;
  90. }
  91. void FxEditor::onTextChanged()
  92. {
  93.     static bool changeByMe = false;
  94.     if(changeByMe)
  95.         return;

  96.     QString oristr = toHtml();
  97.     QTextDocument *td = this->document();
  98.     bool changed = false;

  99.     int newCursorPosition = this->textCursor().position();
  100.     QMap<QString,QString>::const_iterator i = strToHtml.constBegin();
  101.     qDebug()<<oristr;
  102.     //qDebug()<<QString("position:%1").arg(newCursorPosition);
  103.     while (i != strToHtml.end()) {
  104.         //计算新位置
  105.         QTextCursor foundCursor(td);
  106.         while(!foundCursor.isNull() && !foundCursor.atEnd()){
  107.             foundCursor = td->find(i.key(),foundCursor);
  108.             if(!foundCursor.isNull()){
  109.                 qDebug()<<i.key();
  110.                 changed=true;
  111.                 int foundCursorPosition  = foundCursor.position();
  112.                 if(foundCursorPosition >= newCursorPosition){
  113.                     //@ TO FIX
  114.                     //  when & turn to &amp; && > turn to &gt;
  115.                     //  this doen't work as expect.
  116.                     newCursorPosition -= i.key().size()-1;
  117.                 }
  118.                 //QString str = td->toHtml();
  119.                 //str.replace(foundCursorPosition,i.key().size(), i.value());
  120.             }
  121.         }
  122.         //切换
  123.         QString k = i.value();
  124.         oristr.replace(i.key(),k.replace("%path%","./faces"));
  125.         ++i;
  126.     }
  127.     //qDebug()<<QString("position:%1").arg(newCursorPosition);
  128.     if(changed){
  129.         changeByMe = true;
  130.         setHtml(oristr);
  131.         QTextCursor c(td);
  132.         c.setPosition(newCursorPosition);
  133.         this->setTextCursor(c);
  134.         changeByMe = false;
  135.     }
  136.     qDebug()<<"\n"<<toPlainText()<<"\n";
  137. }

复制代码
表情符对应的图片是硬编码的,这个做法扩展性不好,更好的做法是从外部读取字符和图片对应表。这样就可以方便的支持自定义表情了
您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2024-5-15 23:08

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表