Tommaso patch part 6, FindAndReplace not compiled yet.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@27522 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2008-11-16 00:15:51 +00:00
parent 4209cbc631
commit 3091b7f284
3 changed files with 709 additions and 0 deletions

View File

@ -0,0 +1,276 @@
/**
* \file FindAndReplace.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Tommaso Cucinotta
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "FindAndReplace.h"
#include "GuiWorkArea.h"
#include "GuiView.h"
#include "qt_helpers.h"
#include "Application.h"
#include "BufferList.h"
#include "buffer_funcs.h"
#include "BufferParams.h"
#include "Cursor.h"
#include "FuncRequest.h"
#include "lyxfind.h"
#include "OutputParams.h"
#include "output_latex.h"
#include "TexRow.h"
#include "support/FileName.h"
#include "support/convert.h"
#include "support/debug.h"
#include "support/gettext.h"
#include "support/lassert.h"
#include <QLineEdit>
#include <QCloseEvent>
#include <iostream>
using namespace std;
using namespace lyx::support;
namespace lyx {
namespace frontend {
FindAndReplace::FindAndReplace(GuiView & parent)
: DockView(parent, "Find LyX", "Find LyX Dialog", Qt::RightDockWidgetArea),
parent_view_(parent),
delayedFocusTimer_(this)
{
searchBuffer_ = theBufferList().newBuffer(
support::FileName::tempName().absFilename() + "_searchadv.internal");
LASSERT(searchBuffer_ != 0, /* */);
searchBufferView_ = new BufferView(*searchBuffer_);
searchBuffer_->setUnnamed(true);
searchBuffer_->setFullyLoaded(true);
setupUi(this);
find_work_area_->setGuiView(parent_view_);
find_work_area_->setBuffer(*searchBuffer_);
find_work_area_->setUpdatesEnabled(false);
find_work_area_->setDialogMode(true);
}
FindAndReplace::~FindAndReplace()
{
// No need to destroy buffer and bufferview here, because it is done
// in theBuffeerList() destruction loop at application exit
LYXERR(Debug::DEBUG, "FindAndReplace::~FindAndReplace()");
}
bool FindAndReplace::eventFilter(QObject *obj, QEvent *event)
{
LYXERR(Debug::DEBUG, "FindAndReplace::eventFilter()" << std::endl);
if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
QKeyEvent *e = static_cast<QKeyEvent *> (event);
if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
on_closePB_clicked();
return true;
} else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
if (e->modifiers() == Qt::ShiftModifier) {
on_findPrevPB_clicked();
return true;
} else if (e->modifiers() == Qt::NoModifier) {
on_findNextPB_clicked();
return true;
}
}
}
// standard event processing
return QObject::eventFilter(obj, event);
}
void FindAndReplace::closeEvent(QCloseEvent * close_event)
{
LYXERR(Debug::DEBUG, "FindAndReplace::closeEvent()");
find_work_area_->removeEventFilter(this);
disableSearchWorkArea();
DockView::closeEvent(close_event);
}
void FindAndReplace::selectAll()
{
dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
find_work_area_->redraw();
}
void FindAndReplace::findAdv(bool casesensitive,
bool matchword, bool backwards,
bool expandmacros, bool ignoreformat)
{
docstring searchString;
if (! ignoreformat) {
OutputParams runparams(&searchBuffer_->params().encoding());
odocstringstream os;
runparams.nice = true;
runparams.flavor = OutputParams::LATEX;
runparams.linelen = 80; //lyxrc.plaintext_linelen;
// No side effect of file copying and image conversion
runparams.dryrun = true;
searchBuffer_->texrow().reset();
// latexParagraphs(searchBuffer_, searchBuffer_.paragraphs(), os, searchBuffer_.texrow(), runparams);
for (ParagraphList::const_iterator pit = searchBuffer_->paragraphs().begin(); pit != searchBuffer_->paragraphs().end(); ++pit) {
TeXOnePar(*searchBuffer_, searchBuffer_->text(), pit, os, searchBuffer_->texrow(), runparams);
lyxerr << "searchString up to here: " << to_utf8(os.str()) << std::endl;
}
searchString = os.str();
} else {
for (ParIterator it = searchBuffer_->par_iterator_begin(); it != searchBuffer_->par_iterator_end(); ++it) {
lyxerr << "Adding to search string: '" << to_utf8(it->asString(false)) << "'" << std::endl;
searchString += it->asString(AS_STR_INSETS);
}
}
// lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
if (to_utf8(searchString).empty()) {
searchBufferView_->message(_("Nothing to search"));
return;
}
bool regexp = (to_utf8(searchString).find("\\regexp") != std::string::npos);
FindAdvOptions opt(searchString, casesensitive, matchword, ! backwards, expandmacros, ignoreformat, regexp);
std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
std::ostringstream oss;
oss << opt;
std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
// findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
// searchString, len, casesensitive, matchword, ! backwards, expandmacros);
}
void FindAndReplace::onDelayedFocus()
{
LYXERR(Debug::DEBUG, "Delayed Focus");
parent_view_.setCurrentWorkArea(find_work_area_);
find_work_area_->setFocus();
}
void FindAndReplace::showEvent(QShowEvent *ev)
{
LYXERR(Debug::DEBUG, "FindAndReplace::showEvent");
parent_view_.setCurrentWorkArea(find_work_area_);
selectAll();
find_work_area_->redraw();
find_work_area_->setFocus();
find_work_area_->installEventFilter(this);
connect(&delayedFocusTimer_, SIGNAL(timeout()), this, SLOT(onDelayedFocus()));
delayedFocusTimer_.setSingleShot(true);
delayedFocusTimer_.start(100);
this->QWidget::showEvent(ev);
}
void FindAndReplace::disableSearchWorkArea()
{
LYXERR(Debug::DEBUG, "FindAndReplace::disableSearchWorkArea()");
// Ok, closing the window before 100ms may be impossible, however...
delayedFocusTimer_.stop();
if (parent_view_.currentWorkArea() == find_work_area_) {
LASSERT(parent_view_.currentMainWorkArea(), /* */);
parent_view_.setCurrentWorkArea(parent_view_.currentMainWorkArea());
}
find_work_area_->stopBlinkingCursor();
}
void FindAndReplace::hideEvent(QHideEvent *ev)
{
LYXERR(Debug::DEBUG, "FindAndReplace::hideEvent");
find_work_area_->removeEventFilter(this);
disableSearchWorkArea();
this->QWidget::hideEvent(ev);
}
void FindAndReplace::find(bool backwards)
{
parent_view_.setCurrentWorkArea(parent_view_.currentMainWorkArea());
findAdv(caseCB->isChecked(),
wordsCB->isChecked(),
backwards,
expandMacrosCB->isChecked(),
ignoreFormatCB->isChecked());
parent_view_.currentMainWorkArea()->redraw();
parent_view_.setCurrentWorkArea(find_work_area_);
find_work_area_->setFocus();
}
void FindAndReplace::on_regexpInsertCombo_currentIndexChanged(int index)
{
static char const * regexps[] = {
".*", ".+", "[a-z]+", "[0-9]+"
};
//lyxerr << "Index: " << index << std::endl;
if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
find_work_area_->setFocus();
Cursor & cur = find_work_area_->bufferView().cursor();
if (! cur.inRegexped())
dispatch(FuncRequest(LFUN_REGEXP_MODE));
dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
regexpInsertCombo->setCurrentIndex(0);
}
}
void FindAndReplace::on_closePB_clicked() {
disableSearchWorkArea();
LYXERR(Debug::DEBUG, "Dispatching dialog-hide findreplaceadv" << std::endl);
parent_view_.dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
}
void FindAndReplace::on_findNextPB_clicked() {
find(false);
}
void FindAndReplace::on_findPrevPB_clicked() {
find(true);
}
void FindAndReplace::on_replacePB_clicked()
{
}
void FindAndReplace::on_replaceallPB_clicked()
{
}
Dialog * createGuiSearchAdv(GuiView & lv)
{
return new FindAndReplace(lv);
}
} // namespace frontend
} // namespace lyx
#include "moc_FindAndReplace.cpp"

View File

@ -0,0 +1,107 @@
// -*- C++ -*-
/**
* \file FindAndReplace.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Tommaso Cucinotta
*
* Full author contact details are available in file CREDITS.
*/
#ifndef QSEARCHADV_H
#define QSEARCHADV_H
#include "GuiWorkArea.h"
#include "DockView.h"
#include "ui_FindAndReplaceUi.h"
#include "BufferView.h"
#include "Buffer.h"
#include "LyX.h"
#include "LyXFunc.h"
#include "Text.h"
#include <QDialog>
#include <string>
namespace lyx {
namespace frontend {
class FindAndReplace : public DockView, public Ui::FindAndReplaceUi
{
Q_OBJECT
public:
FindAndReplace(GuiView & parent);
~FindAndReplace();
bool initialiseParams(std::string const &) { return true; }
void clearParams() {}
void dispatchParams() {}
bool isBufferDependent() const { return true; }
void selectAll();
void showEvent(QShowEvent * ev);
void hideEvent(QHideEvent * ev);
/// update
void updateView() {}
//virtual void update_contents() {}
protected Q_SLOTS:
void on_findNextPB_clicked();
void on_findPrevPB_clicked();
void on_replacePB_clicked();
void on_replaceallPB_clicked();
void on_closePB_clicked();
void on_regexpInsertCombo_currentIndexChanged(int index);
protected:
void find(bool backwards);
virtual bool wantInitialFocus() const { return true; }
private:
// add a string to the combo if needed
void remember(std::string const & find, QComboBox & combo);
void findAdv(bool casesensitive,
bool matchword, bool backwards,
bool expandmacros, bool ignoreformat);
GuiView & parent_view_;
Buffer *searchBuffer_;
BufferView *searchBufferView_;
GuiWorkArea * searchWorkArea_; // The work area defining what to search
/// @TODO: Investigate on focus issue and remove this ugly hack, please !
QTimer delayedFocusTimer_;
void disableSearchWorkArea();
private:
/// Apply changes
virtual void apply() {}
void find(docstring const & str, int len, bool casesens,
bool words, bool backwards, bool expandmacros);
void replace(docstring const & findstr,
docstring const & replacestr,
bool casesens, bool words, bool backwards, bool expandmacros, bool all);
bool eventFilter(QObject *obj, QEvent *event);
public Q_SLOTS:
/// this happens when the dialog is simply closed/hidden
void closeEvent(QCloseEvent * e);
/// this happens 100ms after dialog showEvent()
void onDelayedFocus();
};
} // namespace frontend
} // namespace lyx
#endif // QSEARCHADV_H

View File

@ -0,0 +1,326 @@
<ui version="4.0" >
<class>FindAndReplaceUi</class>
<widget class="QWidget" name="FindAndReplaceUi" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>338</width>
<height>400</height>
</rect>
</property>
<property name="sizePolicy" >
<sizepolicy vsizetype="Preferred" hsizetype="Preferred" >
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize" >
<size>
<width>240</width>
<height>400</height>
</size>
</property>
<property name="baseSize" >
<size>
<width>300</width>
<height>400</height>
</size>
</property>
<property name="windowTitle" >
<string>Find LyX Text</string>
</property>
<property name="toolTip" >
<string/>
</property>
<widget class="QWidget" name="" >
<property name="geometry" >
<rect>
<x>4</x>
<y>4</y>
<width>301</width>
<height>381</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout" >
<item row="0" column="0" colspan="2" >
<widget class="lyx::frontend::GuiWorkArea" name="find_work_area_" >
<property name="widgetResizable" >
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>293</width>
<height>108</height>
</rect>
</property>
</widget>
</widget>
</item>
<item row="1" column="0" >
<widget class="QCheckBox" name="expandMacrosCB" >
<property name="enabled" >
<bool>false</bool>
</property>
<property name="text" >
<string>&amp;Expand macros</string>
</property>
</widget>
</item>
<item row="1" column="1" >
<widget class="QCheckBox" name="caseCB" >
<property name="text" >
<string>Case &amp;sensitive</string>
</property>
</widget>
</item>
<item row="2" column="0" >
<widget class="QCheckBox" name="wordsCB" >
<property name="enabled" >
<bool>false</bool>
</property>
<property name="text" >
<string>Whole words onl&amp;y</string>
</property>
</widget>
</item>
<item row="2" column="1" >
<widget class="QCheckBox" name="ignoreFormatCB" >
<property name="enabled" >
<bool>true</bool>
</property>
<property name="text" >
<string>Ignore For&amp;mat</string>
</property>
<property name="checked" >
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="0" >
<widget class="QPushButton" name="findNextPB" >
<property name="enabled" >
<bool>true</bool>
</property>
<property name="text" >
<string>Find &amp;Next</string>
</property>
<property name="default" >
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="1" >
<widget class="QPushButton" name="findPrevPB" >
<property name="enabled" >
<bool>true</bool>
</property>
<property name="text" >
<string>Find &amp;Prev</string>
</property>
<property name="default" >
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="0" >
<widget class="QPushButton" name="replacePB" >
<property name="enabled" >
<bool>false</bool>
</property>
<property name="text" >
<string>&amp;Replace</string>
</property>
</widget>
</item>
<item row="5" column="0" >
<widget class="QPushButton" name="replaceallPB" >
<property name="enabled" >
<bool>false</bool>
</property>
<property name="text" >
<string>Replace &amp;All</string>
</property>
</widget>
</item>
<item row="5" column="1" >
<widget class="QPushButton" name="closePB" >
<property name="text" >
<string>&amp;Close</string>
</property>
</widget>
</item>
<item row="6" column="0" >
<widget class="QGroupBox" name="groupBox_2" >
<property name="enabled" >
<bool>false</bool>
</property>
<property name="sizePolicy" >
<sizepolicy vsizetype="Preferred" hsizetype="Preferred" >
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize" >
<size>
<width>0</width>
<height>111</height>
</size>
</property>
<property name="title" >
<string>Sco&amp;pe</string>
</property>
<widget class="QRadioButton" name="scopeRB" >
<property name="geometry" >
<rect>
<x>10</x>
<y>40</y>
<width>121</width>
<height>23</height>
</rect>
</property>
<property name="toolTip" >
<string>Current buffer only</string>
</property>
<property name="text" >
<string>Buffer</string>
</property>
<property name="checked" >
<bool>true</bool>
</property>
</widget>
<widget class="QRadioButton" name="scopeRB_3" >
<property name="geometry" >
<rect>
<x>10</x>
<y>60</y>
<width>121</width>
<height>23</height>
</rect>
</property>
<property name="toolTip" >
<string>Current file and all included files</string>
</property>
<property name="text" >
<string>Document</string>
</property>
</widget>
<widget class="QRadioButton" name="scopeRB_2" >
<property name="geometry" >
<rect>
<x>10</x>
<y>20</y>
<width>121</width>
<height>23</height>
</rect>
</property>
<property name="toolTip" >
<string>Current paragraph only</string>
</property>
<property name="text" >
<string>Paragraph</string>
</property>
<property name="checked" >
<bool>false</bool>
</property>
</widget>
<widget class="QRadioButton" name="scopeRB_4" >
<property name="geometry" >
<rect>
<x>10</x>
<y>80</y>
<width>121</width>
<height>23</height>
</rect>
</property>
<property name="toolTip" >
<string>All open buffers</string>
</property>
<property name="text" >
<string>Open buffers</string>
</property>
</widget>
</widget>
</item>
<item row="6" column="1" >
<widget class="QGroupBox" name="groupBox_3" >
<property name="enabled" >
<bool>true</bool>
</property>
<property name="sizePolicy" >
<sizepolicy vsizetype="Preferred" hsizetype="Preferred" >
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize" >
<size>
<width>0</width>
<height>111</height>
</size>
</property>
<property name="title" >
<string>RegExp</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout" >
<item>
<widget class="QComboBox" name="regexpInsertCombo" >
<property name="enabled" >
<bool>true</bool>
</property>
<property name="sizePolicy" >
<sizepolicy vsizetype="Fixed" hsizetype="Minimum" >
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item>
<property name="text" >
<string>Match...</string>
</property>
</item>
<item>
<property name="text" >
<string>Anything</string>
</property>
</item>
<item>
<property name="text" >
<string>Any non-empty</string>
</property>
</item>
<item>
<property name="text" >
<string>Any word</string>
</property>
</item>
<item>
<property name="text" >
<string>Any number</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>lyx::frontend::GuiWorkArea</class>
<extends>QScrollArea</extends>
<header>GuiWorkArea.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<includes>
<include location="local" >qt_helpers.h</include>
</includes>
<resources/>
<connections/>
</ui>