]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiCitation.cpp
header cleanup
[lyx.git] / src / frontends / qt4 / GuiCitation.cpp
index 4610839a5d07007c81a3b8cecff22ae386b30e66..1135d54aa4f5e14f102836cb082cff725c76daf4 100644 (file)
@@ -15,8 +15,8 @@
 
 #include "GuiCitation.h"
 
-#include "debug.h"
-#include "gettext.h"
+#include "support/debug.h"
+#include "support/gettext.h"
 #include "qt_helpers.h"
 #include "Buffer.h"
 #include "BufferParams.h"
@@ -30,6 +30,7 @@
 #include <string>
 
 #include <QCloseEvent>
+#include <QShowEvent>
 
 #undef KeyPress
 
@@ -39,9 +40,8 @@
 #include <string>
 #include <vector>
 
-using std::string;
-using std::vector;
-using std::pair;
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace frontend {
@@ -76,13 +76,12 @@ static vector<lyx::docstring> to_docstring_vector(QStringList const & qlist)
 
 
 GuiCitation::GuiCitation(GuiView & lv)
-       : GuiCommand(lv, "citation")
+       : GuiCommand(lv, "citation", qt_("Citation"))
 {
        setupUi(this);
-       setViewTitle(_("Citation"));
 
        connect(citationStyleCO, SIGNAL(activated(int)),
-               this, SLOT(changed()));
+               this, SLOT(on_citationStyleCO_currentIndexChanged(int)));
        connect(fulllistCB, SIGNAL(clicked()),
                this, SLOT(changed()));
        connect(forceuppercaseCB, SIGNAL(clicked()),
@@ -120,13 +119,13 @@ void GuiCitation::closeEvent(QCloseEvent * e)
 {
        clearSelection();
        clearParams();
-       GuiDialog::closeEvent(e);
+       e->accept();
 }
 
 
 void GuiCitation::applyView()
 {
-       int  const choice = std::max(0, citationStyleCO->currentIndex());
+       int  const choice = max(0, citationStyleCO->currentIndex());
        style_ = choice;
        bool const full  = fulllistCB->isChecked();
        bool const force = forceuppercaseCB->isChecked();
@@ -138,27 +137,12 @@ void GuiCitation::applyView()
 }
 
 
-void GuiCitation::hideView()
-{
-       clearParams();
-       accept();
-}
-
-
-void GuiCitation::showView()
+void GuiCitation::showEvent(QShowEvent * e)
 {
        init();
        findLE->clear();
        availableLV->setFocus();
-       QDialog::show();
-       raise();
-       activateWindow();
-}
-
-
-bool GuiCitation::isVisibleView() const
-{
-       return QDialog::isVisible();
+       GuiDialog::showEvent(e);
 }
 
 
@@ -166,14 +150,14 @@ void GuiCitation::on_okPB_clicked()
 {
        applyView();
        clearSelection();
-       hideView();
+       hide();
 }
 
 
 void GuiCitation::on_cancelPB_clicked()
 {
        clearSelection();
-       hideView();
+       hide();
 }
 
 
@@ -203,7 +187,7 @@ void GuiCitation::updateView()
 // called in update() do not need to be called for INTERNAL updates,
 // such as when addPB is pressed, as the list of fields, entries, etc,
 // will not have changed. At the moment, however, the division between
-// fillStyles() and updateStyles() doesn't lend itself to dividing the
+// fillStyles() and updateStyle() doesn't lend itself to dividing the
 // two methods, though they should be divisible.
 void GuiCitation::updateDialog()
 {
@@ -227,7 +211,7 @@ void GuiCitation::updateDialog()
 }
 
 
-void GuiCitation::updateStyle()
+void GuiCitation::updateFormatting(biblio::CiteStyle currentStyle)
 {
        biblio::CiteEngine const engine = getEngine();
        bool const natbib_engine =
@@ -237,15 +221,22 @@ void GuiCitation::updateStyle()
 
        bool const haveSelection = 
                selectedLV->model()->rowCount() > 0;
-       fulllistCB->setEnabled(natbib_engine && haveSelection);
-       forceuppercaseCB->setEnabled(natbib_engine && haveSelection);
-       textBeforeED->setEnabled(!basic_engine && haveSelection);
-       textBeforeLA->setEnabled(!basic_engine && haveSelection);
-       textAfterED->setEnabled(haveSelection);
-       textAfterLA->setEnabled(haveSelection);
-       citationStyleCO->setEnabled(!basic_engine && haveSelection);
-       citationStyleLA->setEnabled(!basic_engine && haveSelection);
 
+       bool const isNocite = currentStyle == biblio::NOCITE;
+
+       fulllistCB->setEnabled(natbib_engine && haveSelection && !isNocite);
+       forceuppercaseCB->setEnabled(natbib_engine && haveSelection && !isNocite);
+       textBeforeED->setEnabled(!basic_engine && haveSelection && !isNocite);
+       textBeforeLA->setEnabled(!basic_engine && haveSelection && !isNocite);
+       textAfterED->setEnabled(haveSelection && !isNocite);
+       textAfterLA->setEnabled(haveSelection && !isNocite);
+       citationStyleCO->setEnabled(haveSelection);
+       citationStyleLA->setEnabled(haveSelection);
+}
+
+
+void GuiCitation::updateStyle()
+{
        string const & command = params_.getCmdName();
 
        // Find the style of the citekeys
@@ -270,9 +261,9 @@ void GuiCitation::updateStyle()
                fulllistCB->setChecked(false);
                forceuppercaseCB->setChecked(false);
        }
+       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.
@@ -298,12 +289,10 @@ void GuiCitation::fillStyles()
 
        QStringList sty = citationStyles(curr);
 
-       bool const basic_engine = (getEngine() == biblio::ENGINE_BASIC);
+       citationStyleCO->setEnabled(!sty.isEmpty());
+       citationStyleLA->setEnabled(!sty.isEmpty());
 
-       citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
-       citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
-
-       if (sty.isEmpty() || basic_engine)
+       if (sty.isEmpty())
                return;
 
        citationStyleCO->insertItems(0, sty);
@@ -387,15 +376,15 @@ void GuiCitation::findText(QString const & text, bool reset)
        //"All Entry Types" is first.
        index = entriesCO->currentIndex() - 1; 
        vector<docstring> const & entries = availableEntries();
-       docstring entryType;
+       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 
@@ -418,6 +407,15 @@ 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_;
+               updateFormatting(styles[index]);
+       }
+}
+
+
 void GuiCitation::on_findLE_textChanged(const QString & text)
 {
        clearPB->setDisabled(text.isEmpty());
@@ -441,19 +439,24 @@ void GuiCitation::on_regexCB_stateChanged(int)
 
 void GuiCitation::changed()
 {
-       fillStyles();
        setButtons();
 }
 
 
-void GuiCitation::apply(int const choice, bool const full, bool const force,
+void GuiCitation::apply(int const choice, bool full, bool force,
        QString before, QString after)
 {
        if (cited_keys_.isEmpty())
                return;
 
        vector<biblio::CiteStyle> const & styles = citeStyles_;
-
+       if (styles[choice] == biblio::NOCITE) {
+               full = false;
+               force = false;
+               before.clear();
+               after.clear();
+       }
+       
        string const command =
                biblio::CitationStyle(styles[choice], full, force)
                .asLatexStr();
@@ -502,7 +505,7 @@ void GuiCitation::init()
 
 
 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.
@@ -538,10 +541,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);
@@ -590,17 +593,9 @@ bool GuiCitation::initialiseParams(string const & data)
 
        biblio::CiteEngine const engine = buffer().params().getEngine();
 
-       bool use_styles = engine != biblio::ENGINE_BASIC;
-
        bibkeysInfo_.fillWithBibKeys(&buffer());
        
-       if (citeStyles_.empty())
-               citeStyles_ = biblio::getCiteStyles(engine);
-       else {
-               if ((use_styles && citeStyles_.size() == 1) ||
-                   (!use_styles && citeStyles_.size() != 1))
-                       citeStyles_ = biblio::getCiteStyles(engine);
-       }
+       citeStyles_ = biblio::getCiteStyles(engine);
 
        return true;
 }
@@ -632,9 +627,9 @@ vector<docstring> const GuiCitation::availableEntries() const
 
 
 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();
@@ -646,7 +641,7 @@ void GuiCitation::filterByEntryType(
                BiblioInfo::const_iterator cit = bibkeysInfo_.find(key);
                if (cit == bibkeysInfo_.end())
                        continue;
-               if (cit->second.entryType == entryType)
+               if (cit->second.entryType() == entry_type)
                        result.push_back(key);
        }
        keyVector = result;
@@ -698,7 +693,7 @@ vector<docstring> GuiCitation::searchKeys(
 {
        vector<docstring> foundKeys;
 
-       docstring expr = support::trim(search_expression);
+       docstring expr = trim(search_expression);
        if (expr.empty())
                return foundKeys;
 
@@ -707,8 +702,16 @@ vector<docstring> GuiCitation::searchKeys(
                // it is treated as a simple string by boost::regex.
                expr = escape_special_chars(expr);
 
-       boost::regex reg_exp(to_utf8(expr), case_sensitive ?
-               boost::regex_constants::normal : boost::regex_constants::icase);
+       boost::regex reg_exp;
+       try {
+               reg_exp.assign(to_utf8(expr), case_sensitive ?
+                       boost::regex_constants::normal : boost::regex_constants::icase);
+       } catch (boost::regex_error & e) {
+               // boost::regex throws an exception if the regular expression is not
+               // valid.
+               LYXERR(Debug::GUI, e.what());
+               return vector<docstring>();
+       }
 
        vector<docstring>::const_iterator it = keys_to_search.begin();
        vector<docstring>::const_iterator end = keys_to_search.end();
@@ -722,7 +725,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));
                
@@ -733,7 +736,8 @@ vector<docstring> GuiCitation::searchKeys(
                        if (boost::regex_search(data, reg_exp))
                                foundKeys.push_back(*it);
                }
-               catch (boost::regex_error &) {
+               catch (boost::regex_error & e) {
+                       LYXERR(Debug::GUI, e.what());
                        return vector<docstring>();
                }
        }