X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FGuiSelectionManager.cpp;h=825115cb3a368fff67470dc774a2269ff427bd25;hb=43c09d723435a5b203f2ac0c39e2086de836b386;hp=55484b410ef5a7be681a0b287c6b701c9f9149bb;hpb=696f745f6371fb5b4284ca1085e2866916535dc3;p=lyx.git diff --git a/src/frontends/qt4/GuiSelectionManager.cpp b/src/frontends/qt4/GuiSelectionManager.cpp index 55484b410e..825115cb3a 100644 --- a/src/frontends/qt4/GuiSelectionManager.cpp +++ b/src/frontends/qt4/GuiSelectionManager.cpp @@ -13,19 +13,35 @@ */ #include + #include "GuiSelectionManager.h" -#include "GuiDocument.h" #include "support/debug.h" -using std::vector; +#include +#include +#include +#include +#include + +#ifdef KeyPress +#undef KeyPress +#endif + +#ifdef ControlModifier +#undef ControlModifier +#endif + +#ifdef FocusIn +#undef FocusIn +#endif + namespace lyx { namespace frontend { - GuiSelectionManager::GuiSelectionManager( - QListView * avail, + QAbstractItemView * avail, QListView * sel, QPushButton * add, QPushButton * del, @@ -33,25 +49,26 @@ GuiSelectionManager::GuiSelectionManager( QPushButton * down, QAbstractListModel * amod, QAbstractListModel * smod) + : availableLV(avail), selectedLV(sel), addPB(add), deletePB(del), + upPB(up), downPB(down), availableModel(amod), selectedModel(smod), + selectedHasFocus_(false) { - availableLV = avail; - selectedLV = sel; - addPB = add; - deletePB = del; - upPB = up; - downPB = down; - availableModel = amod; - selectedModel = smod; selectedLV->setModel(smod); availableLV->setModel(amod); connect(availableLV->selectionModel(), - SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), - this, SLOT(availableChanged(const QModelIndex &, const QModelIndex &))); + SIGNAL(currentChanged(QModelIndex, QModelIndex)), + this, SLOT(availableChanged(QModelIndex, QModelIndex))); + connect(selectedLV->selectionModel(), + SIGNAL(currentChanged(QModelIndex, QModelIndex)), + this, SLOT(selectedChanged(QModelIndex, QModelIndex))); + connect(availableLV->selectionModel(), + SIGNAL(selectionChanged(QItemSelection, QItemSelection)), + this, SLOT(availableChanged(QItemSelection, QItemSelection))); connect(selectedLV->selectionModel(), - SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), - this, SLOT(selectedChanged(const QModelIndex &, const QModelIndex &))); + SIGNAL(selectionChanged(QItemSelection, QItemSelection)), + this, SLOT(selectedChanged(QItemSelection, QItemSelection))); connect(addPB, SIGNAL(clicked()), this, SLOT(addPB_clicked())); connect(deletePB, SIGNAL(clicked()), @@ -60,12 +77,8 @@ GuiSelectionManager::GuiSelectionManager( this, SLOT(upPB_clicked())); connect(downPB, SIGNAL(clicked()), this, SLOT(downPB_clicked())); - connect(availableLV, SIGNAL(clicked(const QModelIndex &)), - this, SLOT(availableLV_clicked(const QModelIndex &))); - connect(availableLV, SIGNAL(doubleClicked(const QModelIndex &)), - this, SLOT(availableLV_doubleClicked(const QModelIndex &))); - connect(selectedLV, SIGNAL(clicked(const QModelIndex &)), - this, SLOT(selectedLV_clicked(const QModelIndex &))); + connect(availableLV, SIGNAL(doubleClicked(QModelIndex)), + this, SLOT(availableLV_doubleClicked(QModelIndex))); availableLV->installEventFilter(this); selectedLV->installEventFilter(this); @@ -81,6 +94,29 @@ void GuiSelectionManager::update() } +QModelIndex GuiSelectionManager::getSelectedIndex() const +{ + QModelIndexList avail = availableLV->selectionModel()->selectedIndexes(); + QModelIndexList sel = selectedLV->selectionModel()->selectedIndexes(); + bool const have_avl = !avail.isEmpty(); + bool const have_sel = !sel.isEmpty(); + + if (selectedFocused()) { + if (have_sel) + return sel.front(); + if (have_avl) + return avail.front(); + } + else { // available has focus + if (have_avl) + return avail.front(); + if (have_sel) + return sel.front(); + } + return QModelIndex(); +} + + void GuiSelectionManager::updateAddPB() { int const arows = availableModel->rowCount(); @@ -141,12 +177,21 @@ bool GuiSelectionManager::isSelected(const QModelIndex & idx) QVariant const & str = availableModel->data(idx, Qt::DisplayRole); QModelIndexList qmil = selectedModel->match(selectedModel->index(0), - Qt::DisplayRole, str, - Qt::MatchExactly | Qt::MatchWrap); + Qt::DisplayRole, str, 1, + Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap)); return !qmil.empty(); } +void GuiSelectionManager::availableChanged(QItemSelection const & qis, QItemSelection const &) +{ + QModelIndexList il = qis.indexes(); + if (il.empty()) + return; + availableChanged(il.front(), QModelIndex()); +} + + void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModelIndex &) { if (!idx.isValid()) @@ -157,6 +202,15 @@ void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModel } +void GuiSelectionManager::selectedChanged(QItemSelection const & qis, QItemSelection const &) +{ + QModelIndexList il = qis.indexes(); + if (il.empty()) + return; + selectedChanged(il.front(), QModelIndex()); +} + + void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelIndex &) { if (!idx.isValid()) @@ -170,8 +224,10 @@ void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelI bool GuiSelectionManager::insertRowToSelected(int i, QMap const & itemData) { - if (i <= -1 || i > selectedModel->rowCount()) - return false; + if (i <= -1) + i = 0; + if (i > selectedModel->rowCount()) + i = selectedModel->rowCount(); if (!selectedModel->insertRow(i)) return false; return selectedModel->setItemData(selectedModel->index(i), itemData); @@ -180,9 +236,12 @@ bool GuiSelectionManager::insertRowToSelected(int i, void GuiSelectionManager::addPB_clicked() { - QModelIndex const idxToAdd = getSelectedIndex(availableLV); - if (!idxToAdd.isValid()) + QModelIndexList selIdx = + availableLV->selectionModel()->selectedIndexes(); + if (selIdx.isEmpty()) return; + + QModelIndex const idxToAdd = selIdx.first(); QModelIndex const idx = selectedLV->currentIndex(); int const srows = selectedModel->rowCount(); @@ -190,7 +249,7 @@ void GuiSelectionManager::addPB_clicked() insertRowToSelected(srows, qm); selectionChanged(); //signal - + if (idx.isValid()) selectedLV->setCurrentIndex(idx); @@ -200,10 +259,11 @@ void GuiSelectionManager::addPB_clicked() void GuiSelectionManager::deletePB_clicked() { - QModelIndex idx = getSelectedIndex(selectedLV); - if (!idx.isValid()) + QModelIndexList selIdx = + selectedLV->selectionModel()->selectedIndexes(); + if (selIdx.isEmpty()) return; - + QModelIndex idx = selIdx.first(); selectedModel->removeRow(idx.row()); selectionChanged(); //signal @@ -214,7 +274,7 @@ void GuiSelectionManager::deletePB_clicked() if (nrows > 1) selectedLV->setCurrentIndex(idx); else if (nrows == 1) - selectedLV->setCurrentIndex(selectedLV->model()->index(0,0)); + selectedLV->setCurrentIndex(selectedLV->model()->index(0, 0)); selectedHasFocus_ = (nrows > 0); updateHook(); } @@ -222,7 +282,11 @@ void GuiSelectionManager::deletePB_clicked() void GuiSelectionManager::upPB_clicked() { - QModelIndex idx = selectedLV->currentIndex(); + QModelIndexList selIdx = + selectedLV->selectionModel()->selectedIndexes(); + if (selIdx.isEmpty()) + return; + QModelIndex idx = selIdx.first(); int const pos = idx.row(); if (pos <= 0) @@ -243,7 +307,11 @@ void GuiSelectionManager::upPB_clicked() void GuiSelectionManager::downPB_clicked() { - QModelIndex idx = selectedLV->currentIndex(); + QModelIndexList selIdx = + selectedLV->selectionModel()->selectedIndexes(); + if (selIdx.isEmpty()) + return; + QModelIndex idx = selIdx.first(); int const pos = idx.row(); if (pos >= selectedModel->rowCount() - 1) @@ -262,17 +330,6 @@ void GuiSelectionManager::downPB_clicked() } -// FIXME These slots do not really do what they need to do, since focus -// can enter the QListView in other ways. But there are no signals sent -// in that case. We need to reimplement focusInEvent() to capture those, -// which means subclassing QListView. (rgh) -void GuiSelectionManager::availableLV_clicked(const QModelIndex &) -{ - selectedHasFocus_ = false; - updateHook(); -} - - void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx) { if (isSelected(idx) || !addPB->isEnabled()) @@ -285,75 +342,82 @@ void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx) } -void GuiSelectionManager::selectedLV_clicked(const QModelIndex &) -{ - selectedHasFocus_ = true; - updateHook(); -} - - bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event) { + QEvent::Type etype = event->type(); 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) { - if (!keyModifiers) - addPB_clicked(); - else if ((keyModifiers == Qt::ControlModifier) || - (keyModifiers == Qt::KeypadModifier) || - (keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) - ) { - if (addPB->isEnabled()) { + if (etype == QEvent::KeyPress) { + 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) { + if (!keyModifiers) addPB_clicked(); - okHook(); //signal + else if (keyModifiers == Qt::ControlModifier || + keyModifiers == Qt::KeypadModifier || + keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) { + if (addPB->isEnabled()) { + addPB_clicked(); + okHook(); //signal + } } + event->accept(); + return true; + } + } else if (etype == QEvent::FocusIn) { + if (selectedHasFocus_) { + selectedHasFocus_ = false; + updateHook(); } event->accept(); return true; - } - } else if (obj == selectedLV) { - 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(); - // Delete or backspace key will delete current item - // ...with control modifier will clear the list - if (keyPressed == Qt::Key_Delete || keyPressed == Qt::Key_Backspace) { - if (keyModifiers == Qt::NoModifier && deletePB->isEnabled()) { - deletePB_clicked(); - updateHook(); - } else if (keyModifiers == Qt::ControlModifier) { - selectedModel->removeRows(0, selectedModel->rowCount()); - updateHook(); - } else - return QObject::eventFilter(obj, event); } - // Ctrl-Up activates upPB - else if (keyPressed == Qt::Key_Up) { - if (keyModifiers == Qt::ControlModifier) { - if (upPB->isEnabled()) - upPB_clicked(); - event->accept(); - return true; + } else if (obj == selectedLV) { + if (etype == QEvent::KeyPress) { + QKeyEvent * keyEvent = static_cast(event); + int const keyPressed = keyEvent->key(); + Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers(); + // Delete or backspace key will delete current item + // ...with control modifier will clear the list + if (keyPressed == Qt::Key_Delete || keyPressed == Qt::Key_Backspace) { + if (keyModifiers == Qt::NoModifier && deletePB->isEnabled()) { + deletePB_clicked(); + updateHook(); + } else if (keyModifiers == Qt::ControlModifier) { + selectedModel->removeRows(0, selectedModel->rowCount()); + updateHook(); + } else + return QObject::eventFilter(obj, event); + } + // Ctrl-Up activates upPB + else if (keyPressed == Qt::Key_Up) { + if (keyModifiers == Qt::ControlModifier) { + if (upPB->isEnabled()) + upPB_clicked(); + event->accept(); + return true; + } + } + // Ctrl-Down activates downPB + else if (keyPressed == Qt::Key_Down) { + if (keyModifiers == Qt::ControlModifier) { + if (downPB->isEnabled()) + downPB_clicked(); + event->accept(); + return true; + } } - } - // Ctrl-Down activates downPB - else if (keyPressed == Qt::Key_Down) { - if (keyModifiers == Qt::ControlModifier) { - if (downPB->isEnabled()) - downPB_clicked(); - event->accept(); - return true; + } else if (etype == QEvent::FocusIn) { + if (!selectedHasFocus_) { + selectedHasFocus_ = true; + updateHook(); } + event->accept(); + return true; } } return QObject::eventFilter(obj, event); @@ -362,4 +426,4 @@ bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event) } // namespace frontend } // namespace lyx -#include "GuiSelectionManager_moc.cpp" +#include "moc_GuiSelectionManager.cpp"