21 #include <QDialogButtonBox> 23 #include <QFileDialog> 24 #include <QMessageBox> 25 #include <QTextStream> 31 connect( mButtonAdd, &QToolButton::clicked,
this, &QgsSubstitutionListWidget::mButtonAdd_clicked );
32 connect( mButtonRemove, &QToolButton::clicked,
this, &QgsSubstitutionListWidget::mButtonRemove_clicked );
33 connect( mButtonExport, &QToolButton::clicked,
this, &QgsSubstitutionListWidget::mButtonExport_clicked );
34 connect( mButtonImport, &QToolButton::clicked,
this, &QgsSubstitutionListWidget::mButtonImport_clicked );
35 connect( mTableSubstitutions, &QTableWidget::cellChanged,
this, &QgsSubstitutionListWidget::tableChanged );
40 mTableSubstitutions->blockSignals(
true );
41 mTableSubstitutions->clearContents();
44 addSubstitution( replacement );
46 mTableSubstitutions->blockSignals(
false );
51 QList< QgsStringReplacement > result;
52 for (
int i = 0; i < mTableSubstitutions->rowCount(); ++i )
54 if ( !mTableSubstitutions->item( i, 0 ) )
57 if ( mTableSubstitutions->item( i, 0 )->text().isEmpty() )
60 QCheckBox *chkCaseSensitive = qobject_cast<QCheckBox *>( mTableSubstitutions->cellWidget( i, 2 ) );
61 QCheckBox *chkWholeWord = qobject_cast<QCheckBox *>( mTableSubstitutions->cellWidget( i, 3 ) );
64 mTableSubstitutions->item( i, 1 )->text(),
65 chkCaseSensitive->isChecked(),
66 chkWholeWord->isChecked() );
67 result << replacement;
72 void QgsSubstitutionListWidget::mButtonAdd_clicked()
75 mTableSubstitutions->setFocus();
76 mTableSubstitutions->setCurrentCell( mTableSubstitutions->rowCount() - 1, 0 );
79 void QgsSubstitutionListWidget::mButtonRemove_clicked()
81 int currentRow = mTableSubstitutions->currentRow();
82 mTableSubstitutions->removeRow( currentRow );
86 void QgsSubstitutionListWidget::tableChanged()
91 void QgsSubstitutionListWidget::mButtonExport_clicked()
93 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save Substitutions" ), QDir::homePath(),
94 tr(
"XML files (*.xml *.XML)" ) );
95 if ( fileName.isEmpty() )
101 if ( !fileName.endsWith( QLatin1String(
".xml" ), Qt::CaseInsensitive ) )
103 fileName += QLatin1String(
".xml" );
107 QDomElement root = doc.createElement( QStringLiteral(
"substitutions" ) );
108 root.setAttribute( QStringLiteral(
"version" ), QStringLiteral(
"1.0" ) );
111 doc.appendChild( root );
113 QFile file( fileName );
114 if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
116 QMessageBox::warning(
nullptr, tr(
"Export Substitutions" ),
117 tr(
"Cannot write file %1:\n%2" ).arg( fileName, file.errorString() ),
123 QTextStream out( &file );
127 void QgsSubstitutionListWidget::mButtonImport_clicked()
129 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Substitutions" ), QDir::homePath(),
130 tr(
"XML files (*.xml *.XML)" ) );
131 if ( fileName.isEmpty() )
136 QFile file( fileName );
137 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
139 QMessageBox::warning(
nullptr, tr(
"Import Substitutions" ),
140 tr(
"Cannot read file %1:\n%2" ).arg( fileName, file.errorString() ),
151 if ( !doc.setContent( &file,
true, &errorStr, &errorLine, &errorColumn ) )
153 QMessageBox::warning(
nullptr, tr(
"Import substitutions" ),
154 tr(
"Parse error at line %1, column %2:\n%3" )
163 QDomElement root = doc.documentElement();
164 if ( root.tagName() != QLatin1String(
"substitutions" ) )
166 QMessageBox::warning(
nullptr, tr(
"Import Substitutions" ),
167 tr(
"The selected file is not a substitution list." ),
181 int row = mTableSubstitutions->rowCount();
182 mTableSubstitutions->insertRow( row );
184 Qt::ItemFlags itemFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
185 | Qt::ItemIsEditable;
187 QTableWidgetItem *matchItem =
new QTableWidgetItem( substitution.
match() );
188 matchItem->setFlags( itemFlags );
189 mTableSubstitutions->setItem( row, 0, matchItem );
190 QTableWidgetItem *replaceItem =
new QTableWidgetItem( substitution.
replacement() );
191 replaceItem->setFlags( itemFlags );
192 mTableSubstitutions->setItem( row, 1, replaceItem );
194 QCheckBox *caseSensitiveChk =
new QCheckBox(
this );
195 caseSensitiveChk->setChecked( substitution.
caseSensitive() );
196 mTableSubstitutions->setCellWidget( row, 2, caseSensitiveChk );
197 connect( caseSensitiveChk, &QAbstractButton::toggled,
this, &QgsSubstitutionListWidget::tableChanged );
199 QCheckBox *wholeWordChk =
new QCheckBox(
this );
201 mTableSubstitutions->setCellWidget( row, 3, wholeWordChk );
202 connect( wholeWordChk, &QAbstractButton::toggled,
this, &QgsSubstitutionListWidget::tableChanged );
215 setWindowTitle( tr(
"Substitutions" ) );
216 QVBoxLayout *vLayout =
new QVBoxLayout();
218 vLayout->addWidget( mWidget );
219 QDialogButtonBox *bbox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
220 connect( bbox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
221 connect( bbox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
222 vLayout->addWidget( bbox );
223 setLayout( vLayout );
A representation of a single string replacement.
bool caseSensitive() const
Returns true if match is case sensitive.
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.
QString match() const
Returns the string matched by this object.
QString replacement() const
Returns the string to replace matches with.
QgsSubstitutionListDialog(QWidget *parent=nullptr)
Constructor for QgsSubstitutionListDialog.
bool wholeWordOnly() const
Returns true if match only applies to whole words, or false if partial word matches are permitted...
A collection of string replacements (specified using QgsStringReplacement objects).
void setSubstitutions(const QgsStringReplacementCollection &substitutions)
Sets the list of substitutions to show in the dialog.
void readXml(const QDomElement &elem)
Reads the collection state from an XML element.
QgsStringReplacementCollection substitutions() const
Returns the list of substitutions currently defined by the dialog.