Files
lsim/src/graphicsview.cpp
2008-12-16 21:59:25 +00:00

115 lines
4.1 KiB
C++

/***************************************************************************
* Copyright (C) 2008 by Peter Dahlberg *
* pdahlberg@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "graphicsview.h"
//#include <QGLWidget>
#include <QGraphicsItem>
#include <QDebug>
#include <cmath>
#include <QMouseEvent>
#include <QStatusBar>
#include "simulscene.h"
#include <QLocale>
GraphicsView::GraphicsView(QWidget * parent, QStatusBar* statusbar): QGraphicsView(parent) {
mainWindowStatusBar = statusbar;
//setMinimumSize(900,700);
//setDragMode(QGraphicsView::ScrollHandDrag);
//setRenderHint (QPainter::QPainter::Antialiasing,true);
//setCacheMode(QGraphicsView::CacheBackground);
//setTransformationAnchor (QGraphicsView::AnchorUnderMouse);
//scale(2,2);
}
void GraphicsView::drawBackground ( QPainter * painter, const QRectF & rect ) {
int lineDistance = 20;
double startX = rect.x();
double startY = rect.y();
double endX = rect.x() + rect.width();
double endY = rect.y() + rect.height();
QPainterPath currPath;
int startPaintX = floor(startX/(double)lineDistance)*lineDistance;
int endPaintX = ceil(endX/(double)lineDistance)*lineDistance;
for (double i = startPaintX; i<endPaintX; i= i+lineDistance) {
if (i==0 )continue;
QPolygonF polygon;
polygon << QPointF(i,startY) << QPointF(i,endY);
currPath.addPolygon(polygon);
}
int startPaintY = floor(startY/(double)lineDistance)*lineDistance;
int endPaintY = ceil(endY/(double)lineDistance)*lineDistance;
for (double i = startPaintY ; i<endPaintY; i= i+lineDistance) {
if (i==0 )continue;
QPolygonF polygon;
polygon << QPointF(startX,i) << QPointF(endX,i);
currPath.addPolygon(polygon);
}
painter->setPen(QPen(Qt::lightGray, 1, Qt::SolidLine));
painter->drawPath(currPath);
painter->setPen(QPen(Qt::green, 1, Qt::SolidLine));
painter->drawLine(startX,0,endX,0);
painter->drawLine(0,startY,0,endY);
}
void GraphicsView::mouseMoveEvent ( QMouseEvent * e ) {
QGraphicsView::mouseMoveEvent(e);
SimulScene* simulScene = dynamic_cast<SimulScene*> (scene());
if (simulScene != 0 && mainWindowStatusBar !=0) {
QPointF scenePos = mapToScene(e->pos());
QString posString;
posString += "Mausposition: ";
posString += "x: " + QLocale().toString(scenePos.x(),'g') + " px"+"/";
posString += QLocale().toString(scenePos.x()*simulScene->getMeterPerPx(),'g') + " m";
posString += " y: " + QLocale().toString(scenePos.y(),'g') + " px"+"/";
posString += QLocale().toString(scenePos.y()*simulScene->getMeterPerPx(),'g') + " m";
mainWindowStatusBar->showMessage(posString);
}
}
void GraphicsView::enableOGLViewport() {
//setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
}
void GraphicsView::disableOGLViewport() {
//setViewport(new QWidget(parentWidget()));
}
void GraphicsView::centerOnProbe() {
SimulScene* simulScene = dynamic_cast<SimulScene*> (scene());
if (simulScene == 0) return;
centerOn(simulScene->getProbeChargeItem());
}
GraphicsView::~GraphicsView() {
}