Line |
Branch |
Exec |
Source |
1 |
|
|
// Copyright (c) 2015-2018, LAAS-CNRS |
2 |
|
|
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr) |
3 |
|
|
// |
4 |
|
|
// This file is part of gepetto-viewer. |
5 |
|
|
// gepetto-viewer is free software: you can redistribute it |
6 |
|
|
// and/or modify it under the terms of the GNU Lesser General Public |
7 |
|
|
// License as published by the Free Software Foundation, either version |
8 |
|
|
// 3 of the License, or (at your option) any later version. |
9 |
|
|
// |
10 |
|
|
// gepetto-viewer is distributed in the hope that it will be |
11 |
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
12 |
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
|
|
// General Lesser Public License for more details. You should have |
14 |
|
|
// received a copy of the GNU Lesser General Public License along with |
15 |
|
|
// gepetto-viewer. If not, see <http://www.gnu.org/licenses/>. |
16 |
|
|
|
17 |
|
|
#include <QAbstractItemView> |
18 |
|
|
#include <QAction> |
19 |
|
|
#include <QCompleter> |
20 |
|
|
#include <QDebug> |
21 |
|
|
#include <QHBoxLayout> |
22 |
|
|
#include <QKeyEvent> |
23 |
|
|
#include <gepetto/gui/action-search-bar.hh> |
24 |
|
|
|
25 |
|
|
namespace gepetto { |
26 |
|
|
namespace gui { |
27 |
|
✗ |
ActionSearchBar::ActionSearchBar(QWidget* grandparent) |
28 |
|
|
: QLineEdit(NULL), |
29 |
|
✗ |
model_(new QStringListModel(this)), |
30 |
|
✗ |
completer_(new QCompleter(model_, this)), |
31 |
|
✗ |
showAction_(new QAction("Open action search bar popup", this)) { |
32 |
|
✗ |
setPlaceholderText("Type here"); |
33 |
|
✗ |
setCompleter(completer_); |
34 |
|
✗ |
completer_->setCaseSensitivity(Qt::CaseInsensitive); |
35 |
|
✗ |
showAction_->setShortcut(Qt::Key_M); |
36 |
|
✗ |
showAction_->setShortcutContext(Qt::WidgetWithChildrenShortcut); |
37 |
|
|
|
38 |
|
✗ |
connect(this, SIGNAL(returnPressed()), SLOT(handleReturnPressed())); |
39 |
|
|
|
40 |
|
✗ |
QWidget* popup(new QWidget(grandparent)); |
41 |
|
✗ |
popup->setWindowFlags(Qt::Popup); |
42 |
|
✗ |
popup->connect(showAction_, SIGNAL(triggered()), SLOT(show())); |
43 |
|
✗ |
QHBoxLayout* layout = new QHBoxLayout(popup); |
44 |
|
✗ |
layout->addWidget(this); |
45 |
|
✗ |
layout->setContentsMargins(0, 0, 0, 0); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
✗ |
void ActionSearchBar::addAction(QAction* action) { |
49 |
|
✗ |
QString t = action->text(); |
50 |
|
✗ |
t.remove('&'); |
51 |
|
✗ |
if (actions_.contains(t)) { |
52 |
|
✗ |
actions_[t] = action; |
53 |
|
|
} else { |
54 |
|
✗ |
actions_[t] = action; |
55 |
|
✗ |
model_->setStringList(model_->stringList() << t); |
56 |
|
|
} |
57 |
|
|
} |
58 |
|
|
|
59 |
|
✗ |
void ActionSearchBar::keyPressEvent(QKeyEvent* event) { |
60 |
|
✗ |
switch (event->key()) { |
61 |
|
✗ |
case Qt::Key_Escape: |
62 |
|
✗ |
parentWidget()->close(); |
63 |
|
✗ |
return; |
64 |
|
✗ |
case Qt::Key_Enter: |
65 |
|
✗ |
handleReturnPressed(); |
66 |
|
✗ |
return; |
67 |
|
✗ |
case Qt::Key_Tab: |
68 |
|
|
// Autocomplete |
69 |
|
✗ |
if (completer_->completionCount() > 0) |
70 |
|
✗ |
setText(completer_->currentCompletion()); |
71 |
|
✗ |
return; |
72 |
|
✗ |
default: |
73 |
|
✗ |
QLineEdit::keyPressEvent(event); |
74 |
|
✗ |
return; |
75 |
|
|
} |
76 |
|
|
} |
77 |
|
|
|
78 |
|
✗ |
void ActionSearchBar::showEvent(QShowEvent*) { |
79 |
|
✗ |
parentWidget()->move(QCursor::pos()); |
80 |
|
✗ |
setFocus(Qt::PopupFocusReason); |
81 |
|
✗ |
selectAll(); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
✗ |
bool ActionSearchBar::trigger(const QString& s) { |
85 |
|
|
// qDebug() << "Try action " << s; |
86 |
|
✗ |
if (!actions_.contains(s)) return false; |
87 |
|
✗ |
actions_[s]->trigger(); |
88 |
|
|
// qDebug() << "Action " << s; |
89 |
|
✗ |
parentWidget()->close(); |
90 |
|
✗ |
return true; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
✗ |
void ActionSearchBar::handleReturnPressed() { |
94 |
|
✗ |
trigger(text()); |
95 |
|
✗ |
parentWidget()->close(); |
96 |
|
|
} |
97 |
|
|
} // namespace gui |
98 |
|
|
} // namespace gepetto |
99 |
|
|
|