Qt 4.8
Public Functions | List of all members
QWindowsMimeText Class Reference
Inheritance diagram for QWindowsMimeText:
QWindowsMime

Public Functions

bool canConvertFromMime (const FORMATETC &formatetc, const QMimeData *mimeData) const
 Returns true if the converter can convert from the mimeData to the format specified in formatetc. More...
 
bool canConvertToMime (const QString &mimeType, IDataObject *pDataObj) const
 Returns true if the converter can convert to the mimeType from the available formats in pDataObj. More...
 
bool convertFromMime (const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM *pmedium) const
 Convert the mimeData to the format specified in formatetc. More...
 
QVariant convertToMime (const QString &mime, LPDATAOBJECT pDataObj, QVariant::Type preferredType) const
 
QVector< FORMATETCformatsForMime (const QString &mimeType, const QMimeData *mimeData) const
 Returns a QVector of FORMATETC structures representing the different windows clipboard formats that can be provided for the mimeType from the mimeData. More...
 
QString mimeForFormat (const FORMATETC &formatetc) const
 Returns the mime type that will be created form the format specified in formatetc, or an empty string if this converter does not support formatetc. More...
 
- Public Functions inherited from QWindowsMime
virtual QVariant convertToMime (const QString &mimeType, IDataObject *pDataObj, QVariant::Type preferredType) const =0
 Returns a QVariant containing the converted data for mimeType from pDataObj. More...
 
 QWindowsMime ()
 Constructs a new conversion object, adding it to the globally accessed list of available converters. More...
 
virtual ~QWindowsMime ()
 Destroys a conversion object, removing it from the global list of available converters. More...
 

Additional Inherited Members

- Static Public Functions inherited from QWindowsMime
static int registerMimeType (const QString &mime)
 Registers the MIME type mime, and returns an ID number identifying the format on Windows. More...
 

Detailed Description

Definition at line 425 of file qmime_win.cpp.

Functions

◆ canConvertFromMime()

bool QWindowsMimeText::canConvertFromMime ( const FORMATETC formatetc,
const QMimeData mimeData 
) const
virtual

Returns true if the converter can convert from the mimeData to the format specified in formatetc.

All subclasses must reimplement this pure virtual function.

Implements QWindowsMime.

Definition at line 436 of file qmime_win.cpp.

437 {
438  int cf = getCf(formatetc);
439  return (cf == CF_UNICODETEXT || cf == CF_TEXT) && mimeData->hasText();
440 }
static int getCf(const FORMATETC &formatetc)
Definition: qmime_win.cpp:125
bool hasText() const
Returns true if the object can return plain text (MIME type text/plain); otherwise returns false...
Definition: qmimedata.cpp:389

◆ canConvertToMime()

bool QWindowsMimeText::canConvertToMime ( const QString mimeType,
IDataObject *  pDataObj 
) const
virtual

Returns true if the converter can convert to the mimeType from the available formats in pDataObj.

All subclasses must reimplement this pure virtual function.

Implements QWindowsMime.

Definition at line 518 of file qmime_win.cpp.

519 {
520  return mimeType.startsWith(QLatin1String("text/plain"))
521  && (canGetData(CF_UNICODETEXT, pDataObj)
522  || canGetData(CF_TEXT, pDataObj));
523 }
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static bool canGetData(int cf, IDataObject *pDataObj)
Definition: qmime_win.cpp:191

◆ convertFromMime()

bool QWindowsMimeText::convertFromMime ( const FORMATETC formatetc,
const QMimeData mimeData,
STGMEDIUM pmedium 
) const
virtual

Convert the mimeData to the format specified in formatetc.

The converted data should then be placed in pmedium structure.

Return true if the conversion was successful.

All subclasses must reimplement this pure virtual function.

Implements QWindowsMime.

Definition at line 447 of file qmime_win.cpp.

448 {
449  if (canConvertFromMime(formatetc, mimeData)) {
451  int cf = getCf(formatetc);
452  if (cf == CF_TEXT) {
453  data = mimeData->text().toLocal8Bit();
454  // Anticipate required space for CRLFs at 1/40
455  int maxsize=data.size()+data.size()/40+3;
456  QByteArray r(maxsize, '\0');
457  char* o = r.data();
458  const char* d = data.data();
459  const int s = data.size();
460  bool cr=false;
461  int j=0;
462  for (int i=0; i<s; i++) {
463  char c = d[i];
464  if (c=='\r')
465  cr=true;
466  else {
467  if (c=='\n') {
468  if (!cr)
469  o[j++]='\r';
470  }
471  cr=false;
472  }
473  o[j++]=c;
474  if (j+3 >= maxsize) {
475  maxsize += maxsize/4;
476  r.resize(maxsize);
477  o = r.data();
478  }
479  }
480  o[j]=0;
481  return setData(r, pmedium);
482  } else if (cf == CF_UNICODETEXT) {
483  QString str = mimeData->text();
484  const QChar *u = str.unicode();
485  QString res;
486  const int s = str.length();
487  int maxsize = s + s/40 + 3;
488  res.resize(maxsize);
489  int ri = 0;
490  bool cr = false;
491  for (int i=0; i < s; ++i) {
492  if (*u == QLatin1Char('\r'))
493  cr = true;
494  else {
495  if (*u == QLatin1Char('\n') && !cr)
496  res[ri++] = QLatin1Char('\r');
497  cr = false;
498  }
499  res[ri++] = *u;
500  if (ri+3 >= maxsize) {
501  maxsize += maxsize/4;
502  res.resize(maxsize);
503  }
504  ++u;
505  }
506  res.truncate(ri);
507  const int byteLength = res.length() * sizeof(ushort);
508  QByteArray r(byteLength + 2, '\0');
509  memcpy(r.data(), res.unicode(), byteLength);
510  r[byteLength] = 0;
511  r[byteLength+1] = 0;
512  return setData(r, pmedium);
513  }
514  }
515  return false;
516 }
double d
Definition: qnumeric_p.h:62
unsigned char c[8]
Definition: qnumeric_p.h:62
static int getCf(const FORMATETC &formatetc)
Definition: qmime_win.cpp:125
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
quint16 u
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
void truncate(int pos)
Truncates the string at the given position index.
Definition: qstring.cpp:4603
QString text() const
Returns a plain text (MIME type text/plain) representation of the data.
Definition: qmimedata.cpp:364
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706
static bool setData(const QByteArray &data, STGMEDIUM *pmedium)
Definition: qmime_win.cpp:141
bool canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const
Returns true if the converter can convert from the mimeData to the format specified in formatetc...
Definition: qmime_win.cpp:436
static const char * data(const QByteArray &arr)
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
void resize(int size)
Sets the size of the string to size characters.
Definition: qstring.cpp:1353
unsigned short ushort
Definition: qglobal.h:995
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ convertToMime()

QVariant QWindowsMimeText::convertToMime ( const QString mime,
LPDATAOBJECT  pDataObj,
QVariant::Type  preferredType 
) const

Definition at line 544 of file qmime_win.cpp.

545 {
546  QVariant ret;
547 
548  if (canConvertToMime(mime, pDataObj)) {
549  QString str;
550  QByteArray data = getData(CF_UNICODETEXT, pDataObj);
551  if (!data.isEmpty()) {
552  str = QString::fromWCharArray((const wchar_t *)data.data());
553  str.replace(QLatin1String("\r\n"), QLatin1String("\n"));
554  } else {
555  data = getData(CF_TEXT, pDataObj);
556  if (!data.isEmpty()) {
557  const char* d = data.data();
558  const int s = qstrlen(d);
559  QByteArray r(data.size()+1, '\0');
560  char* o = r.data();
561  int j=0;
562  for (int i=0; i<s; i++) {
563  char c = d[i];
564  if (c!='\r')
565  o[j++]=c;
566  }
567  o[j]=0;
568  str = QString::fromLocal8Bit(r);
569  }
570  }
571  if (preferredType == QVariant::String)
572  ret = str;
573  else
574  ret = str.toUtf8();
575  }
576 
577  return ret;
578 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
double d
Definition: qnumeric_p.h:62
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
unsigned char c[8]
Definition: qnumeric_p.h:62
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const
Returns true if the converter can convert to the mimeType from the available formats in pDataObj...
Definition: qmime_win.cpp:518
static const char * data(const QByteArray &arr)
uint qstrlen(const char *str)
Definition: qbytearray.h:79
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
static QByteArray getData(int cf, IDataObject *pDataObj)
Definition: qmime_win.cpp:156

◆ formatsForMime()

QVector< FORMATETC > QWindowsMimeText::formatsForMime ( const QString mimeType,
const QMimeData mimeData 
) const
virtual

Returns a QVector of FORMATETC structures representing the different windows clipboard formats that can be provided for the mimeType from the mimeData.

All subclasses must reimplement this pure virtual function.

Implements QWindowsMime.

Definition at line 534 of file qmime_win.cpp.

535 {
536  QVector<FORMATETC> formatics;
537  if (mimeType.startsWith(QLatin1String("text/plain")) && mimeData->hasText()) {
538  formatics += setCf(CF_UNICODETEXT);
539  formatics += setCf(CF_TEXT);
540  }
541  return formatics;
542 }
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
static FORMATETC setCf(int cf)
Definition: qmime_win.cpp:130
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
bool hasText() const
Returns true if the object can return plain text (MIME type text/plain); otherwise returns false...
Definition: qmimedata.cpp:389

◆ mimeForFormat()

QString QWindowsMimeText::mimeForFormat ( const FORMATETC formatetc) const
virtual

Returns the mime type that will be created form the format specified in formatetc, or an empty string if this converter does not support formatetc.

All subclasses must reimplement this pure virtual function.

Implements QWindowsMime.

Definition at line 525 of file qmime_win.cpp.

526 {
527  int cf = getCf(formatetc);
528  if (cf == CF_UNICODETEXT || cf == CF_TEXT)
529  return QLatin1String("text/plain");
530  return QString();
531 }
static int getCf(const FORMATETC &formatetc)
Definition: qmime_win.cpp:125
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83

The documentation for this class was generated from the following file: