31 #include "ui_kaider_findextension.h"
36 #include <kmessagebox.h>
41 #include <kprogressdialog.h>
43 #include <kreplacedialog.h>
46 #include <sonnet/backgroundchecker.h>
47 #include <sonnet/dialog.h>
53 #define IGNOREACCELS KFind::MinimumUserOption
54 #define INCLUDENOTES KFind::MinimumUserOption*2
56 static long makeOptions(
long options,
const Ui_findExtension* ui_findExtension)
59 +
IGNOREACCELS*ui_findExtension->m_ignoreAccelMarks->isChecked()
64 class EntryFindDialog:
public KFindDialog
67 EntryFindDialog(
QWidget* parent);
69 long options()
const{
return makeOptions(KFindDialog::options(),ui_findExtension);}
70 static EntryFindDialog* instance(
QWidget* parent=0);
73 static void cleanup(){
delete EntryFindDialog::_instance;}
75 Ui_findExtension* ui_findExtension;
79 EntryFindDialog* EntryFindDialog::instance(
QWidget* parent)
83 _instance=
new EntryFindDialog(parent);
84 qAddPostRoutine(EntryFindDialog::cleanup);
89 EntryFindDialog::EntryFindDialog(
QWidget* parent)
91 , ui_findExtension(new Ui_findExtension)
93 ui_findExtension->setupUi(findExtension());
94 setHasSelection(
false);
97 KConfigGroup stateGroup(&config,
"FindReplace");
98 setOptions(stateGroup.readEntry(
"FindOptions",(qlonglong)0));
99 setFindHistory(stateGroup.readEntry(
"FindHistory",
QStringList()));
102 EntryFindDialog::~EntryFindDialog()
105 KConfigGroup stateGroup(&config,
"FindReplace");
106 stateGroup.writeEntry(
"FindOptions",(qlonglong)options());
107 stateGroup.writeEntry(
"FindHistory",findHistory());
109 delete ui_findExtension;
113 class EntryReplaceDialog:
public KReplaceDialog
116 EntryReplaceDialog(
QWidget* parent);
117 ~EntryReplaceDialog();
118 long options()
const{
return makeOptions(KReplaceDialog::options(),ui_findExtension);}
119 static EntryReplaceDialog* instance(
QWidget* parent=0);
122 static void cleanup(){
delete EntryReplaceDialog::_instance;}
124 Ui_findExtension* ui_findExtension;
128 EntryReplaceDialog* EntryReplaceDialog::instance(
QWidget* parent)
132 _instance=
new EntryReplaceDialog(parent);
133 qAddPostRoutine(EntryReplaceDialog::cleanup);
138 EntryReplaceDialog::EntryReplaceDialog(
QWidget* parent)
139 : KReplaceDialog(parent)
140 , ui_findExtension(new Ui_findExtension)
142 ui_findExtension->setupUi(findExtension());
144 setHasSelection(
false);
147 KConfigGroup stateGroup(&config,
"FindReplace");
148 setOptions(stateGroup.readEntry(
"ReplaceOptions",(qlonglong)0));
149 setFindHistory(stateGroup.readEntry(
"ReplacePatternHistory",
QStringList()));
150 setReplacementHistory(stateGroup.readEntry(
"ReplacementHistory",
QStringList()));
153 EntryReplaceDialog::~EntryReplaceDialog()
156 KConfigGroup stateGroup(&config,
"FindReplace");
157 stateGroup.writeEntry(
"ReplaceOptions",(qlonglong)options());
158 stateGroup.writeEntry(
"ReplacePatternHistory",findHistory());
159 stateGroup.writeEntry(
"ReplacementHistory",replacementHistory());
161 delete ui_findExtension;
170 if (KDE_ISUNLIKELY( data.
at(i)==
'&' ))
174 int limit=offset+length;
175 for (i=offset;i<limit;++i)
176 if (KDE_ISUNLIKELY( data.
at(i)==
'&' ))
179 limit=qMin(data.
size(),offset+length);
183 void EditorTab::find()
186 EntryFindDialog::instance(nativeParentWidget());
195 EntryFindDialog::instance()->setPattern(sel);
198 if ( EntryFindDialog::instance()->exec() != QDialog::Accepted )
203 _find->resetCounts();
204 _find->setPattern(EntryFindDialog::instance()->pattern());
205 _find->setOptions(EntryFindDialog::instance()->options());
210 _find =
new KFind(EntryFindDialog::instance()->pattern(),EntryFindDialog::instance()->options(),
this,EntryFindDialog::instance());
211 connect(_find,SIGNAL(highlight(
QString,
int,
int)),
this, SLOT(highlightFound(
QString,
int,
int)) );
212 connect(_find,SIGNAL(findNext()),
this,SLOT(findNext()));
213 _find->closeFindNextDialog();
217 if (_find->options() & KFind::FromCursor)
219 else if (!determineStartingPos(_find,pos))
226 bool EditorTab::determineStartingPos(KFind* find,
229 if (find->options() & KFind::FindBackwards)
243 void EditorTab::findNext(
const DocPosition& startingPos)
251 bool anotherEntry=_searchingPos.
entry!=m_currentPos.
entry;
252 _searchingPos=startingPos;
269 KFind::Result res = KFind::NoMatch;
279 data=catalog.
notes(_searchingPos).at(_searchingPos.
form).content;
290 if (res!=KFind::NoMatch)
294 (find.options()&KFind::FindBackwards)?
295 switchPrev(m_catalog,_searchingPos,switchOptions):
296 switchNext(m_catalog,_searchingPos,switchOptions)
301 if (res==KFind::NoMatch)
304 if(find.shouldRestart(
true,
true))
307 determineStartingPos(_find,_searchingPos);
314 void EditorTab::findNext()
319 _searchingPos:m_currentPos);
326 void EditorTab::findPrev()
331 _find->setOptions(_find->options() ^ KFind::FindBackwards);
332 findNext(m_currentPos);
341 void EditorTab::highlightFound(
const QString &,
int matchingIndex,
int matchedLength)
349 _searchingPos.
offset=matchingIndex;
353 void EditorTab::replace()
355 EntryReplaceDialog::instance(nativeParentWidget());
363 EntryReplaceDialog::instance()->setPattern(tmp);
370 if ( EntryReplaceDialog::instance()->exec() != QDialog::Accepted )
374 if (_replace) _replace->deleteLater();
378 _replace =
new KReplace(EntryReplaceDialog::instance()->pattern(),EntryReplaceDialog::instance()->replacement(),EntryReplaceDialog::instance()->options(),
this,EntryReplaceDialog::instance());
379 connect(_replace,SIGNAL(highlight(
QString,
int,
int)),
this,SLOT(highlightFound_(
QString,
int,
int)));
380 connect(_replace,SIGNAL(findNext()),
this,SLOT(replaceNext()));
381 connect(_replace,SIGNAL(replace(
QString,
int,
int,
int)),
this,SLOT(doReplace(
QString,
int,
int,
int)));
382 connect(_replace,SIGNAL(dialogClosed()),
this,SLOT(cleanupReplace()));
393 m_doReplaceCalled=
false;
395 if (_replace->options() & KFind::FromCursor)
396 replaceNext(m_currentPos);
400 if (!determineStartingPos(_replace,pos))
return;
407 void EditorTab::replaceNext(
const DocPosition& startingPos)
409 bool anotherEntry=_replacingPos.
entry!=_replacingPos.
entry;
410 _replacingPos=startingPos;
419 kWarning()<<
"includenotes"<<includenotes;
424 KFind::Result res=KFind::NoMatch;
427 if (_replace->needData()||anotherEntry)
434 data=m_catalog->
notes(_replacingPos).at(_replacingPos.
form).content;
438 if (ignoreaccels) data.
remove(
'&');
440 _replace->setData(data);
442 res = _replace->replace();
443 if (res!=KFind::NoMatch)
447 (_replace->options()&KFind::FindBackwards)?
448 switchPrev(m_catalog,_replacingPos,switchOptions):
449 switchNext(m_catalog,_replacingPos,switchOptions)
454 if (res==KFind::NoMatch)
456 if((_replace->options()&KFind::FromCursor)
457 &&_replace->shouldRestart(
true))
460 determineStartingPos(_replace,_replacingPos);
464 if(!(_replace->options() & KFind::FromCursor))
465 _replace->displayFinalDialog();
467 _replace->closeReplaceNextDialog();
470 _replace->resetCounts();
475 void EditorTab::cleanupReplace()
477 if(m_doReplaceCalled)
479 m_doReplaceCalled=
false;
484 void EditorTab::replaceNext()
486 replaceNext(m_currentPos);
489 void EditorTab::highlightFound_(
const QString &,
int matchingIndex,
int matchedLength)
497 _replacingPos.
offset=matchingIndex;
502 void EditorTab::doReplace(
const QString &newStr,
int offset,
int newLen,
int remLen)
504 if(!m_doReplaceCalled)
506 m_doReplaceCalled=
true;
507 m_catalog->
beginMacro(i18nc(
"@item Undo action item",
"Replace"));
541 void EditorTab::spellcheck()
545 m_sonnetChecker=
new Sonnet::BackgroundChecker(
this);
547 m_sonnetDialog=
new Sonnet::Dialog(m_sonnetChecker,
this);
548 connect(m_sonnetDialog,SIGNAL(done(
QString)),
this,SLOT(spellcheckNext()));
551 connect(m_sonnetDialog,SIGNAL(
stop()),
this,SLOT(spellcheckStop()));
552 connect(m_sonnetDialog,SIGNAL(cancel()),
this,SLOT(spellcheckCancel()));
554 connect(m_sonnetDialog,SIGNAL(misspelling(
QString,
int)),
555 this,SLOT(spellcheckShow(
QString,
int)));
567 m_sonnetDialog->setBuffer(text);
569 _spellcheckPos=m_currentPos;
570 _spellcheckStartPos=m_currentPos;
571 _spellcheckStop=
false;
573 _spellcheckStartUndoIndex=m_catalog->
index();
574 m_sonnetDialog->show();
579 void EditorTab::spellcheckNext()
588 kWarning()<<_spellcheckStartPos.
entry;
589 kWarning()<<_spellcheckStartPos.
form;
590 bool continueFromStart=
591 !(_spellcheckStartPos.
entry==0 && _spellcheckStartPos.
form==0)
592 && KMessageBox::questionYesNo(
this,i18n(
"Lokalize has reached end of document. Do you want to continue from start?"), i18nc(
"@title",
"Spellcheck"))==KMessageBox::Yes;
593 if (continueFromStart)
595 _spellcheckStartPos.
entry=0;
596 _spellcheckStartPos.
form=0;
597 _spellcheckPos=_spellcheckStartPos;
601 KMessageBox::information(
this,i18n(
"Lokalize has finished spellchecking"), i18nc(
"@title",
"Spellcheck"));
610 m_sonnetDialog->setBuffer(text);
613 void EditorTab::spellcheckStop()
615 _spellcheckStop=
true;
618 void EditorTab::spellcheckCancel()
620 m_catalog->
setIndex(_spellcheckStartUndoIndex);
624 void EditorTab::spellcheckShow(
const QString &word,
int offset)
630 m_sonnetDialog->setUpdatesEnabled(
false);
631 m_sonnetChecker->continueChecking();
634 m_sonnetDialog->setUpdatesEnabled(
true);
646 void EditorTab::spellcheckReplace(
QString oldWord,
int offset,
const QString &newWord)
649 int length=oldWord.
length();
652 if (length>oldWord.
length())
653 oldWord=m_catalog->
target(pos).
mid(offset,length);
688 void EditorTab::displayWordCount()
701 sourceCount+=words.
size();
703 msg=m_catalog->
target(pos);
705 words=msg.
split(rxSplit,QString::SkipEmptyParts);
706 targetCount+=words.
size();
710 KMessageBox::information(
this, i18nc(
"@info words count",
711 "Source text words: %1<br/>Target text words: %2",
712 sourceCount,targetCount),i18nc(
"@title",
"Word Count"));
QStringList context(const DocPosition &pos) const
void gotoEntry(DocPosition pos=DocPosition(), int selection=0)
CatalogString targetWithTags(const DocPosition &pos) const
how undo system works: undo() and redo() functions call appropriate private method of Catalog to chan...
int numberOfPluralForms() const
void beginMacro(const QString &text)
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
bool switchNext(Catalog *&catalog, DocPosition &pos, int parts)
static Project * instance()
Q_SCRIPTABLE bool findEntryBySourceContext(const QString &source, const QString &ctxt)
void push(QUndoCommand *cmd)
QString & remove(int position, int n)
CatalogString catalogString(const DocPosition &pos) const
This struct represents a position in a catalog.
QString target(const DocPosition &pos) const
QString selectionInTarget() const
QString enhanceLangCode(const QString &langCode)
bool isPlural(uint index) const
Q_SCRIPTABLE QString selectionInSource()
bool contains(QChar ch, Qt::CaseSensitivity cs) const
static long makeOptions(long options, const Ui_findExtension *ui_findExtension)
QString mid(int position, int n) const
void gotoEntry(DocPosition pos, int selection=0)
const QChar at(int position) const
bool isApproved(uint index) const
int numberOfEntries() const
Q_SCRIPTABLE QString selectionInTarget()
QString source(const DocPosition &pos) const
static void calcOffsetWithAccels(const QString &data, int &offset, int &length)
This class represents a catalog It uses CatalogStorage interface to work with catalogs in different f...
int find(QChar c, int i, bool cs) const
QVector< Note > notes(const DocPosition &pos) const
virtual QString msgstr(const DocPosition &) const
Insert or remove (if content is empty) a note.
bool switchPrev(Catalog *&catalog, DocPosition &pos, int parts)