From: Richard Heck Date: Sat, 2 Jun 2007 15:48:21 +0000 (+0000) Subject: Fix for bugs 3741 and 3756. Here's what it does: X-Git-Tag: 1.6.10~9513 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=ce00f4a0ac17ac137e976bcfea5c244eb151f56d;p=features.git Fix for bugs 3741 and 3756. Here's what it does: * In the "Available" list, double clicking or hitting Enter adds a citation. Hitting Ctrl-Enter adds the citation and closes the dialog. Previously, Enter did what Ctrl-Enter now does if there was no other citation already selected and double clicking would close the dialog after adding the citation. * In the "Selected" list, Delete and Backspace delete the selection. Ctrl-Delete and Ctrl-Backspace clear the whole list. Thanks to Abdel, Andre, and Lars for comments and help. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18633 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/frontends/qt4/QCitationDialog.cpp b/src/frontends/qt4/QCitationDialog.cpp index f3f5f24bc0..c33d0cfce7 100644 --- a/src/frontends/qt4/QCitationDialog.cpp +++ b/src/frontends/qt4/QCitationDialog.cpp @@ -65,6 +65,9 @@ QCitationDialog::QCitationDialog(Dialog & dialog, QCitation * form) connect(selectedLV->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(selectedChanged(const QModelIndex &, const QModelIndex &))); + connect(this, SIGNAL(rejected()), this, SLOT(cleanUp())); + availableLV->installEventFilter(this); + selectedLV->installEventFilter(this); } @@ -73,15 +76,65 @@ QCitationDialog::~QCitationDialog() } -void QCitationDialog::keyPressEvent(QKeyEvent * event) +bool QCitationDialog::eventFilter(QObject * obj, QEvent * event) +{ + if (obj == availableLV) { + if (event->type() != QEvent::KeyPress) + return QObject::eventFilter(obj, event); + QKeyEvent * keyEvent = static_cast(event); + int const keyPressed = keyEvent->key(); + Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers(); + //Enter key without modifier will add current item. + //Ctrl-Enter will add it and close the dialog. + //This is designed to work both with the main enter key + //and the one on the numeric keypad. + if ((keyPressed == Qt::Key_Enter || keyPressed == Qt::Key_Return) && + //We want one or both of Control and Keypad, and nothing else + //(KeypadModifier is what you get if you use the Enter key on the + //numeric keypad.) + (!keyModifiers || + (keyModifiers == Qt::ControlModifier) || + (keyModifiers == Qt::KeypadModifier) || + (keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) + ) + ) { + if (addPB->isEnabled()) + on_addPB_clicked(); + if (keyModifiers & Qt::ControlModifier) + on_okPB_clicked(); + event->accept(); + return true; + } + } else if (obj == selectedLV) { + //Delete or backspace key will delete current item + //...with control modifier will clear the list + if (event->type() != QEvent::KeyPress) + return QObject::eventFilter(obj, event); + QKeyEvent * keyEvent = static_cast(event); + int const keyPressed = keyEvent->key(); + Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers(); + if (keyPressed == Qt::Key_Delete || keyPressed == Qt::Key_Backspace) { + if (keyModifiers == Qt::NoModifier && deletePB->isEnabled()) + on_deletePB_clicked(); + else if (keyModifiers == Qt::ControlModifier) { + form_->clearSelection(); + update(); + } else + //ignore it otherwise + return QObject::eventFilter(obj, event); + event->accept(); + return true; + } + } + return QObject::eventFilter(obj, event); +} + + +void QCitationDialog::cleanUp() { - if (event->key() == Qt::Key_Escape) { - form_->clearSelection(); - form_->clearParams(); - event->accept(); - close(); - } else - event->ignore(); + form_->clearSelection(); + form_->clearParams(); + close(); } @@ -323,15 +376,13 @@ void QCitationDialog::availableChanged(const QModelIndex & idx, const QModelInde } -void QCitationDialog::on_availableLV_activated(const QModelIndex & idx) +void QCitationDialog::on_availableLV_doubleClicked(const QModelIndex & idx) { if (isSelected(idx)) return; selectedLV->selectionModel()->reset(); on_addPB_clicked(); - if (selectedLV->model()->rowCount() == 1) - on_okPB_clicked(); } @@ -340,10 +391,27 @@ void QCitationDialog::on_availableLV_entered(const QModelIndex &) } +namespace { +//helper function for next two +QModelIndex getSelectedIndex(QListView * lv) { + //Encourage compiler to use NRVO + QModelIndex retval = QModelIndex(); + QModelIndexList selIdx = + lv->selectionModel()->selectedIndexes(); + if (!selIdx.empty()) + retval = selIdx.first(); + return retval; +} +}//anonymous namespace + + void QCitationDialog::on_addPB_clicked() { + QModelIndex const idxToAdd = getSelectedIndex(availableLV); + if (!idxToAdd.isValid()) + return; QModelIndex idx = selectedLV->currentIndex(); - form_->addKey(availableLV->currentIndex()); + form_->addKey(idxToAdd); if (idx.isValid()) selectedLV->setCurrentIndex(idx); selectedLV->selectionModel()->reset(); @@ -353,7 +421,9 @@ void QCitationDialog::on_addPB_clicked() void QCitationDialog::on_deletePB_clicked() { - QModelIndex idx = selectedLV->currentIndex(); + QModelIndex idx = getSelectedIndex(selectedLV); + if (!idx.isValid()) + return; int nrows = selectedLV->model()->rowCount(); form_->deleteKey(idx); diff --git a/src/frontends/qt4/QCitationDialog.h b/src/frontends/qt4/QCitationDialog.h index 7aa5077a74..9a6e14a959 100644 --- a/src/frontends/qt4/QCitationDialog.h +++ b/src/frontends/qt4/QCitationDialog.h @@ -48,14 +48,16 @@ public: /// \return true if the dialog is visible. bool isVisible() const; + + /// + bool eventFilter(QObject *, QEvent *); protected: void closeEvent (QCloseEvent * e); - void keyPressEvent (QKeyEvent * event); void findText(QString const & text); protected Q_SLOTS: - + void cleanUp(); void on_okPB_clicked(); void on_cancelPB_clicked(); void on_restorePB_clicked(); @@ -70,7 +72,7 @@ protected Q_SLOTS: void on_selectedLV_clicked(const QModelIndex &); void selectedChanged(const QModelIndex &, const QModelIndex &); void on_availableLV_clicked(const QModelIndex &); - void on_availableLV_activated(const QModelIndex &); + void on_availableLV_doubleClicked(const QModelIndex &); void on_availableLV_entered(const QModelIndex &); void availableChanged(const QModelIndex &, const QModelIndex &); virtual void changed(); @@ -88,14 +90,13 @@ private: /// set the styles combo void updateStyle(); - + /// last used citation style int style_; - + QCitation * form_; }; - } // namespace frontend } // namespace lyx