Gazebo Gazebo

API Reference

6.16.0
ResourceSpawner.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef GZ_GAZEBO_GUI_RESOURCE_SPAWNER_HH_
19 #define GZ_GAZEBO_GUI_RESOURCE_SPAWNER_HH_
20 
21 #include <qobjectdefs.h>
22 #include <algorithm>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 #include <gz/fuel_tools/ClientConfig.hh>
27 #include <gz/fuel_tools/FuelClient.hh>
28 
29 #include <gz/gui/Plugin.hh>
30 
31 namespace ignition
32 {
33 namespace gazebo
34 {
35  class ResourceSpawnerPrivate;
36 
38  struct Resource
39  {
42 
46 
49 
53 
55  // cppcheck-suppress unusedStructMember
56  bool isFuel = false;
57 
60  // cppcheck-suppress unusedStructMember
61  bool isDownloaded = false;
62  };
63 
66  struct Display
67  {
71 
76 
80 
83  // cppcheck-suppress unusedStructMember
84  bool isFuel = false;
85  };
86 
89  class PathModel : public QStandardItemModel
90  {
91  Q_OBJECT
92 
94  public: explicit PathModel();
95 
97  public: ~PathModel() override = default;
98 
101  public: void AddPath(const std::string &_path);
102 
105  public: void RemovePath(const std::string &_path);
106 
107  // Documentation inherited
108  public: QHash<int, QByteArray> roleNames() const override;
109  };
110 
113  class ResourceModel : public QStandardItemModel
114  {
115  Q_OBJECT
116 
119  Q_PROPERTY(int totalCount MEMBER gridIndex NOTIFY sizeChanged)
120 
121 
122  public: explicit ResourceModel();
123 
125  public: ~ResourceModel() override = default;
126 
129  public: void AddResource(const Resource &_resource);
130 
133  public: void AddResources(std::vector<Resource> &_resources);
134 
136  public: void Clear();
137 
143  public: void UpdateResourceModel(int index, Resource &_resource);
144 
145  // Documentation inherited
146  public: QHash<int, QByteArray> roleNames() const override;
147 
149  public: signals: void sizeChanged();
150 
151  // \brief Index to keep track of the position of each resource in the qml
152  // grid, used primarily to access currently loaded resources for updates.
153  public: int gridIndex = 0;
154  };
155 
158  class ResourceSpawner : public gz::gui::Plugin
159  {
160  Q_OBJECT
161 
163  public: ResourceSpawner();
164 
166  public: ~ResourceSpawner() override;
167 
168  // Documentation inherited
169  public: void LoadConfig(const tinyxml2::XMLElement *_pluginElem) override;
170 
173  public slots: void OnResourceSpawn(const QString &_sdfPath);
174 
178  public: Resource LocalResource(const std::string &_path);
179 
182  public: void AddPath(const std::string &_path);
183 
188  public: std::vector<Resource> LocalResources(const std::string &_path);
189 
194  public: std::vector<Resource> FuelResources(const std::string &_owner);
195 
199  public: void Resources(std::vector<Resource> &_resources);
200 
204  public: void FilterResources(std::vector<Resource> &_resources);
205 
211  public: void SortResources(std::vector<Resource> &_resources);
212 
215  public slots: void DisplayResources();
216 
220  public slots: void OnPathClicked(const QString &_path);
221 
226  public slots: void OnOwnerClicked(const QString &_owner);
227 
233  public slots: void OnDownloadFuelResource(const QString &_path,
234  const QString &_name, const QString &_owner, int index);
235 
240  public slots: void OnSortChosen(const QString &_sortType);
241 
245  public slots: void OnSearchEntered(const QString &_searchKeyword);
246 
252  public: void SetThumbnail(const std::string &_thumbnailPath,
253  Resource &_resource);
254 
260  public: Q_INVOKABLE void UpdateOwnerListModel(
261  ignition::gazebo::Resource _resource);
262 
267  public: Q_INVOKABLE bool AddOwner(const QString &_owner);
268 
272  public: Q_INVOKABLE void RemoveOwner(const QString &_owner);
273 
276  public: Q_INVOKABLE bool IsDefaultOwner(const QString &_owner) const;
277 
280  signals: void resourceSpawnerError(const QString &_errorMsg);
281 
284  private: void RunFetchResourceListThread(const std::string &_owner);
285 
289  };
290 }
291 }
292 
293 #endif
Resource used to update the ResourceModel.
Definition: ResourceSpawner.hh:38
std::string ownerPath
The name of the owner if the user has Fuel resources chosen, and the name of the local path if the us...
Definition: ResourceSpawner.hh:79
This library is part of the Gazebo project.
STL class.
std::string searchKeyword
The currently entered keyword that the user wants to search, empty if there is currently no search qu...
Definition: ResourceSpawner.hh:70
void UpdateResourceModel(int index, Resource &_resource)
Updates the resource at the provided index with the values in the passed in resource.
bool isDownloaded
Bool to indicate if this model has been downloaded or not, will always be false with local models as ...
Definition: ResourceSpawner.hh:61
std::vector< Resource > LocalResources(const std::string &_path)
Returns the local resources as a vector located under the passed in path.
void OnDownloadFuelResource(const QString &_path, const QString &_name, const QString &_owner, int index)
Callback when a request is made to download a fuel resource.
~ResourceSpawner() override
Destructor.
~ResourceModel() override=default
Destructor.
void OnPathClicked(const QString &_path)
Callback when a resource path is selected, will clear the currently loaded resources and load the one...
STL class.
void OnSearchEntered(const QString &_searchKeyword)
Callback when a search request is made.
bool isFuel
True if the user is currently observing fuel resources and false if the user is currently observing l...
Definition: ResourceSpawner.hh:84
std::string sortMethod
The currently chosen method of sorting, which includes "A - Z", "Z - A", "Most Recent",...
Definition: ResourceSpawner.hh:75
Q_INVOKABLE void UpdateOwnerListModel(ignition::gazebo::Resource _resource)
Called form a download thread to update the GUI's list of resources.
void AddPath(const std::string &_path)
Add a path. param[in] _path The path to be added.
Provides a model by which the resource spawner qml plugin pulls and updates from.
Definition: ResourceSpawner.hh:89
void LoadConfig(const tinyxml2::XMLElement *_pluginElem) override
Q_INVOKABLE void RemoveOwner(const QString &_owner)
Remove owner from the list of owners whose resources would be fetched from Fuel.
void SetThumbnail(const std::string &_thumbnailPath, Resource &_resource)
Finds a thumbnail on the provided thumbnail path and sets the model's thumbnail path attribute to it,...
void AddPath(const std::string &_path)
Adds a path to the path list model.
Data used by the DisplayData() function to filter and sort the resources to be displayed.
Definition: ResourceSpawner.hh:66
bool isFuel
Bool to indicate if this model is fuel or not.
Definition: ResourceSpawner.hh:56
void RemovePath(const std::string &_path)
Remove a path. param[in] _path The path to be removed.
Resource LocalResource(const std::string &_path)
Returns the resource corresponding to the model.config file.
int gridIndex
Definition: ResourceSpawner.hh:153
void FilterResources(std::vector< Resource > &_resources)
Filters the vector of resources by the previously entered search keyword.
void SortResources(std::vector< Resource > &_resources)
Sorts the vector of resources by the previously entered sort method. The sorting types as a string,...
Provides interface for communicating to backend for generation of local models.
Definition: ResourceSpawner.hh:158
std::vector< Resource > FuelResources(const std::string &_owner)
Returns the fuel resources as a vector belonging to the passed in owner.
Q_INVOKABLE bool AddOwner(const QString &_owner)
Add owner to the list of owners whose resources would be fetched from Fuel.
void DisplayResources()
Displays the resources to the qml grid abiding by all search and sort criteria.
void Resources(std::vector< Resource > &_resources)
Populates the passed in _resources vector with the currently selected group of resources.
void Clear()
Clear the current resource model.
void sizeChanged()
Signal used with the totalCount property.
QHash< int, QByteArray > roleNames() const override
Provides a model by which the resource spawner qml plugin pulls and updates from.
Definition: ResourceSpawner.hh:113
void OnSortChosen(const QString &_sortType)
Callback when a sort request is made.
int totalCount
Property used to display the total number of resources associated with an owner.
Definition: ResourceSpawner.hh:119
void OnOwnerClicked(const QString &_owner)
Callback when a fuel owner is selected, will clear the currently loaded resources and load the ones b...
void AddResource(const Resource &_resource)
Add a resource to the grid view. param[in] _resource The local resource to be added.
Q_INVOKABLE bool IsDefaultOwner(const QString &_owner) const
Determine if owner is the default owner.
std::string thumbnailPath
The absolute path to the thumbnail of the local model, will be empty if no thumbnail is found.
Definition: ResourceSpawner.hh:52
void AddResources(std::vector< Resource > &_resources)
Add a vector of resources to the grid view. param[in] _resource The vector of local resources to be a...
std::string sdfPath
The absolute path to the sdf corresponding to the local model.
Definition: ResourceSpawner.hh:48
void resourceSpawnerError(const QString &_errorMsg)
Signal emitted when an error is encountered regarding an owner.
QHash< int, QByteArray > roleNames() const override
~PathModel() override=default
Destructor.
void OnResourceSpawn(const QString &_sdfPath)
Callback when a resource is selected.
std::string name
The name of the resource.
Definition: ResourceSpawner.hh:41
std::string owner
The owner of the resource, if the resource is local, owner will be empty.
Definition: ResourceSpawner.hh:45