#include <config.h>
+#include "ControlCitation.h"
#include "QCitation.h"
-#include "QCitationDialog.h"
-#include "ui/QCitationFindUi.h"
#include "Qt2BC.h"
#include "qt_helpers.h"
#include "support/lstrings.h"
-#include <qcheckbox.h>
-#include <qlineedit.h>
-#include <q3listbox.h>
-#include <q3multilineedit.h>
-#include <qpushbutton.h>
-#include <qlabel.h>
+#include <vector>
+#include <string>
+#include <iostream>
+using std::cout;
+using std::endl;
-using std::find;
-using std::string;
using std::vector;
+using std::string;
+
+
+QStringList toQStringList(vector<string> const & v)
+{
+ QStringList qlist;
+
+ for (size_t i=0; i != v.size(); ++i) {
+ if (v[i].empty())
+ continue;
+ qlist.append(toqstr(v[i]));
+ }
+ return qlist;
+}
-namespace lyx {
-using support::getStringFromVector;
-using support::getVectorFromString;
-using support::trim;
+void toVector(vector<string> & v, const QStringList & qlist)
+{
+ v.clear();
+ for (size_t i=0; i != qlist.size(); ++i)
+ 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 = params();
+
vector<biblio::CiteStyle> const & styles =
ControlCitation::getCiteStyles();
- int const choice = dialog_->citationStyleCO->currentItem();
- bool const full = dialog_->fulllistCB->isChecked();
- bool const force = dialog_->forceuppercaseCB->isChecked();
-
string const command =
biblio::CitationStyle(styles[choice], full, force)
.asLatexStr();
- controller().params().setCmdName(command);
- controller().params().setContents(getStringFromVector(citekeys));
+ params().setContents(fromqstr(selected_keys_.stringList().join(",")));
+ params().setSecOptions(fromqstr(before));
+ params().setOptions(fromqstr(after));
+ dispatchParams();
- string const before = fromqstr(dialog_->textBeforeED->text());
- controller().params().setSecOptions(before);
-
- string const after = fromqstr(dialog_->textAfterED->text());
- controller().params().setOptions(after);
+/*
+ if (dialog().controller().isBufferDependent()) {
+ if (!dialog().kernel().isBufferAvailable() ||
+ dialog().kernel().isBufferReadonly())
+ return;
+ }
+ dialog().controller().dispatchParams();
- style_ = choice;
- open_find_ = false;
+ if (dialog().controller().disconnectOnApply()) {
+ dialog().kernel().disconnect(name());
+ dialog().controller().initialiseParams(string());
+ dialog().view().update();
+ }
+*/
}
-void QCitation::hide()
+QString QCitation::textBefore()
{
- citekeys.clear();
- bibkeys.clear();
- open_find_ = true;
-
- QDialogView::hide();
+ return toqstr(params().getSecOptions());
}
-void QCitation::build_dialog()
+QString QCitation::textAfter()
{
- dialog_.reset(new QCitationDialog(this));
-
- // Manage the ok, apply, restore and cancel/close buttons
- bcview().setOK(dialog_->okPB);
- bcview().setApply(dialog_->applyPB);
- bcview().setCancel(dialog_->closePB);
- bcview().setRestore(dialog_->restorePB);
-
- bcview().addReadOnly(dialog_->addPB);
- bcview().addReadOnly(dialog_->deletePB);
- bcview().addReadOnly(dialog_->upPB);
- bcview().addReadOnly(dialog_->downPB);
- bcview().addReadOnly(dialog_->citationStyleCO);
- bcview().addReadOnly(dialog_->forceuppercaseCB);
- bcview().addReadOnly(dialog_->fulllistCB);
- bcview().addReadOnly(dialog_->textBeforeED);
- bcview().addReadOnly(dialog_->textAfterED);
-
- open_find_ = true;
+ return toqstr(params().getOptions());
}
-void QCitation::fillStyles()
+void QCitation::updateModel()
{
- if (citekeys.empty()) {
- dialog_->citationStyleCO->setEnabled(false);
- dialog_->citationStyleLA->setEnabled(false);
- return;
+ // Make the list of all available bibliography keys
+ QStringList keys = toQStringList(biblio::getKeys(bibkeysInfo()));
+ available_keys_.setStringList(keys);
+
+ // Ditto for the keys cited in this inset
+ QString str = toqstr(params().getContents());
+ if (!str.isEmpty()) {
+ keys = str.split(",");
+ selected_keys_.setStringList(keys);
}
+}
- int const orig = dialog_->citationStyleCO->currentItem();
- dialog_->citationStyleCO->clear();
+bool QCitation::isValid()
+{
+ return selected_keys_.rowCount() > 0;
+}
- int curr = dialog_->selectedLB->currentItem();
- if (curr < 0)
- curr = 0;
- string key = citekeys[curr];
+QModelIndex QCitation::findKey(QString const & str, QModelIndex const & index) const
+{
+ QStringList const avail = available_keys_.stringList();
+ int const pos = avail.indexOf(str, index.row());
+ if (pos == -1)
+ return index;
+ return available_keys_.index(pos);
+}
- vector<string> const & sty = controller().getCiteStrings(key);
- biblio::CiteEngine const engine = controller().getEngine();
- bool const basic_engine = engine == biblio::ENGINE_BASIC;
+QModelIndex QCitation::findKey(QString const & str) const
+{
+ cout << "Find text " << fromqstr(str) << endl;
- dialog_->citationStyleCO->setEnabled(!sty.empty() && !basic_engine);
- dialog_->citationStyleLA->setEnabled(!sty.empty() && !basic_engine);
+ QStringList const avail = available_keys_.stringList();
+ QRegExp reg_exp(str);
- for (vector<string>::const_iterator it = sty.begin();
- it != sty.end(); ++it) {
- dialog_->citationStyleCO->insertItem(toqstr(*it));
- }
+ int const pos = avail.indexOf(reg_exp);
+ if (pos == -1)
+ return QModelIndex();
- if (orig != -1 && orig < dialog_->citationStyleCO->count())
- dialog_->citationStyleCO->setCurrentItem(orig);
+ cout << "found key " << fromqstr(avail[pos]) << " at pos " << pos << endl;
+ return available_keys_.index(pos);
}
-void QCitation::updateStyle()
+void QCitation::addKeys(QModelIndexList const & indexes)
{
- biblio::CiteEngine const engine = controller().getEngine();
- bool const natbib_engine =
- engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
- engine == biblio::ENGINE_NATBIB_NUMERICAL;
- bool const basic_engine = engine == biblio::ENGINE_BASIC;
+ QModelIndex index;
- dialog_->fulllistCB->setEnabled(natbib_engine);
- dialog_->forceuppercaseCB->setEnabled(natbib_engine);
- dialog_->textBeforeED->setEnabled(!basic_engine);
+ if (indexes.empty())
+ return;
- string const & command = controller().params().getCmdName();
+ QStringList keys = selected_keys_.stringList();
- // Find the style of the citekeys
- vector<biblio::CiteStyle> const & styles =
- ControlCitation::getCiteStyles();
- biblio::CitationStyle const cs(command);
-
- vector<biblio::CiteStyle>::const_iterator cit =
- find(styles.begin(), styles.end(), cs.style);
-
- // restore the latest natbib style
- if (style_ >= 0 && style_ < dialog_->citationStyleCO->count())
- dialog_->citationStyleCO->setCurrentItem(style_);
- else
- dialog_->citationStyleCO->setCurrentItem(0);
- dialog_->fulllistCB->setChecked(false);
- dialog_->forceuppercaseCB->setChecked(false);
-
- if (cit != styles.end()) {
- int const i = int(cit - styles.begin());
- dialog_->citationStyleCO->setCurrentItem(i);
- dialog_->fulllistCB->setChecked(cs.full);
- dialog_->forceuppercaseCB->setChecked(cs.forceUCase);
+ foreach(index, indexes) {
+ if (keys.indexOf(index.data().toString()) == -1)
+ keys.append(index.data().toString());
}
+
+ selected_keys_.setStringList(keys);
}
-void QCitation::update_contents()
+void QCitation::deleteKeys(QModelIndexList const & indexes)
{
- // Make the list of all available bibliography keys
- bibkeys = biblio::getKeys(controller().bibkeysInfo());
- updateBrowser(dialog_->ui_.availableLB, bibkeys);
+ QModelIndex index;
- // Ditto for the keys cited in this inset
- citekeys = getVectorFromString(controller().params().getContents());
- updateBrowser(dialog_->selectedLB, citekeys);
+ if (indexes.empty())
+ return;
+
+ QStringList keys = selected_keys_.stringList();
- // No keys have been selected yet, so...
- dialog_->infoML->clear();
- dialog_->setButtons();
+ foreach(index, indexes) {
+ int const pos = keys.indexOf(index.data().toString());
+ if (pos != -1)
+ keys.removeAt(pos);
+ }
- dialog_->textBeforeED->setText(
- toqstr(controller().params().getSecOptions()));
- dialog_->textAfterED->setText(
- toqstr(controller().params().getOptions()));
+ selected_keys_.setStringList(keys);
+}
- fillStyles();
- updateStyle();
- // open the find dialog if nothing has been selected (yet)
- // the bool prevents that this is also done after "apply"
- if (open_find_)
- dialog_->openFind();
+void QCitation::upKey(QModelIndexList const & indexes)
+{
+ if (indexes.empty() || indexes.size() > 1)
+ return;
- bc().valid(isValid());
+ int pos = indexes[0].row();
+ if (pos < 1)
+ return;
+
+ QStringList keys = selected_keys_.stringList();
+ keys.swap(pos, pos-1);
+ selected_keys_.setStringList(keys);
}
-void QCitation::updateBrowser(Q3ListBox * browser,
- vector<string> const & keys) const
+void QCitation::downKey(QModelIndexList const & indexes)
{
- 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));
- }
+ if (indexes.empty() || indexes.size() > 1)
+ return;
+
+ int pos = indexes[0].row();
+ if (pos >= selected_keys_.rowCount() - 1)
+ return;
+
+ QStringList keys = selected_keys_.stringList();
+ keys.swap(pos, pos+1);
+ selected_keys_.setStringList(keys);
}
-bool QCitation::isValid()
+QStringList QCitation::citationStyles(int sel)
{
- return dialog_->selectedLB->count() > 0;
-}
+ string key = fromqstr(selected_keys_.stringList()[sel]);
+ return toQStringList(getCiteStrings(key));
+}
} // namespace frontend
} // namespace lyx