#ifndef PLOTSETTINGS_H
#define PLOTSETTINGS_H
#include <QPointF>
#include <cmath>
/// Àáñòðàêòíûé êëàññ äëÿ çàäàíèÿ äèàïàçîíà çíà÷åíèé ïî îñÿì x è y
class PlotSettings
{
public:
double minX; ///< ìèíèìàëüíîå çíà÷åíèå ïî îñè àáñöèññ
double maxX; ///< ìàêñèìàëüíîå çíà÷åíèå ïî îñè àáñöèññ
int numXTicks; ///< êîëè÷åñòâî äåëåíèé íà îñè àáñöèññ
double minY; ///< ìèíèìàëüíîå çíà÷åíèå ïî îñè îðäèíàò
double maxY; ///< ìàêñèìàëüíîå çíà÷åíèå ïî îñè îðäèíàò
int numYTicks; ///< êîëè÷åñòâî äåëåíèé íà îñè îðäèíàò
protected:
void adjustAxis(double& min, double& max, int& numTicks);
public:
PlotSettings();
void scroll(double dx, double dy);
void scale(double delta_x, double delta_y);
void adjust();
void adjust(QPointF& point);
double spanX() const { return fabs(maxX - minX); }
double spanY() const { return fabs(maxY - minY); }
};
#endif
#include "PlotSettings.h"
PlotSettings::PlotSettings(): minX(-50.), minY(-4.), maxX(50.), maxY(45.)
{
numXTicks = 8;
numYTicks = 8;
}
/// Óâåëè÷åíèå/óìåíüøåíèå çíà÷åíèÿ minX, maxX, minY, maxY íà èíòåðâàë ìåæäó 2-ìÿ îòìåòêàìè
void PlotSettings::scroll(double dx, double dy)
{
double stepX = spanX() / numXTicks;
minX += dx * stepX;
maxX += dx * stepX;
double stepY = spanY() / numYTicks;
minY += dy * stepY;
maxY += dy * stepY;
adjust();
}
/// Óâåëè÷åíèå/óìåíüøåíèå çíà÷åíèÿ minX, maxX, minY, maxY íà èíòåðâàë ìåæäó 2-ìÿ îòìåòêàìè
void PlotSettings::scale(double delta_x, double delta_y)
{
if((minX == maxX || minY == maxY) && delta_x < 0 && delta_y < 0) return;
double stepX = spanX() / numXTicks;
minX -= delta_x * stepX;
maxX += delta_x * stepX;
double stepY = spanY() / numYTicks;
minY -= delta_y * stepY;
maxY += delta_y * stepY;
}
/// Îêðóãëåíèå çíà÷åíèé minX, minY, maxX, maxY
void PlotSettings::adjust()
{
adjustAxis(minX, maxX, numXTicks);
adjustAxis(minY, maxY, numYTicks);
}
/// Îêðóãëåíèå çíà÷åíèé çàäàííîé òî÷êè point
void PlotSettings::adjust(QPointF& point)
{
double mn_x = minX, mn_y = minY;
int ticks_x = numXTicks, ticks_y = numYTicks;
double mx_x = point.x(), mx_y = point.y();
adjustAxis(mn_x, mx_x, ticks_x);
adjustAxis(mn_y, mx_y, ticks_y);
point.setX(mx_x);
point.setY(mx_y);
}
/// Ïðåîáðàçîâàíèå ïàðàìåòðîâ â óäîáíûå çíà÷åíèÿ
void PlotSettings::adjustAxis(double& min, double& max, int& numTicks)
{
const int MinTicks = 5;
double grossStep = (max - min) / MinTicks;
double step = pow(10.0, floor(log10(grossStep)));
if(5 * step < grossStep)
step *= 5;
else if(2 * step < grossStep)
step *= 2;
numTicks = int(ceil(max / step) - floor(min / step));
if(numTicks < MinTicks)
numTicks = MinTicks;
min = floor(min / step) * step;
max = ceil(max / step) * step;
}
#ifndef GRAPHICWIDGET_H
#define GRAPHICWIDGET_H
#include "ui_GraphicWidget.h"
#include "PlotSettings.h"
#include <QtGui/QWidget>
#include <QPoint>
class QPaintEvent;
class QPainter;
class QRubberBand;
class QKeyEvent;
class QResizeEvent;
class QCloseEvent;
class QShowEvent;
class QWheelEvent;
class QMouseEvent;
/// Âèäæåò, ãäå áóäåò îòðèñîâûâàòüñÿ ãðàôèê
class GraphicWidget: public QWidget, public Ui::GraphicWidgetClass
{
Q_OBJECT
private:
PlotSettings settings; ///< íàñòðîéêà äëÿ ðàçëè÷íûõ ìàñøòàáîâ
QRubberBand* rubber; ///< "ðåçèíîâàÿ ëåíòà"
bool rubberBandIsShown; ///< ôëàæîê ïîïàäàíèÿ êóðñîðà â "ðåçèíîâóþ ëåíòó"
QPoint origin; ///< íà÷àëüíûå êîîðäèíàòû âûäåëÿåìîé îáëàñòè
QVector<QPointF> curve_vec; ///< âåêòîð òî÷åê êðèâîé
private:
void drawGrid(QPainter *painter);
void drawCurves(QPainter* painter);
QPointF initXY(double sx, double sy);
void initCurves();
protected:
void paintEvent(QPaintEvent* events);
void keyPressEvent(QKeyEvent* events);
void wheelEvent(QWheelEvent* events);
void mousePressEvent(QMouseEvent* events);
void mouseMoveEvent(QMouseEvent* events);
void mouseReleaseEvent(QMouseEvent* events);
void resizeEvent(QResizeEvent* events) { QWidget::resizeEvent(events); update(); }
void closeEvent(QCloseEvent* events) { QWidget::closeEvent(events); }
void showEvent(QShowEvent* events) { QWidget::showEvent(events); }
public:
GraphicWidget(QWidget *parent = 0, Qt::WFlags flags = 0);
~GraphicWidget();
void setPlotSettings(const PlotSettings& sts) { settings = sts; settings.adjust(); update(); }
void zoom(double delta) { settings.scale(delta, delta); settings.adjust(); update(); }
};
#endif // GRAPHICWIDGET_H
#include "GraphicWidget.h"
#include <QPainter>
#include <QRect>
#include <QString>
#include <QRubberBand>
#include <QKeyEvent>
#include <QWheelEvent>
#include <QMouseEvent>
#include <algorithm>
using namespace std;
GraphicWidget::GraphicWidget(QWidget *parent, Qt::WFlags flags): QWidget(parent, flags), rubberBandIsShown(false),
rubber(new QRubberBand(QRubberBand::Rectangle, this))
{
setupUi(this);
/// Èíèöèàëèçàöèÿ íåîáõîäèìûõ ïàðàìåòðîâ
setPlotSettings(PlotSettings());
initCurves();
}
GraphicWidget::~GraphicWidget()
{}
/// Çàïîëíåíèÿ âåêòîðà ñ êðèâîé (êðèâàÿ y(x) = x)
void GraphicWidget::initCurves()
{
curve_vec.append(QPointF(-100., -100.));
curve_vec.append(QPointF(-10., -10.));
curve_vec.append(QPointF(-1., -1.));
curve_vec.append(QPointF(0., 0.));
curve_vec.append(QPointF(1., 1.));
curve_vec.append(QPointF(1.5, 1.5));
curve_vec.append(QPointF(5., 5.));
curve_vec.append(QPointF(10., 10.));
curve_vec.append(QPointF(100., 100.));
}
/// Îòðèñîâêà ñåòêè
void GraphicWidget::drawGrid(QPainter *painter)
{
QRect rect(graphWidget -> rect());
if(!rect.isValid()) return;
QRect boundString;
double great_max = max(settings.numXTicks, settings.numYTicks) + 1;
for(register int i=0, j=0, k=0; i<=great_max; ++i, ++j, ++k)
{
if(j <= settings.numXTicks) ///< îòðèñîâêà âäîëü îñè X
{
int x = rect.left() + (j * (rect.width() - 1) / settings.numXTicks);
double label = settings.minX + (j * settings.spanX() / settings.numXTicks);
QString s_label(QString::number(label, 'f', 3));
painter -> setPen(Qt::black);
painter -> drawLine(x, rect.top(), x, rect.bottom());
int flags = Qt::AlignHCenter | Qt::AlignTop;
boundString = painter -> boundingRect(boundString, flags, s_label);
painter -> drawText(x - (boundString.width() + 5), rect.bottom() - (boundString.height() + 5),
boundString.width(), boundString.height(), flags, s_label);
}
if(k <= settings.numYTicks) ///< îòðèñîâêà âäîëü îñè Y
{
int y = rect.bottom() - (k * (rect.height() - 1) / settings.numYTicks);
double label = settings.minY + (k * settings.spanY() / settings.numYTicks);
QString s_label(QString::number(label, 'f', 3));
painter -> setPen(Qt::black);
painter -> drawLine(rect.left(), y, rect.right(), y);
int flags = Qt::AlignRight | Qt::AlignTop;
boundString = painter -> boundingRect(boundString, flags, s_label);
painter -> drawText(rect.left() + 7, y - boundString.height(),
boundString.width(), boundString.height(), flags, s_label);
}
}
painter -> drawRect(rect.adjusted(0, 0, -1, -1));
}
/// Èíèöèàëèçàöèÿ êîîðäèíàò - ïðåîáðàçîâàíèå èç êîîðäèíàò ãðàôèêà (sx,sy) â ýêðàííûå (x,y)
QPointF GraphicWidget::initXY(double sx, double sy)
{
QRect rect(graphWidget -> rect());
double dx, dy;
/// Âû÷èñëåíèå ñìåùåíèé âäîëü îñåé
dx = sx - settings.maxX;
dy = sy - settings.minY;
/// Âû÷èñëåíèå ýêðàííûõ êîîðäèíàò
double x = rect.right() + (dx * (rect.width() - 1) / settings.spanX());
double y = rect.bottom() - (dy * (rect.height() - 1) / settings.spanY());
return QPointF(x, y);
}
/// Îòðèñîâêà ãðàôèêà
void GraphicWidget::drawCurves(QPainter* painter)
{
painter -> setPen(Qt::blue);
QPolygonF polyline(curve_vec.size());
for(register int i=0; i<curve_vec.size(); ++i)
polyline[i] = initXY(curve_vec[i].x(), curve_vec[i].y());
painter -> drawPolyline(polyline);
}
/// Îòðèñîâêà ãðàôèêà
void GraphicWidget::paintEvent(QPaintEvent* events)
{
QPainter painter(this);
drawGrid(&painter);
drawCurves(&painter);
QWidget::paintEvent(events);
}
/// Íàæàòèå íà êíîïêè êëàâèàòóðû
void GraphicWidget::keyPressEvent(QKeyEvent* events)
{
switch(events -> key())
{
case Qt::Key_Plus:
zoom(-1.0);
break;
case Qt::Key_Minus:
zoom(1.0);
break;
case Qt::Key_Left:
settings.scroll(-1.0, 0.0);
update();
break;
case Qt::Key_Right:
settings.scroll(1.0, 0.0);
update();
break;
case Qt::Key_Up:
settings.scroll(0.0, 1.0);
update();
break;
case Qt::Key_Down:
settings.scroll(0.0, -1.0);
update();
break;
case Qt::Key_X:
if(events -> modifiers() & Qt::ALT) close();
break;
case Qt::Key_Return:
if(events -> modifiers() & Qt::ALT)
{
if(!isMaximized()) showMaximized();
else showNormal();
}
break;
default:
QWidget::keyPressEvent(events);
}
}
/// Èçìåíåíèå ìàñøòàáà ïðè äâèæåíèè êîëåñèêà
void GraphicWidget::wheelEvent(QWheelEvent* events)
{
int numDegrees = events -> delta() / 8;
double numTicks = numDegrees / 15.0;
zoom(numTicks);
update();
}
/// Íàæàòèå íà êíîïêó - èçìåíåíèå ìàñøòàáà
void GraphicWidget::mousePressEvent(QMouseEvent* events)
{
QWidget::mousePressEvent(events);
QRect r;
switch(events -> button())
{
case Qt::LeftButton: ///< åñëè íàæàòà ëåâàÿ êíîïêà ìûøè
origin = events -> pos();
rubberBandIsShown = true;
setCursor(Qt::CrossCursor);
r = QRect(origin, QSize());
rubber -> setGeometry(r);
rubber -> show();
break;
case Qt::RightButton: ///< åñëè íàæàòà ïðàâàÿ êíîïêà ìûøè
break;
default:
break;
}
}
/// Ïåðåîïðåäåëåíèå ôóíêöèè ïåðåäâèæåíèÿ ìûøè
void GraphicWidget::mouseMoveEvent(QMouseEvent* events)
{
if(rubberBandIsShown)
{
rubber -> setWindowOpacity(0.0);
rubber -> setGeometry(QRect(origin, events -> pos()).normalized());
}
update();
}
/// Âîçâðàùåíèå ïðåæíåãî âèäà êóðñîðó è èçìåíåíèå ìàñøòàáà
void GraphicWidget::mouseReleaseEvent(QMouseEvent* events)
{
if(events -> button() == Qt::LeftButton && rubberBandIsShown)
{
rubberBandIsShown = false;
unsetCursor();
QRect rect = rubber -> geometry().normalized();
if(rect.width() < 10 || rect.height() < 10)
{
double dx = rect.width() / settings.spanX();
double dy = rect.height() / settings.spanY();
settings.scroll(dx, dy);
update();
return;
}
PlotSettings prevSettings = settings;
double dx = prevSettings.spanX() / width();
double dy = prevSettings.spanY() / height();
settings.minX = prevSettings.minX + dx * rect.left();
settings.maxX = prevSettings.minX + dx * rect.right();
settings.minY = prevSettings.maxY - dy * rect.bottom();
settings.maxY = prevSettings.maxY - dy * rect.top();
settings.adjust();
rubber -> hide();
update();
}
}