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

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13