UrlItemModel.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 <QStringList>
20 
22 
23 namespace rqt_multiplot {
24 
25 /*****************************************************************************/
26 /* Constructors and Destructor */
27 /*****************************************************************************/
28 
29 UrlItemModel::UrlItemModel(QObject* parent) {
30 }
31 
33  for (QList<UrlItem*>::iterator it = schemeItems_.begin();
34  it != schemeItems_.end(); ++it)
35  delete *it;
36 }
37 
38 /*****************************************************************************/
39 /* Accessors */
40 /*****************************************************************************/
41 
42 QString UrlItemModel::getUrl(const QModelIndex& index) const {
43  if (index.isValid()) {
44  UrlItem* item = static_cast<UrlItem*>(index.internalPointer());
45  UrlScheme* itemScheme = item->getScheme();
46 
47  if (item->getType() == UrlItem::Scheme)
48  return itemScheme->getPrefix()+"://";
49  else if (item->getType() == UrlItem::Host)
50  return itemScheme->getPrefix()+"://"+itemScheme->getHost(
51  item->getIndex());
52  else if (item->getType() == UrlItem::Path) {
53  QModelIndex hostIndex = item->getIndex(UrlItem::Host);
54 
55  return itemScheme->getPrefix()+"://"+itemScheme->getHost(hostIndex)+
56  "/"+itemScheme->getPath(hostIndex, item->getIndex());
57  }
58  }
59 
60  return QString();
61 }
62 
63 QString UrlItemModel::getFilePath(const QModelIndex& index) const {
64  if (index.isValid()) {
65  UrlItem* item = static_cast<UrlItem*>(index.internalPointer());
66  UrlScheme* itemScheme = item->getScheme();
67 
68  if (item->getType() == UrlItem::Host)
69  return itemScheme->getFilePath(item->getIndex(), QModelIndex());
70  else if (item->getType() == UrlItem::Path) {
71  QModelIndex hostIndex = item->getIndex(UrlItem::Host);
72 
73  return itemScheme->getFilePath(hostIndex, item->getIndex());
74  }
75  }
76 
77  return QString();
78 }
79 
80 QString UrlItemModel::getFilePath(const QString& url) const {
81  QStringList urlParts = url.split("://");
82 
83  if (urlParts.count() > 1) {
84  QString prefix = urlParts[0];
85 
86  for (QList<UrlScheme*>::const_iterator it = schemes_.begin();
87  it != schemes_.end(); ++it) {
88  UrlScheme* scheme = *it;
89 
90  if (scheme->getPrefix() == prefix) {
91  QStringList hostPathParts = urlParts[1].split("/");
92  QString host, path;
93 
94  if (hostPathParts.count() > 1) {
95  host = hostPathParts[0];
96  hostPathParts.removeFirst();
97  path = hostPathParts.join("/");
98  }
99  else
100  host = urlParts[1];
101 
102  return scheme->getFilePath(host, path);
103  }
104  }
105  }
106 
107  return QString();
108 }
109 
110 UrlScheme* UrlItemModel::getScheme(const QModelIndex& index) const {
111  if (index.isValid()) {
112  UrlItem* item = static_cast<UrlItem*>(index.internalPointer());
113 
114  if (item)
115  return item->getScheme();
116  }
117 
118  return 0;
119 }
120 
121 /*****************************************************************************/
122 /* Methods */
123 /*****************************************************************************/
124 
126  schemes_.append(scheme);
127  schemeItems_.append(new UrlItem(scheme));
128 
129  connect(scheme, SIGNAL(resetStarted()), this, SLOT(schemeResetStarted()));
130  connect(scheme, SIGNAL(resetFinished()), this, SLOT(schemeResetFinished()));
131  connect(scheme, SIGNAL(pathLoaded(const QString&, const QString&)),
132  this, SLOT(schemePathLoaded(const QString&, const QString&)));
133 }
134 
135 int UrlItemModel::rowCount(const QModelIndex& parent) const {
136  if (parent.column() <= 0) {
137  if (parent.isValid()) {
138  UrlItem* parentItem = static_cast<UrlItem*>(parent.internalPointer());
139  UrlScheme* parentScheme = parentItem->getScheme();
140 
141  if (parentItem->getType() == UrlItem::Scheme) {
142  size_t numHosts = parentScheme->getNumHosts();
143 
144  if (!numHosts)
145  return parentScheme->getNumPaths(QModelIndex());
146  else
147  return numHosts;
148  }
149  else if (parentItem->getType() == UrlItem::Host)
150  return parentScheme->getNumPaths(parentItem->getIndex());
151  else if (parentItem->getType() == UrlItem::Path)
152  return parentScheme->getNumPaths(parentItem->getIndex(
153  UrlItem::Host), parentItem->getIndex());
154  }
155  else
156  return schemes_.count();
157  }
158 
159  return 0;
160 }
161 
162 int UrlItemModel::columnCount(const QModelIndex& parent) const {
163  return 1;
164 }
165 
166 QVariant UrlItemModel::data(const QModelIndex& index, int role) const {
167  if (index.isValid()) {
168  UrlItem* item = static_cast<UrlItem*>(index.internalPointer());
169  UrlScheme* itemScheme = item->getScheme();
170 
171  if (item->getType() == UrlItem::Scheme) {
172  if ((role == Qt::DisplayRole) || (role == Qt::EditRole))
173  return itemScheme->getPrefix()+"://";
174  }
175  else if (item->getType() == UrlItem::Host)
176  return itemScheme->getHostData(item->getIndex(), role);
177  else if (item->getType() == UrlItem::Path)
178  return itemScheme->getPathData(item->getIndex(), role);
179  }
180 
181  return QVariant();
182 }
183 
184 QModelIndex UrlItemModel::index(int row, int column, const QModelIndex& parent)
185  const {
186  if (hasIndex(row, column, parent)) {
187  if (parent.isValid()) {
188  UrlItem* parentItem = static_cast<UrlItem*>(parent.internalPointer());
189  UrlScheme* parentScheme = parentItem->getScheme();
190  UrlItem* childItem = 0;
191 
192  if (parentItem->getType() == UrlItem::Scheme) {
193  size_t numHosts = parentScheme->getNumHosts();
194 
195  if (!numHosts)
196  childItem = parentItem->addChild(row, UrlItem::Path,
197  parentScheme->getPathIndex(QModelIndex(), row));
198  else
199  childItem = parentItem->addChild(row, UrlItem::Host,
200  parentScheme->getHostIndex(row));
201  }
202  else if (parentItem->getType() == UrlItem::Host)
203  childItem = parentItem->addChild(row, UrlItem::Path,
204  parentScheme->getPathIndex(parentItem->getIndex(UrlItem::Host),
205  row));
206  else if (parentItem->getType() == UrlItem::Path)
207  childItem = parentItem->addChild(row, UrlItem::Path,
208  parentScheme->getPathIndex(parentItem->getIndex(UrlItem::Host),
209  row, parentItem->getIndex()));
210 
211  return createIndex(row, column, childItem);
212  }
213  else
214  return createIndex(row, column, schemeItems_[row]);
215  }
216 
217  return QModelIndex();
218 }
219 
220 QModelIndex UrlItemModel::parent(const QModelIndex& index) const {
221  if (index.isValid()) {
222  UrlItem* childItem = static_cast<UrlItem*>(index.internalPointer());
223 
224  if (childItem) {
225  UrlItem* parentItem = childItem->getParent();
226 
227  if (parentItem)
228  return createIndex(parentItem->getRow(), 0, parentItem);
229  }
230  }
231 
232  return QModelIndex();
233 }
234 
235 /*****************************************************************************/
236 /* Slots */
237 /*****************************************************************************/
238 
240  beginResetModel();
241 
242  UrlScheme* scheme = static_cast<UrlScheme*>(sender());
243  int i = schemes_.indexOf(scheme);
244 
245  if (i >= 0) {
246  delete schemeItems_[i];
247  schemeItems_[i] = new UrlItem(scheme);
248  }
249 }
250 
252  endResetModel();
253 }
254 
255 void UrlItemModel::schemePathLoaded(const QString& host, const QString&
256  path) {
257  UrlScheme* scheme = static_cast<UrlScheme*>(sender());
258 
259  QString url = scheme->getPrefix()+"://"+host;
260 
261  if (!path.isEmpty())
262  url += "/"+path;
263 
264  emit urlLoaded(url);
265 }
266 
267 }
void urlLoaded(const QString &url)
QModelIndex index(int row, int column, const QModelIndex &parent) const
UrlItem * getParent() const
Definition: UrlItem.cpp:45
QList< UrlItem * > schemeItems_
Definition: UrlItemModel.h:54
virtual size_t getNumHosts() const =0
UrlItemModel(QObject *parent=0)
UrlScheme * getScheme() const
Definition: UrlItem.cpp:78
virtual QVariant getPathData(const QModelIndex &index, int role) const =0
int getRow() const
Definition: UrlItem.cpp:62
const QString & getPrefix() const
Definition: UrlScheme.cpp:38
virtual QModelIndex getHostIndex(size_t row) const =0
QString getFilePath(const QModelIndex &index) const
const QModelIndex & getIndex() const
Definition: UrlItem.cpp:94
QString getUrl(const QModelIndex &index) const
UrlScheme * getScheme(const QModelIndex &index) const
QList< UrlScheme * > schemes_
Definition: UrlItemModel.h:53
int rowCount(const QModelIndex &parent) const
UrlItem * addChild(size_t row, Type type, const QModelIndex &index)
Definition: UrlItem.cpp:115
virtual QString getPath(const QModelIndex &hostIndex, const QModelIndex &pathIndex) const =0
void schemePathLoaded(const QString &host, const QString &path)
virtual size_t getNumPaths(const QModelIndex &hostIndex, const QModelIndex &parent=QModelIndex()) const =0
virtual QVariant getHostData(const QModelIndex &index, int role) const =0
int columnCount(const QModelIndex &parent) const
virtual QModelIndex getPathIndex(const QModelIndex &hostIndex, size_t row, const QModelIndex &parent=QModelIndex()) const =0
virtual QString getFilePath(const QModelIndex &hostIndex, const QModelIndex &pathIndex) const =0
void addScheme(UrlScheme *scheme)
QVariant data(const QModelIndex &index, int role) const
virtual QString getHost(const QModelIndex &hostIndex) const =0
QModelIndex parent(const QModelIndex &index) const
Type getType() const
Definition: UrlItem.cpp:86


rqt_multiplot_plugin
Author(s): Ralf Kaestner
autogenerated on Fri Jan 15 2021 03:47:53