1 /***************************************************************************
2                               qgstextannotationdialog.cpp
3                               ---------------------------
4   begin                : February 24, 2010
5   copyright            : (C) 2010 by Marco Hugentobler
6   email                : marco dot hugentobler at hugis dot net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include "qgstextannotationdialog.h"
19 #include "qgsannotationwidget.h"
20 #include "qgstextannotation.h"
21 #include "qgsmapcanvasannotationitem.h"
22 #include "qgsannotationmanager.h"
23 #include "qgsproject.h"
24 #include "qgsgui.h"
25 #include "qgshelp.h"
26 #include "qgsfillsymbol.h"
27 
28 #include <QColorDialog>
29 #include <QGraphicsScene>
30 
QgsTextAnnotationDialog(QgsMapCanvasAnnotationItem * item,QWidget * parent,Qt::WindowFlags f)31 QgsTextAnnotationDialog::QgsTextAnnotationDialog( QgsMapCanvasAnnotationItem *item, QWidget *parent, Qt::WindowFlags f )
32   : QDialog( parent, f )
33   , mItem( item )
34   , mTextDocument( nullptr )
35 {
36   setupUi( this );
37   connect( mFontColorButton, &QgsColorButton::colorChanged, this, &QgsTextAnnotationDialog::mFontColorButton_colorChanged );
38   connect( mButtonBox, &QDialogButtonBox::clicked, this, &QgsTextAnnotationDialog::mButtonBox_clicked );
39   mEmbeddedWidget = new QgsAnnotationWidget( mItem );
40   mStackedWidget->addWidget( mEmbeddedWidget );
41   mStackedWidget->setCurrentWidget( mEmbeddedWidget );
42   connect( mEmbeddedWidget, &QgsAnnotationWidget::backgroundColorChanged, this, &QgsTextAnnotationDialog::backgroundColorChanged );
43 
44   if ( mItem && mItem->annotation() )
45   {
46     QgsTextAnnotation *annotation = static_cast< QgsTextAnnotation * >( mItem->annotation() );
47     mTextDocument.reset( annotation->document() ? annotation->document()->clone() : nullptr );
48     mTextEdit->setDocument( mTextDocument.get() );
49   }
50 
51   QgsGui::enableAutoGeometryRestore( this );
52 
53   mFontColorButton->setColorDialogTitle( tr( "Select Font Color" ) );
54   mFontColorButton->setAllowOpacity( true );
55   mFontColorButton->setContext( QStringLiteral( "symbology" ) );
56 
57   setCurrentFontPropertiesToGui();
58   backgroundColorChanged( mEmbeddedWidget->backgroundColor() );
59 
60   QObject::connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsTextAnnotationDialog::applyTextToItem );
61   QObject::connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsTextAnnotationDialog::showHelp );
62   QObject::connect( mFontComboBox, &QFontComboBox::currentFontChanged, this, &QgsTextAnnotationDialog::changeCurrentFormat );
63   QObject::connect( mFontSizeSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsTextAnnotationDialog::changeCurrentFormat );
64   QObject::connect( mBoldPushButton, &QPushButton::toggled, this, &QgsTextAnnotationDialog::changeCurrentFormat );
65   QObject::connect( mItalicsPushButton, &QPushButton::toggled, this, &QgsTextAnnotationDialog::changeCurrentFormat );
66   QObject::connect( mTextEdit, &QTextEdit::cursorPositionChanged, this, &QgsTextAnnotationDialog::setCurrentFontPropertiesToGui );
67 
68   QPushButton *deleteButton = new QPushButton( tr( "Delete" ) );
69   QObject::connect( deleteButton, &QPushButton::clicked, this, &QgsTextAnnotationDialog::deleteItem );
70   mButtonBox->addButton( deleteButton, QDialogButtonBox::RejectRole );
71 }
72 
showEvent(QShowEvent *)73 void QgsTextAnnotationDialog::showEvent( QShowEvent * )
74 {
75   backgroundColorChanged( mItem && mItem->annotation() && mItem->annotation()->fillSymbol() ? mItem->annotation()->fillSymbol()->color() : Qt::white );
76 }
77 
mButtonBox_clicked(QAbstractButton * button)78 void QgsTextAnnotationDialog::mButtonBox_clicked( QAbstractButton *button )
79 {
80   if ( mButtonBox->buttonRole( button ) == QDialogButtonBox::ApplyRole )
81   {
82     applyTextToItem();
83     mEmbeddedWidget->apply();
84   }
85 }
86 
backgroundColorChanged(const QColor & color)87 void QgsTextAnnotationDialog::backgroundColorChanged( const QColor &color )
88 {
89   QPalette p = mTextEdit->viewport()->palette();
90   p.setColor( QPalette::Base, color );
91   mTextEdit->viewport()->setPalette( p );
92   mTextEdit->setStyleSheet( QStringLiteral( "QTextEdit { background-color: %1; }" ).arg( color.name() ) );
93 }
94 
applyTextToItem()95 void QgsTextAnnotationDialog::applyTextToItem()
96 {
97   if ( mItem && mTextDocument && mItem->annotation() )
98   {
99     QgsTextAnnotation *annotation = static_cast< QgsTextAnnotation * >( mItem->annotation() );
100     //apply settings from embedded item widget
101     if ( mEmbeddedWidget )
102     {
103       mEmbeddedWidget->apply();
104     }
105     annotation->setDocument( mTextDocument.get() );
106     mItem->update();
107   }
108 }
109 
changeCurrentFormat()110 void QgsTextAnnotationDialog::changeCurrentFormat()
111 {
112   QFont newFont;
113   newFont.setFamily( mFontComboBox->currentFont().family() );
114 
115   //bold
116   if ( mBoldPushButton->isChecked() )
117   {
118     newFont.setBold( true );
119   }
120   else
121   {
122     newFont.setBold( false );
123   }
124 
125   //italic
126   if ( mItalicsPushButton->isChecked() )
127   {
128     newFont.setItalic( true );
129   }
130   else
131   {
132     newFont.setItalic( false );
133   }
134 
135   //size
136   newFont.setPointSize( mFontSizeSpinBox->value() );
137   mTextEdit->setCurrentFont( newFont );
138 
139   //color
140   mTextEdit->setTextColor( mFontColorButton->color() );
141 }
142 
mFontColorButton_colorChanged(const QColor & color)143 void QgsTextAnnotationDialog::mFontColorButton_colorChanged( const QColor &color )
144 {
145   Q_UNUSED( color )
146   changeCurrentFormat();
147 }
148 
setCurrentFontPropertiesToGui()149 void QgsTextAnnotationDialog::setCurrentFontPropertiesToGui()
150 {
151   blockAllSignals( true );
152   const QFont currentFont = mTextEdit->currentFont();
153   mFontComboBox->setCurrentFont( currentFont );
154   mFontSizeSpinBox->setValue( currentFont.pointSize() );
155   mBoldPushButton->setChecked( currentFont.bold() );
156   mItalicsPushButton->setChecked( currentFont.italic() );
157   mFontColorButton->setColor( mTextEdit->textColor() );
158   blockAllSignals( false );
159 }
160 
blockAllSignals(bool block)161 void QgsTextAnnotationDialog::blockAllSignals( bool block )
162 {
163   mFontComboBox->blockSignals( block );
164   mFontSizeSpinBox->blockSignals( block );
165   mBoldPushButton->blockSignals( block );
166   mItalicsPushButton->blockSignals( block );
167   mFontColorButton->blockSignals( block );
168 }
169 
deleteItem()170 void QgsTextAnnotationDialog::deleteItem()
171 {
172   if ( mItem && mItem->annotation() )
173     QgsProject::instance()->annotationManager()->removeAnnotation( mItem->annotation() );
174   mItem = nullptr;
175 }
176 
showHelp()177 void QgsTextAnnotationDialog::showHelp()
178 {
179   QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#annotation-tools" ) );
180 }
181