]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/QCitationDialog.C
enable Font cache only for MacOSX and inline width() for other platform.
[lyx.git] / src / frontends / qt4 / QCitationDialog.C
index e070e271a3a8b2af7af2101832cd5d7e7ffb4009..d53a8f9e9c83c22359050e052303e7c73e0ab2db 100644 (file)
 #include <config.h>
 
 #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 "support/lstrings.h"
 
-#include <iostream>
-using std::cout;
-using std::endl;
+#include <vector>
+#include <string>
 
-using std::find;
-using std::string;
 using std::vector;
-
+using std::string;
 
 namespace lyx {
 
 using support::getStringFromVector;
 using support::getVectorFromString;
-using support::trim;
 
 namespace frontend {
 
-void updateBrowser(Q3ListBox * browser,
-                             vector<string> const & keys)
-{
-       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));
-       }
-}
 
-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()));
-*/
-       // Manage the ok, apply, restore and cancel/close buttons
-       form_->bcview().setOK(okPB);
-       form_->bcview().setApply(applyPB);
-       form_->bcview().setCancel(closePB);
-       form_->bcview().setRestore(restorePB);
-
-       form_->bcview().addReadOnly(addPB);
-       form_->bcview().addReadOnly(deletePB);
-       form_->bcview().addReadOnly(upPB);
-       form_->bcview().addReadOnly(downPB);
-       form_->bcview().addReadOnly(citationStyleCO);
-       form_->bcview().addReadOnly(forceuppercaseCB);
-       form_->bcview().addReadOnly(fulllistCB);
-       form_->bcview().addReadOnly(textBeforeED);
-       form_->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() ) );
+       setWindowTitle(toqstr("LyX: " + getTitle()));
 
+       selectedLV->setModel(form_->selected());
+       availableLV->setModel(form_->available());
 
-//     find_ = new QCitationFind(form_, this);
-       
-//     connect(selectedLV, SIGNAL(doubleClicked(const QModelIndex & index)),
-//             form_, SLOT(on_okPB_clicked()));//SLOT(slotOK()));
+    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()));
 }
 
+
 QCitationDialog::~QCitationDialog()
 {
 }
 
 
-void QCitationDialog::on_okPB_clicked()
+void QCitationDialog::apply()
 {
-       form_->apply();
-       accept();
+       int  const choice = std::max(0, citationStyleCO->currentIndex());
+       style_ = choice;
+       bool const full  = fulllistCB->isChecked();
+       bool const force = forceuppercaseCB->isChecked();
+
+       QString const before = textBeforeED->text();
+       QString const after = textAfterED->text();
+
+       form_->apply(choice, full, force, before, after);
 }
 
-void QCitationDialog::on_cancelPB_clicked()
+
+void QCitationDialog::hide()
 {
-       reject();
+       accept();
 }
 
-void QCitationDialog::on_applyPB_clicked()
+
+void QCitationDialog::show()
 {
-       form_->apply();
+       QDialog::show();
 }
 
-void QCitationDialog::on_restorePB_clicked()
+
+bool QCitationDialog::isVisible() const
 {
-       form_->update_contents();
+       return QDialog::isVisible();
 }
 
-void QCitationDialog::apply(InsetCommandParams & params)
+void QCitationDialog::on_okPB_clicked()
 {
-       vector<biblio::CiteStyle> const & styles =
-               ControlCitation::getCiteStyles();
+       apply();
+       accept();
+}
 
-       int  const choice = std::max(0, citationStyleCO->currentItem());
-       bool const full  = fulllistCB->isChecked();
-       bool const force = forceuppercaseCB->isChecked();
 
-       string const command =
-               biblio::CitationStyle(styles[choice], full, force)
-               .asLatexStr();
+void QCitationDialog::on_cancelPB_clicked()
+{
+       accept();
+}
 
-       params.setCmdName(command);
 
-       string const before = fromqstr(textBeforeED->text());
-       params.setSecOptions(before);
+void QCitationDialog::on_applyPB_clicked()
+{
+       apply();
+}
 
-       string const after = fromqstr(textAfterED->text());
-       params.setOptions(after);
 
-       style_ = choice;
+void QCitationDialog::on_restorePB_clicked()
+{
+       update();
 }
 
 
-void QCitationDialog::update(InsetCommandParams const & params)
+void QCitationDialog::update()
 {
-       // No keys have been selected yet, so...
-       infoML->document()->clear();
+       form_->updateModel();
+
+       QModelIndex idxa = availableLV->currentIndex();
+       if (!idxa.isValid())
+               availableLV->setCurrentIndex(availableLV->model()->index(0,0));
+
+       QModelIndex idx = selectedLV->currentIndex();
+       if (form_->isValid() && !idx.isValid()) {
+               selectedLV->setCurrentIndex(selectedLV->model()->index(0,0));
+               updateInfo(selectedLV->currentIndex());
+       } else
+               updateInfo(availableLV->currentIndex());
+
        setButtons();
 
-       textBeforeED->setText(
-               toqstr(params.getSecOptions()));
-       textAfterED->setText(
-               toqstr(params.getOptions()));
+       textBeforeED->setText(form_->textBefore());
+       textAfterED->setText(form_->textAfter());
 
        fillStyles();
        updateStyle();
-
-//     find_->update();
 }
 
+
 void QCitationDialog::updateStyle()
 {
-       biblio::CiteEngine const engine = form_->controller().getEngine();
+       biblio::CiteEngine const engine = form_->getEngine();
        bool const natbib_engine =
                engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
                engine == biblio::ENGINE_NATBIB_NUMERICAL;
@@ -184,7 +158,7 @@ void QCitationDialog::updateStyle()
        forceuppercaseCB->setEnabled(natbib_engine);
        textBeforeED->setEnabled(!basic_engine);
 
-       string const & command = form_->controller().params().getCmdName();
+       string const & command = form_->params().getCmdName();
 
        // Find the style of the citekeys
        vector<biblio::CiteStyle> const & styles =
@@ -196,15 +170,16 @@ void QCitationDialog::updateStyle()
 
        // restore the latest natbib style
        if (style_ >= 0 && style_ < citationStyleCO->count())
-               citationStyleCO->setCurrentItem(style_);
+               citationStyleCO->setCurrentIndex(style_);
        else
-               citationStyleCO->setCurrentItem(0);
+               citationStyleCO->setCurrentIndex(0);
+
        fulllistCB->setChecked(false);
        forceuppercaseCB->setChecked(false);
 
        if (cit != styles.end()) {
                int const i = int(cit - styles.begin());
-               citationStyleCO->setCurrentItem(i);
+               citationStyleCO->setCurrentIndex(i);
                fulllistCB->setChecked(cs.full);
                forceuppercaseCB->setChecked(cs.forceUCase);
        }
@@ -213,260 +188,153 @@ void QCitationDialog::updateStyle()
 
 void QCitationDialog::fillStyles()
 {
-       if (citekeys.empty()) {
-               citationStyleCO->setEnabled(false);
-               citationStyleLA->setEnabled(false);
-               return;
-       }
-
-       int const orig = citationStyleCO->currentItem();
+       int const orig = citationStyleCO->currentIndex();
 
        citationStyleCO->clear();
 
        QStringList selected_keys = form_->selected()->stringList();
-       if (selected_keys.empty())
+       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();
 
-       string key = fromqstr(selected_keys[curr]);
+       QStringList sty = form_->citationStyles(curr);
 
-       vector<string> const & sty = form_->controller().getCiteStrings(key);
+       bool const basic_engine = 
+               (form_->getEngine() == biblio::ENGINE_BASIC);
 
-       biblio::CiteEngine const engine = form_->controller().getEngine();
-       bool const basic_engine = engine == biblio::ENGINE_BASIC;
+       citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
+       citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
 
-       citationStyleCO->setEnabled(!sty.empty() && !basic_engine);
-       citationStyleLA->setEnabled(!sty.empty() && !basic_engine);
-
-       for (vector<string>::const_iterator it = sty.begin();
-               it != sty.end(); ++it) {
-               citationStyleCO->insertItem(toqstr(*it));
-       }
+       citationStyleCO->insertItems(0, sty);
 
        if (orig != -1 && orig < citationStyleCO->count())
-               citationStyleCO->setCurrentItem(orig);
+               citationStyleCO->setCurrentIndex(orig);
 }
 
 
-void QCitationDialog::setButtons()
+bool QCitationDialog::isSelected(const QModelIndex & idx)
 {
-       if (form_->readOnly())
-               return;
+       QString const str = idx.data().toString();
+       return !form_->selected()->stringList().filter(str).isEmpty();
+}
 
-       int const row_count = selectedLV->model()->rowCount();
 
-       int sel_nr=-1;
-       if (! selectedLV->selectionModel()->selectedIndexes().empty()) {
-               sel_nr = 
-               selectedLV->selectionModel()->selectedIndexes()[0].row();
-       }
+void QCitationDialog::setButtons()
+{
+       int const arows = availableLV->model()->rowCount();
+       addPB->setEnabled(arows>0 && !isSelected(availableLV->currentIndex()));
 
+       int const srows = selectedLV->model()->rowCount();
+       int const sel_nr = selectedLV->currentIndex().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*)
-{
-       fillStyles();
-       infoML->document()->clear();
-
-       int const sel = selectedLB->currentItem();
-       if (sel < 0) {
-               setButtons();
-               return;
-       }
-
-       infoML->document()->setPlainText(form_->getKeyInfo(sel));
-
-       setButtons();
+       downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
+       applyPB->setEnabled(srows>0);
+       okPB->setEnabled(srows>0);
 }
-*/
 
 
-void QCitationDialog::on_addPB_clicked()
-{
-       form_->addKeys(availableLV->selectionModel()->selectedIndexes());
-}
-
-void QCitationDialog::on_deletePB_clicked()
+void QCitationDialog::updateInfo(const QModelIndex & idx)
 {
-       form_->addKeys(selectedLV->selectionModel()->selectedIndexes());
-       changed();
+       if (idx.isValid()) {
+               QString const keytxt = form_->getKeyInfo(idx.data().toString());
+               infoML->document()->setPlainText(keytxt);
+       } else
+               infoML->document()->clear();
 }
 
 
-void QCitationDialog::on_upPB_clicked()
+void QCitationDialog::on_selectedLV_clicked(const QModelIndex & idx)
 {
-       form_->upKey(selectedLV->selectionModel()->selectedIndexes());
+       updateInfo(idx);
        changed();
 }
 
-
-void QCitationDialog::on_downPB_clicked()
+void QCitationDialog::on_availableLV_clicked(const QModelIndex & idx)
 {
-       form_->downKey(selectedLV->selectionModel()->selectedIndexes());
-       changed();
+       updateInfo(idx);
+       setButtons();
 }
 
-void QCitationDialog::on_findLE_textChanged(const QString & text)
+
+void QCitationDialog::on_availableLV_activated(const QModelIndex & idx)
 {
-       QModelIndex const index = form_->findKey(text);
-       if (! index.isValid())
+       if (isSelected(idx))
                return;
 
-//     QItemSelection selection(index, index);
-       availableLV->selectionModel()->select(index, QItemSelectionModel::Select);
-       changed();
+       on_addPB_clicked();             
 }
 
-void QCitationDialog::on_advancedSearchPB_clicked()
+
+void QCitationDialog::on_addPB_clicked()
 {
-//     find_->exec();
+       QModelIndex idx = selectedLV->currentIndex();
+       form_->addKey(availableLV->currentIndex());
+       if (idx.isValid())
+               selectedLV->setCurrentIndex(idx);
        changed();
 }
 
 
-void QCitationDialog::changed()
+void QCitationDialog::on_deletePB_clicked()
 {
-       fillStyles();
-       setButtons();
-}
+       QModelIndex idx = selectedLV->currentIndex();
+       int nrows = selectedLV->model()->rowCount();
+       
+       form_->deleteKey(idx);
 
+       if (idx.row() == nrows - 1)     
+               idx = idx.sibling(idx.row() - 1, idx.column());
 
+       if (nrows>1)
+               selectedLV->setCurrentIndex(idx);
 
-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()));
+       updateInfo(selectedLV->currentIndex());
+       changed();
 }
 
-void QCitationFind::update()
-{
-//     updateBrowser(availableLB, form_->availableKeys());
-}
 
-void QCitationFind::on_availableLB_currentChanged(Q3ListBoxItem *)
+void QCitationDialog::on_upPB_clicked()
 {
-       infoML->document()->clear();
-
-       int const sel = availableLB->currentItem();
-       if (sel < 0) {
-               addPB->setEnabled(false);
-               return;
-       }
-
-       addPB->setEnabled(true);
-//     infoML->document()->setPlainText(form_->getKeyInfo(sel));
+       QModelIndex idx = selectedLV->currentIndex();
+       form_->upKey(idx);
+       selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
+       changed();
 }
 
 
-void QCitationFind::on_availableLB_selected(Q3ListBoxItem *)
-{
-       int const sel = availableLB->currentItem();
-       foundkeys.clear();
-//     foundkeys.push_back(form_->availableKeys()[sel]);
-       emit newCitations();
-       accept();
-}
-
-void QCitationFind::on_addPB_clicked()
+void QCitationDialog::on_downPB_clicked()
 {
-//     form_->addKeys(availableLB->selectionModel()->selectedIndexes());
-
-       int const sel = availableLB->currentItem();
-
-       if (sel < 0)
-               return;
-
-       QStringList bibkeys = form_->available()->stringList();
-
-       // 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();
+       QModelIndex idx = selectedLV->currentIndex();
+       form_->downKey(idx);
+       selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
+       changed();
 }
 
 
-void QCitationFind::previous()
+void QCitationDialog::on_findLE_textChanged(const QString & text)
 {
-       find(biblio::BACKWARD);
+       form_->findKey(text);
+       availableLV->setModel(form_->found());
+       changed();
 }
 
 
-void QCitationFind::next()
+void QCitationDialog::changed()
 {
-       find(biblio::FORWARD);
+       fillStyles();
+       setButtons();
 }
 
 
-void QCitationFind::find(biblio::Direction dir)
-{
-/*     QStringList bibkeys = form_->available()->stringList();
-
-       biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
-
-       biblio::Search const type = searchTypeCB->isChecked()
-               ? biblio::REGEX : biblio::SIMPLE;
-
-       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 = searchCaseCB->isChecked();
-       string const str = fromqstr(searchED->text());
-
-       vector<string>::const_iterator cit =
-               biblio::searchKeys(theMap, bibkeys, str,
-                                  start, type, dir, casesens);
-
-       // not found. let's loop round
-       if (cit == bibkeys.end()) {
-               if (dir == biblio::FORWARD) {
-                       start = bibkeys.begin();
-               }
-               else start = bibkeys.end() - 1;
-
-               cit = biblio::searchKeys(theMap, bibkeys, str,
-                                        start, type, dir, casesens);
-
-               if (cit == bibkeys.end())
-                       return;
-       }
-
-       int const found = int(cit - bibkeys.begin());
-       if (found == sel) {
-               return;
-       }
-
-       // Update the display
-       // note that we have multi selection mode!
-       availableLB->setSelected(sel, false);
-       availableLB->setSelected(found, true);
-       availableLB->setCurrentItem(found);
-       availableLB->ensureCurrentVisible();
-*/
-}
-
 } // namespace frontend
 } // namespace lyx
+
+#include "QCitationDialog_moc.cpp"