PackageScheme.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2015 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <ros/package.h>
20 
22 
23 namespace rqt_multiplot {
24 
25 /*****************************************************************************/
26 /* Constructors and Destructor */
27 /*****************************************************************************/
28 
29 PackageScheme::PackageScheme(QObject* parent, const QString& prefix,
30  QDir::Filters filter) :
31  UrlScheme(prefix),
32  registry_(new PackageRegistry(this)),
33  fileSystemModel_(new QFileSystemModel(this)),
34  packageListModel_(new QStringListModel(this)) {
35  fileSystemModel_->setRootPath("/");
36  fileSystemModel_->setFilter(filter);
37 
38  connect(registry_, SIGNAL(updateStarted()), this,
39  SLOT(registryUpdateStarted()));
40  connect(registry_, SIGNAL(updateFinished()), this,
41  SLOT(registryUpdateFinished()));
42 
43  connect(fileSystemModel_, SIGNAL(directoryLoaded(const QString&)),
44  this, SLOT(modelDirectoryLoaded(const QString&)));
45 
46  if (registry_->isUpdating())
48  else if (registry_->isEmpty())
49  registry_->update();
50 }
51 
53 }
54 
55 /*****************************************************************************/
56 /* Accessors */
57 /*****************************************************************************/
58 
59 void PackageScheme::setFilter(QDir::Filters filter) {
60  fileSystemModel_->setFilter(filter);
61 }
62 
63 QDir::Filters PackageScheme::getFilter() const {
64  return fileSystemModel_->filter();
65 }
66 
68  return packages_.count();
69 }
70 
71 QModelIndex PackageScheme::getHostIndex(size_t row) const {
72  return packageListModel_->index(row);
73 }
74 
75 QVariant PackageScheme::getHostData(const QModelIndex& index, int role)
76  const {
77  return packageListModel_->data(index, role);
78 }
79 
80 size_t PackageScheme::getNumPaths(const QModelIndex& hostIndex, const
81  QModelIndex& parent) const {
82  if (!parent.isValid()) {
83  if (hostIndex.isValid()) {
84  QString packagePath = packagePaths_[packages_[hostIndex.row()]];
85  QModelIndex packagePathIndex = fileSystemModel_->index(packagePath);
86 
87  if (fileSystemModel_->canFetchMore(packagePathIndex))
88  fileSystemModel_->fetchMore(packagePathIndex);
89 
90  return fileSystemModel_->rowCount(packagePathIndex);
91  }
92  }
93  else {
94  if (fileSystemModel_->canFetchMore(parent))
95  fileSystemModel_->fetchMore(parent);
96 
97  return fileSystemModel_->rowCount(parent);
98  }
99 
100  return 0;
101 }
102 
103 QModelIndex PackageScheme::getPathIndex(const QModelIndex& hostIndex, size_t
104  row, const QModelIndex& parent) const {
105  if (!parent.isValid()) {
106  if (hostIndex.isValid()) {
107  QString packagePath = packagePaths_[packages_[hostIndex.row()]];
108  QModelIndex packagePathIndex = fileSystemModel_->index(packagePath);
109 
110  return fileSystemModel_->index(row, 0, packagePathIndex);
111  }
112  }
113  else
114  return fileSystemModel_->index(row, 0, parent);
115 
116  return QModelIndex();
117 }
118 
119 QVariant PackageScheme::getPathData(const QModelIndex& index, int role)
120  const {
121  return fileSystemModel_->data(index, role);
122 }
123 
124 QString PackageScheme::getHost(const QModelIndex& hostIndex) const {
125  if (hostIndex.isValid())
126  return packages_[hostIndex.row()];
127 
128  return QString();
129 }
130 
131 QString PackageScheme::getPath(const QModelIndex& hostIndex, const
132  QModelIndex& pathIndex) const {
133  if (hostIndex.isValid()) {
134  QString packagePath = packagePaths_[packages_[hostIndex.row()]];
135  QString path = fileSystemModel_->filePath(pathIndex);
136 
137  return QDir(packagePath).relativeFilePath(path);
138  }
139 
140  return QString();
141 }
142 
143 QString PackageScheme::getFilePath(const QModelIndex& hostIndex, const
144  QModelIndex& pathIndex) const {
145  if (hostIndex.isValid()) {
146  if (pathIndex.isValid())
147  return fileSystemModel_->filePath(pathIndex);
148  else
149  return packagePaths_[packages_[hostIndex.row()]];
150  }
151 
152  return QString();
153 }
154 
155 QString PackageScheme::getFilePath(const QString& host, const QString& path)
156  const {
157  QString packagePath;
158 
159  if (!packagePaths_.isEmpty()) {
160  QMap<QString, QString>::const_iterator it = packagePaths_.find(host);
161 
162  if (it != packagePaths_.end())
163  packagePath = it.value();
164  }
165  else
166  packagePath = QString::fromStdString(ros::package::getPath(
167  host.toStdString()));
168 
169  if (!packagePath.isEmpty()) {
170  QDir packageDir(packagePath);
171 
172  if (packageDir.exists())
173  return packageDir.absoluteFilePath(path);
174  }
175 
176  return QString();
177 }
178 
179 /*****************************************************************************/
180 /* Slots */
181 /*****************************************************************************/
182 
184  emit resetStarted();
185 }
186 
189  packages_ = packagePaths_.keys();
190 
191  packageListModel_->setStringList(packages_);
192 
193  emit resetFinished();
194 }
195 
196 void PackageScheme::modelDirectoryLoaded(const QString& path) {
197  for (QMap<QString, QString>::const_iterator it = packagePaths_.begin();
198  it != packagePaths_.end(); ++it) {
199  if (path.startsWith(it.value()))
200  emit pathLoaded(it.key(), QDir(it.value()).relativeFilePath(path));
201  }
202 }
203 
204 }
QVariant getPathData(const QModelIndex &index, int role) const
QModelIndex getHostIndex(size_t row) const
void pathLoaded(const QString &host, const QString &path)
QVariant getHostData(const QModelIndex &index, int role) const
PackageScheme(QObject *parent=0, const QString &prefix="package", QDir::Filters filter=QDir::NoFilter)
QString getFilePath(const QModelIndex &hostIndex, const QModelIndex &pathIndex) const
QString getPath(const QModelIndex &hostIndex, const QModelIndex &pathIndex) const
QList< QString > packages_
Definition: PackageScheme.h:67
void setFilter(QDir::Filters filter)
QString getHost(const QModelIndex &hostIndex) const
void modelDirectoryLoaded(const QString &path)
QDir::Filters getFilter() const
QFileSystemModel * fileSystemModel_
Definition: PackageScheme.h:64
ROSLIB_DECL std::string getPath(const std::string &package_name)
QStringListModel * packageListModel_
Definition: PackageScheme.h:65
size_t getNumPaths(const QModelIndex &hostIndex, const QModelIndex &parent) const
PackageRegistry * registry_
Definition: PackageScheme.h:62
QMap< QString, QString > getPackages() const
QMap< QString, QString > packagePaths_
Definition: PackageScheme.h:68
QModelIndex getPathIndex(const QModelIndex &hostIndex, size_t row, const QModelIndex &parent=QModelIndex()) const


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Wed Jul 10 2019 03:49:44