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