QGIS API Documentation 3.41.0-Master (cea29feecf2)
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(), mTableSubstitutions->item( i, 1 )->text(), chkCaseSensitive->isChecked(), chkWholeWord->isChecked() );
70 result << replacement;
71 }
72 return QgsStringReplacementCollection( result );
73}
74
75void QgsSubstitutionListWidget::mButtonAdd_clicked()
76{
77 addSubstitution( QgsStringReplacement( QString(), QString(), false, true ) );
78 mTableSubstitutions->setFocus();
79 mTableSubstitutions->setCurrentCell( mTableSubstitutions->rowCount() - 1, 0 );
80}
81
82void QgsSubstitutionListWidget::mButtonRemove_clicked()
83{
84 const int currentRow = mTableSubstitutions->currentRow();
85 mTableSubstitutions->removeRow( currentRow );
86 tableChanged();
87}
88
89void QgsSubstitutionListWidget::tableChanged()
90{
92}
93
94void QgsSubstitutionListWidget::mButtonExport_clicked()
95{
96 QString fileName = QFileDialog::getSaveFileName( this, tr( "Save Substitutions" ), QDir::homePath(), tr( "XML files (*.xml *.XML)" ) );
97 // return dialog focus on Mac
98 activateWindow();
99 raise();
100 if ( fileName.isEmpty() )
101 {
102 return;
103 }
104
105 // ensure the user never omitted the extension from the file name
106 if ( !fileName.endsWith( QLatin1String( ".xml" ), Qt::CaseInsensitive ) )
107 {
108 fileName += QLatin1String( ".xml" );
109 }
110
111 QDomDocument doc;
112 QDomElement root = doc.createElement( QStringLiteral( "substitutions" ) );
113 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
115 collection.writeXml( root, doc );
116 doc.appendChild( root );
117
118 QFile file( fileName );
119 if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
120 {
121 QMessageBox::warning( nullptr, tr( "Export Substitutions" ), tr( "Cannot write file %1:\n%2" ).arg( fileName, file.errorString() ), QMessageBox::Ok, QMessageBox::Ok );
122 return;
123 }
124
125 QTextStream out( &file );
126 doc.save( out, 4 );
127}
128
129void QgsSubstitutionListWidget::mButtonImport_clicked()
130{
131 const QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Substitutions" ), QDir::homePath(), tr( "XML files (*.xml *.XML)" ) );
132 if ( fileName.isEmpty() )
133 {
134 return;
135 }
136
137 QFile file( fileName );
138 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
139 {
140 QMessageBox::warning( nullptr, tr( "Import Substitutions" ), tr( "Cannot read file %1:\n%2" ).arg( fileName, file.errorString() ), QMessageBox::Ok, QMessageBox::Ok );
141 return;
142 }
143
144 QDomDocument doc;
145 QString errorStr;
146 int errorLine;
147 int errorColumn;
148
149 if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
150 {
151 QMessageBox::warning( nullptr, tr( "Import substitutions" ), tr( "Parse error at line %1, column %2:\n%3" ).arg( errorLine ).arg( errorColumn ).arg( errorStr ), QMessageBox::Ok, QMessageBox::Ok );
152 return;
153 }
154
155 const QDomElement root = doc.documentElement();
156 if ( root.tagName() != QLatin1String( "substitutions" ) )
157 {
158 QMessageBox::warning( nullptr, tr( "Import Substitutions" ), tr( "The selected file is not a substitution list." ), QMessageBox::Ok, QMessageBox::Ok );
159 return;
160 }
161
163 collection.readXml( root );
164 setSubstitutions( collection );
165 tableChanged();
166}
167
168void QgsSubstitutionListWidget::addSubstitution( const QgsStringReplacement &substitution )
169{
170 const int row = mTableSubstitutions->rowCount();
171 mTableSubstitutions->insertRow( row );
172
173 const Qt::ItemFlags itemFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
174 | Qt::ItemIsEditable;
175
176 QTableWidgetItem *matchItem = new QTableWidgetItem( substitution.match() );
177 matchItem->setFlags( itemFlags );
178 mTableSubstitutions->setItem( row, 0, matchItem );
179 QTableWidgetItem *replaceItem = new QTableWidgetItem( substitution.replacement() );
180 replaceItem->setFlags( itemFlags );
181 mTableSubstitutions->setItem( row, 1, replaceItem );
182
183 QCheckBox *caseSensitiveChk = new QCheckBox( this );
184 caseSensitiveChk->setChecked( substitution.caseSensitive() );
185 mTableSubstitutions->setCellWidget( row, 2, caseSensitiveChk );
186 connect( caseSensitiveChk, &QAbstractButton::toggled, this, &QgsSubstitutionListWidget::tableChanged );
187
188 QCheckBox *wholeWordChk = new QCheckBox( this );
189 wholeWordChk->setChecked( substitution.wholeWordOnly() );
190 mTableSubstitutions->setCellWidget( row, 3, wholeWordChk );
191 connect( wholeWordChk, &QAbstractButton::toggled, this, &QgsSubstitutionListWidget::tableChanged );
192}
193
194
195//
196// QgsSubstitutionListDialog
197//
198
199
201 : QDialog( parent )
202
203{
204 setWindowTitle( tr( "Substitutions" ) );
205 QVBoxLayout *vLayout = new QVBoxLayout();
206 mWidget = new QgsSubstitutionListWidget();
207 vLayout->addWidget( mWidget );
208 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
209 connect( bbox, &QDialogButtonBox::accepted, this, &QDialog::accept );
210 connect( bbox, &QDialogButtonBox::rejected, this, &QDialog::reject );
211 vLayout->addWidget( bbox );
212 setLayout( vLayout );
213}
214
219
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:210
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