23#include <QDialogButtonBox>
35 connect( mButtonAdd, &QToolButton::clicked,
this, &QgsSubstitutionListWidget::mButtonAdd_clicked );
36 connect( mButtonRemove, &QToolButton::clicked,
this, &QgsSubstitutionListWidget::mButtonRemove_clicked );
37 connect( mButtonExport, &QToolButton::clicked,
this, &QgsSubstitutionListWidget::mButtonExport_clicked );
38 connect( mButtonImport, &QToolButton::clicked,
this, &QgsSubstitutionListWidget::mButtonImport_clicked );
39 connect( mTableSubstitutions, &QTableWidget::cellChanged,
this, &QgsSubstitutionListWidget::tableChanged );
44 mTableSubstitutions->blockSignals(
true );
45 mTableSubstitutions->clearContents();
49 addSubstitution( replacement );
51 mTableSubstitutions->blockSignals(
false );
56 QList< QgsStringReplacement > result;
57 for (
int i = 0; i < mTableSubstitutions->rowCount(); ++i )
59 if ( !mTableSubstitutions->item( i, 0 ) )
62 if ( mTableSubstitutions->item( i, 0 )->text().isEmpty() )
65 QCheckBox *chkCaseSensitive = qobject_cast<QCheckBox *>( mTableSubstitutions->cellWidget( i, 2 ) );
66 QCheckBox *chkWholeWord = qobject_cast<QCheckBox *>( mTableSubstitutions->cellWidget( i, 3 ) );
69 mTableSubstitutions->item( i, 1 )->text(),
70 chkCaseSensitive->isChecked(),
71 chkWholeWord->isChecked() );
72 result << replacement;
77void QgsSubstitutionListWidget::mButtonAdd_clicked()
80 mTableSubstitutions->setFocus();
81 mTableSubstitutions->setCurrentCell( mTableSubstitutions->rowCount() - 1, 0 );
84void QgsSubstitutionListWidget::mButtonRemove_clicked()
86 const int currentRow = mTableSubstitutions->currentRow();
87 mTableSubstitutions->removeRow( currentRow );
91void QgsSubstitutionListWidget::tableChanged()
96void QgsSubstitutionListWidget::mButtonExport_clicked()
98 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save Substitutions" ), QDir::homePath(),
99 tr(
"XML files (*.xml *.XML)" ) );
103 if ( fileName.isEmpty() )
109 if ( !fileName.endsWith( QLatin1String(
".xml" ), Qt::CaseInsensitive ) )
111 fileName += QLatin1String(
".xml" );
115 QDomElement root = doc.createElement( QStringLiteral(
"substitutions" ) );
116 root.setAttribute( QStringLiteral(
"version" ), QStringLiteral(
"1.0" ) );
119 doc.appendChild( root );
121 QFile file( fileName );
122 if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
124 QMessageBox::warning(
nullptr, tr(
"Export Substitutions" ),
125 tr(
"Cannot write file %1:\n%2" ).arg( fileName, file.errorString() ),
131 QTextStream out( &file );
135void QgsSubstitutionListWidget::mButtonImport_clicked()
137 const QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Substitutions" ), QDir::homePath(),
138 tr(
"XML files (*.xml *.XML)" ) );
139 if ( fileName.isEmpty() )
144 QFile file( fileName );
145 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
147 QMessageBox::warning(
nullptr, tr(
"Import Substitutions" ),
148 tr(
"Cannot read file %1:\n%2" ).arg( fileName, file.errorString() ),
159 if ( !doc.setContent( &file,
true, &errorStr, &errorLine, &errorColumn ) )
161 QMessageBox::warning(
nullptr, tr(
"Import substitutions" ),
162 tr(
"Parse error at line %1, column %2:\n%3" )
171 const QDomElement root = doc.documentElement();
172 if ( root.tagName() != QLatin1String(
"substitutions" ) )
174 QMessageBox::warning(
nullptr, tr(
"Import Substitutions" ),
175 tr(
"The selected file is not a substitution list." ),
189 const int row = mTableSubstitutions->rowCount();
190 mTableSubstitutions->insertRow( row );
192 const Qt::ItemFlags itemFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
193 | Qt::ItemIsEditable;
195 QTableWidgetItem *matchItem =
new QTableWidgetItem( substitution.
match() );
196 matchItem->setFlags( itemFlags );
197 mTableSubstitutions->setItem( row, 0, matchItem );
198 QTableWidgetItem *replaceItem =
new QTableWidgetItem( substitution.
replacement() );
199 replaceItem->setFlags( itemFlags );
200 mTableSubstitutions->setItem( row, 1, replaceItem );
202 QCheckBox *caseSensitiveChk =
new QCheckBox(
this );
203 caseSensitiveChk->setChecked( substitution.
caseSensitive() );
204 mTableSubstitutions->setCellWidget( row, 2, caseSensitiveChk );
205 connect( caseSensitiveChk, &QAbstractButton::toggled,
this, &QgsSubstitutionListWidget::tableChanged );
207 QCheckBox *wholeWordChk =
new QCheckBox(
this );
209 mTableSubstitutions->setCellWidget( row, 3, wholeWordChk );
210 connect( wholeWordChk, &QAbstractButton::toggled,
this, &QgsSubstitutionListWidget::tableChanged );
223 setWindowTitle( tr(
"Substitutions" ) );
224 QVBoxLayout *vLayout =
new QVBoxLayout();
226 vLayout->addWidget( mWidget );
227 QDialogButtonBox *bbox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
228 connect( bbox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
229 connect( bbox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
230 vLayout->addWidget( bbox );
231 setLayout( vLayout );
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...
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.