Move copy support for Logs Window to Logs Model

The model is the correct place to determine what data gets copied. This
also paves way to support other models where the data to be copied is
not plain text
This commit is contained in:
Srivats P 2020-03-05 12:43:37 +05:30
parent 88ddb97a52
commit fd6f2c2508
3 changed files with 31 additions and 13 deletions

View File

@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "logsmodel.h"
#include <QBrush>
#include <QMimeData>
// XXX: Keep the enum in sync with it's string
enum {
@ -108,6 +109,31 @@ QVariant LogsModel::data(const QModelIndex &index, int role) const
return QVariant();
}
QStringList LogsModel::mimeTypes() const
{
return QStringList() << "text/plain";
}
QMimeData* LogsModel::mimeData(const QModelIndexList &indexes) const
{
int lastRow = -1;
QString text;
foreach(QModelIndex index, indexes) {
if (index.row() != lastRow) {
if (!text.isEmpty())
text.append("\n");
}
else
text.append("\t");
text.append(data(index).toString());
lastRow = index.row();
}
QMimeData *mimeData = new QMimeData();
mimeData->setText(text);
return mimeData; // caller is responsible for freeing!
}
// --------------------------------------------- //
// Slots
// --------------------------------------------- //

View File

@ -43,6 +43,8 @@ public:
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QStringList mimeTypes() const;
QMimeData* mimeData(const QModelIndexList &indexes) const;
public slots:
void clear();

View File

@ -48,23 +48,13 @@ protected:
virtual void keyPressEvent(QKeyEvent *event)
{
// Copy selection to clipboard (base class copies only current item)
// Selection, by default, is in the order in which items were selected
// - sort them before copying
if (event->matches(QKeySequence::Copy)
&& selectionBehavior() == SelectRows) {
QString text;
int lastRow = -1;
QModelIndexList selected = selectionModel()->selectedIndexes();
std::sort(selected.begin(), selected.end());
foreach(QModelIndex index, selected) {
if (index.row() != lastRow) {
if (!text.isEmpty())
text.append("\n");
}
else
text.append("\t");
text.append(model()->data(index).toString());
lastRow = index.row();
}
qApp->clipboard()->setText(text);
qApp->clipboard()->setMimeData(model()->mimeData(selected));
}
else
QTableView::keyPressEvent(event);