QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
22#include "qgsgui.h"
23
24#include <QCheckBox>
25#include <QDialogButtonBox>
26#include <QFileDialog>
27#include <QMessageBox>
28#include <QTextStream>
29
30#include "moc_qgssubstitutionlistwidget.cpp"
31
33 : QgsPanelWidget( parent )
34{
35 setupUi( this );
37
38 connect( mButtonAdd, &QToolButton::clicked, this, &QgsSubstitutionListWidget::mButtonAdd_clicked );
39 connect( mButtonRemove, &QToolButton::clicked, this, &QgsSubstitutionListWidget::mButtonRemove_clicked );
40 connect( mButtonExport, &QToolButton::clicked, this, &QgsSubstitutionListWidget::mButtonExport_clicked );
41 connect( mButtonImport, &QToolButton::clicked, this, &QgsSubstitutionListWidget::mButtonImport_clicked );
42 connect( mTableSubstitutions, &QTableWidget::cellChanged, this, &QgsSubstitutionListWidget::tableChanged );
43}
44
46{
47 mTableSubstitutions->blockSignals( true );
48 mTableSubstitutions->clearContents();
49 const auto constReplacements = substitutions.replacements();
50 for ( const QgsStringReplacement &replacement : constReplacements )
51 {
52 addSubstitution( replacement );
53 }
54 mTableSubstitutions->blockSignals( false );
55}
56
58{
59 QList<QgsStringReplacement> result;
60 for ( int i = 0; i < mTableSubstitutions->rowCount(); ++i )
61 {
62 if ( !mTableSubstitutions->item( i, 0 ) )
63 continue;
64
65 if ( mTableSubstitutions->item( i, 0 )->text().isEmpty() )
66 continue;
67
68 QCheckBox *chkCaseSensitive = qobject_cast<QCheckBox *>( mTableSubstitutions->cellWidget( i, 2 ) );
69 QCheckBox *chkWholeWord = qobject_cast<QCheckBox *>( mTableSubstitutions->cellWidget( i, 3 ) );
70
71 const QgsStringReplacement replacement( mTableSubstitutions->item( i, 0 )->text(), mTableSubstitutions->item( i, 1 )->text(), chkCaseSensitive->isChecked(), chkWholeWord->isChecked() );
72 result << replacement;
73 }
74 return QgsStringReplacementCollection( result );
75}
76
77void QgsSubstitutionListWidget::mButtonAdd_clicked()
78{
79 addSubstitution( QgsStringReplacement( QString(), QString(), false, true ) );
80 mTableSubstitutions->setFocus();
81 mTableSubstitutions->setCurrentCell( mTableSubstitutions->rowCount() - 1, 0 );
82}
83
84void QgsSubstitutionListWidget::mButtonRemove_clicked()
85{
86 const int currentRow = mTableSubstitutions->currentRow();
87 mTableSubstitutions->removeRow( currentRow );
88 tableChanged();
89}
90
91void QgsSubstitutionListWidget::tableChanged()
92{
94}
95
96void QgsSubstitutionListWidget::mButtonExport_clicked()
97{
98 QString fileName = QFileDialog::getSaveFileName( this, tr( "Save Substitutions" ), QDir::homePath(), tr( "XML files (*.xml *.XML)" ) );
99 // return dialog focus on Mac
100 activateWindow();
101 raise();
102 if ( fileName.isEmpty() )
103 {
104 return;
105 }
106
107 // ensure the user never omitted the extension from the file name
108 if ( !fileName.endsWith( QLatin1String( ".xml" ), Qt::CaseInsensitive ) )
109 {
110 fileName += QLatin1String( ".xml" );
111 }
112
113 QDomDocument doc;
114 QDomElement root = doc.createElement( QStringLiteral( "substitutions" ) );
115 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
116 const QgsStringReplacementCollection collection = substitutions();
117 collection.writeXml( root, doc );
118 doc.appendChild( root );
119
120 QFile file( fileName );
121 if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
122 {
123 QMessageBox::warning( nullptr, tr( "Export Substitutions" ), tr( "Cannot write file %1:\n%2" ).arg( fileName, file.errorString() ), QMessageBox::Ok, QMessageBox::Ok );
124 return;
125 }
126
127 QTextStream out( &file );
128 doc.save( out, 4 );
129}
130
131void QgsSubstitutionListWidget::mButtonImport_clicked()
132{
133 const QString fileName = QFileDialog::getOpenFileName( this, tr( "Load Substitutions" ), QDir::homePath(), tr( "XML files (*.xml *.XML)" ) );
134 if ( fileName.isEmpty() )
135 {
136 return;
137 }
138
139 QFile file( fileName );
140 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
141 {
142 QMessageBox::warning( nullptr, tr( "Import Substitutions" ), tr( "Cannot read file %1:\n%2" ).arg( fileName, file.errorString() ), QMessageBox::Ok, QMessageBox::Ok );
143 return;
144 }
145
146 QDomDocument doc;
147 QString errorStr;
148 int errorLine;
149 int errorColumn;
150
151 if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
152 {
153 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 );
154 return;
155 }
156
157 const QDomElement root = doc.documentElement();
158 if ( root.tagName() != QLatin1String( "substitutions" ) )
159 {
160 QMessageBox::warning( nullptr, tr( "Import Substitutions" ), tr( "The selected file is not a substitution list." ), QMessageBox::Ok, QMessageBox::Ok );
161 return;
162 }
163
164 QgsStringReplacementCollection collection;
165 collection.readXml( root );
166 setSubstitutions( collection );
167 tableChanged();
168}
169
170void QgsSubstitutionListWidget::addSubstitution( const QgsStringReplacement &substitution )
171{
172 const int row = mTableSubstitutions->rowCount();
173 mTableSubstitutions->insertRow( row );
174
175 const Qt::ItemFlags itemFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
176 | Qt::ItemIsEditable;
177
178 QTableWidgetItem *matchItem = new QTableWidgetItem( substitution.match() );
179 matchItem->setFlags( itemFlags );
180 mTableSubstitutions->setItem( row, 0, matchItem );
181 QTableWidgetItem *replaceItem = new QTableWidgetItem( substitution.replacement() );
182 replaceItem->setFlags( itemFlags );
183 mTableSubstitutions->setItem( row, 1, replaceItem );
184
185 QCheckBox *caseSensitiveChk = new QCheckBox( this );
186 caseSensitiveChk->setChecked( substitution.caseSensitive() );
187 mTableSubstitutions->setCellWidget( row, 2, caseSensitiveChk );
188 connect( caseSensitiveChk, &QAbstractButton::toggled, this, &QgsSubstitutionListWidget::tableChanged );
189
190 QCheckBox *wholeWordChk = new QCheckBox( this );
191 wholeWordChk->setChecked( substitution.wholeWordOnly() );
192 mTableSubstitutions->setCellWidget( row, 3, wholeWordChk );
193 connect( wholeWordChk, &QAbstractButton::toggled, this, &QgsSubstitutionListWidget::tableChanged );
194}
195
196
197//
198// QgsSubstitutionListDialog
199//
200
201
203 : QDialog( parent )
204
205{
206 setWindowTitle( tr( "Substitutions" ) );
207 QVBoxLayout *vLayout = new QVBoxLayout();
208 mWidget = new QgsSubstitutionListWidget();
209 vLayout->addWidget( mWidget );
210 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
211 connect( bbox, &QDialogButtonBox::accepted, this, &QDialog::accept );
212 connect( bbox, &QDialogButtonBox::rejected, this, &QDialog::reject );
213 vLayout->addWidget( bbox );
214 setLayout( vLayout );
215}
216
221
223{
224 return mWidget->substitutions();
225}
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:221
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an 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.
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