Wt examples  4.0.2
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...
 
std::unique_ptr< WText > createTitle (const WString &title)
 Creates a title widget. More...
 
std::unique_ptr< WTreeView > folderView ()
 Creates the folder WTreeView. More...
 
std::unique_ptr< WTableView > fileView ()
 Creates the file table view (a WTableView) More...
 
void editFile (const WModelIndex &item)
 Edit a particular row. More...
 
std::unique_ptr< WWidget > pieChart ()
 Creates the chart. More...
 
std::unique_ptr< 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...
 
std::unique_ptr< WStandardItem > createFolderItem (const WString &location, const std::string &folderId=std::string())
 Create a folder item. More...
 

Private Attributes

std::shared_ptr< WStandardItemModel > folderModel_
 The folder model (used by folderView_) More...
 
std::shared_ptr< WStandardItemModel > fileModel_
 The file model (used by fileView_) More...
 
std::shared_ptr< 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...
 
std::unique_ptr< FileEditDialogdialog_
 
std::unique_ptr< WPopupMenu > popup_
 Popup menu on the folder view. More...
 
std::unique_ptr< WMessageBox > popupActionBox_
 Message box to confirm the poup menu action. More...
 

Detailed Description

Main application class.

Definition at line 253 of file TreeViewDragDrop.C.

Constructor & Destructor Documentation

§ TreeViewDragDrop()

TreeViewDragDrop::TreeViewDragDrop ( const WEnvironment &  env)
inline

Constructor.

Definition at line 258 of file TreeViewDragDrop.C.

259  : WApplication(env),
260  popup_(nullptr),
261  popupActionBox_(nullptr)
262  {
263  setCssTheme("polished");
264 
265  /*
266  * Create the data models.
267  */
268  folderModel_ =
269  std::make_shared<WStandardItemModel>(0, 1);
270  populateFolders();
271 
272 
273  fileModel_ =
274  std::make_shared<FileModel>();
275  populateFiles();
276 
277  /*
278  * The header items are also endered using an ItemDelegate, and thus
279  * support other data, e.g.:
280  *
281  * fileModel_->setHeaderFlags(0, Horizontal, HeaderIsUserCheckable);
282  * fileModel_->setHeaderData(0, Horizontal,
283  * std::string("icons/file.gif"),
284  * Wt::DecorationRole);
285  */
286  fileFilterModel_ = std::make_shared<WSortFilterProxyModel>();
287  fileFilterModel_->setSourceModel(fileModel_);
288  fileFilterModel_->setDynamicSortFilter(true);
289  fileFilterModel_->setFilterKeyColumn(0);
290  fileFilterModel_->setFilterRole(ItemDataRole::User);
291 
292  /*
293  * Setup the user interface.
294  */
295  createUI();
296 
297  }
std::shared_ptr< WStandardItemModel > folderModel_
The folder model (used by folderView_)
void populateFiles()
Populate the files model.
std::shared_ptr< WSortFilterProxyModel > fileFilterModel_
The sort filter proxy model that adapts fileModel_.
void createUI()
Setup the user interface.
std::unique_ptr< WPopupMenu > popup_
Popup menu on the folder view.
void populateFolders()
Populate the folders model.
std::shared_ptr< WStandardItemModel > fileModel_
The file model (used by fileView_)
std::unique_ptr< WMessageBox > popupActionBox_
Message box to confirm the poup menu action.

§ ~TreeViewDragDrop()

virtual TreeViewDragDrop::~TreeViewDragDrop ( )
inlinevirtual

Definition at line 299 of file TreeViewDragDrop.C.

300  {
301  dialog_.reset();
302  }
std::unique_ptr< FileEditDialog > dialog_

Member Function Documentation

§ aboutDisplay()

std::unique_ptr<WWidget> TreeViewDragDrop::aboutDisplay ( )
inlineprivate

Creates the hints text.

Definition at line 484 of file TreeViewDragDrop.C.

484  {
485  std::unique_ptr<WText> result
486  = cpp14::make_unique<WText>(WString::tr("about-text"));
487  result->setStyleClass("about");
488  return std::move(result);
489  }

§ convertToDate()

void TreeViewDragDrop::convertToDate ( WStandardItem *  item)
inlineprivate

Convert a string to a date.

Definition at line 617 of file TreeViewDragDrop.C.

617  {
618  WDate d = WDate::fromString(item->text(), FileModel::dateEditFormat);
619  item->setData(cpp17::any(d), ItemDataRole::Display);
620  }
static WString dateEditFormat
Date edit format.

§ convertToNumber()

void TreeViewDragDrop::convertToNumber ( WStandardItem *  item)
inlineprivate

Convert a string to a number.

Definition at line 624 of file TreeViewDragDrop.C.

624  {
625  int i = asNumber(item->text());
626  item->setData(cpp17::any(i), ItemDataRole::Edit);
627  }

§ createFolderItem()

std::unique_ptr<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 664 of file TreeViewDragDrop.C.

666  {
667  auto result
668  = cpp14::make_unique<WStandardItem>(location);
669 
670  if (!folderId.empty()) {
671  result->setData(cpp17::any(folderId));
672  result->setFlags(result->flags() | ItemFlag::DropEnabled);
673  folderNameMap_[folderId] = location;
674  } else
675  result->setFlags(result->flags().clear(ItemFlag::Selectable));
676 
677  result->setIcon("icons/folder.gif");
678 
679  return result;
680  }
std::map< std::string, WString > folderNameMap_
Maps folder id&#39;s to folder descriptions.

§ createTitle()

std::unique_ptr<WText> TreeViewDragDrop::createTitle ( const WString &  title)
inlineprivate

Creates a title widget.

Definition at line 371 of file TreeViewDragDrop.C.

371  {
372  auto result = cpp14::make_unique<WText>(title);
373  result->setInline(false);
374  result->setStyleClass("title");
375 
376  return result;
377  }

§ createUI()

void TreeViewDragDrop::createUI ( )
inlineprivate

Setup the user interface.

Definition at line 333 of file TreeViewDragDrop.C.

333  {
334  WContainerWidget *w = root();
335  w->setStyleClass("maindiv");
336 
337  /*
338  * The main layout is a 3x2 grid layout.
339  */
340  std::unique_ptr<WGridLayout> layout =
341  cpp14::make_unique<WGridLayout>();
342  layout->addWidget(createTitle("Folders"), 0, 0);
343  layout->addWidget(createTitle("Files"), 0, 1);
344  layout->addWidget(folderView(), 1, 0);
345  layout->setColumnResizable(0);
346 
347  // select the first folder
348  folderView_->select(folderModel_->index(0, 0, folderModel_->index(0, 0)));
349 
350  std::unique_ptr<WVBoxLayout> vbox =
351  cpp14::make_unique<WVBoxLayout>();
352  vbox->addWidget(fileView(), 1);
353  vbox->addWidget(pieChart(), 1);
354  vbox->setResizable(0);
355 
356  layout->addLayout(std::move(vbox), 1, 1);
357 
358  layout->addWidget(aboutDisplay(), 2, 0, 1, 2);
359 
360  /*
361  * Let row 1 and column 1 take the excess space.
362  */
363  layout->setRowStretch(1, 1);
364  layout->setColumnStretch(1, 1);
365 
366  w->setLayout(std::move(layout));
367  }
std::shared_ptr< WStandardItemModel > folderModel_
The folder model (used by folderView_)
std::unique_ptr< WTreeView > folderView()
Creates the folder WTreeView.
std::unique_ptr< WTableView > fileView()
Creates the file table view (a WTableView)
WTreeView * folderView_
The folder view.
std::unique_ptr< WText > createTitle(const WString &title)
Creates a title widget.
std::unique_ptr< WWidget > pieChart()
Creates the chart.
std::unique_ptr< WWidget > aboutDisplay()
Creates the hints text.

§ dialogDone()

void TreeViewDragDrop::dialogDone ( )
inlineprivate

Process the result of the message box.

Definition at line 578 of file TreeViewDragDrop.C.

578  {
579  popupActionBox_.reset();
580  }
std::unique_ptr< 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 448 of file TreeViewDragDrop.C.

448  {
449  dialog_ = cpp14::make_unique<FileEditDialog>(fileView_->model(), item);
450  }
std::unique_ptr< FileEditDialog > dialog_
WTableView * fileView_
The file view.

§ fileView()

std::unique_ptr<WTableView> TreeViewDragDrop::fileView ( )
inlineprivate

Creates the file table view (a WTableView)

Definition at line 410 of file TreeViewDragDrop.C.

410  {
411  auto tableView
412  = cpp14::make_unique<WTableView>();
413 
414  tableView->setAlternatingRowColors(true);
415 
416  tableView->setModel(fileFilterModel_);
417  tableView->setSelectionMode(SelectionMode::Extended);
418  tableView->setDragEnabled(true);
419 
420  tableView->setColumnWidth(0, 100);
421  tableView->setColumnWidth(1, 150);
422  tableView->setColumnWidth(2, 100);
423  tableView->setColumnWidth(3, 60);
424  tableView->setColumnWidth(4, 100);
425  tableView->setColumnWidth(5, 100);
426 
427  auto delegate = std::make_shared<WItemDelegate>();
428  delegate->setTextFormat(FileModel::dateDisplayFormat);
429  tableView->setItemDelegateForColumn(4, delegate);
430  tableView->setItemDelegateForColumn(5, delegate);
431 
432  tableView->setColumnAlignment(3, AlignmentFlag::Right);
433  tableView->setColumnAlignment(4, AlignmentFlag::Right);
434  tableView->setColumnAlignment(5, AlignmentFlag::Right);
435 
436  tableView->sortByColumn(1, SortOrder::Ascending);
437 
438  tableView->doubleClicked().connect(this, std::bind(&TreeViewDragDrop::editFile,
439  this, std::placeholders::_1));
440 
441  fileView_ = tableView.get();
442 
443  return tableView;
444  }
static WString dateDisplayFormat
Date display format.
std::shared_ptr< 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 494 of file TreeViewDragDrop.C.

494  {
495  if (folderView_->selectedIndexes().empty())
496  return;
497 
498  WModelIndex selected = *folderView_->selectedIndexes().begin();
499  cpp17::any d = selected.data(ItemDataRole::User);
500  if (!d.empty()) {
501  std::string folder = cpp17::any_cast<std::string>(d);
502 
503  // For simplicity, we assume here that the folder-id does not
504  // contain special regexp characters, otherwise these need to be
505  // escaped -- or use the \Q \E qutoing escape regular expression
506  // syntax (and escape \E)
507  fileFilterModel_->setFilterRegExp(std::unique_ptr<std::regex>(new std::regex(folder)));
508  }
509  }
WTreeView * folderView_
The folder view.
std::shared_ptr< WSortFilterProxyModel > fileFilterModel_
The sort filter proxy model that adapts fileModel_.

§ folderView()

std::unique_ptr<WTreeView> TreeViewDragDrop::folderView ( )
inlineprivate

Creates the folder WTreeView.

Definition at line 381 of file TreeViewDragDrop.C.

381  {
382  auto treeView = cpp14::make_unique<FolderView>();
383 
384  /*
385  * To support right-click, we need to disable the built-in browser
386  * context menu.
387  *
388  * Note that disabling the context menu and catching the
389  * right-click does not work reliably on all browsers.
390  */
391  treeView->setAttributeValue
392  ("oncontextmenu",
393  "event.cancelBubble = true; event.returnValue = false; return false;");
394  treeView->setModel(folderModel_);
395  treeView->resize(200, WLength::Auto);
396  treeView->setSelectionMode(SelectionMode::Single);
397  treeView->expandToDepth(1);
398  treeView->selectionChanged()
399  .connect(this, &TreeViewDragDrop::folderChanged);
400 
401  treeView->mouseWentUp().connect(this, &TreeViewDragDrop::showPopup);
402 
403  folderView_ = treeView.get();
404 
405  return std::move(treeView);
406  }
std::shared_ptr< WStandardItemModel > folderModel_
The folder model (used by folderView_)
WTreeView * folderView_
The folder view.
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()

std::unique_ptr<WWidget> TreeViewDragDrop::pieChart ( )
inlineprivate

Creates the chart.

Definition at line 454 of file TreeViewDragDrop.C.

454  {
455  using namespace Chart;
456 
457  auto chart = cpp14::make_unique<WPieChart>();
458  // chart->setPreferredMethod(WPaintedWidget::PngImage);
459  chart->setModel(fileFilterModel_);
460  chart->setTitle("File sizes");
461 
462  chart->setLabelsColumn(1); // Name
463  chart->setDataColumn(3); // Size
464 
465  chart->setPerspectiveEnabled(true, 0.2);
466  chart->setDisplayLabels(LabelOption::Outside | LabelOption::TextLabel);
467 
468  if (!WApplication::instance()->environment().ajax()) {
469  chart->resize(500, 200);
470  chart->setMargin(WLength::Auto, Side::Left | Side::Right);
471 
472  auto w = cpp14::make_unique<WContainerWidget>();
473  w->addWidget(std::move(chart));
474  w->setStyleClass("about");
475  return std::move(w);
476  } else {
477  chart->setStyleClass("about");
478  return std::move(chart);
479  }
480  }
std::shared_ptr< 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 589 of file TreeViewDragDrop.C.

589  {
590  fileModel_->invisibleRootItem()->setRowCount(0);
591 
592  std::ifstream f((appRoot() + "data/files.csv").c_str());
593 
594  if (!f)
595  throw std::runtime_error("Could not read: data/files.csv");
596 
598 
599  for (int i = 0; i < fileModel_->rowCount(); ++i) {
600  WStandardItem *item = fileModel_->item(i, 0);
601  item->setFlags(item->flags() | ItemFlag::DragEnabled);
602  item->setIcon("icons/file.gif");
603 
604  std::string folderId = item->text().toUTF8();
605 
606  item->setData(cpp17::any(folderId), ItemDataRole::User);
607  item->setText(folderNameMap_[folderId]);
608 
609  convertToNumber(fileModel_->item(i, 3));
610  convertToDate(fileModel_->item(i, 4));
611  convertToDate(fileModel_->item(i, 5));
612  }
613  }
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, std::shared_ptr< WAbstractItemModel > model, int numRows, bool firstLineIsHeaders)
Definition: CsvUtil.C:54
std::shared_ptr< WStandardItemModel > fileModel_
The file model (used by fileView_)

§ populateFolders()

void TreeViewDragDrop::populateFolders ( )
inlineprivate

Populate the folders model.

Definition at line 631 of file TreeViewDragDrop.C.

631  {
632  std::unique_ptr<WStandardItem> level1;
633 
634  level1 = createFolderItem("San Fransisco");
635  level1->appendRow(createFolderItem("Investors", "sf-investors"));
636  level1->appendRow(createFolderItem("Fellows", "sf-fellows"));
637  folderModel_->appendRow(std::move(level1));
638 
639  level1 = createFolderItem("Sophia Antipolis");
640  level1->appendRow(createFolderItem("R&D", "sa-r_d"));
641  level1->appendRow(createFolderItem("Services", "sa-services"));
642  level1->appendRow(createFolderItem("Support", "sa-support"));
643  level1->appendRow(createFolderItem("Billing", "sa-billing"));
644  folderModel_->appendRow(std::move(level1));
645 
646  level1 = createFolderItem("New York");
647  level1->appendRow(createFolderItem("Marketing", "ny-marketing"));
648  level1->appendRow(createFolderItem("Sales", "ny-sales"));
649  level1->appendRow(createFolderItem("Advisors", "ny-advisors"));
650  folderModel_->appendRow(std::move(level1));
651 
652  level1 = createFolderItem(WString(u8"Frankf\u00DCrt"));
653  level1->appendRow(createFolderItem("Sales", "frank-sales"));
654  folderModel_->appendRow(std::move(level1));
655 
656  folderModel_->setHeaderData(0, Orientation::Horizontal,
657  cpp17::any(std::string("SandBox")));
658  }
std::shared_ptr< WStandardItemModel > folderModel_
The folder model (used by folderView_)
std::unique_ptr< 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 555 of file TreeViewDragDrop.C.

555  {
556  if (popup_->result()) {
557  /*
558  * You could also bind extra data to an item using setData() and
559  * check here for the action asked. For now, we just use the text.
560  */
561  WString text = popup_->result()->text();
562  popup_->hide();
563 
564  popupActionBox_ = cpp14::make_unique<WMessageBox>("Sorry.","Action '"
565  + text + "' is not implemented.",
566  Icon::None,
567  StandardButton::Ok);
568  popupActionBox_->buttonClicked()
569  .connect(this, &TreeViewDragDrop::dialogDone);
570  popupActionBox_->show();
571  } else {
572  popup_->hide();
573  }
574  }
void dialogDone()
Process the result of the message box.
std::unique_ptr< WPopupMenu > popup_
Popup menu on the folder view.
std::unique_ptr< WMessageBox > popupActionBox_
Message box to confirm the poup menu action.

§ showPopup()

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

Show a popup for a folder item.

Definition at line 513 of file TreeViewDragDrop.C.

513  {
514  if (event.button() == MouseButton::Right) {
515  // Select the item, it was not yet selected.
516  if (!folderView_->isSelected(item))
517  folderView_->select(item);
518 
519  if (!popup_) {
520  popup_ = cpp14::make_unique<WPopupMenu>();
521  popup_->addItem("icons/folder_new.gif", "Create a New Folder");
522  popup_->addItem("Rename this Folder")->setCheckable(true);
523  popup_->addItem("Delete this Folder");
524  popup_->addSeparator();
525  popup_->addItem("Folder Details");
526  popup_->addSeparator();
527  popup_->addItem("Application Inventory");
528  popup_->addItem("Hardware Inventory");
529  popup_->addSeparator();
530 
531  std::unique_ptr<WPopupMenu> subMenu = cpp14::make_unique<WPopupMenu>();
532  subMenu->addItem("Sub Item 1");
533  subMenu->addItem("Sub Item 2");
534  popup_->addMenu("File Deployments", std::move(subMenu));
535 
536  /*
537  * This is one method of executing a popup, which does not block a
538  * thread for a reentrant event loop, and thus scales.
539  *
540  * Alternatively you could call WPopupMenu::exec(), which returns
541  * the result, but while waiting for it, blocks the thread.
542  */
543  popup_->aboutToHide().connect(this, &TreeViewDragDrop::popupAction);
544  }
545 
546  if (popup_->isHidden())
547  popup_->popup(event);
548  else
549  popup_->hide();
550  }
551  }
WTreeView * folderView_
The folder view.
std::unique_ptr< WPopupMenu > popup_
Popup menu on the folder view.
void popupAction()
Process the result of the popup menu.

Member Data Documentation

§ dialog_

std::unique_ptr<FileEditDialog> TreeViewDragDrop::dialog_
private

Definition at line 323 of file TreeViewDragDrop.C.

§ fileFilterModel_

std::shared_ptr<WSortFilterProxyModel> TreeViewDragDrop::fileFilterModel_
private

The sort filter proxy model that adapts fileModel_.

Definition at line 312 of file TreeViewDragDrop.C.

§ fileModel_

std::shared_ptr<WStandardItemModel> TreeViewDragDrop::fileModel_
private

The file model (used by fileView_)

Definition at line 309 of file TreeViewDragDrop.C.

§ fileView_

WTableView* TreeViewDragDrop::fileView_
private

The file view.

Definition at line 321 of file TreeViewDragDrop.C.

§ folderModel_

std::shared_ptr<WStandardItemModel> TreeViewDragDrop::folderModel_
private

The folder model (used by folderView_)

Definition at line 306 of file TreeViewDragDrop.C.

§ folderNameMap_

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

Maps folder id's to folder descriptions.

Definition at line 315 of file TreeViewDragDrop.C.

§ folderView_

WTreeView* TreeViewDragDrop::folderView_
private

The folder view.

Definition at line 318 of file TreeViewDragDrop.C.

§ popup_

std::unique_ptr<WPopupMenu> TreeViewDragDrop::popup_
private

Popup menu on the folder view.

Definition at line 326 of file TreeViewDragDrop.C.

§ popupActionBox_

std::unique_ptr<WMessageBox> TreeViewDragDrop::popupActionBox_
private

Message box to confirm the poup menu action.

Definition at line 329 of file TreeViewDragDrop.C.


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

Generated on Wed May 30 2018 for the C++ Web Toolkit (Wt) by doxygen 1.8.12