]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiSelectionManager.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / GuiSelectionManager.cpp
index 5523495d4f05931444318ebc3ff4222722f2023f..9eeb5c96851c76d4b528bebbd6b8dd4e1f273d50 100644 (file)
@@ -6,7 +6,7 @@
  * \author Richard Heck
  * \author Et Alia
  *
- * Some of the material in this file previously appeared in 
+ * Some of the material in this file previously appeared in
  * GuiCitationDialog.cpp.
  *
  * Full author contact details are available in file CREDITS.
@@ -49,7 +49,7 @@ GuiSelectionManager::GuiSelectionManager(QObject * parent,
                                          QPushButton * del,
                                          QPushButton * up,
                                          QPushButton * down,
-                                         QAbstractListModel * amod,
+                                         QAbstractItemModel * amod,
                                          QAbstractItemModel * smod,
                                          int const main_sel_col)
 : QObject(parent), availableLV(avail), selectedLV(sel),
@@ -71,9 +71,15 @@ GuiSelectionManager::GuiSelectionManager(QObject * parent,
        connect(availableLV->selectionModel(),
                SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
                this, SLOT(availableChanged(QItemSelection, QItemSelection)));
+       connect(availableLV->selectionModel(),
+               SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
+               this, SLOT(updateButtons()));
        connect(selectedLV->selectionModel(),
                SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
                this, SLOT(selectedChanged(QItemSelection, QItemSelection)));
+       connect(selectedLV->selectionModel(),
+               SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
+               this, SLOT(updateButtons()));
        connect(selectedLV->itemDelegate(), SIGNAL(commitData(QWidget*)),
                this, SLOT(selectedEdited()));
        connect(addPB, SIGNAL(clicked()),
@@ -101,19 +107,26 @@ void GuiSelectionManager::update()
 }
 
 
+void GuiSelectionManager::updateButtons()
+{
+       update();
+       updateHook();
+}
+
+
 QModelIndex GuiSelectionManager::getSelectedIndex(int const c) const
 {
-       QModelIndexList avail = availableLV->selectionModel()->selectedIndexes();
+       QModelIndexList avail = availableLV->selectionModel()->selectedRows(c);
        QModelIndexList sel   = selectedLV->selectionModel()->selectedRows(c);
        bool const have_avl = !avail.isEmpty();
        bool const have_sel = !sel.isEmpty();
 
-       if (selectedFocused()) { 
+       if (selectedFocused()) {
                if (have_sel)
                        return sel.front();
                if (have_avl)
                        return avail.front();
-       } 
+       }
        else { // available has focus
                if (have_avl)
                        return avail.front();
@@ -127,7 +140,7 @@ QModelIndex GuiSelectionManager::getSelectedIndex(int const c) const
 void GuiSelectionManager::updateAddPB()
 {
        int const arows = availableModel->rowCount();
-       QModelIndexList const availSels = 
+       QModelIndexList const availSels =
                availableLV->selectionModel()->selectedIndexes();
        addPB->setEnabled(arows > 0 &&
                !availSels.isEmpty() &&
@@ -142,7 +155,7 @@ void GuiSelectionManager::updateDelPB()
                deletePB->setEnabled(false);
                return;
        }
-       QModelIndexList const selSels = 
+       QModelIndexList const selSels =
                selectedLV->selectionModel()->selectedIndexes();
        int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
        deletePB->setEnabled(sel_nr >= 0);
@@ -156,7 +169,7 @@ void GuiSelectionManager::updateUpPB()
                upPB->setEnabled(false);
                return;
        }
-       QModelIndexList const selSels = 
+       QModelIndexList const selSels =
                        selectedLV->selectionModel()->selectedIndexes();
        int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
        upPB->setEnabled(sel_nr > 0);
@@ -170,7 +183,7 @@ void GuiSelectionManager::updateDownPB()
                downPB->setEnabled(false);
                return;
        }
-       QModelIndexList const selSels = 
+       QModelIndexList const selSels =
                        selectedLV->selectionModel()->selectedIndexes();
        int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
        downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
@@ -182,7 +195,7 @@ bool GuiSelectionManager::isSelected(const QModelIndex & idx)
        if (selectedModel->rowCount() == 0)
                return false;
        QVariant const & str = availableModel->data(idx, Qt::DisplayRole);
-       QModelIndexList qmil = 
+       QModelIndexList qmil =
                        selectedModel->match(selectedModel->index(0, main_sel_col_),
                                             Qt::DisplayRole, str, 1,
                                             Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap));
@@ -193,7 +206,7 @@ bool GuiSelectionManager::isSelected(const QModelIndex & idx)
 void GuiSelectionManager::availableChanged(QItemSelection const & qis, QItemSelection const &)
 {
        QModelIndexList il = qis.indexes();
-       if (il.empty()) 
+       if (il.empty())
                return;
        availableChanged(il.front(), QModelIndex());
 }
@@ -212,7 +225,7 @@ void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModel
 void GuiSelectionManager::selectedChanged(QItemSelection const & qis, QItemSelection const &)
 {
        QModelIndexList il = qis.indexes();
-       if (il.empty()) 
+       if (il.empty())
                return;
        selectedChanged(il.front(), QModelIndex());
 }
@@ -234,7 +247,7 @@ void GuiSelectionManager::selectedEdited()
 }
 
 
-bool GuiSelectionManager::insertRowToSelected(int i, 
+bool GuiSelectionManager::insertRowToSelected(int i,
                QMap<int, QVariant> const & itemData)
 {
        if (i <= -1)
@@ -293,18 +306,19 @@ void GuiSelectionManager::deletePB_clicked()
        if (selIdx.isEmpty())
                return;
        QModelIndex idx = selIdx.first();
-       selectedModel->removeRow(idx.row());
-       selectionChanged(); //signal
 
+       int const row = idx.row();
        int nrows = selectedLV->model()->rowCount();
-       if (idx.row() == nrows) //was last item on list
-               idx = idx.sibling(idx.row() - 1, idx.column());
 
-       if (nrows > 1)
-               selectedLV->setCurrentIndex(idx);
-       else if (nrows == 1)
+       selectedModel->removeRow(row);
+       selectionChanged(); //signal
+
+       // select previous item
+       if (nrows > 0)
+               selectedLV->setCurrentIndex(selectedLV->model()->index(row - 1, 0));
+       else if (nrows == 0)
                selectedLV->setCurrentIndex(selectedLV->model()->index(0, 0));
-       selectedHasFocus_ = (nrows > 0);
+       selectedHasFocus_ = (nrows > 1);
        updateHook();
 }
 
@@ -377,7 +391,7 @@ void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
 }
 
 
-bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event) 
+bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
 {
        QEvent::Type etype = event->type();
        if (obj == availableLV) {
@@ -390,15 +404,16 @@ bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
                        // 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 (addPB->isEnabled()) {
-                                       if (!keyModifiers) {
+                               if (!keyModifiers ||
+                                   keyModifiers == Qt::ControlModifier ||
+                                   keyModifiers == Qt::KeypadModifier  ||
+                                   keyModifiers == (Qt::ControlModifier
+                                                    | Qt::KeypadModifier)) {
+                                       if (addPB->isEnabled()) {
                                                addPB_clicked();
-                                       } else if (keyModifiers == Qt::ControlModifier ||
-                                                 keyModifiers == Qt::KeypadModifier  ||
-                                                 keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
-                                               addPB_clicked();
-                                               okHook(); //signal
                                        }
+                                       if (keyModifiers)
+                                               okHook(); //signal
                                }
                                event->accept();
                                return true;