]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiSelectionManager.cpp
Some improvements to the graphics dialog
[lyx.git] / src / frontends / qt4 / GuiSelectionManager.cpp
index b90d0ea1358dabae4b7c689f1d3a920efadba930..3b802081b6979443be9a5ecf20a683c0af313c86 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.
@@ -15,6 +15,7 @@
 #include <config.h>
 
 #include "GuiSelectionManager.h"
+#include "qt_helpers.h"
 
 #include "support/debug.h"
 
 namespace lyx {
 namespace frontend {
 
-GuiSelectionManager::GuiSelectionManager(
-       QAbstractItemView * avail,
-       QAbstractItemView * sel,
-       QPushButton * add, 
-       QPushButton * del, 
-       QPushButton * up, 
-       QPushButton * down,
-       QAbstractListModel * amod,
-       QAbstractItemModel * smod,
-       int const main_sel_col)
-  : availableLV(avail), selectedLV(sel), addPB(add), deletePB(del),
-               upPB(up), downPB(down), availableModel(amod), selectedModel(smod),
-               selectedHasFocus_(false), main_sel_col_(main_sel_col)
+GuiSelectionManager::GuiSelectionManager(QObject * parent,
+                                         QAbstractItemView * avail,
+                                         QAbstractItemView * sel,
+                                         QPushButton * add,
+                                         QPushButton * del,
+                                         QPushButton * up,
+                                         QPushButton * down,
+                                         QAbstractListModel * amod,
+                                         QAbstractItemModel * smod,
+                                         int const main_sel_col)
+: QObject(parent), availableLV(avail), selectedLV(sel),
+  addPB(add), deletePB(del), upPB(up), downPB(down),
+  availableModel(amod), selectedModel(smod),
+  selectedHasFocus_(false), main_sel_col_(main_sel_col)
 {
-       
        selectedLV->setModel(smod);
        availableLV->setModel(amod);
        selectedLV->setSelectionBehavior(QAbstractItemView::SelectRows);
@@ -75,17 +76,17 @@ GuiSelectionManager::GuiSelectionManager(
                this, SLOT(selectedChanged(QItemSelection, QItemSelection)));
        connect(selectedLV->itemDelegate(), SIGNAL(commitData(QWidget*)),
                this, SLOT(selectedEdited()));
-       connect(addPB, SIGNAL(clicked()), 
+       connect(addPB, SIGNAL(clicked()),
                this, SLOT(addPB_clicked()));
-       connect(deletePB, SIGNAL(clicked()), 
+       connect(deletePB, SIGNAL(clicked()),
                this, SLOT(deletePB_clicked()));
-       connect(upPB, SIGNAL(clicked()), 
+       connect(upPB, SIGNAL(clicked()),
                this, SLOT(upPB_clicked()));
-       connect(downPB, SIGNAL(clicked()), 
+       connect(downPB, SIGNAL(clicked()),
                this, SLOT(downPB_clicked()));
-       connect(availableLV, SIGNAL(doubleClicked(QModelIndex)), 
+       connect(availableLV, SIGNAL(doubleClicked(QModelIndex)),
                this, SLOT(availableLV_doubleClicked(QModelIndex)));
-       
+
        availableLV->installEventFilter(this);
        selectedLV->installEventFilter(this);
 }
@@ -107,12 +108,12 @@ QModelIndex GuiSelectionManager::getSelectedIndex(int const c) const
        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();
@@ -126,7 +127,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() &&
@@ -141,7 +142,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);
@@ -155,7 +156,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);
@@ -169,7 +170,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);
@@ -181,7 +182,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));
@@ -192,7 +193,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());
 }
@@ -202,7 +203,7 @@ void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModel
 {
        if (!idx.isValid())
                return;
-       
+
        selectedHasFocus_ = false;
        updateHook();
 }
@@ -211,7 +212,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());
 }
@@ -221,7 +222,7 @@ void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelI
 {
        if (!idx.isValid())
                return;
-       
+
        selectedHasFocus_ = true;
        updateHook();
 }
@@ -233,7 +234,7 @@ void GuiSelectionManager::selectedEdited()
 }
 
 
-bool GuiSelectionManager::insertRowToSelected(int i, 
+bool GuiSelectionManager::insertRowToSelected(int i,
                QMap<int, QVariant> const & itemData)
 {
        if (i <= -1)
@@ -272,15 +273,15 @@ void GuiSelectionManager::addPB_clicked()
        QModelIndex const idxToAdd = selIdx.first();
        QModelIndex const idx = selectedLV->currentIndex();
        int const srows = selectedModel->rowCount();
-       
+
        QMap<int, QVariant> qm = availableModel->itemData(idxToAdd);
        insertRowToSelected(srows, qm);
-       
+
        selectionChanged(); //signal
 
        if (idx.isValid())
                selectedLV->setCurrentIndex(idx);
-       
+
        updateHook();
 }
 
@@ -294,11 +295,11 @@ void GuiSelectionManager::deletePB_clicked()
        QModelIndex idx = selIdx.first();
        selectedModel->removeRow(idx.row());
        selectionChanged(); //signal
-       
+
        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)
@@ -357,7 +358,7 @@ void GuiSelectionManager::downPB_clicked()
        insertRowToSelected(pos + 1, qms);
 
        selectionChanged(); //signal
-       
+
        selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
        selectedHasFocus_ = true;
        updateHook();
@@ -368,7 +369,7 @@ void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
 {
        if (isSelected(idx) || !addPB->isEnabled())
                return;
-       
+
        if (idx.isValid())
                selectedHasFocus_ = false;
        addPB_clicked();
@@ -376,7 +377,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) {
@@ -389,27 +390,32 @@ 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) {
-                                               addPB_clicked();
-                                       } else if (keyModifiers == Qt::ControlModifier || 
-                                                 keyModifiers == Qt::KeypadModifier  ||
-                                                 keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
+                               if (!keyModifiers ||
+                                   keyModifiers == Qt::ControlModifier ||
+                                   keyModifiers == Qt::KeypadModifier  ||
+                                   keyModifiers == (Qt::ControlModifier
+                                                    | Qt::KeypadModifier)) {
+                                       if (addPB->isEnabled()) {
                                                addPB_clicked();
-                                               okHook(); //signal
                                        }
+                                       if (keyModifiers)
+                                               okHook(); //signal
                                }
                                event->accept();
                                return true;
                        }
+                       else if (keyPressed == Qt::Key_Right) {
+                               focusAndHighlight(selectedLV);
+                               event->accept();
+                               return true;
+                       }
                } else if (etype == QEvent::FocusIn) {
                        if (selectedHasFocus_) {
                                selectedHasFocus_ = false;
                                updateHook();
                        }
-                       event->accept();
-                       return true;
-               } 
+                       return false;
+               }
        } else if (obj == selectedLV) {
                if (etype == QEvent::KeyPress) {
                        QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
@@ -426,7 +432,7 @@ bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
                                        updateHook();
                                } else
                                        return QObject::eventFilter(obj, event);
-                       } 
+                       }
                        // Ctrl-Up activates upPB
                        else if (keyPressed == Qt::Key_Up) {
                                if (keyModifiers == Qt::ControlModifier) {
@@ -435,7 +441,7 @@ bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
                                        event->accept();
                                        return true;
                                }
-                       } 
+                       }
                        // Ctrl-Down activates downPB
                        else if (keyPressed == Qt::Key_Down) {
                                if (keyModifiers == Qt::ControlModifier) {
@@ -445,13 +451,17 @@ bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
                                        return true;
                                }
                        }
+                       else if (keyPressed == Qt::Key_Left) {
+                               focusAndHighlight(availableLV);
+                               event->accept();
+                               return true;
+                       }
                } else if (etype == QEvent::FocusIn) {
                        if (!selectedHasFocus_) {
                                selectedHasFocus_ = true;
                                updateHook();
                        }
-                       event->accept();
-                       return true;
+                       return false;
                }
        }
        return QObject::eventFilter(obj, event);