53 #include <QtCore/QList> 54 #include <QtCore/QMap> 55 #include <QtCore/QPointer> 56 #include <QtCore/QXmlStreamReader> 57 #include <QtCore/QXmlStreamWriter> 58 #include <QtCore/QXmlStreamAttributes> 61 #include <QColorDialog> 63 #include <QtGui/QFontDatabase> 64 #include <QtGui/QTextCursor> 65 #include <QtGui/QPainter> 66 #include <QtGui/QIcon> 68 #include <QtGui/QMoveEvent> 70 #include <QtGui/QTextDocument> 71 #include <QtGui/QTextBlock> 73 #include <QToolButton> 74 #include <QVBoxLayout> 75 #include <QHBoxLayout> 76 #include <QPushButton> 77 #include <QDialogButtonBox> 91 return name != QLatin1String(
"meta") && name != QLatin1String(
"style");
96 QXmlStreamAttributes *atts,
97 bool *paragraphAlignmentFound)
99 typedef QXmlStreamAttributes::iterator AttributeIt;
105 if (name == QLatin1String(
"body")) {
111 if (name == QLatin1String(
"p")) {
112 for (AttributeIt
it = atts->begin();
it != atts->end(); ) {
113 if (
it->name() == QLatin1String(
"align")) {
115 *paragraphAlignmentFound =
true;
117 it = atts->erase(
it);
127 const int count = in.size();
128 for (
int i = 0; i < count; i++)
129 if (!in.at(i).isSpace())
139 unsigned elementCount = 0;
140 bool paragraphAlignmentFound =
false;
142 QXmlStreamReader reader(in);
143 QXmlStreamWriter writer(&out);
144 writer.setAutoFormatting(
false);
145 writer.setAutoFormattingIndent(0);
147 while (!reader.atEnd()) {
148 switch (reader.readNext()) {
149 case QXmlStreamReader::StartElement:
152 const QStringRef name = reader.name();
153 QXmlStreamAttributes attributes = reader.attributes();
155 writer.writeStartElement(name.toString());
156 if (!attributes.isEmpty())
157 writer.writeAttributes(attributes);
159 reader.readElementText();
162 case QXmlStreamReader::Characters:
164 writer.writeCharacters(reader.text().toString());
166 case QXmlStreamReader::EndElement:
167 writer.writeEndElement();
175 *isPlainTextPtr = !paragraphAlignmentFound && elementCount == 4u;
194 QString
text(Qt::TextFormat format)
const;
228 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
241 const QTextCursor cursor =
m_editor->textCursor();
242 if (cursor.hasSelection()) {
257 if (!title.isEmpty()) {
258 QString html = QLatin1String(
"<a href=\"");
260 html += QLatin1String(
"\">");
262 html += QLatin1String(
"</a>");
290 QMenu *menu = createStandardContextMenu();
291 QMenu *htmlMenu =
new QMenu(
tr(
"Insert HTML entity"), menu);
298 const Entry entries[] = {
299 {
"&& (&&)",
"&" },
300 {
"& ",
" " },
301 {
"&< (<)",
"<" },
302 {
"&> (>)",
">" },
303 {
"&© (Copyright)",
"©" },
304 {
"&® (Trade Mark)",
"®" },
307 for (
int i = 0; i < 6; ++i) {
308 QAction *entityAction =
new QAction(QLatin1String(entries[i].text),
310 entityAction->setData(QLatin1String(entries[i].entity));
311 htmlMenu->addAction(entityAction);
314 menu->addMenu(htmlMenu);
315 connect(htmlMenu, SIGNAL(triggered(QAction*)),
317 menu->exec(event->globalPos());
323 insertPlainText(action->data().toString());
349 setText(
tr(
"Text Color"));
351 connect(
this, SIGNAL(triggered()),
this, SLOT(
chooseColor()));
360 QPainter painter(&pix);
361 painter.setRenderHint(QPainter::Antialiasing,
false);
362 painter.fillRect(pix.rect(),
m_color);
363 painter.setPen(
m_color.darker());
364 painter.drawRect(pix.rect().adjusted(0, 0, -1, -1));
370 const QColor col = QColorDialog::getColor(
m_color,
nullptr);
371 if (col.isValid() && col !=
m_color) {
382 QWidget *parent =
nullptr);
417 QObject *receiver,
const char *slot,
418 QObject *parent =
nullptr)
420 QAction *result =
new QAction(parent);
421 result->setIcon(icon);
422 result->setText(text);
423 result->setCheckable(
true);
424 result->setChecked(
false);
426 QObject::connect(result, SIGNAL(triggered(
bool)), receiver, slot);
433 m_link_action(new QAction(this)),
434 m_image_action(new QAction(this)),
436 m_font_size_input(new QComboBox),
442 const QList<int> font_sizes = QFontDatabase::standardSizes();
443 foreach (
int font_size, font_sizes)
454 QIcon(
":/ico/32x32/format-text-bold.png"),
455 tr(
"Texte en gras"), editor, SLOT(setFontBold(
bool)),
this);
460 QIcon(
":/ico/32x32/format-text-italic.png"),
461 tr(
"Texte en italique"), editor, SLOT(setFontItalic(
bool)),
this);
466 QIcon(
":/ico/32x32/format-text-underline.png"),
467 tr(
"Texte souligé"), editor, SLOT(setFontUnderline(
bool)),
this);
474 QActionGroup *alignment_group =
new QActionGroup(
this);
475 connect(alignment_group, SIGNAL(triggered(QAction*)),
480 tr(
"Left Align"), editor,
nullptr, alignment_group);
485 tr(
"Center"), editor,
nullptr, alignment_group);
490 tr(
"Right Align"), editor,
nullptr, alignment_group);
495 tr(
"Justify"), editor,
nullptr, alignment_group);
506 QIcon(
":/ico/22x22/format-text-superscript.png"),
512 QIcon(
":/ico/22x22/format-text-subscript.png"),
541 QIcon(
":/ico/32x32/simplifyrichtext.png"),
542 tr(
"Simplify Rich Text"), editor, SLOT(setSimplifyRichText(
bool)),
this);
544 connect(
m_editor, SIGNAL(simplifyRichTextChanged(
bool)),
548 connect(editor, SIGNAL(textChanged()),
this, SLOT(
updateActions()));
549 connect(editor, SIGNAL(stateChanged()),
this, SLOT(
updateActions()));
556 Qt::Alignment new_alignment;
559 new_alignment = Qt::AlignLeft;
561 new_alignment = Qt::AlignCenter;
563 new_alignment = Qt::AlignRight;
565 new_alignment = Qt::AlignJustify;
568 m_editor->setAlignment(new_alignment);
582 int i = size.toInt(&ok);
592 const QTextCharFormat::VerticalAlignment align =
super ?
593 QTextCharFormat::AlignSuperScript : QTextCharFormat::AlignNormal;
595 QTextCharFormat charFormat =
m_editor->currentCharFormat();
596 charFormat.setVerticalAlignment(align);
597 m_editor->setCurrentCharFormat(charFormat);
604 const QTextCharFormat::VerticalAlignment align =
sub ?
605 QTextCharFormat::AlignSubScript : QTextCharFormat::AlignNormal;
607 QTextCharFormat charFormat =
m_editor->currentCharFormat();
608 charFormat.setVerticalAlignment(align);
609 m_editor->setCurrentCharFormat(charFormat);
624 const QString path = IconSelector::choosePixmapResource(m_core, m_core->resourceModel(), QString(),
this);
626 m_editor->insertHtml(QLatin1String(
"<img src=\"") + path + QLatin1String(
"\"/>"));
637 const Qt::Alignment alignment =
m_editor->alignment();
638 const QTextCursor cursor =
m_editor->textCursor();
639 const QTextCharFormat charFormat = cursor.charFormat();
640 const QFont font = charFormat.font();
641 const QTextCharFormat::VerticalAlignment valign =
642 charFormat.verticalAlignment();
643 const bool superScript = valign == QTextCharFormat::AlignSuperScript;
644 const bool subScript = valign == QTextCharFormat::AlignSubScript;
646 if (alignment & Qt::AlignLeft) {
648 }
else if (alignment & Qt::AlignRight) {
650 }
else if (alignment & Qt::AlignHCenter) {
662 const int size = font.pointSize();
673 connect(
this, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
675 connect(
this, SIGNAL(cursorPositionChanged()),
687 setFontWeight(QFont::Bold);
689 setFontWeight(QFont::Normal);
694 QTextEdit::setFontPointSize(qreal(d));
699 if (Qt::mightBeRichText(
text))
718 const int pointSize = qRound(font.pointSizeF());
719 if (pointSize > 0 && !qFuzzyCompare(qreal(pointSize), font.pointSizeF())) {
720 font.setPointSize(pointSize);
723 document()->setDefaultFont(font);
724 if (font.pointSize() > 0)
735 return toPlainText();
741 const QString html = toHtml();
745 return toPlainText();
753 m_tab_widget(new QTabWidget),
756 setWindowTitle(
tr(
"Edit text"));
757 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
767 tool_bar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
769 QWidget *rich_edit =
new QWidget;
770 QVBoxLayout *rich_edit_layout =
new QVBoxLayout(rich_edit);
771 rich_edit_layout->addWidget(tool_bar);
772 rich_edit_layout->addWidget(
m_editor);
774 QWidget *plain_edit =
new QWidget;
775 QVBoxLayout *plain_edit_layout =
new QVBoxLayout(plain_edit);
785 QPushButton *ok_button = buttonBox->button(QDialogButtonBox::Ok);
786 ok_button->setText(
tr(
"&OK"));
787 ok_button->setDefault(
true);
790 connect(buttonBox, SIGNAL(rejected()),
this, SLOT(reject()));
792 QVBoxLayout *layout =
new QVBoxLayout(
this);
794 layout->addWidget(buttonBox);
830 const bool isSimplifiedRichText = !
text.startsWith(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">");
860 const int position = new_edit->textCursor().position();
869 QTextCursor cursor = new_edit->textCursor();
870 cursor.movePosition(QTextCursor::End);
871 if (cursor.position() > position) {
872 cursor.setPosition(position);
874 new_edit->setTextCursor(cursor);
892 #include "richtexteditor.moc"
void setFontPointSize(double)
void setDefaultFont(const QFont &font)
void setupUi(QDialog *AddLinkDialog)
QAction * m_align_center_action
void setSimplifyRichText(bool v)
RichTextEditor * m_editor
ColorAction * m_color_action
QAction * m_italic_action
static bool isWhiteSpace(const QStringRef &in)
void applyEditText(const QString &)
void setDefaultFont(QFont font)
void setVAlignSub(bool sub)
QAction * m_valign_sub_action
QTabWidget * m_tab_widget
void tabIndexChanged(int newIndex)
QToolBar * createToolBar(QWidget *parent=nullptr)
void actionTriggered(QAction *action)
void setText(const QString &text)
void on_buttonBox_accepted()
RichTextEditorDialog::on_buttonBox_accepted.
void contextMenuEvent(QContextMenuEvent *event) override
~RichTextEditorDialog() override
RichTextEditorDialog(QWidget *parent=nullptr)
QAction * m_align_left_action
QAction * m_align_right_action
HtmlTextEdit(QWidget *parent=nullptr)
RichTextEditor(QWidget *parent=nullptr)
void colorChanged(const QColor &color)
static void filterAttributes(const QStringRef &name, QXmlStreamAttributes *atts, bool *paragraphAlignmentFound)
ColorAction(QObject *parent)
static bool filterElement(const QStringRef &name)
void simplifyRichTextChanged(bool)
void setVAlignSuper(bool super)
static QAction * createCheckableAction(const QIcon &icon, const QString &text, QObject *receiver, const char *slot, QObject *parent=nullptr)
QPointer< RichTextEditor > m_editor
QString simplifyRichTextFilter(const QString &in, bool *isPlainTextPtr=nullptr)
void setColor(const QColor &color)
void sizeInputActivated(const QString &size)
RichTextEditor * m_editor
void alignmentActionTriggered(QAction *action)
QAction * m_simplify_richtext_action
QAction * m_valign_sup_action
QString text(Qt::TextFormat format) const
QString text(Qt::TextFormat format=Qt::AutoText) const
QComboBox * m_font_size_input
AddLinkDialog(RichTextEditor *editor, QWidget *parent=nullptr)
QAction * m_align_justify_action
const QColor & color() const
~AddLinkDialog() override
RichTextEditorToolBar(RichTextEditor *editor, QWidget *parent=nullptr)
QAction * m_underline_action
void setText(const QString &text)
void colorChanged(const QColor &color)
bool simplifyRichText() const