Wt examples  4.10.4
FolderView.C
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
3  *
4  * See the LICENSE file for terms of use.
5  */
6 #include <iostream>
7 
8 #include <Wt/WAbstractItemModel.h>
9 #include <Wt/WItemSelectionModel.h>
10 #include <Wt/WMessageBox.h>
11 
12 #include "FolderView.h"
13 
15  = "application/x-computers-selection";
16 
18  : WTreeView()
19 {
20  /*
21  * Accept drops for the custom mime type.
22  */
24 }
25 
27  const WModelIndex& target)
28 {
29  /*
30  * We reimplement the drop event to handle the dropping of a
31  * selection of computers.
32  *
33  * The test below would always be true in this case, since we only
34  * indicated support for that particular mime type.
35  */
36  if (event.mimeType() == FileSelectionMimeType) {
37  /*
38  * The source object for a drag of a selection from a WTreeView is
39  * a WItemSelectionModel.
40  */
41  WItemSelectionModel *selection
42  = dynamic_cast<WItemSelectionModel *>(event.source());
43 
44 #ifdef WT_THREADED
45  StandardButton result = WMessageBox::show
46  ("Drop event",
47  "Move "
48  + asString(selection->selectedIndexes().size())
49  + " files to folder '"
50  + cpp17::any_cast<WString>(target.data(ItemDataRole::Display)).toUTF8()
51  + "' ?",
52  StandardButton::Yes | StandardButton::No);
53 #else
54  StandardButton result = StandardButton::Yes;
55 #endif
56 
57  if (result == StandardButton::Yes) {
58  /*
59  * You can access the source model from the selection and
60  * manipulate it.
61  */
62  std::shared_ptr<WAbstractItemModel> sourceModel = selection->model();
63 
64  WModelIndexSet toChange = selection->selectedIndexes();
65 
66  for (WModelIndexSet::reverse_iterator i = toChange.rbegin();
67  i != toChange.rend(); ++i) {
68  WModelIndex index = *i;
69 
70  /*
71  * Copy target folder to file. Since we are using a
72  * dynamic WSortFilterProxyModel that filters on folder, this
73  * will also result in the removal of the file from the
74  * current view.
75  */
76  std::map<ItemDataRole, cpp17::any> data = model()->itemData(target);
77  data[ItemDataRole::Decoration] = index.data(ItemDataRole::Decoration);
78  sourceModel->setItemData(index, data);
79  }
80  }
81  }
82 }
virtual void dropEvent(const WDropEvent &event, const WModelIndex &target)
Drop event.
Definition: FolderView.C:26
FolderView()
Constructor.
Definition: FolderView.C:17
static const char * FileSelectionMimeType
Constant that indicates the mime type for a selection of files.
Definition: FolderView.h:28
std::shared_ptr< WAbstractItemModel > model() const
const std::string & mimeType() const
WModelIndexSet selectedIndexes() const
std::shared_ptr< WAbstractItemModel > model() const
cpp17::any data(ItemDataRole role=ItemDataRole::Display) const
std::string toUTF8() const
virtual void acceptDrops(const std::string &mimeType, const WString &hoverStyleClass=WString())
WString asString(const cpp17::any &v, const WString &formatString=WString())
std::set< WModelIndex > WModelIndexSet
StandardButton