TreeViewDragDrop Class Reference
[Drag and drop in WTreeView example]

Main application class. More...

Inherits Wt::WApplication.

List of all members.

Public Member Functions

 TreeViewDragDrop (const WEnvironment &env)
 Constructor.
virtual ~TreeViewDragDrop ()

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).
void editFile (const WModelIndex &item)
 Edit a particular row.
WWidgetpieChart ()
 Creates the chart.
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 popupAction ()
 Process the result of the popup menu.
void dialogDone ()
 Process the result of the message box.
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.
WPopupMenupopup_
 Popup menu on the folder view.
WMessageBoxpopupActionBox_
 Message box to confirm the poup menu action.

Detailed Description

Main application class.

Definition at line 235 of file TreeViewDragDrop.C.


Constructor & Destructor Documentation

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

Constructor.

Definition at line 240 of file TreeViewDragDrop.C.

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

virtual TreeViewDragDrop::~TreeViewDragDrop (  )  [inline, virtual]

Definition at line 268 of file TreeViewDragDrop.C.

00268                               {
00269     delete popup_;
00270     delete popupActionBox_;
00271   }


Member Function Documentation

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

Creates the hints text.

Definition at line 439 of file TreeViewDragDrop.C.

00439                           {
00440     WText *result = new WText(WString::tr("about-text"));
00441     result->setStyleClass("about");
00442     return result;
00443   }

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

Convert a string to a date.

Definition at line 563 of file TreeViewDragDrop.C.

00563                                           {
00564     WDate d = WDate::fromString(item->text(), FileModel::dateEditFormat);
00565     item->setData(boost::any(d), DisplayRole);
00566   }

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 600 of file TreeViewDragDrop.C.

00602   {
00603     WStandardItem *result = new WStandardItem(location);
00604 
00605     if (!folderId.empty()) {
00606       result->setData(boost::any(folderId));
00607       result->setFlags(result->flags() | ItemIsDropEnabled);
00608       folderNameMap_[folderId] = location;
00609     } else
00610       result->setFlags(result->flags().clear(ItemIsSelectable));
00611 
00612     result->setIcon("icons/folder.gif");
00613 
00614     return result;
00615   }

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

Creates a title widget.

Definition at line 336 of file TreeViewDragDrop.C.

00336                                            {
00337     WText *result = new WText(title);
00338     result->setInline(false);
00339     result->setStyleClass("title");
00340 
00341     return result;
00342   }

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

Setup the user interface.

Definition at line 300 of file TreeViewDragDrop.C.

00300                   {
00301     WContainerWidget *w = root();
00302     w->setStyleClass("maindiv");
00303 
00304     /*
00305      * The main layout is a 3x2 grid layout.
00306      */
00307     WGridLayout *layout = new WGridLayout();
00308     layout->addWidget(createTitle("Folders"), 0, 0);
00309     layout->addWidget(createTitle("Files"), 0, 1);
00310     layout->addWidget(folderView(), 1, 0);
00311     layout->setColumnResizable(0);
00312 
00313     // select the first folder
00314     folderView_->select(folderModel_->index(0, 0, folderModel_->index(0, 0)));
00315 
00316     WVBoxLayout *vbox = new WVBoxLayout();
00317     vbox->addWidget(fileView(), 1);
00318     vbox->addWidget(pieChart(), 1);
00319     vbox->setResizable(0);
00320 
00321     layout->addLayout(vbox, 1, 1);
00322 
00323     layout->addWidget(aboutDisplay(), 2, 0, 1, 2, AlignTop);
00324 
00325     /*
00326      * Let row 1 and column 1 take the excess space.
00327      */
00328     layout->setRowStretch(1, 1);
00329     layout->setColumnStretch(1, 1);
00330 
00331     w->setLayout(layout);
00332   }

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

Process the result of the message box.

Definition at line 528 of file TreeViewDragDrop.C.

00528                     {
00529     delete popupActionBox_;
00530     popupActionBox_ = 0;
00531   }

void TreeViewDragDrop::editFile ( const WModelIndex item  )  [inline, private]

Edit a particular row.

Definition at line 414 of file TreeViewDragDrop.C.

00414                                          {
00415     new FileEditDialog(fileView_->model(), item);
00416   }

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

Creates the file table view (also a WTreeView).

Definition at line 375 of file TreeViewDragDrop.C.

00375                         {
00376     WTreeView *treeView = new WTreeView();
00377 
00378     // Hide the tree-like decoration on the first column, to make it
00379     // resemble a plain table
00380     treeView->setRootIsDecorated(false);
00381     treeView->setAlternatingRowColors(true);
00382 
00383     treeView->setModel(fileFilterModel_);
00384     treeView->setSelectionMode(ExtendedSelection);
00385     treeView->setDragEnabled(true);
00386 
00387     treeView->setColumnWidth(0, 100);
00388     treeView->setColumnWidth(1, 150);
00389     treeView->setColumnWidth(2, 100);
00390     treeView->setColumnWidth(3, 60);
00391     treeView->setColumnWidth(4, 100);
00392     treeView->setColumnWidth(5, 100);
00393 
00394     WItemDelegate *delegate = new WItemDelegate(this);
00395     delegate->setTextFormat(FileModel::dateDisplayFormat);
00396     treeView->setItemDelegateForColumn(4, delegate);
00397     treeView->setItemDelegateForColumn(5, delegate);
00398 
00399     treeView->setColumnAlignment(3, AlignRight);
00400     treeView->setColumnAlignment(4, AlignRight);
00401     treeView->setColumnAlignment(5, AlignRight);
00402 
00403     treeView->sortByColumn(1, AscendingOrder);
00404 
00405     treeView->doubleClicked().connect(SLOT(this, TreeViewDragDrop::editFile));
00406 
00407     fileView_ = treeView;
00408 
00409     return treeView;
00410   }

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

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

Definition at line 448 of file TreeViewDragDrop.C.

00448                        {
00449     if (folderView_->selectedIndexes().empty())
00450       return;
00451 
00452     WModelIndex selected = *folderView_->selectedIndexes().begin();
00453     boost::any d = selected.data(UserRole);
00454     if (!d.empty()) {
00455       std::string folder = boost::any_cast<std::string>(d);
00456 
00457       // For simplicity, we assume here that the folder-id does not
00458       // contain special regexp characters, otherwise these need to be
00459       // escaped -- or use the \Q \E qutoing escape regular expression
00460       // syntax (and escape \E)
00461       fileFilterModel_->setFilterRegExp(folder);
00462     }
00463   }

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

Creates the folder WTreeView.

Definition at line 346 of file TreeViewDragDrop.C.

00346                           {
00347     WTreeView *treeView = new FolderView();
00348 
00349     /*
00350      * To support right-click, we need to disable the built-in browser
00351      * context menu.
00352      *
00353      * Note that disabling the context menu and catching the
00354      * right-click does not work reliably on all browsers.
00355      */
00356     treeView->setAttributeValue
00357       ("oncontextmenu",
00358        "event.cancelBubble = true; event.returnValue = false; return false;");
00359     treeView->setModel(folderModel_);
00360     treeView->resize(200, WLength::Auto);
00361     treeView->setSelectionMode(SingleSelection);
00362     treeView->expandToDepth(1);
00363     treeView->selectionChanged().connect(SLOT(this,
00364                                               TreeViewDragDrop::folderChanged));
00365 
00366     treeView->mouseWentUp().connect(SLOT(this, TreeViewDragDrop::showPopup));
00367 
00368     folderView_ = treeView;
00369 
00370     return treeView;
00371   }

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

Creates the chart.

Definition at line 420 of file TreeViewDragDrop.C.

00420                       {
00421     using namespace Chart;
00422 
00423     WPieChart *chart = new WPieChart();
00424     chart->setModel(fileFilterModel_);
00425     chart->setTitle("File sizes");
00426 
00427     chart->setLabelsColumn(1); // Name
00428     chart->setDataColumn(3);   // Size
00429 
00430     chart->setPerspectiveEnabled(true, 0.2);
00431     chart->setDisplayLabels(Outside | TextLabel);
00432     chart->setStyleClass("about");
00433 
00434     return chart;
00435   }

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 540 of file TreeViewDragDrop.C.

00540                        {
00541     fileModel_->invisibleRootItem()->setRowCount(0);
00542 
00543     std::ifstream f("data/files.csv");
00544     readFromCsv(f, fileModel_);
00545 
00546     for (int i = 0; i < fileModel_->rowCount(); ++i) {
00547       WStandardItem *item = fileModel_->item(i, 0);
00548       item->setFlags(item->flags() | ItemIsDragEnabled);
00549       item->setIcon("icons/file.gif");
00550 
00551       std::string folderId = item->text().toUTF8();
00552 
00553       item->setData(boost::any(folderId), UserRole);
00554       item->setText(folderNameMap_[folderId]);
00555 
00556       convertToDate(fileModel_->item(i, 4));
00557       convertToDate(fileModel_->item(i, 5));
00558     }
00559   }

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

Populate the folders model.

Definition at line 570 of file TreeViewDragDrop.C.

00570                          {
00571     WStandardItem *level1, *level2;
00572 
00573     folderModel_->appendRow(level1 = createFolderItem("San Fransisco"));
00574     level1->appendRow(level2 = createFolderItem("Investors", "sf-investors"));
00575     level1->appendRow(level2 = createFolderItem("Fellows", "sf-fellows"));
00576 
00577     folderModel_->appendRow(level1 = createFolderItem("Sophia Antipolis"));
00578     level1->appendRow(level2 = createFolderItem("R&D", "sa-r_d"));
00579     level1->appendRow(level2 = createFolderItem("Services", "sa-services"));
00580     level1->appendRow(level2 = createFolderItem("Support", "sa-support"));
00581     level1->appendRow(level2 = createFolderItem("Billing", "sa-billing"));
00582 
00583     folderModel_->appendRow(level1 = createFolderItem("New York"));
00584     level1->appendRow(level2 = createFolderItem("Marketing", "ny-marketing"));
00585     level1->appendRow(level2 = createFolderItem("Sales", "ny-sales"));
00586     level1->appendRow(level2 = createFolderItem("Advisors", "ny-advisors"));
00587 
00588     folderModel_->appendRow(level1 = createFolderItem
00589                              (WString::fromUTF8("Frankfürt")));
00590     level1->appendRow(level2 = createFolderItem("Sales", "frank-sales"));
00591 
00592     folderModel_->setHeaderData(0, Horizontal,
00593                                  boost::any(std::string("SandBox")));
00594   }

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

Process the result of the popup menu.

Definition at line 505 of file TreeViewDragDrop.C.

00505                      {
00506     if (popup_->result()) {
00507       /*
00508        * You could also bind extra data to an item using setData() and
00509        * check here for the action asked. For now, we just use the text.
00510        */
00511       WString text = popup_->result()->text();
00512       delete popup_;
00513       popup_ = 0;
00514 
00515       popupActionBox_ = new WMessageBox("Sorry.","Action '" + text
00516                                         + "' is not implemented.", NoIcon, Ok);
00517       popupActionBox_->buttonClicked()
00518         .connect(SLOT(this, TreeViewDragDrop::dialogDone));
00519       popupActionBox_->show();
00520     } else {
00521       delete popup_;
00522       popup_ = 0;
00523     }
00524   }

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

Show a popup for a folder item.

Definition at line 467 of file TreeViewDragDrop.C.

00467                                                                     {
00468     if (event.button() == WMouseEvent::RightButton) {
00469       // Select the item, it was not yet selected.
00470       if (!folderView_->isSelected(item))
00471         folderView_->select(item);
00472 
00473       delete popup_;
00474 
00475       popup_ = new WPopupMenu();
00476       popup_->addItem("icons/folder_new.gif", "Create a New Folder");
00477       popup_->addItem("Rename this Folder")->setCheckable(true);
00478       popup_->addItem("Delete this Folder");
00479       popup_->addSeparator();
00480       popup_->addItem("Folder Details");
00481       popup_->addSeparator();
00482       popup_->addItem("Application Inventory");
00483       popup_->addItem("Hardware Inventory");
00484       popup_->addSeparator();
00485 
00486       WPopupMenu *subMenu = new WPopupMenu();
00487       subMenu->addItem("Sub Item 1");
00488       subMenu->addItem("Sub Item 2");
00489       popup_->addMenu("File Deployments", subMenu);
00490 
00491       /*
00492        * This is one method of executing a popup, which does not block a
00493        * thread for a reentrant event loop, and thus scales.
00494        *
00495        * Alternatively you could call WPopupMenu::exec(), which returns
00496        * the result, but while waiting for it, blocks the thread.
00497        */      
00498       popup_->aboutToHide().connect(SLOT(this, TreeViewDragDrop::popupAction));
00499       popup_->popup(event);
00500     }
00501   }


Member Data Documentation

The sort filter proxy model that adapts fileModel_.

Definition at line 281 of file TreeViewDragDrop.C.

The file model (used by fileView_).

Definition at line 278 of file TreeViewDragDrop.C.

The file view.

Definition at line 290 of file TreeViewDragDrop.C.

The folder model (used by folderView_).

Definition at line 275 of file TreeViewDragDrop.C.

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

Maps folder id's to folder descriptions.

Definition at line 284 of file TreeViewDragDrop.C.

The folder view.

Definition at line 287 of file TreeViewDragDrop.C.

Popup menu on the folder view.

Definition at line 293 of file TreeViewDragDrop.C.

Message box to confirm the poup menu action.

Definition at line 296 of file TreeViewDragDrop.C.


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

Generated on Thu May 13 05:16:01 2010 for Wt by doxygen 1.6.3