I have continued a bit on my track to do a real model view separation for the Citation Dialog. In this new scheme, QCitation is the controller and the model at the same time, it inherits ControlCitation and it doesn't know about the view. QCitationDialog is the view, it is using QCitation to get its model and for communication with the core; it is inheriting Dialog::View directly.

In frontend/qt4/Dialog.C, we use these class like this:

    } else if (name == "citation") {
        QCitation * ci = new QCitation(*dialog);
        dialog->setController(ci);
        dialog->setView(new QCitationDialog(*dialog, ci));
        dialog->bc().bp(new NoRepeatedApplyReadOnlyPolicy);

Now, it should be possible to define another view like this:

    } else if (name == "citation-inline") {
        QCitation * ci = new QCitation(*dialog);
        dialog->setController(ci);
        dialog->setView(new QCitationInline(*dialog, ci));

All the citation functionalities are not there yet but the basic ones are there. There are still a few "intelligence" still to be transfered from the view to the dialog.



git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@13635 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2006-04-11 08:26:43 +00:00
parent cc0a9f811c
commit 31d7f2ed30
5 changed files with 225 additions and 164 deletions

View File

@ -53,6 +53,7 @@
#include "QChanges.h"
#include "QCharacter.h"
#include "QCitation.h"
#include "QCitationDialog.h"
#include "QDocument.h"
#include "QErrorList.h"
#include "QERT.h"
@ -173,8 +174,9 @@ Dialogs::DialogPtr Dialogs::build(string const & name)
dialog->setView(new QCharacter(*dialog));
dialog->bc().bp(new OkApplyCancelReadOnlyPolicy);
} else if (name == "citation") {
dialog->setController(new ControlCitation(*dialog));
dialog->setView(new QCitation(*dialog));
QCitation * ci = new QCitation(*dialog);
dialog->setController(ci);
dialog->setView(new QCitationDialog(*dialog, ci));
dialog->bc().bp(new NoRepeatedApplyReadOnlyPolicy);
} else if (name == "document" || name == "preamble") {

View File

@ -11,8 +11,8 @@
#include <config.h>
#include "ControlCitation.h"
#include "QCitation.h"
#include "QCitationDialog.h"
#include "Qt2BC.h"
#include "qt_helpers.h"
@ -32,16 +32,20 @@ using std::endl;
using std::vector;
using std::string;
void toQStringList(QStringList & qlist, vector<string> const & v)
QStringList toQStringList(vector<string> const & v)
{
qlist.clear();
QStringList qlist;
for (size_t i=0; i != v.size(); ++i) {
if (v[i].empty())
continue;
qlist.append(toqstr(v[i]));
}
return qlist;
}
void toVector(vector<string> & v, const QStringList & qlist)
{
v.clear();
@ -50,32 +54,40 @@ void toVector(vector<string> & v, const QStringList & qlist)
v.push_back(fromqstr(qlist[i]));
}
namespace lyx {
namespace frontend {
typedef QController<ControlCitation, QView<QCitationDialog> > base_class;
QCitation::QCitation(Dialog & parent)
: base_class(parent, _("Citation"))
: ControlCitation(parent)
{
}
void QCitation::apply()
void QCitation::apply(int const choice, bool const full, bool const force,
QString before, QString after)
{
InsetCommandParams & params = controller().params();
dialog_->apply(params);
// InsetCommandParams & params = params();
vector<biblio::CiteStyle> const & styles =
ControlCitation::getCiteStyles();
string const command =
biblio::CitationStyle(styles[choice], full, force)
.asLatexStr();
params().setContents(fromqstr(selected_keys_.stringList().join(",")));
params().setSecOptions(fromqstr(before));
params().setOptions(fromqstr(after));
dispatchParams();
params.setContents(fromqstr(selected_keys_.stringList().join("'")));
/*
if (dialog().controller().isBufferDependent()) {
if (!dialog().kernel().isBufferAvailable() ||
dialog().kernel().isBufferReadonly())
return;
}
dialog().view().apply();
dialog().controller().dispatchParams();
if (dialog().controller().disconnectOnApply()) {
@ -84,48 +96,42 @@ void QCitation::apply()
dialog().view().update();
}
*/
// dialog().ApplyButton();
// dialog().apply();
}
void QCitation::build_dialog()
QString QCitation::textBefore()
{
dialog_.reset(new QCitationDialog(this));
return toqstr(params().getSecOptions());
}
void QCitation::update_contents()
QString QCitation::textAfter()
{
QStringList keys;
return toqstr(params().getOptions());
}
void QCitation::updateModel()
{
// Make the list of all available bibliography keys
toQStringList(keys,
biblio::getKeys(controller().bibkeysInfo()));
QStringList keys = toQStringList(biblio::getKeys(bibkeysInfo()));
available_keys_.setStringList(keys);
// Ditto for the keys cited in this inset
QString str = toqstr(controller().params().getContents());
QString str = toqstr(params().getContents());
if (!str.isEmpty()) {
keys = str.split(",");
selected_keys_.setStringList(keys);
}
dialog_->update(controller().params());
bc().valid(isValid());
}
void QCitation::hide()
{
QDialogView::hide();
}
bool QCitation::isValid()
{
return selected_keys_.rowCount() > 0;
}
QModelIndex QCitation::findKey(QString const & str, QModelIndex const & index) const
{
QStringList const avail = available_keys_.stringList();
@ -135,6 +141,7 @@ QModelIndex QCitation::findKey(QString const & str, QModelIndex const & index) c
return available_keys_.index(pos);
}
QModelIndex QCitation::findKey(QString const & str) const
{
cout << "Find text " << fromqstr(str) << endl;
@ -150,10 +157,9 @@ QModelIndex QCitation::findKey(QString const & str) const
return available_keys_.index(pos);
}
void QCitation::addKeys(QModelIndexList const & indexes)
{
// = selectionModel->selectedIndexes();
QModelIndex index;
if (indexes.empty())
@ -167,10 +173,9 @@ void QCitation::addKeys(QModelIndexList const & indexes)
}
selected_keys_.setStringList(keys);
changed();
}
void QCitation::deleteKeys(QModelIndexList const & indexes)
{
QModelIndex index;
@ -187,10 +192,9 @@ void QCitation::deleteKeys(QModelIndexList const & indexes)
}
selected_keys_.setStringList(keys);
changed();
}
void QCitation::upKey(QModelIndexList const & indexes)
{
if (indexes.empty() || indexes.size() > 1)
@ -203,10 +207,9 @@ void QCitation::upKey(QModelIndexList const & indexes)
QStringList keys = selected_keys_.stringList();
keys.swap(pos, pos-1);
selected_keys_.setStringList(keys);
changed();
}
void QCitation::downKey(QModelIndexList const & indexes)
{
if (indexes.empty() || indexes.size() > 1)
@ -219,8 +222,14 @@ void QCitation::downKey(QModelIndexList const & indexes)
QStringList keys = selected_keys_.stringList();
keys.swap(pos, pos+1);
selected_keys_.setStringList(keys);
}
changed();
QStringList QCitation::citationStyles(int sel)
{
string key = fromqstr(selected_keys_.stringList()[sel]);
return toQStringList(getCiteStrings(key));
}
} // namespace frontend

View File

@ -13,21 +13,16 @@
#ifndef QCITATION_H
#define QCITATION_H
#include "QDialogView.h"
#include "ControlCitation.h"
#include <QStringListModel>
namespace lyx {
namespace frontend {
class ControlCitation;
class QCitationDialog;
class QCitation : public QController<ControlCitation, QView<QCitationDialog> >
class QCitation : public ControlCitation
{
public:
friend class QCitationDialog;
///
QCitation(Dialog &);
@ -40,6 +35,9 @@ public:
QStringListModel * found()
{ return &found_keys_; }
QString textBefore();
QString textAfter();
QModelIndex findKey(QString const & str, QModelIndex const & index) const;
QModelIndex findKey(QString const & str) const;
@ -48,20 +46,19 @@ public:
void upKey(QModelIndexList const & indexes);
void downKey(QModelIndexList const & indexes);
protected:
QStringList citationStyles(int sel);
virtual bool isValid();
private:
/// Set the Params variable for the Controller.
virtual void apply();
/// Build the dialog.
virtual void build_dialog();
/// Hide the dialog.
virtual void hide();
/// Update dialog before/whilst showing it.
virtual void update_contents();
virtual void apply(int const choice, bool const full, bool const force,
QString before, QString After);
/// Update dialog before/whilst showing it.
virtual void updateModel();
private:
/// available keys
QStringListModel available_keys_;
@ -72,6 +69,47 @@ private:
QStringListModel found_keys_;
};
/** A controller for Citation dialogs.
*/
/*
class Citation {
public:
///
Citation();
///
virtual bool initialiseParams(std::string const & data);
/// clean-up on hide.
virtual void clearParams();
/** Disconnect from the inset when the Apply button is pressed.
* Allows easy insertion of multiple citations.
*/
/* virtual bool disconnectOnApply() const { return true; }
/// Returns a reference to the map of stored keys
biblio::InfoMap const & bibkeysInfo() const;
///
biblio::CiteEngine_enum getEngine() const;
/// Possible citations based on this key
std::vector<std::string> const getCiteStrings(std::string const & key) const;
/// available CiteStyle-s (depends on availability of Natbib/Jurabib)
static std::vector<biblio::CiteStyle> const & getCiteStyles() {
return citeStyles_;
}
private:
/// The info associated with each key
biblio::InfoMap bibkeysInfo_;
///
static std::vector<biblio::CiteStyle> citeStyles_;
};
*/
} // namespace frontend
} // namespace lyx

View File

@ -42,49 +42,32 @@ using support::trim;
namespace frontend {
void updateBrowser(Q3ListBox * browser,
vector<string> const & keys)
{
browser->clear();
for (vector<string>::const_iterator it = keys.begin();
it < keys.end(); ++it) {
string const key = trim(*it);
// FIXME: why the .empty() test ?
if (!key.empty())
browser->insertItem(toqstr(key));
}
}
QCitationDialog::QCitationDialog(QCitation * form)
: form_(form)
QCitationDialog::QCitationDialog(Dialog & dialog, QCitation * form)
: Dialog::View(dialog, "Citation"), form_(form)
{
setupUi(this);
/* connect(restorePB, SIGNAL(clicked()),
form, SLOT(slotRestore()));
connect(okPB, SIGNAL(clicked()),
form, SLOT(slotOK()));
connect(applyPB, SIGNAL(clicked()),
form, SLOT(slotApply()));
connect(closePB, SIGNAL(clicked()),
form, SLOT(slotClose()));
*/
// Manage the ok, apply, restore and cancel/close buttons
form_->bcview().setOK(okPB);
form_->bcview().setApply(applyPB);
form_->bcview().setCancel(closePB);
form_->bcview().setRestore(restorePB);
setCaption(toqstr("LyX: " + getTitle()));
form_->bcview().addReadOnly(addPB);
form_->bcview().addReadOnly(deletePB);
form_->bcview().addReadOnly(upPB);
form_->bcview().addReadOnly(downPB);
form_->bcview().addReadOnly(citationStyleCO);
form_->bcview().addReadOnly(forceuppercaseCB);
form_->bcview().addReadOnly(fulllistCB);
form_->bcview().addReadOnly(textBeforeED);
form_->bcview().addReadOnly(textAfterED);
/*
// Manage the ok, apply, restore and cancel/close buttons
bcview().setOK(okPB);
bcview().setApply(applyPB);
bcview().setCancel(closePB);
bcview().setRestore(restorePB);
bcview().addReadOnly(addPB);
bcview().addReadOnly(deletePB);
bcview().addReadOnly(upPB);
bcview().addReadOnly(downPB);
bcview().addReadOnly(citationStyleCO);
bcview().addReadOnly(forceuppercaseCB);
bcview().addReadOnly(fulllistCB);
bcview().addReadOnly(textBeforeED);
bcview().addReadOnly(textAfterED);
*/
selectedLV->setModel(form_->selected());
availableLV->setModel(form_->available());
@ -99,7 +82,7 @@ QCitationDialog::QCitationDialog(QCitation * form)
// find_ = new QCitationFind(form_, this);
// connect(selectedLV, SIGNAL(doubleClicked(const QModelIndex & index)),
// form_, SLOT(on_okPB_clicked()));//SLOT(slotOK()));
}
@ -108,73 +91,78 @@ QCitationDialog::~QCitationDialog()
{
}
void QCitationDialog::apply()
{
int const choice = std::max(0, citationStyleCO->currentItem());
bool const full = fulllistCB->isChecked();
bool const force = forceuppercaseCB->isChecked();
QString const before = textBeforeED->text();
QString const after = textAfterED->text();
form_->apply(choice, full, force, before, after);
}
void QCitationDialog::hide()
{
accept();
}
void QCitationDialog::show()
{
QDialog::show();
}
bool QCitationDialog::isVisible() const
{
return QDialog::isVisible();
}
void QCitationDialog::on_okPB_clicked()
{
form_->apply();
apply();
accept();
}
void QCitationDialog::on_cancelPB_clicked()
{
reject();
accept();
// reject();
}
void QCitationDialog::on_applyPB_clicked()
{
form_->apply();
apply();
}
void QCitationDialog::on_restorePB_clicked()
{
form_->update_contents();
update();
bc().valid(form_->isValid() );
}
void QCitationDialog::apply(InsetCommandParams & params)
void QCitationDialog::update()
{
vector<biblio::CiteStyle> const & styles =
ControlCitation::getCiteStyles();
form_->updateModel();
int const choice = std::max(0, citationStyleCO->currentItem());
bool const full = fulllistCB->isChecked();
bool const force = forceuppercaseCB->isChecked();
string const command =
biblio::CitationStyle(styles[choice], full, force)
.asLatexStr();
params.setCmdName(command);
string const before = fromqstr(textBeforeED->text());
params.setSecOptions(before);
string const after = fromqstr(textAfterED->text());
params.setOptions(after);
style_ = choice;
}
void QCitationDialog::update(InsetCommandParams const & params)
{
// No keys have been selected yet, so...
infoML->document()->clear();
setButtons();
textBeforeED->setText(
toqstr(params.getSecOptions()));
textAfterED->setText(
toqstr(params.getOptions()));
textBeforeED->setText(form_->textBefore());
textAfterED->setText(form_->textAfter());
fillStyles();
updateStyle();
// find_->update();
}
void QCitationDialog::updateStyle()
{
biblio::CiteEngine const engine = form_->controller().getEngine();
biblio::CiteEngine const engine = form_->getEngine();
bool const natbib_engine =
engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
engine == biblio::ENGINE_NATBIB_NUMERICAL;
@ -184,7 +172,7 @@ void QCitationDialog::updateStyle()
forceuppercaseCB->setEnabled(natbib_engine);
textBeforeED->setEnabled(!basic_engine);
string const & command = form_->controller().params().getCmdName();
string const & command = form_->params().getCmdName();
// Find the style of the citekeys
vector<biblio::CiteStyle> const & styles =
@ -199,6 +187,7 @@ void QCitationDialog::updateStyle()
citationStyleCO->setCurrentItem(style_);
else
citationStyleCO->setCurrentItem(0);
fulllistCB->setChecked(false);
forceuppercaseCB->setChecked(false);
@ -213,39 +202,31 @@ void QCitationDialog::updateStyle()
void QCitationDialog::fillStyles()
{
if (citekeys.empty()) {
citationStyleCO->setEnabled(false);
citationStyleLA->setEnabled(false);
return;
}
int const orig = citationStyleCO->currentItem();
citationStyleCO->clear();
QStringList selected_keys = form_->selected()->stringList();
if (selected_keys.empty())
if (selected_keys.empty()) {
citationStyleCO->setEnabled(false);
citationStyleLA->setEnabled(false);
return;
}
if (selectedLV->selectionModel()->selectedIndexes().empty())
return;
int curr = selectedLV->selectionModel()->selectedIndexes()[0].row();//selectedLV->currentItem();
string key = fromqstr(selected_keys[curr]);
QStringList sty = form_->citationStyles(curr);
vector<string> const & sty = form_->controller().getCiteStrings(key);
bool const basic_engine =
(form_->getEngine() == biblio::ENGINE_BASIC);
biblio::CiteEngine const engine = form_->controller().getEngine();
bool const basic_engine = engine == biblio::ENGINE_BASIC;
citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
citationStyleCO->setEnabled(!sty.empty() && !basic_engine);
citationStyleLA->setEnabled(!sty.empty() && !basic_engine);
for (vector<string>::const_iterator it = sty.begin();
it != sty.end(); ++it) {
citationStyleCO->insertItem(toqstr(*it));
}
citationStyleCO->insertItems(0, sty);
if (orig != -1 && orig < citationStyleCO->count())
citationStyleCO->setCurrentItem(orig);
@ -254,14 +235,14 @@ void QCitationDialog::fillStyles()
void QCitationDialog::setButtons()
{
if (form_->readOnly())
return;
// if (form_->readOnly())
// return;
int const row_count = selectedLV->model()->rowCount();
int sel_nr=-1;
if (! selectedLV->selectionModel()->selectedIndexes().empty()) {
sel_nr =
sel_nr =
selectedLV->selectionModel()->selectedIndexes()[0].row();
}
@ -292,11 +273,12 @@ void QCitationDialog::on_selectedLV_currentChanged(Q3ListBoxItem*)
void QCitationDialog::on_addPB_clicked()
{
form_->addKeys(availableLV->selectionModel()->selectedIndexes());
changed();
}
void QCitationDialog::on_deletePB_clicked()
{
form_->addKeys(selectedLV->selectionModel()->selectedIndexes());
form_->deleteKeys(selectedLV->selectionModel()->selectedIndexes());
changed();
}
@ -339,6 +321,20 @@ void QCitationDialog::changed()
}
void updateBrowser(Q3ListBox * browser,
vector<string> const & keys)
{
browser->clear();
for (vector<string>::const_iterator it = keys.begin();
it < keys.end(); ++it) {
string const key = trim(*it);
// FIXME: why the .empty() test ?
if (!key.empty())
browser->insertItem(toqstr(key));
}
}
QCitationFind::QCitationFind(QCitation * form, QWidget * parent, Qt::WFlags f)
: form_(form), QDialog(parent, f)
@ -419,7 +415,7 @@ void QCitationFind::find(biblio::Direction dir)
{
/* QStringList bibkeys = form_->available()->stringList();
biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
biblio::InfoMap const & theMap = form_->bibkeysInfo();
biblio::Search const type = searchTypeCB->isChecked()
? biblio::REGEX : biblio::SIMPLE;

View File

@ -12,6 +12,8 @@
#ifndef QCITATIONDIALOG_H
#define QCITATIONDIALOG_H
#include "Dialog.h"
#include "ui/QCitationUi.h"
#include "ui/QCitationFindUi.h"
#include "controllers/biblio.h"
@ -30,23 +32,37 @@ namespace frontend {
class QCitation;
class QCitationFind;
class QCitationDialog : public QDialog, public Ui::QCitationUi {
class QCitationDialog: public QDialog, public Ui::QCitationUi, public Dialog::View {
Q_OBJECT
public:
QCitationDialog(QCitation * form);
QCitationDialog(Dialog &, QCitation * form );
~QCitationDialog();
//QCitationDialog(QCitation * form);
void update(InsetCommandParams const & params);
void apply(InsetCommandParams & params);
virtual ~QCitationDialog();
// virtual bool isValid();
virtual void apply();
/// Hide the dialog from sight
void hide();
/// Redraw the dialog (e.g. if the colors have been remapped).
void redraw() {}
/// Create the dialog if necessary, update it and display it.
void show();
/// Update the display of the dialog whilst it is still visible.
void update();
/// \return true if the dialog is visible.
bool isVisible() const;
protected slots:
// void on_selectedLB_currentChanged(Q3ListBoxItem*);
void on_okPB_clicked();
void on_cancelPB_clicked();
void on_restorePB_clicked();