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);
90 selectedHasFocus_ = false;
94 void GuiSelectionManager::update()
103 QModelIndex GuiSelectionManager::getSelectedIndex() const
105 QModelIndexList avail = availableLV->selectionModel()->selectedIndexes();
106 QModelIndexList sel = selectedLV->selectionModel()->selectedIndexes();
107 bool const have_avl = !avail.isEmpty();
108 bool const have_sel = !sel.isEmpty();
110 if (selectedFocused()) {
114 return avail.front();
116 else { // available has focus
118 return avail.front();
122 return QModelIndex();
126 void GuiSelectionManager::updateAddPB()
128 int const arows = availableModel->rowCount();
129 QModelIndexList const availSels =
130 availableLV->selectionModel()->selectedIndexes();
131 addPB->setEnabled(arows > 0 &&
132 !availSels.isEmpty() &&
133 !isSelected(availSels.first()));
137 void GuiSelectionManager::updateDelPB()
139 int const srows = selectedModel->rowCount();
141 deletePB->setEnabled(false);
144 QModelIndexList const selSels =
145 selectedLV->selectionModel()->selectedIndexes();
146 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
147 deletePB->setEnabled(sel_nr >= 0);
151 void GuiSelectionManager::updateUpPB()
153 int const srows = selectedModel->rowCount();
155 upPB->setEnabled(false);
158 QModelIndexList const selSels =
159 selectedLV->selectionModel()->selectedIndexes();
160 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
161 upPB->setEnabled(sel_nr > 0);
165 void GuiSelectionManager::updateDownPB()
167 int const srows = selectedModel->rowCount();
169 downPB->setEnabled(false);
172 QModelIndexList const selSels =
173 selectedLV->selectionModel()->selectedIndexes();
174 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
175 downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
179 bool GuiSelectionManager::isSelected(const QModelIndex & idx)
181 if (selectedModel->rowCount() == 0)
183 QVariant const & str = availableModel->data(idx, Qt::DisplayRole);
184 QModelIndexList qmil =
185 selectedModel->match(selectedModel->index(0),
186 Qt::DisplayRole, str, 1,
187 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap));
188 return !qmil.empty();
192 void GuiSelectionManager::availableChanged(QItemSelection const & qis, QItemSelection const &)
194 QModelIndexList il = qis.indexes();
197 availableChanged(il.front(), QModelIndex());
201 void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModelIndex &)
206 selectedHasFocus_ = false;
211 void GuiSelectionManager::selectedChanged(QItemSelection const & qis, QItemSelection const &)
213 QModelIndexList il = qis.indexes();
216 selectedChanged(il.front(), QModelIndex());
220 void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelIndex &)
225 selectedHasFocus_ = true;
230 bool GuiSelectionManager::insertRowToSelected(int i,
231 QMap<int, QVariant> const & itemData)
235 if (i > selectedModel->rowCount())
236 i = selectedModel->rowCount();
237 if (!selectedModel->insertRow(i))
239 return selectedModel->setItemData(selectedModel->index(i), itemData);
243 void GuiSelectionManager::addPB_clicked()
245 QModelIndexList selIdx =
246 availableLV->selectionModel()->selectedIndexes();
247 if (selIdx.isEmpty())
250 QModelIndex const idxToAdd = selIdx.first();
251 QModelIndex const idx = selectedLV->currentIndex();
252 int const srows = selectedModel->rowCount();
254 QMap<int, QVariant> qm = availableModel->itemData(idxToAdd);
255 insertRowToSelected(srows, qm);
257 selectionChanged(); //signal
260 selectedLV->setCurrentIndex(idx);
266 void GuiSelectionManager::deletePB_clicked()
268 QModelIndexList selIdx =
269 selectedLV->selectionModel()->selectedIndexes();
270 if (selIdx.isEmpty())
272 QModelIndex idx = selIdx.first();
273 selectedModel->removeRow(idx.row());
274 selectionChanged(); //signal
276 int nrows = selectedLV->model()->rowCount();
277 if (idx.row() == nrows) //was last item on list
278 idx = idx.sibling(idx.row() - 1, idx.column());
281 selectedLV->setCurrentIndex(idx);
283 selectedLV->setCurrentIndex(selectedLV->model()->index(0, 0));
284 selectedHasFocus_ = (nrows > 0);
289 void GuiSelectionManager::upPB_clicked()
291 QModelIndexList selIdx =
292 selectedLV->selectionModel()->selectedIndexes();
293 if (selIdx.isEmpty())
295 QModelIndex idx = selIdx.first();
297 int const pos = idx.row();
301 QMap<int, QVariant> qm = selectedModel->itemData(idx);
303 selectedModel->removeRow(pos);
304 insertRowToSelected(pos - 1, qm);
306 selectionChanged(); //signal
308 selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
309 selectedHasFocus_ = true;
314 void GuiSelectionManager::downPB_clicked()
316 QModelIndexList selIdx =
317 selectedLV->selectionModel()->selectedIndexes();
318 if (selIdx.isEmpty())
320 QModelIndex idx = selIdx.first();
322 int const pos = idx.row();
323 if (pos >= selectedModel->rowCount() - 1)
326 QMap<int, QVariant> qm = selectedModel->itemData(idx);
328 selectedModel->removeRow(pos);
329 insertRowToSelected(pos + 1, qm);
331 selectionChanged(); //signal
333 selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
334 selectedHasFocus_ = true;
339 void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
341 if (isSelected(idx) || !addPB->isEnabled())
345 selectedHasFocus_ = false;
347 //updateHook() will be emitted there
351 bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
353 QEvent::Type etype = event->type();
354 if (obj == availableLV) {
355 if (etype == QEvent::KeyPress) {
356 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
357 int const keyPressed = keyEvent->key();
358 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
359 // Enter key without modifier will add current item.
360 // Ctrl-Enter will add it and close the dialog.
361 // This is designed to work both with the main enter key
362 // and the one on the numeric keypad.
363 if (keyPressed == Qt::Key_Enter || keyPressed == Qt::Key_Return) {
366 else if (keyModifiers == Qt::ControlModifier ||
367 keyModifiers == Qt::KeypadModifier ||
368 keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
369 if (addPB->isEnabled()) {
377 } else if (etype == QEvent::FocusIn) {
378 if (selectedHasFocus_) {
379 selectedHasFocus_ = false;
385 } else if (obj == selectedLV) {
386 if (etype == QEvent::KeyPress) {
387 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
388 int const keyPressed = keyEvent->key();
389 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
390 // Delete or backspace key will delete current item
391 // ...with control modifier will clear the list
392 if (keyPressed == Qt::Key_Delete || keyPressed == Qt::Key_Backspace) {
393 if (keyModifiers == Qt::NoModifier && deletePB->isEnabled()) {
396 } else if (keyModifiers == Qt::ControlModifier) {
397 selectedModel->removeRows(0, selectedModel->rowCount());
400 return QObject::eventFilter(obj, event);
402 // Ctrl-Up activates upPB
403 else if (keyPressed == Qt::Key_Up) {
404 if (keyModifiers == Qt::ControlModifier) {
405 if (upPB->isEnabled())
411 // Ctrl-Down activates downPB
412 else if (keyPressed == Qt::Key_Down) {
413 if (keyModifiers == Qt::ControlModifier) {
414 if (downPB->isEnabled())
420 } else if (etype == QEvent::FocusIn) {
421 if (!selectedHasFocus_) {
422 selectedHasFocus_ = true;
429 return QObject::eventFilter(obj, event);
432 } // namespace frontend
435 #include "moc_GuiSelectionManager.cpp"