diff options
Diffstat (limited to 'ui/qt/filter_dialog.cpp')
-rw-r--r-- | ui/qt/filter_dialog.cpp | 169 |
1 files changed, 78 insertions, 91 deletions
diff --git a/ui/qt/filter_dialog.cpp b/ui/qt/filter_dialog.cpp index 98985baa92..cc7e87a3f7 100644 --- a/ui/qt/filter_dialog.cpp +++ b/ui/qt/filter_dialog.cpp @@ -23,10 +23,10 @@ #include <QMessageBox> #include <QThread> #include <QUrl> +#include <QSortFilterProxyModel> #include <ui/qt/utils/qt_ui_utils.h> #include <ui/qt/widgets/capture_filter_edit.h> -//#include "capture_filter_syntax_worker.h" #include <ui/qt/widgets/display_filter_edit.h> #include "wireshark_application.h" @@ -41,15 +41,14 @@ enum { filter_col_ }; -FilterDialog::FilterDialog(QWidget *parent, FilterType filter_type, const QString new_filter) : +FilterDialog::FilterDialog(QWidget *parent, FilterType filter_type, QString new_filter_) : GeometryStateDialog(parent), ui(new Ui::FilterDialog), filter_type_(filter_type), -// syntax_worker_(NULL), - filter_tree_delegate_(new FilterTreeDelegate(this, filter_type)), - new_filter_(new_filter) + filter_tree_delegate_(new FilterTreeDelegate(this, filter_type)) { ui->setupUi(this); + if (parent) loadGeometry(parent->width() * 2 / 3, parent->height() * 2 / 3); setWindowIcon(wsApp->normalIcon()); @@ -64,30 +63,40 @@ FilterDialog::FilterDialog(QWidget *parent, FilterType filter_type, const QStrin ui->pathLabel->setAttribute(Qt::WA_MacSmallSize, true); #endif +#if 0 ui->filterTreeWidget->setDragEnabled(true); ui->filterTreeWidget->viewport()->setAcceptDrops(true); ui->filterTreeWidget->setDropIndicatorShown(true); ui->filterTreeWidget->setDragDropMode(QAbstractItemView::InternalMove); +#endif const gchar * filename = NULL; + QString newFilterText; if (filter_type == CaptureFilter) { setWindowTitle(wsApp->windowTitleString(tr("Capture Filters"))); filename = CFILTER_FILE_NAME; - -// QThread *syntax_thread = new QThread; -// syntax_worker_ = new CaptureFilterSyntaxWorker; -// syntax_worker_->moveToThread(syntax_thread); -// connect(syntax_thread, SIGNAL(started()), syntax_worker_, SLOT(start())); -// // connect(syntax_thread, SIGNAL(started()), this, SLOT(checkFilter())); -// connect(syntax_worker_, SIGNAL(syntaxResult(QString, bool, QString)), -// this, SLOT(setFilterSyntaxState(QString, bool, QString))); -// connect(syntax_thread, SIGNAL(finished()), syntax_worker_, SLOT(deleteLater())); -// syntax_thread->start(); + newFilterText = tr("New capture filter"); + model_ = new FilterListModel(FilterListModel::Capture, this); } else { setWindowTitle(wsApp->windowTitleString(tr("Display Filters"))); filename = DFILTER_FILE_NAME; + newFilterText = tr("New display filter"); + model_ = new FilterListModel(FilterListModel::Display, this); } + if ( new_filter_.length() > 0 ) + model_->addFilter(newFilterText, new_filter_); + + sortModel_ = new QSortFilterProxyModel(this); + sortModel_->setSourceModel(model_); + ui->filterTreeView->setModel(sortModel_); + + ui->filterTreeView->setItemDelegate(new FilterTreeDelegate(this, filter_type)); + + ui->filterTreeView->resizeColumnToContents(FilterListModel::ColumnName); + + connect( ui->filterTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &FilterDialog::selectionChanged ); + QString abs_path = gchar_free_to_qstring(get_persconffile_path(filename, TRUE)); if (file_exists(abs_path.toUtf8().constData())) { ui->pathLabel->setText(abs_path); @@ -95,8 +104,6 @@ FilterDialog::FilterDialog(QWidget *parent, FilterType filter_type, const QStrin ui->pathLabel->setToolTip(tr("Open ") + filename); ui->pathLabel->setEnabled(true); } - - ui->filterTreeWidget->setItemDelegateForColumn(filter_col_, filter_tree_delegate_); } FilterDialog::~FilterDialog() @@ -104,64 +111,28 @@ FilterDialog::~FilterDialog() delete ui; } -void FilterDialog::showEvent(QShowEvent *event) -{ - ui->filterTreeWidget->clear(); - - GList *filter_list; - if (filter_type_ == CaptureFilter) { - filter_list = get_filter_list_first(CFILTER_LIST); - } else { - filter_list = get_filter_list_first(DFILTER_LIST); - } - for (GList *fl_item = filter_list; fl_item; fl_item = gxx_list_next(fl_item)) { - if (!fl_item->data) continue; - filter_def *fl_data = gxx_list_data(filter_def *, fl_item); - if (!fl_data->name || !fl_data->strval) continue; - - addFilter(fl_data->name, fl_data->strval); - } - - if (!new_filter_.isEmpty()) { - addFilter(tr("New filter"), new_filter_, true); - new_filter_.clear(); - } - - ui->filterTreeWidget->resizeColumnToContents(name_col_); - ui->filterTreeWidget->resizeColumnToContents(filter_col_); - - QDialog::showEvent(event); -} - void FilterDialog::addFilter(QString name, QString filter, bool start_editing) { - QTreeWidgetItem *ti = new QTreeWidgetItem(ui->filterTreeWidget); - ti->setFlags(ti->flags() | Qt::ItemIsEditable); - ti->setFlags(ti->flags() & ~(Qt::ItemIsDropEnabled)); - ti->setText(name_col_, name); - ti->setText(filter_col_, filter); - - if (start_editing) { - ui->filterTreeWidget->setCurrentItem(ti); - updateWidgets(); - ui->filterTreeWidget->editItem(ti, filter_col_); + if ( model_ ) + { + QModelIndex idx = model_->addFilter(name, filter); + if ( start_editing ) + ui->filterTreeView->edit(sortModel_->mapFromSource(idx)); } } void FilterDialog::updateWidgets() { - int num_selected = ui->filterTreeWidget->selectedItems().count(); + if ( ! ui->filterTreeView->selectionModel() ) + return; + + int num_selected = ui->filterTreeView->selectionModel()->selectedRows().count(); ui->copyToolButton->setEnabled(num_selected == 1); ui->deleteToolButton->setEnabled(num_selected > 0); } -//void FilterDialog::setFilterSyntaxState(QString filter, bool valid, QString err_msg) -//{ - -//} - -void FilterDialog::on_filterTreeWidget_itemSelectionChanged() +void FilterDialog::selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/) { updateWidgets(); } @@ -186,36 +157,34 @@ void FilterDialog::on_newToolButton_clicked() void FilterDialog::on_deleteToolButton_clicked() { - QList<QTreeWidgetItem*> selected = ui->filterTreeWidget->selectedItems(); - foreach (QTreeWidgetItem *ti, selected) { - delete ti; + QModelIndexList selected = ui->filterTreeView->selectionModel()->selectedRows(); + QList<int> rows; + foreach ( QModelIndex idx, selected ) + { + QModelIndex original = sortModel_->mapToSource(idx); + if ( original.isValid() && ! rows.contains(original.row()) ) + { + rows << original.row(); + model_->removeFilter(original); + } } } void FilterDialog::on_copyToolButton_clicked() { - if (!ui->filterTreeWidget->currentItem()) return; - QTreeWidgetItem *ti = ui->filterTreeWidget->currentItem(); + QModelIndexList selected = ui->filterTreeView->selectionModel()->selectedRows(); + if ( selected.count() <= 0 ) + return; + + int rowNr = selected.at(0).row(); + QModelIndex row = selected.at(0).sibling(rowNr, FilterListModel::ColumnName); - addFilter(ti->text(name_col_), ti->text(filter_col_), true); + addFilter(row.data().toString(), row.sibling(rowNr, FilterListModel::ColumnExpression).data().toString(), true); } void FilterDialog::on_buttonBox_accepted() { - filter_list_type_t fl_type = filter_type_ == CaptureFilter ? CFILTER_LIST : DFILTER_LIST; - - while (GList *fl_item = get_filter_list_first(fl_type)) { - remove_from_filter_list(fl_type, fl_item); - } - - QTreeWidgetItemIterator it(ui->filterTreeWidget); - while (*it) { - add_to_filter_list(fl_type, (*it)->text(name_col_).toUtf8().constData(), - (*it)->text(filter_col_).toUtf8().constData()); - ++it; - } - - save_filter_list(fl_type); + model_->saveList(); if (filter_type_ == CaptureFilter) { wsApp->emitAppSignal(WiresharkApplication::CaptureFilterListChanged); @@ -238,23 +207,41 @@ void FilterDialog::on_buttonBox_helpRequested() // Delegate for editing capture and display filters. // +FilterTreeDelegate::FilterTreeDelegate(QObject *parent, FilterDialog::FilterType filter_type) : + QStyledItemDelegate(parent), + filter_type_(filter_type) +{} + + QWidget *FilterTreeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { + QWidget * w = Q_NULLPTR; if (index.column() != filter_col_) { - return QStyledItemDelegate::createEditor(parent, option, index); + w = QStyledItemDelegate::createEditor(parent, option, index); } - - QWidget *w; - - if (filter_type_ == FilterDialog::CaptureFilter) { - w = new CaptureFilterEdit(parent, true); - } else { - w = new DisplayFilterEdit(parent, DisplayFilterToEnter); + else + { + if (filter_type_ == FilterDialog::CaptureFilter) { + w = new CaptureFilterEdit(parent, true); + } else { + w = new DisplayFilterEdit(parent, DisplayFilterToEnter); + } } return w; } +void FilterTreeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const +{ + if ( ! editor || ! index.isValid() ) + return; + + QStyledItemDelegate::setEditorData(editor, index); + + if ( qobject_cast<QLineEdit *>(editor) ) + qobject_cast<QLineEdit *>(editor)->setText(index.data().toString()); +} + /* * Editor modelines * |