//-------------------------------------------------------------------------------- QByteArray TextZone::mimeToRtf(const QMimeData* source) const { // Parse HTML QTextDocument document; if (source->hasHtml()) { document.setHtml(source->html()); } else { document.setPlainText(source->text()); } // Convert to RTF RTF::Writer writer; QBuffer buffer; buffer.open(QIODevice::WriteOnly); writer.write(&buffer, &document, false); buffer.close(); return buffer.data(); }
QList<QByteArray> RTF::Converter::convertFromMime(const QString &mime, QVariant data, QString flavor) { QList<QByteArray> result; if (!canConvert(mime, flavor)) { return result; } // Parse HTML QTextDocument document; document.setHtml(data.toString()); // Convert to RTF QByteArray rtf; QBuffer buffer(&rtf); buffer.open(QIODevice::WriteOnly); RTF::Writer writer; writer.write(&buffer, &document); result.append(rtf); return result; }
bool Document::save() { // Save progress QSettings settings; settings.setValue("Progress/Words", m_current_wordcount); settings.setValue("Progress/Time", m_current_time); if (m_filename.isEmpty()) { return saveAs(); } // Write file to disk bool saved = true; if (!m_rich_text) { QFile file(m_filename); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream(&file); stream.setCodec(QTextCodec::codecForName("UTF-8")); stream.setGenerateByteOrderMark(true); stream << m_text->toPlainText(); saved = (file.error() == QFile::NoError); file.close(); } else { saved = false; } } else { RTF::Writer writer; saved = writer.write(m_filename, m_text); } if (!saved) { QMessageBox::critical(window(), tr("Sorry"), tr("Unable to save '%1'.").arg(m_filename)); return false; } m_text->document()->setModified(false); return true; }