]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiCitation.cpp
Compil fix.
[lyx.git] / src / frontends / qt4 / GuiCitation.cpp
index 10e6d9bdaa82be936e49e13808994ede6a7f1399..abc30048c296dd335e28cfaf5b35e8d7cd98c9d9 100644 (file)
 
 #include "GuiCitation.h"
 
-#include "support/debug.h"
-#include "support/gettext.h"
+#include "GuiSelectionManager.h"
 #include "qt_helpers.h"
+
 #include "Buffer.h"
+#include "BiblioInfo.h"
 #include "BufferParams.h"
-
-#include "support/lstrings.h"
-#include "support/docstring.h"
+#include "FuncRequest.h"
 
 #include "insets/InsetCommand.h"
 
-#include <vector>
-#include <string>
+#include "support/debug.h"
+#include "support/docstring.h"
+#include "support/gettext.h"
+#include "support/lstrings.h"
 
 #include <QCloseEvent>
+#include <QSettings>
 #include <QShowEvent>
+#include <QVariant>
+
+#include <vector>
+#include <string>
 
 #undef KeyPress
 
@@ -46,7 +52,7 @@ using namespace lyx::support;
 namespace lyx {
 namespace frontend {
 
-static vector<biblio::CiteStyle> citeStyles_;
+static vector<CiteStyle> citeStyles_;
 
 
 template<typename String>
@@ -76,10 +82,10 @@ static vector<lyx::docstring> to_docstring_vector(QStringList const & qlist)
 
 
 GuiCitation::GuiCitation(GuiView & lv)
-       : GuiCommand(lv, "citation")
+       : DialogView(lv, "citation", qt_("Citation")),
+         params_(insetCode("citation"))
 {
        setupUi(this);
-       setViewTitle(_("Citation"));
 
        connect(citationStyleCO, SIGNAL(activated(int)),
                this, SLOT(on_citationStyleCO_currentIndexChanged(int)));
@@ -91,20 +97,32 @@ GuiCitation::GuiCitation(GuiView & lv)
                this, SLOT(changed()));
        connect(textAfterED, SIGNAL(textChanged(QString)),
                this, SLOT(changed()));
-       connect(clearPB, SIGNAL(clicked()),
-               findLE, SLOT(clear()));
+       connect(findLE, SIGNAL(returnPressed()), 
+               this, SLOT(on_searchPB_clicked()));
+       connect(textBeforeED, SIGNAL(returnPressed()),
+               this, SLOT(on_okPB_clicked()));
+       connect(textAfterED, SIGNAL(returnPressed()),
+               this, SLOT(on_okPB_clicked()));
+               
        connect(this, SIGNAL(rejected()), this, SLOT(cleanUp()));
 
        selectionManager = new GuiSelectionManager(availableLV, selectedLV, 
-                       addPB, deletePB, upPB, downPB, available(), selected());
+                       addPB, deletePB, upPB, downPB, &available_model_, &selected_model_);
        connect(selectionManager, SIGNAL(selectionChanged()),
                this, SLOT(setCitedKeys()));
        connect(selectionManager, SIGNAL(updateHook()),
-               this, SLOT(updateDialog()));
+               this, SLOT(updateControls()));
        connect(selectionManager, SIGNAL(okHook()),
                this, SLOT(on_okPB_clicked()));
 
-       bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
+       // FIXME: the sizeHint() for this is _way_ too high
+       infoML->setFixedHeight(60);
+}
+
+
+GuiCitation::~GuiCitation()
+{
+       delete selectionManager;
 }
 
 
@@ -120,13 +138,13 @@ void GuiCitation::closeEvent(QCloseEvent * e)
 {
        clearSelection();
        clearParams();
-       GuiDialog::closeEvent(e);
+       e->accept();
 }
 
 
 void GuiCitation::applyView()
 {
-       int  const choice = max(0, citationStyleCO->currentIndex());
+       int const choice = max(0, citationStyleCO->currentIndex());
        style_ = choice;
        bool const full  = fulllistCB->isChecked();
        bool const force = forceuppercaseCB->isChecked();
@@ -140,10 +158,9 @@ void GuiCitation::applyView()
 
 void GuiCitation::showEvent(QShowEvent * e)
 {
-       init();
        findLE->clear();
        availableLV->setFocus();
-       GuiDialog::showEvent(e);
+       DialogView::showEvent(e);
 }
 
 
@@ -171,16 +188,6 @@ void GuiCitation::on_applyPB_clicked()
 void GuiCitation::on_restorePB_clicked()
 {
        init();
-       updateView();
-}
-
-
-void GuiCitation::updateView()
-{
-       init();
-       fillFields();
-       fillEntries();
-       updateDialog();
 }
 
 
@@ -190,7 +197,7 @@ void GuiCitation::updateView()
 // will not have changed. At the moment, however, the division between
 // fillStyles() and updateStyle() doesn't lend itself to dividing the
 // two methods, though they should be divisible.
-void GuiCitation::updateDialog()
+void GuiCitation::updateControls()
 {
        if (selectionManager->selectedFocused()) { 
                if (selectedLV->selectionModel()->selectedIndexes().isEmpty())
@@ -205,25 +212,25 @@ void GuiCitation::updateDialog()
        }
        setButtons();
 
-       textBeforeED->setText(textBefore());
-       textAfterED->setText(textAfter());
+       textBeforeED->setText(toqstr(params_["before"]));
+       textAfterED->setText(toqstr(params_["after"]));
        fillStyles();
        updateStyle();
 }
 
 
-void GuiCitation::updateFormatting(biblio::CiteStyle currentStyle)
+void GuiCitation::updateFormatting(CiteStyle currentStyle)
 {
-       biblio::CiteEngine const engine = getEngine();
+       CiteEngine const engine = citeEngine();
        bool const natbib_engine =
-               engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
-               engine == biblio::ENGINE_NATBIB_NUMERICAL;
-       bool const basic_engine = engine == biblio::ENGINE_BASIC;
+               engine == ENGINE_NATBIB_AUTHORYEAR ||
+               engine == ENGINE_NATBIB_NUMERICAL;
+       bool const basic_engine = engine == ENGINE_BASIC;
 
        bool const haveSelection = 
                selectedLV->model()->rowCount() > 0;
 
-       bool const isNocite = currentStyle == biblio::NOCITE;
+       bool const isNocite = currentStyle == NOCITE;
 
        fulllistCB->setEnabled(natbib_engine && haveSelection && !isNocite);
        forceuppercaseCB->setEnabled(natbib_engine && haveSelection && !isNocite);
@@ -241,10 +248,10 @@ void GuiCitation::updateStyle()
        string const & command = params_.getCmdName();
 
        // Find the style of the citekeys
-       vector<biblio::CiteStyle> const & styles = citeStyles_;
-       biblio::CitationStyle const cs(command);
+       vector<CiteStyle> const & styles = citeStyles_;
+       CitationStyle const cs = citationStyleFromString(command);
 
-       vector<biblio::CiteStyle>::const_iterator cit =
+       vector<CiteStyle>::const_iterator cit =
                std::find(styles.begin(), styles.end(), cs.style);
 
        // restore the latest natbib style
@@ -257,7 +264,7 @@ void GuiCitation::updateStyle()
                int const i = int(cit - styles.begin());
                citationStyleCO->setCurrentIndex(i);
                fulllistCB->setChecked(cs.full);
-               forceuppercaseCB->setChecked(cs.forceUCase);
+               forceuppercaseCB->setChecked(cs.forceUpperCase);
        } else {
                fulllistCB->setChecked(false);
                forceuppercaseCB->setChecked(false);
@@ -265,16 +272,16 @@ void GuiCitation::updateStyle()
        updateFormatting(cs.style);
 }
 
-//This one needs to be called whenever citationStyleCO needs
-//to be updated---and this would be on anything that changes the
-//selection in selectedLV, or on a general update.
+// This one needs to be called whenever citationStyleCO needs
+// to be updated---and this would be on anything that changes the
+// selection in selectedLV, or on a general update.
 void GuiCitation::fillStyles()
 {
        int const oldIndex = citationStyleCO->currentIndex();
 
        citationStyleCO->clear();
 
-       QStringList selected_keys = selected()->stringList();
+       QStringList selected_keys = selected_model_.stringList();
        if (selected_keys.empty()) {
                citationStyleCO->setEnabled(false);
                citationStyleLA->setEnabled(false);
@@ -308,7 +315,7 @@ void GuiCitation::fillFields()
        fieldsCO->blockSignals(true);
        int const oldIndex = fieldsCO->currentIndex();
        fieldsCO->clear();
-       QStringList const & fields = getFieldsAsQStringList();
+       QStringList const fields = to_qstring_list(bibInfo().getFields());
        fieldsCO->insertItem(0, qt_("All Fields"));
        fieldsCO->insertItem(1, qt_("Keys"));
        fieldsCO->insertItems(2, fields);
@@ -323,7 +330,7 @@ void GuiCitation::fillEntries()
        entriesCO->blockSignals(true);
        int const oldIndex = entriesCO->currentIndex();
        entriesCO->clear();
-       QStringList const & entries = getEntriesAsQStringList();
+       QStringList const entries = to_qstring_list(bibInfo().getEntries());
        entriesCO->insertItem(0, qt_("All Entry Types"));
        entriesCO->insertItems(1, entries);
        if (oldIndex != -1 && oldIndex < entriesCO->count())
@@ -335,7 +342,7 @@ void GuiCitation::fillEntries()
 bool GuiCitation::isSelected(const QModelIndex & idx)
 {
        QString const str = idx.data().toString();
-       return selected()->stringList().contains(str);
+       return selected_model_.stringList().contains(str);
 }
 
 
@@ -350,11 +357,14 @@ void GuiCitation::setButtons()
 
 void GuiCitation::updateInfo(QModelIndex const & idx)
 {
-       if (idx.isValid()) {
-               QString const keytxt = getKeyInfo(idx.data().toString());
-               infoML->document()->setPlainText(keytxt);
-       } else
+       if (!idx.isValid() || bibInfo().empty()) {
                infoML->document()->clear();
+               return;
+       }
+
+       QString const keytxt = toqstr(
+               bibInfo().getInfo(qstring_to_ucs4(idx.data().toString())));
+       infoML->document()->setPlainText(keytxt);
 }
 
 
@@ -362,7 +372,7 @@ void GuiCitation::findText(QString const & text, bool reset)
 {
        //"All Fields" and "Keys" are the first two
        int index = fieldsCO->currentIndex() - 2; 
-       vector<docstring> const & fields = availableFields();
+       vector<docstring> const & fields = bibInfo().getFields();
        docstring field;
        
        if (index <= -1 || index >= int(fields.size()))
@@ -376,23 +386,23 @@ void GuiCitation::findText(QString const & text, bool reset)
        
        //"All Entry Types" is first.
        index = entriesCO->currentIndex() - 1; 
-       vector<docstring> const & entries = availableEntries();
-       docstring entryType;
+       vector<docstring> const & entries = bibInfo().getEntries();
+       docstring entry_type;
        if (index < 0 || index >= int(entries.size()))
-               entryType = from_ascii("");
+               entry_type = from_ascii("");
        else 
-               entryType = entries[index];
+               entry_type = entries[index];
        
        bool const case_sentitive = caseCB->checkState();
        bool const reg_exp = regexCB->checkState();
-       findKey(text, onlyKeys, field, entryType, 
+       findKey(text, onlyKeys, field, entry_type, 
                       case_sentitive, reg_exp, reset);
        //FIXME
        //It'd be nice to save and restore the current selection in 
        //availableLV. Currently, we get an automatic reset, since the
        //model is reset.
        
-       updateDialog();
+       updateControls();
 }
 
 
@@ -411,7 +421,7 @@ void GuiCitation::on_entriesCO_currentIndexChanged(int /*index*/)
 void GuiCitation::on_citationStyleCO_currentIndexChanged(int index)
 {
        if (index >= 0 && index < citationStyleCO->count()) {
-               vector<biblio::CiteStyle> const & styles = citeStyles_;
+               vector<CiteStyle> const & styles = citeStyles_;
                updateFormatting(styles[index]);
        }
 }
@@ -419,10 +429,20 @@ void GuiCitation::on_citationStyleCO_currentIndexChanged(int index)
 
 void GuiCitation::on_findLE_textChanged(const QString & text)
 {
-       clearPB->setDisabled(text.isEmpty());
-       if (text.isEmpty())
-               findLE->setFocus();
-       findText(text);
+       bool const searchAsWeGo = (asTypeCB->checkState() == Qt::Checked);
+       searchPB->setDisabled(text.isEmpty() || searchAsWeGo);
+       if (!text.isEmpty()) {
+               if (searchAsWeGo)
+                       findText(findLE->text());
+               return;
+       }
+       findText(findLE->text());
+       findLE->setFocus();
+}
+
+void GuiCitation::on_searchPB_clicked()
+{
+       findText(findLE->text(), true);
 }
 
 
@@ -438,6 +458,15 @@ void GuiCitation::on_regexCB_stateChanged(int)
 }
 
 
+void GuiCitation::on_asTypeCB_stateChanged(int)
+{
+       bool const searchAsWeGo = (asTypeCB->checkState() == Qt::Checked);
+       searchPB->setDisabled(findLE->text().isEmpty() || searchAsWeGo);
+       if (searchAsWeGo)
+               findText(findLE->text(), true);
+}
+
+
 void GuiCitation::changed()
 {
        setButtons();
@@ -450,17 +479,19 @@ void GuiCitation::apply(int const choice, bool full, bool force,
        if (cited_keys_.isEmpty())
                return;
 
-       vector<biblio::CiteStyle> const & styles = citeStyles_;
-       if (styles[choice] == biblio::NOCITE) {
+       vector<CiteStyle> const & styles = citeStyles_;
+       if (styles[choice] == NOCITE) {
                full = false;
                force = false;
                before.clear();
                after.clear();
        }
        
-       string const command =
-               biblio::CitationStyle(styles[choice], full, force)
-               .asLatexStr();
+       CitationStyle s;
+       s.style = styles[choice];
+       s.full = full;
+       s.forceUpperCase = force;
+       string const command = citationStyleToString(s);
 
        params_.setCmdName(command);
        params_["key"] = qstring_to_ucs4(cited_keys_.join(","));
@@ -477,22 +508,10 @@ void GuiCitation::clearSelection()
 }
 
 
-QString GuiCitation::textBefore()
-{
-       return toqstr(params_["before"]);
-}
-
-
-QString GuiCitation::textAfter()
-{
-       return toqstr(params_["after"]);
-}
-
-
 void GuiCitation::init()
 {
        // Make the list of all available bibliography keys
-       all_keys_ = to_qstring_list(availableKeys());
+       all_keys_ = to_qstring_list(bibInfo().getKeys());
        available_model_.setStringList(all_keys_);
 
        // Ditto for the keys cited in this inset
@@ -502,11 +521,15 @@ void GuiCitation::init()
        else
                cited_keys_ = str.split(",");
        selected_model_.setStringList(cited_keys_);
+
+       fillFields();
+       fillEntries();
+       updateControls();
 }
 
 
 void GuiCitation::findKey(QString const & str, bool only_keys,
-       docstring field, docstring entryType,
+       docstring field, docstring entry_type,
        bool case_sensitive, bool reg_exp, bool reset)
 {
        // Used for optimisation: store last searched string.
@@ -524,7 +547,7 @@ void GuiCitation::findKey(QString const & str, bool only_keys,
        last_case_sensitive = case_sensitive;
        last_reg_exp = reg_exp;
 
-       Qt::CaseSensitivity qtcase = case_sensitive?
+       Qt::CaseSensitivity qtcase = case_sensitive ?
                        Qt::CaseSensitive: Qt::CaseInsensitive;
        QStringList keys;
        // If new string (str) contains the last searched one...
@@ -542,10 +565,10 @@ void GuiCitation::findKey(QString const & str, bool only_keys,
 
        QStringList result;
        
-       // First, filter by entryType, which will be faster than 
+       // First, filter by entry_type, which will be faster than 
        // what follows, so we may get to do that on less.
        vector<docstring> keyVector = to_docstring_vector(keys);
-       filterByEntryType(keyVector, entryType);
+       filterByEntryType(keyVector, entry_type);
        
        if (str.isEmpty())
                result = to_qstring_list(keyVector);
@@ -557,28 +580,10 @@ void GuiCitation::findKey(QString const & str, bool only_keys,
 }
 
 
-QStringList GuiCitation::getFieldsAsQStringList()
-{
-       return to_qstring_list(availableFields());
-}
-
-
-QStringList GuiCitation::getEntriesAsQStringList()
-{
-       return to_qstring_list(availableEntries());
-}
-
-
 QStringList GuiCitation::citationStyles(int sel)
 {
        docstring const key = qstring_to_ucs4(cited_keys_[sel]);
-       return to_qstring_list(bibkeysInfo_.getCiteStrings(key, buffer()));
-}
-
-
-QString GuiCitation::getKeyInfo(QString const & sel)
-{
-       return toqstr(getInfo(qstring_to_ucs4(sel)));
+       return to_qstring_list(bibInfo().getCiteStrings(key, buffer()));
 }
 
 
@@ -590,14 +595,10 @@ void GuiCitation::setCitedKeys()
 
 bool GuiCitation::initialiseParams(string const & data)
 {
-       InsetCommandMailer::string2params(lfun_name_, data, params_);
-
-       biblio::CiteEngine const engine = buffer().params().getEngine();
-
-       bibkeysInfo_.fillWithBibKeys(&buffer());
-       
-       citeStyles_ = biblio::getCiteStyles(engine);
-
+       InsetCommand::string2params("citation", data, params_);
+       CiteEngine const engine = buffer().params().citeEngine();
+       citeStyles_ = citeStyles(engine);
+       init();
        return true;
 }
 
@@ -605,32 +606,13 @@ bool GuiCitation::initialiseParams(string const & data)
 void GuiCitation::clearParams()
 {
        params_.clear();
-       bibkeysInfo_.clear();
-}
-
-
-vector<docstring> const GuiCitation::availableKeys() const
-{
-       return bibkeysInfo_.getKeys();
-}
-
-
-vector<docstring> const GuiCitation::availableFields() const
-{
-       return bibkeysInfo_.getFields();
-}
-
-
-vector<docstring> const GuiCitation::availableEntries() const
-{
-       return bibkeysInfo_.getEntries();
 }
 
 
 void GuiCitation::filterByEntryType(
-       vector<docstring> & keyVector, docstring entryType) 
+       vector<docstring> & keyVector, docstring entry_type) 
 {
-       if (entryType.empty())
+       if (entry_type.empty())
                return;
        
        vector<docstring>::iterator it = keyVector.begin();
@@ -639,28 +621,19 @@ void GuiCitation::filterByEntryType(
        vector<docstring> result;
        for (; it != end; ++it) {
                docstring const key = *it;
-               BiblioInfo::const_iterator cit = bibkeysInfo_.find(key);
-               if (cit == bibkeysInfo_.end())
+               BiblioInfo::const_iterator cit = bibInfo().find(key);
+               if (cit == bibInfo().end())
                        continue;
-               if (cit->second.entryType == entryType)
+               if (cit->second.entryType() == entry_type)
                        result.push_back(key);
        }
        keyVector = result;
 }
 
 
-biblio::CiteEngine GuiCitation::getEngine() const
+CiteEngine GuiCitation::citeEngine() const
 {
-       return buffer().params().getEngine();
-}
-
-
-docstring GuiCitation::getInfo(docstring const & key) const
-{
-       if (bibkeysInfo_.empty())
-               return docstring();
-
-       return bibkeysInfo_.getInfo(key);
+       return buffer().params().citeEngine();
 }
 
 
@@ -717,8 +690,8 @@ vector<docstring> GuiCitation::searchKeys(
        vector<docstring>::const_iterator it = keys_to_search.begin();
        vector<docstring>::const_iterator end = keys_to_search.end();
        for (; it != end; ++it ) {
-               BiblioInfo::const_iterator info = bibkeysInfo_.find(*it);
-               if (info == bibkeysInfo_.end())
+               BiblioInfo::const_iterator info = bibInfo().find(*it);
+               if (info == bibInfo().end())
                        continue;
                
                BibTeXInfo const & kvm = info->second;
@@ -726,7 +699,7 @@ vector<docstring> GuiCitation::searchKeys(
                if (only_keys)
                        data = to_utf8(*it);
                else if (field.empty())
-                       data = to_utf8(*it) + ' ' + to_utf8(kvm.allData);
+                       data = to_utf8(*it) + ' ' + to_utf8(kvm.allData());
                else if (kvm.hasField(field))
                        data = to_utf8(kvm.getValueForField(field));
                
@@ -746,6 +719,45 @@ vector<docstring> GuiCitation::searchKeys(
 }
 
 
+void GuiCitation::dispatchParams()
+{
+       std::string const lfun = InsetCommand::params2string("citation", params_);
+       dispatch(FuncRequest(getLfun(), lfun));
+}
+
+
+BiblioInfo const & GuiCitation::bibInfo() const
+{
+       return buffer().masterBibInfo();
+}
+
+
+void GuiCitation::saveSession() const
+{
+       Dialog::saveSession();
+       QSettings settings;
+       settings.setValue(
+               sessionKey() + "/regex", regexCB->isChecked());
+       settings.setValue(
+               sessionKey() + "/casesensitive", caseCB->isChecked());
+       settings.setValue(
+               sessionKey() + "/autofind", asTypeCB->isChecked());
+}
+
+
+void GuiCitation::restoreSession()
+{
+       Dialog::restoreSession();
+       QSettings settings;
+       regexCB->setChecked(
+               settings.value(sessionKey() + "/regex").toBool());
+       caseCB->setChecked(
+               settings.value(sessionKey() + "/casesensitive").toBool());
+       asTypeCB->setChecked(
+               settings.value(sessionKey() + "/autofind").toBool());
+}
+
+
 Dialog * createGuiCitation(GuiView & lv) { return new GuiCitation(lv); }