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);
}
}
-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<QKeyEvent *>(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<QKeyEvent *>(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();
}
}
-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();
}
}
+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();
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);
/// \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();
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();
/// set the styles combo
void updateStyle();
-
+
/// last used citation style
int style_;
-
+
QCitation * form_;
};
-
} // namespace frontend
} // namespace lyx