LeechCraft  0.6.70-11552-gf61ee51c3d
Modular cross-platform feature rich live environment.
flattofoldersproxymodel.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
31 #include <QSet>
32 #include <QMimeData>
33 #include <QItemSelectionRange>
34 #include <util/sll/prelude.h>
35 #include <interfaces/iinfo.h>
37 
38 namespace LeechCraft
39 {
40  struct FlatTreeItem
41  {
44 
45  enum class Type
46  {
47  Root,
48  Folder,
49  Item
50  };
51 
53 
54  QPersistentModelIndex Index_;
55  QString Tag_;
56 
57  int Row () const
58  {
59  if (Parent_)
60  {
61  const auto& c = Parent_->C_;
62  for (int i = 0, size = c.size (); i < size; ++i)
63  if (c.at (i).get () == this)
64  return i;
65  }
66  return 0;
67  }
68  };
69 
70  FlatTreeItem* ToFlat (const QModelIndex& idx)
71  {
72  return static_cast<FlatTreeItem*> (idx.internalPointer ());
73  }
74 
75  namespace Util
76  {
78  : QAbstractItemModel { parent }
79  , Root_ { std::make_shared<FlatTreeItem> () }
80  {
81  Root_->Type_ = FlatTreeItem::Type::Root;
82  }
83 
85  {
86  beginResetModel ();
87  TM_ = tm;
88  endResetModel ();
89  }
90 
91  int FlatToFoldersProxyModel::columnCount (const QModelIndex&) const
92  {
93  return SourceModel_ ?
94  SourceModel_->columnCount (QModelIndex ()) :
95  0;
96  }
97 
98  QVariant FlatToFoldersProxyModel::data (const QModelIndex& index, int role) const
99  {
100  FlatTreeItem *fti = ToFlat (index);
101  if (fti->Type_ == FlatTreeItem::Type::Item)
102  {
103  QModelIndex source = fti->Index_;
104  return source.sibling (source.row (), index.column ()).data (role);
105  }
106  else if (fti->Type_ == FlatTreeItem::Type::Folder &&
107  index.column () == 0)
108  {
109  if (role == Qt::DisplayRole)
110  {
111  if (fti->Tag_.isEmpty ())
112  return tr ("untagged");
113 
114  QString ut = TM_->GetTag (fti->Tag_);
115  if (ut.isEmpty ())
116  return tr ("<unknown tag>");
117  else
118  return ut;
119  }
120  else if (role == RoleTags)
121  return fti->Tag_;
122  else
123  return QVariant ();
124  }
125  else
126  return QVariant ();
127  }
128 
129  QVariant FlatToFoldersProxyModel::headerData (int section,
130  Qt::Orientation orient, int role) const
131  {
132  if (SourceModel_)
133  return SourceModel_->headerData (section, orient, role);
134  else
135  return QVariant ();
136  }
137 
138  Qt::ItemFlags FlatToFoldersProxyModel::flags (const QModelIndex& index) const
139  {
140  auto fti = ToFlat (index);
141  if (fti && fti->Type_ == FlatTreeItem::Type::Item)
142  return fti->Index_.flags ();
143  else
144  return Qt::ItemIsSelectable |
145  Qt::ItemIsEnabled |
146  Qt::ItemIsDragEnabled |
147  Qt::ItemIsDropEnabled;
148  }
149 
150  QModelIndex FlatToFoldersProxyModel::index (int row, int column,
151  const QModelIndex& parent) const
152  {
153  if (!hasIndex (row, column, parent))
154  return QModelIndex ();
155 
156  FlatTreeItem *fti = 0;
157  if (parent.isValid ())
158  fti = ToFlat (parent);
159  else
160  fti = Root_.get ();
161 
162  if (fti->Type_ == FlatTreeItem::Type::Item)
163  return QModelIndex ();
164  else
165  return createIndex (row, column, fti->C_.at (row).get ());
166  }
167 
168  QModelIndex FlatToFoldersProxyModel::parent (const QModelIndex& index) const
169  {
170  FlatTreeItem *fti = 0;
171  if (index.isValid ())
172  fti = ToFlat (index);
173  else
174  fti = Root_.get ();
175 
177  parent = fti->Parent_;
178 
179  if (parent &&
181  return createIndex (parent->Row (), 0, parent.get ());
182  else
183  return QModelIndex ();
184  }
185 
186  int FlatToFoldersProxyModel::rowCount (const QModelIndex& index) const
187  {
188  if (index.isValid ())
189  return ToFlat (index)->C_.size ();
190  else
191  return Root_->C_.size ();
192  }
193 
195  {
196  return SourceModel_ ?
197  SourceModel_->supportedDropActions () :
198  QAbstractItemModel::supportedDropActions ();
199  }
200 
202  {
203  return SourceModel_ ?
204  SourceModel_->mimeTypes () :
205  QAbstractItemModel::mimeTypes ();
206  }
207 
208  QMimeData* FlatToFoldersProxyModel::mimeData (const QModelIndexList& indexes) const
209  {
210  if (!SourceModel_)
211  return QAbstractItemModel::mimeData (indexes);
212 
213  QModelIndexList sourceIdxs;
214  for (const auto& index : indexes)
215  {
216  auto item = static_cast<FlatTreeItem*> (index.internalPointer ());
217  switch (item->Type_)
218  {
220  sourceIdxs << MapToSource (index);
221  break;
223  for (const auto& subItem : item->C_)
224  sourceIdxs << subItem->Index_;
225  break;
226  default:
227  break;
228  }
229  }
230 
231  return SourceModel_->mimeData (sourceIdxs);
232  }
233 
234  bool FlatToFoldersProxyModel::dropMimeData (const QMimeData* data, Qt::DropAction action, int, int, const QModelIndex& parent)
235  {
236  if (!SourceModel_)
237  return false;
238 
239  QMimeData modified;
240  for (const auto& format : data->formats ())
241  modified.setData (format, data->data (format));
242 
243  if (auto ptr = static_cast<FlatTreeItem*> (parent.internalPointer ()))
244  {
245  switch (ptr->Type_)
246  {
249  modified.setData ("x-leechcraft/tag", ptr->Tag_.toLatin1 ());
250  break;
251  default:
252  break;
253  }
254  }
255 
256  return SourceModel_->dropMimeData (&modified, action, -1, -1, QModelIndex ());
257  }
258 
259  void FlatToFoldersProxyModel::SetSourceModel (QAbstractItemModel *model)
260  {
261  if (SourceModel_)
262  disconnect (SourceModel_,
263  0,
264  this,
265  0);
266 
267  SourceModel_ = model;
268 
269  if (model)
270  {
271  // We don't support changing columns (yet) so don't connect
272  // to columns* signals.
273  connect (model,
274  SIGNAL (headerDataChanged (Qt::Orientation, int, int)),
275  this,
276  SIGNAL (headerDataChanged (Qt::Orientation, int, int)));
277  connect (model,
278  SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)),
279  this,
280  SLOT (handleDataChanged (const QModelIndex&, const QModelIndex&)));
281  connect (model,
282  SIGNAL (layoutAboutToBeChanged ()),
283  this,
284  SIGNAL (layoutAboutToBeChanged ()));
285  connect (model,
286  SIGNAL (layoutChanged ()),
287  this,
288  SIGNAL (layoutChanged ()));
289  connect (model,
290  SIGNAL (modelReset ()),
291  this,
292  SLOT (handleModelReset ()));
293  connect (model,
294  SIGNAL (rowsInserted (const QModelIndex&,
295  int, int)),
296  this,
297  SLOT (handleRowsInserted (const QModelIndex&,
298  int, int)));
299  connect (model,
300  SIGNAL (rowsAboutToBeRemoved (const QModelIndex&,
301  int, int)),
302  this,
303  SLOT (handleRowsAboutToBeRemoved (const QModelIndex&,
304  int, int)));
305  }
306 
307  handleModelReset ();
308  }
309 
310  QAbstractItemModel* FlatToFoldersProxyModel::GetSourceModel () const
311  {
312  return SourceModel_;
313  }
314 
315  QModelIndex FlatToFoldersProxyModel::MapToSource (const QModelIndex& proxy) const
316  {
317  if (!GetSourceModel ())
318  return {};
319 
320  if (!proxy.isValid ())
321  return {};
322 
323  const auto item = ToFlat (proxy);
324 
325  if (item->Type_ != FlatTreeItem::Type::Item)
326  return {};
327 
328  return item->Index_;
329  }
330 
332  {
333  auto tags = source.data (RoleTags).toStringList ();
334  if (tags.isEmpty ())
335  tags << QString ();
336 
337  QList<QModelIndex> result;
338  for (const auto& tag : tags)
339  {
340  const auto& folder = FindFolder (tag);
341  if (!folder)
342  {
343  qWarning () << Q_FUNC_INFO
344  << "could not find folder for tag"
345  << tag
346  << GetSourceModel ();
347  continue;
348  }
349 
350  const auto& folderIdx = index (folder->Row (), 0, {});
351 
352  for (int i = 0; i < folder->C_.size (); ++i)
353  {
354  const auto& child = folder->C_.at (i);
355  if (child->Index_ != source)
356  continue;
357 
358  result << index (i, 0, folderIdx);
359  break;
360  }
361  }
362  return result;
363  }
364 
365  FlatTreeItem_ptr FlatToFoldersProxyModel::FindFolder (const QString& tag) const
366  {
367  for (const auto& item : Root_->C_)
368  if (item->Tag_ == tag)
369  return item;
370 
371  return {};
372  }
373 
374  FlatTreeItem_ptr FlatToFoldersProxyModel::GetFolder (const QString& tag)
375  {
376  auto& c = Root_->C_;
377  for (const auto& item : c)
378  if (item->Tag_ == tag)
379  return item;
380 
381  const auto& item = std::make_shared<FlatTreeItem> ();
382  item->Type_ = FlatTreeItem::Type::Folder;
383  item->Tag_ = tag;
384  item->Parent_ = Root_;
385 
386  int size = c.size ();
387  beginInsertRows (QModelIndex (), size, size);
388  c.append (item);
389  endInsertRows ();
390 
391  return item;
392  }
393 
394  void FlatToFoldersProxyModel::HandleRowInserted (int i)
395  {
396  QModelIndex idx = SourceModel_->index (i, 0);
397 
398  QStringList tags = idx.data (RoleTags).toStringList ();
399 
400  if (tags.isEmpty ())
401  tags << QString ();
402 
403  QPersistentModelIndex pidx (idx);
404 
405  for (auto tag : tags)
406  AddForTag (tag, pidx);
407  }
408 
409  void FlatToFoldersProxyModel::HandleRowRemoved (int i)
410  {
411  QAbstractItemModel *model = SourceModel_;
412  QModelIndex idx = model->index (i, 0);
413 
414  QStringList tags = idx.data (RoleTags).toStringList ();
415 
416  if (tags.isEmpty ())
417  tags << QString ();
418 
419  QPersistentModelIndex pidx (idx);
420 
421  for (const auto tag : tags)
422  RemoveFromTag (tag, pidx);
423  }
424 
425  void FlatToFoldersProxyModel::AddForTag (const QString& tag,
426  const QPersistentModelIndex& pidx)
427  {
428  FlatTreeItem_ptr folder = GetFolder (tag);
429 
430  const auto& item = std::make_shared<FlatTreeItem> ();
431  item->Type_ = FlatTreeItem::Type::Item;
432  item->Index_ = pidx;
433  item->Parent_ = folder;
434  item->Tag_ = tag;
435 
436  int size = folder->C_.size ();
437  QModelIndex iidx = index (Root_->C_.indexOf (folder), 0);
438  beginInsertRows (iidx, size, size);
439  folder->C_.append (item);
440  Items_.insert (pidx, item);
441  endInsertRows ();
442  }
443 
444  void FlatToFoldersProxyModel::RemoveFromTag (const QString& tag,
445  const QPersistentModelIndex& pidx)
446  {
447  const auto& folder = GetFolder (tag);
448  auto& c = folder->C_;
449  int findex = Root_->C_.indexOf (folder);
450  for (int i = 0, size = c.size ();
451  i < size; ++i)
452  {
453  if (c.at (i)->Index_ != pidx)
454  continue;
455 
456  beginRemoveRows (index (findex, 0), i, i);
457  Items_.remove (pidx, c.at (i));
458  c.removeAt (i);
459  endRemoveRows ();
460  break;
461  }
462 
463  if (c.isEmpty ())
464  {
465  beginRemoveRows (QModelIndex (), findex, findex);
466  Root_->C_.removeAt (findex);
467  endRemoveRows ();
468  }
469  }
470 
471  void FlatToFoldersProxyModel::HandleChanged (const QModelIndex& idx)
472  {
473  QSet<QString> newTags = QSet<QString>::fromList (idx.data (RoleTags).toStringList ());
474  if (newTags.isEmpty ())
475  newTags << QString ();
476 
477  QPersistentModelIndex pidx (idx);
478 
479  const auto& oldTags = Util::MapAs<QSet> (Items_.values (pidx), [] (const auto& item) { return item->Tag_; });
480 
481  const auto added = QSet<QString> (newTags).subtract (oldTags);
482  const auto removed = QSet<QString> (oldTags).subtract (newTags);
483  const auto changed = QSet<QString> (newTags).intersect (oldTags);
484 
485  for (const auto& ch : changed)
486  {
487  FlatTreeItem_ptr folder = GetFolder (ch);
488 
489  QList<FlatTreeItem_ptr>& c = folder->C_;
490  int findex = Root_->C_.indexOf (folder);
491  QModelIndex fmi = index (findex, 0);
492  for (int i = 0, size = c.size ();
493  i < size; ++i)
494  {
495  if (c.at (i)->Index_ != pidx)
496  continue;
497 
498  emit dataChanged (index (i, 0, fmi),
499  index (i, columnCount () - 1, fmi));
500  break;
501  }
502  }
503 
504  for (const auto& rem : removed)
505  RemoveFromTag (rem, pidx);
506 
507  for (const auto& add : added)
508  AddForTag (add, pidx);
509  }
510 
511  void FlatToFoldersProxyModel::handleDataChanged (const QModelIndex& topLeft,
512  const QModelIndex& bottomRight)
513  {
514  QItemSelectionRange range (topLeft.sibling (topLeft.row (), 0),
515  bottomRight.sibling (bottomRight.row (), 0));
516  QModelIndexList indexes = range.indexes ();
517  for (int i = 0, size = indexes.size ();
518  i < size; ++i)
519  HandleChanged (indexes.at (i));
520  }
521 
522  void FlatToFoldersProxyModel::handleModelReset ()
523  {
524  if (const int size = Root_->C_.size ())
525  {
526  beginRemoveRows (QModelIndex (), 0, size - 1);
527  Root_->C_.clear ();
528  Items_.clear ();
529  endRemoveRows ();
530  }
531 
532  if (SourceModel_)
533  {
534  for (int i = 0, size = SourceModel_->rowCount ();
535  i < size; ++i)
536  HandleRowInserted (i);
537  }
538  }
539 
540  void FlatToFoldersProxyModel::handleRowsInserted (const QModelIndex&,
541  int start, int end)
542  {
543  for (int i = start; i <= end; ++i)
544  HandleRowInserted (i);
545  }
546 
547  void FlatToFoldersProxyModel::handleRowsAboutToBeRemoved (const QModelIndex&,
548  int start, int end)
549  {
550  for (int i = start; i <= end; ++i)
551  HandleRowRemoved (i);
552  }
553  };
554 };
555 
QMimeData * mimeData(const QModelIndexList &indexes) const override
QList< QModelIndex > MapFromSource(const QModelIndex &) const
virtual QString GetTag(tag_id id) const =0
Returns the tag with the given id.
QVariant headerData(int, Qt::Orientation, int) const override
std::shared_ptr< FlatTreeItem > FlatTreeItem_ptr
QModelIndex parent(const QModelIndex &) const override
QList< FlatTreeItem_ptr > C_
Tags manager&#39;s interface.
Definition: itagsmanager.h:43
QModelIndex MapToSource(const QModelIndex &) const
QVariant data(const QModelIndex &, int=Qt::DisplayRole) const override
FlatTreeItem * ToFlat(const QModelIndex &idx)
int columnCount(const QModelIndex &={}) const override
Qt::ItemFlags flags(const QModelIndex &) const override
Qt::DropActions supportedDropActions() const override
int rowCount(const QModelIndex &={}) const override
QModelIndex index(int, int, const QModelIndex &={}) const override
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override