File indexing completed on 2024-04-21 03:49:34
0001 // SPDX-License-Identifier: LGPL-2.1-or-later 0002 // 0003 // SPDX-FileCopyrightText: 2014 Calin Cruceru <crucerucalincristian@gmail.com> 0004 // SPDX-FileCopyrightText: 2015 Constantin Mihalache <mihalache.c94@gmail.com> 0005 // 0006 0007 //self 0008 #include "FormattedTextWidget.h" 0009 #include "ui_FormattedTextWidget.h" 0010 0011 //Qt 0012 #include <QFileDialog> 0013 #include <QColorDialog> 0014 #include <QFontComboBox> 0015 #include <QLineEdit> 0016 #include <QPointer> 0017 0018 //Marble 0019 #include "MarbleWidget.h" 0020 #include "AddLinkDialog.h" 0021 0022 namespace Marble{ 0023 0024 class Q_DECL_HIDDEN FormattedTextWidget::Private : public Ui::FormattedTextWidget 0025 { 0026 public: 0027 Private(); 0028 ~Private(); 0029 0030 QColorDialog *m_textColorDialog; 0031 }; 0032 0033 FormattedTextWidget::Private::Private() : 0034 Ui::FormattedTextWidget(), 0035 m_textColorDialog( nullptr ) 0036 { 0037 //nothing to do 0038 } 0039 0040 FormattedTextWidget::Private::~Private() 0041 { 0042 delete m_textColorDialog; 0043 } 0044 0045 FormattedTextWidget::FormattedTextWidget( QWidget *parent ) : 0046 QWidget( parent ), 0047 d( new Private() ) 0048 { 0049 d->setupUi( this ); 0050 0051 d->m_formattedTextToolBar->insertSeparator( d->m_actionAddImage ); 0052 QPixmap textColorPixmap(20, 20); 0053 textColorPixmap.fill( d->m_description->textCursor().charFormat().foreground().color() ); 0054 d->m_actionColor->setIcon( textColorPixmap ); 0055 d->m_textColorDialog = new QColorDialog( this ); 0056 d->m_textColorDialog->setOption( QColorDialog::ShowAlphaChannel ); 0057 d->m_textColorDialog->setCurrentColor( d->m_description->textCursor().charFormat().foreground().color() ); 0058 d->m_fontSize->setValidator( new QIntValidator( 1, 9000, this ) ); 0059 int index = d->m_fontSize->findText( QString::number( d->m_description->textCursor().charFormat().font().pointSize() ) ); 0060 if( index != -1 ) { 0061 d->m_fontSize->setCurrentIndex( index ); 0062 } else { 0063 d->m_fontSize->lineEdit()->setText( QString::number( d->m_description->textCursor().charFormat().font().pointSize() ) ); 0064 } 0065 connect( d->m_actionColor, SIGNAL(triggered()), d->m_textColorDialog, SLOT(exec()) ); 0066 connect( d->m_textColorDialog, SIGNAL(colorSelected(QColor)), this, SLOT(setTextCursorColor(QColor)) ); 0067 connect( d->m_isFormattedTextMode, SIGNAL(toggled(bool)), this, SLOT(toggleDescriptionEditMode(bool)) ); 0068 connect( d->m_fontFamily, SIGNAL(currentFontChanged(QFont)), this, SLOT(setTextCursorFont(QFont)) ); 0069 connect( d->m_fontSize, SIGNAL(editTextChanged(QString)), this, SLOT(setTextCursorFontSize(QString)) ); 0070 connect( d->m_actionBold, SIGNAL(toggled(bool)), this, SLOT(setTextCursorBold(bool)) ); 0071 connect( d->m_actionItalics, SIGNAL(toggled(bool)), this, SLOT(setTextCursorItalic(bool)) ); 0072 connect( d->m_actionUnderlined, SIGNAL(toggled(bool)), this, SLOT(setTextCursorUnderlined(bool)) ); 0073 connect( d->m_actionAddImage, SIGNAL(triggered()), this, SLOT(addImageToDescription()) ); 0074 connect( d->m_actionAddLink, SIGNAL(triggered()), this, SLOT(addLinkToDescription()) ); 0075 connect( d->m_description, SIGNAL(cursorPositionChanged()), this, SLOT(updateDescriptionEditButtons()) ); 0076 } 0077 0078 FormattedTextWidget::~FormattedTextWidget() 0079 { 0080 delete d; 0081 } 0082 0083 void FormattedTextWidget::setText( const QString &text ) 0084 { 0085 d->m_description->setHtml( text ); 0086 } 0087 0088 const QString FormattedTextWidget::text() 0089 { 0090 return d->m_description->toHtml(); 0091 } 0092 0093 void FormattedTextWidget::toggleDescriptionEditMode( bool isFormattedTextMode ) 0094 { 0095 d->m_formattedTextToolBar->setVisible( isFormattedTextMode ); 0096 d->m_fontSize->setVisible( isFormattedTextMode ); 0097 d->m_fontFamily->setVisible( isFormattedTextMode ); 0098 if( isFormattedTextMode ) { 0099 d->m_description->setHtml( d->m_description->toPlainText() ); 0100 } else { 0101 QTextCursor cursor = d->m_description->textCursor(); 0102 QTextCharFormat format; 0103 format.setFont( QFont() ); 0104 format.setFontWeight( QFont::Normal ); 0105 format.setFontItalic( false ); 0106 format.setFontUnderline( false ); 0107 format.clearForeground(); 0108 cursor.setCharFormat( format ); 0109 d->m_description->setTextCursor( cursor ); 0110 d->m_description->setPlainText( d->m_description->toHtml() ); 0111 } 0112 } 0113 0114 void FormattedTextWidget::setTextCursorBold( bool bold ) 0115 { 0116 QTextCursor cursor = d->m_description->textCursor(); 0117 QTextCharFormat format; 0118 format.setFontWeight( bold ? QFont::Bold : QFont::Normal ); 0119 cursor.mergeCharFormat( format ); 0120 d->m_description->setTextCursor( cursor ); 0121 } 0122 0123 void FormattedTextWidget::setTextCursorItalic( bool italic ) 0124 { 0125 QTextCursor cursor = d->m_description->textCursor(); 0126 QTextCharFormat format; 0127 format.setFontItalic( italic ); 0128 cursor.mergeCharFormat( format ); 0129 d->m_description->setTextCursor( cursor ); 0130 } 0131 0132 void FormattedTextWidget::setTextCursorUnderlined( bool underlined ) 0133 { 0134 QTextCursor cursor = d->m_description->textCursor(); 0135 QTextCharFormat format; 0136 format.setFontUnderline( underlined ); 0137 cursor.mergeCharFormat( format ); 0138 d->m_description->setTextCursor( cursor ); 0139 } 0140 0141 void FormattedTextWidget::setTextCursorColor( const QColor &color ) 0142 { 0143 QTextCursor cursor = d->m_description->textCursor(); 0144 QTextCharFormat format; 0145 QBrush brush( color ); 0146 format.setForeground( brush ); 0147 cursor.mergeCharFormat( format ); 0148 d->m_description->setTextCursor( cursor ); 0149 QPixmap textColorPixmap(22, 22); 0150 textColorPixmap.fill( format.foreground().color() ); 0151 d->m_actionColor->setIcon( QIcon( textColorPixmap ) ); 0152 d->m_textColorDialog->setCurrentColor( format.foreground().color() ); 0153 } 0154 0155 void FormattedTextWidget::setTextCursorFont( const QFont &font ) 0156 { 0157 QTextCursor cursor = d->m_description->textCursor(); 0158 QTextCharFormat format; 0159 format.setFontFamily( font.family() ); 0160 cursor.mergeCharFormat( format ); 0161 d->m_description->setTextCursor( cursor ); 0162 } 0163 0164 void FormattedTextWidget::setTextCursorFontSize( const QString &fontSize ) 0165 { 0166 bool ok = false; 0167 int size = fontSize.toInt( &ok ); 0168 if( ok ) { 0169 QTextCursor cursor = d->m_description->textCursor(); 0170 QTextCharFormat format; 0171 format.setFontPointSize( size ); 0172 cursor.mergeCharFormat( format ); 0173 d->m_description->setTextCursor( cursor ); 0174 } 0175 } 0176 0177 void FormattedTextWidget::addImageToDescription() 0178 { 0179 QString filename = QFileDialog::getOpenFileName( this, tr( "Choose image" ), tr( "All Supported Files (*.png *.jpg *.jpeg)" ) ); 0180 QImage image( filename ); 0181 if( !image.isNull() ) { 0182 QTextCursor cursor = d->m_description->textCursor(); 0183 cursor.insertImage( image, filename ); 0184 } 0185 } 0186 0187 void FormattedTextWidget::addLinkToDescription() 0188 { 0189 QPointer<AddLinkDialog> dialog = new AddLinkDialog( this ); 0190 if( dialog->exec() ) { 0191 QTextCharFormat oldFormat = d->m_description->textCursor().charFormat(); 0192 QTextCharFormat linkFormat = oldFormat; 0193 linkFormat.setAnchor( true ); 0194 linkFormat.setFontUnderline( true ); 0195 linkFormat.setForeground( QApplication::palette().link() ); 0196 linkFormat.setAnchorHref( dialog->url() ); 0197 d->m_description->textCursor().insertText( dialog->name(), linkFormat ); 0198 QTextCursor cursor = d->m_description->textCursor(); 0199 cursor.setCharFormat( oldFormat ); 0200 d->m_description->setTextCursor( cursor ); 0201 d->m_description->textCursor().insertText( " " ); 0202 } 0203 } 0204 0205 void FormattedTextWidget::updateDescriptionEditButtons() 0206 { 0207 disconnect( d->m_actionBold, SIGNAL(toggled(bool)), this, SLOT(setTextCursorBold(bool)) ); 0208 disconnect( d->m_actionItalics, SIGNAL(toggled(bool)), this, SLOT(setTextCursorItalic(bool)) ); 0209 disconnect( d->m_actionUnderlined, SIGNAL(toggled(bool)), this, SLOT(setTextCursorUnderlined(bool)) ); 0210 disconnect( d->m_fontFamily, SIGNAL(currentFontChanged(QFont)), this, SLOT(setTextCursorFont(QFont)) ); 0211 disconnect( d->m_fontSize, SIGNAL(editTextChanged(QString)), this, SLOT(setTextCursorFontSize(QString)) ); 0212 0213 QTextCharFormat format = d->m_description->textCursor().charFormat(); 0214 0215 d->m_fontFamily->setCurrentFont( format.font() ); 0216 0217 if( format.fontWeight() == QFont::Bold ) { 0218 d->m_actionBold->setChecked( true ); 0219 } else if ( format.fontWeight() == QFont::Normal ) { 0220 d->m_actionBold->setChecked( false ); 0221 } 0222 d->m_actionItalics->setChecked( format.fontItalic() ); 0223 d->m_actionUnderlined->setChecked( format.fontUnderline() ); 0224 0225 QPixmap textColorPixmap(22, 22); 0226 textColorPixmap.fill( format.foreground().color() ); 0227 d->m_actionColor->setIcon( QIcon( textColorPixmap ) ); 0228 d->m_textColorDialog->setCurrentColor( format.foreground().color() ); 0229 0230 int index = d->m_fontSize->findText( QString::number( d->m_description->textCursor().charFormat().font().pointSize() ) ); 0231 if( index != -1 ) { 0232 d->m_fontSize->setCurrentIndex( index ); 0233 } else { 0234 d->m_fontSize->lineEdit()->setText( QString::number( d->m_description->textCursor().charFormat().font().pointSize() ) ); 0235 } 0236 connect( d->m_actionBold, SIGNAL(toggled(bool)), this, SLOT(setTextCursorBold(bool)) ); 0237 connect( d->m_actionItalics, SIGNAL(toggled(bool)), this, SLOT(setTextCursorItalic(bool)) ); 0238 connect( d->m_actionUnderlined, SIGNAL(toggled(bool)), this, SLOT(setTextCursorUnderlined(bool)) ); 0239 connect( d->m_fontFamily, SIGNAL(currentFontChanged(QFont)), this, SLOT(setTextCursorFont(QFont)) ); 0240 connect( d->m_fontSize, SIGNAL(editTextChanged(QString)), this, SLOT(setTextCursorFontSize(QString)) ); 0241 } 0242 0243 void FormattedTextWidget::setReadOnly( bool state ) 0244 { 0245 d->m_description->setReadOnly( state ); 0246 d->m_formattedTextToolBar->setDisabled( state ); 0247 d->m_fontFamily->setDisabled( state ); 0248 d->m_fontSize->setDisabled( state ); 0249 d->m_actionColor->setDisabled( state ); 0250 } 0251 0252 } 0253 0254 #include "moc_FormattedTextWidget.cpp"