]> git.lyx.org Git - features.git/commitdiff
restore cursor position in preamble.
authorAbdelrazak Younes <younes@lyx.org>
Mon, 14 May 2007 12:09:14 +0000 (12:09 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Mon, 14 May 2007 12:09:14 +0000 (12:09 +0000)
* 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
src/frontends/controllers/ControlDocument.h
src/frontends/qt4/QDocument.cpp
src/frontends/qt4/QDocument.h

index 36c3bbd5294ab53884a2de1b607891643b23630a..701ff89cd98dd8b912d7d951ae5cc6f9d97bcb2e 100644 (file)
@@ -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];
index d85cdf306e3fc77a63a4898c14ba2c6238862a3c..03ff4eebe45ebb7f8887e1636ba33ff21e9cb4fb 100644 (file)
@@ -50,6 +50,8 @@ public:
        ///
        BufferParams & params() const;
        ///
+       int id() const;
+       ///
        void setLanguage() const;
        ///
        void saveAsDefault() const;
index 200a3892b34b84fa4080e5fa9ce8c9dfc8edc86c..04dc9f0c5f893a66d93670a4a87abd92f0ce21c7 100644 (file)
 #include <config.h>
 
 #include "QDocument.h"
-#include "Qt2BC.h"
-#include "qt_helpers.h"
-#include "QBranches.h"
-
-#include <QCloseEvent>
 
+#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"
 
 #include "support/lstrings.h"
 
-#include "controllers/ControlDocument.h"
+#include <QCloseEvent>
+#include <QTextCursor>
 
+#include <map>
 
 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<Ui::PreambleUi>;
-       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
index 25e55b433f0ee218cc1b80985fc3f7df65ac8a2b..a44e6d35986beb3c1e4e06d63a386a95baeb5530 100644 (file)
 #include "ui/BiblioUi.h"
 #include "ui/NumberingUi.h"
 #include "ui/MarginsUi.h"
+
+// For the Preamble module
 #include "ui/PreambleUi.h"
 
 #include <QCloseEvent>
 #include <QDialog>
+
 #include <vector>
 #include <string>
 
@@ -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<Ui::BiblioUi> *biblioModule;
        UiWidget<Ui::MathsUi> *mathsModule;
        UiWidget<Ui::LaTeXUi> *latexModule;
-       UiWidget<Ui::PreambleUi> *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<Ui::PreambleUi>
+{
+       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<int, std::pair<int,int> > Coords;
+       Coords preamble_coords_;
+       int current_id_;
+};
+
+
+
 } // namespace frontend
 } // namespace lyx