]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiSelectionManager.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[lyx.git] / src / frontends / qt4 / GuiSelectionManager.cpp
index 4aa730e0acc3512ca0d42e6ce46409c370279f0b..b9e356d5665cb847e0ccf01ea84c91ead6204921 100644 (file)
 
 #include <config.h>
 #include "GuiSelectionManager.h"
+#include "GuiDocument.h"
 
+#include "support/debug.h"
+
+using std::vector;
 
 namespace lyx {
 namespace frontend {
 
+
 GuiSelectionManager::GuiSelectionManager(
        QListView * avail, 
        QListView * sel,
@@ -26,8 +31,8 @@ GuiSelectionManager::GuiSelectionManager(
        QPushButton * del, 
        QPushButton * up, 
        QPushButton * down,
-       QStringListModel * amod,
-       QStringListModel * smod)
+       QAbstractListModel * amod,
+       QAbstractListModel * smod)
        {
        availableLV = avail;
        selectedLV = sel;
@@ -69,27 +74,76 @@ GuiSelectionManager::GuiSelectionManager(
 
 void GuiSelectionManager::update()
 {
-       int const arows = availableLV->model()->rowCount();
+       updateAddPB();
+       updateDelPB();
+       updateDownPB();
+       updateUpPB();
+}
+
+
+void GuiSelectionManager::updateAddPB()
+{
+       int const arows = availableModel->rowCount();
        QModelIndexList const availSels = 
                availableLV->selectionModel()->selectedIndexes();
        addPB->setEnabled(arows > 0 &&
                !availSels.isEmpty() &&
                !isSelected(availSels.first()));
-       
-       int const srows = selectedLV->model()->rowCount();
+}
+
+
+void GuiSelectionManager::updateDelPB()
+{
+       int const srows = selectedModel->rowCount();
+       if (srows == 0) {
+               deletePB->setEnabled(false);
+               return;
+       }
        QModelIndexList const selSels = 
                selectedLV->selectionModel()->selectedIndexes();
        int const sel_nr =      selSels.empty() ? -1 : selSels.first().row();
        deletePB->setEnabled(sel_nr >= 0);
+}
+
+
+void GuiSelectionManager::updateUpPB()
+{
+       int const srows = selectedModel->rowCount();
+       if (srows == 0) {
+               upPB->setEnabled(false);
+               return;
+       }
+       QModelIndexList const selSels = 
+                       selectedLV->selectionModel()->selectedIndexes();
+       int const sel_nr =      selSels.empty() ? -1 : selSels.first().row();
        upPB->setEnabled(sel_nr > 0);
+}
+
+
+void GuiSelectionManager::updateDownPB()
+{
+       int const srows = selectedModel->rowCount();
+       if (srows == 0) {
+               downPB->setEnabled(false);
+               return;
+       }
+       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);
 }
 
 
 bool GuiSelectionManager::isSelected(const QModelIndex & idx)
 {
-       QString const str = idx.data().toString();
-       return selectedModel->stringList().contains(str);
+       if (selectedModel->rowCount() == 0)
+               return false;
+       QVariant const & str = availableModel->data(idx, Qt::DisplayRole);
+       QModelIndexList qmil = 
+                       selectedModel->match(selectedModel->index(0), 
+                                            Qt::DisplayRole, str, 
+                                            Qt::MatchExactly | Qt::MatchWrap);
+       return !qmil.empty();
 }
 
 
@@ -113,14 +167,14 @@ void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelI
 }
 
 
-static QModelIndex getSelectedIndex(QListView * lv)
+bool GuiSelectionManager::insertRowToSelected(int i, 
+               QMap<int, QVariant> const & itemData)
 {
-       QModelIndex retval = QModelIndex();
-       QModelIndexList selIdx = 
-                       lv->selectionModel()->selectedIndexes();
-       if (!selIdx.empty())
-               retval = selIdx.first();
-       return retval;
+       if (i <= -1 || i > selectedModel->rowCount())
+               return false;
+       if (!selectedModel->insertRow(i))
+               return false;
+       return selectedModel->setItemData(selectedModel->index(i), itemData);
 }
 
 
@@ -129,15 +183,17 @@ void GuiSelectionManager::addPB_clicked()
        QModelIndex const idxToAdd = getSelectedIndex(availableLV);
        if (!idxToAdd.isValid())
                return;
-       QModelIndex idx = selectedLV->currentIndex();
+       QModelIndex const idx = selectedLV->currentIndex();
+       int const srows = selectedModel->rowCount();
+       
+       QMap<int, QVariant> qm = availableModel->itemData(idxToAdd);
+       insertRowToSelected(srows, qm);
        
-       QStringList keys = selectedModel->stringList();
-       keys.append(idxToAdd.data().toString());
-       selectedModel->setStringList(keys);
        selectionChanged(); //signal
        
        if (idx.isValid())
                selectedLV->setCurrentIndex(idx);
+       
        updateHook();
 }
 
@@ -148,9 +204,7 @@ void GuiSelectionManager::deletePB_clicked()
        if (!idx.isValid())
                return;
        
-       QStringList keys = selectedModel->stringList();
-       keys.removeAt(idx.row());
-       selectedModel->setStringList(keys);
+       selectedModel->removeRow(idx.row());
        selectionChanged(); //signal
        
        int nrows = selectedLV->model()->rowCount();
@@ -169,13 +223,18 @@ void GuiSelectionManager::deletePB_clicked()
 void GuiSelectionManager::upPB_clicked()
 {
        QModelIndex idx = selectedLV->currentIndex();
-       
+
        int const pos = idx.row();
-       QStringList keys = selectedModel->stringList();
-       keys.swap(pos, pos - 1);
-       selectedModel->setStringList(keys);
-       selectionChanged(); //signal
+       if (pos <= 0)
+               return;
        
+       QMap<int, QVariant> qm = selectedModel->itemData(idx);
+
+       selectedModel->removeRow(pos);
+       insertRowToSelected(pos - 1, qm);
+
+       selectionChanged(); //signal
+
        selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
        selectedHasFocus_ = true;
        updateHook();
@@ -185,11 +244,16 @@ void GuiSelectionManager::upPB_clicked()
 void GuiSelectionManager::downPB_clicked()
 {
        QModelIndex idx = selectedLV->currentIndex();
-       
+
        int const pos = idx.row();
-       QStringList keys = selectedModel->stringList();
-       keys.swap(pos, pos + 1);
-       selectedModel->setStringList(keys);
+       if (pos >= selectedModel->rowCount() - 1)
+               return;
+
+       QMap<int, QVariant> qm = selectedModel->itemData(idx);
+
+       selectedModel->removeRow(pos);
+       insertRowToSelected(pos + 1, qm);
+
        selectionChanged(); //signal
        
        selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
@@ -211,7 +275,7 @@ void GuiSelectionManager::availableLV_clicked(const QModelIndex &)
 
 void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
 {
-       if (isSelected(idx))
+       if (isSelected(idx) || !addPB->isEnabled())
                return;
        
        if (idx.isValid())
@@ -238,25 +302,25 @@ bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
                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()) {
-                                       addPB_clicked();
-                                       okHook(); //signal
-                               }
-                               event->accept();
-                               return true;
-                               
+               //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()) {
+                               addPB_clicked();
+                               okHook(); //signal
+                       }
+                       event->accept();
+                       return true;
+                       } 
        } else if (obj == selectedLV) {
                //Delete or backspace key will delete current item
                //...with control modifier will clear the list
@@ -269,9 +333,7 @@ bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
                        if (keyModifiers == Qt::NoModifier && deletePB->isEnabled())
                                deletePB_clicked();
                        else if (keyModifiers == Qt::ControlModifier) {
-                               QStringList list = selectedModel->stringList();
-                               list.clear();
-                               selectedModel->setStringList(list);
+                               selectedModel->removeRows(0, selectedModel->rowCount());
                                updateHook();
                        } else
                                //ignore it otherwise