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
39 GuiSelectionManager::GuiSelectionManager(
40 QAbstractItemView * avail,
46 QAbstractListModel * amod,
47 QAbstractListModel * smod)
55 availableModel = amod;
58 selectedLV->setModel(smod);
59 availableLV->setModel(amod);
61 connect(availableLV->selectionModel(),
62 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
63 this, SLOT(availableChanged(QModelIndex, QModelIndex)));
64 connect(selectedLV->selectionModel(),
65 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
66 this, SLOT(selectedChanged(QModelIndex, QModelIndex)));
67 connect(availableLV->selectionModel(),
68 SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
69 this, SLOT(availableChanged(QItemSelection, QItemSelection)));
70 connect(selectedLV->selectionModel(),
71 SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
72 this, SLOT(selectedChanged(QItemSelection, QItemSelection)));
73 connect(addPB, SIGNAL(clicked()),
74 this, SLOT(addPB_clicked()));
75 connect(deletePB, SIGNAL(clicked()),
76 this, SLOT(deletePB_clicked()));
77 connect(upPB, SIGNAL(clicked()),
78 this, SLOT(upPB_clicked()));
79 connect(downPB, SIGNAL(clicked()),
80 this, SLOT(downPB_clicked()));
81 connect(availableLV, SIGNAL(doubleClicked(QModelIndex)),
82 this, SLOT(availableLV_doubleClicked(QModelIndex)));
84 availableLV->installEventFilter(this);
85 selectedLV->installEventFilter(this);
89 void GuiSelectionManager::update()
98 QModelIndex GuiSelectionManager::getSelectedIndex() const
100 QModelIndexList avail = availableLV->selectionModel()->selectedIndexes();
101 QModelIndexList sel = selectedLV->selectionModel()->selectedIndexes();
102 bool const have_avl = !avail.isEmpty();
103 bool const have_sel = !sel.isEmpty();
105 if (selectedFocused()) {
109 return avail.front();
111 else { // available has focus
113 return avail.front();
117 return QModelIndex();
121 void GuiSelectionManager::updateAddPB()
123 int const arows = availableModel->rowCount();
124 QModelIndexList const availSels =
125 availableLV->selectionModel()->selectedIndexes();
126 addPB->setEnabled(arows > 0 &&
127 !availSels.isEmpty() &&
128 !isSelected(availSels.first()));
132 void GuiSelectionManager::updateDelPB()
134 int const srows = selectedModel->rowCount();
136 deletePB->setEnabled(false);
139 QModelIndexList const selSels =
140 selectedLV->selectionModel()->selectedIndexes();
141 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
142 deletePB->setEnabled(sel_nr >= 0);
146 void GuiSelectionManager::updateUpPB()
148 int const srows = selectedModel->rowCount();
150 upPB->setEnabled(false);
153 QModelIndexList const selSels =
154 selectedLV->selectionModel()->selectedIndexes();
155 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
156 upPB->setEnabled(sel_nr > 0);
160 void GuiSelectionManager::updateDownPB()
162 int const srows = selectedModel->rowCount();
164 downPB->setEnabled(false);
167 QModelIndexList const selSels =
168 selectedLV->selectionModel()->selectedIndexes();
169 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
170 downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
174 bool GuiSelectionManager::isSelected(const QModelIndex & idx)
176 if (selectedModel->rowCount() == 0)
178 QVariant const & str = availableModel->data(idx, Qt::DisplayRole);
179 QModelIndexList qmil =
180 selectedModel->match(selectedModel->index(0),
181 Qt::DisplayRole, str, 1,
182 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap));
183 return !qmil.empty();
187 void GuiSelectionManager::availableChanged(QItemSelection const & qis, QItemSelection const &)
189 QModelIndexList il = qis.indexes();
192 availableChanged(il.front(), QModelIndex());
196 void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModelIndex &)
201 selectedHasFocus_ = false;
206 void GuiSelectionManager::selectedChanged(QItemSelection const & qis, QItemSelection const &)
208 QModelIndexList il = qis.indexes();
211 selectedChanged(il.front(), QModelIndex());
215 void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelIndex &)
220 selectedHasFocus_ = true;
225 bool GuiSelectionManager::insertRowToSelected(int i,
226 QMap<int, QVariant> const & itemData)
228 if (i <= -1 || i > selectedModel->rowCount())
230 if (!selectedModel->insertRow(i))
232 return selectedModel->setItemData(selectedModel->index(i), itemData);
236 void GuiSelectionManager::addPB_clicked()
238 QModelIndexList selIdx =
239 availableLV->selectionModel()->selectedIndexes();
240 if (selIdx.isEmpty())
243 QModelIndex const idxToAdd = selIdx.first();
244 QModelIndex const idx = selectedLV->currentIndex();
245 int const srows = selectedModel->rowCount();
247 QMap<int, QVariant> qm = availableModel->itemData(idxToAdd);
248 insertRowToSelected(srows, qm);
250 selectionChanged(); //signal
253 selectedLV->setCurrentIndex(idx);
259 void GuiSelectionManager::deletePB_clicked()
261 QModelIndexList selIdx =
262 selectedLV->selectionModel()->selectedIndexes();
263 if (selIdx.isEmpty())
265 QModelIndex idx = selIdx.first();
266 selectedModel->removeRow(idx.row());
267 selectionChanged(); //signal
269 int nrows = selectedLV->model()->rowCount();
270 if (idx.row() == nrows) //was last item on list
271 idx = idx.sibling(idx.row() - 1, idx.column());
274 selectedLV->setCurrentIndex(idx);
276 selectedLV->setCurrentIndex(selectedLV->model()->index(0, 0));
277 selectedHasFocus_ = (nrows > 0);
282 void GuiSelectionManager::upPB_clicked()
284 QModelIndex idx = selectedLV->currentIndex();
286 int const pos = idx.row();
290 QMap<int, QVariant> qm = selectedModel->itemData(idx);
292 selectedModel->removeRow(pos);
293 insertRowToSelected(pos - 1, qm);
295 selectionChanged(); //signal
297 selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
298 selectedHasFocus_ = true;
303 void GuiSelectionManager::downPB_clicked()
305 QModelIndex idx = selectedLV->currentIndex();
307 int const pos = idx.row();
308 if (pos >= selectedModel->rowCount() - 1)
311 QMap<int, QVariant> qm = selectedModel->itemData(idx);
313 selectedModel->removeRow(pos);
314 insertRowToSelected(pos + 1, qm);
316 selectionChanged(); //signal
318 selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
319 selectedHasFocus_ = true;
324 void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
326 if (isSelected(idx) || !addPB->isEnabled())
330 selectedHasFocus_ = false;
332 //updateHook() will be emitted there
336 bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
338 QEvent::Type etype = event->type();
339 if (obj == availableLV) {
340 if (etype == QEvent::KeyPress) {
341 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
342 int const keyPressed = keyEvent->key();
343 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
344 // Enter key without modifier will add current item.
345 // Ctrl-Enter will add it and close the dialog.
346 // This is designed to work both with the main enter key
347 // and the one on the numeric keypad.
348 if (keyPressed == Qt::Key_Enter || keyPressed == Qt::Key_Return) {
351 else if (keyModifiers == Qt::ControlModifier ||
352 keyModifiers == Qt::KeypadModifier ||
353 keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
354 if (addPB->isEnabled()) {
362 } else if (etype == QEvent::FocusIn) {
363 if (selectedHasFocus_) {
364 selectedHasFocus_ = false;
370 } else if (obj == selectedLV) {
371 if (etype == QEvent::KeyPress) {
372 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
373 int const keyPressed = keyEvent->key();
374 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
375 // Delete or backspace key will delete current item
376 // ...with control modifier will clear the list
377 if (keyPressed == Qt::Key_Delete || keyPressed == Qt::Key_Backspace) {
378 if (keyModifiers == Qt::NoModifier && deletePB->isEnabled()) {
381 } else if (keyModifiers == Qt::ControlModifier) {
382 selectedModel->removeRows(0, selectedModel->rowCount());
385 return QObject::eventFilter(obj, event);
387 // Ctrl-Up activates upPB
388 else if (keyPressed == Qt::Key_Up) {
389 if (keyModifiers == Qt::ControlModifier) {
390 if (upPB->isEnabled())
396 // Ctrl-Down activates downPB
397 else if (keyPressed == Qt::Key_Down) {
398 if (keyModifiers == Qt::ControlModifier) {
399 if (downPB->isEnabled())
405 } else if (etype == QEvent::FocusIn) {
406 if (!selectedHasFocus_) {
407 selectedHasFocus_ = true;
414 return QObject::eventFilter(obj, event);
417 } // namespace frontend
420 #include "moc_GuiSelectionManager.cpp"