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
This commit is contained in:
Abdelrazak Younes 2007-05-14 12:09:14 +00:00
parent c35007dbce
commit 06099a8959
4 changed files with 116 additions and 24 deletions

View File

@ -79,6 +79,12 @@ BufferParams & ControlDocument::params() const
} }
int ControlDocument::id() const
{
return (int) &kernel().buffer();
}
TextClass const & ControlDocument::textClass() const TextClass const & ControlDocument::textClass() const
{ {
return textclasslist[bp_->textclass]; return textclasslist[bp_->textclass];

View File

@ -50,6 +50,8 @@ public:
/// ///
BufferParams & params() const; BufferParams & params() const;
/// ///
int id() const;
///
void setLanguage() const; void setLanguage() const;
/// ///
void saveAsDefault() const; void saveAsDefault() const;

View File

@ -11,21 +11,21 @@
#include <config.h> #include <config.h>
#include "QDocument.h" #include "QDocument.h"
#include "Qt2BC.h"
#include "qt_helpers.h"
#include "QBranches.h"
#include <QCloseEvent>
#include "CheckedLineEdit.h"
#include "FloatPlacement.h" #include "FloatPlacement.h"
#include "LengthCombo.h" #include "LengthCombo.h"
#include "Validator.h"
#include "PanelStack.h" #include "PanelStack.h"
#include "Qt2BC.h" #include "Qt2BC.h"
#include "CheckedLineEdit.h" #include "qt_helpers.h"
#include "Validator.h"
// For latexHighlighter use in the preamble. // For the Branches module
#include "QViewSource.h" #include "QBranches.h"
#include "QViewSource.h" // For latexHighlighter use in the preamble.
#include "controllers/ControlDocument.h"
#include "BufferParams.h" #include "BufferParams.h"
#include "Encoding.h" #include "Encoding.h"
@ -41,8 +41,10 @@
#include "support/lstrings.h" #include "support/lstrings.h"
#include "controllers/ControlDocument.h" #include <QCloseEvent>
#include <QTextCursor>
#include <map>
using lyx::support::token; using lyx::support::token;
using lyx::support::bformat; using lyx::support::bformat;
@ -50,6 +52,7 @@ using lyx::support::findToken;
using lyx::support::getVectorFromString; using lyx::support::getVectorFromString;
using std::distance; using std::distance;
using std::make_pair;
using std::vector; using std::vector;
using std::string; using std::string;
@ -93,6 +96,67 @@ char const * tex_fonts_monospaced_gui[] = { N_("Default"), N_("Computer Modern T
namespace lyx { namespace lyx {
namespace frontend { 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 // DocumentDialog
@ -481,13 +545,9 @@ QDocumentDialog::QDocumentDialog(QDocument * form)
this, SLOT(change_adaptor())); this, SLOT(change_adaptor()));
// preamble // preamble
preambleModule = new UiWidget<Ui::PreambleUi>; preambleModule = new PreambleModule;
connect(preambleModule->preambleTE, SIGNAL(textChanged()), connect(preambleModule, SIGNAL(changed()),
this, SLOT(change_adaptor())); this, SLOT(change_adaptor()));
// This is not a memory leak. The object will be destroyed
// with preambleModule.
(void) new LaTeXHighlighter(preambleModule->preambleTE->document());
// bullets // bullets
bulletsModule = new BulletsModule; bulletsModule = new BulletsModule;
@ -770,8 +830,7 @@ void QDocumentDialog::updateNumbering()
void QDocumentDialog::apply(BufferParams & params) void QDocumentDialog::apply(BufferParams & params)
{ {
// preamble // preamble
params.preamble = preambleModule->apply(params);
fromqstr(preambleModule->preambleTE->document()->toPlainText());
// biblio // biblio
params.setCiteEngine(biblio::ENGINE_BASIC); params.setCiteEngine(biblio::ENGINE_BASIC);
@ -1050,9 +1109,7 @@ void QDocumentDialog::updateParams(BufferParams const & params)
} }
// preamble // preamble
QString preamble = toqstr(params.preamble); preambleModule->update(params, form_->controller().id());
if (preamble != preambleModule->preambleTE->document()->toPlainText())
preambleModule->preambleTE->document()->setPlainText(preamble);
// biblio // biblio
biblioModule->citeDefaultRB->setChecked( biblioModule->citeDefaultRB->setChecked(
@ -1291,7 +1348,6 @@ void QDocumentDialog::updateParams(BufferParams const & params)
} }
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
// //
// Document // Document

View File

@ -25,10 +25,13 @@
#include "ui/BiblioUi.h" #include "ui/BiblioUi.h"
#include "ui/NumberingUi.h" #include "ui/NumberingUi.h"
#include "ui/MarginsUi.h" #include "ui/MarginsUi.h"
// For the Preamble module
#include "ui/PreambleUi.h" #include "ui/PreambleUi.h"
#include <QCloseEvent> #include <QCloseEvent>
#include <QDialog> #include <QDialog>
#include <vector> #include <vector>
#include <string> #include <string>
@ -49,6 +52,7 @@ namespace frontend {
class QBranches; class QBranches;
class QDocument; class QDocument;
class PreambleModule;
class QDocumentDialog : public QDialog, public Ui::QDocumentUi { class QDocumentDialog : public QDialog, public Ui::QDocumentUi {
Q_OBJECT Q_OBJECT
@ -99,7 +103,7 @@ private:
UiWidget<Ui::BiblioUi> *biblioModule; UiWidget<Ui::BiblioUi> *biblioModule;
UiWidget<Ui::MathsUi> *mathsModule; UiWidget<Ui::MathsUi> *mathsModule;
UiWidget<Ui::LaTeXUi> *latexModule; UiWidget<Ui::LaTeXUi> *latexModule;
UiWidget<Ui::PreambleUi> *preambleModule; PreambleModule *preambleModule;
QBranches *branchesModule; QBranches *branchesModule;
@ -113,7 +117,6 @@ private:
}; };
class ControlDocument; class ControlDocument;
class QDocument class QDocument
@ -140,6 +143,31 @@ private:
void useClassDefaults(); 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 frontend
} // namespace lyx } // namespace lyx