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 <QFileDialog>
13#include <QColorDialog>
14#include <QFontComboBox>
15#include <QLineEdit>
16#include <QPointer>
17
18//Marble
19#include "MarbleWidget.h"
20#include "AddLinkDialog.h"
21
22namespace Marble{
23
24class Q_DECL_HIDDEN FormattedTextWidget::Private : public Ui::FormattedTextWidget
25{
26public:
27 Private();
28 ~Private();
29
30 QColorDialog *m_textColorDialog;
31};
32
33FormattedTextWidget::Private::Private() :
34 Ui::FormattedTextWidget(),
35 m_textColorDialog( nullptr )
36{
37 //nothing to do
38}
39
40FormattedTextWidget::Private::~Private()
41{
42 delete m_textColorDialog;
43}
44
45FormattedTextWidget::FormattedTextWidget( QWidget *parent ) :
46 QWidget( parent ),
47 d( new Private() )
48{
49 d->setupUi( this );
50
51 d->m_formattedTextToolBar->insertSeparator( d->m_actionAddImage );
52 QPixmap textColorPixmap(20, 20);
53 textColorPixmap.fill( d->m_description->textCursor().charFormat().foreground().color() );
54 d->m_actionColor->setIcon( textColorPixmap );
55 d->m_textColorDialog = new QColorDialog( this );
56 d->m_textColorDialog->setOption( QColorDialog::ShowAlphaChannel );
57 d->m_textColorDialog->setCurrentColor( d->m_description->textCursor().charFormat().foreground().color() );
58 d->m_fontSize->setValidator( new QIntValidator( 1, 9000, this ) );
59 int index = d->m_fontSize->findText( QString::number( d->m_description->textCursor().charFormat().font().pointSize() ) );
60 if( index != -1 ) {
61 d->m_fontSize->setCurrentIndex( index );
62 } else {
63 d->m_fontSize->lineEdit()->setText( QString::number( d->m_description->textCursor().charFormat().font().pointSize() ) );
64 }
65 connect( d->m_actionColor, SIGNAL(triggered()), d->m_textColorDialog, SLOT(exec()) );
66 connect( d->m_textColorDialog, SIGNAL(colorSelected(QColor)), this, SLOT(setTextCursorColor(QColor)) );
67 connect( d->m_isFormattedTextMode, SIGNAL(toggled(bool)), this, SLOT(toggleDescriptionEditMode(bool)) );
68 connect( d->m_fontFamily, SIGNAL(currentFontChanged(QFont)), this, SLOT(setTextCursorFont(QFont)) );
69 connect( d->m_fontSize, SIGNAL(editTextChanged(QString)), this, SLOT(setTextCursorFontSize(QString)) );
70 connect( d->m_actionBold, SIGNAL(toggled(bool)), this, SLOT(setTextCursorBold(bool)) );
71 connect( d->m_actionItalics, SIGNAL(toggled(bool)), this, SLOT(setTextCursorItalic(bool)) );
72 connect( d->m_actionUnderlined, SIGNAL(toggled(bool)), this, SLOT(setTextCursorUnderlined(bool)) );
73 connect( d->m_actionAddImage, SIGNAL(triggered()), this, SLOT(addImageToDescription()) );
74 connect( d->m_actionAddLink, SIGNAL(triggered()), this, SLOT(addLinkToDescription()) );
75 connect( d->m_description, SIGNAL(cursorPositionChanged()), this, SLOT(updateDescriptionEditButtons()) );
76}
77
78FormattedTextWidget::~FormattedTextWidget()
79{
80 delete d;
81}
82
83void FormattedTextWidget::setText( const QString &text )
84{
85 d->m_description->setHtml( text );
86}
87
88const QString FormattedTextWidget::text()
89{
90 return d->m_description->toHtml();
91}
92
93void FormattedTextWidget::toggleDescriptionEditMode( bool isFormattedTextMode )
94{
95 d->m_formattedTextToolBar->setVisible( isFormattedTextMode );
96 d->m_fontSize->setVisible( isFormattedTextMode );
97 d->m_fontFamily->setVisible( isFormattedTextMode );
98 if( isFormattedTextMode ) {
99 d->m_description->setHtml( d->m_description->toPlainText() );
100 } else {
101 QTextCursor cursor = d->m_description->textCursor();
102 QTextCharFormat format;
103 format.setFont( QFont() );
105 format.setFontItalic( false );
106 format.setFontUnderline( false );
107 format.clearForeground();
108 cursor.setCharFormat( format );
109 d->m_description->setTextCursor( cursor );
110 d->m_description->setPlainText( d->m_description->toHtml() );
111 }
112}
113
114void FormattedTextWidget::setTextCursorBold( bool bold )
115{
116 QTextCursor cursor = d->m_description->textCursor();
117 QTextCharFormat format;
118 format.setFontWeight( bold ? QFont::Bold : QFont::Normal );
119 cursor.mergeCharFormat( format );
120 d->m_description->setTextCursor( cursor );
121}
122
123void FormattedTextWidget::setTextCursorItalic( bool italic )
124{
125 QTextCursor cursor = d->m_description->textCursor();
126 QTextCharFormat format;
127 format.setFontItalic( italic );
128 cursor.mergeCharFormat( format );
129 d->m_description->setTextCursor( cursor );
130}
131
132void FormattedTextWidget::setTextCursorUnderlined( bool underlined )
133{
134 QTextCursor cursor = d->m_description->textCursor();
135 QTextCharFormat format;
136 format.setFontUnderline( underlined );
137 cursor.mergeCharFormat( format );
138 d->m_description->setTextCursor( cursor );
139}
140
141void FormattedTextWidget::setTextCursorColor( const QColor &color )
142{
143 QTextCursor cursor = d->m_description->textCursor();
144 QTextCharFormat format;
145 QBrush brush( color );
146 format.setForeground( brush );
147 cursor.mergeCharFormat( format );
148 d->m_description->setTextCursor( cursor );
149 QPixmap textColorPixmap(22, 22);
150 textColorPixmap.fill( format.foreground().color() );
151 d->m_actionColor->setIcon( QIcon( textColorPixmap ) );
152 d->m_textColorDialog->setCurrentColor( format.foreground().color() );
153}
154
155void FormattedTextWidget::setTextCursorFont( const QFont &font )
156{
157 QTextCursor cursor = d->m_description->textCursor();
158 QTextCharFormat format;
159 format.setFontFamily( font.family() );
160 cursor.mergeCharFormat( format );
161 d->m_description->setTextCursor( cursor );
162}
163
164void FormattedTextWidget::setTextCursorFontSize( const QString &fontSize )
165{
166 bool ok = false;
167 int size = fontSize.toInt( &ok );
168 if( ok ) {
169 QTextCursor cursor = d->m_description->textCursor();
170 QTextCharFormat format;
171 format.setFontPointSize( size );
172 cursor.mergeCharFormat( format );
173 d->m_description->setTextCursor( cursor );
174 }
175}
176
177void FormattedTextWidget::addImageToDescription()
178{
179 QString filename = QFileDialog::getOpenFileName( this, tr( "Choose image" ), tr( "All Supported Files (*.png *.jpg *.jpeg)" ) );
180 QImage image( filename );
181 if( !image.isNull() ) {
182 QTextCursor cursor = d->m_description->textCursor();
183 cursor.insertImage( image, filename );
184 }
185}
186
187void FormattedTextWidget::addLinkToDescription()
188{
189 QPointer<AddLinkDialog> dialog = new AddLinkDialog( this );
190 if( dialog->exec() ) {
191 QTextCharFormat oldFormat = d->m_description->textCursor().charFormat();
192 QTextCharFormat linkFormat = oldFormat;
193 linkFormat.setAnchor( true );
194 linkFormat.setFontUnderline( true );
195 linkFormat.setForeground( QApplication::palette().link() );
196 linkFormat.setAnchorHref( dialog->url() );
197 d->m_description->textCursor().insertText( dialog->name(), linkFormat );
198 QTextCursor cursor = d->m_description->textCursor();
199 cursor.setCharFormat( oldFormat );
200 d->m_description->setTextCursor( cursor );
201 d->m_description->textCursor().insertText( " " );
202 }
203}
204
205void FormattedTextWidget::updateDescriptionEditButtons()
206{
207 disconnect( d->m_actionBold, SIGNAL(toggled(bool)), this, SLOT(setTextCursorBold(bool)) );
208 disconnect( d->m_actionItalics, SIGNAL(toggled(bool)), this, SLOT(setTextCursorItalic(bool)) );
209 disconnect( d->m_actionUnderlined, SIGNAL(toggled(bool)), this, SLOT(setTextCursorUnderlined(bool)) );
210 disconnect( d->m_fontFamily, SIGNAL(currentFontChanged(QFont)), this, SLOT(setTextCursorFont(QFont)) );
211 disconnect( d->m_fontSize, SIGNAL(editTextChanged(QString)), this, SLOT(setTextCursorFontSize(QString)) );
212
213 QTextCharFormat format = d->m_description->textCursor().charFormat();
214
215 d->m_fontFamily->setCurrentFont( format.font() );
216
217 if( format.fontWeight() == QFont::Bold ) {
218 d->m_actionBold->setChecked( true );
219 } else if ( format.fontWeight() == QFont::Normal ) {
220 d->m_actionBold->setChecked( false );
221 }
222 d->m_actionItalics->setChecked( format.fontItalic() );
223 d->m_actionUnderlined->setChecked( format.fontUnderline() );
224
225 QPixmap textColorPixmap(22, 22);
226 textColorPixmap.fill( format.foreground().color() );
227 d->m_actionColor->setIcon( QIcon( textColorPixmap ) );
228 d->m_textColorDialog->setCurrentColor( format.foreground().color() );
229
230 int index = d->m_fontSize->findText( QString::number( d->m_description->textCursor().charFormat().font().pointSize() ) );
231 if( index != -1 ) {
232 d->m_fontSize->setCurrentIndex( index );
233 } else {
234 d->m_fontSize->lineEdit()->setText( QString::number( d->m_description->textCursor().charFormat().font().pointSize() ) );
235 }
236 connect( d->m_actionBold, SIGNAL(toggled(bool)), this, SLOT(setTextCursorBold(bool)) );
237 connect( d->m_actionItalics, SIGNAL(toggled(bool)), this, SLOT(setTextCursorItalic(bool)) );
238 connect( d->m_actionUnderlined, SIGNAL(toggled(bool)), this, SLOT(setTextCursorUnderlined(bool)) );
239 connect( d->m_fontFamily, SIGNAL(currentFontChanged(QFont)), this, SLOT(setTextCursorFont(QFont)) );
240 connect( d->m_fontSize, SIGNAL(editTextChanged(QString)), this, SLOT(setTextCursorFontSize(QString)) );
241}
242
243void FormattedTextWidget::setReadOnly( bool state )
244{
245 d->m_description->setReadOnly( state );
246 d->m_formattedTextToolBar->setDisabled( state );
247 d->m_fontFamily->setDisabled( state );
248 d->m_fontSize->setDisabled( state );
249 d->m_actionColor->setDisabled( state );
250}
251
252}
253
254#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.
const QColor & color() const const
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
QString family() const const
QPalette palette()
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 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 Tue Mar 26 2024 11:18:17 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.