]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSelectionManager.cpp
88946f501ec7a76eea4ecf31d1ab71971d2c45e1
[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 #include "GuiSelectionManager.h"
17
18
19 namespace lyx {
20 namespace frontend {
21
22 GuiSelectionManager::GuiSelectionManager(
23         QListView * avail, 
24         QListView * sel,
25         QPushButton * add, 
26         QPushButton * del, 
27         QPushButton * up, 
28         QPushButton * down,
29         QStringListModel * amod,
30         QStringListModel * smod)
31         {
32         availableLV = avail;
33         selectedLV = sel;
34         addPB = add;
35         deletePB = del;
36         upPB = up;
37         downPB = down;
38         availableModel = amod;
39         selectedModel = smod;
40         
41         selectedLV->setModel(smod);
42         availableLV->setModel(amod);
43         
44         connect(availableLV->selectionModel(),
45                 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
46                 this, SLOT(availableChanged(const QModelIndex &, const QModelIndex &)));
47         connect(selectedLV->selectionModel(),
48                 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
49                 this, SLOT(selectedChanged(const QModelIndex &, const QModelIndex &)));
50         connect(addPB, SIGNAL(clicked()), 
51                 this, SLOT(addPB_clicked()));
52         connect(deletePB, SIGNAL(clicked()), 
53                 this, SLOT(deletePB_clicked()));
54         connect(upPB, SIGNAL(clicked()), 
55                 this, SLOT(upPB_clicked()));
56         connect(downPB, SIGNAL(clicked()), 
57                 this, SLOT(downPB_clicked()));
58         connect(availableLV, SIGNAL(clicked(const QModelIndex &)), 
59                 this, SLOT(availableLV_clicked(const QModelIndex &)));
60         connect(availableLV, SIGNAL(doubleClicked(const QModelIndex &)), 
61                 this, SLOT(availableLV_doubleClicked(const QModelIndex &)));
62         connect(selectedLV, SIGNAL(clicked(const QModelIndex &)), 
63                 this, SLOT(selectedLV_clicked(const QModelIndex &)));
64         
65         availableLV->installEventFilter(this);
66         selectedLV->installEventFilter(this);
67 }
68
69
70 void GuiSelectionManager::update()
71 {
72         updateAddPB();
73         updateDelPB();
74         updateDownPB();
75         updateUpPB();
76 }
77
78
79 void GuiSelectionManager::updateAddPB()
80 {
81         int const arows = availableLV->model()->rowCount();
82         QModelIndexList const availSels = 
83                 availableLV->selectionModel()->selectedIndexes();
84         addPB->setEnabled(arows > 0 &&
85                 !availSels.isEmpty() &&
86                 !isSelected(availSels.first()));
87 }
88
89
90 void GuiSelectionManager::updateDelPB()
91 {
92         int const srows = selectedLV->model()->rowCount();
93         QModelIndexList const selSels = 
94                 selectedLV->selectionModel()->selectedIndexes();
95         int const sel_nr =      selSels.empty() ? -1 : selSels.first().row();
96         deletePB->setEnabled(sel_nr >= 0);
97 }
98
99
100 void GuiSelectionManager::updateDownPB()
101 {
102         int const srows = selectedLV->model()->rowCount();
103         QModelIndexList const selSels = 
104                         selectedLV->selectionModel()->selectedIndexes();
105         int const sel_nr =      selSels.empty() ? -1 : selSels.first().row();
106         upPB->setEnabled(sel_nr > 0);
107 }
108
109
110 void GuiSelectionManager::updateUpPB()
111 {
112         int const srows = selectedLV->model()->rowCount();
113         QModelIndexList const selSels = 
114                         selectedLV->selectionModel()->selectedIndexes();
115         int const sel_nr =      selSels.empty() ? -1 : selSels.first().row();
116         downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
117 }
118
119 bool GuiSelectionManager::isSelected(const QModelIndex & idx)
120 {
121         QString const str = idx.data().toString();
122         return selectedModel->stringList().contains(str);
123 }
124
125
126 void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModelIndex &)
127 {
128         if (!idx.isValid())
129                 return;
130         
131         selectedHasFocus_ = false;
132         updateHook();
133 }
134
135
136 void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelIndex &)
137 {
138         if (!idx.isValid())
139                 return;
140         
141         selectedHasFocus_ = true;
142         updateHook();
143 }
144
145
146 static QModelIndex getSelectedIndex(QListView * lv)
147 {
148         QModelIndex retval = QModelIndex();
149         QModelIndexList selIdx = 
150                         lv->selectionModel()->selectedIndexes();
151         if (!selIdx.empty())
152                 retval = selIdx.first();
153         return retval;
154 }
155
156
157 void GuiSelectionManager::addPB_clicked()
158 {
159         QModelIndex const idxToAdd = getSelectedIndex(availableLV);
160         if (!idxToAdd.isValid())
161                 return;
162         QModelIndex idx = selectedLV->currentIndex();
163         
164         QStringList keys = selectedModel->stringList();
165         keys.append(idxToAdd.data().toString());
166         selectedModel->setStringList(keys);
167         selectionChanged(); //signal
168         
169         if (idx.isValid())
170                 selectedLV->setCurrentIndex(idx);
171         updateHook();
172 }
173
174
175 void GuiSelectionManager::deletePB_clicked()
176 {
177         QModelIndex idx = getSelectedIndex(selectedLV);
178         if (!idx.isValid())
179                 return;
180         
181         QStringList keys = selectedModel->stringList();
182         keys.removeAt(idx.row());
183         selectedModel->setStringList(keys);
184         selectionChanged(); //signal
185         
186         int nrows = selectedLV->model()->rowCount();
187         if (idx.row() == nrows) //was last item on list
188                 idx = idx.sibling(idx.row() - 1, idx.column());
189         
190         if (nrows > 1)
191                 selectedLV->setCurrentIndex(idx);
192         else if (nrows == 1)
193                 selectedLV->setCurrentIndex(selectedLV->model()->index(0,0));
194         selectedHasFocus_ = (nrows > 0);
195         updateHook();
196 }
197
198
199 void GuiSelectionManager::upPB_clicked()
200 {
201         QModelIndex idx = selectedLV->currentIndex();
202         
203         int const pos = idx.row();
204         QStringList keys = selectedModel->stringList();
205         keys.swap(pos, pos - 1);
206         selectedModel->setStringList(keys);
207         selectionChanged(); //signal
208         
209         selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
210         selectedHasFocus_ = true;
211         updateHook();
212 }
213
214
215 void GuiSelectionManager::downPB_clicked()
216 {
217         QModelIndex idx = selectedLV->currentIndex();
218         
219         int const pos = idx.row();
220         QStringList keys = selectedModel->stringList();
221         keys.swap(pos, pos + 1);
222         selectedModel->setStringList(keys);
223         selectionChanged(); //signal
224         
225         selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
226         selectedHasFocus_ = true;
227         updateHook();
228 }
229
230
231 //FIXME These slots do not really do what they need to do, since focus
232 //can enter the QListView in other ways. But there are no signals sent
233 //in that case. We need to reimplement focusInEvent() to capture those,
234 //which means subclassing QListView. (rgh)
235 void GuiSelectionManager::availableLV_clicked(const QModelIndex &)
236 {
237         selectedHasFocus_ = false;
238         updateHook();
239 }
240
241
242 void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
243 {
244         if (isSelected(idx) || !addPB->isEnabled())
245                 return;
246         
247         if (idx.isValid())
248                 selectedHasFocus_ = false;
249         addPB_clicked();
250         //updateHook() will be emitted there
251 }
252
253
254 void GuiSelectionManager::selectedLV_clicked(const QModelIndex &)
255 {
256         selectedHasFocus_ = true;
257         updateHook();
258 }
259
260
261 bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event) 
262 {
263         if (obj == availableLV) {
264                 if (event->type() != QEvent::KeyPress)
265                         return QObject::eventFilter(obj, event);
266                 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
267                 int const keyPressed = keyEvent->key();
268                 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
269                 //Enter key without modifier will add current item.
270                 //Ctrl-Enter will add it and close the dialog.
271                 //This is designed to work both with the main enter key
272                 //and the one on the numeric keypad.
273                 if ((keyPressed == Qt::Key_Enter || keyPressed == Qt::Key_Return) &&
274                                 //We want one or both of Control and Keypad, and nothing else
275                                 //(KeypadModifier is what you get if you use the Enter key on the
276                                 //numeric keypad.)
277                                         (!keyModifiers || 
278                                         (keyModifiers == Qt::ControlModifier) ||
279                                         (keyModifiers == Qt::KeypadModifier)  ||
280                                         (keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier))
281                                         )
282                         ) {
283                         if (addPB->isEnabled()) {
284                                 addPB_clicked();
285                                 okHook(); //signal
286                         }
287                         event->accept();
288                         return true;
289                         } 
290         } else if (obj == selectedLV) {
291                 //Delete or backspace key will delete current item
292                 //...with control modifier will clear the list
293                 if (event->type() != QEvent::KeyPress)
294                         return QObject::eventFilter(obj, event);
295                 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
296                 int const keyPressed = keyEvent->key();
297                 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
298                 if (keyPressed == Qt::Key_Delete || keyPressed == Qt::Key_Backspace) {
299                         if (keyModifiers == Qt::NoModifier && deletePB->isEnabled())
300                                 deletePB_clicked();
301                         else if (keyModifiers == Qt::ControlModifier) {
302                                 QStringList list = selectedModel->stringList();
303                                 list.clear();
304                                 selectedModel->setStringList(list);
305                                 updateHook();
306                         } else
307                                 //ignore it otherwise
308                                 return QObject::eventFilter(obj, event);
309                                 event->accept();
310                                 return true;
311                 }
312         }
313         return QObject::eventFilter(obj, event);
314 }
315
316 } // namespace frontend
317 } // namespace lyx
318
319 #include "GuiSelectionManager_moc.cpp"