23 #include <QDialogButtonBox>
25 #include <QFileDialog>
26 #include <QMessageBox>
27 #include <QTextStream>
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;
77 void QgsSubstitutionListWidget::mButtonAdd_clicked()
80 mTableSubstitutions->setFocus();
81 mTableSubstitutions->setCurrentCell( mTableSubstitutions->rowCount() - 1, 0 );
84 void QgsSubstitutionListWidget::mButtonRemove_clicked()
86 const int currentRow = mTableSubstitutions->currentRow();
87 mTableSubstitutions->removeRow( currentRow );
91 void QgsSubstitutionListWidget::tableChanged()
96 void QgsSubstitutionListWidget::mButtonExport_clicked()
98 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save Substitutions" ), QDir::homePath(),
99 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" ),
122 tr(
"Cannot write file %1:\n%2" ).arg( fileName, file.errorString() ),
128 QTextStream out( &file );
132 void QgsSubstitutionListWidget::mButtonImport_clicked()
134 const QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Substitutions" ), QDir::homePath(),
135 tr(
"XML files (*.xml *.XML)" ) );
136 if ( fileName.isEmpty() )
141 QFile file( fileName );
142 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
144 QMessageBox::warning(
nullptr, tr(
"Import Substitutions" ),
145 tr(
"Cannot read file %1:\n%2" ).arg( fileName, file.errorString() ),
156 if ( !doc.setContent( &file,
true, &errorStr, &errorLine, &errorColumn ) )
158 QMessageBox::warning(
nullptr, tr(
"Import substitutions" ),
159 tr(
"Parse error at line %1, column %2:\n%3" )
168 const QDomElement root = doc.documentElement();
169 if ( root.tagName() != QLatin1String(
"substitutions" ) )
171 QMessageBox::warning(
nullptr, tr(
"Import Substitutions" ),
172 tr(
"The selected file is not a substitution list." ),
186 const int row = mTableSubstitutions->rowCount();
187 mTableSubstitutions->insertRow( row );
189 const Qt::ItemFlags itemFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
190 | Qt::ItemIsEditable;
192 QTableWidgetItem *matchItem =
new QTableWidgetItem( substitution.
match() );
193 matchItem->setFlags( itemFlags );
194 mTableSubstitutions->setItem( row, 0, matchItem );
195 QTableWidgetItem *replaceItem =
new QTableWidgetItem( substitution.
replacement() );
196 replaceItem->setFlags( itemFlags );
197 mTableSubstitutions->setItem( row, 1, replaceItem );
199 QCheckBox *caseSensitiveChk =
new QCheckBox(
this );
200 caseSensitiveChk->setChecked( substitution.
caseSensitive() );
201 mTableSubstitutions->setCellWidget( row, 2, caseSensitiveChk );
202 connect( caseSensitiveChk, &QAbstractButton::toggled,
this, &QgsSubstitutionListWidget::tableChanged );
204 QCheckBox *wholeWordChk =
new QCheckBox(
this );
206 mTableSubstitutions->setCellWidget( row, 3, wholeWordChk );
207 connect( wholeWordChk, &QAbstractButton::toggled,
this, &QgsSubstitutionListWidget::tableChanged );
220 setWindowTitle( tr(
"Substitutions" ) );
221 QVBoxLayout *vLayout =
new QVBoxLayout();
223 vLayout->addWidget( mWidget );
224 QDialogButtonBox *bbox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
225 connect( bbox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
226 connect( bbox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
227 vLayout->addWidget( bbox );
228 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.