#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,
QPushButton * del,
QPushButton * up,
QPushButton * down,
- QStringListModel * amod,
- QStringListModel * smod)
+ QAbstractListModel * amod,
+ QAbstractListModel * smod)
{
availableLV = avail;
selectedLV = sel;
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();
}
}
-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);
}
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();
}
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();
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();
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()));
void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
{
- if (isSelected(idx))
+ if (isSelected(idx) || !addPB->isEnabled())
return;
if (idx.isValid())
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
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