Juergen Spitzmueller wrote:
Abdelrazak Younes wrote:
This patch does that and a bit more. It ensures that the coords are
saved and restored in any case (closeEvent and buffer switching). It
also does move the preamble related code in apply() and updateParam() in
the new PreambleModule class. Ideally I'd like all modules to follow the
same route.
I have no time to check that now, but if you find your solution working also
with buffer changes (and buffer changes with open dialogs and buffer
changes with different documents with identical preambles), please go ahead
and put it in. Even if I invested some time, I'm not particularly happy
with my proposal.
Here is my current patch. I added and 'id()' method in ControlDocument
to get the id of the current buffer (which is in fact his address). As
explained before, I could not rely on the address of the BufferParam as
it is a local copy. AFAIAC, this is as clean as it can be. This Document
dialog needs a lot of cleanup... 1.6 stuff.
Guys, this is a new feature as bug 3201 is already solved. This patch is
well tested and I am confident that it works. But, to show that I am a
good boy, I will not commit it unless there's an explicit approval from
the majority. If not, this will be a good candidate for 1.5.1.
Abdel.
Index: controllers/ControlDocument.cpp
===================================================================
--- controllers/ControlDocument.cpp (revision 18191)
+++ controllers/ControlDocument.cpp (working copy)
@@ -79,6 +79,12 @@
}
+int ControlDocument::id() const
+{
+ return (int) &kernel().buffer();
+}
+
+
TextClass const & ControlDocument::textClass() const
{
return textclasslist[bp_->textclass];
Index: controllers/ControlDocument.h
===================================================================
--- controllers/ControlDocument.h (revision 18191)
+++ controllers/ControlDocument.h (working copy)
@@ -50,6 +50,8 @@
///
BufferParams & params() const;
///
+ int id() const;
+ ///
void setLanguage() const;
///
void saveAsDefault() const;
Index: qt4/QDocument.cpp
===================================================================
--- qt4/QDocument.cpp (revision 18191)
+++ qt4/QDocument.cpp (working copy)
@@ -11,22 +11,22 @@
#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 latexHighlighter use in the preamble.
-#include "QViewSource.h"
+// For the Branches module
+#include "QBranches.h"
+#include "QViewSource.h" // For latexHighlighter use in the preamble.
+
+#include "controllers/ControlDocument.h"
+
#include "BufferParams.h"
#include "Encoding.h"
#include "gettext.h"
@@ -36,12 +36,13 @@
#include "TextClassList.h"
#include "Spacing.h"
-#include "controllers/ControlDocument.h"
#include "support/lstrings.h"
-#include "controllers/ControlDocument.h"
+#include <QCloseEvent>
+#include <QTextCursor>
+#include <map>
using lyx::support::token;
using lyx::support::bformat;
@@ -49,6 +50,7 @@
using lyx::support::getVectorFromString;
using std::distance;
+using std::make_pair;
using std::vector;
using std::string;
@@ -94,6 +96,67 @@
/////////////////////////////////////////////////////////////////////
//
+// 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
//
/////////////////////////////////////////////////////////////////////
@@ -475,14 +538,10 @@
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;
connect(bulletsModule, SIGNAL(changed()),
@@ -744,8 +803,7 @@
void QDocumentDialog::apply(BufferParams & params)
{
// preamble
- params.preamble =
- fromqstr(preambleModule->preambleTE->document()->toPlainText());
+ preambleModule->apply(params);
// biblio
params.setCiteEngine(biblio::ENGINE_BASIC);
@@ -1021,9 +1079,7 @@
}
// 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(
@@ -1258,7 +1314,6 @@
}
-
/////////////////////////////////////////////////////////////////////
//
// Document
Index: qt4/QDocument.h
===================================================================
--- qt4/QDocument.h (revision 18191)
+++ qt4/QDocument.h (working copy)
@@ -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 <QCloseEvent>
#include <QDialog>
+
#include <vector>
#include <string>
@@ -49,6 +52,7 @@
class QBranches;
class QDocument;
+class PreambleModule;
class QDocumentDialog : public QDialog, public Ui::QDocumentUi {
Q_OBJECT
@@ -98,7 +102,7 @@
UiWidget<Ui::BiblioUi> *biblioModule;
UiWidget<Ui::MathsUi> *mathsModule;
UiWidget<Ui::LaTeXUi> *latexModule;
- UiWidget<Ui::PreambleUi> *preambleModule;
+ PreambleModule *preambleModule;
QBranches *branchesModule;
@@ -112,7 +116,6 @@
};
-
class ControlDocument;
class QDocument
@@ -139,6 +142,31 @@
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