Wt examples: /home/koen/project/wt/public-git/wt/examples/treeview-dragdrop/FolderView.C Source File

Wt examples 3.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

/home/koen/project/wt/public-git/wt/examples/treeview-dragdrop/FolderView.C
Go to the documentation of this file.

    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 #ifdef WT_THREADED
    00047     int result = WMessageBox::show
    00048       ("Drop event",
    00049        "Move "
    00050        + boost::lexical_cast<std::string>(selection->selectedIndexes().size())
    00051        + " files to folder '"
    00052        + boost::any_cast<WString>(target.data(DisplayRole)).toUTF8()
    00053        + "' ?",
    00054        Yes | No);
    00055 #else
    00056     int result = Yes;
    00057 #endif
    00058
    00059     if (result == Yes) {
    00060       /*
    00061        * You can access the source model from the selection and
    00062        * manipulate it.
    00063        */
    00064       WAbstractItemModel *sourceModel = selection->model();
    00065
    00066       WModelIndexSet toChange = selection->selectedIndexes();
    00067
    00068       for (WModelIndexSet::reverse_iterator i = toChange.rbegin();
    00069            i != toChange.rend(); ++i) {
    00070         WModelIndex index = *i;
    00071
    00072         /*
    00073          * Copy target folder to file. Since we are using a
    00074          * dynamic WSortFilterProxyModel that filters on folder, this
    00075          * will also result in the removal of the file from the
    00076          * current view.
    00077          */
    00078         std::map<int, boost::any> data = model()->itemData(target);
    00079         data[DecorationRole] = index.data(DecorationRole);
    00080         sourceModel->setItemData(index, data);
    00081       }
    00082     }
    00083   }
    00084 }