lib

KoSearchDia.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
00003    Copyright (C) 2001, S.R.Haque <srhaque@iee.org>
00004    Copyright (C) 2001, David Faure <faure@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "KoSearchDia.h"
00023 #include "KoTextParag.h"
00024 
00025 #include <KoGlobal.h>
00026 #include <KoTextObject.h>
00027 #include <KoTextView.h>
00028 
00029 #include <kcolorbutton.h>
00030 #include <kcommand.h>
00031 #include <kdebug.h>
00032 #include <kfontcombo.h>
00033 #include <klocale.h>
00034 #include <kseparator.h>
00035 
00036 #include <qbuttongroup.h>
00037 #include <qcheckbox.h>
00038 #include <qcombobox.h>
00039 #include <qradiobutton.h>
00040 #include <qregexp.h>
00041 #include <qspinbox.h>
00042 #include <qlayout.h>
00043 
00044 KoSearchContext::KoSearchContext()
00045 {
00046     m_family = "times";
00047     m_color = Qt::black;
00048     m_backGroundColor = Qt::black;
00049 
00050     m_size = 12;
00051     m_vertAlign = KoTextFormat::AlignNormal;
00052     m_optionsMask = 0;
00053     m_options = KFindDialog::FromCursor | KReplaceDialog::PromptOnReplace;
00054     m_underline = KoTextFormat::U_NONE;
00055     m_strikeOut = KoTextFormat::S_NONE;
00056     m_attribute = KoTextFormat::ATT_NONE;
00057     m_language = QString::null;
00058 }
00059 
00060 KoSearchContext::~KoSearchContext()
00061 {
00062 }
00063 
00064 
00065 KoSearchContextUI::KoSearchContextUI( KoSearchContext *ctx, QWidget *parent )
00066     : QObject(parent), m_ctx(ctx), m_parent(parent)
00067 {
00068     m_bOptionsShown = false;
00069     m_btnShowOptions = new QPushButton( i18n("Show Formatting Options"), parent );
00070     connect( m_btnShowOptions, SIGNAL( clicked() ), SLOT( slotShowOptions() ) );
00071 
00072     m_grid = new QGridLayout( m_parent, 1, 1, 0, 6 );
00073     m_grid->addWidget( m_btnShowOptions, 0, 0 );
00074     m_btnShowOptions->setEnabled( true );
00075 }
00076 
00077 void KoSearchContextUI::slotShowOptions()
00078 {
00079     KoFormatDia * dlg = new KoFormatDia( m_parent, i18n("Formatting Options"), m_ctx );
00080     if ( dlg->exec())
00081     {
00082         dlg->ctxOptions( );
00083         m_bOptionsShown = true;
00084     }
00085 
00086     delete dlg;
00087 }
00088 
00089 void KoSearchContextUI::setCtxOptions( long options )
00090 {
00091     if ( m_bOptionsShown )
00092     {
00093         options |= m_ctx->m_options;
00094     }
00095     m_ctx->m_options = options;
00096 }
00097 
00098 void KoSearchContextUI::setCtxHistory( const QStringList & history )
00099 {
00100     m_ctx->m_strings = history;
00101 }
00102 
00103 KoSearchDia::KoSearchDia( QWidget * parent,const char *name, KoSearchContext *find, bool hasSelection, bool hasCursor )
00104     : KFindDialog( parent, name, find->m_options, find->m_strings )
00105 {
00106     // The dialog extension.
00107     m_findUI = new KoSearchContextUI( find, findExtension() );
00108     setHasSelection(hasSelection);
00109     setHasCursor(hasCursor);
00110 }
00111 
00112 void KoSearchDia::slotOk()
00113 {
00114     KFindDialog::slotOk();
00115 
00116     // Save the current state back into the context required.
00117     if ( optionSelected() )
00118         m_findUI->setCtxOptions( options() );
00119     m_findUI->setCtxHistory( findHistory() );
00120 }
00121 
00122 KoReplaceDia::KoReplaceDia( QWidget *parent, const char *name, KoSearchContext *find, KoSearchContext *replace, bool hasSelection, bool hasCursor )
00123     : KReplaceDialog( parent, name, find->m_options, find->m_strings, replace->m_strings )
00124 {
00125     // The dialog extension.
00126     m_findUI = new KoSearchContextUI( find, findExtension() );
00127     m_replaceUI = new KoSearchContextUI( replace, replaceExtension() );
00128     // Look whether we have a selection, and/or a cursor
00129     setHasSelection(hasSelection);
00130     setHasCursor(hasCursor);
00131 }
00132 
00133 void KoReplaceDia::slotOk()
00134 {
00135     KReplaceDialog::slotOk();
00136 
00137     // Save the current state back into the context required.
00138     m_findUI->setCtxHistory( findHistory() );
00139     if ( optionFindSelected() )
00140         m_findUI->setCtxOptions( KReplaceDialog::options() );
00141 
00142     m_replaceUI->setCtxHistory( replacementHistory() );
00143     if ( optionReplaceSelected() )
00144         m_replaceUI->setCtxOptions( KReplaceDialog::options() );
00145 }
00146 
00147 
00148 
00149 KoFindReplace::KoFindReplace( QWidget * parent, KoSearchDia * dialog, const QValueList<KoTextObject *> & lstObject, KoTextView* textView )
00150     : m_find( new KoTextFind( dialog->pattern(), dialog->options(), this, parent ) ),
00151       m_replace( 0L ),
00152       m_searchContext( *dialog->searchContext() ),
00153       m_replaceContext(),
00154       m_searchContextEnabled( dialog->optionSelected() ),
00155       m_doCounting( true ),
00156       m_macroCmd( 0L ),
00157       m_offset( 0 ),
00158       m_textIterator( lstObject, textView, dialog->options() ),
00159       m_lastTextObjectHighlighted( 0 )
00160 {
00161     connectFind( m_find );
00162 }
00163 
00164 KoFindReplace::KoFindReplace( QWidget * parent, KoReplaceDia * dialog, const QValueList<KoTextObject *> & lstObject, KoTextView* textView )
00165     : m_find( 0L ),
00166       m_replace( new KoTextReplace( dialog->pattern(), dialog->replacement(), dialog->options(), this, parent ) ),
00167       m_searchContext( *dialog->searchContext() ),
00168       m_replaceContext( *dialog->replaceContext() ),
00169       m_searchContextEnabled( dialog->optionFindSelected() ),
00170       m_doCounting( true ),
00171       m_macroCmd( 0L ),
00172       m_offset( 0 ),
00173       m_textIterator( lstObject, textView, dialog->options() ),
00174       m_lastTextObjectHighlighted( 0 )
00175 {
00176     connectFind( m_replace );
00177     connect( m_replace, SIGNAL( replace( const QString &, int , int, int ) ),
00178              this, SLOT( replace( const QString &, int , int,int ) ) );
00179 }
00180 
00181 void KoFindReplace::connectFind( KFind* find )
00182 {
00183     connect( find, SIGNAL( optionsChanged() ),
00184              this, SLOT( optionsChanged() ) );
00185     connect( find, SIGNAL( dialogClosed() ),
00186              this, SLOT( dialogClosed() ) );
00187     // Connect highlight signal to code which handles highlighting
00188     // of found text.
00189     connect( find, SIGNAL( highlight( const QString &, int, int ) ),
00190              this, SLOT( highlight( const QString &, int, int ) ) );
00191     // Connect findNext signal - called when pressing the button in the dialog
00192     connect( find, SIGNAL( findNext() ),
00193              this, SLOT( slotFindNext() ) );
00194     m_bInit = true;
00195     m_currentParagraphModified = false;
00196     m_matchingIndex = -1;
00197 
00198     // Also connect to the textiterator
00199     connect( &m_textIterator, SIGNAL( currentParagraphModified( int, int, int ) ),
00200              this, SLOT( slotCurrentParagraphModified( int, int, int ) ) );
00201 }
00202 
00203 KoFindReplace::~KoFindReplace()
00204 {
00205     removeHighlight();
00206     //kdDebug(32500) << "KoFindReplace::~KoFindReplace" << endl;
00207     delete m_find;
00208     delete m_replace;
00209 }
00210 
00211 void KoFindReplace::optionsChanged()
00212 {
00213     m_textIterator.setOptions( options() );
00214 }
00215 
00216 void KoFindReplace::dialogClosed()
00217 {
00218     removeHighlight();
00219     emitUndoRedo();
00220     // we have to stop the match counting when closing the dialog,
00221     // because Shift-F3 (find previous) would keep increasing that count, wrongly.
00222     m_doCounting = false;
00223 }
00224 
00225 void KoFindReplace::removeHighlight()
00226 {
00227     if ( m_lastTextObjectHighlighted )
00228         m_lastTextObjectHighlighted->removeHighlight(true);
00229     m_lastTextObjectHighlighted = 0L;
00230 }
00231 
00232 void KoFindReplace::emitUndoRedo()
00233 {
00234     // Emit the command containing the replace operations
00235     // #### We allow editing text during a replace operation, so we need
00236     // to call this more often... so that 'undo' undoes the last
00237     // replace operations. TODO!
00238     if(m_macroCmd)
00239         emitNewCommand(m_macroCmd);
00240     m_macroCmd = 0L;
00241 }
00242 
00252 bool KoFindReplace::findNext()
00253 {
00254     KFind::Result res = KFind::NoMatch;
00255     while ( res == KFind::NoMatch && !m_textIterator.atEnd() ) {
00256         //kdDebug(32500) << "findNext loop. m_bInit=" << m_bInit << " needData=" << needData() << " m_currentParagraphModified=" << m_currentParagraphModified << endl;
00257         if ( needData() || m_currentParagraphModified ) {
00258             if ( !m_bInit && !m_currentParagraphModified ) {
00259                 ++m_textIterator;
00260                 if ( m_textIterator.atEnd() )
00261                     break;
00262             }
00263             m_bInit = false;
00264             QPair<int, QString> c = m_textIterator.currentTextAndIndex();
00265             m_offset = c.first;
00266             if ( !m_currentParagraphModified )
00267                 setData( c.second );
00268             else
00269                 setData( c.second, m_matchingIndex );
00270             m_currentParagraphModified = false;
00271         }
00272 
00273         if ( m_find )
00274             // Let KFind inspect the text fragment, and display a dialog if a match is found
00275             res = m_find->find();
00276         else
00277             res = m_replace->replace();
00278     }
00279 
00280     //kdDebug(32500) << k_funcinfo << "we're done. res=" << res << endl;
00281     if ( res == KFind::NoMatch ) // i.e. at end
00282     {
00283         emitUndoRedo();
00284         removeHighlight();
00285         if ( shouldRestart() ) {
00286             m_textIterator.setOptions( m_textIterator.options() & ~KFindDialog::FromCursor );
00287             m_textIterator.restart();
00288             m_bInit = true;
00289             if ( m_find )
00290                 m_find->resetCounts();
00291             else
00292                 m_replace->resetCounts();
00293             return findNext();
00294         }
00295         else { // done, close the 'find next' dialog
00296             if ( m_find )
00297                 m_find->closeFindNextDialog();
00298             else
00299                 m_replace->closeReplaceNextDialog();
00300         }
00301         return false;
00302     }
00303     return true;
00304 }
00305 
00306 void KoFindReplace::slotFindNext() // called by the button in the small "find next?" dialog
00307 {
00308     bool ret = findNext();
00309     Q_UNUSED(ret);
00310 }
00311 
00312 bool KoFindReplace::findPrevious()
00313 {
00314     int opt = options();
00315     bool forw = ! ( options() & KFindDialog::FindBackwards );
00316     if ( forw )
00317         setOptions( opt | KFindDialog::FindBackwards );
00318     else
00319         setOptions( opt & ~KFindDialog::FindBackwards );
00320 
00321     bool ret = findNext();
00322 
00323     setOptions( opt ); // restore initial options
00324 
00325     return ret;
00326 }
00327 
00328 long KoFindReplace::options() const
00329 {
00330     return m_find ? m_find->options() : m_replace->options();
00331 }
00332 
00333 void KoFindReplace::setOptions(long opt)
00334 {
00335     if ( m_find )
00336         m_find->setOptions(opt);
00337     else
00338         m_replace->setOptions(opt);
00339     m_textIterator.setOptions( opt );
00340 }
00341 
00342 void KoFindReplace::slotCurrentParagraphModified( int, int pos, int )
00343 {
00344     if ( pos >= m_offset )
00345         m_currentParagraphModified = true;
00346     // (this bool forces the next findNext() to call setData again)
00347 }
00348 
00349 // slot connected to the 'highlight' signal
00350 void KoFindReplace::highlight( const QString &, int matchingIndex, int matchingLength )
00351 {
00352     m_matchingIndex = matchingIndex;
00353     if ( m_lastTextObjectHighlighted )
00354         m_lastTextObjectHighlighted->removeHighlight(true);
00355     m_lastTextObjectHighlighted = m_textIterator.currentTextObject();
00356     //kdDebug(32500) << "KoFindReplace::highlight " << matchingIndex << "," << matchingLength << endl;
00357     KDialogBase* dialog = m_find ? m_find->findNextDialog() : m_replace->replaceNextDialog();
00358     highlightPortion(m_textIterator.currentParag(), m_offset + matchingIndex, matchingLength, m_lastTextObjectHighlighted->textDocument(), dialog );
00359 }
00360 
00361 // slot connected to the 'replace' signal
00362 void KoFindReplace::replace( const QString &text, int matchingIndex,
00363                              int replacementLength, int matchedLength )
00364 {
00365     //kdDebug(32500) << "KoFindReplace::replace m_offset=" << m_offset << " matchingIndex=" << matchingIndex << " matchedLength=" << matchedLength << " options=" << options() << endl;
00366     m_matchingIndex = matchingIndex;
00367     int index = m_offset + matchingIndex;
00368 
00369     // highlight might not have happened (if 'prompt on replace' is off)
00370     if ( (options() & KReplaceDialog::PromptOnReplace) == 0 )
00371         highlight( text, matchingIndex, matchedLength );
00372 
00373     KoTextObject* currentTextObj = m_textIterator.currentTextObject();
00374     KoTextDocument * textdoc = currentTextObj->textDocument();
00375     KoTextCursor cursor( textdoc );
00376     cursor.setParag( m_textIterator.currentParag() );
00377     cursor.setIndex( index );
00378 
00379     //reactive spellchecking
00380     currentTextObj->setNeedSpellCheck(true);
00381     if ( m_replaceContext.m_optionsMask )
00382     {
00383         replaceWithAttribut( &cursor, index );
00384     }
00385     // Don't repaint if we're doing batch changes
00386     bool repaint = options() & KReplaceDialog::PromptOnReplace;
00387 
00388     // Grab replacement string
00389     QString rep = text.mid( matchingIndex, replacementLength );
00390 
00391     // Don't let the replacement set the paragraph to "modified by user"
00392     disconnect( &m_textIterator, SIGNAL( currentParagraphModified( int, int, int ) ),
00393                 this, SLOT( slotCurrentParagraphModified( int, int, int ) ) );
00394 
00395     KCommand *cmd = currentTextObj->replaceSelectionCommand(
00396         &cursor, rep, QString::null,
00397         KoTextDocument::HighlightSelection,
00398         repaint ? KoTextObject::DefaultInsertFlags : KoTextObject::DoNotRepaint );
00399 
00400     connect( &m_textIterator, SIGNAL( currentParagraphModified( int, int, int ) ),
00401              this, SLOT( slotCurrentParagraphModified( int, int, int ) ) );
00402 
00403     if( cmd )
00404         macroCommand()->addCommand(cmd);
00405 }
00406 
00407 void KoFindReplace::replaceWithAttribut( KoTextCursor * cursor, int index )
00408 {
00409     KoTextFormat * lastFormat = m_textIterator.currentParag()->at( index )->format();
00410     KoTextFormat * newFormat = new KoTextFormat(*lastFormat);
00411     int flags = 0;
00412     if (m_replaceContext.m_optionsMask & KoSearchContext::Bold)
00413     {
00414         flags |= KoTextFormat::Bold;
00415         newFormat->setBold( (bool)(m_replaceContext.m_options & KoSearchContext::Bold) );
00416     }
00417     if (m_replaceContext.m_optionsMask & KoSearchContext::Size)
00418     {
00419         flags |= KoTextFormat::Size;
00420         newFormat->setPointSize( m_replaceContext.m_size );
00421 
00422     }
00423     if ( m_replaceContext.m_optionsMask & KoSearchContext::Family)
00424     {
00425         flags |= KoTextFormat::Family;
00426         newFormat->setFamily( m_replaceContext.m_family );
00427     }
00428     if ( m_replaceContext.m_optionsMask & KoSearchContext::Color)
00429     {
00430         flags |= KoTextFormat::Color;
00431         newFormat->setColor( m_replaceContext.m_color );
00432     }
00433     if ( m_replaceContext.m_optionsMask & KoSearchContext::Italic)
00434     {
00435         flags |= KoTextFormat::Italic;
00436         newFormat->setItalic( (bool)(m_replaceContext.m_options & KoSearchContext::Italic) );
00437     }
00438     if ( m_replaceContext.m_optionsMask & KoSearchContext::Underline)
00439     {
00440         flags |= KoTextFormat::ExtendUnderLine;
00441         newFormat->setUnderlineType( m_replaceContext.m_underline );
00442 
00443     }
00444     if ( m_replaceContext.m_optionsMask & KoSearchContext::VertAlign)
00445     {
00446         flags |= KoTextFormat::VAlign;
00447         newFormat->setVAlign( m_replaceContext.m_vertAlign);
00448     }
00449     if ( m_replaceContext.m_optionsMask & KoSearchContext::StrikeOut)
00450     {
00451         flags |= KoTextFormat::StrikeOut;
00452         newFormat->setStrikeOutType( m_replaceContext.m_strikeOut);
00453     }
00454     if ( m_replaceContext.m_optionsMask & KoSearchContext::BgColor)
00455     {
00456         newFormat->setTextBackgroundColor(m_replaceContext.m_backGroundColor);
00457         flags |= KoTextFormat::TextBackgroundColor;
00458     }
00459     if (m_replaceContext.m_optionsMask & KoSearchContext::Shadow)
00460     {
00461         flags |= KoTextFormat::ShadowText;
00462         // If shadow has been selected, we set a shadow (any shadow) in the new format
00463         if ( m_replaceContext.m_options & KoSearchContext::Shadow )
00464             newFormat->setShadow( 1, 1, Qt::gray );
00465         else
00466             newFormat->setShadow( 0, 0, QColor() );
00467     }
00468     if (m_replaceContext.m_optionsMask & KoSearchContext::WordByWord)
00469     {
00470         flags |= KoTextFormat::WordByWord;
00471         newFormat->setWordByWord( (bool)(m_replaceContext.m_options & KoSearchContext::WordByWord) );
00472     }
00473     if (m_replaceContext.m_optionsMask & KoSearchContext::Language)
00474     {
00475         flags |= KoTextFormat::Language;
00476         newFormat->setLanguage( m_replaceContext.m_language );
00477     }
00478 
00479 
00480     KCommand *cmd = m_textIterator.currentTextObject()->setFormatCommand( cursor, &lastFormat ,newFormat,flags , false, KoTextDocument::HighlightSelection );
00481 
00482     if( cmd )
00483         macroCommand()->addCommand(cmd);
00484 }
00485 
00486 KMacroCommand* KoFindReplace::macroCommand()
00487 {
00488     // Create on demand, to avoid making an empty command
00489     if(!m_macroCmd)
00490         m_macroCmd = new KMacroCommand(i18n("Replace Text"));
00491     return m_macroCmd;
00492 }
00493 
00494 void KoFindReplace::setActiveWindow()
00495 {
00496     KDialogBase* dialog = m_find ? m_find->findNextDialog() : m_replace->replaceNextDialog();
00497     if ( dialog )
00498         dialog->setActiveWindow();
00499 }
00500 
00501 /*int KoFindReplace::numMatches() const
00502 {
00503     return m_find->numMatches();
00504 }
00505 
00506 int KoFindReplace::numReplacements() const
00507 {
00508     return m_replace->numReplacements();
00509 }*/
00510 
00512 
00513 KoTextFind::KoTextFind( const QString &pattern, long options, KoFindReplace *_findReplace, QWidget *parent )
00514     : KFind( pattern, options, parent),
00515       m_findReplace( _findReplace)
00516 {
00517 }
00518 
00519 KoTextFind::~KoTextFind()
00520 {
00521 }
00522 
00523 bool KoTextFind::validateMatch( const QString &text, int index, int matchedlength )
00524 {
00525     return m_findReplace->validateMatch( text, index, matchedlength );
00526 }
00527 
00528 KoTextReplace::KoTextReplace(const QString &pattern, const QString &replacement, long options, KoFindReplace *_findReplace, QWidget *parent )
00529     : KReplace( pattern, replacement, options, parent),
00530       m_findReplace( _findReplace)
00531 {
00532 }
00533 
00534 KoTextReplace::~KoTextReplace()
00535 {
00536 }
00537 
00538 bool KoTextReplace::validateMatch( const QString &text, int index, int matchedlength )
00539 {
00540     return m_findReplace->validateMatch( text, index, matchedlength );
00541 }
00542 
00543 KoFormatDia::KoFormatDia( QWidget* parent, const QString & _caption, KoSearchContext *_ctx ,  const char* name)
00544     : KDialogBase( parent, name, true, _caption, Ok|Cancel|User1 |User2 ),
00545       m_ctx(_ctx)
00546 {
00547     QWidget *page = new QWidget( this );
00548     setMainWidget(page);
00549     setButtonText( KDialogBase::User1, i18n("Reset") );
00550     setButtonText( KDialogBase::User2, i18n("Clear") );
00551 
00552     connect( this, SIGNAL( user1Clicked() ), this, SLOT(slotReset()));
00553     connect( this, SIGNAL( user2Clicked() ), this, SLOT(slotClear()));
00554 
00555     QGridLayout *m_grid = new QGridLayout( page, 15, 2, 0, 6 );
00556     m_checkFamily = new QCheckBox( i18n( "Family:" ),page  );
00557     m_checkSize = new QCheckBox( i18n( "Size:" ), page );
00558     m_checkColor = new QCheckBox( i18n( "Color:" ), page );
00559     m_checkBgColor = new QCheckBox( i18n( "Background color:" ), page );
00560     m_checkBold = new QCheckBox( i18n( "Bold:" ), page );
00561     m_checkItalic = new QCheckBox( i18n( "Italic:" ),page );
00562     m_checkShadow = new QCheckBox( i18n( "Shadow:" ), page );
00563     m_checkWordByWord = new QCheckBox( i18n( "Word by word:" ), page );
00564 
00565     m_checkUnderline = new QCheckBox( i18n( "Underline:" ), page);
00566     m_underlineItem = new QComboBox( page );
00567     // This has to be the type list, not the style list (we need the "no underline" case).
00568     // Of course we could even have both...
00569     m_underlineItem->insertStringList( KoTextFormat::underlineTypeList() );
00570     m_underlineItem->setCurrentItem( (int)m_ctx->m_underline );
00571 
00572     m_checkStrikeOut= new QCheckBox( i18n( "Strikeout:" ), page);
00573 
00574     m_strikeOutItem = new QComboBox( page );
00575     m_strikeOutItem->insertStringList( KoTextFormat::strikeOutTypeList() );
00576     m_strikeOutItem->setCurrentItem( (int)m_ctx->m_strikeOut );
00577 
00578 
00579     m_checkFontAttribute = new QCheckBox( i18n( "Capitalization:" ), page);
00580     m_fontAttributeItem = new QComboBox( page );
00581     m_fontAttributeItem->insertStringList( KoTextFormat::fontAttributeList() );
00582     m_fontAttributeItem->setCurrentItem( (int)m_ctx->m_attribute );
00583 
00584     m_checkLanguage = new QCheckBox( i18n( "Language:" ), page);
00585     m_languageItem = new QComboBox( page );
00586     m_languageItem->insertStringList( KoGlobal::listOfLanguages() );
00587     m_languageItem->setCurrentText( KoGlobal::languageFromTag( m_ctx->m_language ) );
00588 
00589 
00590     m_checkVertAlign = new QCheckBox( i18n( "Vertical alignment:" ), page );
00591 
00592     m_familyItem = new KFontCombo(page);
00593     m_familyItem->setCurrentFont(m_ctx->m_family);
00594 
00595     m_sizeItem = new QSpinBox( 4, 100, 1, page );
00596     m_sizeItem->setValue( m_ctx->m_size );
00597 
00598     m_colorItem = new KColorButton( page );
00599     m_colorItem->setColor( m_ctx->m_color );
00600 
00601     m_bgColorItem = new KColorButton( page );
00602     m_bgColorItem->setColor( m_ctx->m_backGroundColor);
00603 
00604 
00605 
00606     QButtonGroup *grpBold = new QButtonGroup( 1, QGroupBox::Vertical, page );
00607     grpBold->setRadioButtonExclusive( TRUE );
00608     grpBold->layout();
00609     m_boldYes=new QRadioButton( i18n("Yes"), grpBold );
00610     m_boldNo=new QRadioButton( i18n("No"), grpBold );
00611 
00612     QButtonGroup *grpItalic = new QButtonGroup( 1, QGroupBox::Vertical, page );
00613     grpItalic->setRadioButtonExclusive( TRUE );
00614     grpItalic->layout();
00615     m_italicYes=new QRadioButton( i18n("Yes"), grpItalic );
00616     m_italicNo=new QRadioButton( i18n("No"), grpItalic );
00617 
00618     QButtonGroup *grpShadow = new QButtonGroup( 1, QGroupBox::Vertical, page );
00619     grpShadow->setRadioButtonExclusive( TRUE );
00620     grpShadow->layout();
00621     m_shadowYes=new QRadioButton( i18n("Yes"), grpShadow );
00622     m_shadowNo=new QRadioButton( i18n("No"), grpShadow );
00623 
00624     QButtonGroup *grpWordByWord = new QButtonGroup( 1, QGroupBox::Vertical, page );
00625     grpWordByWord->setRadioButtonExclusive( TRUE );
00626     grpWordByWord->layout();
00627     m_wordByWordYes=new QRadioButton( i18n("Yes"), grpWordByWord );
00628     m_wordByWordNo=new QRadioButton( i18n("No"), grpWordByWord );
00629 
00630 
00631     m_vertAlignItem = new QComboBox( false, page );
00632     m_vertAlignItem->insertItem( i18n( "Normal" ), -1 );
00633     m_vertAlignItem->insertItem( i18n( "Subscript" ), -1 );
00634     m_vertAlignItem->insertItem( i18n( "Superscript" ), -1 );
00635     m_vertAlignItem->setCurrentItem( (int)m_ctx->m_vertAlign );
00636 
00637     m_grid->addWidget( m_checkFamily, 1, 0 );
00638     m_grid->addWidget( m_checkSize, 2, 0 );
00639     m_grid->addWidget( m_checkColor, 3, 0 );
00640     m_grid->addWidget( m_checkBgColor, 4, 0);
00641     m_grid->addWidget( m_checkBold, 5, 0 );
00642     m_grid->addWidget( m_checkItalic, 6, 0 );
00643     m_grid->addWidget( m_checkStrikeOut, 7, 0 );
00644     m_grid->addWidget( m_checkUnderline, 8, 0 );
00645     m_grid->addWidget( m_checkVertAlign, 9, 0 );
00646     m_grid->addWidget( m_checkShadow, 10, 0 );
00647     m_grid->addWidget( m_checkWordByWord, 11, 0 );
00648     m_grid->addWidget( m_checkFontAttribute, 12, 0 );
00649 
00650     m_grid->addWidget( m_familyItem, 1, 1 );
00651     m_grid->addWidget( m_sizeItem, 2, 1 );
00652     m_grid->addWidget( m_colorItem, 3, 1 );
00653     m_grid->addWidget( m_bgColorItem, 4, 1);
00654     m_grid->addWidget( grpBold, 5, 1 );
00655     m_grid->addWidget( grpItalic, 6, 1 );
00656 
00657     m_grid->addWidget( m_strikeOutItem, 7, 1 );
00658     m_grid->addWidget( m_underlineItem, 8, 1 );
00659 
00660     m_grid->addWidget( m_vertAlignItem, 9, 1 );
00661     m_grid->addWidget( grpShadow, 10, 1 );
00662     m_grid->addWidget( grpWordByWord, 11, 1 );
00663 
00664     m_grid->addWidget( m_fontAttributeItem, 12, 1);
00665 
00666     m_grid->addWidget( m_checkLanguage, 13, 0);
00667     m_grid->addWidget( m_languageItem, 13, 1);
00668 
00669     KSeparator *tmpSep = new KSeparator( page );
00670     m_grid->addMultiCellWidget( tmpSep, 14, 14, 0, 1 );
00671 
00672     // signals and slots connections
00673     QObject::connect( m_checkFamily, SIGNAL( toggled( bool ) ), m_familyItem, SLOT( setEnabled( bool ) ) );
00674     QObject::connect( m_checkSize, SIGNAL( toggled( bool ) ), m_sizeItem, SLOT( setEnabled( bool ) ) );
00675     QObject::connect( m_checkColor, SIGNAL( toggled( bool ) ), m_colorItem, SLOT( setEnabled( bool ) ) );
00676     QObject::connect( m_checkBgColor, SIGNAL( toggled( bool ) ), m_bgColorItem, SLOT( setEnabled( bool ) ) );
00677 
00678     QObject::connect( m_checkBold, SIGNAL( toggled( bool ) ), m_boldYes, SLOT( setEnabled( bool ) ) );
00679     QObject::connect( m_checkItalic, SIGNAL( toggled( bool ) ), m_italicYes, SLOT( setEnabled( bool ) ) );
00680     QObject::connect( m_checkStrikeOut, SIGNAL( toggled( bool ) ), m_strikeOutItem, SLOT( setEnabled( bool ) ) );
00681     QObject::connect( m_checkShadow, SIGNAL( toggled( bool ) ), m_shadowYes, SLOT( setEnabled( bool ) ) );
00682     QObject::connect( m_checkWordByWord, SIGNAL( toggled( bool ) ), m_wordByWordYes, SLOT( setEnabled( bool ) ) );
00683     QObject::connect( m_checkFontAttribute, SIGNAL( toggled( bool ) ), m_fontAttributeItem, SLOT( setEnabled( bool ) ) );
00684     QObject::connect( m_checkLanguage, SIGNAL( toggled( bool ) ), m_languageItem, SLOT( setEnabled( bool ) ) );
00685 
00686 
00687     QObject::connect( m_checkBold, SIGNAL( toggled( bool ) ), m_boldNo, SLOT( setEnabled( bool ) ) );
00688     QObject::connect( m_checkItalic, SIGNAL( toggled( bool ) ), m_italicNo, SLOT( setEnabled( bool ) ) );
00689     QObject::connect( m_checkShadow, SIGNAL( toggled( bool ) ), m_shadowNo, SLOT( setEnabled( bool ) ) );
00690     QObject::connect( m_checkWordByWord, SIGNAL( toggled( bool ) ), m_wordByWordNo, SLOT( setEnabled( bool ) ) );
00691 
00692 
00693     QObject::connect( m_checkVertAlign, SIGNAL( toggled( bool ) ), m_vertAlignItem, SLOT( setEnabled( bool ) ) );
00694 
00695     QObject::connect( m_checkUnderline, SIGNAL( toggled( bool ) ), m_underlineItem, SLOT( setEnabled( bool ) ) );
00696 
00697     slotReset();
00698 }
00699 
00700 void KoFormatDia::slotClear()
00701 {
00702     m_ctx->m_optionsMask = 0;
00703     m_ctx->m_options = 0;
00704     slotReset();
00705 }
00706 
00707 void KoFormatDia::slotReset()
00708 {
00709     m_checkFamily->setChecked( m_ctx->m_optionsMask & KoSearchContext::Family );
00710     m_familyItem->setEnabled(m_checkFamily->isChecked());
00711 
00712     m_checkSize->setChecked( m_ctx->m_optionsMask & KoSearchContext::Size );
00713     m_sizeItem->setEnabled(m_checkSize->isChecked());
00714 
00715     m_checkColor->setChecked( m_ctx->m_optionsMask & KoSearchContext::Color );
00716     m_colorItem->setEnabled(m_checkColor->isChecked());
00717 
00718     m_checkBgColor->setChecked( m_ctx->m_optionsMask & KoSearchContext::BgColor );
00719     m_bgColorItem->setEnabled(m_checkBgColor->isChecked());
00720 
00721 
00722     m_checkBold->setChecked( m_ctx->m_optionsMask & KoSearchContext::Bold );
00723     m_boldYes->setEnabled(m_checkBold->isChecked());
00724     m_boldNo->setEnabled(m_checkBold->isChecked());
00725 
00726     m_checkShadow->setChecked( m_ctx->m_optionsMask & KoSearchContext::Shadow );
00727     m_shadowYes->setEnabled(m_checkShadow->isChecked());
00728     m_shadowNo->setEnabled(m_checkShadow->isChecked());
00729 
00730     m_checkWordByWord->setChecked( m_ctx->m_optionsMask & KoSearchContext::WordByWord );
00731     m_wordByWordYes->setEnabled(m_checkWordByWord->isChecked());
00732     m_wordByWordNo->setEnabled(m_checkWordByWord->isChecked());
00733 
00734 
00735     m_checkStrikeOut->setChecked( m_ctx->m_optionsMask & KoSearchContext::StrikeOut );
00736     m_strikeOutItem->setEnabled( m_checkStrikeOut->isChecked());
00737 
00738 
00739     m_checkItalic->setChecked( m_ctx->m_optionsMask & KoSearchContext::Italic );
00740     m_italicNo->setEnabled(m_checkItalic->isChecked());
00741     m_italicYes->setEnabled(m_checkItalic->isChecked());
00742 
00743     m_checkUnderline->setChecked( m_ctx->m_optionsMask & KoSearchContext::Underline );
00744     m_underlineItem->setEnabled(m_checkUnderline->isChecked());
00745 
00746     m_checkVertAlign->setChecked( m_ctx->m_optionsMask & KoSearchContext::VertAlign );
00747     m_vertAlignItem->setEnabled(m_checkVertAlign->isChecked());
00748 
00749     m_checkFontAttribute->setChecked( m_ctx->m_optionsMask & KoSearchContext::Attribute );
00750     m_fontAttributeItem->setEnabled(m_checkFontAttribute->isChecked());
00751 
00752 
00753     m_checkLanguage->setChecked( m_ctx->m_optionsMask & KoSearchContext::Language );
00754     m_languageItem->setEnabled(m_checkLanguage->isChecked());
00755 
00756 
00757     if (m_ctx->m_options & KoSearchContext::Bold)
00758         m_boldYes->setChecked( true );
00759     else
00760         m_boldNo->setChecked( true );
00761 
00762     if (m_ctx->m_options & KoSearchContext::Italic)
00763         m_italicYes->setChecked( true );
00764     else
00765         m_italicNo->setChecked( true );
00766 
00767     if (m_ctx->m_options & KoSearchContext::Shadow)
00768         m_shadowYes->setChecked( true );
00769     else
00770         m_shadowNo->setChecked( true );
00771 
00772     if (m_ctx->m_options & KoSearchContext::WordByWord)
00773         m_wordByWordYes->setChecked( true );
00774     else
00775         m_wordByWordNo->setChecked( true );
00776 
00777 }
00778 
00779 void KoFormatDia::ctxOptions( )
00780 {
00781     long optionsMask = 0;
00782     long options = 0;
00783     if ( m_checkFamily->isChecked() )
00784         optionsMask |= KoSearchContext::Family;
00785     if ( m_checkSize->isChecked() )
00786         optionsMask |= KoSearchContext::Size;
00787     if ( m_checkColor->isChecked() )
00788         optionsMask |= KoSearchContext::Color;
00789     if ( m_checkBgColor->isChecked() )
00790         optionsMask |= KoSearchContext::BgColor;
00791     if ( m_checkBold->isChecked() )
00792         optionsMask |= KoSearchContext::Bold;
00793     if ( m_checkItalic->isChecked() )
00794         optionsMask |= KoSearchContext::Italic;
00795     if ( m_checkUnderline->isChecked() )
00796         optionsMask |= KoSearchContext::Underline;
00797     if ( m_checkVertAlign->isChecked() )
00798         optionsMask |= KoSearchContext::VertAlign;
00799     if ( m_checkStrikeOut->isChecked() )
00800         optionsMask |= KoSearchContext::StrikeOut;
00801     if ( m_checkShadow->isChecked() )
00802         optionsMask |= KoSearchContext::Shadow;
00803     if ( m_checkWordByWord->isChecked() )
00804         optionsMask |= KoSearchContext::WordByWord;
00805     if ( m_checkLanguage->isChecked() )
00806         optionsMask |= KoSearchContext::Language;
00807 
00808 
00809     if ( m_boldYes->isChecked() )
00810         options |= KoSearchContext::Bold;
00811     if ( m_italicYes->isChecked() )
00812         options |= KoSearchContext::Italic;
00813     if ( m_shadowYes->isChecked() )
00814         options |= KoSearchContext::Shadow;
00815     if ( m_wordByWordYes->isChecked() )
00816         options |= KoSearchContext::WordByWord;
00817 
00818 
00819     m_ctx->m_optionsMask = optionsMask;
00820     m_ctx->m_family = m_familyItem->currentText();
00821     m_ctx->m_size = m_sizeItem->cleanText().toInt();
00822     m_ctx->m_color = m_colorItem->color();
00823     m_ctx->m_backGroundColor = m_bgColorItem->color();
00824     m_ctx->m_vertAlign = (KoTextFormat::VerticalAlignment)m_vertAlignItem->currentItem();
00825     m_ctx->m_underline = (KoTextFormat::UnderlineType)m_underlineItem->currentItem();
00826     m_ctx->m_strikeOut = (KoTextFormat::StrikeOutType)m_strikeOutItem->currentItem();
00827     m_ctx->m_attribute = (KoTextFormat::AttributeStyle)m_fontAttributeItem->currentItem();
00828     m_ctx->m_language = KoGlobal::listTagOfLanguages()[m_languageItem->currentItem()];
00829 
00830     m_ctx->m_options = options;
00831 }
00832 
00833 
00834 bool KoFindReplace::validateMatch( const QString & /*text*/, int index, int matchedlength )
00835 {
00836     if ( !m_searchContextEnabled || !m_searchContext.m_optionsMask )
00837         return true;
00838     KoTextString * s = currentParag()->string();
00839     for ( int i = index ; i < index+matchedlength ; ++i )
00840     {
00841         KoTextStringChar & ch = s->at(i);
00842         KoTextFormat *format = ch.format();
00843         if (m_searchContext.m_optionsMask & KoSearchContext::Bold)
00844         {
00845             if ( (!format->font().bold() && (m_searchContext.m_options & KoSearchContext::Bold)) || (format->font().bold() && ((m_searchContext.m_options & KoSearchContext::Bold)==0)))
00846                 return false;
00847         }
00848         if (m_searchContext.m_optionsMask & KoSearchContext::Shadow)
00849         {
00850             bool hasShadow = format->shadowDistanceX() != 0 || format->shadowDistanceY() != 0;
00851             if ( (!hasShadow && (m_searchContext.m_options & KoSearchContext::Shadow))
00852                  || (hasShadow && ((m_searchContext.m_options & KoSearchContext::Shadow)==0)) )
00853                 return false;
00854         }
00855 
00856         if (m_searchContext.m_optionsMask & KoSearchContext::WordByWord)
00857         {
00858             if ( (!format->wordByWord() && (m_searchContext.m_options & KoSearchContext::WordByWord)) || (format->wordByWord() && ((m_searchContext.m_options & KoSearchContext::WordByWord)==0)))
00859                 return false;
00860         }
00861 
00862 
00863         if (m_searchContext.m_optionsMask & KoSearchContext::Size)
00864         {
00865             if ( format->font().pointSize() != m_searchContext.m_size )
00866                 return false;
00867         }
00868         if ( m_searchContext.m_optionsMask & KoSearchContext::Family)
00869         {
00870             if (format->font().family() != m_searchContext.m_family)
00871                 return false;
00872         }
00873         if ( m_searchContext.m_optionsMask & KoSearchContext::Color)
00874         {
00875             if (format->color() != m_searchContext.m_color)
00876                 return false;
00877         }
00878         if ( m_searchContext.m_optionsMask & KoSearchContext::BgColor)
00879         {
00880             if (format->textBackgroundColor() != m_searchContext.m_backGroundColor)
00881                 return false;
00882         }
00883 
00884         if ( m_searchContext.m_optionsMask & KoSearchContext::Italic)
00885         {
00886             if ( (!format->font().italic() && (m_searchContext.m_options & KoSearchContext::Italic)) || (format->font().italic() && ((m_searchContext.m_options & KoSearchContext::Italic)==0)))
00887                 return false;
00888 
00889         }
00890         if ( m_searchContext.m_optionsMask & KoSearchContext::Underline)
00891         {
00892             if ( format->underlineType() != m_searchContext.m_underline )
00893                 return false;
00894         }
00895         if ( m_searchContext.m_optionsMask & KoSearchContext::StrikeOut)
00896         {
00897             if ( format->strikeOutType() != m_searchContext.m_strikeOut )
00898                 return false;
00899         }
00900 
00901         if ( m_searchContext.m_optionsMask & KoSearchContext::VertAlign)
00902         {
00903             if ( format->vAlign() != m_searchContext.m_vertAlign )
00904                 return false;
00905         }
00906         if ( m_searchContext.m_optionsMask & KoSearchContext::Language)
00907         {
00908             if ( format->language() != m_searchContext.m_language )
00909                 return false;
00910         }
00911 
00912         if ( m_searchContext.m_optionsMask & KoSearchContext::Attribute)
00913         {
00914             if ( format->attributeFont() != m_searchContext.m_attribute )
00915                 return false;
00916         }
00917 
00918     }
00919     return true;
00920 }
00921 
00922 bool KoFindReplace::shouldRestart()
00923 {
00924     if ( m_find )
00925         return m_find->shouldRestart( true /*since text is editable*/, m_doCounting );
00926     else
00927         return m_replace->shouldRestart( true /*since text is editable*/, m_doCounting );
00928 }
00929 
00930 #include "KoSearchDia.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys