]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiSelectionManager.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiSelectionManager.cpp
index ab103c7653ad7083a064db595f177b3f27ab18be..4d172bc35d890cb52de14e27d6ec9f50f9edcfaf 100644 (file)
  */
 
 #include <config.h>
+
 #include "GuiSelectionManager.h"
-#include "GuiDocument.h"
 
 #include "support/debug.h"
 
-using std::vector;
+#include <QKeyEvent>
+#include <QListView>
+#include <QPushButton>
+#include <QAbstractListModel>
+
+#ifdef KeyPress
+#undef KeyPress
+#endif
+
+#ifdef ControlModifier
+#undef ControlModifier
+#endif
+
 
 namespace lyx {
 namespace frontend {
 
-
 GuiSelectionManager::GuiSelectionManager(
-       QListView * avail, 
+       QAbstractItemView * avail,
        QListView * sel,
        QPushButton * add, 
        QPushButton * del, 
@@ -47,11 +58,11 @@ GuiSelectionManager::GuiSelectionManager(
        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(const QModelIndex &, const QModelIndex &)),
-               this, SLOT(selectedChanged(const QModelIndex &, const QModelIndex &)));
+               SIGNAL(currentChanged(QModelIndex, QModelIndex)),
+               this, SLOT(selectedChanged(QModelIndex, QModelIndex)));
        connect(addPB, SIGNAL(clicked()), 
                this, SLOT(addPB_clicked()));
        connect(deletePB, SIGNAL(clicked()), 
@@ -60,12 +71,12 @@ 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(clicked(QModelIndex)), 
+               this, SLOT(availableLV_clicked(QModelIndex)));
+       connect(availableLV, SIGNAL(doubleClicked(QModelIndex)), 
+               this, SLOT(availableLV_doubleClicked(QModelIndex)));
+       connect(selectedLV, SIGNAL(clicked(QModelIndex)), 
+               this, SLOT(selectedLV_clicked(QModelIndex)));
        
        availableLV->installEventFilter(this);
        selectedLV->installEventFilter(this);
@@ -141,8 +152,8 @@ 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();
 }
 
@@ -180,9 +191,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 +204,7 @@ void GuiSelectionManager::addPB_clicked()
        insertRowToSelected(srows, qm);
        
        selectionChanged(); //signal
-       
+
        if (idx.isValid())
                selectedLV->setCurrentIndex(idx);
        
@@ -200,10 +214,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 +229,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();
 }
@@ -266,6 +281,7 @@ void GuiSelectionManager::downPB_clicked()
 // 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)
+// Or by installing an event listener.. (andre)
 void GuiSelectionManager::availableLV_clicked(const QModelIndex &)
 {
        selectedHasFocus_ = false;
@@ -307,10 +323,9 @@ bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
                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))
-                                ) {
+                       else if (keyModifiers == Qt::ControlModifier ||
+                                       keyModifiers == Qt::KeypadModifier  ||
+                                       keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
                                if (addPB->isEnabled()) {
                                        addPB_clicked();
                                        okHook(); //signal
@@ -320,13 +335,13 @@ bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
                        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();
+               // 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();
@@ -336,14 +351,18 @@ bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
                                updateHook();
                        } else
                                return QObject::eventFilter(obj, event);
-               } else if (keyPressed == Qt::Key_Up) {
+               } 
+               // 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 (keyPressed == Qt::Key_Down) {
+               } 
+               // Ctrl-Down activates downPB
+               else if (keyPressed == Qt::Key_Down) {
                        if (keyModifiers == Qt::ControlModifier) {
                                if (downPB->isEnabled())
                                        downPB_clicked();
@@ -358,4 +377,4 @@ bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiSelectionManager_moc.cpp"
+#include "moc_GuiSelectionManager.cpp"