Marble

FormattedTextWidget.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2014 Calin Cruceru <crucerucalincristian@gmail.com>
4// SPDX-FileCopyrightText: 2015 Constantin Mihalache <mihalache.c94@gmail.com>
5//
6
7// self
8#include "FormattedTextWidget.h"
9#include "ui_FormattedTextWidget.h"
10
11// Qt
12#include <QColorDialog>
13#include <QFileDialog>
14#include <QFontComboBox>
15#include <QLineEdit>
16#include <QPointer>
17
18// Marble
19#include "AddLinkDialog.h"
20#include "MarbleWidget.h"
21
22namespace Marble
23{
24
25class Q_DECL_HIDDEN FormattedTextWidget::Private : public Ui::FormattedTextWidget
26{
27public:
28 Private();
29 ~Private();
30
31 QColorDialog *m_textColorDialog;
32};
33
34FormattedTextWidget::Private::Private()
35 : Ui::FormattedTextWidget()
36 , m_textColorDialog(nullptr)
37{
38 // nothing to do
39}
40
41FormattedTextWidget::Private::~Private()
42{
43 delete m_textColorDialog;
44}
45
46FormattedTextWidget::FormattedTextWidget(QWidget *parent)
47 : QWidget(parent)
48 , d(new Private())
49{
50 d->setupUi(this);
51
52 d->m_formattedTextToolBar->insertSeparator(d->m_actionAddImage);
53 QPixmap textColorPixmap(20, 20);
54 textColorPixmap.fill(d->m_description->textCursor().charFormat().foreground().color());
55 d->m_actionColor->setIcon(textColorPixmap);
56 d->m_textColorDialog = new QColorDialog(this);
57 d->m_textColorDialog->setOption(QColorDialog::ShowAlphaChannel);
58 d->m_textColorDialog->setCurrentColor(d->m_description->textCursor().charFormat().foreground().color());
59 d->m_fontSize->setValidator(new QIntValidator(1, 9000, this));
60 int index = d->m_fontSize->findText(QString::number(d->m_description->textCursor().charFormat().font().pointSize()));
61 if (index != -1) {
62 d->m_fontSize->setCurrentIndex(index);
63 } else {
64 d->m_fontSize->lineEdit()->setText(QString::number(d->m_description->textCursor().charFormat().font().pointSize()));
65 }
66 connect(d->m_actionColor, &QAction::triggered, d->m_textColorDialog, &QDialog::exec);
67 connect(d->m_textColorDialog, &QColorDialog::colorSelected, this, &FormattedTextWidget::setTextCursorColor);
68 connect(d->m_isFormattedTextMode, &QAbstractButton::toggled, this, &FormattedTextWidget::toggleDescriptionEditMode);
69 connect(d->m_fontFamily, &QFontComboBox::currentFontChanged, this, &FormattedTextWidget::setTextCursorFont);
70 connect(d->m_fontSize, &QComboBox::editTextChanged, this, &FormattedTextWidget::setTextCursorFontSize);
71 connect(d->m_actionBold, &QAction::toggled, this, &FormattedTextWidget::setTextCursorBold);
72 connect(d->m_actionItalics, &QAction::toggled, this, &FormattedTextWidget::setTextCursorItalic);
73 connect(d->m_actionUnderlined, &QAction::toggled, this, &FormattedTextWidget::setTextCursorUnderlined);
74 connect(d->m_actionAddImage, &QAction::triggered, this, &FormattedTextWidget::addImageToDescription);
75 connect(d->m_actionAddLink, &QAction::triggered, this, &FormattedTextWidget::addLinkToDescription);
76 connect(d->m_description, &QTextEdit::cursorPositionChanged, this, &FormattedTextWidget::updateDescriptionEditButtons);
77}
78
79FormattedTextWidget::~FormattedTextWidget()
80{
81 delete d;
82}
83
84void FormattedTextWidget::setText(const QString &text)
85{
86 d->m_description->setHtml(text);
87}
88
89const QString FormattedTextWidget::text()
90{
91 return d->m_description->toHtml();
92}
93
94void FormattedTextWidget::toggleDescriptionEditMode(bool isFormattedTextMode)
95{
96 d->m_formattedTextToolBar->setVisible(isFormattedTextMode);
97 d->m_fontSize->setVisible(isFormattedTextMode);
98 d->m_fontFamily->setVisible(isFormattedTextMode);
99 if (isFormattedTextMode) {
100 d->m_description->setHtml(d->m_description->toPlainText());
101 } else {
102 QTextCursor cursor = d->m_description->textCursor();
103 QTextCharFormat format;
104 format.setFont(QFont());
106 format.setFontItalic(false);
107 format.setFontUnderline(false);
108 format.clearForeground();
109 cursor.setCharFormat(format);
110 d->m_description->setTextCursor(cursor);
111 d->m_description->setPlainText(d->m_description->toHtml());
112 }
113}
114
115void FormattedTextWidget::setTextCursorBold(bool bold)
116{
117 QTextCursor cursor = d->m_description->textCursor();
118 QTextCharFormat format;
119 format.setFontWeight(bold ? QFont::Bold : QFont::Normal);
120 cursor.mergeCharFormat(format);
121 d->m_description->setTextCursor(cursor);
122}
123
124void FormattedTextWidget::setTextCursorItalic(bool italic)
125{
126 QTextCursor cursor = d->m_description->textCursor();
127 QTextCharFormat format;
128 format.setFontItalic(italic);
129 cursor.mergeCharFormat(format);
130 d->m_description->setTextCursor(cursor);
131}
132
133void FormattedTextWidget::setTextCursorUnderlined(bool underlined)
134{
135 QTextCursor cursor = d->m_description->textCursor();
136 QTextCharFormat format;
137 format.setFontUnderline(underlined);
138 cursor.mergeCharFormat(format);
139 d->m_description->setTextCursor(cursor);
140}
141
142void FormattedTextWidget::setTextCursorColor(const QColor &color)
143{
144 QTextCursor cursor = d->m_description->textCursor();
145 QTextCharFormat format;
146 QBrush brush(color);
147 format.setForeground(brush);
148 cursor.mergeCharFormat(format);
149 d->m_description->setTextCursor(cursor);
150 QPixmap textColorPixmap(22, 22);
151 textColorPixmap.fill(format.foreground().color());
152 d->m_actionColor->setIcon(QIcon(textColorPixmap));
153 d->m_textColorDialog->setCurrentColor(format.foreground().color());
154}
155
156void FormattedTextWidget::setTextCursorFont(const QFont &font)
157{
158 QTextCursor cursor = d->m_description->textCursor();
159 QTextCharFormat format;
160 format.setFontFamily(font.family());
161 cursor.mergeCharFormat(format);
162 d->m_description->setTextCursor(cursor);
163}
164
165void FormattedTextWidget::setTextCursorFontSize(const QString &fontSize)
166{
167 bool ok = false;
168 int size = fontSize.toInt(&ok);
169 if (ok) {
170 QTextCursor cursor = d->m_description->textCursor();
171 QTextCharFormat format;
172 format.setFontPointSize(size);
173 cursor.mergeCharFormat(format);
174 d->m_description->setTextCursor(cursor);
175 }
176}
177
178void FormattedTextWidget::addImageToDescription()
179{
180 QString filename = QFileDialog::getOpenFileName(this, tr("Choose image"), tr("All Supported Files (*.png *.jpg *.jpeg)"));
181 QImage image(filename);
182 if (!image.isNull()) {
183 QTextCursor cursor = d->m_description->textCursor();
184 cursor.insertImage(image, filename);
185 }
186}
187
188void FormattedTextWidget::addLinkToDescription()
189{
190 QPointer<AddLinkDialog> dialog = new AddLinkDialog(this);
191 if (dialog->exec()) {
192 QTextCharFormat oldFormat = d->m_description->textCursor().charFormat();
193 QTextCharFormat linkFormat = oldFormat;
194 linkFormat.setAnchor(true);
195 linkFormat.setFontUnderline(true);
197 linkFormat.setAnchorHref(dialog->url());
198 d->m_description->textCursor().insertText(dialog->name(), linkFormat);
199 QTextCursor cursor = d->m_description->textCursor();
200 cursor.setCharFormat(oldFormat);
201 d->m_description->setTextCursor(cursor);
202 d->m_description->textCursor().insertText(QStringLiteral(" "));
203 }
204}
205
206void FormattedTextWidget::updateDescriptionEditButtons()
207{
208 disconnect(d->m_actionBold, SIGNAL(toggled(bool)), this, SLOT(setTextCursorBold(bool)));
209 disconnect(d->m_actionItalics, SIGNAL(toggled(bool)), this, SLOT(setTextCursorItalic(bool)));
210 disconnect(d->m_actionUnderlined, SIGNAL(toggled(bool)), this, SLOT(setTextCursorUnderlined(bool)));
211 disconnect(d->m_fontFamily, SIGNAL(currentFontChanged(QFont)), this, SLOT(setTextCursorFont(QFont)));
212 disconnect(d->m_fontSize, SIGNAL(editTextChanged(QString)), this, SLOT(setTextCursorFontSize(QString)));
213
214 QTextCharFormat format = d->m_description->textCursor().charFormat();
215
216 d->m_fontFamily->setCurrentFont(format.font());
217
218 if (format.fontWeight() == QFont::Bold) {
219 d->m_actionBold->setChecked(true);
220 } else if (format.fontWeight() == QFont::Normal) {
221 d->m_actionBold->setChecked(false);
222 }
223 d->m_actionItalics->setChecked(format.fontItalic());
224 d->m_actionUnderlined->setChecked(format.fontUnderline());
225
226 QPixmap textColorPixmap(22, 22);
227 textColorPixmap.fill(format.foreground().color());
228 d->m_actionColor->setIcon(QIcon(textColorPixmap));
229 d->m_textColorDialog->setCurrentColor(format.foreground().color());
230
231 int index = d->m_fontSize->findText(QString::number(d->m_description->textCursor().charFormat().font().pointSize()));
232 if (index != -1) {
233 d->m_fontSize->setCurrentIndex(index);
234 } else {
235 d->m_fontSize->lineEdit()->setText(QString::number(d->m_description->textCursor().charFormat().font().pointSize()));
236 }
237 connect(d->m_actionBold, SIGNAL(toggled(bool)), this, SLOT(setTextCursorBold(bool)));
238 connect(d->m_actionItalics, SIGNAL(toggled(bool)), this, SLOT(setTextCursorItalic(bool)));
239 connect(d->m_actionUnderlined, SIGNAL(toggled(bool)), this, SLOT(setTextCursorUnderlined(bool)));
240 connect(d->m_fontFamily, SIGNAL(currentFontChanged(QFont)), this, SLOT(setTextCursorFont(QFont)));
241 connect(d->m_fontSize, SIGNAL(editTextChanged(QString)), this, SLOT(setTextCursorFontSize(QString)));
242}
243
244void FormattedTextWidget::setReadOnly(bool state)
245{
246 d->m_description->setReadOnly(state);
247 d->m_formattedTextToolBar->setDisabled(state);
248 d->m_fontFamily->setDisabled(state);
249 d->m_fontSize->setDisabled(state);
250 d->m_actionColor->setDisabled(state);
251}
252
253}
254
255#include "moc_FormattedTextWidget.cpp"
This file contains the headers for MarbleWidget.
KIOCORE_EXPORT CopyJob * link(const QList< QUrl > &src, const QUrl &destDir, JobFlags flags=DefaultFlags)
Binds a QML item to a specific geodetic location in screen coordinates.
void toggled(bool checked)
void toggled(bool checked)
void triggered(bool checked)
const QColor & color() const const
void colorSelected(const QColor &color)
void setOption(ColorDialogOption option, bool on)
void editTextChanged(const QString &text)
virtual int exec()
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
QString family() const const
void currentFontChanged(const QFont &font)
QPalette palette()
void fill(const QColor &color)
QString number(double n, char format, int precision)
int toInt(bool *ok, int base) const const
void setFontFamily(const QString &family)
QFont font() const const
bool fontItalic() const const
bool fontUnderline() const const
int fontWeight() const const
void setAnchor(bool anchor)
void setAnchorHref(const QString &value)
void setFont(const QFont &font, FontPropertiesInheritanceBehavior behavior)
void setFontItalic(bool italic)
void setFontPointSize(qreal size)
void setFontUnderline(bool underline)
void setFontWeight(int weight)
void insertImage(const QImage &image, const QString &name)
void mergeCharFormat(const QTextCharFormat &modifier)
void setCharFormat(const QTextCharFormat &format)
void cursorPositionChanged()
void clearForeground()
QBrush foreground() const const
void setForeground(const QBrush &brush)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setupUi(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 12:02:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.