TreeViewDragDrop Class Reference
[Drag and drop in WTreeView example]

Main application class. More...

Inheritance diagram for TreeViewDragDrop:

Inheritance graph
[legend]

List of all members.

Public Member Functions

 TreeViewDragDrop (const WEnvironment &env)
 Constructor.

Private Member Functions

void createUI ()
 Setup the user interface.
WTextcreateTitle (const WString &title)
 Creates a title widget.
WTreeViewfolderView ()
 Creates the folder WTreeView.
WTreeViewfileView ()
 Creates the file table view (also a WTreeView).
WWidgetaboutDisplay ()
 Creates the hints text.
void folderChanged ()
 Change the filter on the file view when the selected folder changes.
void showPopup (const WModelIndex &item, const WMouseEvent &event)
 Show a popup for a folder item.
void populateFiles ()
 Populate the files model.
void convertToDate (WStandardItem *item)
 Convert a string to a date.
void populateFolders ()
 Populate the folders model.
WStandardItemcreateFolderItem (const WString &location, const std::string &folderId=std::string())
 Create a folder item.

Private Attributes

WStandardItemModelfolderModel_
 The folder model (used by folderView_).
WStandardItemModelfileModel_
 The file model (used by fileView_).
WSortFilterProxyModelfileFilterModel_
 The sort filter proxy model that adapts fileModel_.
std::map< std::string, WStringfolderNameMap_
 Maps folder id's to folder descriptions.
WTreeViewfolderView_
 The folder view.
WTreeViewfileView_
 The file view.


Detailed Description

Main application class.

Definition at line 234 of file TreeViewDragDrop.C.


Constructor & Destructor Documentation

TreeViewDragDrop::TreeViewDragDrop ( const WEnvironment env  )  [inline]

Constructor.

Definition at line 239 of file TreeViewDragDrop.C.

00240     : WApplication(env) {
00241 
00242     /*
00243      * Create the data models.
00244      */
00245     folderModel_ = new WStandardItemModel(0, 1, this);
00246     populateFolders();
00247 
00248     fileModel_ = new FileModel(this);
00249     populateFiles();
00250 
00251     fileFilterModel_ = new WSortFilterProxyModel(this);
00252     fileFilterModel_->setSourceModel(fileModel_);
00253     fileFilterModel_->setDynamicSortFilter(true);
00254     fileFilterModel_->setFilterKeyColumn(0);
00255     fileFilterModel_->setFilterRole(UserRole);
00256 
00257     /*
00258      * Setup the user interface.
00259      */
00260     createUI();
00261 
00262     /*
00263      * Select the first folder
00264      */
00265     folderView_->select
00266       (folderModel_->index(0, 0, folderModel_->index(0, 0)));
00267   }


Member Function Documentation

void TreeViewDragDrop::createUI (  )  [inline, private]

Setup the user interface.

Definition at line 290 of file TreeViewDragDrop.C.

00290                   {
00291     WContainerWidget *w = root();
00292     w->setStyleClass("maindiv");
00293 
00294     /*
00295      * The main layout is a 3x2 grid layout.
00296      */
00297     WGridLayout *layout = new WGridLayout();
00298     layout->addWidget(createTitle("Folders"), 0, 0);
00299     layout->addWidget(createTitle("Files"), 0, 1);
00300     layout->addWidget(folderView(), 1, 0);
00301     layout->addWidget(fileView(), 1, 1);
00302 
00303     layout->addWidget(aboutDisplay(), 2, 0, 1, 2, AlignTop);
00304 
00305     /*
00306      * Let row 1 and column 1 take the excess space.
00307      */
00308     layout->setRowStretch(1, 1);
00309     layout->setColumnStretch(1, 1);
00310 
00311     w->setLayout(layout);
00312   }

WText* TreeViewDragDrop::createTitle ( const WString title  )  [inline, private]

Creates a title widget.

Definition at line 316 of file TreeViewDragDrop.C.

00316                                            {
00317     WText *result = new WText(title);
00318     result->setInline(false);
00319     result->setStyleClass("title");
00320 
00321     return result;
00322   }

WTreeView* TreeViewDragDrop::folderView (  )  [inline, private]

Creates the folder WTreeView.

Definition at line 326 of file TreeViewDragDrop.C.

00326                           {
00327     WTreeView *treeView = new FolderView();
00328 
00329     /*
00330      * To support right-click, we need to disable the built-in browser
00331      * context menu.
00332      *
00333      * Note that disabling the context menu and catching the
00334      * right-click does not work reliably on all browsers.
00335      */
00336     treeView->setAttributeValue
00337       ("oncontextmenu",
00338        "event.cancelBubble = true; event.returnValue = false; return false;");
00339     treeView->setModel(folderModel_);
00340     treeView->resize(200, WLength::Auto);
00341     treeView->setSelectionMode(SingleSelection);
00342     treeView->expandToDepth(1);
00343     treeView->selectionChanged.connect(SLOT(this,
00344                                             TreeViewDragDrop::folderChanged));
00345 
00346     treeView->mouseWentDown.connect(SLOT(this, TreeViewDragDrop::showPopup));
00347 
00348     folderView_ = treeView;
00349 
00350     return treeView;
00351   }

WTreeView* TreeViewDragDrop::fileView (  )  [inline, private]

Creates the file table view (also a WTreeView).

Definition at line 355 of file TreeViewDragDrop.C.

00355                         {
00356     WTreeView *treeView = new FileView();
00357 
00358     // Hide the tree-like decoration on the first column, to make it
00359     // resemble a plain table
00360     treeView->setRootIsDecorated(false);
00361 
00362     treeView->setModel(fileFilterModel_);
00363     treeView->setSelectionMode(ExtendedSelection);
00364     treeView->setDragEnabled(true);
00365 
00366     treeView->setColumnWidth(0, 100);
00367     treeView->setColumnWidth(1, 150);
00368     treeView->setColumnWidth(2, 100);
00369     treeView->setColumnWidth(3, 80);
00370     treeView->setColumnWidth(4, 120);
00371     treeView->setColumnWidth(5, 120);
00372 
00373     treeView->setColumnFormat(4, FileModel::dateDisplayFormat);
00374     treeView->setColumnFormat(5, FileModel::dateDisplayFormat);
00375 
00376     treeView->setColumnAlignment(3, AlignRight);
00377     treeView->setColumnAlignment(4, AlignRight);
00378     treeView->setColumnAlignment(5, AlignRight);
00379 
00380     treeView->sortByColumn(1, AscendingOrder);
00381 
00382     fileView_ = treeView;
00383 
00384     return treeView;
00385   }

WWidget* TreeViewDragDrop::aboutDisplay (  )  [inline, private]

Creates the hints text.

Definition at line 389 of file TreeViewDragDrop.C.

00389                           {
00390     WText *result = new WText(WString::tr("about-text"));
00391     result->setStyleClass("about");
00392     return result;
00393   }

void TreeViewDragDrop::folderChanged (  )  [inline, private]

Change the filter on the file view when the selected folder changes.

Definition at line 398 of file TreeViewDragDrop.C.

00398                        {
00399     if (folderView_->selectedIndexes().empty())
00400       return;
00401 
00402     WModelIndex selected = *folderView_->selectedIndexes().begin();
00403     boost::any d = selected.data(UserRole);
00404     if (!d.empty()) {
00405       std::string folder = boost::any_cast<std::string>(d);
00406 
00407       // For simplicity, we assume here that the folder-id does not
00408       // contain special regexp characters, otherwise these need to be
00409       // escaped -- or use the \Q \E qutoing escape regular expression
00410       // syntax (and escape \E)
00411       fileFilterModel_->setFilterRegExp(folder);
00412     }
00413   }

void TreeViewDragDrop::showPopup ( const WModelIndex item,
const WMouseEvent event 
) [inline, private]

Show a popup for a folder item.

Definition at line 417 of file TreeViewDragDrop.C.

00417                                                                     {
00418     if (event.button() == WMouseEvent::RightButton) {
00419 
00420       // Select the item, it was not yet selected.
00421       folderView_->select(item);
00422 
00423       WPopupMenu popup;
00424       popup.addItem("icons/folder_new.gif", "Create a New Folder");
00425       popup.addItem("Rename this Folder")->setCheckable(true);
00426       popup.addItem("Delete this Folder");
00427       popup.addSeparator();
00428       popup.addItem("Folder Details");
00429       popup.addSeparator();
00430       popup.addItem("Application Inventory");
00431       popup.addItem("Hardware Inventory");
00432       popup.addSeparator();
00433 
00434       WPopupMenu *subMenu = new WPopupMenu();
00435       subMenu->addItem("Sub Item 1");
00436       subMenu->addItem("Sub Item 2");
00437       popup.addMenu("File Deployments", subMenu);
00438 
00439       /*
00440        * This is one method of executing a popup, i.e. by using a reentrant
00441        * event loop (blocking the current thread).
00442        *
00443        * Alternatively you could call WPopupMenu::popup(), listen for
00444        * to the WPopupMenu::aboutToHide signal, and check the
00445        * WPopupMenu::result()
00446        */
00447       WPopupMenuItem *item = popup.exec(event);
00448 
00449       if (item) {
00450         /*
00451          * You may bind extra data to an item using setData() and
00452          * check here for the action asked.
00453          */
00454         WMessageBox::show("Sorry.",
00455                           "Action '" + item->text() + "' is not implemented.",
00456                           Ok);
00457       }
00458     }
00459   }

void TreeViewDragDrop::populateFiles (  )  [inline, private]

Populate the files model.

Data (and headers) is read from the CSV file data/files.csv. We add icons to the first column, resolve the folder id to the actual folder name, and configure item flags, and parse date values.

Definition at line 468 of file TreeViewDragDrop.C.

00468                        {
00469     fileModel_->invisibleRootItem()->setRowCount(0);
00470 
00471     std::ifstream f("data/files.csv");
00472     readFromCsv(f, fileModel_);
00473 
00474     for (int i = 0; i < fileModel_->rowCount(); ++i) {
00475       WStandardItem *item = fileModel_->item(i, 0);
00476       item->setFlags(item->flags() | ItemIsDragEnabled);
00477       item->setIcon("icons/file.gif");
00478 
00479       std::string folderId = item->text().toUTF8();
00480 
00481       item->setData(boost::any(folderId), UserRole);
00482       item->setText(folderNameMap_[folderId]);
00483 
00484       convertToDate(fileModel_->item(i, 4));
00485       convertToDate(fileModel_->item(i, 5));
00486     }
00487   }

void TreeViewDragDrop::convertToDate ( WStandardItem item  )  [inline, private]

Convert a string to a date.

Definition at line 491 of file TreeViewDragDrop.C.

00491                                           {
00492     WDate d = WDate::fromString(item->text(), FileModel::dateEditFormat);
00493     item->setData(boost::any(d), DisplayRole);
00494   }

void TreeViewDragDrop::populateFolders (  )  [inline, private]

Populate the folders model.

Definition at line 498 of file TreeViewDragDrop.C.

00498                          {
00499     WStandardItem *level1, *level2;
00500 
00501     folderModel_->appendRow(level1 = createFolderItem("San Fransisco"));
00502     level1->appendRow(level2 = createFolderItem("Investors", "sf-investors"));
00503     level1->appendRow(level2 = createFolderItem("Fellows", "sf-fellows"));
00504 
00505     folderModel_->appendRow(level1 = createFolderItem("Sophia Antipolis"));
00506     level1->appendRow(level2 = createFolderItem("R&D", "sa-r_d"));
00507     level1->appendRow(level2 = createFolderItem("Services", "sa-services"));
00508     level1->appendRow(level2 = createFolderItem("Support", "sa-support"));
00509     level1->appendRow(level2 = createFolderItem("Billing", "sa-billing"));
00510 
00511     folderModel_->appendRow(level1 = createFolderItem("New York"));
00512     level1->appendRow(level2 = createFolderItem("Marketing", "ny-marketing"));
00513     level1->appendRow(level2 = createFolderItem("Sales", "ny-sales"));
00514     level1->appendRow(level2 = createFolderItem("Advisors", "ny-advisors"));
00515 
00516     folderModel_->appendRow(level1 = createFolderItem
00517                              (WString::fromUTF8("Frankfürt")));
00518     level1->appendRow(level2 = createFolderItem("Sales", "frank-sales"));
00519 
00520     folderModel_->setHeaderData(0, Horizontal,
00521                                  boost::any(std::string("SandBox")));
00522   }

WStandardItem* TreeViewDragDrop::createFolderItem ( const WString location,
const std::string &  folderId = std::string() 
) [inline, private]

Create a folder item.

Configures flags for drag and drop support.

Definition at line 528 of file TreeViewDragDrop.C.

00530   {
00531     WStandardItem *result = new WStandardItem(location);
00532 
00533     if (!folderId.empty()) {
00534       result->setData(boost::any(folderId));
00535       result->setFlags(result->flags() | ItemIsDropEnabled);
00536       folderNameMap_[folderId] = location;
00537     } else
00538       result->setFlags(result->flags() & ~ItemIsSelectable);
00539 
00540     result->setIcon("icons/folder.gif");
00541 
00542     return result;
00543   }


Member Data Documentation

The folder model (used by folderView_).

Definition at line 271 of file TreeViewDragDrop.C.

The file model (used by fileView_).

Definition at line 274 of file TreeViewDragDrop.C.

The sort filter proxy model that adapts fileModel_.

Definition at line 277 of file TreeViewDragDrop.C.

std::map<std::string, WString> TreeViewDragDrop::folderNameMap_ [private]

Maps folder id's to folder descriptions.

Definition at line 280 of file TreeViewDragDrop.C.

The folder view.

Definition at line 283 of file TreeViewDragDrop.C.

The file view.

Definition at line 286 of file TreeViewDragDrop.C.


The documentation for this class was generated from the following file:

Generated on Mon Jan 26 14:14:23 2009 for Wt by doxygen 1.5.6