diff options
author | Hannah von Reth <hannah.vonreth@owncloud.com> | 2021-05-11 12:18:28 +0300 |
---|---|---|
committer | Hannah von Reth <vonreth@kde.org> | 2021-05-26 18:12:30 +0300 |
commit | 78a4fddc8b647f7bea8671a51c3997e9777b52ed (patch) | |
tree | 5d30fd1a16850d86346b25a792272eeefad0b763 /src | |
parent | 286d6229a78067b4a4b14142d0be287037a934e7 (diff) |
Add rtl support to copy to clipboard
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/activitywidget.cpp | 2 | ||||
-rw-r--r-- | src/gui/models/models.cpp | 46 | ||||
-rw-r--r-- | src/gui/models/models.h | 2 |
3 files changed, 38 insertions, 12 deletions
diff --git a/src/gui/activitywidget.cpp b/src/gui/activitywidget.cpp index 466f3a4d5..dfbac6db2 100644 --- a/src/gui/activitywidget.cpp +++ b/src/gui/activitywidget.cpp @@ -453,7 +453,7 @@ void ActivityWidget::slotItemContextMenu() menu->setAttribute(Qt::WA_DeleteOnClose); // keep in sync with ProtocolWidget::showContextMenu - menu->addAction(tr("Copy to clipboard"), this, [text = Models::formatSelection(rows)] { + menu->addAction(tr("Copy to clipboard"), this, [text = Models::formatSelection(rows, Models::UnderlyingDataRole)] { QApplication::clipboard()->setText(text); }); diff --git a/src/gui/models/models.cpp b/src/gui/models/models.cpp index 1e62eed1e..e32d28594 100644 --- a/src/gui/models/models.cpp +++ b/src/gui/models/models.cpp @@ -19,7 +19,9 @@ #include <QSortFilterProxyModel> #include <QMenu> -QString OCC::Models::formatSelection(const QModelIndexList &items) +#include <functional> + +QString OCC::Models::formatSelection(const QModelIndexList &items, int dataRole) { if (items.isEmpty()) { return {}; @@ -28,23 +30,47 @@ QString OCC::Models::formatSelection(const QModelIndexList &items) QString out; QTextStream stream(&out); - for (int c = 0; c < columns; ++c) { + QString begin; + QString end; + + const auto iterate = [columns](const std::function<void(int)> &f) { + if (qApp->layoutDirection() != Qt::RightToLeft) { + for (int c = 0; c < columns; ++c) { + f(c); + } + } else { + for (int c = columns - 1; c >= 0; --c) { + f(c); + } + } + }; + if (qApp->layoutDirection() == Qt::RightToLeft) { + stream << right; + begin = QLatin1Char(','); + } else { + stream << left; + end = QLatin1Char(','); + } + + iterate([&](int c) { const auto width = items.first().model()->headerData(c, Qt::Horizontal, StringFormatWidthRole).toInt(); Q_ASSERT(width); - stream << right + stream << begin << qSetFieldWidth(width) << items.first().model()->headerData(c, Qt::Horizontal).toString() - << qSetFieldWidth(0) << ","; - } + << qSetFieldWidth(0) + << end; + }); stream << endl; for (const auto &index : items) { - for (int c = 0; c < columns; ++c) { + iterate([&](int c) { const auto &child = index.siblingAtColumn(c); - stream << right + stream << begin << qSetFieldWidth(child.model()->headerData(c, Qt::Horizontal, StringFormatWidthRole).toInt()) - << child.data(Qt::DisplayRole).toString() - << qSetFieldWidth(0) << ","; - } + << child.data(dataRole).toString() + << qSetFieldWidth(0) + << end; + }); stream << endl; } return out; diff --git a/src/gui/models/models.h b/src/gui/models/models.h index fcb6b3664..035d2fdfe 100644 --- a/src/gui/models/models.h +++ b/src/gui/models/models.h @@ -30,7 +30,7 @@ namespace Models { /** * Returns a cvs representation of a table */ - QString formatSelection(const QModelIndexList &items); + QString formatSelection(const QModelIndexList &items, int dataRole = Qt::DisplayRole); void displayFilterDialog(const QStringList &candidates, QSortFilterProxyModel *model, int column, int role, QWidget *parent = nullptr); |