Wt examples  3.3.6
Public Member Functions | Private Member Functions | Private Attributes | List of all members
TreeViewDragDrop Class Reference

Main application class. More...

Inheritance diagram for TreeViewDragDrop:
Inheritance graph
[legend]

Public Member Functions

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

Private Member Functions

void createUI ()
 Setup the user interface. More...
 
WText * createTitle (const WString &title)
 Creates a title widget. More...
 
WTreeView * folderView ()
 Creates the folder WTreeView. More...
 
WTableView * fileView ()
 Creates the file table view (a WTableView) More...
 
void editFile (const WModelIndex &item)
 Edit a particular row. More...
 
WWidget * pieChart ()
 Creates the chart. More...
 
WWidget * aboutDisplay ()
 Creates the hints text. More...
 
void folderChanged ()
 Change the filter on the file view when the selected folder changes. More...
 
void showPopup (const WModelIndex &item, const WMouseEvent &event)
 Show a popup for a folder item. More...
 
void popupAction ()
 Process the result of the popup menu. More...
 
void dialogDone ()
 Process the result of the message box. More...
 
void populateFiles ()
 Populate the files model. More...
 
void convertToDate (WStandardItem *item)
 Convert a string to a date. More...
 
void convertToNumber (WStandardItem *item)
 Convert a string to a number. More...
 
void populateFolders ()
 Populate the folders model. More...
 
WStandardItem * createFolderItem (const WString &location, const std::string &folderId=std::string())
 Create a folder item. More...
 

Private Attributes

WStandardItemModel * folderModel_
 The folder model (used by folderView_) More...
 
WStandardItemModel * fileModel_
 The file model (used by fileView_) More...
 
WSortFilterProxyModel * fileFilterModel_
 The sort filter proxy model that adapts fileModel_. More...
 
std::map< std::string, WString > folderNameMap_
 Maps folder id's to folder descriptions. More...
 
WTreeView * folderView_
 The folder view. More...
 
WTableView * fileView_
 The file view. More...
 
WPopupMenu * popup_
 Popup menu on the folder view. More...
 
WMessageBox * popupActionBox_
 Message box to confirm the poup menu action. More...
 

Detailed Description

Main application class.

Definition at line 237 of file TreeViewDragDrop.C.

Constructor & Destructor Documentation

§ TreeViewDragDrop()

TreeViewDragDrop::TreeViewDragDrop ( const WEnvironment &  env)
inline

Constructor.

Definition at line 242 of file TreeViewDragDrop.C.

243  : WApplication(env),
244  popup_(0),
245  popupActionBox_(0)
246  {
247  setCssTheme("polished");
248 
249  /*
250  * Create the data models.
251  */
252  folderModel_ = new WStandardItemModel(0, 1, this);
253  populateFolders();
254 
255  fileModel_ = new FileModel(this);
256  populateFiles();
257 
258  /*
259  The header items are also endered using an ItemDelegate, and thus
260  support other data, e.g.:
261 
262  fileModel_->setHeaderFlags(0, Horizontal, HeaderIsUserCheckable);
263  fileModel_->setHeaderData(0, Horizontal,
264  std::string("icons/file.gif"),
265  Wt::DecorationRole);
266  */
267  fileFilterModel_ = new WSortFilterProxyModel(this);
268  fileFilterModel_->setSourceModel(fileModel_);
269  fileFilterModel_->setDynamicSortFilter(true);
270  fileFilterModel_->setFilterKeyColumn(0);
271  fileFilterModel_->setFilterRole(UserRole);
272 
273  /*
274  * Setup the user interface.
275  */
276  createUI();
277  }
WStandardItemModel * folderModel_
The folder model (used by folderView_)
WMessageBox * popupActionBox_
Message box to confirm the poup menu action.
void populateFiles()
Populate the files model.
A specialized standard item model which report a specific drag and drop mime type.
WSortFilterProxyModel * fileFilterModel_
The sort filter proxy model that adapts fileModel_.
void createUI()
Setup the user interface.
WPopupMenu * popup_
Popup menu on the folder view.
void populateFolders()
Populate the folders model.
WStandardItemModel * fileModel_
The file model (used by fileView_)

§ ~TreeViewDragDrop()

virtual TreeViewDragDrop::~TreeViewDragDrop ( )
inlinevirtual

Definition at line 279 of file TreeViewDragDrop.C.

279  {
280  delete popup_;
281  delete popupActionBox_;
282  }
WMessageBox * popupActionBox_
Message box to confirm the poup menu action.
WPopupMenu * popup_
Popup menu on the folder view.

Member Function Documentation

§ aboutDisplay()

WWidget* TreeViewDragDrop::aboutDisplay ( )
inlineprivate

Creates the hints text.

Definition at line 457 of file TreeViewDragDrop.C.

457  {
458  WText *result = new WText(WString::tr("about-text"));
459  result->setStyleClass("about");
460  return result;
461  }

§ convertToDate()

void TreeViewDragDrop::convertToDate ( WStandardItem *  item)
inlineprivate

Convert a string to a date.

Definition at line 588 of file TreeViewDragDrop.C.

588  {
589  WDate d = WDate::fromString(item->text(), FileModel::dateEditFormat);
590  item->setData(boost::any(d), DisplayRole);
591  }
static WString dateEditFormat
Date edit format.

§ convertToNumber()

void TreeViewDragDrop::convertToNumber ( WStandardItem *  item)
inlineprivate

Convert a string to a number.

Definition at line 595 of file TreeViewDragDrop.C.

595  {
596  int i = boost::lexical_cast<int>(item->text());
597  item->setData(boost::any(i), EditRole);
598  }

§ createFolderItem()

WStandardItem* TreeViewDragDrop::createFolderItem ( const WString &  location,
const std::string &  folderId = std::string() 
)
inlineprivate

Create a folder item.

Configures flags for drag and drop support.

Definition at line 632 of file TreeViewDragDrop.C.

634  {
635  WStandardItem *result = new WStandardItem(location);
636 
637  if (!folderId.empty()) {
638  result->setData(boost::any(folderId));
639  result->setFlags(result->flags() | ItemIsDropEnabled);
640  folderNameMap_[folderId] = location;
641  } else
642  result->setFlags(result->flags().clear(ItemIsSelectable));
643 
644  result->setIcon("icons/folder.gif");
645 
646  return result;
647  }
std::map< std::string, WString > folderNameMap_
Maps folder id&#39;s to folder descriptions.

§ createTitle()

WText* TreeViewDragDrop::createTitle ( const WString &  title)
inlineprivate

Creates a title widget.

Definition at line 347 of file TreeViewDragDrop.C.

347  {
348  WText *result = new WText(title);
349  result->setInline(false);
350  result->setStyleClass("title");
351 
352  return result;
353  }

§ createUI()

void TreeViewDragDrop::createUI ( )
inlineprivate

Setup the user interface.

Definition at line 311 of file TreeViewDragDrop.C.

311  {
312  WContainerWidget *w = root();
313  w->setStyleClass("maindiv");
314 
315  /*
316  * The main layout is a 3x2 grid layout.
317  */
318  WGridLayout *layout = new WGridLayout();
319  layout->addWidget(createTitle("Folders"), 0, 0);
320  layout->addWidget(createTitle("Files"), 0, 1);
321  layout->addWidget(folderView(), 1, 0);
322  layout->setColumnResizable(0);
323 
324  // select the first folder
325  folderView_->select(folderModel_->index(0, 0, folderModel_->index(0, 0)));
326 
327  WVBoxLayout *vbox = new WVBoxLayout();
328  vbox->addWidget(fileView(), 1);
329  vbox->addWidget(pieChart(), 1);
330  vbox->setResizable(0);
331 
332  layout->addLayout(vbox, 1, 1);
333 
334  layout->addWidget(aboutDisplay(), 2, 0, 1, 2);
335 
336  /*
337  * Let row 1 and column 1 take the excess space.
338  */
339  layout->setRowStretch(1, 1);
340  layout->setColumnStretch(1, 1);
341 
342  w->setLayout(layout);
343  }
WStandardItemModel * folderModel_
The folder model (used by folderView_)
WTableView * fileView()
Creates the file table view (a WTableView)
WWidget * pieChart()
Creates the chart.
WTreeView * folderView_
The folder view.
WWidget * aboutDisplay()
Creates the hints text.
WTreeView * folderView()
Creates the folder WTreeView.
WText * createTitle(const WString &title)
Creates a title widget.

§ dialogDone()

void TreeViewDragDrop::dialogDone ( )
inlineprivate

Process the result of the message box.

Definition at line 548 of file TreeViewDragDrop.C.

548  {
549  delete popupActionBox_;
550  popupActionBox_ = 0;
551  }
WMessageBox * popupActionBox_
Message box to confirm the poup menu action.

§ editFile()

void TreeViewDragDrop::editFile ( const WModelIndex &  item)
inlineprivate

Edit a particular row.

Definition at line 422 of file TreeViewDragDrop.C.

422  {
423  new FileEditDialog(fileView_->model(), item);
424  }
A dialog for editing a &#39;file&#39;.
WTableView * fileView_
The file view.

§ fileView()

WTableView* TreeViewDragDrop::fileView ( )
inlineprivate

Creates the file table view (a WTableView)

Definition at line 386 of file TreeViewDragDrop.C.

386  {
387  WTableView *tableView = new WTableView();
388 
389  tableView->setAlternatingRowColors(true);
390 
391  tableView->setModel(fileFilterModel_);
392  tableView->setSelectionMode(ExtendedSelection);
393  tableView->setDragEnabled(true);
394 
395  tableView->setColumnWidth(0, 100);
396  tableView->setColumnWidth(1, 150);
397  tableView->setColumnWidth(2, 100);
398  tableView->setColumnWidth(3, 60);
399  tableView->setColumnWidth(4, 100);
400  tableView->setColumnWidth(5, 100);
401 
402  WItemDelegate *delegate = new WItemDelegate(this);
403  delegate->setTextFormat(FileModel::dateDisplayFormat);
404  tableView->setItemDelegateForColumn(4, delegate);
405  tableView->setItemDelegateForColumn(5, delegate);
406 
407  tableView->setColumnAlignment(3, AlignRight);
408  tableView->setColumnAlignment(4, AlignRight);
409  tableView->setColumnAlignment(5, AlignRight);
410 
411  tableView->sortByColumn(1, AscendingOrder);
412 
413  tableView->doubleClicked().connect(this, &TreeViewDragDrop::editFile);
414 
415  fileView_ = tableView;
416 
417  return tableView;
418  }
static WString dateDisplayFormat
Date display format.
WSortFilterProxyModel * fileFilterModel_
The sort filter proxy model that adapts fileModel_.
void editFile(const WModelIndex &item)
Edit a particular row.
WTableView * fileView_
The file view.

§ folderChanged()

void TreeViewDragDrop::folderChanged ( )
inlineprivate

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

Definition at line 466 of file TreeViewDragDrop.C.

466  {
467  if (folderView_->selectedIndexes().empty())
468  return;
469 
470  WModelIndex selected = *folderView_->selectedIndexes().begin();
471  boost::any d = selected.data(UserRole);
472  if (!d.empty()) {
473  std::string folder = boost::any_cast<std::string>(d);
474 
475  // For simplicity, we assume here that the folder-id does not
476  // contain special regexp characters, otherwise these need to be
477  // escaped -- or use the \Q \E qutoing escape regular expression
478  // syntax (and escape \E)
479  fileFilterModel_->setFilterRegExp(folder);
480  }
481  }
WTreeView * folderView_
The folder view.
WSortFilterProxyModel * fileFilterModel_
The sort filter proxy model that adapts fileModel_.

§ folderView()

WTreeView* TreeViewDragDrop::folderView ( )
inlineprivate

Creates the folder WTreeView.

Definition at line 357 of file TreeViewDragDrop.C.

357  {
358  WTreeView *treeView = new FolderView();
359 
360  /*
361  * To support right-click, we need to disable the built-in browser
362  * context menu.
363  *
364  * Note that disabling the context menu and catching the
365  * right-click does not work reliably on all browsers.
366  */
367  treeView->setAttributeValue
368  ("oncontextmenu",
369  "event.cancelBubble = true; event.returnValue = false; return false;");
370  treeView->setModel(folderModel_);
371  treeView->resize(200, WLength::Auto);
372  treeView->setSelectionMode(SingleSelection);
373  treeView->expandToDepth(1);
374  treeView->selectionChanged()
375  .connect(this, &TreeViewDragDrop::folderChanged);
376 
377  treeView->mouseWentUp().connect(this, &TreeViewDragDrop::showPopup);
378 
379  folderView_ = treeView;
380 
381  return treeView;
382  }
WStandardItemModel * folderModel_
The folder model (used by folderView_)
WTreeView * folderView_
The folder view.
A specialized treeview that supports a custom drop event.
Definition: FolderView.h:19
void showPopup(const WModelIndex &item, const WMouseEvent &event)
Show a popup for a folder item.
void folderChanged()
Change the filter on the file view when the selected folder changes.

§ pieChart()

WWidget* TreeViewDragDrop::pieChart ( )
inlineprivate

Creates the chart.

Definition at line 428 of file TreeViewDragDrop.C.

428  {
429  using namespace Chart;
430 
431  WPieChart *chart = new WPieChart();
432  // chart->setPreferredMethod(WPaintedWidget::PngImage);
433  chart->setModel(fileFilterModel_);
434  chart->setTitle("File sizes");
435 
436  chart->setLabelsColumn(1); // Name
437  chart->setDataColumn(3); // Size
438 
439  chart->setPerspectiveEnabled(true, 0.2);
440  chart->setDisplayLabels(Outside | TextLabel);
441 
442  if (!WApplication::instance()->environment().ajax()) {
443  chart->resize(500, 200);
444  chart->setMargin(WLength::Auto, Left | Right);
445  WContainerWidget *w = new WContainerWidget();
446  w->addWidget(chart);
447  w->setStyleClass("about");
448  return w;
449  } else {
450  chart->setStyleClass("about");
451  return chart;
452  }
453  }
WSortFilterProxyModel * fileFilterModel_
The sort filter proxy model that adapts fileModel_.

§ populateFiles()

void TreeViewDragDrop::populateFiles ( )
inlineprivate

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

560  {
561  fileModel_->invisibleRootItem()->setRowCount(0);
562 
563  std::ifstream f((appRoot() + "data/files.csv").c_str());
564 
565  if (!f)
566  throw std::runtime_error("Could not read: data/files.csv");
567 
569 
570  for (int i = 0; i < fileModel_->rowCount(); ++i) {
571  WStandardItem *item = fileModel_->item(i, 0);
572  item->setFlags(item->flags() | ItemIsDragEnabled);
573  item->setIcon("icons/file.gif");
574 
575  std::string folderId = item->text().toUTF8();
576 
577  item->setData(boost::any(folderId), UserRole);
578  item->setText(folderNameMap_[folderId]);
579 
580  convertToNumber(fileModel_->item(i, 3));
581  convertToDate(fileModel_->item(i, 4));
582  convertToDate(fileModel_->item(i, 5));
583  }
584  }
void convertToNumber(WStandardItem *item)
Convert a string to a number.
void convertToDate(WStandardItem *item)
Convert a string to a date.
std::map< std::string, WString > folderNameMap_
Maps folder id&#39;s to folder descriptions.
void readFromCsv(std::istream &f, Wt::WAbstractItemModel *model, int numRows, bool firstLineIsHeaders)
Utility function that reads a model from a CSV file.
Definition: CsvUtil.C:56
WStandardItemModel * fileModel_
The file model (used by fileView_)

§ populateFolders()

void TreeViewDragDrop::populateFolders ( )
inlineprivate

Populate the folders model.

Definition at line 602 of file TreeViewDragDrop.C.

602  {
603  WStandardItem *level1, *level2;
604 
605  folderModel_->appendRow(level1 = createFolderItem("San Fransisco"));
606  level1->appendRow(level2 = createFolderItem("Investors", "sf-investors"));
607  level1->appendRow(level2 = createFolderItem("Fellows", "sf-fellows"));
608 
609  folderModel_->appendRow(level1 = createFolderItem("Sophia Antipolis"));
610  level1->appendRow(level2 = createFolderItem("R&D", "sa-r_d"));
611  level1->appendRow(level2 = createFolderItem("Services", "sa-services"));
612  level1->appendRow(level2 = createFolderItem("Support", "sa-support"));
613  level1->appendRow(level2 = createFolderItem("Billing", "sa-billing"));
614 
615  folderModel_->appendRow(level1 = createFolderItem("New York"));
616  level1->appendRow(level2 = createFolderItem("Marketing", "ny-marketing"));
617  level1->appendRow(level2 = createFolderItem("Sales", "ny-sales"));
618  level1->appendRow(level2 = createFolderItem("Advisors", "ny-advisors"));
619 
620  folderModel_->appendRow(level1 = createFolderItem
621  (WString::fromUTF8("Frankfürt")));
622  level1->appendRow(level2 = createFolderItem("Sales", "frank-sales"));
623 
624  folderModel_->setHeaderData(0, Horizontal,
625  boost::any(std::string("SandBox")));
626  }
WStandardItemModel * folderModel_
The folder model (used by folderView_)
WStandardItem * createFolderItem(const WString &location, const std::string &folderId=std::string())
Create a folder item.

§ popupAction()

void TreeViewDragDrop::popupAction ( )
inlineprivate

Process the result of the popup menu.

Definition at line 527 of file TreeViewDragDrop.C.

527  {
528  if (popup_->result()) {
529  /*
530  * You could also bind extra data to an item using setData() and
531  * check here for the action asked. For now, we just use the text.
532  */
533  WString text = popup_->result()->text();
534  popup_->hide();
535 
536  popupActionBox_ = new WMessageBox("Sorry.","Action '" + text
537  + "' is not implemented.", NoIcon, Ok);
538  popupActionBox_->buttonClicked()
539  .connect(this, &TreeViewDragDrop::dialogDone);
540  popupActionBox_->show();
541  } else {
542  popup_->hide();
543  }
544  }
WMessageBox * popupActionBox_
Message box to confirm the poup menu action.
void dialogDone()
Process the result of the message box.
WPopupMenu * popup_
Popup menu on the folder view.

§ showPopup()

void TreeViewDragDrop::showPopup ( const WModelIndex &  item,
const WMouseEvent &  event 
)
inlineprivate

Show a popup for a folder item.

Definition at line 485 of file TreeViewDragDrop.C.

485  {
486  if (event.button() == WMouseEvent::RightButton) {
487  // Select the item, it was not yet selected.
488  if (!folderView_->isSelected(item))
489  folderView_->select(item);
490 
491  if (!popup_) {
492  popup_ = new WPopupMenu();
493  popup_->addItem("icons/folder_new.gif", "Create a New Folder");
494  popup_->addItem("Rename this Folder")->setCheckable(true);
495  popup_->addItem("Delete this Folder");
496  popup_->addSeparator();
497  popup_->addItem("Folder Details");
498  popup_->addSeparator();
499  popup_->addItem("Application Inventory");
500  popup_->addItem("Hardware Inventory");
501  popup_->addSeparator();
502 
503  WPopupMenu *subMenu = new WPopupMenu();
504  subMenu->addItem("Sub Item 1");
505  subMenu->addItem("Sub Item 2");
506  popup_->addMenu("File Deployments", subMenu);
507 
508  /*
509  * This is one method of executing a popup, which does not block a
510  * thread for a reentrant event loop, and thus scales.
511  *
512  * Alternatively you could call WPopupMenu::exec(), which returns
513  * the result, but while waiting for it, blocks the thread.
514  */
515  popup_->aboutToHide().connect(this, &TreeViewDragDrop::popupAction);
516  }
517 
518  if (popup_->isHidden())
519  popup_->popup(event);
520  else
521  popup_->hide();
522  }
523  }
WTreeView * folderView_
The folder view.
WPopupMenu * popup_
Popup menu on the folder view.
void popupAction()
Process the result of the popup menu.

Member Data Documentation

§ fileFilterModel_

WSortFilterProxyModel* TreeViewDragDrop::fileFilterModel_
private

The sort filter proxy model that adapts fileModel_.

Definition at line 292 of file TreeViewDragDrop.C.

§ fileModel_

WStandardItemModel* TreeViewDragDrop::fileModel_
private

The file model (used by fileView_)

Definition at line 289 of file TreeViewDragDrop.C.

§ fileView_

WTableView* TreeViewDragDrop::fileView_
private

The file view.

Definition at line 301 of file TreeViewDragDrop.C.

§ folderModel_

WStandardItemModel* TreeViewDragDrop::folderModel_
private

The folder model (used by folderView_)

Definition at line 286 of file TreeViewDragDrop.C.

§ folderNameMap_

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

Maps folder id's to folder descriptions.

Definition at line 295 of file TreeViewDragDrop.C.

§ folderView_

WTreeView* TreeViewDragDrop::folderView_
private

The folder view.

Definition at line 298 of file TreeViewDragDrop.C.

§ popup_

WPopupMenu* TreeViewDragDrop::popup_
private

Popup menu on the folder view.

Definition at line 304 of file TreeViewDragDrop.C.

§ popupActionBox_

WMessageBox* TreeViewDragDrop::popupActionBox_
private

Message box to confirm the poup menu action.

Definition at line 307 of file TreeViewDragDrop.C.


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

Generated on Thu Jan 12 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.12