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 ) );
70 mTableSubstitutions->item( i, 1 )->text(),
71 chkCaseSensitive->isChecked(),
72 chkWholeWord->isChecked() );
73 result << replacement;
78void QgsSubstitutionListWidget::mButtonAdd_clicked()
81 mTableSubstitutions->setFocus();
82 mTableSubstitutions->setCurrentCell( mTableSubstitutions->rowCount() - 1, 0 );
85void QgsSubstitutionListWidget::mButtonRemove_clicked()
87 const int currentRow = mTableSubstitutions->currentRow();
88 mTableSubstitutions->removeRow( currentRow );
92void QgsSubstitutionListWidget::tableChanged()
97void QgsSubstitutionListWidget::mButtonExport_clicked()
99 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save Substitutions" ), QDir::homePath(),
100 tr(
"XML files (*.xml *.XML)" ) );
104 if ( fileName.isEmpty() )
110 if ( !fileName.endsWith( QLatin1String(
".xml" ), Qt::CaseInsensitive ) )
112 fileName += QLatin1String(
".xml" );
116 QDomElement root = doc.createElement( QStringLiteral(
"substitutions" ) );
117 root.setAttribute( QStringLiteral(
"version" ), QStringLiteral(
"1.0" ) );
120 doc.appendChild( root );
122 QFile file( fileName );
123 if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
125 QMessageBox::warning(
nullptr, tr(
"Export Substitutions" ),
126 tr(
"Cannot write file %1:\n%2" ).arg( fileName, file.errorString() ),
132 QTextStream out( &file );
136void QgsSubstitutionListWidget::mButtonImport_clicked()
138 const QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Substitutions" ), QDir::homePath(),
139 tr(
"XML files (*.xml *.XML)" ) );
140 if ( fileName.isEmpty() )
145 QFile file( fileName );
146 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
148 QMessageBox::warning(
nullptr, tr(
"Import Substitutions" ),
149 tr(
"Cannot read file %1:\n%2" ).arg( fileName, file.errorString() ),
160 if ( !doc.setContent( &file,
true, &errorStr, &errorLine, &errorColumn ) )
162 QMessageBox::warning(
nullptr, tr(
"Import substitutions" ),
163 tr(
"Parse error at line %1, column %2:\n%3" )
172 const QDomElement root = doc.documentElement();
173 if ( root.tagName() != QLatin1String(
"substitutions" ) )
175 QMessageBox::warning(
nullptr, tr(
"Import Substitutions" ),
176 tr(
"The selected file is not a substitution list." ),
190 const int row = mTableSubstitutions->rowCount();
191 mTableSubstitutions->insertRow( row );
193 const Qt::ItemFlags itemFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
194 | Qt::ItemIsEditable;
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 );
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 );
208 QCheckBox *wholeWordChk =
new QCheckBox(
this );
210 mTableSubstitutions->setCellWidget( row, 3, wholeWordChk );
211 connect( wholeWordChk, &QAbstractButton::toggled,
this, &QgsSubstitutionListWidget::tableChanged );
224 setWindowTitle( tr(
"Substitutions" ) );
225 QVBoxLayout *vLayout =
new QVBoxLayout();
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 );
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.