lib Library API Documentation

koSearchDia.cc

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