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


Constructor & Destructor Documentation

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

Constructor.

Definition at line 241 of file TreeViewDragDrop.C.

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

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

Definition at line 267 of file TreeViewDragDrop.C.

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


Member Function Documentation

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

Setup the user interface.

Definition at line 299 of file TreeViewDragDrop.C.

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

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

Creates a title widget.

Definition at line 333 of file TreeViewDragDrop.C.

00333                                            {
00334     WText *result = new WText(title);
00335     result->setInline(false);
00336     result->setStyleClass("title");
00337 
00338     return result;
00339   }

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

Creates the folder WTreeView.

Definition at line 343 of file TreeViewDragDrop.C.

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

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

Creates the file table view (also a WTreeView).

Definition at line 372 of file TreeViewDragDrop.C.

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

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

Edit a particular row.

Definition at line 411 of file TreeViewDragDrop.C.

00411                                          {
00412     new FileEditDialog(fileView_->model(), item);
00413   }

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

Creates the chart.

Definition at line 417 of file TreeViewDragDrop.C.

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

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

Creates the hints text.

Definition at line 441 of file TreeViewDragDrop.C.

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

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

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

Definition at line 450 of file TreeViewDragDrop.C.

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

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

Show a popup for a folder item.

Definition at line 469 of file TreeViewDragDrop.C.

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

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

Process the result of the popup menu.

Definition at line 507 of file TreeViewDragDrop.C.

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

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

Process the result of the message box.

Definition at line 530 of file TreeViewDragDrop.C.

00530                     {
00531     delete popupActionBox_;
00532     popupActionBox_ = 0;
00533   }

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

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

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

Convert a string to a date.

Definition at line 565 of file TreeViewDragDrop.C.

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

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

Populate the folders model.

Definition at line 572 of file TreeViewDragDrop.C.

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

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

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


Member Data Documentation

The folder model (used by folderView_).

Definition at line 274 of file TreeViewDragDrop.C.

The file model (used by fileView_).

Definition at line 277 of file TreeViewDragDrop.C.

The sort filter proxy model that adapts fileModel_.

Definition at line 280 of file TreeViewDragDrop.C.

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

Maps folder id's to folder descriptions.

Definition at line 283 of file TreeViewDragDrop.C.

The folder view.

Definition at line 286 of file TreeViewDragDrop.C.

The file view.

Definition at line 289 of file TreeViewDragDrop.C.

Popup menu on the folder view.

Definition at line 292 of file TreeViewDragDrop.C.

Message box to confirm the poup menu action.

Definition at line 295 of file TreeViewDragDrop.C.


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

Generated on Tue Sep 1 17:51:24 2009 for Wt by doxygen 1.5.6