Set paperclip column to be fixed size; correct test failures

This commit is contained in:
Jonathan White 2018-02-21 21:25:04 -05:00 committed by Janek Bevendorff
parent 77e345570d
commit 686adbe65a
4 changed files with 61 additions and 48 deletions

View File

@ -41,8 +41,6 @@ EntryModel::EntryModel(QObject* parent)
, m_group(nullptr)
, m_hideUsernames(false)
, m_hidePasswords(true)
, m_paperClipPixmap(16, 16)
, m_paperClipPixmapCentered(24, 16)
{
}
@ -251,13 +249,13 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
case Title:
if (entry->isExpired()) {
return databaseIcons()->iconPixmap(DatabaseIcons::ExpiredIconIndex);
} else {
return entry->iconScaledPixmap();
}
return entry->iconScaledPixmap();
case Paperclip:
if (!entry->attachments()->isEmpty()) {
return m_paperClipPixmapCentered;
return m_paperClipPixmap;
}
break;
}
} else if (role == Qt::FontRole) {
QFont font;
@ -504,8 +502,4 @@ void EntryModel::togglePasswordsHidden(const bool hide)
void EntryModel::setPaperClipPixmap(const QPixmap& paperclip)
{
m_paperClipPixmap = paperclip;
m_paperClipPixmapCentered.fill(Qt::transparent);
QPainter painter2(&m_paperClipPixmapCentered);
painter2.drawPixmap(8, 0, paperclip);
}

View File

@ -99,7 +99,6 @@ private:
bool m_hidePasswords;
QPixmap m_paperClipPixmap;
QPixmap m_paperClipPixmapCentered;
static const QString HiddenContentDisplay;
static const Qt::DateFormat DateFormat;

View File

@ -90,19 +90,18 @@ EntryView::EntryView(QWidget* parent)
m_headerMenu->addSeparator();
m_headerMenu->addAction(tr("Reset to defaults"), this, SLOT(resetViewToDefaults()));
header()->setMinimumSectionSize(24);
header()->setDefaultSectionSize(100);
// Stretching of last section interferes with fitting columns to window
header()->setStretchLastSection(false);
header()->setContextMenuPolicy(Qt::CustomContextMenu);
connect(header(), SIGNAL(customContextMenuRequested(QPoint)), SLOT(showHeaderMenu(QPoint)));
connect(header(), SIGNAL(sectionCountChanged(int, int)), SIGNAL(viewStateChanged()));
connect(header(), SIGNAL(sectionMoved(int, int, int)), SIGNAL(viewStateChanged()));
connect(header(), SIGNAL(sectionResized(int, int, int)), SIGNAL(viewStateChanged()));
connect(header(), SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)), SIGNAL(viewStateChanged()));
// TODO: not working as expected, columns will end up being very small,
// most likely due to the widget not being sized properly at this time
//fitColumnsToWindow();
resetFixedColumns();
// Configure default search view state and save for later use
header()->showSection(EntryModel::ParentGroup);
@ -299,9 +298,11 @@ QByteArray EntryView::viewState() const
/**
* Set view state
*/
bool EntryView::setViewState(const QByteArray& state) const
bool EntryView::setViewState(const QByteArray& state)
{
return header()->restoreState(state);
bool status = header()->restoreState(state);
resetFixedColumns();
return status;
}
/**
@ -374,6 +375,8 @@ void EntryView::toggleColumnVisibility(QAction *action)
void EntryView::fitColumnsToWindow()
{
header()->resizeSections(QHeaderView::Stretch);
resetFixedColumns();
fillRemainingWidth(true);
emit viewStateChanged();
}
@ -385,37 +388,8 @@ void EntryView::fitColumnsToContents()
{
// Resize columns to fit contents
header()->resizeSections(QHeaderView::ResizeToContents);
// Determine total width of currently visible columns. If there is
// still some space available on the header, equally distribute it to
// visible columns and add remaining fraction to last visible column
int width = 0;
for (int columnIndex = 0; columnIndex < header()->count(); ++columnIndex) {
if (!header()->isSectionHidden(columnIndex)) {
width += header()->sectionSize(columnIndex);
}
}
int visible = header()->count() - header()->hiddenSectionCount();
int avail = header()->width() - width;
if ((visible <= 0) || (avail <= 0)) {
return;
}
int add = avail / visible;
width = 0;
int last = 0;
for (int columnIndex = 0; columnIndex < header()->count(); ++columnIndex) {
if (!header()->isSectionHidden(columnIndex)) {
header()->resizeSection(columnIndex, header()->sectionSize(columnIndex) + add);
width += header()->sectionSize(columnIndex);
if (header()->visualIndex(columnIndex) > last) {
last = header()->visualIndex(columnIndex);
}
}
}
header()->resizeSection(header()->logicalIndex(last), header()->sectionSize(last) + (header()->width() - width));
// Shouldn't be necessary due to use of header()->resizeSection, but
// lets just do it anyway for the sake of completeness
resetFixedColumns();
fillRemainingWidth(false);
emit viewStateChanged();
}
@ -435,3 +409,46 @@ void EntryView::resetViewToDefaults()
fitColumnsToWindow();
}
void EntryView::fillRemainingWidth(bool lastColumnOnly)
{
// Determine total width of currently visible columns
int width = 0;
int lastColumnIndex = 0;
for (int columnIndex = 0; columnIndex < header()->count(); ++columnIndex) {
if (!header()->isSectionHidden(columnIndex)) {
width += header()->sectionSize(columnIndex);
}
if (header()->visualIndex(columnIndex) > lastColumnIndex) {
lastColumnIndex = header()->visualIndex(columnIndex);
}
}
int numColumns = header()->count() - header()->hiddenSectionCount();
int availWidth = header()->width() - width;
if ((numColumns <= 0) || (availWidth <= 0)) {
return;
}
if (!lastColumnOnly) {
// Equally distribute remaining width to visible columns
int add = availWidth / numColumns;
width = 0;
for (int columnIndex = 0; columnIndex < header()->count(); ++columnIndex) {
if (!header()->isSectionHidden(columnIndex)) {
header()->resizeSection(columnIndex, header()->sectionSize(columnIndex) + add);
width += header()->sectionSize(columnIndex);
}
}
}
// Add remaining width to last column
header()->resizeSection(header()->logicalIndex(lastColumnIndex), header()->sectionSize(lastColumnIndex) + (header()->width() - width));
}
void EntryView::resetFixedColumns()
{
header()->setSectionResizeMode(EntryModel::Paperclip, QHeaderView::Fixed);
header()->resizeSection(EntryModel::Paperclip, header()->minimumSectionSize());
}

View File

@ -48,7 +48,7 @@ public:
bool isPasswordsHidden() const;
void setPasswordsHidden(const bool hide);
QByteArray viewState() const;
bool setViewState(const QByteArray& state) const;
bool setViewState(const QByteArray& state);
public slots:
void setGroup(Group* group);
@ -74,6 +74,9 @@ private slots:
void resetViewToDefaults();
private:
void fillRemainingWidth(bool lastColumnOnly);
void resetFixedColumns();
EntryModel* const m_model;
SortFilterHideProxyModel* const m_sortModel;
bool m_inSearchMode;