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.
16 #include "GuiSelectionManager.h"
22 GuiSelectionManager::GuiSelectionManager(
29 QStringListModel * amod,
30 QStringListModel * smod)
38 availableModel = amod;
41 selectedLV->setModel(smod);
42 availableLV->setModel(amod);
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 &)));
65 availableLV->installEventFilter(this);
66 selectedLV->installEventFilter(this);
70 void GuiSelectionManager::update()
79 void GuiSelectionManager::updateAddPB()
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()));
90 void GuiSelectionManager::updateDelPB()
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);
100 void GuiSelectionManager::updateDownPB()
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);
110 void GuiSelectionManager::updateUpPB()
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);
119 bool GuiSelectionManager::isSelected(const QModelIndex & idx)
121 QString const str = idx.data().toString();
122 return selectedModel->stringList().contains(str);
126 void GuiSelectionManager::availableChanged(const QModelIndex & idx, const QModelIndex &)
131 selectedHasFocus_ = false;
136 void GuiSelectionManager::selectedChanged(const QModelIndex & idx, const QModelIndex &)
141 selectedHasFocus_ = true;
146 static QModelIndex getSelectedIndex(QListView * lv)
148 QModelIndex retval = QModelIndex();
149 QModelIndexList selIdx =
150 lv->selectionModel()->selectedIndexes();
152 retval = selIdx.first();
157 void GuiSelectionManager::addPB_clicked()
159 QModelIndex const idxToAdd = getSelectedIndex(availableLV);
160 if (!idxToAdd.isValid())
162 QModelIndex idx = selectedLV->currentIndex();
164 QStringList keys = selectedModel->stringList();
165 keys.append(idxToAdd.data().toString());
166 selectedModel->setStringList(keys);
167 selectionChanged(); //signal
170 selectedLV->setCurrentIndex(idx);
175 void GuiSelectionManager::deletePB_clicked()
177 QModelIndex idx = getSelectedIndex(selectedLV);
181 QStringList keys = selectedModel->stringList();
182 keys.removeAt(idx.row());
183 selectedModel->setStringList(keys);
184 selectionChanged(); //signal
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());
191 selectedLV->setCurrentIndex(idx);
193 selectedLV->setCurrentIndex(selectedLV->model()->index(0,0));
194 selectedHasFocus_ = (nrows > 0);
199 void GuiSelectionManager::upPB_clicked()
201 QModelIndex idx = selectedLV->currentIndex();
203 int const pos = idx.row();
204 QStringList keys = selectedModel->stringList();
205 keys.swap(pos, pos - 1);
206 selectedModel->setStringList(keys);
207 selectionChanged(); //signal
209 selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
210 selectedHasFocus_ = true;
215 void GuiSelectionManager::downPB_clicked()
217 QModelIndex idx = selectedLV->currentIndex();
219 int const pos = idx.row();
220 QStringList keys = selectedModel->stringList();
221 keys.swap(pos, pos + 1);
222 selectedModel->setStringList(keys);
223 selectionChanged(); //signal
225 selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
226 selectedHasFocus_ = true;
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 &)
237 selectedHasFocus_ = false;
242 void GuiSelectionManager::availableLV_doubleClicked(const QModelIndex & idx)
244 if (isSelected(idx) || !addPB->isEnabled())
248 selectedHasFocus_ = false;
250 //updateHook() will be emitted there
254 void GuiSelectionManager::selectedLV_clicked(const QModelIndex &)
256 selectedHasFocus_ = true;
261 bool GuiSelectionManager::eventFilter(QObject * obj, QEvent * event)
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
278 (keyModifiers == Qt::ControlModifier) ||
279 (keyModifiers == Qt::KeypadModifier) ||
280 (keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier))
283 if (addPB->isEnabled()) {
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())
301 else if (keyModifiers == Qt::ControlModifier) {
302 QStringList list = selectedModel->stringList();
304 selectedModel->setStringList(list);
307 //ignore it otherwise
308 return QObject::eventFilter(obj, event);
313 return QObject::eventFilter(obj, event);
316 } // namespace frontend
319 #include "GuiSelectionManager_moc.cpp"