From 2c7a35bc987f0996180ba49e646a5e2ef613a003 Mon Sep 17 00:00:00 2001 From: Roland Knall Date: Fri, 12 Jul 2019 21:35:05 +0200 Subject: [PATCH] Qt: Fix various issues for profile manager - Fixing the fact, that the profile did not allways switch to the active profile when pressing the OK button. - Adding a new profile can be repeated, if the name "New Profile" is already in use, a number is incremented and added to subsequently new profiles. - Copying a profile multiple times, creates individual names each time - Global profiles should come last in the statusbar popup - Global profiles should be called "System profiles" - Fix null-pointer that could happen while querying "DATA_IS_SELECTED" - Renaming an existing profile was not correctly indicated - Renaming the currently selected profile works now Change-Id: Ifa47fd672a6976c07d3e80741cfd61b548a5e1f8 Reviewed-on: https://code.wireshark.org/review/33921 Petri-Dish: Roland Knall Reviewed-by: Roland Knall --- ui/qt/main_status_bar.cpp | 12 ++++--- ui/qt/models/profile_model.cpp | 51 +++++++++++++++++++-------- ui/qt/models/profile_model.h | 5 ++- ui/qt/profile_dialog.cpp | 53 ++++++++++++++++++----------- ui/qt/profile_dialog.ui | 48 +++++++++++++++++++++++--- ui/qt/widgets/profile_tree_view.cpp | 10 ++++++ 6 files changed, 134 insertions(+), 45 deletions(-) diff --git a/ui/qt/main_status_bar.cpp b/ui/qt/main_status_bar.cpp index 2aac68b053..b6f34d8da4 100644 --- a/ui/qt/main_status_bar.cpp +++ b/ui/qt/main_status_bar.cpp @@ -537,6 +537,7 @@ void MainStatusBar::showProfileMenu(const QPoint &global_pos, Qt::MouseButton bu { ProfileModel model; + QMenu profile_menu_; QActionGroup global(this); QActionGroup user(this); @@ -548,7 +549,11 @@ void MainStatusBar::showProfileMenu(const QPoint &global_pos, Qt::MouseButton bu QAction * pa = Q_NULLPTR; QString name = idx.data().toString(); - if ( idx.data(ProfileModel::DATA_IS_DEFAULT).toBool() || idx.data(ProfileModel::DATA_IS_GLOBAL).toBool() ) + if ( idx.data(ProfileModel::DATA_IS_DEFAULT).toBool() ) + { + pa = profile_menu_.addAction(name); + } + else if ( idx.data(ProfileModel::DATA_IS_GLOBAL).toBool() ) { /* Check if this profile does not exist as user */ if ( cnt == model.findByName(name) ) @@ -571,10 +576,9 @@ void MainStatusBar::showProfileMenu(const QPoint &global_pos, Qt::MouseButton bu connect(pa, &QAction::triggered, this, &MainStatusBar::switchToProfile); } - QMenu profile_menu_; - profile_menu_.addActions(global.actions()); - profile_menu_.addSeparator(); profile_menu_.addActions(user.actions()); + profile_menu_.addSeparator(); + profile_menu_.addActions(global.actions()); if (button == Qt::LeftButton) { profile_menu_.exec(global_pos); diff --git a/ui/qt/models/profile_model.cpp b/ui/qt/models/profile_model.cpp index 64360cbcbf..692328b20c 100644 --- a/ui/qt/models/profile_model.cpp +++ b/ui/qt/models/profile_model.cpp @@ -202,8 +202,10 @@ QVariant ProfileModel::data(const QModelIndex &index, int role) const case COL_NAME: return QString(prof->name); case COL_TYPE: - if ( prof->is_global || prof->status == PROF_STAT_DEFAULT ) - return tr("Global"); + if ( prof->status == PROF_STAT_DEFAULT ) + return tr("Default"); + else if ( prof->is_global ) + return tr("System"); else return tr("User"); case COL_PATH: @@ -222,6 +224,9 @@ QVariant ProfileModel::data(const QModelIndex &index, int role) const } case PROF_STAT_NEW: return tr("Created from default settings"); + case PROF_STAT_CHANGED: + if (prof->reference) + return QString("%1 %2").arg(tr("Renamed from: ")).arg(prof->reference); case PROF_STAT_COPY: if (prof->reference) return QString("%1 %2").arg(tr("Copied from: ")).arg(prof->reference); @@ -302,10 +307,6 @@ QVariant ProfileModel::data(const QModelIndex &index, int role) const break; case PROF_STAT_NEW: return tr("Created from default settings"); - case PROF_STAT_CHANGED: - if ( prof->reference ) - return tr("Renamed from %1").arg(prof->reference); - break; default: break; } @@ -333,20 +334,22 @@ QVariant ProfileModel::data(const QModelIndex &index, int role) const case ProfileModel::DATA_IS_SELECTED: { QModelIndex selected = activeProfile(); - profile_def * selprof = guard(selected.row()); - if ( selprof ) + if ( selected.isValid() && selected.row() < profiles_.count() ) { - if ( selprof->is_global != prof->is_global ) + profile_def * selprof = guard(selected.row()); + if ( selprof && selprof->is_global != prof->is_global ) return qVariantFromValue(false); - if ( strcmp(selprof->name, prof->name) == 0 ) + if ( selprof && strcmp(selprof->name, prof->name) == 0 ) return qVariantFromValue(true); } return qVariantFromValue(false); } case ProfileModel::DATA_PATH_IS_NOT_DESCRIPTION: - if ( prof->status == PROF_STAT_NEW || prof->status == PROF_STAT_COPY || ( prof->status == PROF_STAT_DEFAULT && reset_default_ ) ) + if ( prof->status == PROF_STAT_NEW || prof->status == PROF_STAT_COPY + || ( prof->status == PROF_STAT_DEFAULT && reset_default_ ) + || prof->status == PROF_STAT_CHANGED ) return qVariantFromValue(false); else return qVariantFromValue(true); @@ -409,7 +412,7 @@ Qt::ItemFlags ProfileModel::flags(const QModelIndex &index) const if ( ! prof ) return fl; - if ( index.column() == ProfileModel::COL_NAME && prof->status != PROF_STAT_DEFAULT && ! prof->is_global && set_profile_.compare(prof->name) != 0 ) + if ( index.column() == ProfileModel::COL_NAME && prof->status != PROF_STAT_DEFAULT && ! prof->is_global ) fl |= Qt::ItemIsEditable; return fl; @@ -447,10 +450,18 @@ QList ProfileModel::findAllByNameAndVisibility(QString name, bool isGlobal) QModelIndex ProfileModel::addNewProfile(QString name) { - add_to_profile_list(name.toUtf8().constData(), "", PROF_STAT_NEW, FALSE, FALSE); + int cnt = 1; + QString newName = name; + while(findByNameAndVisibility(newName) >= 0) + { + newName = QString("%1 %2").arg(name).arg(QString::number(cnt)); + cnt++; + } + + add_to_profile_list(newName.toUtf8().constData(), "", PROF_STAT_NEW, FALSE, FALSE); loadProfiles(); - return index(findByName(name), COL_NAME); + return index(findByName(newName), COL_NAME); } QModelIndex ProfileModel::duplicateEntry(QModelIndex idx) @@ -473,7 +484,17 @@ QModelIndex ProfileModel::duplicateEntry(QModelIndex idx) new_name = QString("%1 (%2)").arg(parent).arg(tr("copy")); if ( findByNameAndVisibility(new_name) >= 0 ) - return QModelIndex(); + { + int cnt = 1; + QString copyName = new_name; + while(findByNameAndVisibility(copyName) >= 0) + { + copyName = new_name; + copyName = copyName.replace(tr("copy"), tr("copy").append(" %1").arg(QString::number(cnt))); + cnt++; + } + new_name = copyName; + } if ( new_name.compare(QString(new_name.toUtf8().constData())) != 0 && !prof->is_global ) return QModelIndex(); diff --git a/ui/qt/models/profile_model.h b/ui/qt/models/profile_model.h index 527101c531..a7492e9032 100644 --- a/ui/qt/models/profile_model.h +++ b/ui/qt/models/profile_model.h @@ -65,7 +65,10 @@ public: DATA_IS_DEFAULT, DATA_IS_GLOBAL, DATA_IS_SELECTED, - DATA_PATH_IS_NOT_DESCRIPTION + DATA_PATH_IS_NOT_DESCRIPTION, + DATA_COL_NAME, + DATA_COL_TYPE, + DATA_COL_PATH } data_values_; // QAbstractItemModel interface diff --git a/ui/qt/profile_dialog.cpp b/ui/qt/profile_dialog.cpp index 6bf2e689bc..c36441190e 100644 --- a/ui/qt/profile_dialog.cpp +++ b/ui/qt/profile_dialog.cpp @@ -84,7 +84,7 @@ ProfileDialog::ProfileDialog(QWidget *parent) : selectProfile(); QStringList items; - items << tr("All Profiles") << tr("Global profiles") << tr("User-defined profiles"); + items << tr("All Profiles") << tr("System profiles") << tr("User-defined profiles"); pd_ui_->cmbProfileTypes->addItems(items); connect (pd_ui_->cmbProfileTypes, SIGNAL(currentTextChanged(const QString &)), @@ -92,7 +92,9 @@ ProfileDialog::ProfileDialog(QWidget *parent) : connect (pd_ui_->lineProfileFilter, SIGNAL(textChanged(const QString &)), this, SLOT(filterChanged(const QString &))); - updateWidgets(); + currentItemChanged(); + + pd_ui_->profileTreeView->setFocus(); } ProfileDialog::~ProfileDialog() @@ -192,14 +194,23 @@ void ProfileDialog::updateWidgets() void ProfileDialog::currentItemChanged() { + QModelIndex idx = pd_ui_->profileTreeView->currentIndex(); + if ( idx.isValid() ) + { + QModelIndex temp = sort_model_->index(idx.row(), ProfileModel::COL_PATH); + if ( idx.data(ProfileModel::DATA_PATH_IS_NOT_DESCRIPTION).toBool() ) + pd_ui_->lblInfo->setUrl(QUrl::fromLocalFile(temp.data().toString()).toString()); + else + pd_ui_->lblInfo->setUrl(QString()); + pd_ui_->lblInfo->setText(temp.data().toString()); + pd_ui_->lblInfo->setToolTip(temp.data(Qt::ToolTipRole).toString()); + } + updateWidgets(); } void ProfileDialog::on_newToolButton_clicked() { - if ( model_->findByName(tr("New profile")) >= 0 ) - return; - pd_ui_->cmbProfileTypes->setCurrentIndex(ProfileSortModel::UserProfiles); sort_model_->setFilterString(); @@ -207,8 +218,9 @@ void ProfileDialog::on_newToolButton_clicked() if (ridx.isValid()) { pd_ui_->profileTreeView->setCurrentIndex(ridx); + pd_ui_->profileTreeView->scrollTo(ridx); pd_ui_->profileTreeView->edit(ridx); - updateWidgets(); + currentItemChanged(); } } @@ -217,7 +229,8 @@ void ProfileDialog::on_deleteToolButton_clicked() QModelIndex index = sort_model_->mapToSource(pd_ui_->profileTreeView->currentIndex()); model_->deleteEntry(index); - updateWidgets(); + + currentItemChanged(); } void ProfileDialog::on_copyToolButton_clicked() @@ -234,21 +247,22 @@ void ProfileDialog::on_copyToolButton_clicked() if (ridx.isValid()) { pd_ui_->profileTreeView->setCurrentIndex(sort_model_->mapFromSource(ridx)); + pd_ui_->profileTreeView->scrollTo(sort_model_->mapFromSource(ridx)); pd_ui_->profileTreeView->edit(sort_model_->mapFromSource(ridx)); - updateWidgets(); + currentItemChanged(); } } void ProfileDialog::on_buttonBox_accepted() { - QModelIndex default_item = sort_model_->mapFromSource(model_->index(0, ProfileModel::COL_NAME)); - QModelIndex index = sort_model_->mapToSource(pd_ui_->profileTreeView->currentIndex()); - if (index.column() != ProfileModel::COL_NAME) - index = index.sibling(index.row(), ProfileModel::COL_NAME); - bool write_recent = true; bool item_data_removed = false; + QModelIndex index = sort_model_->mapToSource(pd_ui_->profileTreeView->currentIndex()); + QModelIndex default_item = sort_model_->mapFromSource(model_->index(0, ProfileModel::COL_NAME)); + if (index.column() != ProfileModel::COL_NAME) + index = index.sibling(index.row(), ProfileModel::COL_NAME); + if (default_item.data(ProfileModel::DATA_STATUS).toInt() == PROF_STAT_DEFAULT && model_->resetDefault()) { // Reset Default profile. @@ -284,16 +298,15 @@ void ProfileDialog::on_buttonBox_accepted() model_->doResetModel(); - const char * profile_name = Q_NULLPTR; + QString profileName; if (index.isValid() && !item_data_removed) { - QString profileName = model_->data(index).toString(); - profile_name = profileName.toLatin1().data(); + profileName = model_->data(index).toString(); } - if (profile_exists (profile_name, FALSE) || profile_exists (profile_name, TRUE)) { + if (profileName.length() > 0 && model_->findByName(profileName) >= 0) { // The new profile exists, change. - wsApp->setConfigurationProfile (profile_name, FALSE); - } else if (!profile_exists (get_profile_name(), FALSE)) { + wsApp->setConfigurationProfile (profileName.toUtf8().constData(), FALSE); + } else if (!model_->activeProfile().isValid()) { // The new profile does not exist, and the previous profile has // been deleted. Change to the default profile. wsApp->setConfigurationProfile (Q_NULLPTR, FALSE); @@ -307,7 +320,7 @@ void ProfileDialog::on_buttonBox_helpRequested() void ProfileDialog::editingFinished() { - updateWidgets(); + currentItemChanged(); } void ProfileDialog::filterChanged(const QString &text) diff --git a/ui/qt/profile_dialog.ui b/ui/qt/profile_dialog.ui index 63da77d532..3e7f36709e 100644 --- a/ui/qt/profile_dialog.ui +++ b/ui/qt/profile_dialog.ui @@ -6,8 +6,8 @@ 0 0 - 557 - 386 + 570 + 400 @@ -60,6 +60,10 @@ + + + :/stock/plus-8.png:/stock/plus-8.png + @@ -67,6 +71,10 @@ Remove this profile. System provided profiles cannot be removed. + + + :/stock/minus-8.png:/stock/minus-8.png + @@ -77,6 +85,17 @@ + + + :/stock/copy-8.png:/stock/copy-8.png + + + + + + + Import + @@ -93,9 +112,21 @@ - + + + + 1 + 0 + + - Import + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + true @@ -124,8 +155,15 @@ QTreeView
widgets/profile_tree_view.h
+ + ElidedLabel + QLabel +
widgets/elided_label.h
+
- + + + buttonBox diff --git a/ui/qt/widgets/profile_tree_view.cpp b/ui/qt/widgets/profile_tree_view.cpp index 6d00719f70..06b307fe28 100644 --- a/ui/qt/widgets/profile_tree_view.cpp +++ b/ui/qt/widgets/profile_tree_view.cpp @@ -63,6 +63,11 @@ void ProfileTreeView::selectionChanged(const QItemSelection &selected, const QIt QItemSelection newSelection; newSelection << deselected.at(0); selectionModel()->select(newSelection, QItemSelectionModel::ClearAndSelect); + if (newSelection.count() > 0) + { + QModelIndexList selIndex = selectionModel()->selectedIndexes(); + scrollTo(selIndex.at(0)); + } } else if ( selected.count() > 1 ) { @@ -71,6 +76,11 @@ void ProfileTreeView::selectionChanged(const QItemSelection &selected, const QIt QItemSelection newSelection; newSelection << intersection.toList().at(0); selectionModel()->select(newSelection, QItemSelectionModel::ClearAndSelect); + if (newSelection.count() > 0) + { + QModelIndexList selIndex = selectionModel()->selectedIndexes(); + scrollTo(selIndex.at(0)); + } } else QTreeView::selectionChanged(selected, deselected);