QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgssubstitutionlistwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssubstitutionlistwidget.cpp
3 -----------------------------
4 begin : August 2016
5 copyright : (C) 2016 Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7
8
9 ***************************************************************************/
10
11/***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19
21#include "moc_qgssubstitutionlistwidget.cpp"
22#include "qgsgui.h"
23
24#include <QDialogButtonBox>
25#include <QCheckBox>
26#include <QFileDialog>
27#include <QMessageBox>
28#include <QTextStream>
29
31 : QgsPanelWidget( parent )
32{
33 setupUi( this );
35
36 connect( mButtonAdd, &QToolButton::clicked, this, &QgsSubstitutionListWidget::mButtonAdd_clicked );
37 connect( mButtonRemove, &QToolButton::clicked, this, &QgsSubstitutionListWidget::mButtonRemove_clicked );
38 connect( mButtonExport, &QToolButton::clicked, this, &QgsSubstitutionListWidget::mButtonExport_clicked );
39 connect( mButtonImport, &QToolButton::clicked, this, &QgsSubstitutionListWidget::mButtonImport_clicked );
40 connect( mTableSubstitutions, &QTableWidget::cellChanged, this, &QgsSubstitutionListWidget::tableChanged );
41}
42
44{
45 mTableSubstitutions->blockSignals( true );
46 mTableSubstitutions->clearContents();
47 const auto constReplacements = substitutions.replacements();
48 for ( const QgsStringReplacement &replacement : constReplacements )
49 {
50 addSubstitution( replacement );
51 }
52 mTableSubstitutions->blockSignals( false );
53}
54
56{
57 QList< QgsStringReplacement > result;
58 for ( int i = 0; i < mTableSubstitutions->rowCount(); ++i )
59 {
60 if ( !mTableSubstitutions->item( i, 0 ) )
61 continue;
62
63 if ( mTableSubstitutions->item( i, 0 )->text().isEmpty() )
64 continue;
65
66 QCheckBox *chkCaseSensitive = qobject_cast<QCheckBox *>( mTableSubstitutions->cellWidget( i, 2 ) );
67 QCheckBox *chkWholeWord = qobject_cast<QCheckBox *>( mTableSubstitutions->cellWidget( i, 3 ) );
68
69 const QgsStringReplacement replacement( mTableSubstitutions->item( i, 0 )->text(),
70 mTableSubstitutions->item( i, 1 )->text(),
71 chkCaseSensitive->isChecked(),
72 chkWholeWord->isChecked() );
73 result << replacement;
74 }
75 return QgsStringReplacementCollection( result );
76}
77
78void QgsSubstitutionListWidget::mButtonAdd_clicked()
79{
80 addSubstitution( QgsStringReplacement( QString(), QString(), false, true ) );
81 mTableSubstitutions->setFocus();
82 mTableSubstitutions->setCurrentCell( mTableSubstitutions->rowCount() - 1, 0 );
83}
84
85void QgsSubstitutionListWidget::mButtonRemove_clicked()
86{
87 const int currentRow = mTableSubstitutions->currentRow();
88 mTableSubstitutions->removeRow( currentRow );
89 tableChanged();
90}
91
92void QgsSubstitutionListWidget::tableChanged()
93{
95}
96
97void QgsSubstitutionListWidget::mButtonExport_clicked()
98{
99 QString fileName = QFileDialog::getSaveFileName( this, tr( "Save Substitutions" ), QDir::homePath(),
100 tr( "XML files (*.xml *.XML)" ) );
101 // return dialog focus on Mac
102 activateWindow();
103 raise();
104 if ( fileName.isEmpty() )
105 {
106 return;
107 }
108
109 // ensure the user never omitted the extension from the file name
110 if ( !fileName.endsWith( QLatin1String( ".xml" ), Qt::CaseInsensitive ) )
111 {
112 fileName += QLatin1String( ".xml" );
113 }
114
115 QDomDocument doc;
116 QDomElement root = doc.createElement( QStringLiteral( "substitutions" ) );
117 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
119 collection.writeXml( root, doc );
120 doc.appendChild( root );
121
122 QFile file( fileName );
123 if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
124 {
125 QMessageBox::warning( nullptr, tr( "Export Substitutions" ),
126 tr( "Cannot write file %1:\n%2" ).arg( fileName, file.errorString() ),
127 QMessageBox::Ok,
128 QMessageBox::Ok );
129 return;
130 }
131
132 QTextStream out( &file );
133 doc.save( out, 4 );
134}
135
136void QgsSubstitutionListWidget::mButtonImport_clicked()
137{
138 const QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Substitutions" ), QDir::homePath(),
139 tr( "XML files (*.xml *.XML)" ) );
140 if ( fileName.isEmpty() )
141 {
142 return;
143 }
144
145 QFile file( fileName );
146 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
147 {
148 QMessageBox::warning( nullptr, tr( "Import Substitutions" ),
149 tr( "Cannot read file %1:\n%2" ).arg( fileName, file.errorString() ),
150 QMessageBox::Ok,
151 QMessageBox::Ok );
152 return;
153 }
154
155 QDomDocument doc;
156 QString errorStr;
157 int errorLine;
158 int errorColumn;
159
160 if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
161 {
162 QMessageBox::warning( nullptr, tr( "Import substitutions" ),
163 tr( "Parse error at line %1, column %2:\n%3" )
164 .arg( errorLine )
165 .arg( errorColumn )
166 .arg( errorStr ),
167 QMessageBox::Ok,
168 QMessageBox::Ok );
169 return;
170 }
171
172 const QDomElement root = doc.documentElement();
173 if ( root.tagName() != QLatin1String( "substitutions" ) )
174 {
175 QMessageBox::warning( nullptr, tr( "Import Substitutions" ),
176 tr( "The selected file is not a substitution list." ),
177 QMessageBox::Ok,
178 QMessageBox::Ok );
179 return;
180 }
181
183 collection.readXml( root );
184 setSubstitutions( collection );
185 tableChanged();
186}
187
188void QgsSubstitutionListWidget::addSubstitution( const QgsStringReplacement &substitution )
189{
190 const int row = mTableSubstitutions->rowCount();
191 mTableSubstitutions->insertRow( row );
192
193 const Qt::ItemFlags itemFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
194 | Qt::ItemIsEditable;
195
196 QTableWidgetItem *matchItem = new QTableWidgetItem( substitution.match() );
197 matchItem->setFlags( itemFlags );
198 mTableSubstitutions->setItem( row, 0, matchItem );
199 QTableWidgetItem *replaceItem = new QTableWidgetItem( substitution.replacement() );
200 replaceItem->setFlags( itemFlags );
201 mTableSubstitutions->setItem( row, 1, replaceItem );
202
203 QCheckBox *caseSensitiveChk = new QCheckBox( this );
204 caseSensitiveChk->setChecked( substitution.caseSensitive() );
205 mTableSubstitutions->setCellWidget( row, 2, caseSensitiveChk );
206 connect( caseSensitiveChk, &QAbstractButton::toggled, this, &QgsSubstitutionListWidget::tableChanged );
207
208 QCheckBox *wholeWordChk = new QCheckBox( this );
209 wholeWordChk->setChecked( substitution.wholeWordOnly() );
210 mTableSubstitutions->setCellWidget( row, 3, wholeWordChk );
211 connect( wholeWordChk, &QAbstractButton::toggled, this, &QgsSubstitutionListWidget::tableChanged );
212}
213
214
215//
216// QgsSubstitutionListDialog
217//
218
219
221 : QDialog( parent )
222
223{
224 setWindowTitle( tr( "Substitutions" ) );
225 QVBoxLayout *vLayout = new QVBoxLayout();
226 mWidget = new QgsSubstitutionListWidget();
227 vLayout->addWidget( mWidget );
228 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
229 connect( bbox, &QDialogButtonBox::accepted, this, &QDialog::accept );
230 connect( bbox, &QDialogButtonBox::rejected, this, &QDialog::reject );
231 vLayout->addWidget( bbox );
232 setLayout( vLayout );
233}
234
239
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:209
Base class for any widget that can be shown as a inline panel.
A collection of string replacements (specified using QgsStringReplacement objects).
void readXml(const QDomElement &elem)
Reads the collection state from an XML element.
void writeXml(QDomElement &elem, QDomDocument &doc) const
Writes the collection state to an XML element.
QList< QgsStringReplacement > replacements() const
Returns the list of string replacements in this collection.
A representation of a single string replacement.
bool wholeWordOnly() const
Returns true if match only applies to whole words, or false if partial word matches are permitted.
QString replacement() const
Returns the string to replace matches with.
bool caseSensitive() const
Returns true if match is case sensitive.
QString match() const
Returns the string matched by this object.
QgsSubstitutionListDialog(QWidget *parent=nullptr)
Constructor for QgsSubstitutionListDialog.
QgsStringReplacementCollection substitutions
void setSubstitutions(const QgsStringReplacementCollection &substitutions)
Sets the list of substitutions to show in the dialog.
A widget which allows users to specify a list of substitutions to apply to a string,...
QgsSubstitutionListWidget(QWidget *parent=nullptr)
Constructor for QgsSubstitutionListWidget.
void setSubstitutions(const QgsStringReplacementCollection &substitutions)
Sets the list of substitutions to show in the widget.
void substitutionsChanged(const QgsStringReplacementCollection &substitutions)
Emitted when the substitution definitions change.
QgsStringReplacementCollection substitutions