Significantly improve visual when dragging entries to copy/move

* Fixes #6079
This commit is contained in:
Jonathan White 2023-07-04 07:15:39 -04:00
parent 5b47190fcc
commit 6fb498648d
No known key found for this signature in database
GPG Key ID: 440FC65F2E0C6E01
3 changed files with 82 additions and 0 deletions

View File

@ -3997,6 +3997,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>

View File

@ -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
@ -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;

View File

@ -61,6 +61,7 @@ protected:
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);