예제 #1
0
void caStripPlot::setScaleColor(QColor c)
{
    QwtScaleWidget *scaleX =axisWidget(QwtPlot::xBottom);
    QwtScaleWidget *scaleY =axisWidget(QwtPlot::yLeft);
    thisScaleColor = c;
    QPalette palette = scaleX->palette();
    palette.setColor( QPalette::WindowText, c); // for ticks
    palette.setColor( QPalette::Text, c);       // for ticks' labels
    scaleX->setPalette( palette);
    scaleY->setPalette (palette);
    titleLabel()->setPalette(palette);
}
예제 #2
0
파일: Plot.cpp 프로젝트: trnielsen/mantid
Plot::Plot(int width, int height, QWidget *parent, const char *)
: QwtPlot(parent)
{
	setAutoReplot (false);

	marker_key = 0;
	curve_key = 0;

	minTickLength = 5;
	majTickLength = 9;

	setGeometry(QRect(0, 0, width, height));
	setAxisTitle(QwtPlot::yLeft, tr("Y Axis Title"));
	setAxisTitle(QwtPlot::xBottom, tr("X Axis Title"));
	//due to the plot layout updates, we must always have a non empty title
	setAxisTitle(QwtPlot::yRight, tr(" "));
	setAxisTitle(QwtPlot::xTop, tr(" "));

	// grid
	d_grid = new Grid();
	d_grid->attach(this);

	//custom scale
	for (int i= 0; i<QwtPlot::axisCnt; i++) {
		QwtScaleWidget *scale = (QwtScaleWidget *) axisWidget(i);
		if (scale) {
			scale->setMargin(0);

			//the axis title color must be initialized...
			QwtText title = scale->title();
			title.setColor(Qt::black);
			scale->setTitle(title);

            //...same for axis color
            QPalette pal = scale->palette();
            pal.setColor(QPalette::Foreground, QColor(Qt::black));
            scale->setPalette(pal);

			ScaleDraw *sd = new ScaleDraw(this);
			sd->setTickLength(QwtScaleDiv::MinorTick, minTickLength);
			sd->setTickLength(QwtScaleDiv::MediumTick, minTickLength);
			sd->setTickLength(QwtScaleDiv::MajorTick, majTickLength);

			setAxisScaleDraw (i, sd);
			setAxisScaleEngine (i, new ScaleEngine());
		}
	}

	QwtPlotLayout *pLayout = plotLayout();
	pLayout->setCanvasMargin(0);
	pLayout->setAlignCanvasToScales (true);

	QwtPlotCanvas* plCanvas = canvas();
	plCanvas->setFocusPolicy(Qt::StrongFocus);
	plCanvas->setFocusIndicator(QwtPlotCanvas::ItemFocusIndicator);
	//plCanvas->setFocus();
	plCanvas->setFrameShadow(QwtPlot::Plain);
	plCanvas->setCursor(Qt::arrowCursor);
	plCanvas->setLineWidth(0);
	plCanvas->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
	plCanvas->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);

    QColor background = QColor(Qt::white);
    background.setAlpha(255);

	QPalette palette;
    palette.setColor(QPalette::Window, background);
    setPalette(palette);

	setCanvasBackground (background);
	setFocusPolicy(Qt::StrongFocus);
	//setFocusProxy(plCanvas);
	setFrameShape(QFrame::Box);
	setLineWidth(0);
}
/*! 
   Reset color and fonts of a plot
   \sa apply()
*/
void QwtPlotPrintFilter::reset(QwtPlot *plot) const
{
    if ( d_data->cache == 0 )
        return;

    const bool doAutoReplot = plot->autoReplot();
    plot->setAutoReplot(false);

    const PrivateData::Cache &cache = *d_data->cache;

    if ( plot->titleLabel() )
    {
        QwtTextLabel* title = plot->titleLabel();
        if ( title->text().testPaintAttribute(QwtText::PaintUsingTextFont) )
        {
            QwtText text = title->text();
            text.setColor(cache.titleColor);
            title->setText(text);
        }
        else
        {
            QPalette palette = title->palette();
            palette.setColor(
                QPalette::Active, Palette::Text, cache.titleColor);
            title->setPalette(palette);
        }

        if ( title->text().testPaintAttribute(QwtText::PaintUsingTextFont) )
        {
            QwtText text = title->text();
            text.setFont(cache.titleFont);
            title->setText(text);
        }
        else
        {
            title->setFont(cache.titleFont);
        }
    }

    if ( plot->legend() )
    {
#if QT_VERSION < 0x040000
        QValueList<QWidget *> list = plot->legend()->legendItems();
        for ( QValueListIterator<QWidget *> it = list.begin();
            it != list.end(); ++it )
#else
        QList<QWidget *> list = plot->legend()->legendItems();
        for ( QList<QWidget*>::iterator it = list.begin();
            it != list.end(); ++it )
#endif
        {
            QWidget *w = *it;

            if ( cache.legendFonts.contains(w) )
                w->setFont(cache.legendFonts[w]);

            if ( w->inherits("QwtLegendItem") )
            {
                QwtLegendItem *label = (QwtLegendItem *)w;
                const QwtPlotItem *plotItem = 
                    (const QwtPlotItem*)plot->legend()->find(label);

                QwtSymbol symbol = label->symbol();
                if ( cache.curveSymbolPenColors.contains(plotItem) )
                {
                    QPen pen = symbol.pen();
                    pen.setColor(cache.curveSymbolPenColors[plotItem]);
                    symbol.setPen(pen);
                }

                if ( cache.curveSymbolBrushColors.contains(plotItem) )
                {
                    QBrush brush = symbol.brush();
                    brush.setColor(cache.curveSymbolBrushColors[plotItem]);
                    symbol.setBrush(brush);
                }
                label->setSymbol(symbol);

                if ( cache.curveColors.contains(plotItem) )
                {
                    QPen pen = label->curvePen();
                    pen.setColor(cache.curveColors[plotItem]);
                    label->setCurvePen(pen);
                }
            }
        }
    }
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
        QwtScaleWidget *scaleWidget = plot->axisWidget(axis);
        if ( scaleWidget )
        {
            QPalette palette = scaleWidget->palette();
            palette.setColor(QPalette::Active, Palette::Foreground,
                             cache.scaleColor[axis]);
            scaleWidget->setPalette(palette);

            scaleWidget->setFont(cache.scaleFont[axis]);
            scaleWidget->setTitle(cache.scaleTitle[axis]);

            int startDist, endDist;
            scaleWidget->getBorderDistHint(startDist, endDist);
            scaleWidget->setBorderDist(startDist, endDist);
        }
    }

    if ( hasBackgroundColor(plot) )
    {
        QPalette p = plot->palette();
        p.setColor(QPalette::Active, Palette::Background, cache.widgetBackground);
        plot->setPalette(p);
    }

    if ( hasBackgroundColor(plot->canvas()) )
    {
        plot->setCanvasBackground(cache.canvasBackground);
    }
   
    const QwtPlotItemList& itmList = plot->itemList();
    for ( QwtPlotItemIterator it = itmList.begin();
        it != itmList.end(); ++it )
    {
        reset(*it);
    }

    delete d_data->cache;
    d_data->cache = 0;

    plot->setAutoReplot(doAutoReplot);
}
/*! 
  Change color and fonts of a plot
  \sa apply()
*/
void QwtPlotPrintFilter::apply(QwtPlot *plot) const
{
    const bool doAutoReplot = plot->autoReplot();
    plot->setAutoReplot(false);

    delete d_data->cache;
    d_data->cache = new PrivateData::Cache;

    PrivateData::Cache &cache = *d_data->cache;

    if ( plot->titleLabel() )
    {
        QPalette palette = plot->titleLabel()->palette();
        cache.titleColor = palette.color(
            QPalette::Active, Palette::Text);
        palette.setColor(QPalette::Active, Palette::Text,
                         color(cache.titleColor, Title));
        plot->titleLabel()->setPalette(palette);

        cache.titleFont = plot->titleLabel()->font();
        plot->titleLabel()->setFont(font(cache.titleFont, Title));
    }
    if ( plot->legend() )
    {
#if QT_VERSION < 0x040000
        QValueList<QWidget *> list = plot->legend()->legendItems();
        for ( QValueListIterator<QWidget *> it = list.begin();
            it != list.end(); ++it )
#else
        QList<QWidget *> list = plot->legend()->legendItems();
        for ( QList<QWidget*>::iterator it = list.begin();
            it != list.end(); ++it )
#endif
        {
            QWidget *w = *it;

            cache.legendFonts.insert(w, w->font());
            w->setFont(font(w->font(), Legend));

            if ( w->inherits("QwtLegendItem") )
            {
                QwtLegendItem *label = (QwtLegendItem *)w;

                QwtSymbol symbol = label->symbol();
                QPen pen = symbol.pen();
                QBrush brush = symbol.brush();

                pen.setColor(color(pen.color(), CurveSymbol));
                brush.setColor(color(brush.color(), CurveSymbol));

                symbol.setPen(pen);
                symbol.setBrush(brush);
                label->setSymbol(symbol);

                pen = label->curvePen();
                pen.setColor(color(pen.color(), Curve));
                label->setCurvePen(pen);
            }
        }
    }
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
        QwtScaleWidget *scaleWidget = plot->axisWidget(axis);
        if ( scaleWidget )
        {
            cache.scaleColor[axis] = scaleWidget->palette().color(
                QPalette::Active, Palette::Foreground);
            QPalette palette = scaleWidget->palette();
            palette.setColor(QPalette::Active, Palette::Foreground,
                             color(cache.scaleColor[axis], AxisScale));
            scaleWidget->setPalette(palette);

            cache.scaleFont[axis] = scaleWidget->font();
            scaleWidget->setFont(font(cache.scaleFont[axis], AxisScale));

            cache.scaleTitle[axis] = scaleWidget->title();

            QwtText scaleTitle = scaleWidget->title();
            if ( scaleTitle.testPaintAttribute(QwtText::PaintUsingTextColor) )
            {
                cache.scaleTitleColor[axis] = scaleTitle.color();
                scaleTitle.setColor(
                    color(cache.scaleTitleColor[axis], AxisTitle));
            }

            if ( scaleTitle.testPaintAttribute(QwtText::PaintUsingTextFont) )
            {
                cache.scaleTitleFont[axis] = scaleTitle.font();
                scaleTitle.setFont(
                    font(cache.scaleTitleFont[axis], AxisTitle));
            }

            scaleWidget->setTitle(scaleTitle);

            int startDist, endDist;
            scaleWidget->getBorderDistHint(startDist, endDist);
            scaleWidget->setBorderDist(startDist, endDist);
        }
    }

    if ( hasBackgroundColor(plot) )
    {
        QPalette p = plot->palette();
        cache.widgetBackground = plot->palette().color(
            QPalette::Active, Palette::Background);
        p.setColor(QPalette::Active, Palette::Background, 
            color(cache.widgetBackground, WidgetBackground));
        plot->setPalette(p);
    }

    if ( hasBackgroundColor(plot->canvas()))
    {
        cache.canvasBackground = plot->canvasBackground();
        plot->setCanvasBackground(color(cache.canvasBackground, CanvasBackground));
    }

    const QwtPlotItemList& itmList = plot->itemList();
    for ( QwtPlotItemIterator it = itmList.begin();
        it != itmList.end(); ++it )
    {
        apply(*it);
    }

    plot->setAutoReplot(doAutoReplot);
}