GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/gui/action-search-bar.cc Lines: 0 50 0.0 %
Date: 2020-05-14 11:23:33 Branches: 0 90 0.0 %

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 <gepetto/gui/action-search-bar.hh>
18
19
#include <QDebug>
20
21
#include <QAction>
22
#include <QKeyEvent>
23
#include <QCompleter>
24
#include <QAbstractItemView>
25
#include <QHBoxLayout>
26
27
namespace gepetto {
28
  namespace gui {
29
    ActionSearchBar::ActionSearchBar (QWidget* grandparent)
30
      : QLineEdit(NULL)
31
      , model_ (new QStringListModel(this))
32
      , completer_ (new QCompleter(model_, this))
33
      , showAction_ (new QAction("Open action search bar popup", this))
34
    {
35
      setPlaceholderText("Type here");
36
      setCompleter(completer_);
37
      completer_->setCaseSensitivity(Qt::CaseInsensitive);
38
      showAction_->setShortcut(Qt::Key_M);
39
      showAction_->setShortcutContext(Qt::WidgetWithChildrenShortcut);
40
41
      connect(this, SIGNAL(returnPressed()), SLOT(handleReturnPressed()));
42
43
      QWidget* popup(new QWidget(grandparent));
44
      popup->setWindowFlags(Qt::Popup);
45
      popup->connect(showAction_, SIGNAL(triggered()), SLOT(show()));
46
      QHBoxLayout *layout = new QHBoxLayout(popup);
47
      layout->addWidget(this);
48
      layout->setContentsMargins(0,0,0,0);
49
    }
50
51
    void ActionSearchBar::addAction (QAction* action)
52
    {
53
      QString t = action->text();
54
      t.remove('&');
55
      if (actions_.contains(t)) {
56
        actions_[t] = action;
57
      } else {
58
        actions_[t] = action;
59
        model_->setStringList( model_->stringList() << t );
60
      }
61
    }
62
63
    void ActionSearchBar::keyPressEvent ( QKeyEvent* event )
64
    {
65
      switch (event->key()) {
66
        case Qt::Key_Escape:
67
          parentWidget()->close();
68
          return;
69
        case Qt::Key_Enter:
70
          handleReturnPressed();
71
          return;
72
        case Qt::Key_Tab:
73
          // Autocomplete
74
          if (completer_->completionCount() > 0)
75
            setText(completer_->currentCompletion());
76
          return;
77
        default:
78
          QLineEdit::keyPressEvent(event);
79
          return;
80
      }
81
    }
82
83
    void ActionSearchBar::showEvent ( QShowEvent* )
84
    {
85
      parentWidget()->move(QCursor::pos());
86
      setFocus(Qt::PopupFocusReason);
87
      selectAll();
88
    }
89
90
    bool ActionSearchBar::trigger ( const QString& s )
91
    {
92
      // qDebug() << "Try action " << s;
93
      if (!actions_.contains(s)) return false;
94
      actions_[s]->trigger();
95
      // qDebug() << "Action " << s;
96
      parentWidget()->close();
97
      return true;
98
    }
99
100
    void ActionSearchBar::handleReturnPressed ()
101
    {
102
      trigger(text());
103
      parentWidget()->close();
104
    }
105
  }
106
}