Qt3 to 4
[kdeaccessibility.git] / kttsd / filters / stringreplacer / stringreplacerconf.cpp
blobfcdae8cd247ddf0e6b920dd58d3425f0540c5147
1 /***************************************************** vim:set ts=4 sw=4 sts=4:
2 Generic String Replacement Filter Configuration class.
3 -------------------
4 Copyright:
5 (C) 2005 by Gary Cramblitt <garycramblitt@comcast.net>
6 -------------------
7 Original author: Gary Cramblitt <garycramblitt@comcast.net>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 ******************************************************************************/
24 // Qt includes.
25 #include <QFile>
26 #include <QFileInfo>
27 #include <QString>
28 #include <QLayout>
29 #include <QDomDocument>
30 #include <QFile>
31 #include <QRadioButton>
32 #include <QTextStream>
33 #include <QVBoxLayout>
34 #include <QTableWidget>
36 // KDE includes.
37 #include <kglobal.h>
38 #include <klocale.h>
39 #include <klineedit.h>
40 #include <kdialog.h>
41 #include <kpushbutton.h>
42 #include <kconfig.h>
43 #include <kstandarddirs.h>
44 #include <kregexpeditorinterface.h>
45 #include <ktrader.h>
46 #include <kparts/componentfactory.h>
47 #include <kfiledialog.h>
48 #include <kmessagebox.h>
49 #include <kvbox.h>
51 // KTTS includes.
52 #include "filterconf.h"
54 // StringReplacer includes.
55 #include "stringreplacerconf.h"
56 #include "stringreplacerconf.moc"
58 /**
59 * Constructor
61 StringReplacerConf::StringReplacerConf( QWidget *parent, const QStringList& args ) :
62 KttsFilterConf(parent),
63 m_editDlg(0),
64 m_editWidget(0)
66 Q_UNUSED(args);
67 // kDebug() << "StringReplacerConf::StringReplacerConf: Running" << endl;
69 // Create configuration widget.
70 QVBoxLayout *layout = new QVBoxLayout(this);
71 layout->setAlignment (Qt::AlignTop);
72 setupUi(this);
73 layout->addWidget(this);
74 substLView->setSortingEnabled(false);
76 connect(nameLineEdit, SIGNAL(textChanged(const QString&)),
77 this, SLOT(configChanged()));
78 connect(languageBrowseButton, SIGNAL(clicked()),
79 this, SLOT(slotLanguageBrowseButton_clicked()));
80 connect(addButton, SIGNAL(clicked()),
81 this, SLOT(slotAddButton_clicked()));
82 connect(upButton, SIGNAL(clicked()),
83 this, SLOT(slotUpButton_clicked()));
84 connect(downButton, SIGNAL(clicked()),
85 this, SLOT(slotDownButton_clicked()));
86 connect(editButton, SIGNAL(clicked()),
87 this, SLOT(slotEditButton_clicked()));
88 connect(removeButton, SIGNAL(clicked()),
89 this, SLOT(slotRemoveButton_clicked()));
90 connect(loadButton, SIGNAL(clicked()),
91 this, SLOT(slotLoadButton_clicked()));
92 connect(saveButton, SIGNAL(clicked()),
93 this, SLOT(slotSaveButton_clicked()));
94 connect(clearButton, SIGNAL(clicked()),
95 this, SLOT(slotClearButton_clicked()));
96 connect(substLView, SIGNAL(selectionChanged()),
97 this, SLOT(enableDisableButtons()));
98 connect(appIdLineEdit, SIGNAL(textChanged(const QString&)),
99 this, SLOT(configChanged()));
101 // Determine if kdeutils Regular Expression Editor is installed.
102 m_reEditorInstalled = !KTrader::self()->query("KRegExpEditor/KRegExpEditor").isEmpty();
104 // Set up defaults.
105 defaults();
109 * Destructor.
111 StringReplacerConf::~StringReplacerConf(){
112 // kDebug() << "StringReplacerConf::~StringReplacerConf: Running" << endl;
116 * This method is invoked whenever the module should read its
117 * configuration (most of the times from a config file) and update the
118 * user interface. This happens when the user clicks the "Reset" button in
119 * the control center, to undo all of his changes and restore the currently
120 * valid settings. Note that kttsmgr calls this when the plugin is
121 * loaded, so it not necessary to call it in your constructor.
122 * The plugin should read its configuration from the specified group
123 * in the specified config file.
124 * @param config Pointer to a KConfig object.
125 * @param configGroup Call config->setGroup with this argument before
126 * loading your configuration.
128 void StringReplacerConf::load(KConfig* config, const QString& configGroup){
129 // kDebug() << "StringReplacerConf::load: Running" << endl;
130 // See if this filter previously save its word list.
131 config->setGroup( configGroup );
132 QString wordsFilename = config->readEntry( "WordListFile" );
133 if ( !wordsFilename.isEmpty() )
135 QString errMsg = loadFromFile( wordsFilename, true );
136 if ( !errMsg.isEmpty() )
137 kDebug() << "StringReplacerConf::load: " << errMsg << endl;
138 enableDisableButtons();
142 // Loads word list and settings from a file. Clearing configuration if clear is True.
143 QString StringReplacerConf::loadFromFile( const QString& filename, bool clear)
145 // Open existing word list.
146 QFile file( filename );
147 if ( !file.open( QIODevice::ReadOnly ) )
149 return i18n("Unable to open file.") + filename;
151 // QDomDocument doc( "http://www.kde.org/share/apps/kttsd/stringreplacer/wordlist.dtd []" );
152 QDomDocument doc( "" );
153 if ( !doc.setContent( &file ) ) {
154 file.close();
155 return i18n("File not in proper XML format.");
157 // kDebug() << "StringReplacerConf::load: document successfully parsed." << endl;
158 file.close();
160 // Clear list view.
161 if ( clear ) substLView->clear();
163 // Name setting.
164 QDomNodeList nameList = doc.elementsByTagName( "name" );
165 QDomNode nameNode = nameList.item( 0 );
166 nameLineEdit->setText( nameNode.toElement().text() );
167 // kDebug() << "StringReplacerConf::load: name = " << nameNode.toElement().text() << endl;
169 // Language Codes setting. List may be single element of comma-separated values,
170 // or multiple elements.
171 QString languageCodes;
172 QDomNodeList languageList = doc.elementsByTagName( "language-code" );
173 for ( int ndx=0; ndx < languageList.count(); ++ndx )
175 QDomNode languageNode = languageList.item( ndx );
176 if (!languageCodes.isEmpty()) languageCodes += ",";
177 languageCodes += languageNode.toElement().text();
179 if ( clear )
180 m_languageCodeList = languageCodes.split(',', QString::SkipEmptyParts);
181 else
182 m_languageCodeList += languageCodes.split(',', QString::SkipEmptyParts);
183 QString language;
184 m_languageCodeList.sort();
185 // Eliminate dups.
187 int ndx = m_languageCodeList.count() - 2;
188 while ( ndx >= 0 ) {
189 if ( m_languageCodeList[ndx] == m_languageCodeList[ndx+1] )
190 m_languageCodeList.removeAt(ndx+1);
191 else
192 ndx--;
195 for ( int ndx=0; ndx < m_languageCodeList.count(); ++ndx )
197 if (!language.isEmpty()) language += ",";
198 language += KGlobal::locale()->twoAlphaToLanguageName(m_languageCodeList[ndx]);
200 languageLineEdit->setText(language);
202 // AppId. Apply this filter only if DCOP appId of application that queued
203 // the text contains this string. List may be single element of comma-separated values,
204 // or multiple elements.
205 QDomNodeList appIdList = doc.elementsByTagName( "appid" );
206 QString appIds;
207 for ( int ndx=0; ndx < appIdList.count(); ++ndx )
209 QDomNode appIdNode = appIdList.item( ndx );
210 if (!appIds.isEmpty()) appIds += ",";
211 appIds += appIdNode.toElement().text();
213 if ( !clear ) appIds = appIdLineEdit->text() + appIds;
214 appIdLineEdit->setText( appIds );
216 // Word list.
217 QDomNodeList wordList = doc.elementsByTagName( "word" );
218 const int wordListCount = wordList.count();
219 for ( int wordIndex = 0; wordIndex < wordListCount; ++wordIndex )
221 // kDebug() << "StringReplacerConf::load: start parsing of word " << wordIndex << endl;
222 QDomNode wordNode = wordList.item(wordIndex);
223 QDomNodeList propList = wordNode.childNodes();
224 QString wordType;
225 QString match;
226 QString subst;
227 const int propListCount = propList.count();
228 for ( int propIndex = 0; propIndex < propListCount; ++propIndex )
230 QDomNode propNode = propList.item(propIndex);
231 QDomElement prop = propNode.toElement();
232 if (prop.tagName() == "type") wordType = prop.text();
233 if (prop.tagName() == "match") match = prop.text();
234 if (prop.tagName() == "subst") subst = prop.text();
236 int tableRow = substLView->rowCount();
237 substLView->setRowCount( tableRow + 1 );
238 substLView->setItem( tableRow, 0, new QTableWidgetItem( wordType ) );
239 substLView->setItem( tableRow, 1, new QTableWidgetItem( match ) );
240 substLView->setItem( tableRow, 2, new QTableWidgetItem( subst ) );
243 return QString();
247 * This function gets called when the user wants to save the settings in
248 * the user interface, updating the config files or wherever the
249 * configuration is stored. The method is called when the user clicks "Apply"
250 * or "Ok". The plugin should save its configuration in the specified
251 * group of the specified config file.
252 * @param config Pointer to a KConfig object.
253 * @param configGroup Call config->setGroup with this argument before
254 * saving your configuration.
256 void StringReplacerConf::save(KConfig* config, const QString& configGroup){
257 // kDebug() << "StringReplacerConf::save: Running" << endl;
258 QString wordsFilename =
259 KGlobal::dirs()->saveLocation( "data" ,"kttsd/stringreplacer/", true );
260 if ( wordsFilename.isEmpty() )
262 kDebug() << "StringReplacerConf::save: no save location" << endl;
263 return;
265 wordsFilename += configGroup;
266 QString errMsg = saveToFile( wordsFilename );
267 if ( errMsg.isEmpty() )
269 config->setGroup( configGroup );
270 config->writeEntry( "WordListFile", realFilePath(wordsFilename) );
272 else
273 kDebug() << "StringReplacerConf::save: " << errMsg << endl;
276 // Saves word list and settings to a file.
277 QString StringReplacerConf::saveToFile(const QString& filename)
279 // kDebug() << "StringReplacerConf::saveToFile: saving to file " << wordsFilename << endl;
281 QFile file( filename );
282 if ( !file.open( QIODevice::WriteOnly ) )
283 return i18n("Unable to open file ") + filename;
285 // QDomDocument doc( "http://www.kde.org/share/apps/kttsd/stringreplacer/wordlist.dtd []" );
286 QDomDocument doc( "" );
288 QDomElement root = doc.createElement( "wordlist" );
289 doc.appendChild( root );
291 // Name.
292 QDomElement name = doc.createElement( "name" );
293 root.appendChild( name );
294 QDomText t = doc.createTextNode( nameLineEdit->text() );
295 name.appendChild( t );
297 // Language code.
298 for ( int ndx=0; ndx < m_languageCodeList.count(); ++ndx )
300 QDomElement languageCode = doc.createElement( "language-code" );
301 root.appendChild( languageCode );
302 t = doc.createTextNode( m_languageCodeList[ndx] );
303 languageCode.appendChild( t );
306 // Application ID
307 QString appId = appIdLineEdit->text().replace(" ", "");
308 if ( !appId.isEmpty() )
310 QStringList appIdList = appId.split( ",", QString::SkipEmptyParts );
311 for ( int ndx=0; ndx < appIdList.count(); ++ndx )
313 QDomElement appIdElem = doc.createElement( "appid" );
314 root.appendChild( appIdElem );
315 t = doc.createTextNode( appIdList[ndx] );
316 appIdElem.appendChild( t );
320 // Words.
321 for ( int row = 1; row < substLView->rowCount(); ++row )
323 QDomElement wordTag = doc.createElement( "word" );
324 root.appendChild( wordTag );
325 QDomElement propTag = doc.createElement( "type" );
326 wordTag.appendChild( propTag);
327 QDomText t = doc.createTextNode( substLView->item(row, 0)->text() );
328 propTag.appendChild( t );
330 propTag = doc.createElement( "match" );
331 wordTag.appendChild( propTag);
332 t = doc.createCDATASection( substLView->item(row, 1)->text() );
333 propTag.appendChild( t );
335 propTag = doc.createElement( "subst" );
336 wordTag.appendChild( propTag);
337 t = doc.createCDATASection( substLView->item(row, 2)->text() );
338 propTag.appendChild( t );
341 // Write it all out.
342 QTextStream ts( &file );
343 ts.setCodec( "UTF-8" );
344 ts << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
345 ts << doc.toString();
346 // kDebug() << "StringReplacerConf::saveToFile: writing out " << doc.toString() << endl;
347 file.close();
349 return QString();
352 /**
353 * This function is called to set the settings in the module to sensible
354 * default values. It gets called when hitting the "Default" button. The
355 * default values should probably be the same as the ones the application
356 * uses when started without a config file. Note that defaults should
357 * be applied to the on-screen widgets; not to the config file.
359 void StringReplacerConf::defaults(){
360 // kDebug() << "StringReplacerConf::defaults: Running" << endl;
361 // Default language is none.
362 m_languageCodeList.clear();
363 languageLineEdit->setText( "" );
364 // Default name.
365 nameLineEdit->setText( i18n("String Replacer") );
366 substLView->clear();
367 // Default App ID is blank.
368 appIdLineEdit->setText( "" );
369 enableDisableButtons();
370 // kDebug() << "StringReplacerConf::defaults: Exiting" << endl;
374 * Indicates whether the plugin supports multiple instances. Return
375 * False if only one instance of the plugin can be configured.
376 * @return True if multiple instances are possible.
378 bool StringReplacerConf::supportsMultiInstance() { return true; }
381 * Returns the name of the plugin. Displayed in Filters tab of KTTSMgr.
382 * If there can be more than one instance of a filter, it should return
383 * a unique name for each instance. The name should be translated for
384 * the user if possible. If the plugin is not correctly configured,
385 * return an empty string.
386 * @return Filter instance name.
388 QString StringReplacerConf::userPlugInName()
390 if ( substLView->rowCount() == 0 ) return QString();
391 QString instName = nameLineEdit->text();
392 if ( instName.isEmpty() )
394 QString language;
395 if (m_languageCodeList.count() == 1)
396 language = KGlobal::locale()->twoAlphaToLanguageName(m_languageCodeList[0]);
397 if (m_languageCodeList.count() > 1)
398 language = i18n("Multiple Languages");
399 if (!language.isEmpty())
400 instName = i18n("String Replacer") + " (" + language + ")";
402 return instName;
405 // Converts a Substitution Type to displayable string.
406 QString StringReplacerConf::substitutionTypeToString(const int substitutionType)
408 switch (substitutionType)
410 case stWord: return i18n("Word");
411 case stRegExp: return "RegExp";
413 return i18n("Error");
416 void StringReplacerConf::slotLanguageBrowseButton_clicked()
418 // Create a QHBox to host K3ListView.
419 KHBox* hBox = new KHBox(this);
420 // Create a QTableWidget and fill with all known languages.
421 QTableWidget* langLView = new QTableWidget(hBox);
422 langLView->setColumnCount(2);
423 langLView->setHorizontalHeaderItem(0, new QTableWidgetItem(i18n("Language")));
424 langLView->setHorizontalHeaderItem(1, new QTableWidgetItem(i18n("Code")));
425 langLView->setSelectionMode(QAbstractItemView::ExtendedSelection);
426 langLView->setSelectionBehavior(QAbstractItemView::SelectRows);
427 QStringList allLocales = KGlobal::locale()->allLanguagesTwoAlpha();
428 QString locale;
429 QString languageCode;
430 QString countryCode;
431 QString charSet;
432 QString language;
433 // Blank line so user can select no language.
434 langLView->setRowCount(2);
435 langLView->setItem(1, 0, new QTableWidgetItem(""));
436 langLView->setItem(1, 1, new QTableWidgetItem(""));
437 if (m_languageCodeList.isEmpty()) langLView->selectRow(1);
438 const int allLocalesCount = allLocales.count();
439 for (int ndx=0; ndx < allLocalesCount; ++ndx)
441 locale = allLocales[ndx];
442 KGlobal::locale()->splitLocale(locale, languageCode, countryCode, charSet);
443 language = KGlobal::locale()->twoAlphaToLanguageName(languageCode);
444 if (!countryCode.isEmpty()) language +=
445 " (" + KGlobal::locale()->twoAlphaToCountryName(countryCode)+")";
446 int row = langLView->rowCount();
447 langLView->setRowCount(row+1);
448 langLView->setItem(row, 0, new QTableWidgetItem(language));
449 langLView->setItem(row, 1, new QTableWidgetItem(locale));
450 if (m_languageCodeList.contains(locale)) langLView->selectRow(row);
452 // Sort by language.
453 langLView->sortItems(0);
454 // Display the box in a dialog.
455 KDialog* dlg = new KDialog(
456 this,
457 i18n("Select Languages"),
458 KDialog::Help|KDialog::Ok|KDialog::Cancel);
459 dlg->setMainWidget(hBox);
460 dlg->setHelp("", "kttsd");
461 dlg->setInitialSize(QSize(300, 500));
462 int dlgResult = dlg->exec();
463 languageCode.clear();
464 if (dlgResult == QDialog::Accepted)
466 m_languageCodeList.clear();
467 for (int row = 1; row < langLView->rowCount(); ++row)
469 if (langLView->isItemSelected(langLView->item(row, 1)))
470 m_languageCodeList += langLView->item(row, 1)->text();
473 delete dlg;
474 // TODO: Also delete K3ListView and QHBox?
475 if (dlgResult != QDialog::Accepted) return;
476 language = "";
477 for ( int ndx=0; ndx < m_languageCodeList.count(); ++ndx)
479 if (!language.isEmpty()) language += ",";
480 language += KGlobal::locale()->twoAlphaToLanguageName(m_languageCodeList[ndx]);
482 QString s1 = languageLineEdit->text();
483 languageLineEdit->setText(language);
484 // Replace language in the user's filter name.
485 QString s2 = nameLineEdit->text();
486 if (m_languageCodeList.count() > 1) language = i18n("Multiple Languages");
487 if ( !s1.isEmpty() )
489 s2.replace( s1, language );
490 s2.replace( i18n("Multiple Languages"), language );
492 s2.replace(" ()", "");
493 if ( !s2.contains("(") && !language.isEmpty() ) s2 += " (" + language + ")";
494 nameLineEdit->setText(s2);
495 configChanged();
498 void StringReplacerConf::enableDisableButtons()
500 int row = substLView->currentRow();
501 bool enableBtn = row > 0 && row < substLView->rowCount();
502 if (enableBtn)
504 upButton->setEnabled(row > 0);
505 downButton->setEnabled(row < (substLView->rowCount() - 1));
506 } else {
507 upButton->setEnabled(false);
508 downButton->setEnabled(false);
510 editButton->setEnabled(enableBtn);
511 removeButton->setEnabled(enableBtn);
512 clearButton->setEnabled(substLView->rowCount() > 1);
513 saveButton->setEnabled(substLView->rowCount() > 1);
516 void StringReplacerConf::slotUpButton_clicked()
518 int row = substLView->currentRow();
519 if (row < 1 || row >= substLView->rowCount()) return;
521 QTableWidgetItem *itemAbove = substLView->itemAt(row - 1, 0);
522 QTableWidgetItem *item = substLView->itemAt(row, 0);
523 QString t = itemAbove->text();
524 itemAbove->setText(item->text());
525 item->setText(t);
527 itemAbove = substLView->itemAt(row - 1, 1);
528 item = substLView->itemAt(row, 1);
529 t = itemAbove->text();
530 itemAbove->setText(item->text());
531 item->setText(t);
533 substLView->setCurrentItem(substLView->itemAt(row - 1, substLView->currentColumn()));
534 // TODO: Is this needed? substLView->scrollTo(substLView->indexFromItem(itemAbove));
535 enableDisableButtons();
536 configChanged();
539 void StringReplacerConf::slotDownButton_clicked()
541 int row = substLView->currentRow();
542 if (row == 0 || row >= substLView->rowCount() - 1) return;
544 QTableWidgetItem *itemBelow = substLView->itemAt(row + 1, 0);
545 QTableWidgetItem *item = substLView->itemAt(row, 0);
546 QString t = itemBelow->text();
547 itemBelow->setText(item->text());
548 item->setText(t);
550 itemBelow = substLView->itemAt(row - 1, 1);
551 item = substLView->itemAt(row, 1);
552 t = itemBelow->text();
553 itemBelow->setText(item->text());
554 item->setText(t);
556 itemBelow = substLView->itemAt(row - 1, 2);
557 item = substLView->itemAt(row, 2);
558 t = itemBelow->text();
559 itemBelow->setText(item->text());
560 item->setText(t);
562 substLView->setCurrentItem(substLView->itemAt(row + 1, substLView->currentColumn()));
563 // TODO: Is this needed? substLView->scrollTo(substLView->indexFromItem(itemBelow));
564 enableDisableButtons();
565 configChanged();
568 void StringReplacerConf::slotAddButton_clicked()
570 addOrEditSubstitution( true );
573 void StringReplacerConf::slotEditButton_clicked()
575 addOrEditSubstitution( false );
578 // Displays the add/edit string replacement dialog.
579 void StringReplacerConf::addOrEditSubstitution(bool isAdd)
581 int row;
582 if (isAdd)
583 row = substLView->rowCount() - 1;
584 else
585 row = substLView->currentRow();
586 // Create a QHBox to host widget.
587 KHBox* hBox = new KHBox(this);
588 // Create widget.
589 m_editWidget = new Ui::EditReplacementWidget();
590 m_editWidget->setupUi( hBox );
591 // Set controls if editing existing.
592 m_editWidget->matchButton->setEnabled( false );
593 if (!isAdd)
595 if ( substLView->itemAt(row, 0)->text() == "RegExp" )
597 m_editWidget->regexpRadioButton->setChecked( true );
598 m_editWidget->matchButton->setEnabled( m_reEditorInstalled );
600 m_editWidget->matchLineEdit->setText( substLView->itemAt(row, 1)->text() );
601 m_editWidget->substLineEdit->setText( substLView->itemAt(row, 2)->text() );
603 // The match box may not be blank.
604 connect( m_editWidget->matchLineEdit, SIGNAL(textChanged(const QString&)),
605 this, SLOT(slotMatchLineEdit_textChanged(const QString&)) );
606 connect( m_editWidget->regexpRadioButton, SIGNAL(clicked()),
607 this, SLOT(slotTypeButtonGroup_clicked()) );
608 connect( m_editWidget->wordRadioButton, SIGNAL(clicked()),
609 this, SLOT(slotTypeButtonGroup_clicked()) );
610 connect( m_editWidget->matchButton, SIGNAL(clicked()),
611 this, SLOT(slotMatchButton_clicked()) );
612 // Display the box in a dialog.
613 m_editDlg = new KDialog(
614 this,
615 i18n("Edit String Replacement"),
616 KDialog::Help|KDialog::Ok|KDialog::Cancel);
617 // Disable OK button if match field blank.
618 m_editDlg->setMainWidget( hBox );
619 m_editDlg->setHelp( "", "kttsd" );
620 m_editDlg->enableButton( KDialog::Ok, !m_editWidget->matchLineEdit->text().isEmpty() );
621 int dlgResult = m_editDlg->exec();
622 QString substType = i18n( "Word" );
623 if ( m_editWidget->regexpRadioButton->isChecked() ) substType = "RegExp";
624 QString match = m_editWidget->matchLineEdit->text();
625 QString subst = m_editWidget->substLineEdit->text();
626 delete m_editDlg;
627 m_editDlg = 0;
628 m_editWidget = 0;
629 if (dlgResult != QDialog::Accepted) return;
630 // TODO: Also delete hBox and w?
631 if ( match.isEmpty() ) return;
632 if ( isAdd )
634 row = substLView->rowCount();
635 substLView->setRowCount(row + 1);
636 substLView->setCurrentItem(substLView->itemAt(row, 0));
638 substLView->itemAt(row, 0)->setText(substType);
639 substLView->itemAt(row, 1)->setText(match);
640 substLView->itemAt(row, 2)->setText(subst);
641 // TODO: Is this needed? substLView->scrollTo(substLView->indexFromItem(substLView->itemAt(row,0)));
642 enableDisableButtons();
643 configChanged();
646 void StringReplacerConf::slotMatchLineEdit_textChanged(const QString& text)
648 // Disable OK button if match field blank.
649 if ( !m_editDlg ) return;
650 m_editDlg->enableButton( KDialog::Ok, !text.isEmpty() );
653 void StringReplacerConf::slotRemoveButton_clicked()
655 int row = substLView->currentRow();
656 if (row <= 0 || row >= substLView->rowCount()) return;
657 delete substLView->takeItem(row, 0);
658 delete substLView->takeItem(row, 1);
659 delete substLView->takeItem(row, 2);
660 substLView->removeRow(row);
661 enableDisableButtons();
662 configChanged();
665 void StringReplacerConf::slotTypeButtonGroup_clicked()
667 // Enable Regular Expression Editor button if editor is installed (requires kdeutils).
668 if ( !m_editWidget ) return;
669 m_editWidget->matchButton->setEnabled( m_editWidget->regexpRadioButton->isChecked() && m_reEditorInstalled );
672 void StringReplacerConf::slotMatchButton_clicked()
674 // Show Regular Expression Editor dialog if it is installed.
675 if ( !m_editWidget ) return;
676 if ( !m_editDlg ) return;
677 if ( !m_reEditorInstalled ) return;
678 QDialog *editorDialog =
679 KParts::ComponentFactory::createInstanceFromQuery<QDialog>( "KRegExpEditor/KRegExpEditor" );
680 if ( editorDialog )
682 // kdeutils was installed, so the dialog was found. Fetch the editor interface.
684 KRegExpEditorInterface *reEditor = dynamic_cast<KRegExpEditorInterface*>( editorDialog );
685 Q_ASSERT( reEditor ); // This should not fail!// now use the editor.
686 reEditor->setRegExp( m_editWidget->matchLineEdit->text() );
687 int dlgResult = editorDialog->exec();
688 if ( dlgResult == QDialog::Accepted )
690 QString re = reEditor->regExp();
691 m_editWidget->matchLineEdit->setText( re );
692 m_editDlg->enableButton( KDialog::Ok, !re.isEmpty() );
694 delete editorDialog;
695 } else return;
698 void StringReplacerConf::slotLoadButton_clicked()
700 QStringList dataDirs = KGlobal::dirs()->findAllResources("data", "kttsd/stringreplacer/");
701 QString dataDir;
702 if (!dataDirs.isEmpty()) dataDir = dataDirs.last();
703 QString filename = KFileDialog::getOpenFileName(
704 dataDir,
705 "*.xml|String Replacer Word List (*.xml)",
706 this,
707 "stringreplacer_loadfile");
708 if ( filename.isEmpty() ) return;
709 QString errMsg = loadFromFile( filename, false );
710 enableDisableButtons();
711 if ( !errMsg.isEmpty() )
712 KMessageBox::sorry( this, errMsg, i18n("Error Opening File") );
713 else
714 configChanged();
717 void StringReplacerConf::slotSaveButton_clicked()
719 QString filename = KFileDialog::getSaveFileName(
720 KGlobal::dirs()->saveLocation( "data" ,"kttsd/stringreplacer/", false ),
721 "*.xml|String Replacer Word List (*.xml)",
722 this,
723 "stringreplacer_savefile");
724 if ( filename.isEmpty() ) return;
725 QString errMsg = saveToFile( filename );
726 enableDisableButtons();
727 if ( !errMsg.isEmpty() )
728 KMessageBox::sorry( this, errMsg, i18n("Error Opening File") );
731 void StringReplacerConf::slotClearButton_clicked()
733 substLView->clear();
734 enableDisableButtons();