#include "editprofile.h" #include "ui_editprofile.h" #include "mainwindow.h" editProfile::editProfile(QWidget *parent) : QDialog(parent), ui(new Ui::editProfile) { ui->setupUi(this); // Define the filler line vectors and graphs so they don't need to be recreated every update leftLineX.append(x_lower); leftLineX.append(0); leftLineY.append(0); leftLineY.append(0); rightLineX.append(0); rightLineX.append(x_upper); rightLineY.append(0); rightLineY.append(0); ui->curvePlot->addGraph(); ui->curvePlot->addGraph(); ui->curvePlot->addGraph(); ui->curvePlot->graph(0)->setScatterStyle(QCPScatterStyle::ssCircle); ui->curvePlot->graph(0)->setLineStyle(QCPGraph::lsLine); //ui->curvePlot->setInteractions(QCP::iSelectItems | QCP::iSelectPlottables); //ui->curvePlot->graph(0)->setSelectable(QCP::SelectionType::stSingleData); ui->curvePlot->xAxis->setLabel("Temperature"); ui->curvePlot->yAxis->setLabel("Fan speed (%)"); ui->curvePlot->xAxis->setRange(x_lower, (x_upper+5)); ui->curvePlot->yAxis->setRange(y_lower, (y_upper+5)); //connect(ui->curvePlot, SIGNAL(on_clickedGraph(QCPAbstractPlottable *plottable, int dataIndex, QMouseEvent*)), SLOT(getDataIndex(QCPAbstractPlottable *plottable, int dataIndex))); connect(ui->curvePlot, SIGNAL(mouseDoubleClick(QMouseEvent*)), SLOT(clickedGraph(QMouseEvent*))); //connect(ui->curvePlot, SIGNAL(mouseMove(QMouseEvent*)), SLOT(getPixelLength(QMouseEvent*))); connect(ui->curvePlot, SIGNAL(plottableClick(QCPAbstractPlottable*,int,QMouseEvent*)), SLOT(clickedPoint(QCPAbstractPlottable*,int,QMouseEvent*))); connect(ui->curvePlot, SIGNAL(mousePress(QMouseEvent*)), SLOT(detectPress(QMouseEvent*))); connect(ui->curvePlot, SIGNAL(mouseMove(QMouseEvent*)), SLOT(detectMove(QMouseEvent*))); connect(ui->curvePlot, SIGNAL(mouseRelease(QMouseEvent*)), SLOT(detectRelease(QMouseEvent*))); connect(ui->curvePlot, SIGNAL(mouseMove(QMouseEvent*)), SLOT(getYcoordValue(QMouseEvent*))); } editProfile::~editProfile() { delete ui; } void editProfile::addPoint(double x, double y) { y = round(y); x = round(x); checkForDuplicatePoint(x, y); if ((x_lower<=x) && (x<=x_upper) && (y_lower<=y) && (y<=y_upper) && !duplicatePoint) { qv_x.append(x); qv_y.append(y); } } void editProfile::rePlot() { ui->curvePlot->replot(); ui->curvePlot->update(); } void editProfile::clickedGraph(QMouseEvent *event) { QPoint point = event->pos(); addPoint(ui->curvePlot->xAxis->pixelToCoord(point.x()), ui->curvePlot->yAxis->pixelToCoord(point.y())); ui->curvePlot->graph(0)->setData(qv_x, qv_y); drawFillerLines(); } void editProfile::clickedPoint(QCPAbstractPlottable *plottable, int dataIndex, QMouseEvent *event) { checkForNearbyPoints(event); //double ycoord = ui->curvePlot->graph(0)->dataMainValue(dataIndex); //double xcoord = ui->curvePlot->graph(0)->dataSortKey(dataIndex); ycoord = round(ycoord); xcoord = round(xcoord); if (isNearbyPoint) { for (int i=0; icurvePlot->graph(0)->setData(qv_x, qv_y); rePlot(); drawFillerLines(); } } bool editProfile::checkForNearbyPoints(QMouseEvent *event) { QPoint point = event->pos(); double pointerxcoord = ui->curvePlot->xAxis->pixelToCoord(point.x()); double pointerycoord = ui->curvePlot->yAxis->pixelToCoord(point.y()); for (int i=0; ipos(); double pointerycoord = ui->curvePlot->yAxis->pixelToCoord(point.y()); double pointerxcoord = ui->curvePlot->xAxis->pixelToCoord(point.x()); for (int i=0; i 0) { for (int i=0; icurvePlot->clearItems(); drawCoordtext(index_x, index_y); QPoint point = event->pos(); qv_y[index_y] = round(ui->curvePlot->yAxis->pixelToCoord(point.y())); qv_x[index_x] = round(ui->curvePlot->xAxis->pixelToCoord(point.x())); if (qv_x[index_x] > x_upper) { qv_x[index_x] = x_upper; } if (qv_x[index_x] < x_lower) { qv_x[index_x] = x_lower; } if (qv_y[index_y] > y_upper) { qv_y[index_y] = y_upper; } if (qv_y[index_y] < y_lower) { qv_y[index_y] = y_lower; } ui->curvePlot->graph(0)->setData(qv_x, qv_y); drawFillerLines(); } void editProfile::drawCoordtext(int index_x, int index_y) { QCPItemText *coordText = new QCPItemText(ui->curvePlot); if (draggingPoint) { if (qv_x[index_x] > x_upper) { qv_x[index_x] = x_upper; } if (qv_x[index_x] < x_lower) { qv_x[index_x] = x_lower; } if (qv_y[index_y] > y_upper) { qv_y[index_y] = y_upper; } if (qv_y[index_y] < y_lower) { qv_y[index_y] = y_lower; } coordText->position->setType(QCPItemPosition::ptPlotCoords); coordText->position->setCoords(qv_x[index_x], qv_y[index_y] + 4); QString xString = QString::number(qv_x[index_x]); QString yString = QString::number(qv_y[index_y]); coordText->setText(xString + ", " + yString); } if (!mouseDragging) { ui->curvePlot->clearItems(); } } void editProfile::drawFillerLines() { // Draw the filler lines separately so they don't interfere with the main data. graph(1) = leftward line, graph(2) = rightward line leftLineX[1] = ui->curvePlot->graph(0)->dataSortKey(0); leftLineY[0] = ui->curvePlot->graph(0)->dataMainValue(0); leftLineY[1] = ui->curvePlot->graph(0)->dataMainValue(0); ui->curvePlot->graph(1)->setData(leftLineX, leftLineY); rightLineX[0] = ui->curvePlot->graph(0)->dataSortKey(qv_x.length() -1); rightLineY[0] = ui->curvePlot->graph(0)->dataMainValue(qv_x.length() -1); rightLineY[1] = ui->curvePlot->graph(0)->dataMainValue(qv_x.length() -1); ui->curvePlot->graph(2)->setData(rightLineX, rightLineY); rePlot(); } bool editProfile::detectRelease(QMouseEvent *event) { mousePressed = false; resetMouseMove(); resetMouseDragging(); draggedIndicesUnset(); draggingPointUnset(); drawCoordtext(index_x, index_y); return mousePressed; } bool editProfile::draggingPointUnset() { draggingPoint = false; return draggingPoint; } bool editProfile::draggingPointSet() { draggingPoint = true; return draggingPoint; } bool editProfile::draggedIndicesSet() { indicesSet = true; return indicesSet; } bool editProfile::draggedIndicesUnset() { indicesSet = false; return indicesSet; } bool editProfile::resetMouseMove() { mouseMoving = false; return mouseMoving; } bool editProfile::resetMouseDragging() { mouseDragging = false; return mouseDragging; } double editProfile::getPixelLength(QMouseEvent *event) { QPoint point = event->pos(); qDebug() << ui->curvePlot->graph(0)->selectTest(point, 2); qDebug() << pixelLength; return pixelLength; } void editProfile::on_pushButton_clicked() { MainWindow mw; ui->curvePlot->clearItems(); qDebug() << mw.voltInt; } void editProfile::on_saveButton_clicked() { /*QFile file("C:/Users/Gnometech/Documents/rojekti/test.xml"); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { document.setContent(&file); file.close(); // Remove existing xpoints and ypoints attributes QDomElement root = document.firstChildElement(); QDomNodeList ypoints =root.elementsByTagName("ypoints"); QDomNode ypntnode = ypoints.item(0); QDomNodeList xpoints =root.elementsByTagName("xpoints"); QDomNode xpntnode = xpoints.item(0); QString xString; QString yString; // Add the curve points to strings for (int i=0; icurvePlot->graph(0)->dataSortKey(i)); QString y = QString::number(ui->curvePlot->graph(0)->dataMainValue(i)); xString.append(x + ", "); yString.append(y + ", "); if (ypntnode.isElement()) { QDomElement elem = ypntnode.toElement(); if (elem.hasAttribute("ypoints")) { elem.removeAttribute("ypoints"); elem.setAttribute("ypoints", yString); } } if (xpntnode.isElement()) { QDomElement elem = xpntnode.toElement(); elem.removeAttribute("xpoints"); elem.setAttribute("xpoints", xString); } } if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream(&file); stream << document.toString(); file.close(); qDebug() << "valmis"; } } */ QString xString; QString yString; for (int i=0; icurvePlot->graph(0)->dataSortKey(i)); QString y = QString::number(ui->curvePlot->graph(0)->dataMainValue(i)); xString.append(x + ", "); yString.append(y + ", "); } QVariant xarray = xString; QVariant yarray = yString; qDebug() << xarray.toString() << yarray.toString(); QSettings settings("nvfancurve"); settings.setValue("profile/curveXpoints", xarray); settings.setValue("profile/curveYpoints", yarray); } void editProfile::on_clearButton_clicked() { MainWindow mw; qv_x.clear(); qv_y.clear(); qDebug() << mw.currentProfile; ui->curvePlot->graph(0)->setData(qv_x, qv_y); drawFillerLines(); }