Ticket #646: copy-emoticons-as-text.patch

File copy-emoticons-as-text.patch, 4.6 kB (added by erijo, 2 years ago)

Patch for the second part.

  • mlview.h

     
    4747 
    4848public slots: 
    4949  virtual void setSource(const QString& name); 
     50  virtual void copy(); 
     51 
     52private slots: 
    5053  void slotCopyUrl(); 
     54  void slotClipboardSelectionChanged(); 
     55  void slotClipboardDataChanged(); 
    5156 
    5257private: 
    5358  bool m_handleLinks; 
    5459  QString m_url; 
     60  int m_clipboardMode; 
    5561 
    5662signals: 
    5763  void viewurl(QWidget*, QString); 
  • emoticon.h

     
    8383    void themeChanged(); 
    8484 
    8585  private: 
     86    CEmoticons(const CEmoticons&); 
     87    CEmoticons& operator=(const CEmoticons&); 
     88 
    8689    static CEmoticons *m_self; 
    8790 
    8891    class Impl; 
  • mlview.cpp

     
    2727 
    2828#include <qapplication.h> 
    2929#include <qclipboard.h> 
     30#include <qdragobject.h> 
     31#include <qmime.h> 
    3032#include <qpopupmenu.h> 
    3133#include <qregexp.h> 
    3234 
     
    3436#include "emoticon.h" 
    3537 
    3638MLView::MLView (QWidget* parent, const char *name) 
    37   : QTextBrowser(parent, name), m_handleLinks(true) 
     39  : QTextBrowser(parent, name), m_handleLinks(true), m_clipboardMode(-1) 
    3840{ 
    3941  setWordWrap(WidgetWidth); 
    4042#if QT_VERSION >= 0x030100 
     
    178180    // This copies m_url to both the normal clipboard (Ctrl+C/V/X) 
    179181    // and the selection clipboard (paste with middle mouse button). 
    180182    QClipboard *cb = QApplication::clipboard(); 
    181     cb->setText(m_url); 
     183    cb->setText(m_url, QClipboard::Clipboard); 
    182184    if (cb->supportsSelection()) 
     185      cb->setText(m_url, QClipboard::Selection); 
     186  } 
     187} 
     188 
     189/** @see MLView::copy() */ 
     190void MLView::slotClipboardSelectionChanged() 
     191{ 
     192  m_clipboardMode = QClipboard::Selection; 
     193} 
     194 
     195/** @see MLView::copy() */ 
     196void MLView::slotClipboardDataChanged() 
     197{ 
     198  m_clipboardMode = QClipboard::Clipboard; 
     199} 
     200 
     201/** 
     202 * After we have copied the text, we update the copy so that all emoticons are 
     203 * replaced with their respective smiley. 
     204 */ 
     205void MLView::copy() 
     206{ 
     207  m_clipboardMode = -1; 
     208 
     209  // We connect these so that we know which clipboard changed (selection or data) 
     210  connect(QApplication::clipboard(), SIGNAL(selectionChanged()), this, SLOT(slotClipboardSelectionChanged())); 
     211  connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(slotClipboardDataChanged())); 
     212 
     213  QTextBrowser::copy(); 
     214 
     215  disconnect(QApplication::clipboard(), SIGNAL(selectionChanged()), this, SLOT(slotClipboardSelectionChanged())); 
     216  disconnect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(slotClipboardDataChanged())); 
     217 
     218  // m_clipboardMode is set in one of slotClipboard{Selection,Data}Changed() 
     219  if (m_clipboardMode != -1) 
     220  { 
     221    // QTextEdit copies the text both as rich text and as regular text. 
     222    // We parse the rich text and then update the regular text. 
     223    QMimeSource *m = QApplication::clipboard()->data(static_cast<QClipboard::Mode>(m_clipboardMode)); 
     224    if (m->provides("application/x-qrichtext") && QTextDrag::canDecode(m)) 
    183225    { 
    184       bool enabled = cb->selectionModeEnabled(); 
    185       cb->setSelectionMode(!enabled); 
    186       cb->setText(m_url); 
    187       cb->setSelectionMode(enabled); 
     226      if (QTextDrag *drag = dynamic_cast<QTextDrag*>(m)) 
     227      { 
     228        QString text = QString::fromUtf8(m->encodedData("application/x-qrichtext").data()); 
     229        qDebug("'%s'", text.latin1()); 
     230 
     231        const QRegExp imageAlt("<img alt=([^>]*) src=[^>]* >"); 
     232        text.replace(imageAlt, "\\1"); 
     233 
     234        const QRegExp newline("<br( /)?>"); 
     235        text.replace(newline, "\n"); 
     236 
     237        const QRegExp htmlTags("</?[^>]+>"); 
     238        text.remove(htmlTags); 
     239 
     240        text.replace("&lt;", "<"); 
     241        text.replace("&gt;", ">"); 
     242        text.replace("&amp;", "&"); 
     243 
     244        drag->setText(text); 
     245      } 
    188246    } 
    189247  } 
    190248} 
  • emoticon.cpp

     
    456456            } 
    457457          } 
    458458 
    459           const QString img = QString::fromLatin1("<img src=\"%1\" title=\"%2\" />").arg(emo.file).arg(emo.escapedSmiley); 
     459          const QString img = QString::fromLatin1("<img src=\"%1\" alt=\"%2\">").arg(emo.file).arg(emo.escapedSmiley); 
    460460//           qDebug(" Replacing '%s' with '%s'", message.mid(pos, emo.escapedSmiley.length()).latin1(), img.latin1()); 
    461461          message.replace(pos, emo.escapedSmiley.length(), img); 
    462462          pos += img.length() - 1; // Point pos at '>'