Gazebo Gazebo

API Reference

3.15.1
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 <memory>
23 #include <string>
24 #include <vector>
25 #include <gz/fuel_tools/ClientConfig.hh>
26 #include <gz/fuel_tools/FuelClient.hh>
27 
28 #include <gz/gui/Plugin.hh>
29 
30 namespace ignition
31 {
32 namespace gazebo
33 {
34  class ResourceSpawnerPrivate;
35 
37  struct Resource
38  {
41 
45 
48 
52 
54  // cppcheck-suppress unusedStructMember
55  bool isFuel = false;
56 
59  // cppcheck-suppress unusedStructMember
60  bool isDownloaded = false;
61  };
62 
65  struct Display
66  {
70 
75 
79 
82  // cppcheck-suppress unusedStructMember
83  bool isFuel = false;
84  };
85 
88  class PathModel : public QStandardItemModel
89  {
90  Q_OBJECT
91 
93  public: explicit PathModel();
94 
96  public: ~PathModel() override = default;
97 
100  public: void AddPath(const std::string &_path);
101 
104  public: void RemovePath(const std::string &_path);
105 
106  // Documentation inherited
107  public: QHash<int, QByteArray> roleNames() const override;
108  };
109 
112  class ResourceModel : public QStandardItemModel
113  {
114  Q_OBJECT
115 
118  Q_PROPERTY(int totalCount MEMBER gridIndex NOTIFY sizeChanged)
119 
120 
121  public: explicit ResourceModel();
122 
124  public: ~ResourceModel() override = default;
125 
128  public: void AddResource(const Resource &_resource);
129 
132  public: void AddResources(std::vector<Resource> &_resources);
133 
135  public: void Clear();
136 
142  public: void UpdateResourceModel(int index, Resource &_resource);
143 
144  // Documentation inherited
145  public: QHash<int, QByteArray> roleNames() const override;
146 
148  public: signals: void sizeChanged();
149 
150  // \brief Index to keep track of the position of each resource in the qml
151  // grid, used primarily to access currently loaded resources for updates.
152  public: int gridIndex = 0;
153  };
154 
157  class ResourceSpawner : public gz::gui::Plugin
158  {
159  Q_OBJECT
160 
162  public: ResourceSpawner();
163 
165  public: ~ResourceSpawner() override;
166 
167  // Documentation inherited
168  public: void LoadConfig(const tinyxml2::XMLElement *_pluginElem) override;
169 
172  public slots: void OnResourceSpawn(const QString &_sdfPath);
173 
177  public: Resource LocalResource(const std::string &_path);
178 
181  public: void AddPath(const std::string &_path);
182 
187  public: std::vector<Resource> LocalResources(const std::string &_path);
188 
193  public: std::vector<Resource> FuelResources(const std::string &_owner);
194 
198  public: void Resources(std::vector<Resource> &_resources);
199 
203  public: void FilterResources(std::vector<Resource> &_resources);
204 
210  public: void SortResources(std::vector<Resource> &_resources);
211 
214  public slots: void DisplayResources();
215 
219  public slots: void OnPathClicked(const QString &_path);
220 
225  public slots: void OnOwnerClicked(const QString &_owner);
226 
232  public slots: void OnDownloadFuelResource(const QString &_path,
233  const QString &_name, const QString &_owner, int index);
234 
239  public slots: void OnSortChosen(const QString &_sortType);
240 
244  public slots: void OnSearchEntered(const QString &_searchKeyword);
245 
251  public: void SetThumbnail(const std::string &_thumbnailPath,
252  Resource &_resource);
253 
259  public: Q_INVOKABLE void UpdateOwnerListModel(
260  ignition::gazebo::Resource _resource);
261 
266  public: Q_INVOKABLE bool AddOwner(const QString &_owner);
267 
271  public: Q_INVOKABLE void RemoveOwner(const QString &_owner);
272 
275  public: Q_INVOKABLE bool IsDefaultOwner(const QString &_owner) const;
276 
279  signals: void resourceSpawnerError(const QString &_errorMsg);
280 
283  private: void RunFetchResourceListThread(const std::string &_owner);
284 
288  };
289 }
290 }
291 
292 #endif
Resource used to update the ResourceModel.
Definition: ResourceSpawner.hh:37
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:78
This library is part of the Ignition Robotics 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:69
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:60
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:83
std::string sortMethod
The currently chosen method of sorting, which includes "A - Z", "Z - A", "Most Recent",...
Definition: ResourceSpawner.hh:74
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:88
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:65
bool isFuel
Bool to indicate if this model is fuel or not.
Definition: ResourceSpawner.hh:55
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:152
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:157
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:112
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:118
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:51
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:47
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:40
std::string owner
The owner of the resource, if the resource is local, owner will be empty.
Definition: ResourceSpawner.hh:44