1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
/***************************************************** vim:set ts=4 sw=4 sts=4:
Generic String Replacement Filter Configuration class.
-------------------
Copyright:
(C) 2005 by Gary Cramblitt <garycramblitt@comcast.net>
-------------------
Original author: Gary Cramblitt <garycramblitt@comcast.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
// TQt includes.
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqstring.h>
#include <tqhbox.h>
#include <tqlayout.h>
#include <tqcheckbox.h>
#include <tqdom.h>
#include <tqfile.h>
#include <tqradiobutton.h>
// KDE includes.
#include <kglobal.h>
#include <klocale.h>
#include <klistview.h>
#include <klineedit.h>
#include <kdialog.h>
#include <kdialogbase.h>
#include <kpushbutton.h>
#include <kconfig.h>
#include <kstandarddirs.h>
#include <kregexpeditorinterface.h>
#include <ktrader.h>
#include <kparts/componentfactory.h>
#include <kfiledialog.h>
#include <kmessagebox.h>
// KTTS includes.
#include "filterconf.h"
// StringReplacer includes.
#include "stringreplacerconf.h"
#include "stringreplacerconf.moc"
#include "editreplacementwidget.h"
/**
* Constructor
*/
StringReplacerConf::StringReplacerConf( TQWidget *parent, const char *name, const TQStringList& /*args*/) :
KttsFilterConf(parent, name),
m_editDlg(0),
m_editWidget(0)
{
// kdDebug() << "StringReplacerConf::StringReplacerConf: Running" << endl;
// Create configuration widget.
TQVBoxLayout *layout = new TQVBoxLayout(this, KDialog::marginHint(),
KDialog::spacingHint(), "StringReplacerConfigWidgetLayout");
layout->setAlignment (TQt::AlignTop);
m_widget = new StringReplacerConfWidget(this, "StringReplacerConfigWidget");
layout->addWidget(m_widget);
m_widget->substLView->setSortColumn(-1);
connect(m_widget->nameLineEdit, TQT_SIGNAL(textChanged(const TQString&)),
this, TQT_SLOT(configChanged()));
connect(m_widget->languageBrowseButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotLanguageBrowseButton_clicked()));
connect(m_widget->addButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotAddButton_clicked()));
connect(m_widget->upButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotUpButton_clicked()));
connect(m_widget->downButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotDownButton_clicked()));
connect(m_widget->editButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotEditButton_clicked()));
connect(m_widget->removeButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotRemoveButton_clicked()));
connect(m_widget->loadButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotLoadButton_clicked()));
connect(m_widget->saveButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotSaveButton_clicked()));
connect(m_widget->clearButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotClearButton_clicked()));
connect(m_widget->substLView, TQT_SIGNAL(selectionChanged()),
this, TQT_SLOT(enableDisableButtons()));
connect(m_widget->appIdLineEdit, TQT_SIGNAL(textChanged(const TQString&)),
this, TQT_SLOT(configChanged()));
// Determine if kdeutils Regular Expression Editor is installed.
m_reEditorInstalled = !KTrader::self()->query("KRegExpEditor/KRegExpEditor").isEmpty();
// Set up defaults.
defaults();
}
/**
* Destructor.
*/
StringReplacerConf::~StringReplacerConf(){
// kdDebug() << "StringReplacerConf::~StringReplacerConf: Running" << endl;
}
/**
* This method is invoked whenever the module should read its
* configuration (most of the times from a config file) and update the
* user interface. This happens when the user clicks the "Reset" button in
* the control center, to undo all of his changes and restore the currently
* valid settings. Note that kttsmgr calls this when the plugin is
* loaded, so it not necessary to call it in your constructor.
* The plugin should read its configuration from the specified group
* in the specified config file.
* @param config Pointer to a KConfig object.
* @param configGroup Call config->setGroup with this argument before
* loading your configuration.
*/
void StringReplacerConf::load(KConfig* config, const TQString& configGroup){
// kdDebug() << "StringReplacerConf::load: Running" << endl;
// See if this filter previously save its word list.
config->setGroup( configGroup );
TQString wordsFilename = config->readEntry( "WordListFile" );
if ( !wordsFilename.isEmpty() )
{
TQString errMsg = loadFromFile( wordsFilename, true );
if ( !errMsg.isEmpty() )
kdDebug() << "StringReplacerConf::load: " << errMsg << endl;
enableDisableButtons();
}
}
// Loads word list and settings from a file. Clearing configuration if clear is True.
TQString StringReplacerConf::loadFromFile( const TQString& filename, bool clear)
{
// Open existing word list.
TQFile file( filename );
if ( !file.open( IO_ReadOnly ) )
{
return i18n("Unable to open file.") + filename;
}
// TQDomDocument doc( "http://www.kde.org/share/apps/kttsd/stringreplacer/wordlist.dtd []" );
TQDomDocument doc( "" );
if ( !doc.setContent( &file ) ) {
file.close();
return i18n("File not in proper XML format.");
}
// kdDebug() << "StringReplacerConf::load: document successfully parsed." << endl;
file.close();
// Clear list view.
if ( clear ) m_widget->substLView->clear();
// Name setting.
TQDomNodeList nameList = doc.elementsByTagName( "name" );
TQDomNode nameNode = nameList.item( 0 );
m_widget->nameLineEdit->setText( nameNode.toElement().text() );
// kdDebug() << "StringReplacerConf::load: name = " << nameNode.toElement().text() << endl;
// Language Codes setting. List may be single element of comma-separated values,
// or multiple elements.
TQString languageCodes;
TQDomNodeList languageList = doc.elementsByTagName( "language-code" );
for ( uint ndx=0; ndx < languageList.count(); ++ndx )
{
TQDomNode languageNode = languageList.item( ndx );
if (!languageCodes.isEmpty()) languageCodes += ",";
languageCodes += languageNode.toElement().text();
}
if ( clear )
m_languageCodeList = TQStringList::split(',', languageCodes, false);
else
m_languageCodeList += TQStringList::split(',', languageCodes, false);
TQString language;
m_languageCodeList.sort();
// Eliminate dups.
for ( int ndx = m_languageCodeList.count() - 2; ndx >= 0; --ndx )
{
if ( m_languageCodeList[ndx] == m_languageCodeList[ndx+1] )
m_languageCodeList.remove(m_languageCodeList.at(ndx+1));
}
for ( uint ndx=0; ndx < m_languageCodeList.count(); ++ndx )
{
if (!language.isEmpty()) language += ",";
language += KGlobal::locale()->twoAlphaToLanguageName(m_languageCodeList[ndx]);
}
m_widget->languageLineEdit->setText(language);
// AppId. Apply this filter only if DCOP appId of application that queued
// the text contains this string. List may be single element of comma-separated values,
// or multiple elements.
TQDomNodeList appIdList = doc.elementsByTagName( "appid" );
TQString appIds;
for ( uint ndx=0; ndx < appIdList.count(); ++ndx )
{
TQDomNode appIdNode = appIdList.item( ndx );
if (!appIds.isEmpty()) appIds += ",";
appIds += appIdNode.toElement().text();
}
if ( !clear ) appIds = m_widget->appIdLineEdit->text() + appIds;
m_widget->appIdLineEdit->setText( appIds );
// Word list.
TQListViewItem* item = 0;
if ( !clear ) item = m_widget->substLView->lastChild();
TQDomNodeList wordList = doc.elementsByTagName("word");
const int wordListCount = wordList.count();
for (int wordIndex = 0; wordIndex < wordListCount; ++wordIndex)
{
// kdDebug() << "StringReplacerConf::load: start parsing of word " << wordIndex << endl;
TQDomNode wordNode = wordList.item(wordIndex);
TQDomNodeList propList = wordNode.childNodes();
TQString wordType;
TQString matchCase = "No"; // Default for old (v<=3.5.3) config files with no <case/>.
TQString match;
TQString subst;
const int propListCount = propList.count();
for (int propIndex = 0; propIndex < propListCount; ++propIndex)
{
TQDomNode propNode = propList.item(propIndex);
TQDomElement prop = propNode.toElement();
if (prop.tagName() == "type") wordType = prop.text();
if (prop.tagName() == "case") matchCase = prop.text();
if (prop.tagName() == "match") match = prop.text();
if (prop.tagName() == "subst") subst = prop.text();
}
TQString wordTypeStr =
(wordType=="RegExp"?i18n("Abbreviation for 'Regular Expression'", "RegExp"):i18n("Word"));
TQString matchCaseStr =
(matchCase=="Yes"?i18n("Yes"):i18n("No"));
if (!item)
item = new KListViewItem(m_widget->substLView, wordTypeStr, matchCaseStr, match, subst);
else
item = new KListViewItem(m_widget->substLView, item, wordTypeStr, matchCaseStr, match, subst);
}
return TQString();
}
/**
* This function gets called when the user wants to save the settings in
* the user interface, updating the config files or wherever the
* configuration is stored. The method is called when the user clicks "Apply"
* or "Ok". The plugin should save its configuration in the specified
* group of the specified config file.
* @param config Pointer to a KConfig object.
* @param configGroup Call config->setGroup with this argument before
* saving your configuration.
*/
void StringReplacerConf::save(KConfig* config, const TQString& configGroup){
// kdDebug() << "StringReplacerConf::save: Running" << endl;
TQString wordsFilename =
KGlobal::dirs()->saveLocation( "data" ,"kttsd/stringreplacer/", true );
if ( wordsFilename.isEmpty() )
{
kdDebug() << "StringReplacerConf::save: no save location" << endl;
return;
}
wordsFilename += configGroup;
TQString errMsg = saveToFile( wordsFilename );
if ( errMsg.isEmpty() )
{
config->setGroup( configGroup );
config->writeEntry( "WordListFile", realFilePath(wordsFilename) );
}
else
kdDebug() << "StringReplacerConf::save: " << errMsg << endl;
}
// Saves word list and settings to a file.
TQString StringReplacerConf::saveToFile(const TQString& filename)
{
// kdDebug() << "StringReplacerConf::saveToFile: saving to file " << wordsFilename << endl;
TQFile file( filename );
if ( !file.open( IO_WriteOnly ) )
return i18n("Unable to open file ") + filename;
// TQDomDocument doc( "http://www.kde.org/share/apps/kttsd/stringreplacer/wordlist.dtd []" );
TQDomDocument doc( "" );
TQDomElement root = doc.createElement( "wordlist" );
doc.appendChild( root );
// Name.
TQDomElement name = doc.createElement( "name" );
root.appendChild( name );
TQDomText t = doc.createTextNode( m_widget->nameLineEdit->text() );
name.appendChild( t );
// Language code.
for ( uint ndx=0; ndx < m_languageCodeList.count(); ++ndx )
{
TQDomElement languageCode = doc.createElement( "language-code" );
root.appendChild( languageCode );
t = doc.createTextNode( m_languageCodeList[ndx] );
languageCode.appendChild( t );
}
// Application ID
TQString appId = m_widget->appIdLineEdit->text().replace(" ", "");
if ( !appId.isEmpty() )
{
TQStringList appIdList = TQStringList::split(",", appId);
for ( uint ndx=0; ndx < appIdList.count(); ++ndx )
{
TQDomElement appIdElem = doc.createElement( "appid" );
root.appendChild( appIdElem );
t = doc.createTextNode( appIdList[ndx] );
appIdElem.appendChild( t );
}
}
// Words.
TQListView* lView = m_widget->substLView;
TQListViewItem* item = lView->firstChild();
while (item)
{
TQDomElement wordTag = doc.createElement( "word" );
root.appendChild( wordTag );
TQDomElement propTag = doc.createElement( "type" );
wordTag.appendChild( propTag);
TQDomText t = doc.createTextNode( item->text(0)==i18n("Word")?"Word":"RegExp" );
propTag.appendChild( t );
propTag = doc.createElement( "case" );
wordTag.appendChild( propTag);
t = doc.createTextNode( item->text(1)==i18n("Yes")?"Yes":"No" );
propTag.appendChild( t );
propTag = doc.createElement( "match" );
wordTag.appendChild( propTag);
t = doc.createCDATASection( item->text(2) );
propTag.appendChild( t );
propTag = doc.createElement( "subst" );
wordTag.appendChild( propTag);
t = doc.createCDATASection( item->text(3) );
propTag.appendChild( t );
item = item->nextSibling();
}
// Write it all out.
TQTextStream ts( &file );
ts.setEncoding( TQTextStream::UnicodeUTF8 );
ts << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
ts << doc.toString();
// kdDebug() << "StringReplacerConf::saveToFile: writing out " << doc.toString() << endl;
file.close();
return TQString();
}
/**
* This function is called to set the settings in the module to sensible
* default values. It gets called when hitting the "Default" button. The
* default values should probably be the same as the ones the application
* uses when started without a config file. Note that defaults should
* be applied to the on-screen widgets; not to the config file.
*/
void StringReplacerConf::defaults(){
// kdDebug() << "StringReplacerConf::defaults: Running" << endl;
// Default language is none.
m_languageCodeList.clear();
m_widget->languageLineEdit->setText( "" );
// Default name.
m_widget->nameLineEdit->setText( i18n("String Replacer") );
m_widget->substLView->clear();
// Default App ID is blank.
m_widget->appIdLineEdit->setText( "" );
enableDisableButtons();
// kdDebug() << "StringReplacerConf::defaults: Exiting" << endl;
}
/**
* Indicates whether the plugin supports multiple instances. Return
* False if only one instance of the plugin can be configured.
* @return True if multiple instances are possible.
*/
bool StringReplacerConf::supportsMultiInstance() { return true; }
/**
* Returns the name of the plugin. Displayed in Filters tab of KTTSMgr.
* If there can be more than one instance of a filter, it should return
* a unique name for each instance. The name should be translated for
* the user if possible. If the plugin is not correctly configured,
* return an empty string.
* @return Filter instance name.
*/
TQString StringReplacerConf::userPlugInName()
{
if ( m_widget->substLView->childCount() == 0 ) return TQString();
TQString instName = m_widget->nameLineEdit->text();
if ( instName.isEmpty() )
{
TQString language;
if (m_languageCodeList.count() == 1)
language = KGlobal::locale()->twoAlphaToLanguageName(m_languageCodeList[0]);
if (m_languageCodeList.count() > 1)
language = i18n("Multiple Languages");
if (!language.isEmpty())
instName = i18n("String Replacer") + " (" + language + ")";
}
return instName;
}
void StringReplacerConf::slotLanguageBrowseButton_clicked()
{
// Create a TQHBox to host KListView.
TQHBox* hBox = new TQHBox(m_widget, "SelectLanguage_hbox");
// Create a KListView and fill with all known languages.
KListView* langLView = new KListView(hBox, "SelectLanguage_lview");
langLView->addColumn(i18n("Language"));
langLView->addColumn(i18n("Code"));
langLView->setSelectionMode(TQListView::Extended);
TQStringList allLocales = KGlobal::locale()->allLanguagesTwoAlpha();
TQString locale;
TQString languageCode;
TQString countryCode;
TQString charSet;
TQString language;
// Blank line so user can select no language.
TQListViewItem* item = new KListViewItem(langLView, "", "");
if (m_languageCodeList.isEmpty()) item->setSelected(true);
const int allLocalesCount = allLocales.count();
for (int ndx=0; ndx < allLocalesCount; ++ndx)
{
locale = allLocales[ndx];
KGlobal::locale()->splitLocale(locale, languageCode, countryCode, charSet);
language = KGlobal::locale()->twoAlphaToLanguageName(languageCode);
if (!countryCode.isEmpty()) language +=
" (" + KGlobal::locale()->twoAlphaToCountryName(countryCode)+")";
item = new KListViewItem(langLView, language, locale);
if (m_languageCodeList.contains(locale)) item->setSelected(true);
}
// Sort by language.
langLView->setSorting(0);
langLView->sort();
// Display the box in a dialog.
KDialogBase* dlg = new KDialogBase(
KDialogBase::Swallow,
i18n("Select Languages"),
KDialogBase::Help|KDialogBase::Ok|KDialogBase::Cancel,
KDialogBase::Cancel,
m_widget,
"SelectLanguage_dlg",
true,
true);
dlg->setMainWidget(hBox);
dlg->setHelp("", "kttsd");
dlg->setInitialSize(TQSize(300, 500), false);
int dlgResult = dlg->exec();
languageCode = TQString();
if (dlgResult == TQDialog::Accepted)
{
m_languageCodeList.clear();
TQListViewItem* item = langLView->firstChild();
while (item)
{
if (item->isSelected()) m_languageCodeList += item->text(1);
item = item->nextSibling();
}
}
delete dlg;
// TODO: Also delete KListView and TQHBox?
if (dlgResult != TQDialog::Accepted) return;
language = "";
for ( uint ndx=0; ndx < m_languageCodeList.count(); ++ndx)
{
if (!language.isEmpty()) language += ",";
language += KGlobal::locale()->twoAlphaToLanguageName(m_languageCodeList[ndx]);
}
TQString s1 = m_widget->languageLineEdit->text();
m_widget->languageLineEdit->setText(language);
// Replace language in the user's filter name.
TQString s2 = m_widget->nameLineEdit->text();
if (m_languageCodeList.count() > 1) language = i18n("Multiple Languages");
if ( !s1.isEmpty() )
{
s2.replace( s1, language );
s2.replace( i18n("Multiple Languages"), language );
}
s2.replace(" ()", "");
if ( !s2.contains("(") && !language.isEmpty() ) s2 += " (" + language + ")";
m_widget->nameLineEdit->setText(s2);
configChanged();
}
void StringReplacerConf::enableDisableButtons()
{
bool enableBtn = (m_widget->substLView->selectedItem() != 0);
if (enableBtn)
{
m_widget->upButton->setEnabled(
m_widget->substLView->selectedItem()->itemAbove() != 0);
m_widget->downButton->setEnabled(
m_widget->substLView->selectedItem()->itemBelow() != 0);
} else {
m_widget->upButton->setEnabled(false);
m_widget->downButton->setEnabled(false);
}
m_widget->editButton->setEnabled(enableBtn);
m_widget->removeButton->setEnabled(enableBtn);
m_widget->clearButton->setEnabled(m_widget->substLView->firstChild());
m_widget->saveButton->setEnabled(m_widget->substLView->firstChild());
}
void StringReplacerConf::slotUpButton_clicked()
{
TQListViewItem* item = m_widget->substLView->selectedItem();
if (!item) return;
TQListViewItem* prevItem = item->itemAbove();
if (!prevItem) return;
prevItem->moveItem(item);
m_widget->substLView->setSelected(item, true);
m_widget->substLView->ensureItemVisible(item);
enableDisableButtons();
configChanged();
}
void StringReplacerConf::slotDownButton_clicked()
{
TQListViewItem* item = m_widget->substLView->selectedItem();
if (!item) return;
TQListViewItem* nextItem = item->itemBelow();
if (!nextItem) return;
item->moveItem(nextItem);
m_widget->substLView->setSelected(item, true);
m_widget->substLView->ensureItemVisible(item);
enableDisableButtons();
configChanged();
}
void StringReplacerConf::slotAddButton_clicked()
{
addOrEditSubstitution( true );
}
void StringReplacerConf::slotEditButton_clicked()
{
addOrEditSubstitution( false );
}
// Displays the add/edit string replacement dialog.
void StringReplacerConf::addOrEditSubstitution(bool isAdd)
{
TQListViewItem* item = 0;
if (isAdd)
item = m_widget->substLView->lastChild();
else
{
item = m_widget->substLView->selectedItem();
if (!item) return;
}
// Create a TQHBox to host widget.
TQHBox* hBox = new TQHBox(m_widget, "AddOrEditSubstitution_hbox" );
// Create widget.
m_editWidget = new EditReplacementWidget( hBox, "AddOrEditSubstitution_widget" );
// Set controls if editing existing.
m_editWidget->matchButton->setEnabled( false );
if (!isAdd)
{
if ( item->text(0) == i18n("Abbreviation for 'Regular Expression'", "RegExp") )
{
m_editWidget->regexpRadioButton->setChecked( true );
m_editWidget->matchButton->setEnabled( m_reEditorInstalled );
}
m_editWidget->caseCheckBox->setChecked( (item->text(1))==i18n("Yes") );
m_editWidget->matchLineEdit->setText( item->text(2) );
m_editWidget->substLineEdit->setText( item->text(3) );
}
// The match box may not be blank.
connect( m_editWidget->matchLineEdit, TQT_SIGNAL(textChanged(const TQString&)),
this, TQT_SLOT(slotMatchLineEdit_textChanged(const TQString&)) );
connect( m_editWidget->regexpRadioButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotTypeButtonGroup_clicked()) );
connect( m_editWidget->wordRadioButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotTypeButtonGroup_clicked()) );
connect( m_editWidget->matchButton, TQT_SIGNAL(clicked()),
this, TQT_SLOT(slotMatchButton_clicked()) );
// Display the box in a dialog.
m_editDlg = new KDialogBase(
KDialogBase::Swallow,
i18n("Edit String Replacement"),
KDialogBase::Help|KDialogBase::Ok|KDialogBase::Cancel,
KDialogBase::Cancel,
m_widget,
"AddOrEditSubstitution_dlg",
true,
true);
// Disable OK button if match field blank.
m_editDlg->setMainWidget( hBox );
m_editDlg->setHelp( "", "kttsd" );
m_editDlg->enableButton( KDialogBase::Ok, !m_editWidget->matchLineEdit->text().isEmpty() );
int dlgResult = m_editDlg->exec();
TQString substType = i18n( "Word" );
if ( m_editWidget->regexpRadioButton->isChecked() )
substType = i18n("Abbreviation for 'Regular Expression'", "RegExp");
TQString matchCase = i18n("No");
if ( m_editWidget->caseCheckBox->isChecked() ) matchCase = i18n("Yes");
TQString match = m_editWidget->matchLineEdit->text();
TQString subst = m_editWidget->substLineEdit->text();
delete m_editDlg;
m_editDlg = 0;
m_editWidget = 0;
if (dlgResult != TQDialog::Accepted) return;
// TODO: Also delete hBox and w?
if ( match.isEmpty() ) return;
if ( isAdd )
{
if ( item )
item = new KListViewItem( m_widget->substLView, item, substType, matchCase, match, subst );
else
item = new KListViewItem( m_widget->substLView, substType, matchCase, match, subst );
m_widget->substLView->setSelected( item, true );
}
else
{
item->setText( 0, substType );
item->setText( 1, matchCase );
item->setText( 2, match );
item->setText( 3, subst );
}
m_widget->substLView->ensureItemVisible( item );
enableDisableButtons();
configChanged();
}
void StringReplacerConf::slotMatchLineEdit_textChanged(const TQString& text)
{
// Disable OK button if match field blank.
if ( !m_editDlg ) return;
m_editDlg->enableButton( KDialogBase::Ok, !text.isEmpty() );
}
void StringReplacerConf::slotRemoveButton_clicked()
{
TQListViewItem* item = m_widget->substLView->selectedItem();
if (!item) return;
delete item;
enableDisableButtons();
configChanged();
}
void StringReplacerConf::slotTypeButtonGroup_clicked()
{
// Enable Regular Expression Editor button if editor is installed (requires kdeutils).
if ( !m_editWidget ) return;
m_editWidget->matchButton->setEnabled( m_editWidget->regexpRadioButton->isOn() && m_reEditorInstalled );
}
void StringReplacerConf::slotMatchButton_clicked()
{
// Show Regular Expression Editor dialog if it is installed.
if ( !m_editWidget ) return;
if ( !m_editDlg ) return;
if ( !m_reEditorInstalled ) return;
TQDialog *editorDialog =
KParts::ComponentFactory::createInstanceFromQuery<TQDialog>( "KRegExpEditor/KRegExpEditor" );
if ( editorDialog )
{
// kdeutils was installed, so the dialog was found. Fetch the editor interface.
KRegExpEditorInterface *reEditor =
static_cast<KRegExpEditorInterface *>(editorDialog->qt_cast( "KRegExpEditorInterface" ) );
Q_ASSERT( reEditor ); // This should not fail!// now use the editor.
reEditor->setRegExp( m_editWidget->matchLineEdit->text() );
int dlgResult = editorDialog->exec();
if ( dlgResult == TQDialog::Accepted )
{
TQString re = reEditor->regExp();
m_editWidget->matchLineEdit->setText( re );
m_editDlg->enableButton( KDialogBase::Ok, !re.isEmpty() );
}
delete editorDialog;
} else return;
}
void StringReplacerConf::slotLoadButton_clicked()
{
// TQString dataDir = KGlobal::dirs()->resourceDirs("data").last() + "/kttsd/stringreplacer/";
TQString dataDir = KGlobal::dirs()->findAllResources("data", "kttsd/stringreplacer/").last();
TQString filename = KFileDialog::getOpenFileName(
dataDir,
"*.xml|String Replacer Word List (*.xml)",
m_widget,
"stringreplacer_loadfile");
if ( filename.isEmpty() ) return;
TQString errMsg = loadFromFile( filename, false );
enableDisableButtons();
if ( !errMsg.isEmpty() )
KMessageBox::sorry( m_widget, errMsg, i18n("Error Opening File") );
else
configChanged();
}
void StringReplacerConf::slotSaveButton_clicked()
{
TQString filename = KFileDialog::getSaveFileName(
KGlobal::dirs()->saveLocation( "data" ,"kttsd/stringreplacer/", false ),
"*.xml|String Replacer Word List (*.xml)",
m_widget,
"stringreplacer_savefile");
if ( filename.isEmpty() ) return;
TQString errMsg = saveToFile( filename );
enableDisableButtons();
if ( !errMsg.isEmpty() )
KMessageBox::sorry( m_widget, errMsg, i18n("Error Opening File") );
}
void StringReplacerConf::slotClearButton_clicked()
{
m_widget->substLView->clear();
enableDisableButtons();
}
|