Skip to content

Commit

Permalink
Significantly improve visual when dragging entries to copy/move
Browse files Browse the repository at this point in the history
* Fixes #6079
  • Loading branch information
droidmonkey committed Aug 6, 2023
1 parent 0a75858 commit 15b8b5d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
7 changes: 7 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3965,6 +3965,13 @@ Error: %1</source>
<source>Reset to defaults</source>
<translation type="unfinished"></translation>
</message>
<message numerus="yes">
<source>+ %1 entry(s)...</source>
<translation type="unfinished">
<numerusform></numerusform>
<numerusform></numerusform>
</translation>
</message>
</context>
<context>
<name>ExportDialog</name>
Expand Down
74 changes: 74 additions & 0 deletions src/gui/entry/EntryView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@
#include "EntryView.h"

#include <QAccessible>
#include <QDrag>
#include <QGuiApplication>
#include <QHeaderView>
#include <QListWidget>
#include <QMenu>
#include <QPainter>
#include <QScreen>
#include <QShortcut>
#include <QStyledItemDelegate>
#include <QWindow>

#include "gui/Icons.h"
#include "gui/SortFilterHideProxyModel.h"

#define ICON_ONLY_SECTION_SIZE 26
Expand Down Expand Up @@ -507,6 +513,74 @@ void EntryView::showEvent(QShowEvent* event)
}
}

void EntryView::startDrag(Qt::DropActions supportedActions)
{
auto selectedIndexes = selectionModel()->selectedRows(EntryModel::Title);
if (selectedIndexes.isEmpty()) {
return;
}

// Create a mime data object for the selected rows
auto mimeData = m_sortModel->mimeData(selectedIndexes);
if (!mimeData) {
return;
}

// Create a temporary list widget to display the dragged items
int i = 0;
QListWidget listWidget;
for (auto& index : selectedIndexes) {
if (++i > 4) {
int remaining = selectedIndexes.size() - i + 1;
listWidget.addItem(tr("+ %1 entry(s)...", nullptr, remaining).arg(remaining));
break;
}

QIcon icon;
icon.addPixmap(m_sortModel->data(index, Qt::DecorationRole).value<QPixmap>());

auto item = new QListWidgetItem;
item->setText(m_sortModel->data(index, Qt::DisplayRole).toString());
item->setIcon(icon);
listWidget.addItem(item);
}

listWidget.setStyleSheet("QListWidget { background-color: palette(highlight); border: 1px solid palette(dark); "
"padding: 4px; color: palette(highlighted-text); }");
auto width = listWidget.sizeHintForColumn(0) + 2 * listWidget.frameWidth();
auto height = listWidget.sizeHintForRow(0) * listWidget.count() + 2 * listWidget.frameWidth();
listWidget.setFixedWidth(width);
listWidget.setFixedHeight(height);

// Grab the screen pixel ratio where the window resides
// TODO: Use direct call to screen() when moving to Qt 6
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
auto screen = QGuiApplication::screenAt(window()->geometry().center());
if (!screen) {
screen = QGuiApplication::primaryScreen();
}
#else
auto screen = QGuiApplication::primaryScreen();
if (windowHandle()) {
screen = windowHandle()->screen();
}
#endif

auto pixelRatio = screen->devicePixelRatio();

// Render the list widget to a pixmap
QPixmap pixmap(QSize(width, height) * pixelRatio);
pixmap.fill(Qt::transparent);
pixmap.setDevicePixelRatio(pixelRatio);
listWidget.render(&pixmap);

// Create a drag object and start the drag
auto drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->exec(supportedActions, defaultDropAction());
}

bool EntryView::isColumnHidden(int logicalIndex)
{
return header()->isSectionHidden(logicalIndex) || header()->sectionSize(logicalIndex) == 0;
Expand Down
1 change: 1 addition & 0 deletions src/gui/entry/EntryView.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class EntryView : public QTreeView
void keyPressEvent(QKeyEvent* event) override;
void focusInEvent(QFocusEvent* event) override;
void showEvent(QShowEvent* event) override;
void startDrag(Qt::DropActions supportedActions) override;

private slots:
void emitEntryActivated(const QModelIndex& index);
Expand Down

0 comments on commit 15b8b5d

Please sign in to comment.