From 6b4d1cf931c31edcd6dc27a4c20fb90a76bf122f Mon Sep 17 00:00:00 2001 From: Michael Mann Date: Fri, 12 Jan 2018 12:50:52 -0500 Subject: [PATCH] InterfaceFrame: don't use pointers for models Change-Id: I6d41ba89006abb8f8e703f6d703370169062f7e9 Reviewed-on: https://code.wireshark.org/review/25285 Petri-Dish: Michael Mann Tested-by: Petri Dish Buildbot Reviewed-by: Roland Knall Reviewed-by: Michael Mann --- ui/qt/interface_frame.cpp | 98 +++++++++++++++---------------- ui/qt/interface_frame.h | 6 +- ui/qt/models/info_proxy_model.cpp | 34 ++++++++++- ui/qt/models/info_proxy_model.h | 4 +- 4 files changed, 86 insertions(+), 56 deletions(-) diff --git a/ui/qt/interface_frame.cpp b/ui/qt/interface_frame.cpp index 06ea7508b3..92a10f7efa 100644 --- a/ui/qt/interface_frame.cpp +++ b/ui/qt/interface_frame.cpp @@ -49,6 +49,9 @@ const int stat_update_interval_ = 1000; // ms InterfaceFrame::InterfaceFrame(QWidget * parent) : QFrame(parent), ui(new Ui::InterfaceFrame) + , proxyModel(Q_NULLPTR) + , sourceModel(Q_NULLPTR) + , infoModel(this) #ifdef HAVE_LIBPCAP ,stat_timer_(NULL) #endif // HAVE_LIBPCAP @@ -80,23 +83,20 @@ InterfaceFrame::InterfaceFrame(QWidget * parent) ifTypeDescription.insert(IF_EXTCAP, tr("External Capture")); ifTypeDescription.insert(IF_VIRTUAL, tr ("Virtual")); - proxyModel = new InterfaceSortFilterModel(this); - sourceModel = new InterfaceTreeModel(this); - QList columns; columns.append(IFTREE_COL_EXTCAP); columns.append(IFTREE_COL_NAME); columns.append(IFTREE_COL_STATS); - proxyModel->setColumns(columns); - proxyModel->setStoreOnChange(true); - proxyModel->setSourceModel(sourceModel); + proxyModel.setColumns(columns); + proxyModel.setStoreOnChange(true); + proxyModel.setSourceModel(&sourceModel); - infoModel = new InfoProxyModel(2, this); - infoModel->setSourceModel(proxyModel); + infoModel.setSourceModel(&proxyModel); + infoModel.setColumn(columns.indexOf(IFTREE_COL_STATS)); + + ui->interfaceTree->setModel(&infoModel); - ui->interfaceTree->setModel(infoModel); - - ui->interfaceTree->setItemDelegateForColumn(proxyModel->mapSourceToColumn(IFTREE_COL_STATS), new SparkLineDelegate(this)); + ui->interfaceTree->setItemDelegateForColumn(proxyModel.mapSourceToColumn(IFTREE_COL_STATS), new SparkLineDelegate(this)); connect(wsApp, SIGNAL(appInitialized()), this, SLOT(interfaceListChanged())); connect(wsApp, SIGNAL(localInterfaceListChanged()), this, SLOT(interfaceListChanged())); @@ -107,15 +107,13 @@ InterfaceFrame::InterfaceFrame(QWidget * parent) InterfaceFrame::~InterfaceFrame() { - delete sourceModel; - delete proxyModel; delete ui; } QMenu * InterfaceFrame::getSelectionMenu() { QMenu * contextMenu = new QMenu(this); - QList typesDisplayed = proxyModel->typesDisplayed(); + QList typesDisplayed = proxyModel.typesDisplayed(); QMap::const_iterator it = ifTypeDescription.constBegin(); while(it != ifTypeDescription.constEnd()) @@ -127,7 +125,7 @@ QMenu * InterfaceFrame::getSelectionMenu() QAction *endp_action = new QAction(it.value(), this); endp_action->setData(qVariantFromValue(ifType)); endp_action->setCheckable(true); - endp_action->setChecked(proxyModel->isInterfaceTypeShown(ifType)); + endp_action->setChecked(proxyModel.isInterfaceTypeShown(ifType)); connect(endp_action, SIGNAL(triggered()), this, SLOT(triggeredIfTypeButton())); contextMenu->addAction(endp_action); } @@ -135,11 +133,11 @@ QMenu * InterfaceFrame::getSelectionMenu() } #ifdef HAVE_PCAP_REMOTE - if ( proxyModel->remoteInterfacesExist() ) + if ( proxyModel.remoteInterfacesExist() ) { QAction * toggleRemoteAction = new QAction(tr("Remote interfaces"), this); toggleRemoteAction->setCheckable(true); - toggleRemoteAction->setChecked(! proxyModel->remoteDisplay()); + toggleRemoteAction->setChecked(! proxyModel.remoteDisplay()); connect(toggleRemoteAction, SIGNAL(triggered()), this, SLOT(toggleRemoteInterfaces())); contextMenu->addAction(toggleRemoteAction); } @@ -160,12 +158,12 @@ QMenu * InterfaceFrame::getSelectionMenu() int InterfaceFrame::interfacesHidden() { - return proxyModel->interfacesHidden(); + return proxyModel.interfacesHidden(); } int InterfaceFrame::interfacesPresent() { - return sourceModel->rowCount() - proxyModel->interfacesHidden(); + return sourceModel.rowCount() - proxyModel.interfacesHidden(); } void InterfaceFrame::ensureSelectedInterface() @@ -173,8 +171,8 @@ void InterfaceFrame::ensureSelectedInterface() #ifdef HAVE_LIBPCAP if (interfacesPresent() < 1) return; - if (sourceModel->selectedDevices().count() < 1) { - QModelIndex first_idx = infoModel->mapFromSource(proxyModel->index(0, 0)); + if (sourceModel.selectedDevices().count() < 1) { + QModelIndex first_idx = infoModel.mapFromSource(proxyModel.index(0, 0)); ui->interfaceTree->setCurrentIndex(first_idx); } @@ -186,7 +184,7 @@ void InterfaceFrame::hideEvent(QHideEvent *) { #ifdef HAVE_LIBPCAP if (stat_timer_) stat_timer_->stop(); - sourceModel->stopStatistic(); + sourceModel.stopStatistic(); #endif // HAVE_LIBPCAP } @@ -203,7 +201,7 @@ void InterfaceFrame::actionButton_toggled(bool checked) QVariant ifType = sender()->property(BTN_IFTYPE_PROPERTY); if ( ifType.isValid() ) { - proxyModel->setInterfaceTypeVisible(ifType.toInt(), checked); + proxyModel.setInterfaceTypeVisible(ifType.toInt(), checked); } resetInterfaceTreeDisplay(); @@ -215,7 +213,7 @@ void InterfaceFrame::triggeredIfTypeButton() if ( sender ) { int ifType = sender->data().value(); - proxyModel->toggleTypeVisibility(ifType); + proxyModel.toggleTypeVisibility(ifType); resetInterfaceTreeDisplay(); emit typeSelectionChanged(); @@ -224,9 +222,9 @@ void InterfaceFrame::triggeredIfTypeButton() void InterfaceFrame::interfaceListChanged() { - infoModel->clearInfos(); + infoModel.clearInfos(); if ( prefs.capture_no_extcap ) - infoModel->appendInfo(tr("External capture interfaces disabled.")); + infoModel.appendInfo(tr("External capture interfaces disabled.")); resetInterfaceTreeDisplay(); // Ensure that device selection is consistent with the displayed selection. @@ -244,7 +242,7 @@ void InterfaceFrame::interfaceListChanged() void InterfaceFrame::toggleHiddenInterfaces() { - proxyModel->toggleFilterHidden(); + proxyModel.toggleFilterHidden(); emit typeSelectionChanged(); } @@ -252,37 +250,37 @@ void InterfaceFrame::toggleHiddenInterfaces() #ifdef HAVE_PCAP_REMOTE void InterfaceFrame::toggleRemoteInterfaces() { - proxyModel->toggleRemoteDisplay(); + proxyModel.toggleRemoteDisplay(); emit typeSelectionChanged(); } #endif void InterfaceFrame::resetInterfaceTreeDisplay() { - if ( proxyModel->rowCount() == 0 ) + if ( proxyModel.rowCount() == 0 ) { ui->interfaceTree->setHidden(true); ui->lblNoInterfaces->setHidden(false); - ui->lblNoInterfaces->setText( proxyModel->interfaceError() ); + ui->lblNoInterfaces->setText( proxyModel.interfaceError() ); } else { ui->interfaceTree->setHidden(false); ui->lblNoInterfaces->setHidden(true); - ui->interfaceTree->resizeColumnToContents(proxyModel->mapSourceToColumn(IFTREE_COL_EXTCAP)); - ui->interfaceTree->resizeColumnToContents(proxyModel->mapSourceToColumn(IFTREE_COL_NAME)); - ui->interfaceTree->resizeColumnToContents(proxyModel->mapSourceToColumn(IFTREE_COL_STATS)); + ui->interfaceTree->resizeColumnToContents(proxyModel.mapSourceToColumn(IFTREE_COL_EXTCAP)); + ui->interfaceTree->resizeColumnToContents(proxyModel.mapSourceToColumn(IFTREE_COL_NAME)); + ui->interfaceTree->resizeColumnToContents(proxyModel.mapSourceToColumn(IFTREE_COL_STATS)); } } void InterfaceFrame::updateSelectedInterfaces() { - if ( sourceModel->rowCount() == 0 ) + if ( sourceModel.rowCount() == 0 ) return; #ifdef HAVE_LIBPCAP - QItemSelection sourceSelection = sourceModel->selectedDevices(); - QItemSelection mySelection = infoModel->mapSelectionFromSource(proxyModel->mapSelectionFromSource(sourceSelection)); + QItemSelection sourceSelection = sourceModel.selectedDevices(); + QItemSelection mySelection = infoModel.mapSelectionFromSource(proxyModel.mapSelectionFromSource(sourceSelection)); ui->interfaceTree->selectionModel()->clearSelection(); ui->interfaceTree->selectionModel()->select(mySelection, QItemSelectionModel::SelectCurrent ); @@ -293,30 +291,30 @@ void InterfaceFrame::interfaceTreeSelectionChanged(const QItemSelection & select { if (selected.count() == 0 && deselected.count() == 0) return; - if ( sourceModel->rowCount() == 0 ) + if ( sourceModel.rowCount() == 0 ) return; #ifdef HAVE_LIBPCAP /* Take all selected interfaces, not just the newly ones */ QItemSelection allSelected = ui->interfaceTree->selectionModel()->selection(); - QItemSelection sourceSelection = proxyModel->mapSelectionToSource(infoModel->mapSelectionToSource(allSelected)); + QItemSelection sourceSelection = proxyModel.mapSelectionToSource(infoModel.mapSelectionToSource(allSelected)); - if ( sourceModel->updateSelectedDevices(sourceSelection) ) + if ( sourceModel.updateSelectedDevices(sourceSelection) ) emit itemSelectionChanged(); #endif } void InterfaceFrame::on_interfaceTree_doubleClicked(const QModelIndex &index) { - QModelIndex realIndex = proxyModel->mapToSource(infoModel->mapToSource(index)); + QModelIndex realIndex = proxyModel.mapToSource(infoModel.mapToSource(index)); if ( ! realIndex.isValid() ) return; #ifdef HAVE_LIBPCAP - QString device_name = sourceModel->getColumnContent(realIndex.row(), IFTREE_COL_INTERFACE_NAME).toString(); - QString extcap_string = sourceModel->getColumnContent(realIndex.row(), IFTREE_COL_EXTCAP_PATH).toString(); + QString device_name = sourceModel.getColumnContent(realIndex.row(), IFTREE_COL_INTERFACE_NAME).toString(); + QString extcap_string = sourceModel.getColumnContent(realIndex.row(), IFTREE_COL_EXTCAP_PATH).toString(); /* We trust the string here. If this interface is really extcap, the string is * being checked immediatly before the dialog is being generated */ @@ -338,13 +336,13 @@ void InterfaceFrame::on_interfaceTree_clicked(const QModelIndex &index) { if ( index.column() == 0 ) { - QModelIndex realIndex = proxyModel->mapToSource(infoModel->mapToSource(index)); + QModelIndex realIndex = proxyModel.mapToSource(infoModel.mapToSource(index)); if ( ! realIndex.isValid() ) return; - QString device_name = sourceModel->getColumnContent(realIndex.row(), IFTREE_COL_INTERFACE_NAME).toString(); - QString extcap_string = sourceModel->getColumnContent(realIndex.row(), IFTREE_COL_EXTCAP_PATH).toString(); + QString device_name = sourceModel.getColumnContent(realIndex.row(), IFTREE_COL_INTERFACE_NAME).toString(); + QString extcap_string = sourceModel.getColumnContent(realIndex.row(), IFTREE_COL_EXTCAP_PATH).toString(); /* We trust the string here. If this interface is really extcap, the string is * being checked immediatly before the dialog is being generated */ @@ -363,18 +361,18 @@ void InterfaceFrame::on_interfaceTree_clicked(const QModelIndex &index) void InterfaceFrame::updateStatistics(void) { - if ( sourceModel->rowCount() == 0 ) + if ( sourceModel.rowCount() == 0 ) return; #ifdef HAVE_LIBPCAP - for( int idx = 0; idx < proxyModel->rowCount(); idx++ ) + for( int idx = 0; idx < proxyModel.rowCount(); idx++ ) { - QModelIndex selectIndex = infoModel->mapFromSource(proxyModel->mapFromSource(sourceModel->index(idx, 0))); + QModelIndex selectIndex = infoModel.mapFromSource(proxyModel.mapFromSource(sourceModel.index(idx, 0))); /* Proxy model has not masked out the interface */ if ( selectIndex.isValid() ) - sourceModel->updateStatistic(idx); + sourceModel.updateStatistic(idx); } #endif @@ -383,7 +381,7 @@ void InterfaceFrame::updateStatistics(void) /* Proxy Method so we do not need to expose the source model */ void InterfaceFrame::getPoints(int idx, PointList * pts) { - sourceModel->getPoints(idx, pts); + sourceModel.getPoints(idx, pts); } /* diff --git a/ui/qt/interface_frame.h b/ui/qt/interface_frame.h index c5a3a6067c..4d3ed6a9cc 100644 --- a/ui/qt/interface_frame.h +++ b/ui/qt/interface_frame.h @@ -81,9 +81,9 @@ private: Ui::InterfaceFrame *ui; - InterfaceSortFilterModel * proxyModel; - InterfaceTreeModel * sourceModel; - InfoProxyModel * infoModel; + InterfaceSortFilterModel proxyModel; + InterfaceTreeModel sourceModel; + InfoProxyModel infoModel; QMap ifTypeDescription; diff --git a/ui/qt/models/info_proxy_model.cpp b/ui/qt/models/info_proxy_model.cpp index 4d61434687..197cc9e0d8 100644 --- a/ui/qt/models/info_proxy_model.cpp +++ b/ui/qt/models/info_proxy_model.cpp @@ -14,9 +14,10 @@ #include -InfoProxyModel::InfoProxyModel(int column, QObject * parent) : QIdentityProxyModel (parent) +InfoProxyModel::InfoProxyModel(QObject * parent) + : QIdentityProxyModel(parent), + column_(-1) { - column_ = column; } InfoProxyModel::~InfoProxyModel() @@ -98,6 +99,35 @@ QModelIndex InfoProxyModel::mapFromSource(const QModelIndex &fromIndex) const return QIdentityProxyModel::mapFromSource(fromIndex); } +void InfoProxyModel::setColumn(int column) +{ + int old_column = column_; + column_ = column; + +#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) + QVector roles; + roles << Qt::DisplayRole; +#endif + + if (old_column >= 0) { + //Notify old column has changed + emit dataChanged(index(0, old_column), index(rowCount(), old_column) +#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) + , roles +#endif + ); + } + + if (column_ >= 0) { + //Notify new column has changed + emit dataChanged(index(0, column_), index(rowCount(), column_) +#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) + , roles +#endif + ); + } +} + /* * Editor modelines * diff --git a/ui/qt/models/info_proxy_model.h b/ui/qt/models/info_proxy_model.h index 72b36f85ee..9574882f5a 100644 --- a/ui/qt/models/info_proxy_model.h +++ b/ui/qt/models/info_proxy_model.h @@ -21,7 +21,7 @@ class InfoProxyModel : public QIdentityProxyModel Q_OBJECT public: - explicit InfoProxyModel(int column, QObject * parent); + explicit InfoProxyModel(QObject * parent = 0); ~InfoProxyModel(); virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; @@ -36,6 +36,8 @@ public: void appendInfo(QString info); void clearInfos(); + void setColumn(int column); + private: int column_;