Quick fix for Q_ASSERT error, may rework m_pMiniLexica for next release
[linguistica.git] / preferencesdialog.cpp
blob2e61322f9e0fbeb98ec64ee20e5d411faceba8d2
1 // Implementation of preferences dialog
2 // Copyright © 2009 The University of Chicago
3 #include "preferencesdialog.h"
5 #include "Config.h"
6 #include <QMessageBox>
7 #include <QFileDialog>
8 #include <QFontDialog>
9 #include <QCheckBox>
10 #include <Q3TextStream>
11 #include <QIODevice>
12 #include <QFile>
13 #include <Q3PtrList>
14 #include <QString>
15 #include "linguisticamainwindow.h"
16 #include "LPreferences.h"
17 #include "ScrubRules.h"
19 const int MAX_REPLACEMENTS = 100;
21 namespace {
22 void LoadDictIntoListView(QMap<QString, QString>* dict, Q3ListView* view )
24 Q3ListViewItem * item, * parent;
25 QString dKey, dVal;
26 int slash;
28 view->clear();
30 StringToString::Iterator it;
31 for( it = dict->begin(); it != dict->end(); ++it )
33 dKey = it.key();
34 dVal = it.data();
36 slash = dKey.find( '\\' );
37 if( slash >= 0 )
39 parent = view->findItem( dKey.left( slash ), 0, Q3ListView::ExactMatch );
40 if( !parent )
42 parent = new Q3ListViewItem( view, dKey.left( slash ) );
43 parent->setOpen( true );
45 item = new Q3ListViewItem( parent, dKey.mid( slash + 1 ), dVal.replace( '_', ' ' ) );
47 else
49 item = new Q3ListViewItem( view, dKey, dVal.replace( '_', ' ' ) );
55 QColor GetColorPreference( CLPreferences* prefs, QString title )
57 QString clrname = prefs->GetPreference( title + "_Color" );
58 if( clrname.length() < 1 ) clrname = "#000000";
59 return QColor( clrname );
63 void SaveScrubRulesToPreferences( PreferencesDialog* dialog, CLPreferences* prefs, bool initialized )
65 Q3ListBoxItem* item = dialog->m_scrubListBox->item(0);
66 QStringList scrubRules;
68 while( item )
70 scrubRules << item->text();
72 if( item->text() == SCR_REMOVE_PRECEDING_PUNCT ) scrubRules << dialog->m_precedingPunctLineEdit->text();
73 else if( item->text() == SCR_REMOVE_INTERNAL_PUNCT ) scrubRules << dialog->m_internalPunctLineEdit->text();
74 else if( item->text() == SCR_REMOVE_FOLLOWING_PUNCT ) scrubRules << dialog->m_followingPunctLineEdit->text();
76 item = item->next();
79 if (initialized)
80 prefs->SetStringListPreference("Scrub_Replacements",
81 scrubRules);
85 PreferencesDialog::PreferencesDialog( CLPreferences* prefs,
86 StringToString* params,
87 QWidget* parent, Qt::WindowFlags fl )
88 : QDialog( parent, 0, false, fl ), Ui::PreferencesDialogBase()
90 setupUi( this );
92 m_Prefs = prefs;
93 m_DefaultParams = params;
94 m_replacements = new StringToString();
96 m_initialized = FALSE;
97 init();
98 m_initialized = TRUE;
101 PreferencesDialog::~PreferencesDialog()
103 if( m_replacements ) delete m_replacements;
107 void PreferencesDialog::init()
109 QString preference = "";
111 QFont font = m_Prefs->GetFontPreference( "Affix" );
112 QColor color = GetColorPreference( m_Prefs, "Affix" );
114 // Morphology Highlighting
115 m_MH_ListBox->setCurrentItem(0);
116 m_MH_PreviewLineEdit->setFont( font );
117 m_MH_PreviewLineEdit->setPointSize( 10 );
118 if( color.isValid() ) m_MH_PreviewLineEdit->setColor( color );
120 m_MH_BoldCheckBox->setChecked( font.bold() );
121 m_MH_ItalicCheckBox->setChecked( font.italic() );
122 m_MH_UnderlineCheckBox->setChecked( font.underline() );
124 // Main font
125 font = m_Prefs->GetFontPreference( "Main" );
126 color = GetColorPreference( m_Prefs, "Main" );
127 m_DSF_PreviewLineEdit->setFont( font );
128 if( color.isValid() ) m_DSF_PreviewLineEdit->setColor( color );
130 // Character Combinations
131 m_CC_ListBox->clear();
132 QStringList combos;
133 m_Prefs->GetStringListPreference( "Character_Combinations", &combos );
134 for( QStringList::Iterator strIt = combos.begin(); strIt != combos.end(); ++strIt )
136 m_CC_ListBox->insertItem( *strIt );
139 // Signature Delimiter
140 preference = m_Prefs->GetPreference( "Sig_Delimiter" );
141 if( preference.length() ) m_SigDelimiterLineEdit->setText( preference );
142 else m_SigDelimiterLineEdit->setText( "." );
144 // Save notifications
145 preference = m_Prefs->GetPreference( "Notifications" );
146 if( preference.length() ) m_NotificationsCheckBox->setChecked( preference.toInt() );
147 else m_NotificationsCheckBox->setChecked( TRUE );
149 // Scrubbing
150 m_scrubListBox->clear();
151 m_makeLowerCaseCheckBox->setChecked( FALSE );
152 m_removeNumbersCheckBox->setChecked( FALSE );
153 m_removePrecedingPunctCheckBox->setChecked( FALSE );
154 m_removeInternalPunctCheckBox->setChecked( FALSE );
155 m_removeFollowingPunctCheckBox->setChecked( FALSE );
157 QStringList scrubRules;
158 m_Prefs->GetStringListPreference( "Scrub_Replacements", &scrubRules );
160 for( QStringList::Iterator it = scrubRules.begin(); it != scrubRules.end(); ++it )
162 if( (*it) == SCR_MAKE_LOWER_CASE ) m_makeLowerCaseCheckBox->toggle();
163 else if( (*it) == SCR_REMOVE_NUMBERS ) m_removeNumbersCheckBox->toggle();
164 else if( (*it) == SCR_REMOVE_PRECEDING_PUNCT )
166 m_removePrecedingPunctCheckBox->toggle();
167 ++it;
168 m_precedingPunctLineEdit->setText( *it );
170 else if( (*it) == SCR_REMOVE_INTERNAL_PUNCT )
172 m_removeInternalPunctCheckBox->toggle();
173 ++it;
174 m_internalPunctLineEdit->setText( *it );
176 else if( (*it) == SCR_REMOVE_FOLLOWING_PUNCT )
178 m_removeFollowingPunctCheckBox->toggle();
179 ++it;
180 m_followingPunctLineEdit->setText( *it );
182 else
184 int arrow = (*it).findRev( " --> " );
185 if( arrow >= 0 )
187 QString replace = (*it).left( arrow );
188 QString with = (*it).right( (*it).length() - arrow - 5 );
189 m_replacements->replace( replace, with );
191 m_scrubListBox->insertItem( *it );
196 // Dash at end of line -- defaults to "Join_Without_Dash"
197 preference = m_Prefs->GetPreference( "Line_Final_Dash" );
198 if( preference == "Join_With_Dash" ) m_joinWithDashRadioButton->setChecked( TRUE );
199 else if( preference == "Do_Not_Join" ) m_doNotJoinRadioButton->setChecked( TRUE );
201 // Dash inside word -- defaults to "Do_Not_Remove" and "Include_Full_Word_Only"
202 preference = m_Prefs->GetPreference( "Word_Internal_Dash" );
203 if( preference == "Remove" ) m_removeHyphenCheckBox->setChecked( TRUE );
204 else m_removeHyphenCheckBox->setChecked( FALSE );
206 preference = m_Prefs->GetPreference( "Word_With_Dash" );
207 if( preference == "Include_Full_Word_Only" )
209 m_includeFullHyphenatedWordOnlyRadioButton->setChecked( TRUE );
211 else if( preference == "Include_Substrings_Only" )
213 m_includeSubstringsOfHyphenatedWordOnlyRadioButton->setChecked( TRUE );
215 else if( preference == "Include_Full_Word_And_Substrings" )
217 m_includeFullHyphenatedWordAndSubstringsRadioButton->setChecked( TRUE );
220 initParams();
222 RedrawPreviews();
226 void PreferencesDialog::initParams()
228 // Build Parameters list
229 StringToString dict;
230 m_Prefs->GetDictionaryPreference( "Lxa_Parameters", &dict );
231 m_paramListView->setRootIsDecorated( true );
232 LoadDictIntoListView( &dict, m_paramListView );
234 // Make values renamable
235 m_paramListView->setDefaultRenameAction( Q3ListView::Accept );
236 m_paramListView->setSelectionMode( Q3ListView::Single );
237 m_paramListView->setResizeMode( Q3ListView::LastColumn );
238 Q3ListViewItemIterator listIt( m_paramListView );
239 while ( listIt.current() )
241 if( listIt.current()->text(1).length() > 0 )
242 listIt.current()->setRenameEnabled( 1, true );
243 ++listIt;
248 void PreferencesDialog::RedrawPreviews()
250 // Morphology Highlighting
251 m_MH_PreviewLineEdit->clear();
252 m_MH_PreviewLineEdit->setPointSize( 10 );
253 m_MH_PreviewLineEdit->insert( "Preview text" );
255 // Default Screen Font
256 QString text = m_DSF_PreviewLineEdit->text();
257 m_DSF_PreviewLineEdit->clear();
258 m_DSF_PreviewLineEdit->insert( text );
263 // SLOTS //////////////////////////////////////////////
265 void PreferencesDialog::mhListBoxHighlightedSlot()
267 QFont font;
268 QColor color;
269 QString itemTxt;
270 Q3ListBoxItem* item = m_MH_ListBox->selectedItem();
272 if( item ) itemTxt = item->text();
273 else itemTxt = "Affix";
275 font = m_Prefs->GetFontPreference( itemTxt );
276 color = GetColorPreference( m_Prefs, itemTxt );
278 m_MH_BoldCheckBox->setChecked( font.bold() );
279 m_MH_ItalicCheckBox->setChecked( font.italic() );
280 m_MH_UnderlineCheckBox->setChecked( font.underline() );
281 m_MH_PreviewLineEdit->setFont( font );
282 m_MH_PreviewLineEdit->setColor( color );
283 RedrawPreviews();
287 void PreferencesDialog::mhBoldCheckBoxClickedSlot()
289 Q3ListBoxItem* item = m_MH_ListBox->selectedItem();
290 if( !item ) return;
292 QFont font = m_Prefs->GetFontPreference( item->text() );
294 font.setBold( m_MH_BoldCheckBox->isChecked() );
295 m_Prefs->SetFontPreference( item->text(), font );
296 m_MH_PreviewLineEdit->setFont( font );
297 RedrawPreviews();
301 void PreferencesDialog::mhItalicCheckBoxClickedSlot()
303 Q3ListBoxItem* item = m_MH_ListBox->selectedItem();
304 if( !item ) return;
306 QFont font = m_Prefs->GetFontPreference( item->text() );
308 font.setItalic( m_MH_ItalicCheckBox->isChecked() );
309 m_Prefs->SetFontPreference( item->text(), font );
310 m_MH_PreviewLineEdit->setFont( font );
311 RedrawPreviews();
315 void PreferencesDialog::mhUnderlineCheckBoxClickedSlot()
317 Q3ListBoxItem* item = m_MH_ListBox->selectedItem();
318 if( !item ) return;
320 QFont font = m_Prefs->GetFontPreference( item->text() );
322 font.setUnderline( m_MH_UnderlineCheckBox->isChecked() );
323 m_Prefs->SetFontPreference( item->text(), font );
324 m_MH_PreviewLineEdit->setFont( font );
325 RedrawPreviews();
328 void PreferencesDialog::mhColorButtonClickedSlot()
330 Q3ListBoxItem* item = m_MH_ListBox->selectedItem();
331 if( !item ) return;
333 QColor color = GetColorPreference( m_Prefs, item->text() );
335 color = QColorDialog::getColor(color);
336 if( color.isValid() )
338 m_MH_PreviewLineEdit->setColor( color );
339 m_Prefs->SetPreference( item->text() + "_Color", color.name() );
340 RedrawPreviews();
345 void PreferencesDialog::dsfFontButtonClickedSlot()
347 bool ok;
348 QFont font = QFontDialog::getFont( &ok, m_Prefs->GetFontPreference( "Main" ) ),
349 temp;
350 if ( ok ) {
351 // font is set to the font the user selected
352 m_Prefs->SetFontPreference( "Main", font );
354 temp = m_Prefs->GetFontPreference( "Affix" );
355 temp.setFamily( font.family() );
356 temp.setPointSize( font.pointSize() );
357 m_Prefs->SetFontPreference( "Affix", temp );
359 temp = m_Prefs->GetFontPreference( "Signature" );
360 temp.setFamily( font.family() );
361 temp.setPointSize( font.pointSize() );
362 m_Prefs->SetFontPreference( "Signature", temp );
364 temp = m_Prefs->GetFontPreference( "Stem" );
365 temp.setFamily( font.family() );
366 temp.setPointSize( font.pointSize() );
367 m_Prefs->SetFontPreference( "Stem", temp );
369 temp = m_Prefs->GetFontPreference( "Word" );
370 temp.setFamily( font.family() );
371 temp.setPointSize( font.pointSize() );
372 m_Prefs->SetFontPreference( "Word", temp );
374 temp = m_Prefs->GetFontPreference( "Error" );
375 temp.setFamily( font.family() );
376 temp.setPointSize( font.pointSize() );
377 m_Prefs->SetFontPreference( "Error", temp );
379 ((LinguisticaMainWindow*)parent())->SetEastFont( font );
380 m_DSF_PreviewLineEdit->setFont( font );
381 RedrawPreviews();
383 else
385 // the user canceled the dialog; font is set to the initial
390 void PreferencesDialog::loadPrefsButtonClickedSlot()
392 QSettings& settings =
393 *static_cast<LinguisticaMainWindow*>(parent())
394 ->GetSettings();
396 const QString lastfile =
397 settings.readEntry(
398 "/linguistica.uchicago.edu/Linguistica/PreferencesFileName",
399 QString());
400 // Dialog for preferences file
401 const QString filename =
402 QFileDialog::getOpenFileName(lastfile,
403 "Text Files( *.txt );;"
404 "Rich Text Files( *.rtf );;"
405 "All Files( *.* )");
406 if (filename.isEmpty())
407 return;
408 if (filename != lastfile)
409 settings.writeEntry(
410 "/linguistica.uchicago.edu/Linguistica/PreferencesFileName",
411 filename);
413 m_Prefs->load_prefs_from_file(filename);
414 init();
417 // XXX. refactor to share code with loadPrefsButtonClickedSlot()
418 void PreferencesDialog::savePrefsButtonClickedSlot()
420 QSettings& settings =
421 *static_cast<LinguisticaMainWindow*>(parent())
422 ->GetSettings();
423 const QString lastfile =
424 settings.readEntry(
425 "/linguistica.uchicago.edu/Linguistica/PreferencesFileName",
426 QString());
427 const QString filename =
428 QFileDialog::getSaveFileName(lastfile,
429 "Text Files( *.txt );;"
430 "Rich Text Files( *.rtf );;"
431 "All Files( *.* )",
432 this,
433 "Choose a file name to save under");
434 if (filename.isEmpty())
435 return;
436 if (filename != lastfile)
437 settings.writeEntry(
438 "/linguistica.uchicago.edu/Linguistica/PreferencesFileName",
439 filename);
440 m_Prefs->store_prefs_to_file(filename);
443 void PreferencesDialog::notificationsCheckBoxClickedSlot()
445 m_Prefs->SetPreference( "Notifications", QString("%1").arg( m_NotificationsCheckBox->isChecked() ) );}
448 void PreferencesDialog::sigDelimiterChangedSlot( const QString& delimiter )
450 m_Prefs->SetPreference( "Sig_Delimiter", delimiter.stripWhiteSpace() );
454 void PreferencesDialog::resetValuePushButtonClickedSlot()
456 Q3ListViewItem* item = m_paramListView->selectedItem();
457 if( item )
459 Q3ListViewItem* parent = item->parent();
461 QString key = item->text(0),
462 value;
463 if( parent ) key = parent->text(0) + "\\" + key;
465 StringToString::Iterator it = m_DefaultParams->find(key);
466 if( it != m_DefaultParams->end() )
468 value = it.data();
469 item->setText( 1, value );
470 paramListItemRenamedSlot(item, 1, value);
476 void PreferencesDialog::resetAllValsPushButtonClickedSlot()
478 m_Prefs->SetDictionaryPreference("Lxa_Parameters", *m_DefaultParams);
479 initParams();
482 void PreferencesDialog::paramListItemRenamedSlot(Q3ListViewItem* item,
483 int /* column */, const QString& value)
485 QMap<QString, QString> dict;
486 m_Prefs->GetDictionaryPreference("Lxa_Parameters", &dict);
488 Q3ListViewItem* parent = item->parent();
490 QString key;
491 if (parent) {
492 key.append(parent->text(0));
493 key.append('\\');
495 key.append(item->text(0));
496 dict.replace(key, value);
498 m_Prefs->SetDictionaryPreference("Lxa_Parameters", dict);
501 void PreferencesDialog::help()
503 Q3WhatsThis::enterWhatsThisMode();
507 void PreferencesDialog::makeLowerCaseCheckBoxToggled(bool b)
509 if( b )
511 m_scrubListBox->insertItem( SCR_MAKE_LOWER_CASE );
513 else
515 m_scrubListBox->removeItem( m_scrubListBox->index( m_scrubListBox->findItem( SCR_MAKE_LOWER_CASE ) ) );
518 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
522 void PreferencesDialog::removeNumbersCheckBoxToggled(bool b)
524 if( b )
526 Q_ASSERT( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_NUMBERS ) ) < 0 );
528 m_scrubListBox->insertItem( SCR_REMOVE_NUMBERS );
530 else
532 Q_ASSERT( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_NUMBERS ) ) >= 0 );
534 m_scrubListBox->removeItem( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_NUMBERS ) ) );
537 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
541 void PreferencesDialog::removePrecedingPunctCheckBoxToggled(bool b)
543 m_precedingPunctLineEdit->setEnabled(b);
545 if( b )
547 Q_ASSERT( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_PRECEDING_PUNCT ) ) < 0 );
549 m_scrubListBox->insertItem( SCR_REMOVE_PRECEDING_PUNCT );
551 else
553 Q_ASSERT( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_PRECEDING_PUNCT ) ) >= 0 );
555 m_scrubListBox->removeItem( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_PRECEDING_PUNCT ) ) );
558 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
562 void PreferencesDialog::removeInternalPunctCheckBoxToggled(bool b)
564 m_internalPunctLineEdit->setEnabled(b);
566 if( b )
568 Q_ASSERT( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_INTERNAL_PUNCT ) ) < 0 );
570 m_scrubListBox->insertItem( SCR_REMOVE_INTERNAL_PUNCT );
572 else
574 Q_ASSERT( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_INTERNAL_PUNCT ) ) >= 0 );
576 m_scrubListBox->removeItem( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_INTERNAL_PUNCT ) ) );
579 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
583 void PreferencesDialog::removeFollowingPunctCheckBoxToggled(bool b)
585 m_followingPunctLineEdit->setEnabled(b);
587 if( b )
589 Q_ASSERT( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_FOLLOWING_PUNCT ) ) < 0 );
591 m_scrubListBox->insertItem( SCR_REMOVE_FOLLOWING_PUNCT );
593 else
595 Q_ASSERT( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_FOLLOWING_PUNCT ) ) >= 0 );
597 m_scrubListBox->removeItem( m_scrubListBox->index( m_scrubListBox->findItem( SCR_REMOVE_FOLLOWING_PUNCT ) ) );
600 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
604 void PreferencesDialog::addNewReplacementButtonPressed()
606 QString replace = m_replaceLineEdit->text();
607 QString with = m_withLineEdit->text();
608 QString replacement = replace + " --> " + with;
610 if( !replace.isEmpty() )
612 if( !(*m_replacements)[ replace ].isEmpty() )
614 QMessageBox::information( this,
615 "Alchemist",
616 "This replacement will not be added. Each regular expression \n"
617 "may only be replaced by one string. The regular expression \n"
618 "already exists." );
619 return;
621 if( static_cast <int> ( m_replacements->count() ) > MAX_REPLACEMENTS )
623 QString message = "This replacement will not be added. The maximum number of\nadvanced replacements allowed is %1.";
624 QMessageBox::information( this,
625 "Alchemist",
626 message.arg( MAX_REPLACEMENTS ) );
627 return;
630 m_scrubListBox->insertItem( replacement );
631 m_replacements->replace( replace, with );
634 m_replaceLineEdit->setText("");
635 m_withLineEdit->setText("");
637 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
641 void PreferencesDialog::moveUpButtonPressed()
643 Q3ListBoxItem* item = m_scrubListBox->item(0),
644 * next;
646 while( item )
648 next = item->next();
650 if( item->isSelected() )
652 if( item->prev() && !item->prev()->isSelected() )
654 int index = m_scrubListBox->index( item );
656 m_scrubListBox->insertItem( item->text(), index - 1 );
657 m_scrubListBox->removeItem( index + 1 );
658 m_scrubListBox->setSelected( index - 1, TRUE );
662 item = next;
665 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
669 void PreferencesDialog::moveDownButtonPressed()
671 Q3ListBoxItem* item = m_scrubListBox->item( m_scrubListBox->count()-1 ),
672 * prev;
674 while( item )
676 prev = item->prev();
678 if( item->isSelected() )
680 if( item->next() && !item->next()->isSelected() )
682 int index = m_scrubListBox->index( item );
684 m_scrubListBox->insertItem( item->text(), index + 2 );
685 m_scrubListBox->removeItem( index );
686 m_scrubListBox->setSelected( index + 1, TRUE );
690 item = prev;
693 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
697 void PreferencesDialog::removeReplacementButtonPressed()
699 Q3PtrList<Q3ListBoxItem> selected;
701 Q3ListBoxItem* item = m_scrubListBox->item(0);
703 while( item )
705 if( item->isSelected() ) selected.append( item );
706 item = item->next();
709 for ( item = selected.first(); item; item = selected.next() )
711 if( item->text() == SCR_MAKE_LOWER_CASE ) m_makeLowerCaseCheckBox->toggle();
712 else if( item->text() == SCR_REMOVE_NUMBERS ) m_removeNumbersCheckBox->toggle();
713 else if( item->text() == SCR_REMOVE_PRECEDING_PUNCT ) m_removePrecedingPunctCheckBox->toggle();
714 else if( item->text() == SCR_REMOVE_INTERNAL_PUNCT ) m_removeInternalPunctCheckBox->toggle();
715 else if( item->text() == SCR_REMOVE_FOLLOWING_PUNCT ) m_removeFollowingPunctCheckBox->toggle();
716 else
718 int arrow = item->text().findRev( " --> " );
719 if( arrow >= 0 ) m_replacements->remove( item->text().left( arrow ) );
721 m_scrubListBox->removeItem( m_scrubListBox->index( item ) );
725 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
729 void PreferencesDialog::replaceStringsChanged()
731 if( m_replaceLineEdit->text().isEmpty() )
733 m_addButton->setEnabled( FALSE );
735 else
737 m_addButton->setEnabled( TRUE );
742 void PreferencesDialog::scrubListBoxSelectionChanged()
744 Q3PtrList<Q3ListBoxItem> selected;
746 Q3ListBoxItem* item = m_scrubListBox->item(0);
748 while( item )
750 if( item->isSelected() ) selected.append( item );
751 item = item->next();
754 if( selected.count() )
756 m_moveUpButton->setEnabled( TRUE );
757 m_moveDownButton->setEnabled( TRUE );
758 m_removeButton->setEnabled( TRUE );
760 else
762 m_moveUpButton->setEnabled( FALSE );
763 m_moveDownButton->setEnabled( FALSE );
764 m_removeButton->setEnabled( FALSE );
769 void PreferencesDialog::precedingPunctLineEditTextChanged(const QString& s)
772 QString temp = s; //just to avoid warning. fix.
773 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
777 void PreferencesDialog::internalPunctLineEditTextChanged(const QString& s)
779 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
780 QString temp = s; //just to avoid warning. fix.
784 void PreferencesDialog::followingPunctLineEditTextChanged(const QString& s)
786 SaveScrubRulesToPreferences( this, m_Prefs, m_initialized );
787 QString temp = s; //just to avoid warning. fix.
791 void PreferencesDialog::importRulesButtonClickedSlot()
793 // Get the last file name used
794 QSettings* settings = ((LinguisticaMainWindow*)parent())->GetSettings();
795 QString loadFileName = settings->readEntry( "/linguistica.uchicago.edu/Linguistica/Scrubbing/File", QString::null );
797 loadFileName = Q3FileDialog::getOpenFileName( loadFileName,
798 "Text Files (*.txt);;Rich Text Files (*.rtf);;All Files (*)",
799 this,
800 "load scrub file dialog",
801 "Choose a scrub file to load" );
803 if( !loadFileName.isEmpty() )
805 QFile file( loadFileName );
806 QString scrubExp; // Scrub expression
808 if ( file.open( QIODevice::ReadOnly ) )
810 settings->writeEntry( "/linguistica.uchicago.edu/Linguistica/Scrubbing/File", loadFileName );
812 Q3TextStream stream( &file );
813 stream.setEncoding( Q3TextStream::Locale );
816 m_scrubListBox->clear();
817 m_replacements->clear();
819 while( !stream.atEnd() )
821 scrubExp = stream.readLine();
823 if( scrubExp == SCR_MAKE_LOWER_CASE ) m_makeLowerCaseCheckBox->setChecked( TRUE );
824 else if( scrubExp == SCR_REMOVE_NUMBERS ) m_removeNumbersCheckBox->setChecked( TRUE );
825 else if( scrubExp == SCR_REMOVE_PRECEDING_PUNCT )
827 m_removePrecedingPunctCheckBox->setChecked( TRUE );
828 m_precedingPunctLineEdit->setText( stream.readLine() );
830 else if( scrubExp == SCR_REMOVE_INTERNAL_PUNCT )
832 m_removeInternalPunctCheckBox->setChecked( TRUE );
833 m_internalPunctLineEdit->setText( stream.readLine() );
835 else if( scrubExp == SCR_REMOVE_FOLLOWING_PUNCT )
837 m_removeFollowingPunctCheckBox->setChecked( TRUE );
838 m_followingPunctLineEdit->setText( stream.readLine() );
840 else
842 int arrow = scrubExp.findRev( " --> " );
843 if( arrow >= 0 )
845 QString replace = scrubExp.left( arrow );
846 QString with = scrubExp.right( scrubExp.length() - arrow - 5 );
847 m_replacements->replace( replace, with );
853 if( !m_scrubListBox->findItem( scrubExp ) ) m_scrubListBox->insertItem( scrubExp );
856 file.close();
860 m_Prefs->SetDictionaryPreference("Scrub_Replacements", *m_replacements);
864 void PreferencesDialog::exportRulesButtonClickedSlot()
866 // Get the save file name
867 QSettings* settings = ((LinguisticaMainWindow*)parent())->GetSettings();
868 QString saveFileName = settings->readEntry( "/linguistica.uchicago.edu/Linguistica/Scrubbing/File", QString::null );
870 saveFileName = Q3FileDialog::getSaveFileName( saveFileName,
871 "Text Files (*.txt);;Rich Text Files (*.rtf);;All Files (*)",
872 this,
873 "save scrub file dialog",
874 "Choose a filename to save the scrub file under" );
876 if( !saveFileName.isEmpty() )
878 QFile file( saveFileName );
880 if ( file.open( QIODevice::WriteOnly ) )
882 settings->writeEntry( "/linguistica.uchicago.edu/Linguistica/Scrubbing/File", saveFileName );
884 Q3TextStream stream( &file );
885 stream.setEncoding( Q3TextStream::Unicode );
887 Q3ListBoxItem* item = m_scrubListBox->item(0);
889 while( item )
891 stream << item->text();
893 if( item->text() == SCR_REMOVE_PRECEDING_PUNCT ) stream << "\n" << m_precedingPunctLineEdit->text();
894 else if( item->text() == SCR_REMOVE_INTERNAL_PUNCT ) stream << "\n" << m_internalPunctLineEdit->text();
895 else if( item->text() == SCR_REMOVE_FOLLOWING_PUNCT ) stream << "\n" << m_followingPunctLineEdit->text();
897 stream << "\n";
899 item = item->next();
902 file.close();
908 void PreferencesDialog::removeComboButtonClickedSlot()
910 Q3PtrList<Q3ListBoxItem> selected;
911 QStringList not_removed;
913 Q3ListBoxItem* item = m_CC_ListBox->item(0);
915 while( item )
917 if( item->isSelected() ) selected.append( item );
918 else
920 not_removed << item->text();
922 item = item->next();
925 for ( item = selected.first(); item; item = selected.next() )
927 m_CC_ListBox->removeItem( m_CC_ListBox->index( item ) );
930 m_Prefs->SetStringListPreference("Character_Combinations",
931 not_removed);
935 void PreferencesDialog::newComboButtonClickedSlot()
937 bool ok;
938 QString label = "Enter a character combination with length greater than one and no white space.\n";
939 label += "A single character will be substituted for the combination during analysis and\n";
940 label += "the combination will be replaced during output.\n";
941 QString text = QInputDialog::getText( "Linguistica",
942 label,
943 QLineEdit::Normal,
944 QString::null, &ok, this );
945 if ( ok && !text.isEmpty() )
947 QRegExp whitespace( "\\s" );
948 if( text.find( whitespace ) >= 0 )
950 QMessageBox::information( this,
951 "Linguistica",
952 "The character combination cannot include whitespace.\n" );
953 return;
955 if( m_CC_ListBox->findItem( text ) )
957 QMessageBox::information( this,
958 "Linguistica",
959 "This character combination already exists in the list.\n" );
960 return;
963 m_CC_ListBox->insertItem( text );
965 QStringList allCombos;
967 Q3ListBoxItem* item = m_CC_ListBox->item(0);
969 while( item )
971 allCombos << item->text();
972 item = item->next();
975 m_Prefs->SetStringListPreference("Character_Combinations",
976 allCombos);
981 void PreferencesDialog::comboListBoxSelectionChangedSlot()
983 Q3PtrList<Q3ListBoxItem> selected;
985 Q3ListBoxItem* item = m_CC_ListBox->item(0);
987 while( item )
989 if( item->isSelected() ) selected.append( item );
990 item = item->next();
993 if( selected.count() )
995 m_CC_RemoveButton->setEnabled( TRUE );
997 else
999 m_CC_RemoveButton->setEnabled( FALSE );
1004 void PreferencesDialog::joinWithoutDashRadioButtonClickedSlot()
1006 m_Prefs->SetPreference( "Line_Final_Dash", "Join_Without_Dash" );
1010 void PreferencesDialog::joinWithDashRadioButtonClickedSlot()
1012 m_Prefs->SetPreference( "Line_Final_Dash", "Join_With_Dash" );
1016 void PreferencesDialog::doNotJoinRadioButtonClickedSlot()
1018 m_Prefs->SetPreference( "Line_Final_Dash", "Do_Not_Join" );
1022 void PreferencesDialog::removeHyphenCheckBoxClickedSlot()
1024 switch( m_removeHyphenCheckBox->checkState() )
1026 case Qt::Checked:
1027 m_Prefs->SetPreference( "Word_Internal_Dash", "Remove" );
1028 break;
1029 case Qt::Unchecked:
1030 m_Prefs->SetPreference( "Word_Internal_Dash", "Do_Not_Remove" );
1031 break;
1032 case Qt::PartiallyChecked:
1033 default:
1034 break;
1039 void PreferencesDialog::includeFullHyphenatedWordOnlyRadioButtonClickedSlot()
1041 m_Prefs->SetPreference( "Word_With_Dash", "Include_Full_Word_Only" );
1045 void PreferencesDialog::includeSubstringsOfHyphenatedWordOnlyRadioButtonClickedSlot()
1047 m_Prefs->SetPreference( "Word_With_Dash", "Include_Substrings_Only" );
1051 void PreferencesDialog::includeFullHyphenatedWordAndSubstringsRadioButtonClickedSlot()
1053 m_Prefs->SetPreference( "Word_With_Dash", "Include_Full_Word_And_Substrings" );