diff options
author | saturneric <[email protected]> | 2024-12-21 02:36:30 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2024-12-21 02:36:30 +0000 |
commit | 78d76b454769e0dcbaaeb7bbe30ec9d315b7388c (patch) | |
tree | 384c51fcbe9cf380e38add322708682718fec9af | |
parent | fix: symbols link error on mingw64 (diff) | |
download | GpgFrontend-78d76b454769e0dcbaaeb7bbe30ec9d315b7388c.tar.gz GpgFrontend-78d76b454769e0dcbaaeb7bbe30ec9d315b7388c.zip |
fix: ResizeToContents behaviors of key list
-rw-r--r-- | src/core/model/GpgKeyTableModel.cpp | 121 | ||||
-rw-r--r-- | src/ui/model/GpgKeyTableProxyModel.cpp | 76 | ||||
-rw-r--r-- | src/ui/model/GpgKeyTableProxyModel.h | 6 | ||||
-rw-r--r-- | src/ui/widgets/KeyTable.cpp | 7 |
4 files changed, 70 insertions, 140 deletions
diff --git a/src/core/model/GpgKeyTableModel.cpp b/src/core/model/GpgKeyTableModel.cpp index edc125ef..2fbc60ee 100644 --- a/src/core/model/GpgKeyTableModel.cpp +++ b/src/core/model/GpgKeyTableModel.cpp @@ -64,67 +64,84 @@ auto GpgKeyTableModel::data(const QModelIndex &index, if (index.column() == 0) { return key_check_state_[index.row()] ? Qt::Checked : Qt::Unchecked; } - return {}; } - const auto &key = buffered_keys_.at(index.row()); - - switch (index.column()) { - case 0: { - return index.row(); - } - case 1: { - QString type_sym; - type_sym += key.IsPrivateKey() ? "pub/sec" : "pub"; - if (key.IsPrivateKey() && !key.IsHasMasterKey()) type_sym += "#"; - if (key.IsHasCardKey()) type_sym += "^"; - return type_sym; - } - case 2: { - return key.GetName(); - } - case 3: { - return key.GetEmail(); - } - case 4: { - QString usage_sym; - if (key.IsHasActualCertificationCapability()) usage_sym += "C"; - if (key.IsHasActualEncryptionCapability()) usage_sym += "E"; - if (key.IsHasActualSigningCapability()) usage_sym += "S"; - if (key.IsHasActualAuthenticationCapability()) usage_sym += "A"; - return usage_sym; - } - case 5: { - return key.GetOwnerTrust(); - } - case 6: { - return key.GetId(); - } - case 7: { - return QLocale().toString(key.GetCreateTime()); - } - case 8: { - return key.GetKeyAlgo(); + if (role == Qt::DisplayRole) { + const auto &key = buffered_keys_.at(index.row()); + + switch (index.column()) { + case 0: { + return index.row(); + } + case 1: { + QString type_sym; + type_sym += key.IsPrivateKey() ? "pub/sec" : "pub"; + if (key.IsPrivateKey() && !key.IsHasMasterKey()) type_sym += "#"; + if (key.IsHasCardKey()) type_sym += "^"; + return type_sym; + } + case 2: { + return key.GetName(); + } + case 3: { + return key.GetEmail(); + } + case 4: { + QString usage_sym; + if (key.IsHasActualCertificationCapability()) usage_sym += "C"; + if (key.IsHasActualEncryptionCapability()) usage_sym += "E"; + if (key.IsHasActualSigningCapability()) usage_sym += "S"; + if (key.IsHasActualAuthenticationCapability()) usage_sym += "A"; + return usage_sym; + } + case 5: { + return key.GetOwnerTrust(); + } + case 6: { + return key.GetId(); + } + case 7: { + return QLocale().toString(key.GetCreateTime()); + } + case 8: { + return key.GetKeyAlgo(); + } + case 9: { + return static_cast<int>(key.GetSubKeys()->size()); + } + case 10: { + return key.GetComment(); + } + default: + return {}; } - case 9: { - return static_cast<int>(key.GetSubKeys()->size()); - } - case 10: { - return key.GetComment(); + } + + if (role == Qt::TextAlignmentRole) { + switch (index.column()) { + case 0: + case 1: + case 4: + case 5: + case 6: + case 7: + case 9: + return Qt::AlignCenter; + default: + return {}; } - default: - return {}; } + + return {}; } auto GpgKeyTableModel::headerData(int section, Qt::Orientation orientation, int role) const -> QVariant { - if (role != Qt::DisplayRole) return {}; - - if (orientation == Qt::Horizontal) { - return column_headers_[section]; + if (role == Qt::DisplayRole) { + if (orientation == Qt::Horizontal) { + return column_headers_[section]; + } } - return {}; } @@ -132,7 +149,7 @@ auto GpgKeyTableModel::flags(const QModelIndex &index) const -> Qt::ItemFlags { if (!index.isValid()) return Qt::NoItemFlags; if (index.column() == 0) { - return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled; + return Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled; } return Qt::ItemIsSelectable | Qt::ItemIsEnabled; diff --git a/src/ui/model/GpgKeyTableProxyModel.cpp b/src/ui/model/GpgKeyTableProxyModel.cpp index 5fa5bea4..9dc98bcd 100644 --- a/src/ui/model/GpgKeyTableProxyModel.cpp +++ b/src/ui/model/GpgKeyTableProxyModel.cpp @@ -198,80 +198,4 @@ void GpgKeyTableProxyModel::slot_update_favorites_cache() { } } -auto GpgKeyTableProxyModel::data(const QModelIndex &index, - int role) const -> QVariant { - if (role == Qt::FontRole) { - return default_font_; - } - - if (role == Qt::TextAlignmentRole) { - return Qt::AlignCenter; - } - - if (role == Qt::SizeHintRole) { - const QVariant display_data = model_->data(index, Qt::DisplayRole); - if (!display_data.isValid()) { - return {}; - } - - const QString text = display_data.toString(); - - QRect rect = default_metrics_.boundingRect(QRect{}, Qt::AlignCenter, text); - - const int horizontal_padding = 15; - const int vertical_padding = 2; - -#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0) - const int raw_width = default_metrics_.horizontalAdvance(text); -#else - const int raw_width = rect.width(); -#endif - - const int width = - static_cast<int>(raw_width * 1.15) + horizontal_padding * 2; - const int height = rect.height() + vertical_padding * 2; - - LOG_D() << "row text: " << text << "width: " << width; - - return QSize(width, height); - } - - return sourceModel()->data(index, role); -} - -auto GpgKeyTableProxyModel::headerData(int section, Qt::Orientation orientation, - int role) const -> QVariant { - if (role == Qt::FontRole) { - return default_font_; - } - - if (role == Qt::SizeHintRole) { - const QVariant display_data = - model_->headerData(section, orientation, Qt::DisplayRole); - if (!display_data.isValid()) { - return {}; - } - - const QString text = display_data.toString(); - - QRect rect = default_metrics_.boundingRect(QRect{}, Qt::AlignCenter, text); - - const int horizontal_padding = 15; - const int vertical_padding = 2; - -#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0) - const int raw_width = default_metrics_.horizontalAdvance(text); -#else - const int raw_width = rect.width(); -#endif - - const int width = - static_cast<int>(raw_width * 1.15) + horizontal_padding * 2; - const int height = rect.height() + vertical_padding * 2; - - return QSize(width, height); - } - - return sourceModel()->headerData(section, orientation, role); -} } // namespace GpgFrontend::UI
\ No newline at end of file diff --git a/src/ui/model/GpgKeyTableProxyModel.h b/src/ui/model/GpgKeyTableProxyModel.h index 44ceee45..dd086ba9 100644 --- a/src/ui/model/GpgKeyTableProxyModel.h +++ b/src/ui/model/GpgKeyTableProxyModel.h @@ -49,12 +49,6 @@ class GpgKeyTableProxyModel : public QSortFilterProxyModel { void ResetGpgKeyTableModel(QSharedPointer<GpgKeyTableModel> model); - [[nodiscard]] auto data(const QModelIndex &index, - int role) const -> QVariant override; - - [[nodiscard]] auto headerData(int section, Qt::Orientation orientation, - int role) const -> QVariant override; - protected: [[nodiscard]] auto filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const -> bool override; diff --git a/src/ui/widgets/KeyTable.cpp b/src/ui/widgets/KeyTable.cpp index 6fe2d966..542382cf 100644 --- a/src/ui/widgets/KeyTable.cpp +++ b/src/ui/widgets/KeyTable.cpp @@ -54,8 +54,7 @@ KeyTable::KeyTable(QWidget* parent, QSharedPointer<GpgKeyTableModel> model, verticalHeader()->hide(); horizontalHeader()->setStretchLastSection(false); - horizontalHeader()->setResizeContentsPrecision(1000); - + horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); setShowGrid(false); sortByColumn(2, Qt::AscendingOrder); setSelectionBehavior(QAbstractItemView::SelectRows); @@ -68,10 +67,6 @@ KeyTable::KeyTable(QWidget* parent, QSharedPointer<GpgKeyTableModel> model, setAlternatingRowColors(true); setSortingEnabled(true); - for (int i = 1; i < proxy_model_.columnCount(); ++i) { - this->resizeColumnToContents(i); - } - connect(CommonUtils::GetInstance(), &CommonUtils::SignalFavoritesChanged, &proxy_model_, &GpgKeyTableProxyModel::SignalFavoritesChanged); connect(this, &KeyTable::SignalColumnTypeChange, this, |