Skip to content

Commit

Permalink
fix: adjust the width of key list cell correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
saturneric committed Dec 20, 2024
1 parent bd190d9 commit ee34918
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 17 deletions.
8 changes: 1 addition & 7 deletions src/core/model/GpgKeyTableModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

#include "GpgKeyTableModel.h"

#include <utility>

#include "core/function/gpg/GpgKeyGetter.h"
#include "core/model/GpgKey.h"

Expand Down Expand Up @@ -62,10 +60,6 @@ auto GpgKeyTableModel::data(const QModelIndex &index,
int role) const -> QVariant {
if (!index.isValid() || buffered_keys_.empty()) return {};

if (role == Qt::TextAlignmentRole) {
return Qt::AlignCenter;
}

if (role == Qt::CheckStateRole) {
if (index.column() == 0) {
return key_check_state_[index.row()] ? Qt::Checked : Qt::Unchecked;
Expand Down Expand Up @@ -107,7 +101,7 @@ auto GpgKeyTableModel::data(const QModelIndex &index,
return key.GetId();
}
case 7: {
return key.GetCreateTime();
return QLocale().toString(key.GetCreateTime());
}
case 8: {
return key.GetKeyAlgo();
Expand Down
1 change: 1 addition & 0 deletions src/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ aux_source_directory(dialog/import_export UI_SOURCE)
aux_source_directory(dialog/controller UI_SOURCE)
aux_source_directory(dialog UI_SOURCE)
aux_source_directory(function UI_SOURCE)
aux_source_directory(model UI_SOURCE)

# define libgpgfrontend_ui
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
Expand Down
2 changes: 1 addition & 1 deletion src/ui/main_window/MainWindowSlotFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ void MainWindow::SlotEncryptEML() {
}
}

LOG_E() << "mime or signature data is missing";
return 0;
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

#include "GpgKeyTableProxyModel.h"

#include <utility>

#include "core/function/gpg/GpgKeyGetter.h"
#include "core/model/CacheObject.h"
#include "core/model/GpgKey.h"
Expand All @@ -45,7 +43,9 @@ GpgKeyTableProxyModel::GpgKeyTableProxyModel(
model_(std::move(model)),
display_mode_(display_mode),
filter_columns_(columns),
custom_filter_(std::move(filter)) {
custom_filter_(std::move(filter)),
default_font_("Arial", 14),
default_metrics_(default_font_) {
setSourceModel(model_.get());

connect(this, &GpgKeyTableProxyModel::SignalFavoritesChanged, this,
Expand Down Expand Up @@ -197,4 +197,81 @@ void GpgKeyTableProxyModel::slot_update_favorites_cache() {
favorite_key_ids_ = cache_obj.key_dbs[key_db_name].key_ids;
}
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

#pragma once

#include <utility>
#include <QFont>
#include <QFontMetrics>

#include "core/model/GpgKeyTableModel.h"

Expand All @@ -49,6 +50,12 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyTableProxyModel

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;
Expand Down Expand Up @@ -97,6 +104,9 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyTableProxyModel
QString filter_keywords_;
QList<QString> favorite_key_ids_;
KeyFilter custom_filter_;

QFont default_font_;
QFontMetrics default_metrics_;
};

} // namespace GpgFrontend
2 changes: 0 additions & 2 deletions src/ui/widgets/KeyList.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ class KeyList : public QWidget {
*/
void SignalRefreshDatabase();

signals:

/**
* @brief
*
Expand Down
7 changes: 6 additions & 1 deletion src/ui/widgets/KeyTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ KeyTable::KeyTable(QWidget* parent, QSharedPointer<GpgKeyTableModel> model,

verticalHeader()->hide();
horizontalHeader()->setStretchLastSection(false);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
horizontalHeader()->setResizeContentsPrecision(1000);

setShowGrid(false);
sortByColumn(2, Qt::AscendingOrder);
Expand All @@ -68,6 +68,10 @@ 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,
Expand Down Expand Up @@ -146,4 +150,5 @@ void KeyTable::UncheckAll() {

return selected_indexes.first().row();
}

} // namespace GpgFrontend::UI
3 changes: 1 addition & 2 deletions src/ui/widgets/KeyTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#include "core/model/GpgKey.h"
#include "core/model/GpgKeyTableModel.h"
#include "core/model/GpgKeyTableProxyModel.h"
#include "ui/model/GpgKeyTableProxyModel.h"

namespace GpgFrontend::UI {

Expand Down Expand Up @@ -175,7 +175,6 @@ struct KeyTable : public QTableView {
private:
QSharedPointer<GpgKeyTableModel> model_;
GpgKeyTableProxyModel proxy_model_;

GpgKeyTableColumn column_filter_;
};

Expand Down

0 comments on commit ee34918

Please sign in to comment.