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 <rknall@gmail.com>
Reviewed-by: Roland Knall <rknall@gmail.com>
This commit is contained in:
Roland Knall 2019-07-12 21:35:05 +02:00
parent 85e32bdbe7
commit 2c7a35bc98
6 changed files with 134 additions and 45 deletions

View File

@ -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);

View File

@ -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<int> 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();

View File

@ -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

View File

@ -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)

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>557</width>
<height>386</height>
<width>570</width>
<height>400</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
@ -60,6 +60,10 @@
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../image/stock_icons.qrc">
<normaloff>:/stock/plus-8.png</normaloff>:/stock/plus-8.png</iconset>
</property>
</widget>
</item>
<item>
@ -67,6 +71,10 @@
<property name="toolTip">
<string>Remove this profile. System provided profiles cannot be removed.</string>
</property>
<property name="icon">
<iconset resource="../../image/stock_icons.qrc">
<normaloff>:/stock/minus-8.png</normaloff>:/stock/minus-8.png</iconset>
</property>
</widget>
</item>
<item>
@ -77,6 +85,17 @@
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../image/stock_icons.qrc">
<normaloff>:/stock/copy-8.png</normaloff>:/stock/copy-8.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnImport">
<property name="text">
<string>Import</string>
</property>
</widget>
</item>
<item>
@ -93,9 +112,21 @@
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnImport">
<widget class="ElidedLabel" name="lblInfo">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Import</string>
<string/>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
@ -124,8 +155,15 @@
<extends>QTreeView</extends>
<header>widgets/profile_tree_view.h</header>
</customwidget>
<customwidget>
<class>ElidedLabel</class>
<extends>QLabel</extends>
<header>widgets/elided_label.h</header>
</customwidget>
</customwidgets>
<resources/>
<resources>
<include location="../../image/stock_icons.qrc"/>
</resources>
<connections>
<connection>
<sender>buttonBox</sender>

View File

@ -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);