21#include "moc_qgssubstitutionlistwidget.cpp"
24#include <QDialogButtonBox>
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 );
45 mTableSubstitutions->blockSignals(
true );
46 mTableSubstitutions->clearContents();
50 addSubstitution( replacement );
52 mTableSubstitutions->blockSignals(
false );
57 QList<QgsStringReplacement> result;
58 for (
int i = 0; i < mTableSubstitutions->rowCount(); ++i )
60 if ( !mTableSubstitutions->item( i, 0 ) )
63 if ( mTableSubstitutions->item( i, 0 )->text().isEmpty() )
66 QCheckBox *chkCaseSensitive = qobject_cast<QCheckBox *>( mTableSubstitutions->cellWidget( i, 2 ) );
67 QCheckBox *chkWholeWord = qobject_cast<QCheckBox *>( mTableSubstitutions->cellWidget( i, 3 ) );
69 const QgsStringReplacement replacement( mTableSubstitutions->item( i, 0 )->text(), mTableSubstitutions->item( i, 1 )->text(), chkCaseSensitive->isChecked(), chkWholeWord->isChecked() );
70 result << replacement;
75void QgsSubstitutionListWidget::mButtonAdd_clicked()
78 mTableSubstitutions->setFocus();
79 mTableSubstitutions->setCurrentCell( mTableSubstitutions->rowCount() - 1, 0 );
82void QgsSubstitutionListWidget::mButtonRemove_clicked()
84 const int currentRow = mTableSubstitutions->currentRow();
85 mTableSubstitutions->removeRow( currentRow );
89void QgsSubstitutionListWidget::tableChanged()
94void QgsSubstitutionListWidget::mButtonExport_clicked()
96 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save Substitutions" ), QDir::homePath(), tr(
"XML files (*.xml *.XML)" ) );
100 if ( fileName.isEmpty() )
106 if ( !fileName.endsWith( QLatin1String(
".xml" ), Qt::CaseInsensitive ) )
108 fileName += QLatin1String(
".xml" );
112 QDomElement root = doc.createElement( QStringLiteral(
"substitutions" ) );
113 root.setAttribute( QStringLiteral(
"version" ), QStringLiteral(
"1.0" ) );
116 doc.appendChild( root );
118 QFile file( fileName );
119 if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
121 QMessageBox::warning(
nullptr, tr(
"Export Substitutions" ), tr(
"Cannot write file %1:\n%2" ).arg( fileName, file.errorString() ), QMessageBox::Ok, QMessageBox::Ok );
125 QTextStream out( &file );
129void QgsSubstitutionListWidget::mButtonImport_clicked()
131 const QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Substitutions" ), QDir::homePath(), tr(
"XML files (*.xml *.XML)" ) );
132 if ( fileName.isEmpty() )
137 QFile file( fileName );
138 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
140 QMessageBox::warning(
nullptr, tr(
"Import Substitutions" ), tr(
"Cannot read file %1:\n%2" ).arg( fileName, file.errorString() ), QMessageBox::Ok, QMessageBox::Ok );
149 if ( !doc.setContent( &file,
true, &errorStr, &errorLine, &errorColumn ) )
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 );
155 const QDomElement root = doc.documentElement();
156 if ( root.tagName() != QLatin1String(
"substitutions" ) )
158 QMessageBox::warning(
nullptr, tr(
"Import Substitutions" ), tr(
"The selected file is not a substitution list." ), QMessageBox::Ok, QMessageBox::Ok );
170 const int row = mTableSubstitutions->rowCount();
171 mTableSubstitutions->insertRow( row );
173 const Qt::ItemFlags itemFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
174 | Qt::ItemIsEditable;
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 );
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 );
188 QCheckBox *wholeWordChk =
new QCheckBox(
this );
190 mTableSubstitutions->setCellWidget( row, 3, wholeWordChk );
191 connect( wholeWordChk, &QAbstractButton::toggled,
this, &QgsSubstitutionListWidget::tableChanged );
204 setWindowTitle( tr(
"Substitutions" ) );
205 QVBoxLayout *vLayout =
new QVBoxLayout();
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 );
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.