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 <QColorDialog>
27 #include <QGraphicsScene>
28 
QgsTextAnnotationDialog(QgsMapCanvasAnnotationItem * item,QWidget * parent,Qt::WindowFlags f)29 QgsTextAnnotationDialog::QgsTextAnnotationDialog( QgsMapCanvasAnnotationItem *item, QWidget *parent, Qt::WindowFlags f )
30   : QDialog( parent, f )
31   , mItem( item )
32   , mTextDocument( nullptr )
33 {
34   setupUi( this );
35   connect( mFontColorButton, &QgsColorButton::colorChanged, this, &QgsTextAnnotationDialog::mFontColorButton_colorChanged );
36   connect( mButtonBox, &QDialogButtonBox::clicked, this, &QgsTextAnnotationDialog::mButtonBox_clicked );
37   mEmbeddedWidget = new QgsAnnotationWidget( mItem );
38   mStackedWidget->addWidget( mEmbeddedWidget );
39   mStackedWidget->setCurrentWidget( mEmbeddedWidget );
40   connect( mEmbeddedWidget, &QgsAnnotationWidget::backgroundColorChanged, this, &QgsTextAnnotationDialog::backgroundColorChanged );
41 
42   if ( mItem && mItem->annotation() )
43   {
44     QgsTextAnnotation *annotation = static_cast< QgsTextAnnotation * >( mItem->annotation() );
45     mTextDocument.reset( annotation->document() ? annotation->document()->clone() : nullptr );
46     mTextEdit->setDocument( mTextDocument.get() );
47   }
48 
49   QgsGui::enableAutoGeometryRestore( this );
50 
51   mFontColorButton->setColorDialogTitle( tr( "Select Font Color" ) );
52   mFontColorButton->setAllowOpacity( true );
53   mFontColorButton->setContext( QStringLiteral( "symbology" ) );
54 
55   setCurrentFontPropertiesToGui();
56   backgroundColorChanged( mEmbeddedWidget->backgroundColor() );
57 
58   QObject::connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsTextAnnotationDialog::applyTextToItem );
59   QObject::connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsTextAnnotationDialog::showHelp );
60   QObject::connect( mFontComboBox, &QFontComboBox::currentFontChanged, this, &QgsTextAnnotationDialog::changeCurrentFormat );
61   QObject::connect( mFontSizeSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsTextAnnotationDialog::changeCurrentFormat );
62   QObject::connect( mBoldPushButton, &QPushButton::toggled, this, &QgsTextAnnotationDialog::changeCurrentFormat );
63   QObject::connect( mItalicsPushButton, &QPushButton::toggled, this, &QgsTextAnnotationDialog::changeCurrentFormat );
64   QObject::connect( mTextEdit, &QTextEdit::cursorPositionChanged, this, &QgsTextAnnotationDialog::setCurrentFontPropertiesToGui );
65 
66   QPushButton *deleteButton = new QPushButton( tr( "Delete" ) );
67   QObject::connect( deleteButton, &QPushButton::clicked, this, &QgsTextAnnotationDialog::deleteItem );
68   mButtonBox->addButton( deleteButton, QDialogButtonBox::RejectRole );
69 }
70 
showEvent(QShowEvent *)71 void QgsTextAnnotationDialog::showEvent( QShowEvent * )
72 {
73   backgroundColorChanged( mItem && mItem->annotation() && mItem->annotation()->fillSymbol() ? mItem->annotation()->fillSymbol()->color() : Qt::white );
74 }
75 
mButtonBox_clicked(QAbstractButton * button)76 void QgsTextAnnotationDialog::mButtonBox_clicked( QAbstractButton *button )
77 {
78   if ( mButtonBox->buttonRole( button ) == QDialogButtonBox::ApplyRole )
79   {
80     applyTextToItem();
81     mEmbeddedWidget->apply();
82   }
83 }
84 
backgroundColorChanged(const QColor & color)85 void QgsTextAnnotationDialog::backgroundColorChanged( const QColor &color )
86 {
87   QPalette p = mTextEdit->viewport()->palette();
88   p.setColor( QPalette::Base, color );
89   mTextEdit->viewport()->setPalette( p );
90   mTextEdit->setStyleSheet( QStringLiteral( "QTextEdit { background-color: %1; }" ).arg( color.name() ) );
91 }
92 
applyTextToItem()93 void QgsTextAnnotationDialog::applyTextToItem()
94 {
95   if ( mItem && mTextDocument && mItem->annotation() )
96   {
97     QgsTextAnnotation *annotation = static_cast< QgsTextAnnotation * >( mItem->annotation() );
98     //apply settings from embedded item widget
99     if ( mEmbeddedWidget )
100     {
101       mEmbeddedWidget->apply();
102     }
103     annotation->setDocument( mTextDocument.get() );
104     mItem->update();
105   }
106 }
107 
changeCurrentFormat()108 void QgsTextAnnotationDialog::changeCurrentFormat()
109 {
110   QFont newFont;
111   newFont.setFamily( mFontComboBox->currentFont().family() );
112 
113   //bold
114   if ( mBoldPushButton->isChecked() )
115   {
116     newFont.setBold( true );
117   }
118   else
119   {
120     newFont.setBold( false );
121   }
122 
123   //italic
124   if ( mItalicsPushButton->isChecked() )
125   {
126     newFont.setItalic( true );
127   }
128   else
129   {
130     newFont.setItalic( false );
131   }
132 
133   //size
134   newFont.setPointSize( mFontSizeSpinBox->value() );
135   mTextEdit->setCurrentFont( newFont );
136 
137   //color
138   mTextEdit->setTextColor( mFontColorButton->color() );
139 }
140 
mFontColorButton_colorChanged(const QColor & color)141 void QgsTextAnnotationDialog::mFontColorButton_colorChanged( const QColor &color )
142 {
143   Q_UNUSED( color )
144   changeCurrentFormat();
145 }
146 
setCurrentFontPropertiesToGui()147 void QgsTextAnnotationDialog::setCurrentFontPropertiesToGui()
148 {
149   blockAllSignals( true );
150   QFont currentFont = mTextEdit->currentFont();
151   mFontComboBox->setCurrentFont( currentFont );
152   mFontSizeSpinBox->setValue( currentFont.pointSize() );
153   mBoldPushButton->setChecked( currentFont.bold() );
154   mItalicsPushButton->setChecked( currentFont.italic() );
155   mFontColorButton->setColor( mTextEdit->textColor() );
156   blockAllSignals( false );
157 }
158 
blockAllSignals(bool block)159 void QgsTextAnnotationDialog::blockAllSignals( bool block )
160 {
161   mFontComboBox->blockSignals( block );
162   mFontSizeSpinBox->blockSignals( block );
163   mBoldPushButton->blockSignals( block );
164   mItalicsPushButton->blockSignals( block );
165   mFontColorButton->blockSignals( block );
166 }
167 
deleteItem()168 void QgsTextAnnotationDialog::deleteItem()
169 {
170   if ( mItem && mItem->annotation() )
171     QgsProject::instance()->annotationManager()->removeAnnotation( mItem->annotation() );
172   mItem = nullptr;
173 }
174 
showHelp()175 void QgsTextAnnotationDialog::showHelp()
176 {
177   QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#annotation-tools" ) );
178 }
179