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).
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 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 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 
00243     /*
00244      * Create the data models.
00245      */
00246     folderModel_ = new WStandardItemModel(0, 1, this);
00247     populateFolders();
00248 
00249     fileModel_ = new FileModel(this);
00250     populateFiles();
00251 
00252     fileFilterModel_ = new WSortFilterProxyModel(this);
00253     fileFilterModel_->setSourceModel(fileModel_);
00254     fileFilterModel_->setDynamicSortFilter(true);
00255     fileFilterModel_->setFilterKeyColumn(0);
00256     fileFilterModel_->setFilterRole(UserRole);
00257 
00258     /*
00259      * Setup the user interface.
00260      */
00261     createUI();
00262   }


Member Function Documentation

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

Setup the user interface.

Definition at line 285 of file TreeViewDragDrop.C.

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

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

Creates a title widget.

Definition at line 319 of file TreeViewDragDrop.C.

00319                                            {
00320     WText *result = new WText(title);
00321     result->setInline(false);
00322     result->setStyleClass("title");
00323 
00324     return result;
00325   }

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

Creates the folder WTreeView.

Definition at line 329 of file TreeViewDragDrop.C.

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

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

Creates the file table view (also a WTreeView).

Definition at line 358 of file TreeViewDragDrop.C.

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

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

Edit a particular row.

Definition at line 395 of file TreeViewDragDrop.C.

00395                                          {
00396     new FileEditDialog(fileView_->model(), item);
00397   }

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

Creates the chart.

Definition at line 401 of file TreeViewDragDrop.C.

00401                       {
00402     using namespace Chart;
00403 
00404     WPieChart *chart = new WPieChart();
00405     chart->resize(450, 200);
00406     chart->setModel(fileFilterModel_);
00407     chart->setTitle("File sizes");
00408 
00409     chart->setLabelsColumn(1); // Name
00410     chart->setDataColumn(3);   // Size
00411 
00412     chart->setPerspectiveEnabled(true, 0.2);
00413     chart->setDisplayLabels(Outside | TextLabel);
00414 
00415     WContainerWidget *w = new WContainerWidget();
00416     w->setContentAlignment(AlignCenter);
00417     w->setStyleClass("about");
00418     w->addWidget(chart);
00419 
00420     return w;
00421   }

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

Creates the hints text.

Definition at line 425 of file TreeViewDragDrop.C.

00425                           {
00426     WText *result = new WText(WString::tr("about-text"));
00427     result->setStyleClass("about");
00428     return result;
00429   }

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

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

Definition at line 434 of file TreeViewDragDrop.C.

00434                        {
00435     if (folderView_->selectedIndexes().empty())
00436       return;
00437 
00438     WModelIndex selected = *folderView_->selectedIndexes().begin();
00439     boost::any d = selected.data(UserRole);
00440     if (!d.empty()) {
00441       std::string folder = boost::any_cast<std::string>(d);
00442 
00443       // For simplicity, we assume here that the folder-id does not
00444       // contain special regexp characters, otherwise these need to be
00445       // escaped -- or use the \Q \E qutoing escape regular expression
00446       // syntax (and escape \E)
00447       fileFilterModel_->setFilterRegExp(folder);
00448     }
00449   }

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

Show a popup for a folder item.

Definition at line 453 of file TreeViewDragDrop.C.

00453                                                                     {
00454     if (event.button() == WMouseEvent::RightButton) {
00455 
00456       // Select the item, it was not yet selected.
00457       folderView_->select(item);
00458 
00459       WPopupMenu popup;
00460       popup.addItem("icons/folder_new.gif", "Create a New Folder");
00461       popup.addItem("Rename this Folder")->setCheckable(true);
00462       popup.addItem("Delete this Folder");
00463       popup.addSeparator();
00464       popup.addItem("Folder Details");
00465       popup.addSeparator();
00466       popup.addItem("Application Inventory");
00467       popup.addItem("Hardware Inventory");
00468       popup.addSeparator();
00469 
00470       WPopupMenu *subMenu = new WPopupMenu();
00471       subMenu->addItem("Sub Item 1");
00472       subMenu->addItem("Sub Item 2");
00473       popup.addMenu("File Deployments", subMenu);
00474 
00475       /*
00476        * This is one method of executing a popup, i.e. by using a reentrant
00477        * event loop (blocking the current thread).
00478        *
00479        * Alternatively you could call WPopupMenu::popup(), listen for
00480        * to the WPopupMenu::aboutToHide signal, and check the
00481        * WPopupMenu::result()
00482        */
00483       WPopupMenuItem *item = popup.exec(event);
00484 
00485       if (item) {
00486         /*
00487          * You may bind extra data to an item using setData() and
00488          * check here for the action asked.
00489          */
00490         WMessageBox::show("Sorry.",
00491                           "Action '" + item->text() + "' is not implemented.",
00492                           Ok);
00493       }
00494     }
00495   }

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

00504                        {
00505     fileModel_->invisibleRootItem()->setRowCount(0);
00506 
00507     std::ifstream f("data/files.csv");
00508     readFromCsv(f, fileModel_);
00509 
00510     for (int i = 0; i < fileModel_->rowCount(); ++i) {
00511       WStandardItem *item = fileModel_->item(i, 0);
00512       item->setFlags(item->flags() | ItemIsDragEnabled);
00513       item->setIcon("icons/file.gif");
00514 
00515       std::string folderId = item->text().toUTF8();
00516 
00517       item->setData(boost::any(folderId), UserRole);
00518       item->setText(folderNameMap_[folderId]);
00519 
00520       convertToDate(fileModel_->item(i, 4));
00521       convertToDate(fileModel_->item(i, 5));
00522     }
00523   }

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

Convert a string to a date.

Definition at line 527 of file TreeViewDragDrop.C.

00527                                           {
00528     WDate d = WDate::fromString(item->text(), FileModel::dateEditFormat);
00529     item->setData(boost::any(d), DisplayRole);
00530   }

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

Populate the folders model.

Definition at line 534 of file TreeViewDragDrop.C.

00534                          {
00535     WStandardItem *level1, *level2;
00536 
00537     folderModel_->appendRow(level1 = createFolderItem("San Fransisco"));
00538     level1->appendRow(level2 = createFolderItem("Investors", "sf-investors"));
00539     level1->appendRow(level2 = createFolderItem("Fellows", "sf-fellows"));
00540 
00541     folderModel_->appendRow(level1 = createFolderItem("Sophia Antipolis"));
00542     level1->appendRow(level2 = createFolderItem("R&amp;D", "sa-r_d"));
00543     level1->appendRow(level2 = createFolderItem("Services", "sa-services"));
00544     level1->appendRow(level2 = createFolderItem("Support", "sa-support"));
00545     level1->appendRow(level2 = createFolderItem("Billing", "sa-billing"));
00546 
00547     folderModel_->appendRow(level1 = createFolderItem("New York"));
00548     level1->appendRow(level2 = createFolderItem("Marketing", "ny-marketing"));
00549     level1->appendRow(level2 = createFolderItem("Sales", "ny-sales"));
00550     level1->appendRow(level2 = createFolderItem("Advisors", "ny-advisors"));
00551 
00552     folderModel_->appendRow(level1 = createFolderItem
00553                              (WString::fromUTF8("Frankfürt")));
00554     level1->appendRow(level2 = createFolderItem("Sales", "frank-sales"));
00555 
00556     folderModel_->setHeaderData(0, Horizontal,
00557                                  boost::any(std::string("SandBox")));
00558   }

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

00566   {
00567     WStandardItem *result = new WStandardItem(location);
00568 
00569     if (!folderId.empty()) {
00570       result->setData(boost::any(folderId));
00571       result->setFlags(result->flags() | ItemIsDropEnabled);
00572       folderNameMap_[folderId] = location;
00573     } else
00574       result->setFlags(result->flags().clear(ItemIsSelectable));
00575 
00576     result->setIcon("icons/folder.gif");
00577 
00578     return result;
00579   }


Member Data Documentation

The folder model (used by folderView_).

Definition at line 266 of file TreeViewDragDrop.C.

The file model (used by fileView_).

Definition at line 269 of file TreeViewDragDrop.C.

The sort filter proxy model that adapts fileModel_.

Definition at line 272 of file TreeViewDragDrop.C.

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

Maps folder id's to folder descriptions.

Definition at line 275 of file TreeViewDragDrop.C.

The folder view.

Definition at line 278 of file TreeViewDragDrop.C.

The file view.

Definition at line 281 of file TreeViewDragDrop.C.


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

Generated on Mon Mar 9 08:28:57 2009 for Wt by doxygen 1.5.6