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"
23 #include <QPushButton>
24 #include <QAbstractListModel>
30 #ifdef ControlModifier
31 #undef ControlModifier
38 GuiSelectionManager::GuiSelectionManager(
39 QAbstractItemView * avail,
45 QAbstractListModel * amod,
46 QAbstractListModel * smod)
54 availableModel = amod;
57 selectedLV->setModel(smod);
58 availableLV->setModel(amod);
60 connect(availableLV->selectionModel(),
61 SIGNAL(currentChanged(QModelIndex,QModelIndex)),
62 this, SLOT(availableChanged(QModelIndex, QModelIndex)));
63 connect(selectedLV->selectionModel(),
64 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
65 this, SLOT(selectedChanged(QModelIndex, QModelIndex)));
66 connect(addPB, SIGNAL(clicked()),
67 this, SLOT(addPB_clicked()));
68 connect(deletePB, SIGNAL(clicked()),
69 this, SLOT(deletePB_clicked()));
70 connect(upPB, SIGNAL(clicked()),
71 this, SLOT(upPB_clicked()));
72 connect(downPB, SIGNAL(clicked()),
73 this, SLOT(downPB_clicked()));
74 connect(availableLV, SIGNAL(clicked(QModelIndex)),
75 this, SLOT(availableLV_clicked(QModelIndex)));
76 connect(availableLV, SIGNAL(doubleClicked(QModelIndex)),
77 this, SLOT(availableLV_doubleClicked(QModelIndex)));
78 connect(selectedLV, SIGNAL(clicked(QModelIndex)),
79 this, SLOT(selectedLV_clicked(QModelIndex)));
81 availableLV->installEventFilter(this);
82 selectedLV->installEventFilter(this);
86 void GuiSelectionManager::update()
95 void GuiSelectionManager::updateAddPB()
97 int const arows = availableModel->rowCount();
98 QModelIndexList const availSels =
99 availableLV->selectionModel()->selectedIndexes();
100 addPB->setEnabled(arows > 0 &&
101 !availSels.isEmpty() &&
102 !isSelected(availSels.first()));
106 void GuiSelectionManager::updateDelPB()
108 int const srows = selectedModel->rowCount();
110 deletePB->setEnabled(false);
113 QModelIndexList const selSels =
114 selectedLV->selectionModel()->selectedIndexes();
115 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
116 deletePB->setEnabled(sel_nr >= 0);
120 void GuiSelectionManager::updateUpPB()
122 int const srows = selectedModel->rowCount();
124 upPB->setEnabled(false);
127 QModelIndexList const selSels =
128 selectedLV->selectionModel()->selectedIndexes();
129 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
130 upPB->setEnabled(sel_nr > 0);
134 void GuiSelectionManager::updateDownPB()
136 int const srows = selectedModel->rowCount();
138 downPB->setEnabled(false);
141 QModelIndexList const selSels =
142 selectedLV->selectionModel()->selectedIndexes();
143 int const sel_nr = selSels.empty() ? -1 : selSels.first().row();
144 downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
148 bool GuiSelectionManager::isSelected(const QModelIndex & idx)
150 if (selectedModel->rowCount() == 0)
152 QVariant const & str = availableModel->data(idx, Qt::DisplayRole);
153 QModelIndexList qmil =
154 selectedModel->match(selectedModel->index(0),
155 Qt::DisplayRole, str, 1,
156 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap));
157 return !qmil.empty();
161 void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModelIndex &)
166 selectedHasFocus_ = false;
171 void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelIndex &)
176 selectedHasFocus_ = true;
181 bool GuiSelectionManager::insertRowToSelected(int i,
182 QMap<int, QVariant> const & itemData)
184 if (i <= -1 || i > selectedModel->rowCount())
186 if (!selectedModel->insertRow(i))
188 return selectedModel->setItemData(selectedModel->index(i), itemData);
192 void GuiSelectionManager::addPB_clicked()
194 QModelIndexList selIdx =
195 availableLV->selectionModel()->selectedIndexes();
196 if (selIdx.isEmpty())
199 QModelIndex const idxToAdd = selIdx.first();
200 QModelIndex const idx = selectedLV->currentIndex();
201 int const srows = selectedModel->rowCount();
203 QMap<int, QVariant> qm = availableModel->itemData(idxToAdd);
204 insertRowToSelected(srows, qm);
206 selectionChanged(); //signal
209 selectedLV->setCurrentIndex(idx);
215 void GuiSelectionManager::deletePB_clicked()
217 QModelIndexList selIdx =
218 selectedLV->selectionModel()->selectedIndexes();
219 if (selIdx.isEmpty())
221 QModelIndex idx = selIdx.first();
222 selectedModel->removeRow(idx.row());
223 selectionChanged(); //signal
225 int nrows = selectedLV->model()->rowCount();
226 if (idx.row() == nrows) //was last item on list
227 idx = idx.sibling(idx.row() - 1, idx.column());
230 selectedLV->setCurrentIndex(idx);
232 selectedLV->setCurrentIndex(selectedLV->model()->index(0, 0));
233 selectedHasFocus_ = (nrows > 0);
238 void GuiSelectionManager::upPB_clicked()
240 QModelIndex idx = selectedLV->currentIndex();
242 int const pos = idx.row();
246 QMap<int, QVariant> qm = selectedModel->itemData(idx);
248 selectedModel->removeRow(pos);
249 insertRowToSelected(pos - 1, qm);
251 selectionChanged(); //signal
253 selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
254 selectedHasFocus_ = true;
259 void GuiSelectionManager::downPB_clicked()
261 QModelIndex idx = selectedLV->currentIndex();
263 int const pos = idx.row();
264 if (pos >= selectedModel->rowCount() - 1)
267 QMap<int, QVariant> qm = selectedModel->itemData(idx);
269 selectedModel->removeRow(pos);
270 insertRowToSelected(pos + 1, qm);
272 selectionChanged(); //signal
274 selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
275 selectedHasFocus_ = true;
280 // FIXME These slots do not really do what they need to do, since focus
281 // can enter the QListView in other ways. But there are no signals sent
282 // in that case. We need to reimplement focusInEvent() to capture those,
283 // which means subclassing QListView. (rgh)
284 // Or by installing an event listener.. (andre)
285 void GuiSelectionManager::availableLV_clicked(const QModelIndex &)
287 selectedHasFocus_ = false;
292 void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
294 if (isSelected(idx) || !addPB->isEnabled())
298 selectedHasFocus_ = false;
300 //updateHook() will be emitted there
304 void GuiSelectionManager::selectedLV_clicked(const QModelIndex &)
306 selectedHasFocus_ = true;
311 bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
313 if (obj == availableLV) {
314 if (event->type() != QEvent::KeyPress)
315 return QObject::eventFilter(obj, event);
316 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
317 int const keyPressed = keyEvent->key();
318 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
319 // Enter key without modifier will add current item.
320 // Ctrl-Enter will add it and close the dialog.
321 // This is designed to work both with the main enter key
322 // and the one on the numeric keypad.
323 if (keyPressed == Qt::Key_Enter || keyPressed == Qt::Key_Return) {
326 else if (keyModifiers == Qt::ControlModifier ||
327 keyModifiers == Qt::KeypadModifier ||
328 keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
329 if (addPB->isEnabled()) {
337 } else if (obj == selectedLV) {
338 if (event->type() != QEvent::KeyPress)
339 return QObject::eventFilter(obj, event);
340 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
341 int const keyPressed = keyEvent->key();
342 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
343 // Delete or backspace key will delete current item
344 // ...with control modifier will clear the list
345 if (keyPressed == Qt::Key_Delete || keyPressed == Qt::Key_Backspace) {
346 if (keyModifiers == Qt::NoModifier && deletePB->isEnabled()) {
349 } else if (keyModifiers == Qt::ControlModifier) {
350 selectedModel->removeRows(0, selectedModel->rowCount());
353 return QObject::eventFilter(obj, event);
355 // Ctrl-Up activates upPB
356 else if (keyPressed == Qt::Key_Up) {
357 if (keyModifiers == Qt::ControlModifier) {
358 if (upPB->isEnabled())
364 // Ctrl-Down activates downPB
365 else if (keyPressed == Qt::Key_Down) {
366 if (keyModifiers == Qt::ControlModifier) {
367 if (downPB->isEnabled())
374 return QObject::eventFilter(obj, event);
377 } // namespace frontend
380 #include "moc_GuiSelectionManager.cpp"