Wt examples  4.10.3
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
TreeViewDragDrop Class Reference

Main application class. More...

Inheritance diagram for TreeViewDragDrop:
[legend]

Public Member Functions

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

Private Member Functions

void createUI ()
 Setup the user interface.
 
std::unique_ptr< WText > createTitle (const WString &title)
 Creates a title widget.
 
std::unique_ptr< WTreeView > folderView ()
 Creates the folder WTreeView.
 
std::unique_ptr< WTableViewfileView ()
 Creates the file table view (a WTableView)
 
void editFile (const WModelIndex &item)
 Edit a particular row.
 
std::unique_ptr< WWidgetpieChart ()
 Creates the chart.
 
std::unique_ptr< 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 convertToNumber (WStandardItem *item)
 Convert a string to a number.
 
void populateFolders ()
 Populate the folders model.
 
std::unique_ptr< WStandardItem > createFolderItem (const WString &location, const std::string &folderId=std::string())
 Create a folder item.
 

Private Attributes

std::shared_ptr< WStandardItemModel > folderModel_
 The folder model (used by folderView_)
 
std::shared_ptr< WStandardItemModel > fileModel_
 The file model (used by fileView_)
 
std::shared_ptr< WSortFilterProxyModelfileFilterModel_
 The sort filter proxy model that adapts fileModel_.
 
std::map< std::string, WStringfolderNameMap_
 Maps folder id's to folder descriptions.
 
WTreeView * folderView_
 The folder view.
 
WTableViewfileView_
 The file view.
 
std::unique_ptr< FileEditDialogdialog_
 
std::unique_ptr< WPopupMenupopup_
 Popup menu on the folder view.
 
std::unique_ptr< WMessageBoxpopupActionBox_
 Message box to confirm the poup menu action.
 

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 */
269 std::make_shared<WStandardItemModel>(0, 1);
271
272
273 fileModel_ =
274 std::make_shared<FileModel>();
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 }
Wt::Auth::Dbo::UserDatabase< AuthInfo > UserDatabase
Definition Session.h:22
std::shared_ptr< WStandardItemModel > fileModel_
The file model (used by fileView_)
std::shared_ptr< WSortFilterProxyModel > fileFilterModel_
The sort filter proxy model that adapts fileModel_.
void createUI()
Setup the user interface.
std::shared_ptr< WStandardItemModel > folderModel_
The folder model (used by folderView_)
std::unique_ptr< WMessageBox > popupActionBox_
Message box to confirm the poup menu action.
std::unique_ptr< WPopupMenu > popup_
Popup menu on the folder view.
void populateFiles()
Populate the files model.
void populateFolders()
Populate the folders model.

◆ ~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 485 of file TreeViewDragDrop.C.

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

◆ convertToDate()

void TreeViewDragDrop::convertToDate ( WStandardItem *  item)
inlineprivate

Convert a string to a date.

Definition at line 618 of file TreeViewDragDrop.C.

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

◆ convertToNumber()

void TreeViewDragDrop::convertToNumber ( WStandardItem *  item)
inlineprivate

Convert a string to a number.

Definition at line 625 of file TreeViewDragDrop.C.

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

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

667 {
668 auto result
669 = std::make_unique<WStandardItem>(location);
670
671 if (!folderId.empty()) {
672 result->setData(cpp17::any(folderId));
673 result->setFlags(result->flags() | ItemFlag::DropEnabled);
675 } else
676 result->setFlags(result->flags().clear(ItemFlag::Selectable));
677
678 result->setIcon("icons/folder.gif");
679
680 return result;
681 }
std::map< std::string, WString > folderNameMap_
Maps folder id'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 = std::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 std::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 std::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::unique_ptr< WWidget > aboutDisplay()
Creates the hints text.
std::unique_ptr< WTreeView > folderView()
Creates the folder WTreeView.
std::unique_ptr< WText > createTitle(const WString &title)
Creates a title widget.
std::unique_ptr< WTableView > fileView()
Creates the file table view (a WTableView)
std::unique_ptr< WWidget > pieChart()
Creates the chart.
WTreeView * folderView_
The folder view.

◆ dialogDone()

void TreeViewDragDrop::dialogDone ( )
inlineprivate

Process the result of the message box.

Definition at line 579 of file TreeViewDragDrop.C.

579 {
580 popupActionBox_.reset();
581 }

◆ editFile()

void TreeViewDragDrop::editFile ( const WModelIndex item)
inlineprivate

Edit a particular row.

Definition at line 449 of file TreeViewDragDrop.C.

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

◆ fileView()

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

Creates the file table view (a WTableView)

Definition at line 411 of file TreeViewDragDrop.C.

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

◆ folderChanged()

void TreeViewDragDrop::folderChanged ( )
inlineprivate

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

Definition at line 495 of file TreeViewDragDrop.C.

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

◆ folderView()

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

Creates the folder WTreeView.

Definition at line 381 of file TreeViewDragDrop.C.

381 {
382 auto treeView = std::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->setEnabledDropLocations(DropLocation::OnItem);
398 treeView->expandToDepth(1);
399 treeView->selectionChanged()
400 .connect(this, &TreeViewDragDrop::folderChanged);
401
402 treeView->mouseWentUp().connect(this, &TreeViewDragDrop::showPopup);
403
404 folderView_ = treeView.get();
405
406 return std::move(treeView);
407 }
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.

◆ pieChart()

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

Creates the chart.

Definition at line 455 of file TreeViewDragDrop.C.

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

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

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

◆ populateFolders()

void TreeViewDragDrop::populateFolders ( )
inlineprivate

Populate the folders model.

Definition at line 632 of file TreeViewDragDrop.C.

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

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

◆ showPopup()

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

Show a popup for a folder item.

Definition at line 514 of file TreeViewDragDrop.C.

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