00001 /* 00002 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium. 00003 * 00004 * See the LICENSE file for terms of use. 00005 */ 00006 #include <iostream> 00007 00008 #include <Wt/WAbstractItemModel> 00009 #include <Wt/WItemSelectionModel> 00010 #include <Wt/WMessageBox> 00011 00012 #include "FolderView.h" 00013 00014 using namespace Wt; 00015 00016 const char *FolderView::FileSelectionMimeType 00017 = "application/x-computers-selection"; 00018 00019 FolderView::FolderView(Wt::WContainerWidget *parent) 00020 : WTreeView(parent) 00021 { 00022 /* 00023 * Accept drops for the custom mime type. 00024 */ 00025 acceptDrops(FileSelectionMimeType); 00026 } 00027 00028 void FolderView::dropEvent(const Wt::WDropEvent& event, 00029 const Wt::WModelIndex& target) 00030 { 00031 /* 00032 * We reimplement the drop event to handle the dropping of a 00033 * selection of computers. 00034 * 00035 * The test below would always be true in this case, since we only 00036 * indicated support for that particular mime type. 00037 */ 00038 if (event.mimeType() == FileSelectionMimeType) { 00039 /* 00040 * The source object for a drag of a selection from a WTreeView is 00041 * a WItemSelectionModel. 00042 */ 00043 WItemSelectionModel *selection 00044 = dynamic_cast<WItemSelectionModel *>(event.source()); 00045 00046 int result = WMessageBox::show 00047 ("Drop event", 00048 "Move " 00049 + boost::lexical_cast<std::string>(selection->selectedIndexes().size()) 00050 + " files to folder '" 00051 + boost::any_cast<WString>(target.data(DisplayRole)).toUTF8() 00052 + "' ?", 00053 Yes | No); 00054 00055 if (result == Yes) { 00056 /* 00057 * You can access the source model from the selection and 00058 * manipulate it. 00059 */ 00060 WAbstractItemModel *sourceModel = selection->model(); 00061 00062 WModelIndexSet toChange = selection->selectedIndexes(); 00063 00064 for (WModelIndexSet::const_reverse_iterator i = toChange.rbegin(); 00065 i != toChange.rend(); ++i) { 00066 WModelIndex index = *i; 00067 00068 /* 00069 * Copy target folder to file. Since we are using a 00070 * dynamic WSortFilterProxyModel that filters on folder, this 00071 * will also result in the removal of the file from the 00072 * current view. 00073 */ 00074 std::map<int, boost::any> data = model()->itemData(target); 00075 data[DecorationRole] = index.data(DecorationRole); 00076 sourceModel->setItemData(index, data); 00077 } 00078 } 00079 } 00080 }