2 * \file GuiSelectionManager.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Some of the material in this file previously appeared in
10 * GuiCitationDialog.cpp.
12 * Full author contact details are available in file CREDITS.
17 #include "GuiSelectionManager.h"
19 #include "support/debug.h"
21 #include <QAbstractListModel>
22 #include <QItemSelection>
25 #include <QPushButton>
31 #ifdef ControlModifier
32 #undef ControlModifier
43 GuiSelectionManager::GuiSelectionManager(
44 QAbstractItemView * avail,
50 QAbstractListModel * amod,
51 QAbstractListModel * smod)
59 availableModel = amod;
62 selectedLV->setModel(smod);
63 availableLV->setModel(amod);
65 connect(availableLV->selectionModel(),
66 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
67 this, SLOT(availableChanged(QModelIndex, QModelIndex)));
68 connect(selectedLV->selectionModel(),
69 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
70 this, SLOT(selectedChanged(QModelIndex, QModelIndex)));
71 connect(availableLV->selectionModel(),
72 SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
73 this, SLOT(availableChanged(QItemSelection, QItemSelection)));
74 connect(selectedLV->selectionModel(),
75 SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
76 this, SLOT(selectedChanged(QItemSelection, QItemSelection)));
77 connect(addPB, SIGNAL(clicked()),
78 this, SLOT(addPB_clicked()));
79 connect(deletePB, SIGNAL(clicked()),
80 this, SLOT(deletePB_clicked()));
81 connect(upPB, SIGNAL(clicked()),
82 this, SLOT(upPB_clicked()));
83 connect(downPB, SIGNAL(clicked()),
84 this, SLOT(downPB_clicked()));
85 connect(availableLV, SIGNAL(doubleClicked(QModelIndex)),
86 this, SLOT(availableLV_doubleClicked(QModelIndex)));
88 availableLV->installEventFilter(this);
89 selectedLV->installEventFilter(this);
93 void GuiSelectionManager::update()
102 QModelIndex GuiSelectionManager::getSelectedIndex() const
104 QModelIndexList avail = availableLV->selectionModel()->selectedIndexes();
105 QModelIndexList sel = selectedLV->selectionModel()->selectedIndexes();
106 bool const have_avl = !avail.isEmpty();
107 bool const have_sel = !sel.isEmpty();
109 if (selectedFocused()) {
113 return avail.front();
115 else { // available has focus
117 return avail.front();
121 return QModelIndex();
125 void GuiSelectionManager::updateAddPB()
127 int const arows = availableModel->rowCount();
128 QModelIndexList const availSels =
129 availableLV->selectionModel()->selectedIndexes();
130 addPB->setEnabled(arows > 0 &&
131 !availSels.isEmpty() &&
132 !isSelected(availSels.first()));
136 void GuiSelectionManager::updateDelPB()
138 int const srows = selectedModel->rowCount();
140 deletePB->setEnabled(false);
143 QModelIndexList const selSels =
144 selectedLV->selectionModel()->selectedIndexes();
145 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
146 deletePB->setEnabled(sel_nr >= 0);
150 void GuiSelectionManager::updateUpPB()
152 int const srows = selectedModel->rowCount();
154 upPB->setEnabled(false);
157 QModelIndexList const selSels =
158 selectedLV->selectionModel()->selectedIndexes();
159 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
160 upPB->setEnabled(sel_nr > 0);
164 void GuiSelectionManager::updateDownPB()
166 int const srows = selectedModel->rowCount();
168 downPB->setEnabled(false);
171 QModelIndexList const selSels =
172 selectedLV->selectionModel()->selectedIndexes();
173 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
174 downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
178 bool GuiSelectionManager::isSelected(const QModelIndex & idx)
180 if (selectedModel->rowCount() == 0)
182 QVariant const & str = availableModel->data(idx, Qt::DisplayRole);
183 QModelIndexList qmil =
184 selectedModel->match(selectedModel->index(0),
185 Qt::DisplayRole, str, 1,
186 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap));
187 return !qmil.empty();
191 void GuiSelectionManager::availableChanged(QItemSelection const & qis, QItemSelection const &)
193 QModelIndexList il = qis.indexes();
196 availableChanged(il.front(), QModelIndex());
200 void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModelIndex &)
205 selectedHasFocus_ = false;
210 void GuiSelectionManager::selectedChanged(QItemSelection const & qis, QItemSelection const &)
212 QModelIndexList il = qis.indexes();
215 selectedChanged(il.front(), QModelIndex());
219 void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelIndex &)
224 selectedHasFocus_ = true;
229 bool GuiSelectionManager::insertRowToSelected(int i,
230 QMap<int, QVariant> const & itemData)
234 if (i > selectedModel->rowCount())
235 i = selectedModel->rowCount();
236 if (!selectedModel->insertRow(i))
238 return selectedModel->setItemData(selectedModel->index(i), itemData);
242 void GuiSelectionManager::addPB_clicked()
244 QModelIndexList selIdx =
245 availableLV->selectionModel()->selectedIndexes();
246 if (selIdx.isEmpty())
249 QModelIndex const idxToAdd = selIdx.first();
250 QModelIndex const idx = selectedLV->currentIndex();
251 int const srows = selectedModel->rowCount();
253 QMap<int, QVariant> qm = availableModel->itemData(idxToAdd);
254 insertRowToSelected(srows, qm);
256 selectionChanged(); //signal
259 selectedLV->setCurrentIndex(idx);
265 void GuiSelectionManager::deletePB_clicked()
267 QModelIndexList selIdx =
268 selectedLV->selectionModel()->selectedIndexes();
269 if (selIdx.isEmpty())
271 QModelIndex idx = selIdx.first();
272 selectedModel->removeRow(idx.row());
273 selectionChanged(); //signal
275 int nrows = selectedLV->model()->rowCount();
276 if (idx.row() == nrows) //was last item on list
277 idx = idx.sibling(idx.row() - 1, idx.column());
280 selectedLV->setCurrentIndex(idx);
282 selectedLV->setCurrentIndex(selectedLV->model()->index(0, 0));
283 selectedHasFocus_ = (nrows > 0);
288 void GuiSelectionManager::upPB_clicked()
290 QModelIndexList selIdx =
291 selectedLV->selectionModel()->selectedIndexes();
292 if (selIdx.isEmpty())
294 QModelIndex idx = selIdx.first();
296 int const pos = idx.row();
300 QMap<int, QVariant> qm = selectedModel->itemData(idx);
302 selectedModel->removeRow(pos);
303 insertRowToSelected(pos - 1, qm);
305 selectionChanged(); //signal
307 selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
308 selectedHasFocus_ = true;
313 void GuiSelectionManager::downPB_clicked()
315 QModelIndexList selIdx =
316 selectedLV->selectionModel()->selectedIndexes();
317 if (selIdx.isEmpty())
319 QModelIndex idx = selIdx.first();
321 int const pos = idx.row();
322 if (pos >= selectedModel->rowCount() - 1)
325 QMap<int, QVariant> qm = selectedModel->itemData(idx);
327 selectedModel->removeRow(pos);
328 insertRowToSelected(pos + 1, qm);
330 selectionChanged(); //signal
332 selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
333 selectedHasFocus_ = true;
338 void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
340 if (isSelected(idx) || !addPB->isEnabled())
344 selectedHasFocus_ = false;
346 //updateHook() will be emitted there
350 bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
352 QEvent::Type etype = event->type();
353 if (obj == availableLV) {
354 if (etype == QEvent::KeyPress) {
355 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
356 int const keyPressed = keyEvent->key();
357 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
358 // Enter key without modifier will add current item.
359 // Ctrl-Enter will add it and close the dialog.
360 // This is designed to work both with the main enter key
361 // and the one on the numeric keypad.
362 if (keyPressed == Qt::Key_Enter || keyPressed == Qt::Key_Return) {
365 else if (keyModifiers == Qt::ControlModifier ||
366 keyModifiers == Qt::KeypadModifier ||
367 keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
368 if (addPB->isEnabled()) {
376 } else if (etype == QEvent::FocusIn) {
377 if (selectedHasFocus_) {
378 selectedHasFocus_ = false;
384 } else if (obj == selectedLV) {
385 if (etype == QEvent::KeyPress) {
386 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
387 int const keyPressed = keyEvent->key();
388 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
389 // Delete or backspace key will delete current item
390 // ...with control modifier will clear the list
391 if (keyPressed == Qt::Key_Delete || keyPressed == Qt::Key_Backspace) {
392 if (keyModifiers == Qt::NoModifier && deletePB->isEnabled()) {
395 } else if (keyModifiers == Qt::ControlModifier) {
396 selectedModel->removeRows(0, selectedModel->rowCount());
399 return QObject::eventFilter(obj, event);
401 // Ctrl-Up activates upPB
402 else if (keyPressed == Qt::Key_Up) {
403 if (keyModifiers == Qt::ControlModifier) {
404 if (upPB->isEnabled())
410 // Ctrl-Down activates downPB
411 else if (keyPressed == Qt::Key_Down) {
412 if (keyModifiers == Qt::ControlModifier) {
413 if (downPB->isEnabled())
419 } else if (etype == QEvent::FocusIn) {
420 if (!selectedHasFocus_) {
421 selectedHasFocus_ = true;
428 return QObject::eventFilter(obj, event);
431 } // namespace frontend
434 #include "moc_GuiSelectionManager.cpp"