diff options
author | Roland Knall <rknall@gmail.com> | 2019-07-20 23:38:24 +0200 |
---|---|---|
committer | Roland Knall <rknall@gmail.com> | 2019-07-22 15:57:48 +0000 |
commit | 91ed69488f6bcd486ae0673c4af58f1257a671af (patch) | |
tree | 2223308e44c788f7620e5c7abcfc159fe8cd0711 | |
parent | 429082dd71a00e7eb67efb6c8c20d0b95c532da1 (diff) |
Qt: Cleanup data() method and interface
Interface resembles the old interface
Change-Id: I7301f8bb6e00d30c20d7e5fdc9252a6a1765ff97
Reviewed-on: https://code.wireshark.org/review/34054
Petri-Dish: Roland Knall <rknall@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Roland Knall <rknall@gmail.com>
-rw-r--r-- | ui/qt/models/profile_model.cpp | 292 | ||||
-rw-r--r-- | ui/qt/models/profile_model.h | 13 | ||||
-rw-r--r-- | ui/qt/profile_dialog.cpp | 18 | ||||
-rw-r--r-- | ui/qt/widgets/copy_from_profile_menu.cpp | 6 | ||||
-rw-r--r-- | ui/qt/widgets/profile_tree_view.cpp | 8 |
5 files changed, 189 insertions, 148 deletions
diff --git a/ui/qt/models/profile_model.cpp b/ui/qt/models/profile_model.cpp index 441828359a..5e38c93dc4 100644 --- a/ui/qt/models/profile_model.cpp +++ b/ui/qt/models/profile_model.cpp @@ -188,10 +188,8 @@ profile_def * ProfileModel::guard(int row) const return profiles_.at(row); } -QVariant ProfileModel::data(const QModelIndex &index, int role) const +QVariant ProfileModel::dataDisplay(const QModelIndex &index) const { - QString msg; - if ( ! index.isValid() || profiles_.count() <= index.row() ) return QVariant(); @@ -199,140 +197,196 @@ QVariant ProfileModel::data(const QModelIndex &index, int role) const if ( ! prof ) return QVariant(); - switch ( role ) + switch (index.column()) { - case Qt::DisplayRole: - switch (index.column()) - { - case COL_NAME: - return QString(prof->name); - case COL_TYPE: - if ( prof->status == PROF_STAT_DEFAULT ) - return tr("Default"); - else if ( prof->is_global ) - return tr("System"); - else - return tr("User"); - case COL_PATH: - switch (prof->status) - { - case PROF_STAT_DEFAULT: - if (!reset_default_) - return get_persconffile_path("", FALSE); - else - return tr("Resetting to default"); - case PROF_STAT_EXISTS: - { - QString profile_path = prof->is_global ? get_global_profiles_dir() : get_profiles_dir(); - profile_path.append(QDir::separator()).append(prof->name); - return profile_path; - } - 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); - break; - case PROF_STAT_COPY: - if (prof->reference) - return QString("%1 %2").arg(tr("Copied from: ")).arg(prof->reference); - break; - } - break; - default: - break; - } + case COL_NAME: + return QString(prof->name); + case COL_TYPE: + if ( prof->status == PROF_STAT_DEFAULT ) + return tr("Default"); + else if ( prof->is_global ) + return tr("System"); + else + return tr("User"); + default: break; + } - case Qt::FontRole: - { - QFont font; + return QVariant(); +} + +QVariant ProfileModel::dataFontRole(const QModelIndex &index) const +{ + if ( ! index.isValid() || profiles_.count() <= index.row() ) + return QVariant(); + + profile_def * prof = guard(index.row()); + if ( ! prof ) + return QVariant(); + + QFont font; if ( prof->is_global ) - font.setItalic(true); + font.setItalic(true); if ( set_profile_.compare(prof->name) == 0 && ! prof->is_global ) font.setBold(true); - if ( prof->status == PROF_STAT_DEFAULT && reset_default_ ) - font.setStrikeOut(true); + if ( prof->status == PROF_STAT_DEFAULT && reset_default_ ) + font.setStrikeOut(true); - return font; - } + return font; +} - case Qt::BackgroundColorRole: - { - if ( ! ProfileModel::checkNameValidity(QString(prof->name)) ) - return ColorUtils::fromColorT(&prefs.gui_text_invalid); +QVariant ProfileModel::dataBackgroundRole(const QModelIndex &index) const +{ + if ( ! index.isValid() || profiles_.count() <= index.row() ) + return QVariant(); + + profile_def * prof = guard(index.row()); + if ( ! prof ) + return QVariant(); - if ( prof->status == PROF_STAT_DEFAULT && reset_default_ ) - return ColorUtils::fromColorT(&prefs.gui_text_deprecated); + if ( ! ProfileModel::checkNameValidity(QString(prof->name)) ) + return ColorUtils::fromColorT(&prefs.gui_text_invalid); - QList<int> rows = const_cast<ProfileModel *>(this)->findAllByNameAndVisibility(QString(prof->name), prof->is_global); - if ( rows.count() > 1 ) - return ColorUtils::fromColorT(&prefs.gui_text_invalid); + if ( prof->status == PROF_STAT_DEFAULT && reset_default_ ) + return ColorUtils::fromColorT(&prefs.gui_text_deprecated); - break; - } + QList<int> rows = const_cast<ProfileModel *>(this)->findAllByNameAndVisibility(QString(prof->name), prof->is_global); + if ( rows.count() > 1 ) + return ColorUtils::fromColorT(&prefs.gui_text_invalid); - case Qt::ToolTipRole: - switch (prof->status) - { - case PROF_STAT_DEFAULT: - if (reset_default_) - return tr("Will be reset to default values"); - break; - case PROF_STAT_COPY: - if (prof->reference) { - QString reference = prof->reference; - GList *fl_entry = entry(prof); - if (fl_entry) - { - profile_def *profile = reinterpret_cast<profile_def *>(fl_entry->data); - if (strcmp(prof->reference, profile->reference) == 0) { - if (profile->status == PROF_STAT_CHANGED) { - // Reference profile was renamed, use the new name - reference = profile->name; - break; - } + return QVariant(); +} + +QVariant ProfileModel::dataToolTipRole(const QModelIndex &idx) const +{ + if ( ! idx.isValid() || profiles_.count() <= idx.row() ) + return QVariant(); + + profile_def * prof = guard(idx.row()); + if ( ! prof ) + return QVariant(); + + QString msg; + + switch (prof->status) + { + case PROF_STAT_DEFAULT: + if (reset_default_) + return tr("Will be reset to default values"); + break; + case PROF_STAT_COPY: + if (prof->reference) { + QString reference = prof->reference; + GList *fl_entry = entry(prof); + if (fl_entry) + { + profile_def *profile = reinterpret_cast<profile_def *>(fl_entry->data); + if (strcmp(prof->reference, profile->reference) == 0) { + if (profile->status == PROF_STAT_CHANGED) { + // Reference profile was renamed, use the new name + reference = profile->name; + break; } } + } - QString profile_info = tr("Created from %1").arg(reference); - if (prof->from_global) { - profile_info.append(QString(" %1").arg(tr("(system provided)"))); - } else if (!reference.isEmpty()) { - profile_info.append(QString(" %1").arg(tr("(deleted)"))); - } - return profile_info; + QString profile_info = tr("Created from %1").arg(reference); + if (prof->from_global) { + profile_info.append(QString(" %1").arg(tr("(system provided)"))); + } else if (!reference.isEmpty()) { + profile_info.append(QString(" %1").arg(tr("(deleted)"))); } - break; - case PROF_STAT_NEW: - return tr("Created from default settings"); - default: - break; + return profile_info; } + break; + case PROF_STAT_NEW: + return tr("Created from default settings"); + default: + break; + } + + if ( ! ProfileModel::checkNameValidity(QString(prof->name), &msg) ) + return msg; + + if (prof->is_global) + return tr("This is a system provided profile."); + if ( prof->status == PROF_STAT_DEFAULT && reset_default_ ) + return tr("The profile will be reset to default values."); + + return QVariant(); +} - if ( ! ProfileModel::checkNameValidity(QString(prof->name), &msg) ) - return msg; +QVariant ProfileModel::dataPath(const QModelIndex &index) const +{ + if ( ! index.isValid() || profiles_.count() <= index.row() ) + return QVariant(); - if (prof->is_global) - return tr("This is a system provided profile."); - if ( prof->status == PROF_STAT_DEFAULT && reset_default_ ) - return tr("The profile will be reset to default values."); + profile_def * prof = guard(index.row()); + if ( ! prof ) + return QVariant(); + switch (prof->status) + { + case PROF_STAT_DEFAULT: + if (!reset_default_) + return get_persconffile_path("", FALSE); + else + return tr("Resetting to default"); + case PROF_STAT_EXISTS: + { + QString profile_path = prof->is_global ? get_global_profiles_dir() : get_profiles_dir(); + profile_path.append(QDir::separator()).append(prof->name); + return profile_path; + } + 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); break; + case PROF_STAT_COPY: + if (prof->reference) + return QString("%1 %2").arg(tr("Copied from: ")).arg(prof->reference); + break; + } + + return QVariant(); +} +QVariant ProfileModel::data(const QModelIndex &index, int role) const +{ + QString msg; + + if ( ! index.isValid() || profiles_.count() <= index.row() ) + return QVariant(); + + profile_def * prof = guard(index.row()); + if ( ! prof ) + return QVariant(); + + switch ( role ) + { + case Qt::DisplayRole: + return dataDisplay(index); + break; + case Qt::FontRole: + return dataFontRole(index); + break; + case Qt::BackgroundColorRole: + return dataBackgroundRole(index); + break; + case Qt::ToolTipRole: + return dataToolTipRole(index); + break; case ProfileModel::DATA_STATUS: return qVariantFromValue(prof->status); - case ProfileModel::DATA_IS_DEFAULT: return qVariantFromValue(prof->status == PROF_STAT_DEFAULT); - - case ProfileModel::DATA_IS_GLOBAL: return qVariantFromValue(prof->is_global); - case ProfileModel::DATA_IS_SELECTED: { QModelIndex selected = activeProfile(); @@ -347,6 +401,10 @@ QVariant ProfileModel::data(const QModelIndex &index, int role) const } return qVariantFromValue(false); } + break; + case ProfileModel::DATA_PATH: + return dataPath(index); + break; case ProfileModel::DATA_PATH_IS_NOT_DESCRIPTION: if ( prof->status == PROF_STAT_NEW || prof->status == PROF_STAT_COPY @@ -360,26 +418,6 @@ QVariant ProfileModel::data(const QModelIndex &index, int role) const break; } -#if 0 - if (pd_ui_->profileTreeView->topLevelItemCount() > 0) { - profile_def *profile; - for (int i = 0; i < pd_ui_->profileTreeView->topLevelItemCount(); i++) { - item = pd_ui_->profileTreeView->topLevelItem(i); - profile = (profile_def *) VariantPointer<GList>::asPtr(item->data(0, Qt::UserRole))->data; - if (current_profile && !current_profile->is_global && profile != current_profile && strcmp(profile->name, current_profile->name) == 0) { - item->setToolTip(0, tr("A profile already exists with this name.")); - item->setBackground(0, ColorUtils::fromColorT(&prefs.gui_text_invalid)); - if (current_profile->status != PROF_STAT_DEFAULT && - current_profile->status != PROF_STAT_EXISTS) - { - pd_ui_->infoLabel->setText(tr("A profile already exists with this name")); - } - enable_ok = false; - } - } - } -#endif - return QVariant(); } @@ -393,8 +431,6 @@ QVariant ProfileModel::headerData(int section, Qt::Orientation orientation, int return tr("Profile"); case COL_TYPE: return tr("Type"); - case COL_PATH: - return tr("Path"); default: break; } diff --git a/ui/qt/models/profile_model.h b/ui/qt/models/profile_model.h index bb2f6ecd99..42aeaa748a 100644 --- a/ui/qt/models/profile_model.h +++ b/ui/qt/models/profile_model.h @@ -57,7 +57,6 @@ public: enum { COL_NAME, COL_TYPE, - COL_PATH, _LAST_ENTRY } columns_; @@ -66,10 +65,8 @@ public: DATA_IS_DEFAULT, DATA_IS_GLOBAL, DATA_IS_SELECTED, - DATA_PATH_IS_NOT_DESCRIPTION, - DATA_COL_NAME, - DATA_COL_TYPE, - DATA_COL_PATH + DATA_PATH, + DATA_PATH_IS_NOT_DESCRIPTION } data_values_; // QAbstractItemModel interface @@ -118,6 +115,12 @@ private: static bool acceptFile(QString fileName, int fileSize); #endif + QVariant dataDisplay(const QModelIndex & idx) const; + QVariant dataFontRole(const QModelIndex & idx) const; + QVariant dataBackgroundRole(const QModelIndex & idx) const; + QVariant dataToolTipRole(const QModelIndex & idx) const; + QVariant dataPath(const QModelIndex & idx) const; + }; #endif diff --git a/ui/qt/profile_dialog.cpp b/ui/qt/profile_dialog.cpp index c232a594d5..6254ab6ec0 100644 --- a/ui/qt/profile_dialog.cpp +++ b/ui/qt/profile_dialog.cpp @@ -83,6 +83,13 @@ ProfileDialog::ProfileDialog(QWidget *parent) : sort_model_ = new ProfileSortModel(this); sort_model_->setSourceModel(model_); pd_ui_->profileTreeView->setModel(sort_model_); + if ( sort_model_->columnCount() <= 1 ) + pd_ui_->profileTreeView->header()->hide(); + else + { + pd_ui_->profileTreeView->header()->setStretchLastSection(false); + pd_ui_->profileTreeView->header()->setSectionResizeMode(ProfileModel::COL_NAME, QHeaderView::Stretch); + } connect(pd_ui_->profileTreeView, &ProfileTreeView::currentItemChanged, this, &ProfileDialog::currentItemChanged); @@ -217,13 +224,13 @@ void ProfileDialog::currentItemChanged() QModelIndex idx = pd_ui_->profileTreeView->currentIndex(); if ( idx.isValid() ) { - QModelIndex temp = sort_model_->index(idx.row(), ProfileModel::COL_PATH); + QString temp = idx.data(ProfileModel::DATA_PATH).toString(); if ( idx.data(ProfileModel::DATA_PATH_IS_NOT_DESCRIPTION).toBool() ) - pd_ui_->lblInfo->setUrl(QUrl::fromLocalFile(temp.data().toString()).toString()); + pd_ui_->lblInfo->setUrl(QUrl::fromLocalFile(temp).toString()); else pd_ui_->lblInfo->setUrl(QString()); - pd_ui_->lblInfo->setText(temp.data().toString()); - pd_ui_->lblInfo->setToolTip(temp.data(Qt::ToolTipRole).toString()); + pd_ui_->lblInfo->setText(temp); + pd_ui_->lblInfo->setToolTip(idx.data(Qt::ToolTipRole).toString()); } updateWidgets(); @@ -250,7 +257,8 @@ void ProfileDialog::on_deleteToolButton_clicked() model_->deleteEntry(index); - currentItemChanged(); + QModelIndex newIdx = sort_model_->mapFromSource(model_->index(0, 0)); + pd_ui_->profileTreeView->setCurrentIndex(newIdx); } void ProfileDialog::on_copyToolButton_clicked() diff --git a/ui/qt/widgets/copy_from_profile_menu.cpp b/ui/qt/widgets/copy_from_profile_menu.cpp index a4c53a33d8..8ddbb73e21 100644 --- a/ui/qt/widgets/copy_from_profile_menu.cpp +++ b/ui/qt/widgets/copy_from_profile_menu.cpp @@ -30,14 +30,14 @@ CopyFromProfileMenu::CopyFromProfileMenu(QString filename, QWidget *parent) : for(int cnt = 0; cnt < model.rowCount(); cnt++) { QModelIndex idx = model.index(cnt, ProfileModel::COL_NAME); - QModelIndex idxPath = model.index(cnt, ProfileModel::COL_PATH); - if ( ! idx.isValid() || ! idxPath.isValid() ) + QString profilePath = idx.data(ProfileModel::DATA_PATH).toString(); + if ( ! idx.isValid() || ! profilePath.isEmpty() ) continue; if ( ! idx.data(ProfileModel::DATA_PATH_IS_NOT_DESCRIPTION).toBool() || idx.data(ProfileModel::DATA_IS_SELECTED).toBool() ) continue; - QDir profileDir(idxPath.data().toString()); + QDir profileDir(profilePath); if ( ! profileDir.exists() ) continue; diff --git a/ui/qt/widgets/profile_tree_view.cpp b/ui/qt/widgets/profile_tree_view.cpp index b339a9f758..ea3e25d609 100644 --- a/ui/qt/widgets/profile_tree_view.cpp +++ b/ui/qt/widgets/profile_tree_view.cpp @@ -21,9 +21,6 @@ ProfileUrlLinkDelegate::ProfileUrlLinkDelegate(QObject *parent) : UrlLinkDelegat void ProfileUrlLinkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - if ( index.column() != ProfileModel::COL_PATH ) - QStyledItemDelegate::paint(painter, option, index); - /* Only paint links for valid paths */ if ( index.data(ProfileModel::DATA_PATH_IS_NOT_DESCRIPTION).toBool() ) UrlLinkDelegate::paint(painter, option, index); @@ -48,9 +45,6 @@ ProfileTreeView::ProfileTreeView(QWidget *parent) : { delegate_ = new ProfileTreeEditDelegate(); setItemDelegateForColumn(ProfileModel::COL_NAME, delegate_); - setItemDelegateForColumn(ProfileModel::COL_PATH, new ProfileUrlLinkDelegate()); - - resizeColumnToContents(ProfileModel::COL_NAME); connect(this, &QAbstractItemView::clicked, this, &ProfileTreeView::clicked); connect(delegate_, SIGNAL(commitData(QWidget *)), this, SIGNAL(itemUpdated())); @@ -94,7 +88,7 @@ void ProfileTreeView::currentChanged(const QModelIndex ¤t, const QModelInd void ProfileTreeView::clicked(const QModelIndex &index) { - if ( !index.isValid() || index.column() != ProfileModel::COL_PATH ) + if ( !index.isValid() ) return; /* Only paint links for valid paths */ |