#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtGui>
#include <QObject>
#include <QInputDialog>
#include <QDebug>
#include "graph.h"
Graph::Graph(QMainWindow *parent) :
QMainWindow(parent)
{
customPlot = new QCustomPlot;
customPlot->setEnabled(false);
srand(QDateTime::currentDateTime().toTime_t());
customPlot->setInteractions(QCustomPlot::iRangeDrag | QCustomPlot::iRangeZoom | QCustomPlot::iSelectAxes |
QCustomPlot::iSelectLegend | QCustomPlot::iSelectPlottables | QCustomPlot::iSelectTitle);
customPlot->setRangeDrag(Qt::Horizontal|Qt::Vertical);
customPlot->setRangeZoom(Qt::Horizontal|Qt::Vertical);
customPlot->xAxis->setRange(0, 1);
customPlot->yAxis->setRange(0, 1);
customPlot->yAxis->setRangeReversed(true);
customPlot->setupFullAxesBox();
customPlot->xAxis->setLabel("x Axis");
customPlot->yAxis->setLabel("y Axis");
customPlot->legend->setVisible(true);
QFont legendFont = font();
legendFont.setPointSize(10);
customPlot->legend->setFont(legendFont);
customPlot->legend->setSelectedFont(legendFont);
customPlot->legend->setSelectable(QCPLegend::spItems); // legend box shall not be selectable, only legend items
// connect slot that ties some axis selections together (especially opposite axes):
connect(customPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));
// connect slots that takes care that when an axis is selected, only that direction can be dragged and zoomed:
connect(customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress()));
connect(customPlot, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel()));
// make bottom and left axes transfer their ranges to top and right axes:
connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
// connect some interaction slots:
connect(customPlot, SIGNAL(titleDoubleClick(QMouseEvent*)), this, SLOT(titleDoubleClick()));
connect(customPlot, SIGNAL(axisDoubleClick(QCPAxis*,QCPAxis::SelectablePart,QMouseEvent*)), this, SLOT(axisLabelDoubleClick(QCPAxis*,QCPAxis::SelectablePart)));
connect(customPlot, SIGNAL(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*,QMouseEvent*)), this, SLOT(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*)));
// connect slot that shows a message in the status bar when a graph is clicked:
connect(customPlot, SIGNAL(plottableClick(QCPAbstractPlottable*,QMouseEvent*)), this, SLOT(graphClicked(QCPAbstractPlottable*)));
// setup policy and connect slot for context menu popup:
customPlot->setContextMenuPolicy(Qt::CustomContextMenu);
connect(customPlot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
// Oppretter databasen med databasedriver QODBC
db = QSqlDatabase::addDatabase("QODBC");
// Setter maksverdiene for x og y aksene til -1 for å være sikker på at de blir overskrevet
xMax = -1;
yMax = -1;
// Setter startverdi for fargevelger variabelen
colorCounter = 0;
}
Qt::GlobalColor Graph::getColor()
{
// Oppretter en tabell av farger, og legger inn noen farger
Qt::GlobalColor colorArray[7] = {Qt::red, Qt::green, Qt::cyan, Qt::magenta, Qt::yellow, Qt::gray, Qt::darkRed};
int temp;
// Om telleren har nådd maks, nullstill teller og return. Hvis ikke, øk teller og return
if (colorCounter == 6) {
temp = colorCounter;
colorCounter = 0;
return colorArray[temp];
}
else {
temp = colorCounter;
colorCounter ++;
return colorArray[temp];
}
}