diff --git a/src/gui/entry/EntryModel.cpp b/src/gui/entry/EntryModel.cpp index afd25aa1a..6f7ebf6af 100644 --- a/src/gui/entry/EntryModel.cpp +++ b/src/gui/entry/EntryModel.cpp @@ -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); } diff --git a/src/gui/entry/EntryModel.h b/src/gui/entry/EntryModel.h index b436c6a9e..4fc765044 100644 --- a/src/gui/entry/EntryModel.h +++ b/src/gui/entry/EntryModel.h @@ -99,7 +99,6 @@ private: bool m_hidePasswords; QPixmap m_paperClipPixmap; - QPixmap m_paperClipPixmapCentered; static const QString HiddenContentDisplay; static const Qt::DateFormat DateFormat; diff --git a/src/gui/entry/EntryView.cpp b/src/gui/entry/EntryView.cpp index f39dd218b..19978a808 100644 --- a/src/gui/entry/EntryView.cpp +++ b/src/gui/entry/EntryView.cpp @@ -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()); +} + diff --git a/src/gui/entry/EntryView.h b/src/gui/entry/EntryView.h index 26672a665..a8422c563 100644 --- a/src/gui/entry/EntryView.h +++ b/src/gui/entry/EntryView.h @@ -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;