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.
WTableViewfileView ()
 Creates the file table view (a WTableView).
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.
WTableViewfileView_
 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 237 of file TreeViewDragDrop.C.


Constructor & Destructor Documentation

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

Constructor.

Definition at line 242 of file TreeViewDragDrop.C.

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

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

Definition at line 270 of file TreeViewDragDrop.C.

00270                               {
00271     delete popup_;
00272     delete popupActionBox_;
00273   }


Member Function Documentation

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

Setup the user interface.

Definition at line 302 of file TreeViewDragDrop.C.

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

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

Creates a title widget.

Definition at line 338 of file TreeViewDragDrop.C.

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

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

Creates the folder WTreeView.

Definition at line 348 of file TreeViewDragDrop.C.

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

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

Creates the file table view (a WTableView).

Definition at line 377 of file TreeViewDragDrop.C.

00377                          {
00378     WTableView *tableView = new WTableView();
00379 
00380     tableView->setAlternatingRowColors(true);
00381 
00382     tableView->setModel(fileFilterModel_);
00383     tableView->setSelectionMode(ExtendedSelection);
00384     tableView->setDragEnabled(true);
00385 
00386     tableView->setColumnWidth(0, 100);
00387     tableView->setColumnWidth(1, 150);
00388     tableView->setColumnWidth(2, 100);
00389     tableView->setColumnWidth(3, 60);
00390     tableView->setColumnWidth(4, 100);
00391     tableView->setColumnWidth(5, 100);
00392 
00393     WItemDelegate *delegate = new WItemDelegate(this);
00394     delegate->setTextFormat(FileModel::dateDisplayFormat);
00395     tableView->setItemDelegateForColumn(4, delegate);
00396     tableView->setItemDelegateForColumn(5, delegate);
00397 
00398     tableView->setColumnAlignment(3, AlignRight);
00399     tableView->setColumnAlignment(4, AlignRight);
00400     tableView->setColumnAlignment(5, AlignRight);
00401 
00402     tableView->sortByColumn(1, AscendingOrder);
00403 
00404     tableView->doubleClicked()
00405       .connect(SLOT(this, TreeViewDragDrop::editFile));
00406 
00407     fileView_ = tableView;
00408 
00409     return tableView;
00410   }

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   }

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 
00433     if (!WApplication::instance()->environment().ajax()) {
00434       chart->resize(500, 200);
00435       chart->setMargin(WLength::Auto, Left | Right);
00436       WContainerWidget *w = new WContainerWidget();
00437       w->addWidget(chart);
00438       w->setStyleClass("about");
00439       return w;
00440     } else {
00441       chart->setStyleClass("about");
00442       return chart;
00443     }
00444   }

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

Creates the hints text.

Definition at line 448 of file TreeViewDragDrop.C.

00448                           {
00449     WText *result = new WText(WString::tr("about-text"));
00450     result->setStyleClass("about");
00451     return result;
00452   }

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

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

Definition at line 457 of file TreeViewDragDrop.C.

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

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

Show a popup for a folder item.

Definition at line 476 of file TreeViewDragDrop.C.

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

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

Process the result of the popup menu.

Definition at line 514 of file TreeViewDragDrop.C.

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

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

Process the result of the message box.

Definition at line 537 of file TreeViewDragDrop.C.

00537                     {
00538     delete popupActionBox_;
00539     popupActionBox_ = 0;
00540   }

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

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

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

Convert a string to a date.

Definition at line 572 of file TreeViewDragDrop.C.

00572                                           {
00573     WDate d = WDate::fromString(item->text(), FileModel::dateEditFormat);
00574     item->setData(boost::any(d), DisplayRole);
00575   }

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

Populate the folders model.

Definition at line 579 of file TreeViewDragDrop.C.

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

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

00611   {
00612     WStandardItem *result = new WStandardItem(location);
00613 
00614     if (!folderId.empty()) {
00615       result->setData(boost::any(folderId));
00616       result->setFlags(result->flags() | ItemIsDropEnabled);
00617       folderNameMap_[folderId] = location;
00618     } else
00619       result->setFlags(result->flags().clear(ItemIsSelectable));
00620 
00621     result->setIcon("icons/folder.gif");
00622 
00623     return result;
00624   }


Member Data Documentation

The folder model (used by folderView_).

Definition at line 277 of file TreeViewDragDrop.C.

The file model (used by fileView_).

Definition at line 280 of file TreeViewDragDrop.C.

The sort filter proxy model that adapts fileModel_.

Definition at line 283 of file TreeViewDragDrop.C.

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

Maps folder id's to folder descriptions.

Definition at line 286 of file TreeViewDragDrop.C.

The folder view.

Definition at line 289 of file TreeViewDragDrop.C.

The file view.

Definition at line 292 of file TreeViewDragDrop.C.

Popup menu on the folder view.

Definition at line 295 of file TreeViewDragDrop.C.

Message box to confirm the poup menu action.

Definition at line 298 of file TreeViewDragDrop.C.


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

Generated on Thu May 20 18:14:58 2010 for Wt by doxygen 1.5.6