1 /***************************************************************************
2     QgsHTMLAnnotationDialog.cpp
3     ---------------------
4     begin                : March 2010
5     copyright            : (C) 2010 by Marco Hugentobler
6     email                : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 #include "qgshtmlannotationdialog.h"
16 #include "qgshtmlannotation.h"
17 #include "qgsannotationwidget.h"
18 #include "qgsmapcanvasannotationitem.h"
19 #include "qgsvectorlayer.h"
20 #include "qgsproject.h"
21 #include "qgsannotationmanager.h"
22 #include "qgsgui.h"
23 #include "qgshelp.h"
24 #include <QFileDialog>
25 #include <QFileInfo>
26 #include <QGraphicsScene>
27 #include <QPushButton>
28 
QgsHtmlAnnotationDialog(QgsMapCanvasAnnotationItem * item,QWidget * parent,Qt::WindowFlags f)29 QgsHtmlAnnotationDialog::QgsHtmlAnnotationDialog( QgsMapCanvasAnnotationItem *item, QWidget *parent, Qt::WindowFlags f )
30   : QDialog( parent, f )
31   , mItem( item )
32 
33 {
34   setupUi( this );
35   connect( mBrowseToolButton, &QToolButton::clicked, this, &QgsHtmlAnnotationDialog::mBrowseToolButton_clicked );
36   connect( mButtonBox, &QDialogButtonBox::clicked, this, &QgsHtmlAnnotationDialog::mButtonBox_clicked );
37   setWindowTitle( tr( "HTML Annotation" ) );
38   mEmbeddedWidget = new QgsAnnotationWidget( mItem );
39   mStackedWidget->addWidget( mEmbeddedWidget );
40   mStackedWidget->setCurrentWidget( mEmbeddedWidget );
41 
42   if ( item && item->annotation() )
43   {
44     QgsHtmlAnnotation *annotation = static_cast< QgsHtmlAnnotation * >( item->annotation() );
45     const QString file = annotation->sourceFile();
46     if ( !file.isEmpty() )
47     {
48       mFileLineEdit->setText( file );
49       mFileRadioButton->setChecked( true );
50     }
51     else
52     {
53       mHtmlSourceTextEdit->setText( annotation->htmlSource() );
54       mSourceRadioButton->setChecked( true );
55     }
56   }
57 
58   QObject::connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsHtmlAnnotationDialog::applySettingsToItem );
59   QObject::connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsHtmlAnnotationDialog::showHelp );
60   QPushButton *deleteButton = new QPushButton( tr( "Delete" ) );
61   QObject::connect( deleteButton, &QPushButton::clicked, this, &QgsHtmlAnnotationDialog::deleteItem );
62   mButtonBox->addButton( deleteButton, QDialogButtonBox::RejectRole );
63 
64   QgsGui::enableAutoGeometryRestore( this );
65 }
66 
applySettingsToItem()67 void QgsHtmlAnnotationDialog::applySettingsToItem()
68 {
69   //apply settings from embedded item widget
70   if ( mEmbeddedWidget )
71   {
72     mEmbeddedWidget->apply();
73   }
74 
75   if ( mItem && mItem->annotation() )
76   {
77     QgsHtmlAnnotation *annotation = static_cast< QgsHtmlAnnotation * >( mItem->annotation() );
78     const QString file = mFileLineEdit->text();
79     if ( mFileRadioButton->isChecked() )
80     {
81       annotation->setSourceFile( mFileLineEdit->text() );
82     }
83     else
84     {
85       annotation->setHtmlSource( mHtmlSourceTextEdit->text() );
86     }
87     mItem->update();
88   }
89 }
90 
mBrowseToolButton_clicked()91 void QgsHtmlAnnotationDialog::mBrowseToolButton_clicked()
92 {
93   QString directory;
94   const QFileInfo fi( mFileLineEdit->text() );
95   if ( fi.exists() )
96   {
97     directory = fi.absolutePath();
98   }
99   else
100   {
101     directory = QDir::homePath();
102   }
103   const QString filename = QFileDialog::getOpenFileName( nullptr, tr( "html" ), directory, QStringLiteral( "HTML (*.html *.htm);;All files (*.*)" ) );
104   mFileLineEdit->setText( filename );
105 }
106 
on_mFileRadioButton_toggled(bool checked)107 void QgsHtmlAnnotationDialog::on_mFileRadioButton_toggled( bool checked )
108 {
109   mFileLineEdit->setEnabled( checked );
110 }
111 
on_mSourceRadioButton_toggled(bool checked)112 void QgsHtmlAnnotationDialog::on_mSourceRadioButton_toggled( bool checked )
113 {
114   mHtmlSourceTextEdit->setEnabled( checked );
115 }
116 
deleteItem()117 void QgsHtmlAnnotationDialog::deleteItem()
118 {
119   if ( mItem && mItem->annotation() )
120     QgsProject::instance()->annotationManager()->removeAnnotation( mItem->annotation() );
121   mItem = nullptr;
122 }
123 
mButtonBox_clicked(QAbstractButton * button)124 void QgsHtmlAnnotationDialog::mButtonBox_clicked( QAbstractButton *button )
125 {
126   if ( mButtonBox->buttonRole( button ) == QDialogButtonBox::ApplyRole )
127   {
128     applySettingsToItem();
129   }
130 }
131 
showHelp()132 void QgsHtmlAnnotationDialog::showHelp()
133 {
134   QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#annotation-tools" ) );
135 }
136