Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <QStringList>
00020
00021 #include "rqt_multiplot/UrlItemModel.h"
00022
00023 namespace rqt_multiplot {
00024
00025
00026
00027
00028
00029 UrlItemModel::UrlItemModel(QObject* parent) {
00030 }
00031
00032 UrlItemModel::~UrlItemModel() {
00033 for (QList<UrlItem*>::iterator it = schemeItems_.begin();
00034 it != schemeItems_.end(); ++it)
00035 delete *it;
00036 }
00037
00038
00039
00040
00041
00042 QString UrlItemModel::getUrl(const QModelIndex& index) const {
00043 if (index.isValid()) {
00044 UrlItem* item = static_cast<UrlItem*>(index.internalPointer());
00045 UrlScheme* itemScheme = item->getScheme();
00046
00047 if (item->getType() == UrlItem::Scheme)
00048 return itemScheme->getPrefix()+"://";
00049 else if (item->getType() == UrlItem::Host)
00050 return itemScheme->getPrefix()+"://"+itemScheme->getHost(
00051 item->getIndex());
00052 else if (item->getType() == UrlItem::Path) {
00053 QModelIndex hostIndex = item->getIndex(UrlItem::Host);
00054
00055 return itemScheme->getPrefix()+"://"+itemScheme->getHost(hostIndex)+
00056 "/"+itemScheme->getPath(hostIndex, item->getIndex());
00057 }
00058 }
00059
00060 return QString();
00061 }
00062
00063 QString UrlItemModel::getFilePath(const QModelIndex& index) const {
00064 if (index.isValid()) {
00065 UrlItem* item = static_cast<UrlItem*>(index.internalPointer());
00066 UrlScheme* itemScheme = item->getScheme();
00067
00068 if (item->getType() == UrlItem::Host)
00069 return itemScheme->getFilePath(item->getIndex(), QModelIndex());
00070 else if (item->getType() == UrlItem::Path) {
00071 QModelIndex hostIndex = item->getIndex(UrlItem::Host);
00072
00073 return itemScheme->getFilePath(hostIndex, item->getIndex());
00074 }
00075 }
00076
00077 return QString();
00078 }
00079
00080 QString UrlItemModel::getFilePath(const QString& url) const {
00081 QStringList urlParts = url.split("://");
00082
00083 if (urlParts.count() > 1) {
00084 QString prefix = urlParts[0];
00085
00086 for (QList<UrlScheme*>::const_iterator it = schemes_.begin();
00087 it != schemes_.end(); ++it) {
00088 UrlScheme* scheme = *it;
00089
00090 if (scheme->getPrefix() == prefix) {
00091 QStringList hostPathParts = urlParts[1].split("/");
00092 QString host, path;
00093
00094 if (hostPathParts.count() > 1) {
00095 host = hostPathParts[0];
00096 hostPathParts.removeFirst();
00097 path = hostPathParts.join("/");
00098 }
00099 else
00100 host = urlParts[1];
00101
00102 return scheme->getFilePath(host, path);
00103 }
00104 }
00105 }
00106
00107 return QString();
00108 }
00109
00110 UrlScheme* UrlItemModel::getScheme(const QModelIndex& index) const {
00111 if (index.isValid()) {
00112 UrlItem* item = static_cast<UrlItem*>(index.internalPointer());
00113
00114 if (item)
00115 return item->getScheme();
00116 }
00117
00118 return 0;
00119 }
00120
00121
00122
00123
00124
00125 void UrlItemModel::addScheme(UrlScheme* scheme) {
00126 schemes_.append(scheme);
00127 schemeItems_.append(new UrlItem(scheme));
00128
00129 connect(scheme, SIGNAL(resetStarted()), this, SLOT(schemeResetStarted()));
00130 connect(scheme, SIGNAL(resetFinished()), this, SLOT(schemeResetFinished()));
00131 connect(scheme, SIGNAL(pathLoaded(const QString&, const QString&)),
00132 this, SLOT(schemePathLoaded(const QString&, const QString&)));
00133 }
00134
00135 int UrlItemModel::rowCount(const QModelIndex& parent) const {
00136 if (parent.column() <= 0) {
00137 if (parent.isValid()) {
00138 UrlItem* parentItem = static_cast<UrlItem*>(parent.internalPointer());
00139 UrlScheme* parentScheme = parentItem->getScheme();
00140
00141 if (parentItem->getType() == UrlItem::Scheme) {
00142 size_t numHosts = parentScheme->getNumHosts();
00143
00144 if (!numHosts)
00145 return parentScheme->getNumPaths(QModelIndex());
00146 else
00147 return numHosts;
00148 }
00149 else if (parentItem->getType() == UrlItem::Host)
00150 return parentScheme->getNumPaths(parentItem->getIndex());
00151 else if (parentItem->getType() == UrlItem::Path)
00152 return parentScheme->getNumPaths(parentItem->getIndex(
00153 UrlItem::Host), parentItem->getIndex());
00154 }
00155 else
00156 return schemes_.count();
00157 }
00158
00159 return 0;
00160 }
00161
00162 int UrlItemModel::columnCount(const QModelIndex& parent) const {
00163 return 1;
00164 }
00165
00166 QVariant UrlItemModel::data(const QModelIndex& index, int role) const {
00167 if (index.isValid()) {
00168 UrlItem* item = static_cast<UrlItem*>(index.internalPointer());
00169 UrlScheme* itemScheme = item->getScheme();
00170
00171 if (item->getType() == UrlItem::Scheme) {
00172 if ((role == Qt::DisplayRole) || (role == Qt::EditRole))
00173 return itemScheme->getPrefix()+"://";
00174 }
00175 else if (item->getType() == UrlItem::Host)
00176 return itemScheme->getHostData(item->getIndex(), role);
00177 else if (item->getType() == UrlItem::Path)
00178 return itemScheme->getPathData(item->getIndex(), role);
00179 }
00180
00181 return QVariant();
00182 }
00183
00184 QModelIndex UrlItemModel::index(int row, int column, const QModelIndex& parent)
00185 const {
00186 if (hasIndex(row, column, parent)) {
00187 if (parent.isValid()) {
00188 UrlItem* parentItem = static_cast<UrlItem*>(parent.internalPointer());
00189 UrlScheme* parentScheme = parentItem->getScheme();
00190 UrlItem* childItem = 0;
00191
00192 if (parentItem->getType() == UrlItem::Scheme) {
00193 size_t numHosts = parentScheme->getNumHosts();
00194
00195 if (!numHosts)
00196 childItem = parentItem->addChild(row, UrlItem::Path,
00197 parentScheme->getPathIndex(QModelIndex(), row));
00198 else
00199 childItem = parentItem->addChild(row, UrlItem::Host,
00200 parentScheme->getHostIndex(row));
00201 }
00202 else if (parentItem->getType() == UrlItem::Host)
00203 childItem = parentItem->addChild(row, UrlItem::Path,
00204 parentScheme->getPathIndex(parentItem->getIndex(UrlItem::Host),
00205 row));
00206 else if (parentItem->getType() == UrlItem::Path)
00207 childItem = parentItem->addChild(row, UrlItem::Path,
00208 parentScheme->getPathIndex(parentItem->getIndex(UrlItem::Host),
00209 row, parentItem->getIndex()));
00210
00211 return createIndex(row, column, childItem);
00212 }
00213 else
00214 return createIndex(row, column, schemeItems_[row]);
00215 }
00216
00217 return QModelIndex();
00218 }
00219
00220 QModelIndex UrlItemModel::parent(const QModelIndex& index) const {
00221 if (index.isValid()) {
00222 UrlItem* childItem = static_cast<UrlItem*>(index.internalPointer());
00223
00224 if (childItem) {
00225 UrlItem* parentItem = childItem->getParent();
00226
00227 if (parentItem)
00228 return createIndex(parentItem->getRow(), 0, parentItem);
00229 }
00230 }
00231
00232 return QModelIndex();
00233 }
00234
00235
00236
00237
00238
00239 void UrlItemModel::schemeResetStarted() {
00240 beginResetModel();
00241
00242 UrlScheme* scheme = static_cast<UrlScheme*>(sender());
00243 int i = schemes_.indexOf(scheme);
00244
00245 if (i >= 0) {
00246 delete schemeItems_[i];
00247 schemeItems_[i] = new UrlItem(scheme);
00248 }
00249 }
00250
00251 void UrlItemModel::schemeResetFinished() {
00252 endResetModel();
00253 }
00254
00255 void UrlItemModel::schemePathLoaded(const QString& host, const QString&
00256 path) {
00257 UrlScheme* scheme = static_cast<UrlScheme*>(sender());
00258
00259 QString url = scheme->getPrefix()+"://"+host;
00260
00261 if (!path.isEmpty())
00262 url += "/"+path;
00263
00264 emit urlLoaded(url);
00265 }
00266
00267 }