]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiSelectionManager.cpp
Fix bug #6486. Patch inspired by work by John McCabe-Dansted.
[features.git] / src / frontends / qt4 / GuiSelectionManager.cpp
1 /**
2  * \file GuiSelectionManager.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Richard Heck
7  * \author Et Alia
8  *
9  * Some of the material in this file previously appeared in 
10  * GuiCitationDialog.cpp.
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "GuiSelectionManager.h"
18
19 #include "support/debug.h"
20
21 #include <QAbstractListModel>
22 #include <QItemSelection>
23 #include <QListView>
24 #include <QKeyEvent>
25 #include <QPushButton>
26
27 #ifdef KeyPress
28 #undef KeyPress
29 #endif
30
31 #ifdef ControlModifier
32 #undef ControlModifier
33 #endif
34
35
36 namespace lyx {
37 namespace frontend {
38
39 GuiSelectionManager::GuiSelectionManager(
40         QAbstractItemView * avail,
41         QListView * sel,
42         QPushButton * add, 
43         QPushButton * del, 
44         QPushButton * up, 
45         QPushButton * down,
46         QAbstractListModel * amod,
47         QAbstractListModel * smod)
48 {
49         availableLV = avail;
50         selectedLV = sel;
51         addPB = add;
52         deletePB = del;
53         upPB = up;
54         downPB = down;
55         availableModel = amod;
56         selectedModel = smod;
57         
58         selectedLV->setModel(smod);
59         availableLV->setModel(amod);
60         
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(currentChanged(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(clicked(QModelIndex)), 
82                 this, SLOT(availableLV_clicked(QModelIndex)));
83         connect(availableLV, SIGNAL(doubleClicked(QModelIndex)), 
84                 this, SLOT(availableLV_doubleClicked(QModelIndex)));
85         connect(selectedLV, SIGNAL(clicked(QModelIndex)), 
86                 this, SLOT(selectedLV_clicked(QModelIndex)));
87         
88         availableLV->installEventFilter(this);
89         selectedLV->installEventFilter(this);
90 }
91
92
93 void GuiSelectionManager::update()
94 {
95         updateAddPB();
96         updateDelPB();
97         updateDownPB();
98         updateUpPB();
99 }
100
101
102 QModelIndex GuiSelectionManager::getSelectedIndex() const
103 {
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();
108
109         if (selectedFocused()) { 
110                 if (have_sel)
111                         return sel.front();
112                 if (have_avl)
113                         return avail.front();
114         } 
115         else { // available has focus
116                 if (have_avl)
117                         return avail.front();
118                 if (have_sel)
119                         return sel.front();
120         }
121         return QModelIndex();
122 }
123
124
125 void GuiSelectionManager::updateAddPB()
126 {
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()));
133 }
134
135
136 void GuiSelectionManager::updateDelPB()
137 {
138         int const srows = selectedModel->rowCount();
139         if (srows == 0) {
140                 deletePB->setEnabled(false);
141                 return;
142         }
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);
147 }
148
149
150 void GuiSelectionManager::updateUpPB()
151 {
152         int const srows = selectedModel->rowCount();
153         if (srows == 0) {
154                 upPB->setEnabled(false);
155                 return;
156         }
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);
161 }
162
163
164 void GuiSelectionManager::updateDownPB()
165 {
166         int const srows = selectedModel->rowCount();
167         if (srows == 0) {
168                 downPB->setEnabled(false);
169                 return;
170         }
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);
175 }
176
177
178 bool GuiSelectionManager::isSelected(const QModelIndex & idx)
179 {
180         if (selectedModel->rowCount() == 0)
181                 return false;
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();
188 }
189
190
191 void GuiSelectionManager::availableChanged(QItemSelection const & qis, QItemSelection const &)
192 {
193         QModelIndexList il = qis.indexes();
194         if (il.empty()) 
195                 return;
196         availableChanged(il.front(), QModelIndex());
197 }
198
199
200 void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModelIndex &)
201 {
202         if (!idx.isValid())
203                 return;
204         
205         selectedHasFocus_ = false;
206         updateHook();
207 }
208
209
210 void GuiSelectionManager::selectedChanged(QItemSelection const & qis, QItemSelection const &)
211 {
212         QModelIndexList il = qis.indexes();
213         if (il.empty()) 
214                 return;
215         selectedChanged(il.front(), QModelIndex());
216 }
217
218
219 void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelIndex &)
220 {
221         if (!idx.isValid())
222                 return;
223         
224         selectedHasFocus_ = true;
225         updateHook();
226 }
227
228
229 bool GuiSelectionManager::insertRowToSelected(int i, 
230                 QMap<int, QVariant> const & itemData)
231 {
232         if (i <= -1 || i > selectedModel->rowCount())
233                 return false;
234         if (!selectedModel->insertRow(i))
235                 return false;
236         return selectedModel->setItemData(selectedModel->index(i), itemData);
237 }
238
239
240 void GuiSelectionManager::addPB_clicked()
241 {
242         QModelIndexList selIdx =
243                 availableLV->selectionModel()->selectedIndexes();
244         if (selIdx.isEmpty())
245                 return;
246
247         QModelIndex const idxToAdd = selIdx.first();
248         QModelIndex const idx = selectedLV->currentIndex();
249         int const srows = selectedModel->rowCount();
250         
251         QMap<int, QVariant> qm = availableModel->itemData(idxToAdd);
252         insertRowToSelected(srows, qm);
253         
254         selectionChanged(); //signal
255
256         if (idx.isValid())
257                 selectedLV->setCurrentIndex(idx);
258         
259         updateHook();
260 }
261
262
263 void GuiSelectionManager::deletePB_clicked()
264 {
265         QModelIndexList selIdx =
266                 selectedLV->selectionModel()->selectedIndexes();
267         if (selIdx.isEmpty())
268                 return;
269         QModelIndex idx = selIdx.first();
270         selectedModel->removeRow(idx.row());
271         selectionChanged(); //signal
272         
273         int nrows = selectedLV->model()->rowCount();
274         if (idx.row() == nrows) //was last item on list
275                 idx = idx.sibling(idx.row() - 1, idx.column());
276         
277         if (nrows > 1)
278                 selectedLV->setCurrentIndex(idx);
279         else if (nrows == 1)
280                 selectedLV->setCurrentIndex(selectedLV->model()->index(0, 0));
281         selectedHasFocus_ = (nrows > 0);
282         updateHook();
283 }
284
285
286 void GuiSelectionManager::upPB_clicked()
287 {
288         QModelIndex idx = selectedLV->currentIndex();
289
290         int const pos = idx.row();
291         if (pos <= 0)
292                 return;
293         
294         QMap<int, QVariant> qm = selectedModel->itemData(idx);
295
296         selectedModel->removeRow(pos);
297         insertRowToSelected(pos - 1, qm);
298
299         selectionChanged(); //signal
300
301         selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
302         selectedHasFocus_ = true;
303         updateHook();
304 }
305
306
307 void GuiSelectionManager::downPB_clicked()
308 {
309         QModelIndex idx = selectedLV->currentIndex();
310
311         int const pos = idx.row();
312         if (pos >= selectedModel->rowCount() - 1)
313                 return;
314
315         QMap<int, QVariant> qm = selectedModel->itemData(idx);
316
317         selectedModel->removeRow(pos);
318         insertRowToSelected(pos + 1, qm);
319
320         selectionChanged(); //signal
321         
322         selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
323         selectedHasFocus_ = true;
324         updateHook();
325 }
326
327
328 // FIXME These slots do not really do what they need to do, since focus
329 // can enter the QListView in other ways. But there are no signals sent
330 // in that case. We need to reimplement focusInEvent() to capture those,
331 // which means subclassing QListView. (rgh)
332 // Or by installing an event listener.. (andre)
333 void GuiSelectionManager::availableLV_clicked(const QModelIndex &)
334 {
335         selectedHasFocus_ = false;
336         updateHook();
337 }
338
339
340 void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
341 {
342         if (isSelected(idx) || !addPB->isEnabled())
343                 return;
344         
345         if (idx.isValid())
346                 selectedHasFocus_ = false;
347         addPB_clicked();
348         //updateHook() will be emitted there
349 }
350
351
352 void GuiSelectionManager::selectedLV_clicked(const QModelIndex &)
353 {
354         selectedHasFocus_ = true;
355         updateHook();
356 }
357
358
359 bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event) 
360 {
361         if (obj == availableLV) {
362                 if (event->type() != QEvent::KeyPress)
363                         return QObject::eventFilter(obj, event);
364                 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
365                 int const keyPressed = keyEvent->key();
366                 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
367                 // Enter key without modifier will add current item.
368                 // Ctrl-Enter will add it and close the dialog.
369                 // This is designed to work both with the main enter key
370                 // and the one on the numeric keypad.
371                 if (keyPressed == Qt::Key_Enter || keyPressed == Qt::Key_Return) {
372                         if (!keyModifiers)
373                                 addPB_clicked();
374                         else if (keyModifiers == Qt::ControlModifier ||
375                                         keyModifiers == Qt::KeypadModifier  ||
376                                         keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
377                                 if (addPB->isEnabled()) {
378                                         addPB_clicked();
379                                         okHook(); //signal
380                                 }
381                         }
382                         event->accept();
383                         return true;
384                 }
385         } else if (obj == selectedLV) {
386                 if (event->type() != QEvent::KeyPress)
387                         return QObject::eventFilter(obj, event);
388                 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
389                 int const keyPressed = keyEvent->key();
390                 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
391                 // Delete or backspace key will delete current item
392                 // ...with control modifier will clear the list
393                 if (keyPressed == Qt::Key_Delete || keyPressed == Qt::Key_Backspace) {
394                         if (keyModifiers == Qt::NoModifier && deletePB->isEnabled()) {
395                                 deletePB_clicked();
396                                 updateHook();
397                         } else if (keyModifiers == Qt::ControlModifier) {
398                                 selectedModel->removeRows(0, selectedModel->rowCount());
399                                 updateHook();
400                         } else
401                                 return QObject::eventFilter(obj, event);
402                 } 
403                 // Ctrl-Up activates upPB
404                 else if (keyPressed == Qt::Key_Up) {
405                         if (keyModifiers == Qt::ControlModifier) {
406                                 if (upPB->isEnabled())
407                                         upPB_clicked();
408                                 event->accept();
409                                 return true;
410                         }
411                 } 
412                 // Ctrl-Down activates downPB
413                 else if (keyPressed == Qt::Key_Down) {
414                         if (keyModifiers == Qt::ControlModifier) {
415                                 if (downPB->isEnabled())
416                                         downPB_clicked();
417                                 event->accept();
418                                 return true;
419                         }
420                 }
421         }
422         return QObject::eventFilter(obj, event);
423 }
424
425 } // namespace frontend
426 } // namespace lyx
427
428 #include "moc_GuiSelectionManager.cpp"