]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt2/QCitationDialog.C
QBibtex/QCitation ui fixes (bug 1146)
[lyx.git] / src / frontends / qt2 / QCitationDialog.C
index 6f9f32c1dd63574229823e268f07a36238e95ce1..4a188b647c4dbc082203f3b701d5c20635c1dc98 100644 (file)
@@ -1,19 +1,21 @@
 /**
  * \file QCitationDialog.C
- * Copyright 2001 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * \author Kalle Dalheimer <kalle@klaralvdalens-datakonsult.se>
+ * \author Kalle Dalheimer
+ *
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
+
+
 #include <algorithm>
 
-#include "gettext.h"
+#include "qt_helpers.h"
 #include "controllers/ControlCitation.h"
-#include "LyXView.h"
-#include "buffer.h"
+#include "debug.h"
 
 #include <qcheckbox.h>
 #include <qcombobox.h>
 #include <qlistbox.h>
 #include <qmultilineedit.h>
 #include <qpushbutton.h>
+#include <qlabel.h>
 
+#include "ui/QCitationFindDialogBase.h"
 #include "QCitationDialog.h"
+#include "QCitation.h"
+#include "support/lstrings.h"
+
 using std::vector;
 using std::find;
 using std::max;
+using std::endl;
+
 
 QCitationDialog::QCitationDialog(QCitation * form)
        : QCitationDialogBase(0, 0, false, 0),
@@ -40,14 +48,15 @@ QCitationDialog::QCitationDialog(QCitation * form)
                form, SLOT(slotApply()));
        connect(closePB, SIGNAL(clicked()),
                form, SLOT(slotClose()));
-       connect(searchED, SIGNAL(returnPressed()),
-               this, SLOT(slotNextClicked()));
-
-       textBeforeED->setText(_("Not yet supported"));
-       textBeforeED->setReadOnly(true);
-       textBeforeED->setFocusPolicy(QWidget::NoFocus);
-       citationStyleCO->setEnabled(false);
-       citationStyleCO->setFocusPolicy(QWidget::NoFocus);
+
+       add_ = new QCitationFindDialogBase(this, "", true);
+       connect(add_->previousPB, SIGNAL(clicked()), this, SLOT(previous()));
+       connect(add_->nextPB, SIGNAL(clicked()), this, SLOT(next()));
+       connect(add_->availableLB, SIGNAL(currentChanged(QListBoxItem *)), this, SLOT(availableChanged()));
+       connect(add_->availableLB, SIGNAL(selected(QListBoxItem *)), this, SLOT(addCitation()));
+       connect(add_->availableLB, SIGNAL(selected(QListBoxItem *)), add_, SLOT(accept()));
+       connect(add_->addPB, SIGNAL(clicked()), this, SLOT(addCitation()));
+       connect(selectedLB, SIGNAL(returnPressed(QListBoxItem *)), form, SLOT(slotOK()));
 }
 
 
@@ -56,203 +65,171 @@ QCitationDialog::~QCitationDialog()
 }
 
 
-void QCitationDialog::slotBibSelected(int sel)
+void QCitationDialog::setButtons()
 {
-       slotBibHighlighted(sel);
-
        if (form_->readOnly())
                return;
 
-       slotAddClicked();
+       int const sel_nr = selectedLB->currentItem();
+       int const avail_nr = add_->availableLB->currentItem();
+
+       add_->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::slotBibHighlighted(int sel)
+void QCitationDialog::selectedChanged()
 {
+       form_->fillStyles();
        biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
+       infoML->clear();
 
-       citeLB->clearSelection();
-
-       // FIXME: why would this happen ?
-       if (sel < 0 || sel >= (int)form_->bibkeys.size()) {
+       int const sel = selectedLB->currentItem();
+       if (sel < 0) {
+               setButtons();
                return;
        }
 
-       // Put into browser_info the additional info associated with
-       // the selected browser_bib key
-       infoML->clear();
+       infoML->setText(toqstr(biblio::getInfo(theMap, form_->citekeys[sel])));
+       setButtons();
+}
 
-       infoML->setText(biblio::getInfo(theMap, form_->bibkeys[sel]).c_str());
 
-       // Highlight the selected browser_bib key in browser_cite if
-       // present
-       vector<string>::const_iterator cit =
-               std::find(form_->citekeys.begin(), form_->citekeys.end(),
-                         form_->bibkeys[sel]);
+void QCitationDialog::previous()
+{
+       find(biblio::BACKWARD);
+}
 
-       if (cit != form_->citekeys.end()) {
-               int const n = int(cit - form_->citekeys.begin());
-               citeLB->setSelected(n, true);
-               citeLB->setTopItem(n);
-       }
 
-       if (!form_->readOnly()) {
-               if (cit != form_->citekeys.end()) {
-                       form_->setBibButtons(QCitation::OFF);
-                       form_->setCiteButtons(QCitation::ON);
-               } else {
-                       form_->setBibButtons(QCitation::ON);
-                       form_->setCiteButtons(QCitation::OFF);
-               }
-       }
+void QCitationDialog::next()
+{
+       find(biblio::FORWARD);
 }
 
 
-void QCitationDialog::slotCiteHighlighted(int sel)
+void QCitationDialog::availableChanged()
 {
        biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
+       add_->infoML->clear();
 
-       // FIXME: why would this happen ?
-       if (sel < 0 || sel >= (int)form_->citekeys.size()) {
+       int const sel = add_->availableLB->currentItem();
+       if (sel < 0) {
+               setButtons();
                return;
        }
 
-       if (!form_->readOnly()) {
-               form_->setBibButtons(QCitation::OFF);
-               form_->setCiteButtons(QCitation::ON);
-       }
-
-       // Highlight the selected browser_cite key in browser_bib
-       vector<string>::const_iterator cit =
-               std::find(form_->bibkeys.begin(),
-               form_->bibkeys.end(), form_->citekeys[sel]);
-
-       if (cit != form_->bibkeys.end()) {
-               int const n = int(cit - form_->bibkeys.begin());
-               bibLB->setSelected(n, true);
-               bibLB->setTopItem(n);
-
-               // Put into browser_info the additional info associated
-               // with the selected browser_cite key
-               infoML->clear();
-               infoML->setText(biblio::getInfo(theMap, form_->bibkeys[sel]).c_str());
-       }
+       add_->infoML->setText(toqstr(biblio::getInfo(theMap, form_->bibkeys[sel])));
+       setButtons();
 }
 
 
-void QCitationDialog::slotAddClicked()
+void QCitationDialog::addCitation()
 {
-       int const sel = bibLB->currentItem();
+       int const sel = add_->availableLB->currentItem();
 
-       // FIXME: why ?
-       if (sel < 0 || sel >= (int)form_->bibkeys.size()) {
+       if (sel < 0)
                return;
-       }
 
-       // Add the selected browser_bib key to browser_cite
-       citeLB->insertItem(form_->bibkeys[sel].c_str());
-       form_->citekeys.push_back(form_->bibkeys[sel]);
+       // Add the selected browser_bib keys to browser_cite
+       // multiple selections are possible
+       for (unsigned int i = 0; i != add_->availableLB->count(); i++) {
+               if (add_->availableLB->isSelected(i)) {
+                       // do not allow duplicates
+                       if ((selectedLB->findItem(add_->availableLB->text(i))) == 0) {
+                               selectedLB->insertItem(toqstr(form_->bibkeys[i]));
+                               form_->citekeys.push_back(form_->bibkeys[i]);
+                       }
+               }
+       }
 
        int const n = int(form_->citekeys.size());
-       citeLB->setSelected(n - 1, true);
+       selectedLB->setSelected(n - 1, true);
 
-       slotBibHighlighted(sel);
-       form_->setBibButtons(QCitation::OFF);
-       form_->setCiteButtons(QCitation::ON);
        form_->changed();
+       form_->fillStyles();
+       setButtons();
 }
 
 
-void QCitationDialog::slotDelClicked()
+void QCitationDialog::del()
 {
-       int const sel = citeLB->currentItem();
-
-       // FIXME: why ?
-       if (sel < 0 || sel >= (int)form_->citekeys.size()) {
-               return;
-       }
+       int const sel = selectedLB->currentItem();
 
        // Remove the selected key from browser_cite
-       citeLB->removeItem(sel);
+       selectedLB->removeItem(sel);
        form_->citekeys.erase(form_->citekeys.begin() + sel);
 
-       form_->setBibButtons(QCitation::ON);
-       form_->setCiteButtons(QCitation::OFF);
        form_->changed();
+       form_->fillStyles();
+       setButtons();
 }
 
 
-void QCitationDialog::slotUpClicked()
+void QCitationDialog::up()
 {
-       int const sel = citeLB->currentItem();
-
-       // FIXME: why ?
-       if (sel < 1 || sel >= (int)form_->citekeys.size()) {
-               return;
-       }
+       int const sel = selectedLB->currentItem();
 
        // Move the selected key up one line
        vector<string>::iterator it = form_->citekeys.begin() + sel;
        string const tmp = *it;
 
-       citeLB->removeItem(sel);
+       selectedLB->removeItem(sel);
        form_->citekeys.erase(it);
 
-       citeLB->insertItem(tmp.c_str(), sel - 1);
-       citeLB->setSelected(sel - 1, true);
+       selectedLB->insertItem(toqstr(tmp), sel - 1);
+       selectedLB->setSelected(sel - 1, true);
        form_->citekeys.insert(it - 1, tmp);
-       form_->setCiteButtons(QCitation::ON);
+
        form_->changed();
+       form_->fillStyles();
+       setButtons();
 }
 
 
-void QCitationDialog::slotDownClicked()
+void QCitationDialog::down()
 {
-       int const sel = citeLB->currentItem();
-
-       // FIXME: ?
-       if (sel < 0 || sel >= (int)form_->citekeys.size() - 1) {
-               return;
-       }
+       int const sel = selectedLB->currentItem();
 
        // Move the selected key down one line
        vector<string>::iterator it = form_->citekeys.begin() + sel;
        string const tmp = *it;
 
-       citeLB->removeItem(sel);
+       selectedLB->removeItem(sel);
        form_->citekeys.erase(it);
 
-       citeLB->insertItem(tmp.c_str(), sel + 1);
-       citeLB->setSelected(sel + 1, true);
+       selectedLB->insertItem(toqstr(tmp), sel + 1);
+       selectedLB->setSelected(sel + 1, true);
        form_->citekeys.insert(it + 1, tmp);
-       form_->setCiteButtons(QCitation::ON);
+
        form_->changed();
+       form_->fillStyles();
+       setButtons();
 }
 
 
-void QCitationDialog::slotPreviousClicked()
+void QCitationDialog::add()
 {
-       doFind(biblio::BACKWARD);
+       add_->exec();
 }
 
 
-void QCitationDialog::slotNextClicked()
+void QCitationDialog::changed_adaptor()
 {
-       doFind(biblio::FORWARD);
+       form_->changed();
 }
 
 
-void QCitationDialog::doFind(biblio::Direction dir)
+void QCitationDialog::find(biblio::Direction dir)
 {
        biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
-       string const str = searchED->text().latin1();
 
-       biblio::Search const type =
-               searchTypeCB->isChecked() ?
-               biblio::REGEX : biblio::SIMPLE;
+       biblio::Search const type = add_->searchTypeCB->isChecked()
+               ? biblio::REGEX : biblio::SIMPLE;
 
        vector<string>::const_iterator start = form_->bibkeys.begin();
-       int const sel = bibLB->currentItem();
+       int const sel = add_->availableLB->currentItem();
        if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
                start += sel;
 
@@ -262,11 +239,12 @@ void QCitationDialog::doFind(biblio::Direction dir)
        else
                start -= 1;
 
-       bool const caseSensitive = searchCaseCB->isChecked();
+       bool const casesens = add_->searchCaseCB->isChecked();
+       string const str = fromqstr(add_->searchED->text());
 
        vector<string>::const_iterator cit =
                biblio::searchKeys(theMap, form_->bibkeys, str,
-                                  start, type, dir, caseSensitive);
+                                  start, type, dir, casesens);
 
        // not found. let's loop round
        if (cit == form_->bibkeys.end()) {
@@ -276,20 +254,18 @@ void QCitationDialog::doFind(biblio::Direction dir)
                else start = form_->bibkeys.end() - 1;
 
                cit = biblio::searchKeys(theMap, form_->bibkeys, str,
-                                        start, type, dir, caseSensitive);
+                                        start, type, dir, casesens);
 
                if (cit == form_->bibkeys.end())
                        return;
        }
-       
+
        int const found = int(cit - form_->bibkeys.begin());
        if (found == sel) {
                return;
        }
 
        // Update the display
-       int const top = max(found - 5, 1);
-       bibLB->setTopItem(top);
-       bibLB->setSelected(found, true);
-       slotBibHighlighted(0);
+       add_->availableLB->setSelected(found, true);
+       add_->availableLB->ensureCurrentVisible();
 }