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 #include "kotextparag.h"
00024 
00025 #include <koGlobal.h>
00026 #include <kotextobject.h>
00027 #include <kotextview.h>
00028 #include <kozoomhandler.h>
00029 
00030 #include <kcolorbutton.h>
00031 #include <kcommand.h>
00032 #include <kdebug.h>
00033 #include <kfontcombo.h>
00034 #include <klocale.h>
00035 #include <kseparator.h>
00036 
00037 #include <qbuttongroup.h>
00038 #include <qcheckbox.h>
00039 #include <qcombobox.h>
00040 #include <qradiobutton.h>
00041 #include <qregexp.h>
00042 #include <qspinbox.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->setCtxOptions( KReplaceDialog::options() );
00139     if ( optionFindSelected() )
00140         m_findUI->setCtxHistory( findHistory() );
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     emitUndoRedo();
00207     //kdDebug(32500) << "KoFindReplace::~KoFindReplace" << endl;
00208     delete m_find;
00209     delete m_replace;
00210 }
00211 
00212 void KoFindReplace::optionsChanged()
00213 {
00214     m_textIterator.setOptions( options() );
00215 }
00216 
00217 void KoFindReplace::dialogClosed()
00218 {
00219     removeHighlight();
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(&cursor, rep, KoTextObject::HighlightSelection, QString::null, repaint );
00396 
00397     connect( &m_textIterator, SIGNAL( currentParagraphModified( int, int, int ) ),
00398              this, SLOT( slotCurrentParagraphModified( int, int, int ) ) );
00399 
00400     if( cmd )
00401         macroCommand()->addCommand(cmd);
00402 }
00403 
00404 void KoFindReplace::replaceWithAttribut( KoTextCursor * cursor, int index )
00405 {
00406     KoTextFormat * lastFormat = m_textIterator.currentParag()->at( index )->format();
00407     KoTextFormat * newFormat = new KoTextFormat(*lastFormat);
00408     int flags = 0;
00409     if (m_replaceContext.m_optionsMask & KoSearchContext::Bold)
00410     {
00411         flags |= KoTextFormat::Bold;
00412         newFormat->setBold( (bool)(m_replaceContext.m_options & KoSearchContext::Bold) );
00413     }
00414     if (m_replaceContext.m_optionsMask & KoSearchContext::Size)
00415     {
00416         flags |= KoTextFormat::Size;
00417         newFormat->setPointSize( m_replaceContext.m_size );
00418 
00419     }
00420     if ( m_replaceContext.m_optionsMask & KoSearchContext::Family)
00421     {
00422         flags |= KoTextFormat::Family;
00423         newFormat->setFamily( m_replaceContext.m_family );
00424     }
00425     if ( m_replaceContext.m_optionsMask & KoSearchContext::Color)
00426     {
00427         flags |= KoTextFormat::Color;
00428         newFormat->setColor( m_replaceContext.m_color );
00429     }
00430     if ( m_replaceContext.m_optionsMask & KoSearchContext::Italic)
00431     {
00432         flags |= KoTextFormat::Italic;
00433         newFormat->setItalic( (bool)(m_replaceContext.m_options & KoSearchContext::Italic) );
00434     }
00435     if ( m_replaceContext.m_optionsMask & KoSearchContext::Underline)
00436     {
00437         flags |= KoTextFormat::ExtendUnderLine;
00438         newFormat->setUnderlineType( m_replaceContext.m_underline );
00439 
00440     }
00441     if ( m_replaceContext.m_optionsMask & KoSearchContext::VertAlign)
00442     {
00443         flags |= KoTextFormat::VAlign;
00444         newFormat->setVAlign( m_replaceContext.m_vertAlign);
00445     }
00446     if ( m_replaceContext.m_optionsMask & KoSearchContext::StrikeOut)
00447     {
00448         flags |= KoTextFormat::StrikeOut;
00449         newFormat->setStrikeOutType( m_replaceContext.m_strikeOut);
00450     }
00451     if ( m_replaceContext.m_optionsMask & KoSearchContext::BgColor)
00452     {
00453         newFormat->setTextBackgroundColor(m_replaceContext.m_backGroundColor);
00454         flags |= KoTextFormat::TextBackgroundColor;
00455     }
00456     if (m_replaceContext.m_optionsMask & KoSearchContext::Shadow)
00457     {
00458         flags |= KoTextFormat::ShadowText;
00459         // If shadow has been selected, we set a shadow (any shadow) in the new format
00460         if ( m_replaceContext.m_options & KoSearchContext::Shadow )
00461             newFormat->setShadow( 1, 1, Qt::gray );
00462         else
00463             newFormat->setShadow( 0, 0, QColor() );
00464     }
00465     if (m_replaceContext.m_optionsMask & KoSearchContext::WordByWord)
00466     {
00467         flags |= KoTextFormat::WordByWord;
00468         newFormat->setWordByWord( (bool)(m_replaceContext.m_options & KoSearchContext::WordByWord) );
00469     }
00470     if (m_replaceContext.m_optionsMask & KoSearchContext::Language)
00471     {
00472         flags |= KoTextFormat::Language;
00473         newFormat->setLanguage( m_replaceContext.m_language );
00474     }
00475 
00476 
00477     KCommand *cmd = m_textIterator.currentTextObject()->setFormatCommand( cursor, &lastFormat ,newFormat,flags , false, KoTextObject::HighlightSelection );
00478 
00479     if( cmd )
00480         macroCommand()->addCommand(cmd);
00481 }
00482 
00483 KMacroCommand* KoFindReplace::macroCommand()
00484 {
00485     // Create on demand, to avoid making an empty command
00486     if(!m_macroCmd)
00487         m_macroCmd = new KMacroCommand(i18n("Replace Text"));
00488     return m_macroCmd;
00489 }
00490 
00491 void KoFindReplace::setActiveWindow()
00492 {
00493     KDialogBase* dialog = m_find ? m_find->findNextDialog() : m_replace->replaceNextDialog();
00494     if ( dialog )
00495         dialog->setActiveWindow();
00496 }
00497 
00498 /*int KoFindReplace::numMatches() const
00499 {
00500     return m_find->numMatches();
00501 }
00502 
00503 int KoFindReplace::numReplacements() const
00504 {
00505     return m_replace->numReplacements();
00506 }*/
00507 
00509 
00510 KoTextFind::KoTextFind( const QString &pattern, long options, KoFindReplace *_findReplace, QWidget *parent )
00511     : KFind( pattern, options, parent),
00512       m_findReplace( _findReplace)
00513 {
00514 }
00515 
00516 KoTextFind::~KoTextFind()
00517 {
00518 }
00519 
00520 bool KoTextFind::validateMatch( const QString &text, int index, int matchedlength )
00521 {
00522     return m_findReplace->validateMatch( text, index, matchedlength );
00523 }
00524 
00525 KoTextReplace::KoTextReplace(const QString &pattern, const QString &replacement, long options, KoFindReplace *_findReplace, QWidget *parent )
00526     : KReplace( pattern, replacement, options, parent),
00527       m_findReplace( _findReplace)
00528 {
00529 }
00530 
00531 KoTextReplace::~KoTextReplace()
00532 {
00533 }
00534 
00535 bool KoTextReplace::validateMatch( const QString &text, int index, int matchedlength )
00536 {
00537     return m_findReplace->validateMatch( text, index, matchedlength );
00538 }
00539 
00540 KoFormatDia::KoFormatDia( QWidget* parent, const QString & _caption, KoSearchContext *_ctx ,  const char* name)
00541     : KDialogBase( parent, name, true, _caption, Ok|Cancel|User1 |User2 ),
00542       m_ctx(_ctx)
00543 {
00544     QWidget *page = new QWidget( this );
00545     setMainWidget(page);
00546     setButtonText( KDialogBase::User1, i18n("Reset") );
00547     setButtonText( KDialogBase::User2, i18n("Clear") );
00548 
00549     connect( this, SIGNAL( user1Clicked() ), this, SLOT(slotReset()));
00550     connect( this, SIGNAL( user2Clicked() ), this, SLOT(slotClear()));
00551 
00552     QGridLayout *m_grid = new QGridLayout( page, 15, 2, 0, 6 );
00553     m_checkFamily = new QCheckBox( i18n( "Family:" ),page  );
00554     m_checkSize = new QCheckBox( i18n( "Size:" ), page );
00555     m_checkColor = new QCheckBox( i18n( "Color:" ), page );
00556     m_checkBgColor = new QCheckBox( i18n( "Background color:" ), page );
00557     m_checkBold = new QCheckBox( i18n( "Bold:" ), page );
00558     m_checkItalic = new QCheckBox( i18n( "Italic:" ),page );
00559     m_checkShadow = new QCheckBox( i18n( "Shadow:" ), page );
00560     m_checkWordByWord = new QCheckBox( i18n( "Word by word:" ), page );
00561 
00562     m_checkUnderline = new QCheckBox( i18n( "Underline:" ), page);
00563     m_underlineItem = new QComboBox( page );
00564     // This has to be the type list, not the style list (we need the "no underline" case).
00565     // Of course we could even have both...
00566     m_underlineItem->insertStringList( KoTextFormat::underlineTypeList() );
00567     m_underlineItem->setCurrentItem( (int)m_ctx->m_underline );
00568 
00569     m_checkStrikeOut= new QCheckBox( i18n( "Strikeout:" ), page);
00570 
00571     m_strikeOutItem = new QComboBox( page );
00572     m_strikeOutItem->insertStringList( KoTextFormat::strikeOutTypeList() );
00573     m_strikeOutItem->setCurrentItem( (int)m_ctx->m_strikeOut );
00574 
00575 
00576     m_checkFontAttribute = new QCheckBox( i18n( "Capitalization:" ), page);
00577     m_fontAttributeItem = new QComboBox( page );
00578     m_fontAttributeItem->insertStringList( KoTextFormat::fontAttributeList() );
00579     m_fontAttributeItem->setCurrentItem( (int)m_ctx->m_attribute );
00580 
00581     m_checkLanguage = new QCheckBox( i18n( "Language:" ), page);
00582     m_languageItem = new QComboBox( page );
00583     m_languageItem->insertStringList( KoGlobal::listOfLanguages() );
00584     m_languageItem->setCurrentText( KoGlobal::languageFromTag( m_ctx->m_language ) );
00585 
00586 
00587     m_checkVertAlign = new QCheckBox( i18n( "Vertical alignment:" ), page );
00588 
00589     m_familyItem = new KFontCombo(page);
00590     m_familyItem->setCurrentFont(m_ctx->m_family);
00591 
00592     m_sizeItem = new QSpinBox( 4, 100, 1, page );
00593     m_sizeItem->setValue( m_ctx->m_size );
00594 
00595     m_colorItem = new KColorButton( page );
00596     m_colorItem->setColor( m_ctx->m_color );
00597 
00598     m_bgColorItem = new KColorButton( page );
00599     m_bgColorItem->setColor( m_ctx->m_backGroundColor);
00600 
00601 
00602 
00603     QButtonGroup *grpBold = new QButtonGroup( 1, QGroupBox::Vertical, page );
00604     grpBold->setRadioButtonExclusive( TRUE );
00605     grpBold->layout();
00606     m_boldYes=new QRadioButton( i18n("Yes"), grpBold );
00607     m_boldNo=new QRadioButton( i18n("No"), grpBold );
00608 
00609     QButtonGroup *grpItalic = new QButtonGroup( 1, QGroupBox::Vertical, page );
00610     grpItalic->setRadioButtonExclusive( TRUE );
00611     grpItalic->layout();
00612     m_italicYes=new QRadioButton( i18n("Yes"), grpItalic );
00613     m_italicNo=new QRadioButton( i18n("No"), grpItalic );
00614 
00615     QButtonGroup *grpShadow = new QButtonGroup( 1, QGroupBox::Vertical, page );
00616     grpShadow->setRadioButtonExclusive( TRUE );
00617     grpShadow->layout();
00618     m_shadowYes=new QRadioButton( i18n("Yes"), grpShadow );
00619     m_shadowNo=new QRadioButton( i18n("No"), grpShadow );
00620 
00621     QButtonGroup *grpWordByWord = new QButtonGroup( 1, QGroupBox::Vertical, page );
00622     grpWordByWord->setRadioButtonExclusive( TRUE );
00623     grpWordByWord->layout();
00624     m_wordByWordYes=new QRadioButton( i18n("Yes"), grpWordByWord );
00625     m_wordByWordNo=new QRadioButton( i18n("No"), grpWordByWord );
00626 
00627 
00628     m_vertAlignItem = new QComboBox( false, page );
00629     m_vertAlignItem->insertItem( i18n( "Normal" ), -1 );
00630     m_vertAlignItem->insertItem( i18n( "Subscript" ), -1 );
00631     m_vertAlignItem->insertItem( i18n( "Superscript" ), -1 );
00632     m_vertAlignItem->setCurrentItem( (int)m_ctx->m_vertAlign );
00633 
00634     m_grid->addWidget( m_checkFamily, 1, 0 );
00635     m_grid->addWidget( m_checkSize, 2, 0 );
00636     m_grid->addWidget( m_checkColor, 3, 0 );
00637     m_grid->addWidget( m_checkBgColor, 4, 0);
00638     m_grid->addWidget( m_checkBold, 5, 0 );
00639     m_grid->addWidget( m_checkItalic, 6, 0 );
00640     m_grid->addWidget( m_checkStrikeOut, 7, 0 );
00641     m_grid->addWidget( m_checkUnderline, 8, 0 );
00642     m_grid->addWidget( m_checkVertAlign, 9, 0 );
00643     m_grid->addWidget( m_checkShadow, 10, 0 );
00644     m_grid->addWidget( m_checkWordByWord, 11, 0 );
00645     m_grid->addWidget( m_checkFontAttribute, 12, 0 );
00646 
00647     m_grid->addWidget( m_familyItem, 1, 1 );
00648     m_grid->addWidget( m_sizeItem, 2, 1 );
00649     m_grid->addWidget( m_colorItem, 3, 1 );
00650     m_grid->addWidget( m_bgColorItem, 4, 1);
00651     m_grid->addWidget( grpBold, 5, 1 );
00652     m_grid->addWidget( grpItalic, 6, 1 );
00653 
00654     m_grid->addWidget( m_strikeOutItem, 7, 1 );
00655     m_grid->addWidget( m_underlineItem, 8, 1 );
00656 
00657     m_grid->addWidget( m_vertAlignItem, 9, 1 );
00658     m_grid->addWidget( grpShadow, 10, 1 );
00659     m_grid->addWidget( grpWordByWord, 11, 1 );
00660 
00661     m_grid->addWidget( m_fontAttributeItem, 12, 1);
00662 
00663     m_grid->addWidget( m_checkLanguage, 13, 0);
00664     m_grid->addWidget( m_languageItem, 13, 1);
00665 
00666     KSeparator *tmpSep = new KSeparator( page );
00667     m_grid->addMultiCellWidget( tmpSep, 14, 14, 0, 1 );
00668 
00669     // signals and slots connections
00670     QObject::connect( m_checkFamily, SIGNAL( toggled( bool ) ), m_familyItem, SLOT( setEnabled( bool ) ) );
00671     QObject::connect( m_checkSize, SIGNAL( toggled( bool ) ), m_sizeItem, SLOT( setEnabled( bool ) ) );
00672     QObject::connect( m_checkColor, SIGNAL( toggled( bool ) ), m_colorItem, SLOT( setEnabled( bool ) ) );
00673     QObject::connect( m_checkBgColor, SIGNAL( toggled( bool ) ), m_bgColorItem, SLOT( setEnabled( bool ) ) );
00674 
00675     QObject::connect( m_checkBold, SIGNAL( toggled( bool ) ), m_boldYes, SLOT( setEnabled( bool ) ) );
00676     QObject::connect( m_checkItalic, SIGNAL( toggled( bool ) ), m_italicYes, SLOT( setEnabled( bool ) ) );
00677     QObject::connect( m_checkStrikeOut, SIGNAL( toggled( bool ) ), m_strikeOutItem, SLOT( setEnabled( bool ) ) );
00678     QObject::connect( m_checkShadow, SIGNAL( toggled( bool ) ), m_shadowYes, SLOT( setEnabled( bool ) ) );
00679     QObject::connect( m_checkWordByWord, SIGNAL( toggled( bool ) ), m_wordByWordYes, SLOT( setEnabled( bool ) ) );
00680     QObject::connect( m_checkFontAttribute, SIGNAL( toggled( bool ) ), m_fontAttributeItem, SLOT( setEnabled( bool ) ) );
00681     QObject::connect( m_checkLanguage, SIGNAL( toggled( bool ) ), m_languageItem, SLOT( setEnabled( bool ) ) );
00682 
00683 
00684     QObject::connect( m_checkBold, SIGNAL( toggled( bool ) ), m_boldNo, SLOT( setEnabled( bool ) ) );
00685     QObject::connect( m_checkItalic, SIGNAL( toggled( bool ) ), m_italicNo, SLOT( setEnabled( bool ) ) );
00686     QObject::connect( m_checkShadow, SIGNAL( toggled( bool ) ), m_shadowNo, SLOT( setEnabled( bool ) ) );
00687     QObject::connect( m_checkWordByWord, SIGNAL( toggled( bool ) ), m_wordByWordNo, SLOT( setEnabled( bool ) ) );
00688 
00689 
00690     QObject::connect( m_checkVertAlign, SIGNAL( toggled( bool ) ), m_vertAlignItem, SLOT( setEnabled( bool ) ) );
00691 
00692     QObject::connect( m_checkUnderline, SIGNAL( toggled( bool ) ), m_underlineItem, SLOT( setEnabled( bool ) ) );
00693 
00694     slotReset();
00695 }
00696 
00697 void KoFormatDia::slotClear()
00698 {
00699     m_ctx->m_optionsMask = 0;
00700     m_ctx->m_options = 0;
00701     slotReset();
00702 }
00703 
00704 void KoFormatDia::slotReset()
00705 {
00706     m_checkFamily->setChecked( m_ctx->m_optionsMask & KoSearchContext::Family );
00707     m_familyItem->setEnabled(m_checkFamily->isChecked());
00708 
00709     m_checkSize->setChecked( m_ctx->m_optionsMask & KoSearchContext::Size );
00710     m_sizeItem->setEnabled(m_checkSize->isChecked());
00711 
00712     m_checkColor->setChecked( m_ctx->m_optionsMask & KoSearchContext::Color );
00713     m_colorItem->setEnabled(m_checkColor->isChecked());
00714 
00715     m_checkBgColor->setChecked( m_ctx->m_optionsMask & KoSearchContext::BgColor );
00716     m_bgColorItem->setEnabled(m_checkBgColor->isChecked());
00717 
00718 
00719     m_checkBold->setChecked( m_ctx->m_optionsMask & KoSearchContext::Bold );
00720     m_boldYes->setEnabled(m_checkBold->isChecked());
00721     m_boldNo->setEnabled(m_checkBold->isChecked());
00722 
00723     m_checkShadow->setChecked( m_ctx->m_optionsMask & KoSearchContext::Shadow );
00724     m_shadowYes->setEnabled(m_checkShadow->isChecked());
00725     m_shadowNo->setEnabled(m_checkShadow->isChecked());
00726 
00727     m_checkWordByWord->setChecked( m_ctx->m_optionsMask & KoSearchContext::WordByWord );
00728     m_wordByWordYes->setEnabled(m_checkWordByWord->isChecked());
00729     m_wordByWordNo->setEnabled(m_checkWordByWord->isChecked());
00730 
00731 
00732     m_checkStrikeOut->setChecked( m_ctx->m_optionsMask & KoSearchContext::StrikeOut );
00733     m_strikeOutItem->setEnabled( m_checkStrikeOut->isChecked());
00734 
00735 
00736     m_checkItalic->setChecked( m_ctx->m_optionsMask & KoSearchContext::Italic );
00737     m_italicNo->setEnabled(m_checkItalic->isChecked());
00738     m_italicYes->setEnabled(m_checkItalic->isChecked());
00739 
00740     m_checkUnderline->setChecked( m_ctx->m_optionsMask & KoSearchContext::Underline );
00741     m_underlineItem->setEnabled(m_checkUnderline->isChecked());
00742 
00743     m_checkVertAlign->setChecked( m_ctx->m_optionsMask & KoSearchContext::VertAlign );
00744     m_vertAlignItem->setEnabled(m_checkVertAlign->isChecked());
00745 
00746     m_checkFontAttribute->setChecked( m_ctx->m_optionsMask & KoSearchContext::Attribute );
00747     m_fontAttributeItem->setEnabled(m_checkFontAttribute->isChecked());
00748 
00749 
00750     m_checkLanguage->setChecked( m_ctx->m_optionsMask & KoSearchContext::Language );
00751     m_languageItem->setEnabled(m_checkLanguage->isChecked());
00752 
00753 
00754     if (m_ctx->m_options & KoSearchContext::Bold)
00755         m_boldYes->setChecked( true );
00756     else
00757         m_boldNo->setChecked( true );
00758 
00759     if (m_ctx->m_options & KoSearchContext::Italic)
00760         m_italicYes->setChecked( true );
00761     else
00762         m_italicNo->setChecked( true );
00763 
00764     if (m_ctx->m_options & KoSearchContext::Shadow)
00765         m_shadowYes->setChecked( true );
00766     else
00767         m_shadowNo->setChecked( true );
00768 
00769     if (m_ctx->m_options & KoSearchContext::WordByWord)
00770         m_wordByWordYes->setChecked( true );
00771     else
00772         m_wordByWordNo->setChecked( true );
00773 
00774 }
00775 
00776 void KoFormatDia::ctxOptions( )
00777 {
00778     long optionsMask = 0;
00779     long options = 0;
00780     if ( m_checkFamily->isChecked() )
00781         optionsMask |= KoSearchContext::Family;
00782     if ( m_checkSize->isChecked() )
00783         optionsMask |= KoSearchContext::Size;
00784     if ( m_checkColor->isChecked() )
00785         optionsMask |= KoSearchContext::Color;
00786     if ( m_checkBgColor->isChecked() )
00787         optionsMask |= KoSearchContext::BgColor;
00788     if ( m_checkBold->isChecked() )
00789         optionsMask |= KoSearchContext::Bold;
00790     if ( m_checkItalic->isChecked() )
00791         optionsMask |= KoSearchContext::Italic;
00792     if ( m_checkUnderline->isChecked() )
00793         optionsMask |= KoSearchContext::Underline;
00794     if ( m_checkVertAlign->isChecked() )
00795         optionsMask |= KoSearchContext::VertAlign;
00796     if ( m_checkStrikeOut->isChecked() )
00797         optionsMask |= KoSearchContext::StrikeOut;
00798     if ( m_checkShadow->isChecked() )
00799         optionsMask |= KoSearchContext::Shadow;
00800     if ( m_checkWordByWord->isChecked() )
00801         optionsMask |= KoSearchContext::WordByWord;
00802     if ( m_checkLanguage->isChecked() )
00803         optionsMask |= KoSearchContext::Language;
00804 
00805 
00806     if ( m_boldYes->isChecked() )
00807         options |= KoSearchContext::Bold;
00808     if ( m_italicYes->isChecked() )
00809         options |= KoSearchContext::Italic;
00810     if ( m_shadowYes->isChecked() )
00811         options |= KoSearchContext::Shadow;
00812     if ( m_wordByWordYes->isChecked() )
00813         options |= KoSearchContext::WordByWord;
00814 
00815 
00816     m_ctx->m_optionsMask = optionsMask;
00817     m_ctx->m_family = m_familyItem->currentText();
00818     m_ctx->m_size = m_sizeItem->cleanText().toInt();
00819     m_ctx->m_color = m_colorItem->color();
00820     m_ctx->m_backGroundColor = m_bgColorItem->color();
00821     m_ctx->m_vertAlign = (KoTextFormat::VerticalAlignment)m_vertAlignItem->currentItem();
00822     m_ctx->m_underline = (KoTextFormat::UnderlineType)m_underlineItem->currentItem();
00823     m_ctx->m_strikeOut = (KoTextFormat::StrikeOutType)m_strikeOutItem->currentItem();
00824     m_ctx->m_attribute = (KoTextFormat::AttributeStyle)m_fontAttributeItem->currentItem();
00825     m_ctx->m_language = KoGlobal::listTagOfLanguages()[m_languageItem->currentItem()];
00826 
00827     m_ctx->m_options = options;
00828 }
00829 
00830 
00831 bool KoFindReplace::validateMatch( const QString & /*text*/, int index, int matchedlength )
00832 {
00833     if ( !m_searchContextEnabled || !m_searchContext.m_optionsMask )
00834         return true;
00835     KoTextString * s = currentParag()->string();
00836     for ( int i = index ; i < index+matchedlength ; ++i )
00837     {
00838         KoTextStringChar & ch = s->at(i);
00839         KoTextFormat *format = ch.format();
00840         if (m_searchContext.m_optionsMask & KoSearchContext::Bold)
00841         {
00842             if ( (!format->font().bold() && (m_searchContext.m_options & KoSearchContext::Bold)) || (format->font().bold() && ((m_searchContext.m_options & KoSearchContext::Bold)==0)))
00843                 return false;
00844         }
00845         if (m_searchContext.m_optionsMask & KoSearchContext::Shadow)
00846         {
00847             bool hasShadow = format->shadowDistanceX() != 0 || format->shadowDistanceY() != 0;
00848             if ( (!hasShadow && (m_searchContext.m_options & KoSearchContext::Shadow))
00849                  || (hasShadow && ((m_searchContext.m_options & KoSearchContext::Shadow)==0)) )
00850                 return false;
00851         }
00852 
00853         if (m_searchContext.m_optionsMask & KoSearchContext::WordByWord)
00854         {
00855             if ( (!format->wordByWord() && (m_searchContext.m_options & KoSearchContext::WordByWord)) || (format->wordByWord() && ((m_searchContext.m_options & KoSearchContext::WordByWord)==0)))
00856                 return false;
00857         }
00858 
00859 
00860         if (m_searchContext.m_optionsMask & KoSearchContext::Size)
00861         {
00862             if ( format->font().pointSize() != m_searchContext.m_size )
00863                 return false;
00864         }
00865         if ( m_searchContext.m_optionsMask & KoSearchContext::Family)
00866         {
00867             if (format->font().family() != m_searchContext.m_family)
00868                 return false;
00869         }
00870         if ( m_searchContext.m_optionsMask & KoSearchContext::Color)
00871         {
00872             if (format->color() != m_searchContext.m_color)
00873                 return false;
00874         }
00875         if ( m_searchContext.m_optionsMask & KoSearchContext::BgColor)
00876         {
00877             if (format->textBackgroundColor() != m_searchContext.m_backGroundColor)
00878                 return false;
00879         }
00880 
00881         if ( m_searchContext.m_optionsMask & KoSearchContext::Italic)
00882         {
00883             if ( (!format->font().italic() && (m_searchContext.m_options & KoSearchContext::Italic)) || (format->font().italic() && ((m_searchContext.m_options & KoSearchContext::Italic)==0)))
00884                 return false;
00885 
00886         }
00887         if ( m_searchContext.m_optionsMask & KoSearchContext::Underline)
00888         {
00889             if ( format->underlineType() != m_searchContext.m_underline )
00890                 return false;
00891         }
00892         if ( m_searchContext.m_optionsMask & KoSearchContext::StrikeOut)
00893         {
00894             if ( format->strikeOutType() != m_searchContext.m_strikeOut )
00895                 return false;
00896         }
00897 
00898         if ( m_searchContext.m_optionsMask & KoSearchContext::VertAlign)
00899         {
00900             if ( format->vAlign() != m_searchContext.m_vertAlign )
00901                 return false;
00902         }
00903         if ( m_searchContext.m_optionsMask & KoSearchContext::Language)
00904         {
00905             if ( format->language() != m_searchContext.m_language )
00906                 return false;
00907         }
00908 
00909         if ( m_searchContext.m_optionsMask & KoSearchContext::Attribute)
00910         {
00911             if ( format->attributeFont() != m_searchContext.m_attribute )
00912                 return false;
00913         }
00914 
00915     }
00916     return true;
00917 }
00918 
00919 bool KoFindReplace::shouldRestart()
00920 {
00921     if ( m_find )
00922         return m_find->shouldRestart( true /*since text is editable*/, m_doCounting );
00923     else
00924         return m_replace->shouldRestart( true /*since text is editable*/, m_doCounting );
00925 }
00926 
00927 #include "koSearchDia.moc"
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:07 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003