InterfaceFrame: don't use pointers for models

Change-Id: I6d41ba89006abb8f8e703f6d703370169062f7e9
Reviewed-on: https://code.wireshark.org/review/25285
Petri-Dish: Michael Mann <mmann78@netscape.net>
Tested-by: Petri Dish Buildbot
Reviewed-by: Roland Knall <rknall@gmail.com>
Reviewed-by: Michael Mann <mmann78@netscape.net>
This commit is contained in:
Michael Mann 2018-01-12 12:50:52 -05:00
parent 9cc00c5623
commit 6b4d1cf931
4 changed files with 86 additions and 56 deletions

View File

@ -49,6 +49,9 @@ const int stat_update_interval_ = 1000; // ms
InterfaceFrame::InterfaceFrame(QWidget * parent) InterfaceFrame::InterfaceFrame(QWidget * parent)
: QFrame(parent), : QFrame(parent),
ui(new Ui::InterfaceFrame) ui(new Ui::InterfaceFrame)
, proxyModel(Q_NULLPTR)
, sourceModel(Q_NULLPTR)
, infoModel(this)
#ifdef HAVE_LIBPCAP #ifdef HAVE_LIBPCAP
,stat_timer_(NULL) ,stat_timer_(NULL)
#endif // HAVE_LIBPCAP #endif // HAVE_LIBPCAP
@ -80,23 +83,20 @@ InterfaceFrame::InterfaceFrame(QWidget * parent)
ifTypeDescription.insert(IF_EXTCAP, tr("External Capture")); ifTypeDescription.insert(IF_EXTCAP, tr("External Capture"));
ifTypeDescription.insert(IF_VIRTUAL, tr ("Virtual")); ifTypeDescription.insert(IF_VIRTUAL, tr ("Virtual"));
proxyModel = new InterfaceSortFilterModel(this);
sourceModel = new InterfaceTreeModel(this);
QList<InterfaceTreeColumns> columns; QList<InterfaceTreeColumns> columns;
columns.append(IFTREE_COL_EXTCAP); columns.append(IFTREE_COL_EXTCAP);
columns.append(IFTREE_COL_NAME); columns.append(IFTREE_COL_NAME);
columns.append(IFTREE_COL_STATS); columns.append(IFTREE_COL_STATS);
proxyModel->setColumns(columns); proxyModel.setColumns(columns);
proxyModel->setStoreOnChange(true); proxyModel.setStoreOnChange(true);
proxyModel->setSourceModel(sourceModel); 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(appInitialized()), this, SLOT(interfaceListChanged()));
connect(wsApp, SIGNAL(localInterfaceListChanged()), this, SLOT(interfaceListChanged())); connect(wsApp, SIGNAL(localInterfaceListChanged()), this, SLOT(interfaceListChanged()));
@ -107,15 +107,13 @@ InterfaceFrame::InterfaceFrame(QWidget * parent)
InterfaceFrame::~InterfaceFrame() InterfaceFrame::~InterfaceFrame()
{ {
delete sourceModel;
delete proxyModel;
delete ui; delete ui;
} }
QMenu * InterfaceFrame::getSelectionMenu() QMenu * InterfaceFrame::getSelectionMenu()
{ {
QMenu * contextMenu = new QMenu(this); QMenu * contextMenu = new QMenu(this);
QList<int> typesDisplayed = proxyModel->typesDisplayed(); QList<int> typesDisplayed = proxyModel.typesDisplayed();
QMap<int, QString>::const_iterator it = ifTypeDescription.constBegin(); QMap<int, QString>::const_iterator it = ifTypeDescription.constBegin();
while(it != ifTypeDescription.constEnd()) while(it != ifTypeDescription.constEnd())
@ -127,7 +125,7 @@ QMenu * InterfaceFrame::getSelectionMenu()
QAction *endp_action = new QAction(it.value(), this); QAction *endp_action = new QAction(it.value(), this);
endp_action->setData(qVariantFromValue(ifType)); endp_action->setData(qVariantFromValue(ifType));
endp_action->setCheckable(true); endp_action->setCheckable(true);
endp_action->setChecked(proxyModel->isInterfaceTypeShown(ifType)); endp_action->setChecked(proxyModel.isInterfaceTypeShown(ifType));
connect(endp_action, SIGNAL(triggered()), this, SLOT(triggeredIfTypeButton())); connect(endp_action, SIGNAL(triggered()), this, SLOT(triggeredIfTypeButton()));
contextMenu->addAction(endp_action); contextMenu->addAction(endp_action);
} }
@ -135,11 +133,11 @@ QMenu * InterfaceFrame::getSelectionMenu()
} }
#ifdef HAVE_PCAP_REMOTE #ifdef HAVE_PCAP_REMOTE
if ( proxyModel->remoteInterfacesExist() ) if ( proxyModel.remoteInterfacesExist() )
{ {
QAction * toggleRemoteAction = new QAction(tr("Remote interfaces"), this); QAction * toggleRemoteAction = new QAction(tr("Remote interfaces"), this);
toggleRemoteAction->setCheckable(true); toggleRemoteAction->setCheckable(true);
toggleRemoteAction->setChecked(! proxyModel->remoteDisplay()); toggleRemoteAction->setChecked(! proxyModel.remoteDisplay());
connect(toggleRemoteAction, SIGNAL(triggered()), this, SLOT(toggleRemoteInterfaces())); connect(toggleRemoteAction, SIGNAL(triggered()), this, SLOT(toggleRemoteInterfaces()));
contextMenu->addAction(toggleRemoteAction); contextMenu->addAction(toggleRemoteAction);
} }
@ -160,12 +158,12 @@ QMenu * InterfaceFrame::getSelectionMenu()
int InterfaceFrame::interfacesHidden() int InterfaceFrame::interfacesHidden()
{ {
return proxyModel->interfacesHidden(); return proxyModel.interfacesHidden();
} }
int InterfaceFrame::interfacesPresent() int InterfaceFrame::interfacesPresent()
{ {
return sourceModel->rowCount() - proxyModel->interfacesHidden(); return sourceModel.rowCount() - proxyModel.interfacesHidden();
} }
void InterfaceFrame::ensureSelectedInterface() void InterfaceFrame::ensureSelectedInterface()
@ -173,8 +171,8 @@ void InterfaceFrame::ensureSelectedInterface()
#ifdef HAVE_LIBPCAP #ifdef HAVE_LIBPCAP
if (interfacesPresent() < 1) return; if (interfacesPresent() < 1) return;
if (sourceModel->selectedDevices().count() < 1) { if (sourceModel.selectedDevices().count() < 1) {
QModelIndex first_idx = infoModel->mapFromSource(proxyModel->index(0, 0)); QModelIndex first_idx = infoModel.mapFromSource(proxyModel.index(0, 0));
ui->interfaceTree->setCurrentIndex(first_idx); ui->interfaceTree->setCurrentIndex(first_idx);
} }
@ -186,7 +184,7 @@ void InterfaceFrame::hideEvent(QHideEvent *) {
#ifdef HAVE_LIBPCAP #ifdef HAVE_LIBPCAP
if (stat_timer_) if (stat_timer_)
stat_timer_->stop(); stat_timer_->stop();
sourceModel->stopStatistic(); sourceModel.stopStatistic();
#endif // HAVE_LIBPCAP #endif // HAVE_LIBPCAP
} }
@ -203,7 +201,7 @@ void InterfaceFrame::actionButton_toggled(bool checked)
QVariant ifType = sender()->property(BTN_IFTYPE_PROPERTY); QVariant ifType = sender()->property(BTN_IFTYPE_PROPERTY);
if ( ifType.isValid() ) if ( ifType.isValid() )
{ {
proxyModel->setInterfaceTypeVisible(ifType.toInt(), checked); proxyModel.setInterfaceTypeVisible(ifType.toInt(), checked);
} }
resetInterfaceTreeDisplay(); resetInterfaceTreeDisplay();
@ -215,7 +213,7 @@ void InterfaceFrame::triggeredIfTypeButton()
if ( sender ) if ( sender )
{ {
int ifType = sender->data().value<int>(); int ifType = sender->data().value<int>();
proxyModel->toggleTypeVisibility(ifType); proxyModel.toggleTypeVisibility(ifType);
resetInterfaceTreeDisplay(); resetInterfaceTreeDisplay();
emit typeSelectionChanged(); emit typeSelectionChanged();
@ -224,9 +222,9 @@ void InterfaceFrame::triggeredIfTypeButton()
void InterfaceFrame::interfaceListChanged() void InterfaceFrame::interfaceListChanged()
{ {
infoModel->clearInfos(); infoModel.clearInfos();
if ( prefs.capture_no_extcap ) if ( prefs.capture_no_extcap )
infoModel->appendInfo(tr("External capture interfaces disabled.")); infoModel.appendInfo(tr("External capture interfaces disabled."));
resetInterfaceTreeDisplay(); resetInterfaceTreeDisplay();
// Ensure that device selection is consistent with the displayed selection. // Ensure that device selection is consistent with the displayed selection.
@ -244,7 +242,7 @@ void InterfaceFrame::interfaceListChanged()
void InterfaceFrame::toggleHiddenInterfaces() void InterfaceFrame::toggleHiddenInterfaces()
{ {
proxyModel->toggleFilterHidden(); proxyModel.toggleFilterHidden();
emit typeSelectionChanged(); emit typeSelectionChanged();
} }
@ -252,37 +250,37 @@ void InterfaceFrame::toggleHiddenInterfaces()
#ifdef HAVE_PCAP_REMOTE #ifdef HAVE_PCAP_REMOTE
void InterfaceFrame::toggleRemoteInterfaces() void InterfaceFrame::toggleRemoteInterfaces()
{ {
proxyModel->toggleRemoteDisplay(); proxyModel.toggleRemoteDisplay();
emit typeSelectionChanged(); emit typeSelectionChanged();
} }
#endif #endif
void InterfaceFrame::resetInterfaceTreeDisplay() void InterfaceFrame::resetInterfaceTreeDisplay()
{ {
if ( proxyModel->rowCount() == 0 ) if ( proxyModel.rowCount() == 0 )
{ {
ui->interfaceTree->setHidden(true); ui->interfaceTree->setHidden(true);
ui->lblNoInterfaces->setHidden(false); ui->lblNoInterfaces->setHidden(false);
ui->lblNoInterfaces->setText( proxyModel->interfaceError() ); ui->lblNoInterfaces->setText( proxyModel.interfaceError() );
} }
else else
{ {
ui->interfaceTree->setHidden(false); ui->interfaceTree->setHidden(false);
ui->lblNoInterfaces->setHidden(true); ui->lblNoInterfaces->setHidden(true);
ui->interfaceTree->resizeColumnToContents(proxyModel->mapSourceToColumn(IFTREE_COL_EXTCAP)); ui->interfaceTree->resizeColumnToContents(proxyModel.mapSourceToColumn(IFTREE_COL_EXTCAP));
ui->interfaceTree->resizeColumnToContents(proxyModel->mapSourceToColumn(IFTREE_COL_NAME)); ui->interfaceTree->resizeColumnToContents(proxyModel.mapSourceToColumn(IFTREE_COL_NAME));
ui->interfaceTree->resizeColumnToContents(proxyModel->mapSourceToColumn(IFTREE_COL_STATS)); ui->interfaceTree->resizeColumnToContents(proxyModel.mapSourceToColumn(IFTREE_COL_STATS));
} }
} }
void InterfaceFrame::updateSelectedInterfaces() void InterfaceFrame::updateSelectedInterfaces()
{ {
if ( sourceModel->rowCount() == 0 ) if ( sourceModel.rowCount() == 0 )
return; return;
#ifdef HAVE_LIBPCAP #ifdef HAVE_LIBPCAP
QItemSelection sourceSelection = sourceModel->selectedDevices(); QItemSelection sourceSelection = sourceModel.selectedDevices();
QItemSelection mySelection = infoModel->mapSelectionFromSource(proxyModel->mapSelectionFromSource(sourceSelection)); QItemSelection mySelection = infoModel.mapSelectionFromSource(proxyModel.mapSelectionFromSource(sourceSelection));
ui->interfaceTree->selectionModel()->clearSelection(); ui->interfaceTree->selectionModel()->clearSelection();
ui->interfaceTree->selectionModel()->select(mySelection, QItemSelectionModel::SelectCurrent ); ui->interfaceTree->selectionModel()->select(mySelection, QItemSelectionModel::SelectCurrent );
@ -293,30 +291,30 @@ void InterfaceFrame::interfaceTreeSelectionChanged(const QItemSelection & select
{ {
if (selected.count() == 0 && deselected.count() == 0) if (selected.count() == 0 && deselected.count() == 0)
return; return;
if ( sourceModel->rowCount() == 0 ) if ( sourceModel.rowCount() == 0 )
return; return;
#ifdef HAVE_LIBPCAP #ifdef HAVE_LIBPCAP
/* Take all selected interfaces, not just the newly ones */ /* Take all selected interfaces, not just the newly ones */
QItemSelection allSelected = ui->interfaceTree->selectionModel()->selection(); 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(); emit itemSelectionChanged();
#endif #endif
} }
void InterfaceFrame::on_interfaceTree_doubleClicked(const QModelIndex &index) 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() ) if ( ! realIndex.isValid() )
return; return;
#ifdef HAVE_LIBPCAP #ifdef HAVE_LIBPCAP
QString device_name = sourceModel->getColumnContent(realIndex.row(), IFTREE_COL_INTERFACE_NAME).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(); 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 /* We trust the string here. If this interface is really extcap, the string is
* being checked immediatly before the dialog is being generated */ * 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 ) if ( index.column() == 0 )
{ {
QModelIndex realIndex = proxyModel->mapToSource(infoModel->mapToSource(index)); QModelIndex realIndex = proxyModel.mapToSource(infoModel.mapToSource(index));
if ( ! realIndex.isValid() ) if ( ! realIndex.isValid() )
return; return;
QString device_name = sourceModel->getColumnContent(realIndex.row(), IFTREE_COL_INTERFACE_NAME).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(); 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 /* We trust the string here. If this interface is really extcap, the string is
* being checked immediatly before the dialog is being generated */ * 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) void InterfaceFrame::updateStatistics(void)
{ {
if ( sourceModel->rowCount() == 0 ) if ( sourceModel.rowCount() == 0 )
return; return;
#ifdef HAVE_LIBPCAP #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 */ /* Proxy model has not masked out the interface */
if ( selectIndex.isValid() ) if ( selectIndex.isValid() )
sourceModel->updateStatistic(idx); sourceModel.updateStatistic(idx);
} }
#endif #endif
@ -383,7 +381,7 @@ void InterfaceFrame::updateStatistics(void)
/* Proxy Method so we do not need to expose the source model */ /* Proxy Method so we do not need to expose the source model */
void InterfaceFrame::getPoints(int idx, PointList * pts) void InterfaceFrame::getPoints(int idx, PointList * pts)
{ {
sourceModel->getPoints(idx, pts); sourceModel.getPoints(idx, pts);
} }
/* /*

View File

@ -81,9 +81,9 @@ private:
Ui::InterfaceFrame *ui; Ui::InterfaceFrame *ui;
InterfaceSortFilterModel * proxyModel; InterfaceSortFilterModel proxyModel;
InterfaceTreeModel * sourceModel; InterfaceTreeModel sourceModel;
InfoProxyModel * infoModel; InfoProxyModel infoModel;
QMap<int, QString> ifTypeDescription; QMap<int, QString> ifTypeDescription;

View File

@ -14,9 +14,10 @@
#include <QFont> #include <QFont>
InfoProxyModel::InfoProxyModel(int column, QObject * parent) : QIdentityProxyModel (parent) InfoProxyModel::InfoProxyModel(QObject * parent)
: QIdentityProxyModel(parent),
column_(-1)
{ {
column_ = column;
} }
InfoProxyModel::~InfoProxyModel() InfoProxyModel::~InfoProxyModel()
@ -98,6 +99,35 @@ QModelIndex InfoProxyModel::mapFromSource(const QModelIndex &fromIndex) const
return QIdentityProxyModel::mapFromSource(fromIndex); 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<int> 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 * Editor modelines
* *

View File

@ -21,7 +21,7 @@ class InfoProxyModel : public QIdentityProxyModel
Q_OBJECT Q_OBJECT
public: public:
explicit InfoProxyModel(int column, QObject * parent); explicit InfoProxyModel(QObject * parent = 0);
~InfoProxyModel(); ~InfoProxyModel();
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
@ -36,6 +36,8 @@ public:
void appendInfo(QString info); void appendInfo(QString info);
void clearInfos(); void clearInfos();
void setColumn(int column);
private: private:
int column_; int column_;