// $Id: ClientDlg.cpp $ // // class ClientDlg : public QDialog (or something) // // See also file: ClientDlg_EntityItemModel.cpp: // class ClientDlg::EntityItemModel : public QAbstractItemModel // class ClientDlg::EntityItemData // //-- #ifndef ClientDlgINCLUDED #include "ClientDlg.hpp" #endif ... ... ... ... ... ... //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- void ClientDlg::buildEntityTreeView() { QVBoxLayout* lout = new QVBoxLayout (_ui._entityTreeViewFrame); // XXX lout->setObjectName ("_ui._entityTreeViewFrame lout"); lout->setSpacing (0); lout->setMargin (0); // ********************************* // *** Create Entity Tree View *** // ********************************* _entityTreeView = new QTreeView (this); _entityTreeView->setObjectName ("_entityTreeView"); _entityTreeView->setRootIsDecorated (false); _entityTreeView->setAllColumnsShowFocus (true); _entityTreeView->setSelectionMode (QTreeView::ExtendedSelection); _entityTreeView->setSortingEnabled (true); _entityTreeView->setIconSize (RwQPixmap16::seriesSlot().size()); // XXX _entityTreeView->header()->setResizeMode (QHeaderView::ResizeToContents); lout->addWidget (_entityTreeView); // ******************************************************* // *** Create Entity Item Model and Sort Proxy Model *** // ******************************************************* _entityItemModel = new EntityItemModel (this); _entityItemModel->setObjectName ("_entityItemModel"); _entitySortProxyModel = new RwQSortFilterProxyModel (this, _entityItemModel, _entityTreeView, EntityItemModel::COLUMN_COUNT); _entitySortProxyModel->setObjectName ("_entitySortProxyModel"); _entitySortProxyModel->setSourceModel (_entityItemModel); _entitySortProxyModel->setDynamicSortFilter (true); _entitySortProxyModel->setSortRole (EntityItemModel::UserSortRole); _entityTreeView->setModel (_entitySortProxyModel); _entityTreeView->update(); // ************************* // *** Connect Signals *** // ************************* connect (_entityTreeView, SIGNAL (doubleClicked (const QModelIndex&)), SLOT (slotTreeView_doubleClicked (const QModelIndex&))); _entityTreeView->setContextMenuPolicy (Qt::CustomContextMenu); connect (_entityTreeView, SIGNAL (customContextMenuRequested (const QPoint&)), SLOT (slotTreeView_customContextMenuReqAtPoint (const QPoint&))); QItemSelectionModel* selModel = _entityTreeView->selectionModel(); connect (selModel, SIGNAL (selectionChanged (const QItemSelection&, const QItemSelection&)), SLOT (slotTreeView_selectionChanged (const QItemSelection&, const QItemSelection&) )); } //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- QList ClientDlg::allEntityItems() const { QList entityItemList; const int rowCnt = _entityItemModel->rowCount(); for (int row = 0; row < rowCnt; ++row) { EntityItemData* itemDat = _entityItemModel->getItemData (row); if (itemDat) { entityItemList.append (*itemDat); } } return entityItemList; } //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- QList ClientDlg::selectedEntityItems() const { QList entityItemList; const QItemSelectionModel* selModel = _entityTreeView->selectionModel(); const int rowCnt = _entitySortProxyModel->rowCount(); for (int row = 0; row < rowCnt; ++row) { // Process the next selected row ... if (selModel->isRowSelected (row, QModelIndex())) { // Map the Sort Proxy Model's row index to the Source Model row index. QModelIndex proxyRowIndex = _entitySortProxyModel->index (row, 0); QModelIndex modelRowIndex = _entitySortProxyModel->mapToSource (proxyRowIndex); int modelRow = modelRowIndex.row(); EntityItemData* itemDat = _entityItemModel->getItemData (modelRow); if (itemDat) { entityItemList.append (*itemDat); } } } return entityItemList; } //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- void ClientDlg::entityTreeView_doubleClicked (const QModelIndex& minx) { QModelIndex modelRowIndex = _entitySortProxyModel->mapToSource (minx); const int modelRow = modelRowIndex.row(); EXAMPLE ... SlotColRef entityColRef = _entityItemModel->getSlotColRef (modelRow); Slot* theSlot (entityColRef.entity()); // Open the dialogs for the entity if (theSlot) { QGui::openSlot (theSlot); } } //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- void ClientDlg::entityTreeView_customContextMenuReqAtPoint ( const QPoint& widgetPos) { static const char* mname ("ClientDlg:: entityTreeView_customContextMenuReqAtPoint"); QTreeView* tv = _entityTreeView; // Hack. For some reason, in Qt 4.4.3 [January 2010], the height of the // column header is not taken into account. That's really strange. // And strangely, the offset methods don't do the trick. They return 0. // //-- const QPoint adjWidPos (tv->widgetPos.x() + tv->horizontalOffset(), //-- tv->widgetPos.y() + tv->verticalOffset()); // QPoint adjWidPos = widgetPos; if (!tv->isHeaderHidden()) { QHeaderView* hdrView = tv->header(); if (hdrView) adjWidPos += QPoint (0, hdrView->height()); } // Map adjusted widget position to global position const QPoint globalPos = tv->mapToGlobal (adjWidPos); // Note: widget item and column lookup use the non-adjusted widget position. QTreeWidgetItem* twItem = tw->itemAt (widgetPos); const QModelIndex modelIndex = tw->indexAt (widgetPos); const int col = modelIndex.column(); // const int twItemInx = tw->indexOfTopLevelItem (twItem); // std::cout << mname // << ": wid [" << widgetPos.x() << "," << widgetPos.y() << "]" // << ", adj [" << adjWidPos.x() << "," << adjWidPos.y() << "]" // << ", gbl [" << globalPos.x() << "," << globalPos.y() << "]" // << ", inx " << twItemInx // << ", col " << col // << std::endl; // ******************************************************* // *** QTreeWidgetItem-Based Context Menu Processing *** // ******************************************************* QAccountTreeWidgetItem (*acctItem) (dynamic_cast(twItem)); QMenu *contextMenu = new QMenu (this); switch (col) { ... ... ... if (no context menu) { delete contextMenu; contextMenu = NULL; } ... ... ... } if (contextMenu) { contextMenu->popup (globalPos); delete contextMenu; } } //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- void ClientDlg::entityTreeView_selectionChanged ( const QItemSelection& /*selected*/, const QItemSelection& /*deselected*/) { static const char* mname ("ClientDlg::entityTreeView_selectionChanged"); static int callCnt (0); ++callCnt; // *** Test: Query the Selection Model *** const QItemSelectionModel* selModel = _entityTreeView->selectionModel(); const int selected = selModel->selectedRows().count(); const int total = _entityItemModel->itemCount(); selected; total; // std::cout << mname << " [#" << callCnt << "] " // << selected << " of " << total << " selected." // << std::endl; updateEnabledness(); // XXX -- Example updateConfigEnabledness(); // XXX -- Example } //--- (end ClientDlg.cpp) ---