Skip to content

Commit

Permalink
KeyPublisher (#81)
Browse files Browse the repository at this point in the history
Signed-off-by: mohamedsayed18 <[email protected]>
Co-authored-by: Louise Poubel <[email protected]>
  • Loading branch information
mohamedsayed18 and chapulina authored Jun 23, 2020
1 parent 0f64650 commit 9ec2140
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/config/keypublisher.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>

<plugin filename="KeyPublisher">
</plugin>

<plugin filename="TopicEcho">
</plugin>
1 change: 1 addition & 0 deletions src/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ endfunction()
# Plugins
add_subdirectory(grid_3d)
add_subdirectory(image_display)
add_subdirectory(key_publisher)
add_subdirectory(publisher)
add_subdirectory(scene3d)
add_subdirectory(topic_echo)
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/key_publisher/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ign_gui_add_plugin(KeyPublisher
SOURCES
KeyPublisher.cc
QT_HEADERS
KeyPublisher.hh
TEST_SOURCES # todo
# KeyPublisher_TEST.cc
)
91 changes: 91 additions & 0 deletions src/plugins/key_publisher/KeyPublisher.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2020 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <ignition/msgs/int32.pb.h>
#include <ignition/gui/Application.hh>
#include <ignition/gui/MainWindow.hh>
#include <ignition/plugin/Register.hh>

#include "KeyPublisher.hh"

namespace ignition
{
namespace gui
{
class KeyPublisherPrivate
{
/// \brief Node for communication
public: ignition::transport::Node node;

/// \brief Publisher
public: ignition::transport::Node::Publisher pub;

/// \brief Topic
public: std::string topic = "keyboard/keypress";

/// \brief Publish keyboard strokes
/// \param[in] key_press Pointer to the keyevent
public: void KeyPub(QKeyEvent *_keyPress)
{
ignition::msgs::Int32 Msg;
Msg.set_data(_keyPress->key());
pub.Publish(Msg);
}
};
}
}

using namespace ignition;
using namespace gui;

/////////////////////////////////////////////////
KeyPublisher::KeyPublisher(): Plugin(), dataPtr(new KeyPublisherPrivate)
{
// Advertise publisher node
this->dataPtr->pub = this->dataPtr->node.Advertise<ignition::msgs::Int32>
(this->dataPtr->topic);
}

/////////////////////////////////////////////////
KeyPublisher::~KeyPublisher()
{
}

/////////////////////////////////////////////////
void KeyPublisher::LoadConfig(const tinyxml2::XMLElement *)
{
if (this->title.empty())
this->title = "Key tool";

ignition::gui::App()->findChild
<ignition::gui::MainWindow *>()->QuickWindow()->installEventFilter(this);
}

/////////////////////////////////////////////////
bool KeyPublisher::eventFilter(QObject *_obj, QEvent *_event)
{
if (_event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(_event);
this->dataPtr->KeyPub(keyEvent);
}
return QObject::eventFilter(_obj, _event);
}

// Register this plugin
IGNITION_ADD_PLUGIN(ignition::gui::KeyPublisher,
ignition::gui::Plugin)
60 changes: 60 additions & 0 deletions src/plugins/key_publisher/KeyPublisher.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2020 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef IGNITION_GUI_PLUGINS_KEYPUBLISHER_HH_
#define IGNITION_GUI_PLUGINS_KEYPUBLISHER_HH_

#include <ignition/gui/qt.h>
#include <ignition/gui/Plugin.hh>
#include <ignition/transport/Node.hh>

namespace ignition
{
namespace gui
{
class KeyPublisherPrivate;

/// \brief Publish keyboard stokes to "keyboard/keypress" topic.
///
/// ## Configuration
/// This plugin doesn't accept any custom configuration.
class KeyPublisher : public ignition::gui::Plugin
{
Q_OBJECT

/// \brief Constructor
public: KeyPublisher();

/// \brief Destructor
public: virtual ~KeyPublisher();

// Documentation inherited
public: virtual void LoadConfig(const tinyxml2::XMLElement *) override;

/// \brief Filter events in Qt
/// \param[in] _obj The watched object
/// \param[in] _event Event that happen in Qt
protected: bool eventFilter(QObject *_obj, QEvent *_event) override;

/// \internal
/// \brief Pointer to private data.
private: std::unique_ptr<KeyPublisherPrivate> dataPtr;
};
}
}

#endif
26 changes: 26 additions & 0 deletions src/plugins/key_publisher/KeyPublisher.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2020 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import QtQuick 2.0
import QtQuick.Controls 2.0

Rectangle {
visible: false
width: 100
height: 100
}

5 changes: 5 additions & 0 deletions src/plugins/key_publisher/KeyPublisher.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="KeyPublisher/">
<file>KeyPublisher.qml</file>
</qresource>
</RCC>
8 changes: 8 additions & 0 deletions tutorials/03_plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ Echo messages from an Ignition Transport topic.

ign gui -c examples/config/pubsub.config

### Key publisher

Publish key presses through Ignition Transport.

ign gui -c examples/config/keypublisher.config

Change the topic to echo to `/keyboard/keypress`, echo, and start pressing keys.

### Topic viewer

**Not ported to versions 1 or higher yet**
Expand Down

0 comments on commit 9ec2140

Please sign in to comment.