Esempio n. 1
0
QSize MultiLayer::arrangeLayers(bool userSize) {
  const QRect rect = canvas->geometry();

  gsl_vector *xTopR = gsl_vector_calloc(
      graphs);  // ratio between top axis + title and canvas height
  gsl_vector *xBottomR =
      gsl_vector_calloc(graphs);  // ratio between bottom axis and canvas height
  gsl_vector *yLeftR = gsl_vector_calloc(graphs);
  gsl_vector *yRightR = gsl_vector_calloc(graphs);
  gsl_vector *maxXTopHeight =
      gsl_vector_calloc(rows);  // maximum top axis + title height in a row
  gsl_vector *maxXBottomHeight =
      gsl_vector_calloc(rows);  // maximum bottom axis height in a row
  gsl_vector *maxYLeftWidth =
      gsl_vector_calloc(cols);  // maximum left axis width in a column
  gsl_vector *maxYRightWidth =
      gsl_vector_calloc(cols);  // maximum right axis width in a column
  gsl_vector *Y = gsl_vector_calloc(rows);
  gsl_vector *X = gsl_vector_calloc(cols);

  int i;
  for (i = 0; i < graphs;
       i++) {  // calculate scales/canvas dimensions reports for each layer and
               // stores them in the above vectors
    Graph *gr = (Graph *)graphsList.at(i);
    QwtPlot *plot = gr->plotWidget();
    QwtPlotLayout *plotLayout = plot->plotLayout();
    QRect cRect = plotLayout->canvasRect();
    double ch = (double)cRect.height();
    double cw = (double)cRect.width();

    QRect tRect = plotLayout->titleRect();
    QwtScaleWidget *scale = (QwtScaleWidget *)plot->axisWidget(QwtPlot::xTop);

    int topHeight = 0;
    if (!tRect.isNull()) topHeight += tRect.height() + plotLayout->spacing();
    if (scale) {
      QRect sRect = plotLayout->scaleRect(QwtPlot::xTop);
      topHeight += sRect.height();
    }
    gsl_vector_set(xTopR, i, double(topHeight) / ch);

    scale = (QwtScaleWidget *)plot->axisWidget(QwtPlot::xBottom);
    if (scale) {
      QRect sRect = plotLayout->scaleRect(QwtPlot::xBottom);
      gsl_vector_set(xBottomR, i, double(sRect.height()) / ch);
    }

    scale = (QwtScaleWidget *)plot->axisWidget(QwtPlot::yLeft);
    if (scale) {
      QRect sRect = plotLayout->scaleRect(QwtPlot::yLeft);
      gsl_vector_set(yLeftR, i, double(sRect.width()) / cw);
    }

    scale = (QwtScaleWidget *)plot->axisWidget(QwtPlot::yRight);
    if (scale) {
      QRect sRect = plotLayout->scaleRect(QwtPlot::yRight);
      gsl_vector_set(yRightR, i, double(sRect.width()) / cw);
    }

    // calculate max scales/canvas dimensions ratio for each line and column and
    // stores them to vectors
    int row = i / cols;
    if (row >= rows) row = rows - 1;

    int col = i % cols;

    double aux = gsl_vector_get(xTopR, i);
    double old_max = gsl_vector_get(maxXTopHeight, row);
    if (aux >= old_max) gsl_vector_set(maxXTopHeight, row, aux);

    aux = gsl_vector_get(xBottomR, i);
    if (aux >= gsl_vector_get(maxXBottomHeight, row))
      gsl_vector_set(maxXBottomHeight, row, aux);

    aux = gsl_vector_get(yLeftR, i);
    if (aux >= gsl_vector_get(maxYLeftWidth, col))
      gsl_vector_set(maxYLeftWidth, col, aux);

    aux = gsl_vector_get(yRightR, i);
    if (aux >= gsl_vector_get(maxYRightWidth, col))
      gsl_vector_set(maxYRightWidth, col, aux);
  }

  double c_heights = 0.0;
  for (i = 0; i < rows; i++) {
    gsl_vector_set(Y, i, c_heights);
    c_heights += 1 + gsl_vector_get(maxXTopHeight, i) +
                 gsl_vector_get(maxXBottomHeight, i);
  }

  double c_widths = 0.0;
  for (i = 0; i < cols; i++) {
    gsl_vector_set(X, i, c_widths);
    c_widths += 1 + gsl_vector_get(maxYLeftWidth, i) +
                gsl_vector_get(maxYRightWidth, i);
  }

  if (!userSize) {
    l_canvas_width = int(
        (rect.width() - (cols - 1) * colsSpace - right_margin - left_margin) /
        c_widths);
    l_canvas_height = int(
        (rect.height() - (rows - 1) * rowsSpace - top_margin - bottom_margin) /
        c_heights);
  }

  QSize size = QSize(l_canvas_width, l_canvas_height);

  for (i = 0; i < graphs; i++) {
    int row = i / cols;
    if (row >= rows) row = rows - 1;

    int col = i % cols;

    // calculate sizes and positions for layers
    const int w = int(l_canvas_width * (1 + gsl_vector_get(yLeftR, i) +
                                        gsl_vector_get(yRightR, i)));
    const int h = int(l_canvas_height * (1 + gsl_vector_get(xTopR, i) +
                                         gsl_vector_get(xBottomR, i)));

    int x = left_margin + col * colsSpace;
    if (hor_align == HCenter)
      x += int(l_canvas_width *
               (gsl_vector_get(X, col) + gsl_vector_get(maxYLeftWidth, col) -
                gsl_vector_get(yLeftR, i)));
    else if (hor_align == Left)
      x += int(l_canvas_width * gsl_vector_get(X, col));
    else if (hor_align == Right)
      x +=
          int(l_canvas_width *
              (gsl_vector_get(X, col) + gsl_vector_get(maxYLeftWidth, col) -
               gsl_vector_get(yLeftR, i) + gsl_vector_get(maxYRightWidth, col) -
               gsl_vector_get(yRightR, i)));

    int y = top_margin + row * rowsSpace;
    if (vert_align == VCenter)
      y += int(l_canvas_height *
               (gsl_vector_get(Y, row) + gsl_vector_get(maxXTopHeight, row) -
                gsl_vector_get(xTopR, i)));
    else if (vert_align == Top)
      y += int(l_canvas_height * gsl_vector_get(Y, row));
    else if (vert_align == Bottom)
      y += int(l_canvas_height *
               (gsl_vector_get(Y, row) + gsl_vector_get(maxXTopHeight, row) -
                gsl_vector_get(xTopR, i) +
                +gsl_vector_get(maxXBottomHeight, row) -
                gsl_vector_get(xBottomR, i)));

    // resizes and moves layers
    Graph *gr = (Graph *)graphsList.at(i);
    bool autoscaleFonts = false;
    if (!userSize) {  // When the user specifies the layer canvas size, the
                      // window is resized
      // and the fonts must be scaled accordingly. If the size is calculated
      // automatically we don't rescale the fonts in order to prevent problems
      // with too small fonts when the user adds new layers or when removing
      // layers

      autoscaleFonts = gr->autoscaleFonts();  // save user settings
      gr->setAutoscaleFonts(false);
    }

    gr->setGeometry(QRect(x, y, w, h));
    gr->plotWidget()->resize(QSize(w, h));

    if (!userSize)
      gr->setAutoscaleFonts(autoscaleFonts);  // restore user settings
  }

  // free memory
  gsl_vector_free(maxXTopHeight);
  gsl_vector_free(maxXBottomHeight);
  gsl_vector_free(maxYLeftWidth);
  gsl_vector_free(maxYRightWidth);
  gsl_vector_free(xTopR);
  gsl_vector_free(xBottomR);
  gsl_vector_free(yLeftR);
  gsl_vector_free(yRightR);
  gsl_vector_free(X);
  gsl_vector_free(Y);
  return size;
}
Esempio n. 2
0
int main(int argc, char *argv[])
{

    //  QGuiApplication a(argc, argv);
    QApplication a(argc, argv);
    QwtPlot *plot = new QwtPlot();
    QwtPlotCanvas *canvas = new QwtPlotCanvas();
    canvas->setBorderRadius(10);

    plot->setCanvas(canvas);
    plot->setCanvasBackground(QColor("LIGHTGRAY"));

    plot->enableAxis(QwtPlot::yRight);
    plot->enableAxis(QwtPlot::xTop);
    plot->setAxisTitle(QwtPlot::xBottom, "Xline");
    plot->setAxisTitle(QwtPlot::xTop, "Xline");
    plot->setAxisTitle(QwtPlot::yLeft, "Inline");
    plot->setAxisTitle(QwtPlot::yRight, "Inline");
    //    float minx = srv->getStations().first().x();
    //    float maxx = srv->getStations().last().x();
        plot->setAxisScale( QwtPlot::xBottom,3500,300);
    //    plot->setAxisScale( QwtPlot::xTop,minx,maxx );
    //    QwtScaleDraw *sd = axisScaleDraw( QwtPlot::yLeft );
    //    sd->setMinimumExtent( sd->extent( axisWidget( QwtPlot::yLeft )->font() ) );
    plot->plotLayout()->setAlignCanvasToScales( true );
    QFileDialog custDialog;
    QStringList names = custDialog.getOpenFileNames(NULL, ("Open Files..."),QString(), ("UKOOA Files (*.p190 *.p90);;All Files (*)"));

    // handle if the dialog was "Cancelled"
    if(names.isEmpty())
    {
        return 0;
    }
    qSort(names.begin(), names.end());
    QVector <QwtPlotCurve *> curves;
    foreach (QString name, names)
    {


        QwtPlotCurve *vCurve = new QwtPlotCurve;
       if(name.contains(QString( "NS1763")) || name.contains(QString("NS2029")))
        {
           QColor c = Qt::red;
            vCurve->setSymbol( new QwtSymbol( QwtSymbol::Ellipse, Qt::red,c , QSize( 2, 2 ) ) );
        }
        else
        {
           QColor c = Qt::green;
            vCurve->setSymbol( new QwtSymbol( QwtSymbol::Ellipse, Qt::green,c , QSize( 2, 2 ) ) );
        }
        vCurve->setStyle( QwtPlotCurve::NoCurve );
        vCurve->setPen( Qt::gray );

        cout << name.toStdString() << endl;
        QVector<QPointF> curveData;

        //  m_nameLineLable->setText(m_names.at(0));
        QFile *ukFile = new QFile(QString(name));

        bool rt = ukFile->open(QIODevice::ReadOnly);

        cout << "return " << rt << endl;

        qint64 icount = 0;

        qint64 fileSize = ukFile->size();
        char *data = new char[fileSize];
        ukFile->read(data,fileSize);
        QString sData = data;
        QString shot = "SNS";
        while (true)
        {
            ukFile->seek(icount);
            ukFile->read(data,fileSize);
            sData = data;;
            if(icount>=fileSize)
            {
                break;
            }
            auto sPos = sData.indexOf(shot,0,Qt::CaseInsensitive);
            QString cr = sData.mid(sPos,19);
            if(cr.contains("\n"))
            {
                sPos +=2;
            }
            // auto shotNo  = sData.mid(sPos+20,sPos+5);
            QString shotNo = sData.mid(sPos+19,6);
            int shotNos;

            if(shotNo.contains("\n") || shotNo.contains("\r"))
            {
                shotNo = sData.mid(sPos+19,8);
                int shift1 = shotNo.indexOf("\r");
                int shift = shotNo.indexOf("\n");
                //    cout << shift1 << " " << shift << endl;
                QString tmp = shotNo.mid(0,shift1);
                tmp.append(shotNo.mid(shift+1,3));
                shotNos = tmp.toInt();
            }
            else
            {
                shotNos = sData.mid(sPos+19,6).toInt();
            }

            float shotYs;
            sPos = sData.indexOf(shot,0,Qt::CaseInsensitive);
            cr = sData.mid(sPos,55);
            if(cr.contains("\n"))
            {
                //       cout << " cr " << sPos << endl;
                sPos +=2;
            }

            QString shotY = sData.mid(sPos+55,10);
            //   cout << "shotx " << shotX.toStdString() << endl;
            if(shotY.contains("\n") || shotY.contains("\r"))
            {
                shotY = sData.mid(sPos+55,12);
                int shift1 = shotY.indexOf("\r");
                int shift = shotY.indexOf("\n");
                //           cout << shift1 << " " << shift << endl;
                QString tmp = shotY.mid(0,shift1);
                tmp.append(shotY.mid(shift+1,12));
                shotYs = tmp.toFloat();
            }
            else
            {
                shotYs = shotY.toFloat();
            }

            float shotXs;
            sPos = sData.indexOf(shot,0,Qt::CaseInsensitive);
            cr = sData.mid(sPos,46);
            if(cr.contains("\n"))
            {
                //       cout << " cr " << sPos << endl;
                sPos +=2;
            }

            QString shotX = sData.mid(sPos+46,10);
            //   cout << "shotx " << shotX.toStdString() << endl;
            if(shotX.contains("\n") || shotX.contains("\r"))
            {
                shotX = sData.mid(sPos+46,12);
                int shift1 = shotX.indexOf("\r");
                int shift = shotX.indexOf("\n");
                //           cout << shift1 << " " << shift << endl;
                QString tmp = shotX.mid(0,shift1);
                tmp.append(shotX.mid(shift+1,12));
                shotXs = tmp.toFloat();
            }
            else
            {
                shotXs = shotX.toFloat();
            }
            icount +=sPos+1;
            //     cout << shotNos << endl;
            float shotXt = shotXs - 757551.46;


            float shotYt = shotYs - 978769.0;
            float shotYr = shotYt * cosf(13.661f * M_PI/180.0f) + shotXt * sinf(13.661f * M_PI/180.0f);
            int shotYy = 981 + shotYr/25.0;
            float shotXr = shotXt * cosf(13.661f * M_PI/180.0f) - shotYt * sinf(13.661f * M_PI/180.0f);
            int shotXx = 2570 - shotXr/25.0;
//            if(shotXx>0 && shotYy>0)
//            {
                QPointF shotPoint(shotXx,shotYy);
                curveData.append(shotPoint);
            //    cout << " shot " << shotNos << " " << shotXs  << " " << shotYs << " "  << shotXx << " " << shotYy << endl;
 //           }



        }
        vCurve->setSamples(curveData);
        curves.append(vCurve);
        ukFile->close();
    }