mirror of
https://github.com/Lurkki14/tuxclocker.git
synced 2025-02-25 18:55:24 -06:00
qt: add QByteArray conversion for drag and drop
This commit is contained in:
parent
b678c179b4
commit
05da5ee15d
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <QStandardItemModel>
|
#include <QStandardItemModel>
|
||||||
#include <QMimeData>
|
#include <QMimeData>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
#include <ReadableData.h>
|
#include <ReadableData.h>
|
||||||
|
|
||||||
@ -11,20 +12,16 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
QStringList mimeTypes() const override {return QStringList(ReadableData::mimeType());}
|
QStringList mimeTypes() const override {return QStringList(ReadableData::mimeType());}
|
||||||
QMimeData *mimeData(const QModelIndexList &indices) const override {
|
QMimeData *mimeData(const QModelIndexList &indices) const override {
|
||||||
|
// FIXME: Pretty messy to have this here
|
||||||
|
qRegisterMetaTypeStreamOperators<ReadableData>("ReadableData");
|
||||||
|
|
||||||
if (indices.size() < 1) {
|
if (indices.size() < 1) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantList itemDataList;
|
|
||||||
|
|
||||||
for (QModelIndex modelIndex : indices) {
|
|
||||||
itemDataList.append(itemFromIndex(modelIndex)->data(Qt::UserRole));
|
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray b_data;
|
QByteArray b_data;
|
||||||
QDataStream stream(&b_data, QIODevice::WriteOnly);
|
QDataStream stream(&b_data, QIODevice::WriteOnly);
|
||||||
|
|
||||||
stream << itemDataList;
|
stream << itemFromIndex(indices[0])->data(Qt::UserRole);
|
||||||
|
|
||||||
QMimeData *data = new QMimeData;
|
QMimeData *data = new QMimeData;
|
||||||
data->setData(ReadableData::mimeType(), b_data);
|
data->setData(ReadableData::mimeType(), b_data);
|
||||||
|
@ -1,16 +1,86 @@
|
|||||||
#include "ReadableGraphDisplay.h"
|
#include "ReadableGraphDisplay.h"
|
||||||
|
|
||||||
ReadableGraphDisplay::ReadableGraphDisplay(QWidget *parent) : QGraphicsView(new QGraphicsScene, parent) {
|
#include <ReadableData.h>
|
||||||
m_chart = new QChart;
|
#include <ReadableMasterObservable.h>
|
||||||
m_chart->createDefaultAxes();
|
|
||||||
|
#include <QDragEnterEvent>
|
||||||
|
#include <QMimeData>
|
||||||
|
|
||||||
|
ReadableGraphDisplay::ReadableGraphDisplay(QWidget *parent) : QChartView(parent) {
|
||||||
|
// FIXME: drops are only accepted only on a small area on the widget
|
||||||
|
setAcceptDrops(true);
|
||||||
|
setRenderHint(QPainter::Antialiasing);
|
||||||
|
|
||||||
scene()->addItem(m_chart);
|
m_chart = new QChart;
|
||||||
|
|
||||||
|
setChart(m_chart);
|
||||||
|
|
||||||
|
m_chart->addSeries(&series);
|
||||||
|
|
||||||
|
m_chart->addAxis(&xAxis, Qt::AlignBottom);
|
||||||
|
m_chart->addAxis(&yAxis, Qt::AlignLeft);
|
||||||
|
|
||||||
|
series.attachAxis(&xAxis);
|
||||||
|
series.attachAxis(&yAxis);
|
||||||
|
|
||||||
|
yAxis.setRange(0, 70000);
|
||||||
|
xAxis.setRange(0, 20);
|
||||||
|
|
||||||
|
x = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ReadableGraphDisplay::event(QEvent *event) {
|
bool ReadableGraphDisplay::event(QEvent *event) {
|
||||||
if (event->type() == QEvent::Leave) {
|
if (event->type() == QEvent::Leave) {
|
||||||
qDebug() << "leave";
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return QAbstractScrollArea::event(event);
|
return QAbstractScrollArea::event(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ReadableGraphDisplay::dragEnterEvent(QDragEnterEvent *event) {
|
||||||
|
qDebug() << event->mimeData()->formats();
|
||||||
|
|
||||||
|
if (event->mimeData()->hasFormat(ReadableData::mimeType())) {
|
||||||
|
event->accept();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReadableGraphDisplay::dropEvent(QDropEvent *event) {
|
||||||
|
//QVariantList itemDataList;
|
||||||
|
QByteArray b_data = event->mimeData()->data(ReadableData::mimeType());
|
||||||
|
|
||||||
|
qDebug() << b_data;
|
||||||
|
|
||||||
|
// Convert mime data to QVariantList
|
||||||
|
|
||||||
|
QDataStream stream(&b_data, QIODevice::ReadOnly);
|
||||||
|
|
||||||
|
QVariant v;
|
||||||
|
stream >> v;
|
||||||
|
|
||||||
|
qDebug() << v;
|
||||||
|
|
||||||
|
if (!v.canConvert<ReadableData>()) {
|
||||||
|
qDebug() << "can't convert";
|
||||||
|
}
|
||||||
|
|
||||||
|
ReadableData d = qvariant_cast<ReadableData>(v);
|
||||||
|
|
||||||
|
qDebug() << d.value().data.uint_value;
|
||||||
|
|
||||||
|
/* if (itemDataList.size() < 1) {
|
||||||
|
return;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
ReadableMasterObservable *mo = new ReadableMasterObservable(&d);
|
||||||
|
|
||||||
|
connect(mo->createObservable(std::chrono::milliseconds(2000)), &ReadableObservable::valueUpdated, [=](tc_readable_result_t res) {
|
||||||
|
if (!res.valid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
qDebug() << res.data.uint_value << res.valid;
|
||||||
|
|
||||||
|
x++;
|
||||||
|
series.append(x, res.data.uint_value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -1,17 +1,35 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QGraphicsView>
|
#include <QChartView>
|
||||||
#include <QGraphicsItem>
|
#include <QGraphicsItem>
|
||||||
#include <QtCharts/QChart>
|
#include <QtCharts/QChart>
|
||||||
|
#include <QLineSeries>
|
||||||
|
#include <QCategoryAxis>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
|
// Graph view for updating readables. QVariantLists convertible to ReadableData can be dropped on this widget.
|
||||||
|
|
||||||
using namespace QtCharts;
|
using namespace QtCharts;
|
||||||
|
|
||||||
class ReadableGraphDisplay : public QGraphicsView {
|
class ReadableGraphDisplay : public QChartView {
|
||||||
public:
|
public:
|
||||||
ReadableGraphDisplay(QWidget *parent = nullptr);
|
ReadableGraphDisplay(QWidget *parent = nullptr);
|
||||||
protected:
|
protected:
|
||||||
bool event(QEvent *event);
|
bool event(QEvent *event);
|
||||||
|
|
||||||
|
void resizeEvent(QResizeEvent *event) {
|
||||||
|
fitInView(sceneRect());
|
||||||
|
}
|
||||||
|
void dragEnterEvent(QDragEnterEvent *event) override;
|
||||||
|
void dropEvent(QDropEvent *event) override;
|
||||||
private:
|
private:
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
QChart *m_chart;
|
QChart *m_chart;
|
||||||
|
|
||||||
|
QValueAxis xAxis, yAxis;
|
||||||
|
|
||||||
|
int x;
|
||||||
|
|
||||||
|
QLineSeries series;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user