Imported existing sources
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
#include "graphdialog.h"
|
||||
#include "ui_graphdialog.h"
|
||||
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlError>
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
|
||||
#include <QLineSeries>
|
||||
#include <QDateTimeAxis>
|
||||
#include <QValueAxis>
|
||||
|
||||
GraphDialog::GraphDialog(QSqlDatabase &database, QWidget *parent) :
|
||||
QDialog(parent),
|
||||
m_ui(std::make_unique<Ui::GraphDialog>()),
|
||||
m_database(database)
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
|
||||
QString sql;
|
||||
|
||||
if (m_database.driverName() == "QSQLITE")
|
||||
{
|
||||
sql = "SELECT "
|
||||
"`Timestamp`, "
|
||||
"COUNT(`Timestamp`) "
|
||||
"FROM "
|
||||
"`Logs` "
|
||||
"GROUP BY "
|
||||
"STRFTIME('%Y-%m-%d %H:00:00.000', `Timestamp`)";
|
||||
}
|
||||
else if (m_database.driverName() == "QMYSQL")
|
||||
{
|
||||
sql = "SELECT "
|
||||
"`Timestamp`, "
|
||||
"COUNT(`Timestamp`) "
|
||||
"FROM "
|
||||
"`Logs` "
|
||||
"GROUP BY "
|
||||
"DATE_FORMAT(`Timestamp`, '%Y-%m-%d %H:%i:00.000')";
|
||||
}
|
||||
else
|
||||
qFatal("unknown sql driver");
|
||||
|
||||
QSqlQuery query(sql, m_database);
|
||||
if (query.lastError().isValid())
|
||||
qCritical() << query.lastError().text();
|
||||
|
||||
auto chart = new QtCharts::QChart();
|
||||
chart->legend()->hide();
|
||||
chart->setTitle(tr("Charts-Test"));
|
||||
|
||||
auto series = new QtCharts::QLineSeries();
|
||||
chart->addSeries(series);
|
||||
|
||||
QDateTime minDt, maxDt;
|
||||
int maxCount{};
|
||||
|
||||
while (query.next())
|
||||
{
|
||||
const auto timestampStr = query.value(0).toString();
|
||||
qDebug() << timestampStr;
|
||||
|
||||
const auto timestamp = QDateTime::fromString(timestampStr, QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz"));
|
||||
Q_ASSERT(timestamp.isValid());
|
||||
|
||||
if (minDt.isNull() || timestamp < minDt)
|
||||
minDt = timestamp;
|
||||
|
||||
if (maxDt.isNull() || timestamp > maxDt)
|
||||
maxDt = timestamp;
|
||||
|
||||
const auto count = query.value(1).toInt();
|
||||
|
||||
if (count > maxCount)
|
||||
maxCount = count;
|
||||
|
||||
qDebug() << timestamp << count;
|
||||
|
||||
series->append(timestamp.toMSecsSinceEpoch(), count);
|
||||
}
|
||||
|
||||
qDebug() << minDt << maxDt;
|
||||
|
||||
auto axisX = new QtCharts::QDateTimeAxis;
|
||||
axisX->setRange(minDt, maxDt);
|
||||
axisX->setTickCount(20);
|
||||
axisX->setFormat("HH:mm:ss");
|
||||
axisX->setTitleText("Timestamp");
|
||||
chart->addAxis(axisX, Qt::AlignBottom);
|
||||
series->attachAxis(axisX);
|
||||
|
||||
auto axisY = new QtCharts::QValueAxis;
|
||||
axisY->setMax(maxCount);
|
||||
axisY->setLabelFormat("%i");
|
||||
axisY->setTitleText("Logs count");
|
||||
chart->addAxis(axisY, Qt::AlignLeft);
|
||||
series->attachAxis(axisY);
|
||||
|
||||
m_ui->chartView->setRenderHint(QPainter::Antialiasing);
|
||||
m_ui->chartView->setChart(chart);
|
||||
}
|
||||
|
||||
GraphDialog::~GraphDialog() = default;
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class QSqlDatabase;
|
||||
|
||||
namespace Ui { class GraphDialog; }
|
||||
|
||||
class GraphDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
GraphDialog(QSqlDatabase &database, QWidget *parent = nullptr);
|
||||
~GraphDialog() override;
|
||||
|
||||
private:
|
||||
const std::unique_ptr<Ui::GraphDialog> m_ui;
|
||||
|
||||
QSqlDatabase &m_database;
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>GraphDialog</class>
|
||||
<widget class="QDialog" name="GraphDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Dialog</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QtCharts::QChartView" name="chartView"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Close</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>QtCharts::QChartView</class>
|
||||
<extends>QGraphicsView</extends>
|
||||
<header>qchartview.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>GraphDialog</receiver>
|
||||
<slot>close()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>199</x>
|
||||
<y>278</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>199</x>
|
||||
<y>149</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "opendialog.h"
|
||||
#include "ui_opendialog.h"
|
||||
|
||||
#include <QMessageBox>
|
||||
#include <QStringBuilder>
|
||||
#include <QSqlError>
|
||||
|
||||
OpenDialog::OpenDialog(QWidget *parent) :
|
||||
QDialog(parent),
|
||||
m_ui(std::make_unique<Ui::OpenDialog>())
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
|
||||
connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &OpenDialog::submit);
|
||||
}
|
||||
|
||||
OpenDialog::~OpenDialog() = default;
|
||||
|
||||
QSqlDatabase OpenDialog::database()
|
||||
{
|
||||
return m_database;
|
||||
}
|
||||
|
||||
void OpenDialog::submit()
|
||||
{
|
||||
m_database = m_ui->databaseWidget->createConnection();
|
||||
|
||||
if (!m_database.open())
|
||||
{
|
||||
QMessageBox::warning(this, tr("Could not open database!"), tr("Could not open database!") % "\n\n" % m_database.lastError().text());
|
||||
m_database = {};
|
||||
return;
|
||||
}
|
||||
|
||||
accept();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <QDialog>
|
||||
#include <QSqlDatabase>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace Ui { class OpenDialog; }
|
||||
|
||||
class OpenDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit OpenDialog(QWidget *parent = nullptr);
|
||||
~OpenDialog() override;
|
||||
|
||||
QSqlDatabase database();
|
||||
|
||||
private slots:
|
||||
void submit();
|
||||
|
||||
private:
|
||||
const std::unique_ptr<Ui::OpenDialog> m_ui;
|
||||
|
||||
QSqlDatabase m_database;
|
||||
};
|
||||
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>OpenDialog</class>
|
||||
<widget class="QDialog" name="OpenDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Dialog</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,0">
|
||||
<item>
|
||||
<widget class="DatabaseWidget" name="databaseWidget" native="true"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>DatabaseWidget</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>widgets/databasewidget.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>OpenDialog</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>316</x>
|
||||
<y>260</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>286</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user