From 06099a8959c23277ce84baf869c61c681f313313 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Mon, 14 May 2007 12:09:14 +0000 Subject: [PATCH] restore cursor position in preamble. * ControlDocument::id(): new method returning the id of the current buffer. * PreambleModule: new class for the preamble module. Handle everything preamble related, including memorizing the cursor position for each Buffer. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18307 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/controllers/ControlDocument.cpp | 6 ++ src/frontends/controllers/ControlDocument.h | 2 + src/frontends/qt4/QDocument.cpp | 100 ++++++++++++++---- src/frontends/qt4/QDocument.h | 32 +++++- 4 files changed, 116 insertions(+), 24 deletions(-) diff --git a/src/frontends/controllers/ControlDocument.cpp b/src/frontends/controllers/ControlDocument.cpp index 36c3bbd529..701ff89cd9 100644 --- a/src/frontends/controllers/ControlDocument.cpp +++ b/src/frontends/controllers/ControlDocument.cpp @@ -79,6 +79,12 @@ BufferParams & ControlDocument::params() const } +int ControlDocument::id() const +{ + return (int) &kernel().buffer(); +} + + TextClass const & ControlDocument::textClass() const { return textclasslist[bp_->textclass]; diff --git a/src/frontends/controllers/ControlDocument.h b/src/frontends/controllers/ControlDocument.h index d85cdf306e..03ff4eebe4 100644 --- a/src/frontends/controllers/ControlDocument.h +++ b/src/frontends/controllers/ControlDocument.h @@ -50,6 +50,8 @@ public: /// BufferParams & params() const; /// + int id() const; + /// void setLanguage() const; /// void saveAsDefault() const; diff --git a/src/frontends/qt4/QDocument.cpp b/src/frontends/qt4/QDocument.cpp index 200a3892b3..04dc9f0c5f 100644 --- a/src/frontends/qt4/QDocument.cpp +++ b/src/frontends/qt4/QDocument.cpp @@ -11,21 +11,21 @@ #include #include "QDocument.h" -#include "Qt2BC.h" -#include "qt_helpers.h" -#include "QBranches.h" - -#include +#include "CheckedLineEdit.h" #include "FloatPlacement.h" #include "LengthCombo.h" -#include "Validator.h" #include "PanelStack.h" #include "Qt2BC.h" -#include "CheckedLineEdit.h" +#include "qt_helpers.h" +#include "Validator.h" + +// For the Branches module +#include "QBranches.h" -// For latexHighlighter use in the preamble. -#include "QViewSource.h" +#include "QViewSource.h" // For latexHighlighter use in the preamble. + +#include "controllers/ControlDocument.h" #include "BufferParams.h" #include "Encoding.h" @@ -41,8 +41,10 @@ #include "support/lstrings.h" -#include "controllers/ControlDocument.h" +#include +#include +#include using lyx::support::token; using lyx::support::bformat; @@ -50,6 +52,7 @@ using lyx::support::findToken; using lyx::support::getVectorFromString; using std::distance; +using std::make_pair; using std::vector; using std::string; @@ -93,6 +96,67 @@ char const * tex_fonts_monospaced_gui[] = { N_("Default"), N_("Computer Modern T namespace lyx { namespace frontend { +///////////////////////////////////////////////////////////////////// +// +// PreambleModule +// +///////////////////////////////////////////////////////////////////// + +PreambleModule::PreambleModule(): current_id_(0) +{ + // This is not a memory leak. The object will be destroyed + // with this. + (void) new LaTeXHighlighter(preambleTE->document()); + setFocusProxy(preambleTE); +} + + +void PreambleModule::update(BufferParams const & params, int id) +{ + QString preamble = toqstr(params.preamble); + // Nothing to do if the params and preamble are unchanged. + if (id == current_id_ + && preamble == preambleTE->document()->toPlainText()) + return; + + QTextCursor cur = preambleTE->textCursor(); + // Save the coords before switching to the new one. + preamble_coords_[current_id_] = + make_pair(cur.position(), preambleTE->verticalScrollBar()->value()); + + // Save the params address for further use. + current_id_ = id; + preambleTE->document()->setPlainText(preamble); + Coords::const_iterator it = preamble_coords_.find(current_id_); + if (it == preamble_coords_.end()) + // First time we open this one. + preamble_coords_[current_id_] = make_pair(0,0); + else { + // Restore saved coords. + QTextCursor cur = preambleTE->textCursor(); + cur.setPosition(it->second.first); + preambleTE->setTextCursor(cur); + preambleTE->verticalScrollBar()->setValue(it->second.second); + } +} + + +void PreambleModule::apply(BufferParams & params) +{ + params.preamble = fromqstr(preambleTE->document()->toPlainText()); +} + + +void PreambleModule::closeEvent(QCloseEvent * e) +{ + // Save the coords before closing. + QTextCursor cur = preambleTE->textCursor(); + preamble_coords_[current_id_] = + make_pair(cur.position(), preambleTE->verticalScrollBar()->value()); + e->accept(); +} + + ///////////////////////////////////////////////////////////////////// // // DocumentDialog @@ -481,13 +545,9 @@ QDocumentDialog::QDocumentDialog(QDocument * form) this, SLOT(change_adaptor())); // preamble - preambleModule = new UiWidget; - connect(preambleModule->preambleTE, SIGNAL(textChanged()), + preambleModule = new PreambleModule; + connect(preambleModule, SIGNAL(changed()), this, SLOT(change_adaptor())); - // This is not a memory leak. The object will be destroyed - // with preambleModule. - (void) new LaTeXHighlighter(preambleModule->preambleTE->document()); - // bullets bulletsModule = new BulletsModule; @@ -770,8 +830,7 @@ void QDocumentDialog::updateNumbering() void QDocumentDialog::apply(BufferParams & params) { // preamble - params.preamble = - fromqstr(preambleModule->preambleTE->document()->toPlainText()); + preambleModule->apply(params); // biblio params.setCiteEngine(biblio::ENGINE_BASIC); @@ -1050,9 +1109,7 @@ void QDocumentDialog::updateParams(BufferParams const & params) } // preamble - QString preamble = toqstr(params.preamble); - if (preamble != preambleModule->preambleTE->document()->toPlainText()) - preambleModule->preambleTE->document()->setPlainText(preamble); + preambleModule->update(params, form_->controller().id()); // biblio biblioModule->citeDefaultRB->setChecked( @@ -1291,7 +1348,6 @@ void QDocumentDialog::updateParams(BufferParams const & params) } - ///////////////////////////////////////////////////////////////////// // // Document diff --git a/src/frontends/qt4/QDocument.h b/src/frontends/qt4/QDocument.h index 25e55b433f..a44e6d3598 100644 --- a/src/frontends/qt4/QDocument.h +++ b/src/frontends/qt4/QDocument.h @@ -25,10 +25,13 @@ #include "ui/BiblioUi.h" #include "ui/NumberingUi.h" #include "ui/MarginsUi.h" + +// For the Preamble module #include "ui/PreambleUi.h" #include #include + #include #include @@ -49,6 +52,7 @@ namespace frontend { class QBranches; class QDocument; +class PreambleModule; class QDocumentDialog : public QDialog, public Ui::QDocumentUi { Q_OBJECT @@ -99,7 +103,7 @@ private: UiWidget *biblioModule; UiWidget *mathsModule; UiWidget *latexModule; - UiWidget *preambleModule; + PreambleModule *preambleModule; QBranches *branchesModule; @@ -113,7 +117,6 @@ private: }; - class ControlDocument; class QDocument @@ -140,6 +143,31 @@ private: void useClassDefaults(); }; + +class PreambleModule : public UiWidget +{ + Q_OBJECT +public: + PreambleModule(); + void update(BufferParams const & params, int id); + void apply(BufferParams & params); + +Q_SIGNALS: + /// signal that something's changed in the Widget. + void changed(); + +protected: + void closeEvent(QCloseEvent *); + void on_preambleTE_textChanged() { changed(); } + +private: + typedef std::map > Coords; + Coords preamble_coords_; + int current_id_; +}; + + + } // namespace frontend } // namespace lyx -- 2.39.5