PackageScheme.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * Copyright (C) 2015 by Ralf Kaestner                                        *
00003  * ralf.kaestner@gmail.com                                                    *
00004  *                                                                            *
00005  * This program is free software; you can redistribute it and/or modify       *
00006  * it under the terms of the Lesser GNU General Public License as published by*
00007  * the Free Software Foundation; either version 3 of the License, or          *
00008  * (at your option) any later version.                                        *
00009  *                                                                            *
00010  * This program is distributed in the hope that it will be useful,            *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *
00013  * Lesser GNU General Public License for more details.                        *
00014  *                                                                            *
00015  * You should have received a copy of the Lesser GNU General Public License   *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
00017  ******************************************************************************/
00018 
00019 #include <ros/package.h>
00020 
00021 #include "rqt_multiplot/PackageScheme.h"
00022 
00023 namespace rqt_multiplot {
00024 
00025 /*****************************************************************************/
00026 /* Constructors and Destructor                                               */
00027 /*****************************************************************************/
00028 
00029 PackageScheme::PackageScheme(QObject* parent, const QString& prefix,
00030     QDir::Filters filter) :
00031   UrlScheme(prefix),
00032   registry_(new PackageRegistry(this)),
00033   fileSystemModel_(new QFileSystemModel(this)),
00034   packageListModel_(new QStringListModel(this)) {
00035   fileSystemModel_->setRootPath("/");
00036   fileSystemModel_->setFilter(filter);
00037   
00038   connect(registry_, SIGNAL(updateStarted()), this,
00039     SLOT(registryUpdateStarted()));
00040   connect(registry_, SIGNAL(updateFinished()), this,
00041     SLOT(registryUpdateFinished()));
00042   
00043   connect(fileSystemModel_, SIGNAL(directoryLoaded(const QString&)),
00044     this, SLOT(modelDirectoryLoaded(const QString&)));
00045   
00046   if (registry_->isUpdating())
00047     registryUpdateStarted();
00048   else if (registry_->isEmpty())
00049     registry_->update();
00050 }
00051 
00052 PackageScheme::~PackageScheme() {
00053 }
00054 
00055 /*****************************************************************************/
00056 /* Accessors                                                                 */
00057 /*****************************************************************************/
00058 
00059 void PackageScheme::setFilter(QDir::Filters filter) {
00060   fileSystemModel_->setFilter(filter);
00061 }
00062 
00063 QDir::Filters PackageScheme::getFilter() const {
00064   return fileSystemModel_->filter();
00065 }
00066 
00067 size_t PackageScheme::getNumHosts() const {
00068   return packages_.count();
00069 }
00070 
00071 QModelIndex PackageScheme::getHostIndex(size_t row) const {
00072   return packageListModel_->index(row);
00073 }
00074 
00075 QVariant PackageScheme::getHostData(const QModelIndex& index, int role)
00076   const {
00077   return packageListModel_->data(index, role);
00078 }
00079   
00080 size_t PackageScheme::getNumPaths(const QModelIndex& hostIndex, const
00081     QModelIndex& parent) const {
00082   if (!parent.isValid()) {
00083     if (hostIndex.isValid()) {
00084       QString packagePath = packagePaths_[packages_[hostIndex.row()]];
00085       QModelIndex packagePathIndex = fileSystemModel_->index(packagePath);
00086       
00087       if (fileSystemModel_->canFetchMore(packagePathIndex))
00088         fileSystemModel_->fetchMore(packagePathIndex);
00089       
00090       return fileSystemModel_->rowCount(packagePathIndex);
00091     }
00092   }
00093   else {
00094     if (fileSystemModel_->canFetchMore(parent))
00095       fileSystemModel_->fetchMore(parent);
00096     
00097     return fileSystemModel_->rowCount(parent);
00098   }
00099   
00100   return 0;
00101 }
00102 
00103 QModelIndex PackageScheme::getPathIndex(const QModelIndex& hostIndex, size_t
00104     row, const QModelIndex& parent) const {
00105   if (!parent.isValid()) {
00106     if (hostIndex.isValid()) {
00107       QString packagePath = packagePaths_[packages_[hostIndex.row()]];
00108       QModelIndex packagePathIndex = fileSystemModel_->index(packagePath);
00109       
00110       return fileSystemModel_->index(row, 0, packagePathIndex);
00111     }
00112   }
00113   else
00114     return fileSystemModel_->index(row, 0, parent);
00115   
00116   return QModelIndex();
00117 }
00118 
00119 QVariant PackageScheme::getPathData(const QModelIndex& index, int role)
00120     const {
00121   return fileSystemModel_->data(index, role);
00122 }
00123 
00124 QString PackageScheme::getHost(const QModelIndex& hostIndex) const {
00125   if (hostIndex.isValid())
00126     return packages_[hostIndex.row()];
00127 
00128   return QString();
00129 }
00130 
00131 QString PackageScheme::getPath(const QModelIndex& hostIndex, const
00132     QModelIndex& pathIndex) const {
00133   if (hostIndex.isValid()) {
00134     QString packagePath = packagePaths_[packages_[hostIndex.row()]];
00135     QString path = fileSystemModel_->filePath(pathIndex);
00136     
00137     return QDir(packagePath).relativeFilePath(path);
00138   }
00139   
00140   return QString();
00141 }
00142 
00143 QString PackageScheme::getFilePath(const QModelIndex& hostIndex, const
00144     QModelIndex& pathIndex) const {
00145   if (hostIndex.isValid()) {
00146     if (pathIndex.isValid())
00147       return fileSystemModel_->filePath(pathIndex);
00148     else
00149       return packagePaths_[packages_[hostIndex.row()]];
00150   }
00151   
00152   return QString();
00153 }
00154 
00155 QString PackageScheme::getFilePath(const QString& host, const QString& path)
00156     const {
00157   QString packagePath;
00158   
00159   if (!packagePaths_.isEmpty()) {
00160     QMap<QString, QString>::const_iterator it = packagePaths_.find(host);
00161     
00162     if (it != packagePaths_.end())
00163       packagePath = it.value();
00164   }
00165   else
00166     packagePath = QString::fromStdString(ros::package::getPath(
00167       host.toStdString()));
00168     
00169   if (!packagePath.isEmpty())  {
00170     QDir packageDir(packagePath);
00171     
00172     if (packageDir.exists())
00173       return packageDir.absoluteFilePath(path);
00174   }
00175   
00176   return QString();
00177 }
00178 
00179 /*****************************************************************************/
00180 /* Slots                                                                     */
00181 /*****************************************************************************/
00182 
00183 void PackageScheme::registryUpdateStarted() {
00184   emit resetStarted();
00185 }
00186 
00187 void PackageScheme::registryUpdateFinished() {
00188   packagePaths_ = registry_->getPackages();
00189   packages_ = packagePaths_.keys();
00190   
00191   packageListModel_->setStringList(packages_);
00192   
00193   emit resetFinished();
00194 }
00195 
00196 void PackageScheme::modelDirectoryLoaded(const QString& path) {
00197   for (QMap<QString, QString>::const_iterator it = packagePaths_.begin();
00198       it != packagePaths_.end(); ++it) {
00199     if (path.startsWith(it.value()))
00200       emit pathLoaded(it.key(), QDir(it.value()).relativeFilePath(path));
00201   }
00202 }
00203 
00204 }


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Tue May 9 2017 02:16:02