]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSelectionManager.cpp
19d0f78300a8670a796b2a9e1fae4eb6643664de
[lyx.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(doubleClicked(QModelIndex)), 
82                 this, SLOT(availableLV_doubleClicked(QModelIndex)));
83         
84         availableLV->installEventFilter(this);
85         selectedLV->installEventFilter(this);
86 }
87
88
89 void GuiSelectionManager::update()
90 {
91         updateAddPB();
92         updateDelPB();
93         updateDownPB();
94         updateUpPB();
95 }
96
97
98 QModelIndex GuiSelectionManager::getSelectedIndex() const
99 {
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();
104
105         if (selectedFocused()) { 
106                 if (have_sel)
107                         return sel.front();
108                 if (have_avl)
109                         return avail.front();
110         } 
111         else { // available has focus
112                 if (have_avl)
113                         return avail.front();
114                 if (have_sel)
115                         return sel.front();
116         }
117         return QModelIndex();
118 }
119
120
121 void GuiSelectionManager::updateAddPB()
122 {
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()));
129 }
130
131
132 void GuiSelectionManager::updateDelPB()
133 {
134         int const srows = selectedModel->rowCount();
135         if (srows == 0) {
136                 deletePB->setEnabled(false);
137                 return;
138         }
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);
143 }
144
145
146 void GuiSelectionManager::updateUpPB()
147 {
148         int const srows = selectedModel->rowCount();
149         if (srows == 0) {
150                 upPB->setEnabled(false);
151                 return;
152         }
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);
157 }
158
159
160 void GuiSelectionManager::updateDownPB()
161 {
162         int const srows = selectedModel->rowCount();
163         if (srows == 0) {
164                 downPB->setEnabled(false);
165                 return;
166         }
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);
171 }
172
173
174 bool GuiSelectionManager::isSelected(const QModelIndex & idx)
175 {
176         if (selectedModel->rowCount() == 0)
177                 return false;
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();
184 }
185
186
187 void GuiSelectionManager::availableChanged(QItemSelection const & qis, QItemSelection const &)
188 {
189         QModelIndexList il = qis.indexes();
190         if (il.empty()) 
191                 return;
192         availableChanged(il.front(), QModelIndex());
193 }
194
195
196 void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModelIndex &)
197 {
198         if (!idx.isValid())
199                 return;
200         
201         selectedHasFocus_ = false;
202         updateHook();
203 }
204
205
206 void GuiSelectionManager::selectedChanged(QItemSelection const & qis, QItemSelection const &)
207 {
208         QModelIndexList il = qis.indexes();
209         if (il.empty()) 
210                 return;
211         selectedChanged(il.front(), QModelIndex());
212 }
213
214
215 void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelIndex &)
216 {
217         if (!idx.isValid())
218                 return;
219         
220         selectedHasFocus_ = true;
221         updateHook();
222 }
223
224
225 bool GuiSelectionManager::insertRowToSelected(int i, 
226                 QMap<int, QVariant> const & itemData)
227 {
228         if (i <= -1 || i > selectedModel->rowCount())
229                 return false;
230         if (!selectedModel->insertRow(i))
231                 return false;
232         return selectedModel->setItemData(selectedModel->index(i), itemData);
233 }
234
235
236 void GuiSelectionManager::addPB_clicked()
237 {
238         QModelIndexList selIdx =
239                 availableLV->selectionModel()->selectedIndexes();
240         if (selIdx.isEmpty())
241                 return;
242
243         QModelIndex const idxToAdd = selIdx.first();
244         QModelIndex const idx = selectedLV->currentIndex();
245         int const srows = selectedModel->rowCount();
246         
247         QMap<int, QVariant> qm = availableModel->itemData(idxToAdd);
248         insertRowToSelected(srows, qm);
249         
250         selectionChanged(); //signal
251
252         if (idx.isValid())
253                 selectedLV->setCurrentIndex(idx);
254         
255         updateHook();
256 }
257
258
259 void GuiSelectionManager::deletePB_clicked()
260 {
261         QModelIndexList selIdx =
262                 selectedLV->selectionModel()->selectedIndexes();
263         if (selIdx.isEmpty())
264                 return;
265         QModelIndex idx = selIdx.first();
266         selectedModel->removeRow(idx.row());
267         selectionChanged(); //signal
268         
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());
272         
273         if (nrows > 1)
274                 selectedLV->setCurrentIndex(idx);
275         else if (nrows == 1)
276                 selectedLV->setCurrentIndex(selectedLV->model()->index(0, 0));
277         selectedHasFocus_ = (nrows > 0);
278         updateHook();
279 }
280
281
282 void GuiSelectionManager::upPB_clicked()
283 {
284         QModelIndex idx = selectedLV->currentIndex();
285
286         int const pos = idx.row();
287         if (pos <= 0)
288                 return;
289         
290         QMap<int, QVariant> qm = selectedModel->itemData(idx);
291
292         selectedModel->removeRow(pos);
293         insertRowToSelected(pos - 1, qm);
294
295         selectionChanged(); //signal
296
297         selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
298         selectedHasFocus_ = true;
299         updateHook();
300 }
301
302
303 void GuiSelectionManager::downPB_clicked()
304 {
305         QModelIndex idx = selectedLV->currentIndex();
306
307         int const pos = idx.row();
308         if (pos >= selectedModel->rowCount() - 1)
309                 return;
310
311         QMap<int, QVariant> qm = selectedModel->itemData(idx);
312
313         selectedModel->removeRow(pos);
314         insertRowToSelected(pos + 1, qm);
315
316         selectionChanged(); //signal
317         
318         selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
319         selectedHasFocus_ = true;
320         updateHook();
321 }
322
323
324 void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
325 {
326         if (isSelected(idx) || !addPB->isEnabled())
327                 return;
328         
329         if (idx.isValid())
330                 selectedHasFocus_ = false;
331         addPB_clicked();
332         //updateHook() will be emitted there
333 }
334
335
336 bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event) 
337 {
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) {
349                                 if (!keyModifiers)
350                                         addPB_clicked();
351                                 else if (keyModifiers == Qt::ControlModifier ||
352                                                 keyModifiers == Qt::KeypadModifier  ||
353                                                 keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
354                                         if (addPB->isEnabled()) {
355                                                 addPB_clicked();
356                                                 okHook(); //signal
357                                         }
358                                 }
359                                 event->accept();
360                                 return true;
361                         }
362                 } else if (etype == QEvent::FocusIn) {
363                         if (selectedHasFocus_) {
364                                 selectedHasFocus_ = false;
365                                 updateHook();
366                         }
367                         event->accept();
368                         return true;
369                 } 
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()) {
379                                         deletePB_clicked();
380                                         updateHook();
381                                 } else if (keyModifiers == Qt::ControlModifier) {
382                                         selectedModel->removeRows(0, selectedModel->rowCount());
383                                         updateHook();
384                                 } else
385                                         return QObject::eventFilter(obj, event);
386                         } 
387                         // Ctrl-Up activates upPB
388                         else if (keyPressed == Qt::Key_Up) {
389                                 if (keyModifiers == Qt::ControlModifier) {
390                                         if (upPB->isEnabled())
391                                                 upPB_clicked();
392                                         event->accept();
393                                         return true;
394                                 }
395                         } 
396                         // Ctrl-Down activates downPB
397                         else if (keyPressed == Qt::Key_Down) {
398                                 if (keyModifiers == Qt::ControlModifier) {
399                                         if (downPB->isEnabled())
400                                                 downPB_clicked();
401                                         event->accept();
402                                         return true;
403                                 }
404                         }
405                 } else if (etype == QEvent::FocusIn) {
406                         if (!selectedHasFocus_) {
407                                 selectedHasFocus_ = true;
408                                 updateHook();
409                         }
410                         event->accept();
411                         return true;
412                 }
413         }
414         return QObject::eventFilter(obj, event);
415 }
416
417 } // namespace frontend
418 } // namespace lyx
419
420 #include "moc_GuiSelectionManager.cpp"