Esempio n. 1
0
void plotsDialog::savePlotSettings()
{

#ifdef Q_OS_WIN32
    QFile file("plotTemp.xml");
#endif
#ifdef Q_OS_MAC
    QDir dir = qApp->applicationDirPath();
    /*dir.cdUp();*/
    /*dir.cdUp();*/
    /*dir.cdUp();*/
    QString bundleDir(dir.absolutePath());
    QFile ofile(globalpara.caseName),file(bundleDir+"/plotTemp.xml");
#endif
    QTextStream stream;
    stream.setDevice(&file);
    QDomDocument doc;
    QDomElement plotData, currentPlot, general, legend, grid, curve;

    if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        globalpara.reportError("Failed to open and load case file for plot data.",this);
        return;
    }
    else
    {
        if(!doc.setContent(&file))
        {
            globalpara.reportError("Failed to set document for the xml file for plot data.",this);
            file.close();
            return;
        }
        else
        {
            plotData = doc.elementsByTagName("plotData").at(0).toElement();

            for(int i = 0; i < tabs->count(); i++)
            {
                Plot* set_plot = dynamic_cast<Plot*>(tabs->widget(i));
                currentPlot = plotData.elementsByTagName(tabs->tabText(i)).at(0).toElement();

                //general
                if(currentPlot.elementsByTagName("general").count()==0)
                {
                    general = doc.createElement("general");
                    currentPlot.appendChild(general);
                }
                else general = currentPlot.elementsByTagName("general").at(0).toElement();
                general.setAttribute("bgColor",set_plot->canvasBackground().color().name());
                general.setAttribute("lMargin",set_plot->contentsMargins().left());
                general.setAttribute("rMargin",set_plot->contentsMargins().right());
                general.setAttribute("tMargin",set_plot->contentsMargins().top());
                general.setAttribute("bMargin",set_plot->contentsMargins().bottom());
                general.setAttribute("plotTitle",set_plot->title().text());
                general.setAttribute("xTitle",set_plot->axisTitle(QwtPlot::xBottom).text());
                general.setAttribute("yTitle",set_plot->axisTitle(QwtPlot::yLeft).text());

                //legend
                if(currentPlot.elementsByTagName("legend").count()==0)
                {
                    legend = doc.createElement("legend");
                    currentPlot.appendChild(legend);
                }
                else legend = currentPlot.elementsByTagName("legend").at(0).toElement();
                if(set_plot->externalLegend!=NULL||set_plot->internalLegend->isVisible())
                {
                    legend.setAttribute("plotLegend","on");
                    if(set_plot->internalLegend->isVisible())
                    {
                        legend.setAttribute("extInt","int");
                        legend.setAttribute("nCol",set_plot->internalLegend->maxColumns());
                        legend.setAttribute("legendSize",set_plot->internalLegend->font().pointSize());
                    }
                    else if(set_plot->externalLegend!=NULL)
                    {
                        legend.setAttribute("extInt","ext");
                        legend.setAttribute("extPos",0);
                    }
                }
                else legend.setAttribute("plotLegend","off");

                //grid
                if(currentPlot.elementsByTagName("grid").count()==0)
                {
                    grid = doc.createElement("grid");
                    currentPlot.appendChild(grid);
                }
                else grid = currentPlot.elementsByTagName("grid").at(0).toElement();
                if(set_plot->grid->xEnabled()||set_plot->grid->yEnabled())
                {
                    if(set_plot->grid->xEnabled())
                        grid.setAttribute("xMaj","on");
                    else grid.setAttribute("xMaj","off");
                    if(set_plot->grid->yEnabled())
                        grid.setAttribute("yMaj","on");
                    else grid.setAttribute("yMaj","off");
                    grid.setAttribute("majColor",set_plot->grid->majorPen().color().name());
                    grid.setAttribute("majSize",set_plot->grid->majorPen().width());
                    int styleCode;
                    if(set_plot->grid->majorPen().style()==Qt::NoPen)
                        styleCode=0;
                    else if(set_plot->grid->majorPen().style()==Qt::SolidLine)
                        styleCode=1;
                    else if(set_plot->grid->majorPen().style()==Qt::DashLine)
                        styleCode=2;
                    else if(set_plot->grid->majorPen().style()==Qt::DotLine)
                        styleCode=3;
                    else if(set_plot->grid->majorPen().style()==Qt::DashDotLine)
                        styleCode=4;
                    else if(set_plot->grid->majorPen().style()==Qt::DashDotDotLine)
                        styleCode=5;
                    grid.setAttribute("majStyle",styleCode);
                }
                else
                {
                    grid.setAttribute("xMaj","off");
                    grid.setAttribute("yMaj","off");
                }
                if(set_plot->grid->xMinEnabled()||set_plot->grid->yMinEnabled())
                {
                    if(set_plot->grid->xMinEnabled())
                        grid.setAttribute("xMin","on");
                    else grid.setAttribute("xMin","off");
                    if(set_plot->grid->yMinEnabled())
                        grid.setAttribute("yMin","on");
                    else grid.setAttribute("yMin","off");
                    grid.setAttribute("minColor",set_plot->grid->minorPen().color().name());
                    grid.setAttribute("minSize",set_plot->grid->minorPen().width());
                    int styleCode;
                    if(set_plot->grid->minorPen().style()==Qt::NoPen)
                        styleCode=0;
                    else if(set_plot->grid->minorPen().style()==Qt::SolidLine)
                        styleCode=1;
                    else if(set_plot->grid->minorPen().style()==Qt::DashLine)
                        styleCode=2;
                    else if(set_plot->grid->minorPen().style()==Qt::DotLine)
                        styleCode=3;
                    else if(set_plot->grid->minorPen().style()==Qt::DashDotLine)
                        styleCode=4;
                    else if(set_plot->grid->minorPen().style()==Qt::DashDotDotLine)
                        styleCode=5;
                    grid.setAttribute("minStyle",styleCode);
                }
                else
                {
                    grid.setAttribute("xMin","off");
                    grid.setAttribute("yMin","off");
                }

                //curve
                QDomNode const oldCurveSettings = currentPlot.elementsByTagName("curve").at(0);
                currentPlot.removeChild(oldCurveSettings);
                curve = doc.createElement("curve");
                currentPlot.appendChild(curve);
//                if(currentPlot.elementsByTagName("curve").count()==0)
//                {
//                    curve = doc.createElement("curve");
//                    currentPlot.appendChild(curve);
//                }
//                else curve = currentPlot.elementsByTagName("curve").at(0).toElement();
//                if(curve.childNodes().count()!=set_plot->curvelist.count())
//                {
//                    QDomNode node;
//                    for(int i = 0; i < curve.childNodes().count();i++)
//                    {
//                        node = curve.childNodes().at(i);
//                        curve.removeChild(node);
//                    }
//                    QDomElement newCurve;
//                    for(int i = 0; i < set_plot->curvelist.count();i++)
//                    {
//                        newCurve = doc.createElement(set_plot->curvelist.at(i)->title().text().replace(" ",""));
//                        curve.appendChild(newCurve);
//                    }
//                }
                QDomElement currentCurve;
                QwtPlotCurve *thisCurve;
                for(int i = 0; i < set_plot->curvelist.count(); i++)
                {
                    currentCurve = doc.createElement(set_plot->curvelist.at(i)->title().text().replace(" ",""));
                    curve.appendChild(currentCurve);
                    thisCurve = set_plot->curvelist[i];
                    currentCurve = curve.elementsByTagName(set_plot->curvelist.at(i)->title().text().replace(" ","")).at(0).toElement();

                    if(thisCurve->isVisible())
                        currentCurve.setAttribute("isVisible","true");
                    else
                        currentCurve.setAttribute("isVisible","false");
                    currentCurve.setAttribute("lineColor",thisCurve->pen().color().name());
                    currentCurve.setAttribute("lineSize",thisCurve->pen().width());
                    int styleCode;
                    if(thisCurve->pen().style()==Qt::NoPen)
                        styleCode=0;
                    else if(thisCurve->pen().style()==Qt::SolidLine)
                        styleCode=1;
                    else if(thisCurve->pen().style()==Qt::DashLine)
                        styleCode=2;
                    else if(thisCurve->pen().style()==Qt::DotLine)
                        styleCode=3;
                    else if(thisCurve->pen().style()==Qt::DashDotLine)
                        styleCode=4;
                    else if(thisCurve->pen().style()==Qt::DashDotDotLine)
                        styleCode=5;
                    currentCurve.setAttribute("lineType",styleCode);
                }

            }

            file.resize(0);
            doc.save(stream,4);
            file.close();
            return;
        }
    }
}