#include <config.h>
#include "GuiSelectionManager.h"
+#include "qt_helpers.h"
#include "support/debug.h"
+#include <QAbstractItemModel>
#include <QAbstractListModel>
#include <QItemSelection>
#include <QListView>
#undef ControlModifier
#endif
+#ifdef FocusIn
+#undef FocusIn
+#endif
+
namespace lyx {
namespace frontend {
-GuiSelectionManager::GuiSelectionManager(
- QAbstractItemView * avail,
- QListView * sel,
- QPushButton * add,
- QPushButton * del,
- QPushButton * up,
- QPushButton * down,
- QAbstractListModel * amod,
- QAbstractListModel * smod)
+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)
{
- availableLV = avail;
- selectedLV = sel;
- addPB = add;
- deletePB = del;
- upPB = up;
- downPB = down;
- availableModel = amod;
- selectedModel = smod;
-
selectedLV->setModel(smod);
availableLV->setModel(amod);
-
+ selectedLV->setSelectionBehavior(QAbstractItemView::SelectRows);
+ selectedLV->setSelectionMode(QAbstractItemView::SingleSelection);
+
connect(availableLV->selectionModel(),
SIGNAL(currentChanged(QModelIndex, QModelIndex)),
this, SLOT(availableChanged(QModelIndex, QModelIndex)));
connect(selectedLV->selectionModel(),
SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
this, SLOT(selectedChanged(QItemSelection, QItemSelection)));
- connect(addPB, SIGNAL(clicked()),
+ connect(selectedLV->itemDelegate(), SIGNAL(commitData(QWidget*)),
+ this, SLOT(selectedEdited()));
+ 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);
}
}
-QModelIndex GuiSelectionManager::getSelectedIndex() const
+QModelIndex GuiSelectionManager::getSelectedIndex(int const c) const
{
QModelIndexList avail = availableLV->selectionModel()->selectedIndexes();
- QModelIndexList sel = selectedLV->selectionModel()->selectedIndexes();
+ QModelIndexList sel = selectedLV->selectionModel()->selectedRows(c);
bool const have_avl = !avail.isEmpty();
bool const have_sel = !sel.isEmpty();
}
QModelIndexList const selSels =
selectedLV->selectionModel()->selectedIndexes();
- int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
+ int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
deletePB->setEnabled(sel_nr >= 0);
}
}
QModelIndexList const selSels =
selectedLV->selectionModel()->selectedIndexes();
- int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
+ int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
upPB->setEnabled(sel_nr > 0);
}
}
QModelIndexList const selSels =
selectedLV->selectionModel()->selectedIndexes();
- int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
+ int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
}
return false;
QVariant const & str = availableModel->data(idx, Qt::DisplayRole);
QModelIndexList qmil =
- selectedModel->match(selectedModel->index(0),
+ selectedModel->match(selectedModel->index(0, main_sel_col_),
Qt::DisplayRole, str, 1,
Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap));
return !qmil.empty();
{
if (!idx.isValid())
return;
-
+
selectedHasFocus_ = false;
updateHook();
}
{
if (!idx.isValid())
return;
-
+
selectedHasFocus_ = true;
updateHook();
}
+void GuiSelectionManager::selectedEdited()
+{
+ selectionChanged();
+}
+
+
bool GuiSelectionManager::insertRowToSelected(int i,
QMap<int, QVariant> const & itemData)
{
- if (i <= -1 || i > selectedModel->rowCount())
+ if (i <= -1)
+ i = 0;
+ if (i > selectedModel->rowCount())
+ i = selectedModel->rowCount();
+ if (!selectedModel->insertRow(i))
return false;
+ return selectedModel->setItemData(selectedModel->index(i, main_sel_col_), itemData);
+}
+
+
+bool GuiSelectionManager::insertRowToSelected(int i, QMap<int, QMap<int, QVariant>> & qms)
+{
+ if (i <= -1)
+ i = 0;
+ if (i > selectedModel->rowCount())
+ i = selectedModel->rowCount();
if (!selectedModel->insertRow(i))
return false;
- return selectedModel->setItemData(selectedModel->index(i), itemData);
+ bool res = true;
+ QMap<int, QMap<int, QVariant>>::const_iterator it = qms.constBegin();
+ for (; it != qms.constEnd(); ++it)
+ res &= selectedModel->setItemData(selectedModel->index(i, it.key()), it.value());
+ return res;
}
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();
}
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)
void GuiSelectionManager::upPB_clicked()
{
- QModelIndex idx = selectedLV->currentIndex();
+ QModelIndexList selIdx =
+ selectedLV->selectionModel()->selectedIndexes();
+ if (selIdx.isEmpty())
+ return;
+ QModelIndex idx = selIdx.first();
int const pos = idx.row();
if (pos <= 0)
return;
-
- QMap<int, QVariant> qm = selectedModel->itemData(idx);
+
+ QMap<int, QMap<int, QVariant>> qms;
+ QList<QModelIndex>::const_iterator it = selIdx.constBegin();
+ for (; it != selIdx.constEnd(); ++it)
+ qms[it->column()] = selectedModel->itemData(*it);
selectedModel->removeRow(pos);
- insertRowToSelected(pos - 1, qm);
+ insertRowToSelected(pos - 1, qms);
selectionChanged(); //signal
void GuiSelectionManager::downPB_clicked()
{
- QModelIndex idx = selectedLV->currentIndex();
+ QModelIndexList selIdx =
+ selectedLV->selectionModel()->selectedIndexes();
+ if (selIdx.isEmpty())
+ return;
+ QModelIndex idx = selIdx.first();
int const pos = idx.row();
if (pos >= selectedModel->rowCount() - 1)
return;
- QMap<int, QVariant> qm = selectedModel->itemData(idx);
+ QMap<int, QMap<int, QVariant>> qms;
+ QList<QModelIndex>::const_iterator it = selIdx.constBegin();
+ for (; it != selIdx.constEnd(); ++it)
+ qms[it->column()] = selectedModel->itemData(*it);
selectedModel->removeRow(pos);
- insertRowToSelected(pos + 1, qm);
+ insertRowToSelected(pos + 1, qms);
selectionChanged(); //signal
-
+
selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
selectedHasFocus_ = true;
updateHook();
{
if (isSelected(idx) || !addPB->isEnabled())
return;
-
+
if (idx.isValid())
selectedHasFocus_ = false;
addPB_clicked();
// 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 (!keyModifiers)
- addPB_clicked();
- else if (keyModifiers == Qt::ControlModifier ||
- keyModifiers == Qt::KeypadModifier ||
- keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
- if (addPB->isEnabled()) {
+ if (addPB->isEnabled()) {
+ if (!keyModifiers) {
+ addPB_clicked();
+ } else if (keyModifiers == Qt::ControlModifier ||
+ keyModifiers == Qt::KeypadModifier ||
+ keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
addPB_clicked();
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);
updateHook();
} else
return QObject::eventFilter(obj, event);
- }
+ }
// Ctrl-Up activates upPB
else if (keyPressed == Qt::Key_Up) {
if (keyModifiers == Qt::ControlModifier) {
event->accept();
return true;
}
- }
+ }
// Ctrl-Down activates downPB
else if (keyPressed == Qt::Key_Down) {
if (keyModifiers == Qt::ControlModifier) {
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);