]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/QCitationDialog.C
rename LFUN enum values according to their command (as used in th minibuffer/bind...
[lyx.git] / src / frontends / qt4 / QCitationDialog.C
index b4968b60ce118a04efc67a3bc507cccf0f13bf41..7bb6cbba742d7038e9426fa3f253394a70896273 100644 (file)
 #include "QCitationDialog.h"
 #include "ui/QCitationFindUi.h"
 #include "QCitation.h"
+#include "Qt2BC.h"
 #include "qt_helpers.h"
 
+#include "bufferparams.h"
+
 #include "controllers/ControlCitation.h"
 #include "controllers/ButtonController.h"
 
-#include <qcheckbox.h>
-#include <qlineedit.h>
-#include <q3listbox.h>
-#include <q3multilineedit.h>
-#include <qpushbutton.h>
+#include "support/lstrings.h"
 
+#include <iostream>
+using std::cout;
+using std::endl;
 
-using std::vector;
+using std::find;
 using std::string;
+using std::vector;
+
 
 namespace lyx {
+
+using support::getStringFromVector;
+using support::getVectorFromString;
+using support::trim;
+
 namespace frontend {
 
-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()));
-
-    connect( citationStyleCO, SIGNAL( activated(int) ), this, SLOT( changed_adaptor() ) );
-    connect( fulllistCB, SIGNAL( clicked() ), this, SLOT( changed_adaptor() ) );
-    connect( forceuppercaseCB, SIGNAL( clicked() ), this, SLOT( changed_adaptor() ) );
-    connect( textBeforeED, SIGNAL( textChanged(const QString&) ), this, SLOT( changed_adaptor() ) );
-    connect( textAfterED, SIGNAL( textChanged(const QString&) ), this, SLOT( changed_adaptor() ) );
-    connect( upPB, SIGNAL( clicked() ), this, SLOT( up() ) );
-    connect( downPB, SIGNAL( clicked() ), this, SLOT( down() ) );
-    connect( selectedLB, SIGNAL( currentChanged(QListBoxItem*) ), this, SLOT( selectedChanged() ) );
-    connect( addPB, SIGNAL( clicked() ), this, SLOT( add() ) );
-    connect( deletePB, SIGNAL( clicked() ), this, SLOT( del() ) );
-
-       add_ = new QDialog(this, "", true);
-       ui_.setupUi(add_);
-
-    connect( ui_.addPB, SIGNAL( clicked() ), this, SLOT( accept() ) );
-    connect( ui_.closePB, SIGNAL( clicked() ), this, SLOT( reject() ) );
-
-       connect(ui_.previousPB, SIGNAL(clicked()), this, SLOT(previous()));
-       connect(ui_.nextPB, SIGNAL(clicked()), this, SLOT(next()));
-       connect(ui_.availableLB, SIGNAL(currentChanged(Q3ListBoxItem *)), this, SLOT(availableChanged()));
-       connect(ui_.availableLB, SIGNAL(selected(Q3ListBoxItem *)), this, SLOT(addCitation()));
-       connect(ui_.availableLB, SIGNAL(selected(Q3ListBoxItem *)), add_, SLOT(accept()));
-       connect(ui_.addPB, SIGNAL(clicked()), this, SLOT(addCitation()));
-       connect(selectedLB, SIGNAL(returnPressed(Q3ListBoxItem *)), form, SLOT(slotOK()));
-}
 
+       setCaption(toqstr("LyX: " + getTitle()));
+
+/*
+
+       // 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());
+
+//     foundLV.setModel(form_->found());
+
+    connect( citationStyleCO, SIGNAL( activated(int) ), this, SLOT( changed() ) );
+    connect( fulllistCB, SIGNAL( clicked() ), this, SLOT( changed() ) );
+    connect( forceuppercaseCB, SIGNAL( clicked() ), this, SLOT( changed() ) );
+    connect( textBeforeED, SIGNAL( textChanged(const QString&) ), this, SLOT( changed() ) );
+    connect( textAfterED, SIGNAL( textChanged(const QString&) ), this, SLOT( changed() ) );
+
+
+//     find_ = new QCitationFind(form_, this);
+       
+//     connect(selectedLV, SIGNAL(doubleClicked(const QModelIndex & index)),
+//             form_, SLOT(on_okPB_clicked()));//SLOT(slotOK()));
+}
 
 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();
 
-void QCitationDialog::setButtons()
+       form_->apply(choice, full, force, before, after);
+}
+
+
+void QCitationDialog::hide()
 {
-       if (form_->readOnly())
-               return;
+       accept();
+}
 
-       int const sel_nr = selectedLB->currentItem();
-       int const avail_nr = ui_.availableLB->currentItem();
 
-       ui_.addPB->setEnabled(avail_nr >= 0);
-       deletePB->setEnabled(sel_nr >= 0);
-       upPB->setEnabled(sel_nr > 0);
-       downPB->setEnabled(sel_nr >= 0 && sel_nr < int(selectedLB->count() - 1));
+void QCitationDialog::show()
+{
+       QDialog::show();
 }
 
 
-void QCitationDialog::openFind()
+bool QCitationDialog::isVisible() const
 {
-       if (form_->readOnly())
-               return;
+       return QDialog::isVisible();
+}
 
-       if (isVisible() && selectedLB->count() == 0 
-           && ui_.availableLB->count() != 0){
-               // open the find dialog
-               add();
-               // and let the user press ok after a selection
-               if (selectedLB->count() != 0)
-                       form_->bc().valid();
-       }
+void QCitationDialog::on_okPB_clicked()
+{
+       apply();
+       accept();
 }
 
+void QCitationDialog::on_cancelPB_clicked()
+{
+       accept();
+//     reject();
+}
 
-void QCitationDialog::selectedChanged()
+void QCitationDialog::on_applyPB_clicked()
 {
-       form_->fillStyles();
-       biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
-       infoML->clear();
+       apply();
+}
 
-       int const sel = selectedLB->currentItem();
-       if (sel < 0) {
-               setButtons();
-               return;
-       }
+void QCitationDialog::on_restorePB_clicked()
+{
+       update();
+       bc().valid(form_->isValid() );
+}
+
+void QCitationDialog::update()
+{
+       form_->updateModel();
 
-       if (!theMap.empty())
-               infoML->setText(
-                       toqstr(biblio::getInfo(theMap, form_->citekeys[sel])));
+       // No keys have been selected yet, so...
+       infoML->document()->clear();
        setButtons();
-}
 
+       textBeforeED->setText(form_->textBefore());
+       textAfterED->setText(form_->textAfter());
+
+       fillStyles();
+       updateStyle();
+}
 
-void QCitationDialog::previous()
+void QCitationDialog::updateStyle()
 {
-       find(biblio::BACKWARD);
+       biblio::CiteEngine const engine = form_->getEngine();
+       bool const natbib_engine =
+               engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
+               engine == biblio::ENGINE_NATBIB_NUMERICAL;
+       bool const basic_engine = engine == biblio::ENGINE_BASIC;
+
+       fulllistCB->setEnabled(natbib_engine);
+       forceuppercaseCB->setEnabled(natbib_engine);
+       textBeforeED->setEnabled(!basic_engine);
+
+       string const & command = form_->params().getCmdName();
+
+       // Find the style of the citekeys
+       vector<biblio::CiteStyle> const & styles =
+               ControlCitation::getCiteStyles();
+       biblio::CitationStyle const cs(command);
+
+       vector<biblio::CiteStyle>::const_iterator cit =
+               std::find(styles.begin(), styles.end(), cs.style);
+
+       // restore the latest natbib style
+       if (style_ >= 0 && style_ < citationStyleCO->count())
+               citationStyleCO->setCurrentItem(style_);
+       else
+               citationStyleCO->setCurrentItem(0);
+
+       fulllistCB->setChecked(false);
+       forceuppercaseCB->setChecked(false);
+
+       if (cit != styles.end()) {
+               int const i = int(cit - styles.begin());
+               citationStyleCO->setCurrentItem(i);
+               fulllistCB->setChecked(cs.full);
+               forceuppercaseCB->setChecked(cs.forceUCase);
+       }
 }
 
 
-void QCitationDialog::next()
+void QCitationDialog::fillStyles()
 {
-       find(biblio::FORWARD);
+       int const orig = citationStyleCO->currentItem();
+
+       citationStyleCO->clear();
+
+       QStringList selected_keys = form_->selected()->stringList();
+       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();
+
+       QStringList sty = form_->citationStyles(curr);
+
+       bool const basic_engine = 
+               (form_->getEngine() == biblio::ENGINE_BASIC);
+
+       citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
+       citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
+
+       citationStyleCO->insertItems(0, sty);
+
+       if (orig != -1 && orig < citationStyleCO->count())
+               citationStyleCO->setCurrentItem(orig);
 }
 
 
-void QCitationDialog::availableChanged()
+void QCitationDialog::setButtons()
+{
+//     if (form_->readOnly())
+//             return;
+
+       int const row_count = selectedLV->model()->rowCount();
+
+       int sel_nr=-1;
+       if (! selectedLV->selectionModel()->selectedIndexes().empty()) {
+               sel_nr = 
+               selectedLV->selectionModel()->selectedIndexes()[0].row();
+       }
+
+       deletePB->setEnabled(sel_nr >= 0);
+       upPB->setEnabled(sel_nr > 0);
+       downPB->setEnabled(sel_nr >= 0 && sel_nr < row_count - 1);
+}
+
+/*
+void QCitationDialog::on_selectedLV_currentChanged(Q3ListBoxItem*)
 {
-       biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
-       ui_.infoML->clear();
+       fillStyles();
+       infoML->document()->clear();
 
-       int const sel = ui_.availableLB->currentItem();
+       int const sel = selectedLB->currentItem();
        if (sel < 0) {
                setButtons();
                return;
        }
 
-       if (!theMap.empty())
-               ui_.infoML->setText(
-                       toqstr(biblio::getInfo(theMap, form_->bibkeys[sel])));
+       infoML->document()->setPlainText(form_->getKeyInfo(sel));
+
        setButtons();
 }
+*/
 
 
-void QCitationDialog::addCitation()
+void QCitationDialog::on_addPB_clicked()
 {
-       int const sel = ui_.availableLB->currentItem();
+       form_->addKeys(availableLV->selectionModel()->selectedIndexes());
+       changed();
+}
 
-       if (sel < 0)
-               return;
+void QCitationDialog::on_deletePB_clicked()
+{
+       form_->deleteKeys(selectedLV->selectionModel()->selectedIndexes());
+       changed();
+}
 
-       // Add the selected browser_bib keys to browser_cite
-       // multiple selections are possible
-       for (unsigned int i = 0; i != ui_.availableLB->count(); i++) {
-               if (ui_.availableLB->isSelected(i)) {
-                       // do not allow duplicates
-                       if ((selectedLB->findItem(ui_.availableLB->text(i))) == 0) {
-                               selectedLB->insertItem(toqstr(form_->bibkeys[i]));
-                               form_->citekeys.push_back(form_->bibkeys[i]);
-                       }
-               }
-       }
 
-       int const n = int(form_->citekeys.size());
-       selectedLB->setSelected(n - 1, true);
+void QCitationDialog::on_upPB_clicked()
+{
+       form_->upKey(selectedLV->selectionModel()->selectedIndexes());
+       changed();
+}
 
-       form_->changed();
-       form_->fillStyles();
-       setButtons();
+
+void QCitationDialog::on_downPB_clicked()
+{
+       form_->downKey(selectedLV->selectionModel()->selectedIndexes());
+       changed();
 }
 
+void QCitationDialog::on_findLE_textChanged(const QString & text)
+{
+       QModelIndex const index = form_->findKey(text);
+       if (! index.isValid())
+               return;
 
-void QCitationDialog::del()
+//     QItemSelection selection(index, index);
+       availableLV->selectionModel()->select(index, QItemSelectionModel::Select);
+       changed();
+}
+
+void QCitationDialog::on_advancedSearchPB_clicked()
 {
-       int const sel = selectedLB->currentItem();
+//     find_->exec();
+       changed();
+}
 
-       // Remove the selected key from browser_cite
-       selectedLB->removeItem(sel);
-       form_->citekeys.erase(form_->citekeys.begin() + sel);
 
-       form_->changed();
-       form_->fillStyles();
+void QCitationDialog::changed()
+{
+       fillStyles();
        setButtons();
 }
 
 
-void QCitationDialog::up()
+void updateBrowser(Q3ListBox * browser,
+                             vector<string> const & keys)
 {
-       int const sel = selectedLB->currentItem();
+       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)
+{
+       setupUi(this);
+    connect(addPB, SIGNAL(clicked()), this, SLOT(accept()));
+    connect(closePB, SIGNAL(clicked()), this, SLOT(reject()));
+       connect(previousPB, SIGNAL(clicked()), this, SLOT(previous()));
+       connect(nextPB, SIGNAL(clicked()), this, SLOT(next()));
+}
 
-       // Move the selected key up one line
-       vector<string>::iterator it = form_->citekeys.begin() + sel;
-       string const tmp = *it;
+void QCitationFind::update()
+{
+//     updateBrowser(availableLB, form_->availableKeys());
+}
 
-       selectedLB->removeItem(sel);
-       form_->citekeys.erase(it);
+void QCitationFind::on_availableLB_currentChanged(Q3ListBoxItem *)
+{
+       infoML->document()->clear();
 
-       selectedLB->insertItem(toqstr(tmp), sel - 1);
-       selectedLB->setSelected(sel - 1, true);
-       form_->citekeys.insert(it - 1, tmp);
+       int const sel = availableLB->currentItem();
+       if (sel < 0) {
+               addPB->setEnabled(false);
+               return;
+       }
 
-       form_->changed();
-       form_->fillStyles();
-       setButtons();
+       addPB->setEnabled(true);
+//     infoML->document()->setPlainText(form_->getKeyInfo(sel));
 }
 
 
-void QCitationDialog::down()
+void QCitationFind::on_availableLB_selected(Q3ListBoxItem *)
 {
-       int const sel = selectedLB->currentItem();
+       int const sel = availableLB->currentItem();
+       foundkeys.clear();
+//     foundkeys.push_back(form_->availableKeys()[sel]);
+       emit newCitations();
+       accept();
+}
+
+void QCitationFind::on_addPB_clicked()
+{
+//     form_->addKeys(availableLB->selectionModel()->selectedIndexes());
 
-       // Move the selected key down one line
-       vector<string>::iterator it = form_->citekeys.begin() + sel;
-       string const tmp = *it;
+       int const sel = availableLB->currentItem();
 
-       selectedLB->removeItem(sel);
-       form_->citekeys.erase(it);
+       if (sel < 0)
+               return;
 
-       selectedLB->insertItem(toqstr(tmp), sel + 1);
-       selectedLB->setSelected(sel + 1, true);
-       form_->citekeys.insert(it + 1, tmp);
+       QStringList bibkeys = form_->available()->stringList();
 
-       form_->changed();
-       form_->fillStyles();
-       setButtons();
+       // Add the selected browser_bib keys to browser_cite
+       // multiple selections are possible
+       for (unsigned int i = 0; i != availableLB->count(); i++) {
+               if (availableLB->isSelected(i)) {
+                               foundkeys.push_back(fromqstr(bibkeys[i]));
+               }
+       }
+
+       emit newCitations();
+       accept();
 }
 
 
-void QCitationDialog::add()
+void QCitationFind::previous()
 {
-       add_->exec();
+       find(biblio::BACKWARD);
 }
 
 
-void QCitationDialog::changed_adaptor()
+void QCitationFind::next()
 {
-       form_->changed();
+       find(biblio::FORWARD);
 }
 
 
-void QCitationDialog::find(biblio::Direction dir)
+void QCitationFind::find(biblio::Direction dir)
 {
-       biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
+/*     QStringList bibkeys = form_->available()->stringList();
+
+       biblio::InfoMap const & theMap = form_->bibkeysInfo();
 
-       biblio::Search const type = ui_.searchTypeCB->isChecked()
+       biblio::Search const type = searchTypeCB->isChecked()
                ? biblio::REGEX : biblio::SIMPLE;
 
-       vector<string>::const_iterator start = form_->bibkeys.begin();
-       int const sel = ui_.availableLB->currentItem();
-       if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
+       vector<string>::const_iterator start = bibkeys.begin();
+       int const sel = availableLB->currentItem();
+       if (sel >= 0 && sel <= int(bibkeys.size()-1))
                start += sel;
 
        // Find the NEXT instance...
        if (dir == biblio::FORWARD)
                start += 1;
 
-       bool const casesens = ui_.searchCaseCB->isChecked();
-       string const str = fromqstr(ui_.searchED->text());
+       bool const casesens = searchCaseCB->isChecked();
+       string const str = fromqstr(searchED->text());
 
        vector<string>::const_iterator cit =
-               biblio::searchKeys(theMap, form_->bibkeys, str,
+               biblio::searchKeys(theMap, bibkeys, str,
                                   start, type, dir, casesens);
 
        // not found. let's loop round
-       if (cit == form_->bibkeys.end()) {
+       if (cit == bibkeys.end()) {
                if (dir == biblio::FORWARD) {
-                       start = form_->bibkeys.begin();
+                       start = bibkeys.begin();
                }
-               else start = form_->bibkeys.end() - 1;
+               else start = bibkeys.end() - 1;
 
-               cit = biblio::searchKeys(theMap, form_->bibkeys, str,
+               cit = biblio::searchKeys(theMap, bibkeys, str,
                                         start, type, dir, casesens);
 
-               if (cit == form_->bibkeys.end())
+               if (cit == bibkeys.end())
                        return;
        }
 
-       int const found = int(cit - form_->bibkeys.begin());
+       int const found = int(cit - bibkeys.begin());
        if (found == sel) {
                return;
        }
 
        // Update the display
        // note that we have multi selection mode!
-       ui_.availableLB->setSelected(sel, false);
-       ui_.availableLB->setSelected(found, true);
-       ui_.availableLB->setCurrentItem(found);
-       ui_.availableLB->ensureCurrentVisible();
+       availableLB->setSelected(sel, false);
+       availableLB->setSelected(found, true);
+       availableLB->setCurrentItem(found);
+       availableLB->ensureCurrentVisible();
+*/
 }
 
 } // namespace frontend