Wt examples  3.2.1
Public Member Functions | Private Member Functions | Private Attributes
TreeViewDragDrop Class Reference

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.

    : WApplication(env),
      popup_(0),
      popupActionBox_(0)
  {
    setCssTheme("polished");

    /*
     * Create the data models.
     */
    folderModel_ = new WStandardItemModel(0, 1, this);
    populateFolders();

    fileModel_ = new FileModel(this);
    populateFiles();

    /*
      The header items are also endered using an ItemDelegate, and thus
      support other data, e.g.:

      fileModel_->setHeaderFlags(0, Horizontal, HeaderIsUserCheckable);
      fileModel_->setHeaderData(0, Horizontal,
                                std::string("icons/file.gif"),
                                Wt::DecorationRole);
    */
    fileFilterModel_ = new WSortFilterProxyModel(this);
    fileFilterModel_->setSourceModel(fileModel_);
    fileFilterModel_->setDynamicSortFilter(true);
    fileFilterModel_->setFilterKeyColumn(0);
    fileFilterModel_->setFilterRole(UserRole);

    /*
     * Setup the user interface.
     */
    createUI();
  }
virtual TreeViewDragDrop::~TreeViewDragDrop ( ) [inline, virtual]

Definition at line 279 of file TreeViewDragDrop.C.

                              {
    delete popup_;
    delete popupActionBox_;
  }

Member Function Documentation

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

Creates the hints text.

Definition at line 456 of file TreeViewDragDrop.C.

                          {
    WText *result = new WText(WString::tr("about-text"));
    result->setStyleClass("about");
    return result;
  }
void TreeViewDragDrop::convertToDate ( WStandardItem item) [inline, private]

Convert a string to a date.

Definition at line 586 of file TreeViewDragDrop.C.

                                          {
    WDate d = WDate::fromString(item->text(), FileModel::dateEditFormat);
    item->setData(boost::any(d), DisplayRole);
  }
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 623 of file TreeViewDragDrop.C.

  {
    WStandardItem *result = new WStandardItem(location);

    if (!folderId.empty()) {
      result->setData(boost::any(folderId));
      result->setFlags(result->flags() | ItemIsDropEnabled);
      folderNameMap_[folderId] = location;
    } else
      result->setFlags(result->flags().clear(ItemIsSelectable));

    result->setIcon("icons/folder.gif");

    return result;
  }
WText* TreeViewDragDrop::createTitle ( const WString title) [inline, private]

Creates a title widget.

Definition at line 347 of file TreeViewDragDrop.C.

                                           {
    WText *result = new WText(title);
    result->setInline(false);
    result->setStyleClass("title");

    return result;
  }
void TreeViewDragDrop::createUI ( ) [inline, private]

Setup the user interface.

Definition at line 311 of file TreeViewDragDrop.C.

                  {
    WContainerWidget *w = root();
    w->setStyleClass("maindiv");

    /*
     * The main layout is a 3x2 grid layout.
     */
    WGridLayout *layout = new WGridLayout();
    layout->addWidget(createTitle("Folders"), 0, 0);
    layout->addWidget(createTitle("Files"), 0, 1);
    layout->addWidget(folderView(), 1, 0);
    layout->setColumnResizable(0);

    // select the first folder
    folderView_->select(folderModel_->index(0, 0, folderModel_->index(0, 0)));

    WVBoxLayout *vbox = new WVBoxLayout();
    vbox->addWidget(fileView(), 1);
    vbox->addWidget(pieChart(), 1);
    vbox->setResizable(0);

    layout->addLayout(vbox, 1, 1);

    layout->addWidget(aboutDisplay(), 2, 0, 1, 2, AlignTop);

    /*
     * Let row 1 and column 1 take the excess space.
     */
    layout->setRowStretch(1, 1);
    layout->setColumnStretch(1, 1);

    w->setLayout(layout);
  }
void TreeViewDragDrop::dialogDone ( ) [inline, private]

Process the result of the message box.

Definition at line 547 of file TreeViewDragDrop.C.

                    {
    delete popupActionBox_;
    popupActionBox_ = 0;
  }
void TreeViewDragDrop::editFile ( const WModelIndex item) [inline, private]

Edit a particular row.

Definition at line 422 of file TreeViewDragDrop.C.

                                         {
    new FileEditDialog(fileView_->model(), item);
  }
WTableView* TreeViewDragDrop::fileView ( ) [inline, private]

Creates the file table view (a WTableView)

Definition at line 386 of file TreeViewDragDrop.C.

                         {
    WTableView *tableView = new WTableView();

    tableView->setAlternatingRowColors(true);

    tableView->setModel(fileFilterModel_);
    tableView->setSelectionMode(ExtendedSelection);
    tableView->setDragEnabled(true);

    tableView->setColumnWidth(0, 100);
    tableView->setColumnWidth(1, 150);
    tableView->setColumnWidth(2, 100);
    tableView->setColumnWidth(3, 60);
    tableView->setColumnWidth(4, 100);
    tableView->setColumnWidth(5, 100);

    WItemDelegate *delegate = new WItemDelegate(this);
    delegate->setTextFormat(FileModel::dateDisplayFormat);
    tableView->setItemDelegateForColumn(4, delegate);
    tableView->setItemDelegateForColumn(5, delegate);

    tableView->setColumnAlignment(3, AlignRight);
    tableView->setColumnAlignment(4, AlignRight);
    tableView->setColumnAlignment(5, AlignRight);

    tableView->sortByColumn(1, AscendingOrder);

    tableView->doubleClicked().connect(this, &TreeViewDragDrop::editFile);

    fileView_ = tableView;

    return tableView;
  }
void TreeViewDragDrop::folderChanged ( ) [inline, private]

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

Definition at line 465 of file TreeViewDragDrop.C.

                       {
    if (folderView_->selectedIndexes().empty())
      return;

    WModelIndex selected = *folderView_->selectedIndexes().begin();
    boost::any d = selected.data(UserRole);
    if (!d.empty()) {
      std::string folder = boost::any_cast<std::string>(d);

      // For simplicity, we assume here that the folder-id does not
      // contain special regexp characters, otherwise these need to be
      // escaped -- or use the \Q \E qutoing escape regular expression
      // syntax (and escape \E)
      fileFilterModel_->setFilterRegExp(folder);
    }
  }
WTreeView* TreeViewDragDrop::folderView ( ) [inline, private]

Creates the folder WTreeView.

Definition at line 357 of file TreeViewDragDrop.C.

                          {
    WTreeView *treeView = new FolderView();

    /*
     * To support right-click, we need to disable the built-in browser
     * context menu.
     *
     * Note that disabling the context menu and catching the
     * right-click does not work reliably on all browsers.
     */
    treeView->setAttributeValue
      ("oncontextmenu",
       "event.cancelBubble = true; event.returnValue = false; return false;");
    treeView->setModel(folderModel_);
    treeView->resize(200, WLength::Auto);
    treeView->setSelectionMode(SingleSelection);
    treeView->expandToDepth(1);
    treeView->selectionChanged()
      .connect(this, &TreeViewDragDrop::folderChanged);

    treeView->mouseWentUp().connect(this, &TreeViewDragDrop::showPopup);

    folderView_ = treeView;

    return treeView;
  }
WWidget* TreeViewDragDrop::pieChart ( ) [inline, private]

Creates the chart.

Definition at line 428 of file TreeViewDragDrop.C.

                      {
    using namespace Chart;

    WPieChart *chart = new WPieChart();
    chart->setModel(fileFilterModel_);
    chart->setTitle("File sizes");

    chart->setLabelsColumn(1); // Name
    chart->setDataColumn(3);   // Size

    chart->setPerspectiveEnabled(true, 0.2);
    chart->setDisplayLabels(Outside | TextLabel);

    if (!WApplication::instance()->environment().ajax()) {
      chart->resize(500, 200);
      chart->setMargin(WLength::Auto, Left | Right);
      WContainerWidget *w = new WContainerWidget();
      w->addWidget(chart);
      w->setStyleClass("about");
      return w;
    } else {
      chart->setStyleClass("about");
      return chart;
    }
  }
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 559 of file TreeViewDragDrop.C.

                       {
    fileModel_->invisibleRootItem()->setRowCount(0);

    std::ifstream f((appRoot() + "data/files.csv").c_str());

    if (!f)
      throw std::runtime_error("Could not read: data/files.csv");

    readFromCsv(f, fileModel_);

    for (int i = 0; i < fileModel_->rowCount(); ++i) {
      WStandardItem *item = fileModel_->item(i, 0);
      item->setFlags(item->flags() | ItemIsDragEnabled);
      item->setIcon("icons/file.gif");

      std::string folderId = item->text().toUTF8();

      item->setData(boost::any(folderId), UserRole);
      item->setText(folderNameMap_[folderId]);

      convertToDate(fileModel_->item(i, 4));
      convertToDate(fileModel_->item(i, 5));
    }
  }
void TreeViewDragDrop::populateFolders ( ) [inline, private]

Populate the folders model.

Definition at line 593 of file TreeViewDragDrop.C.

                         {
    WStandardItem *level1, *level2;

    folderModel_->appendRow(level1 = createFolderItem("San Fransisco"));
    level1->appendRow(level2 = createFolderItem("Investors", "sf-investors"));
    level1->appendRow(level2 = createFolderItem("Fellows", "sf-fellows"));

    folderModel_->appendRow(level1 = createFolderItem("Sophia Antipolis"));
    level1->appendRow(level2 = createFolderItem("R&D", "sa-r_d"));
    level1->appendRow(level2 = createFolderItem("Services", "sa-services"));
    level1->appendRow(level2 = createFolderItem("Support", "sa-support"));
    level1->appendRow(level2 = createFolderItem("Billing", "sa-billing"));

    folderModel_->appendRow(level1 = createFolderItem("New York"));
    level1->appendRow(level2 = createFolderItem("Marketing", "ny-marketing"));
    level1->appendRow(level2 = createFolderItem("Sales", "ny-sales"));
    level1->appendRow(level2 = createFolderItem("Advisors", "ny-advisors"));

    folderModel_->appendRow(level1 = createFolderItem
                             (WString::fromUTF8("Frankfürt")));
    level1->appendRow(level2 = createFolderItem("Sales", "frank-sales"));

    folderModel_->setHeaderData(0, Horizontal,
                                 boost::any(std::string("SandBox")));
  }
void TreeViewDragDrop::popupAction ( ) [inline, private]

Process the result of the popup menu.

Definition at line 526 of file TreeViewDragDrop.C.

                     {
    if (popup_->result()) {
      /*
       * You could also bind extra data to an item using setData() and
       * check here for the action asked. For now, we just use the text.
       */
      WString text = popup_->result()->text();
      popup_->hide();

      popupActionBox_ = new WMessageBox("Sorry.","Action '" + text
                                        + "' is not implemented.", NoIcon, Ok);
      popupActionBox_->buttonClicked()
        .connect(this, &TreeViewDragDrop::dialogDone);
      popupActionBox_->show();
    } else {
      popup_->hide();
    }
  }
void TreeViewDragDrop::showPopup ( const WModelIndex item,
const WMouseEvent event 
) [inline, private]

Show a popup for a folder item.

Definition at line 484 of file TreeViewDragDrop.C.

                                                                    {
    if (event.button() == WMouseEvent::RightButton) {
      // Select the item, it was not yet selected.
      if (!folderView_->isSelected(item))
        folderView_->select(item);

      if (!popup_) {
        popup_ = new WPopupMenu();
        popup_->addItem("icons/folder_new.gif", "Create a New Folder");
        popup_->addItem("Rename this Folder")->setCheckable(true);
        popup_->addItem("Delete this Folder");
        popup_->addSeparator();
        popup_->addItem("Folder Details");
        popup_->addSeparator();
        popup_->addItem("Application Inventory");
        popup_->addItem("Hardware Inventory");
        popup_->addSeparator();

        WPopupMenu *subMenu = new WPopupMenu();
        subMenu->addItem("Sub Item 1");
        subMenu->addItem("Sub Item 2");
        popup_->addMenu("File Deployments", subMenu);

        /*
         * This is one method of executing a popup, which does not block a
         * thread for a reentrant event loop, and thus scales.
         *
         * Alternatively you could call WPopupMenu::exec(), which returns
         * the result, but while waiting for it, blocks the thread.
         */      
        popup_->aboutToHide().connect(this, &TreeViewDragDrop::popupAction);
      }

      if (popup_->isHidden())
        popup_->popup(event);
      else
        popup_->hide();
    }
  }

Member Data Documentation

The sort filter proxy model that adapts fileModel_.

Definition at line 292 of file TreeViewDragDrop.C.

The file model (used by fileView_)

Definition at line 289 of file TreeViewDragDrop.C.

The file view.

Definition at line 301 of file TreeViewDragDrop.C.

The folder model (used by folderView_)

Definition at line 286 of file TreeViewDragDrop.C.

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

Maps folder id's to folder descriptions.

Definition at line 295 of file TreeViewDragDrop.C.

The folder view.

Definition at line 298 of file TreeViewDragDrop.C.

Popup menu on the folder view.

Definition at line 304 of file TreeViewDragDrop.C.

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 Fri Mar 30 2012 for the C++ Web Toolkit (Wt) by doxygen 1.7.5.1