Skip to content

Commit

Permalink
Merge branch 'main' into messages
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonMrt committed Nov 16, 2024
2 parents f5eba63 + bbf31a4 commit 8898ba1
Show file tree
Hide file tree
Showing 14 changed files with 374 additions and 18 deletions.
2 changes: 1 addition & 1 deletion common_utils/common_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ bool make_string(const string& src,

int find_separator(const std::string& src,
char& separator) {
std::vector<char> ignored_chars = {'+', '-', 'e', '.', '\r', ','};
std::vector<char> ignored_chars = {'+', '-', 'e', '.', '\r', ',', 'E'};
std::set<char> unique_chars;
bool is_service_char = false;
bool is_dot_present = false;
Expand Down
76 changes: 70 additions & 6 deletions davis_one/davis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,41 @@ const char kNoFileFoundedPage[] = R"(<!DOCTYPE html>

extern const char kWarningIcon[] = R"davis_delimeter(<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 156.262 144.407"><path d="M-109.166 7.227a2 2 0 0 0-.406.046c-3.195.03-6.176 1.695-7.785 4.483l-31.25 54.127-31.25 54.127h.002c-3.42 5.922 1.017 13.609 7.855 13.61h125.002c6.839-.001 11.277-7.688 7.857-13.61l-31.25-54.127-31.252-54.127c-1.465-2.539-4.079-4.164-6.978-4.45a2 2 0 0 0-.445-.077h-.004a2.006 2.006 0 0 0-.094-.002z" color="#000" style="solid-color:#000" transform="translate(186.615 2.437) scale(.99073)"/><path fill="#fff" d="M-109.165 9.227a7.081 7.081 0 0 0-6.46 3.529l-31.25 54.127-31.25 54.127c-2.674 4.631.777 10.609 6.126 10.61h125.002c5.348-.001 8.8-5.979 6.125-10.61l-31.25-54.127-31.252-54.127a7.079 7.079 0 0 0-5.79-3.53h-.001z" color="#000" style="solid-color:#000" transform="translate(186.615 2.437) scale(.99073)"/><path d="M-109.26 11.225a5.073 5.073 0 0 0-4.632 2.53l-31.25 54.128-31.25 54.127c-1.953 3.381.488 7.609 4.393 7.61h125.002c3.905-.001 6.345-4.229 4.392-7.61l-31.25-54.127-31.252-54.127a5.073 5.073 0 0 0-4.152-2.531z" color="#000" style="solid-color:#000" transform="translate(186.615 2.437) scale(.99073)"/><path fill="#fc0" d="M140.053 125.83H16.209L47.17 72.204l30.961-53.626 30.961 53.626z"/><g transform="translate(.295 2.437) scale(.99073)"><circle cx="78.564" cy="111.117" r="8.817"/><path d="M78.564 42.955a8.817 8.817 0 0 0-8.818 8.816l3.156 37.461a5.662 5.662 0 0 0 11.325 0l3.154-37.46a8.817 8.817 0 0 0-8.817-8.817z"/></g></svg>)davis_delimeter";



extern const char kHtmlDateTimeModel[] = R"davis_delimeter(
<head>
<script src="%1" charset="utf-8"></script>
</head>
<body><div style = "display: flex;
align-items:center;height:100%; width:100%;background:#dddfd4;
justify-content: center;"><div style="height:95%; aspect-ratio: 1/1;"
id="gd"></div></div>
<script>
var data = [
{
x: [%2],
y: [%3],
type: 'scatter'
}
];
var config = {
editable: true,
showLink: true,
plotlyServerURL: "https://chart-studio.plotly.com"
};
Plotly.newPlot('gd', data);
</script>
</body>
)davis_delimeter";



// *INDENT-ON*

} // namespace dvs end
Expand Down Expand Up @@ -584,7 +619,7 @@ bool make_string(const string& src,

int find_separator(const std::string& src,
char& separator) {
std::vector<char> ignored_chars = {'+', '-', 'e', '.', '\r', ','};
std::vector<char> ignored_chars = {'+', '-', 'e', '.', '\r', ',', 'E'};
std::set<char> unique_chars;
bool is_service_char = false;
bool is_dot_present = false;
Expand Down Expand Up @@ -678,7 +713,7 @@ bool checkThatSizesAreTheSame(const vector<vector<double>>& values) {
for (size_t i = 0; i < values.size(); ++i) {

if (values[i].size() != size) {
showMatrixSizesAreNotTheSame(i);
showMatrixSizesAreNotTheSame();
return false;
}
}
Expand Down Expand Up @@ -981,13 +1016,42 @@ void showReportFileEmpty() {
}


void showMatrixSizesAreNotTheSame(int badRow) {
void showMatrixSizesAreNotTheSame() {

string text;
text.append("Rows have different sizes in matrix. Check the row № ").append(std::to_string(badRow + 1));
showReportPage("Rows sizes are not the same",
kWarningIcon,
text);
"Rows have different sizes in matrix");
}

void showDateTimeChart(const string& date_time_values,
const vector<double>& yValues) {

string out;
string davis_dir;
#ifdef _WIN32
davis_dir = "\\davis_htmls";
#elif __linux__
davis_dir = "/davis_htmls";
#endif
vector<string>args {ARGS_DATE_TIME_PAGE_SIZE, ""};
args[ARG_JS_NAME] = kPlotlyJsName;
args[ARG_DATE_TIME_VALUES] = date_time_values;

std::string values;
for (size_t i = 0; i < yValues.size(); ++i) {
std::string value = std::to_string(yValues[i]);
values.append(value);
if (i != yValues.size() - 1) {
values.append(",");
}
}

args[ARG_Y_DATE_TIME_VALUES] = values;
make_string(kHtmlDateTimeModel, args, out);
saveStringToFile(kReportPagePath, out);
openFileBySystem(kReportPagePath);


}


Expand Down
14 changes: 14 additions & 0 deletions davis_one/davis.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ enum ARGS_REPORT_PAGE_INDEX {
ARGS_REPORT_PAGE_SIZE
};

enum ARGS_DATE_TIME_PAGE_INDEX {
ARG_JS_NAME, //%1
ARG_DATE_TIME_VALUES, //%2
ARG_Y_DATE_TIME_VALUES, //%3
// ADD NEW ENUM BEFORE THIS COMMENT
ARGS_DATE_TIME_PAGE_SIZE
};


extern const char kHtmlModel[];
extern const char kColorMapDefaultPart[];
Expand All @@ -160,6 +168,9 @@ extern const char kNoFileFoundedPage[];

extern const char kWarningIcon[];

extern const char kHtmlDateTimeModel[];


} // namespace dvs end

namespace dvs {
Expand Down Expand Up @@ -332,6 +343,9 @@ void showReportFileEmpty();

void showMatrixSizesAreNotTheSame(int badRow);

void showDateTimeChart(const string& date_time_values,
const vector<double>& yValues);


} // namespace dvs end

Expand Down
2 changes: 2 additions & 0 deletions gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ set(PROJECT_SOURCES
about_window.cpp
davis_gui.ui
about_window.ui
json_utils.h
json_utils.cpp
)

qt5_add_resources(PROJECT_SOURCES res.qrc)
Expand Down
6 changes: 6 additions & 0 deletions gui/date_time_formats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
"dd/MM/yyyy hh:mm",
"yyyy/MM/dd hh:mm:ss",
"yyyy.MM.dd_hh:mm:ss",
"yyyy/MM/dd hh_mm_ss"
]
88 changes: 82 additions & 6 deletions gui/davis_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include "QFileDialog"
#include "QTextStream"
#include <QClipboard>
#include <QJsonArray>
#include "json_utils.h"
#include "QDateTime"
#include <QProcess>

DavisGUI::DavisGUI(QWidget* parent)
: QMainWindow(parent)
Expand Down Expand Up @@ -84,7 +88,9 @@ void DavisGUI::pasteTextAdded() {
QString clipboardText = clipboard->text();
qDebug() << clipboardText;
QStringList lines = clipboardText.split(QRegExp("[\r\n]+"));
readPlotText(lines);
if (checkDateTimeVariant(lines) == false) {
readPlotText(lines);
};
}

void DavisGUI::readPlotText(QStringList& str_lines) {
Expand Down Expand Up @@ -119,7 +125,7 @@ void DavisGUI::readPlotText(QStringList& str_lines) {
}

if (data.empty()) {
qDebug() << "Empty file";
dvs::showReportFileEmpty();
return;
}

Expand Down Expand Up @@ -150,6 +156,60 @@ void DavisGUI::readPlotText(QStringList& str_lines) {
}
}

bool DavisGUI::checkDateTimeVariant(const QStringList& lines) {

QJsonArray jarr;
if(jsn::getJsonArrayFromFile("date_time_formats.json", jarr)==false){
jsn::getJsonArrayFromFile(":/date_time_formats.json", jarr);
}
qDebug() << jarr;
QString dates;
std::vector<double> values;

for (int i = 0; i < lines.size(); ++i) {
QString test = lines[i];
for (int j = 0; j < jarr.size(); ++j) {
int template_time_stamp_size = jarr[j].toString().size();
QString template_time_stamp = jarr[j].toString();
if (test.size() < template_time_stamp_size + 1) {
continue;
}
QString separator = QString(test[template_time_stamp_size]);
QString substr = test.mid(0, template_time_stamp_size);
QDateTime dt = QDateTime::fromString(substr, template_time_stamp);
if (dt.isValid()) {
//2013-10-04 22:23:00
qDebug() << dt.toString("yyyy-MM-dd hh:mm:ss");
dates.append("'");
dates.append(dt.toString("yyyy-MM-dd hh:mm:ss"));
dates.append("'");
if (i < lines.size() - 1) {
dates.append(",");
}

auto values_list = test.split(separator);
if (values_list.size() != 2) {
continue;
}
double value = values_list[1].toDouble();
qDebug()<<value;
values.emplace_back(value);

}
}
}
if (values.size() == 0)
return false;
qDebug() << "check sizes: " << lines.size() << values.size();
if (lines.size() != values.size()) {
return false;
}
dvs::showDateTimeChart(dates.toStdString(), values);
return true;


}

void DavisGUI::dragEnterEvent(QDragEnterEvent* event) {
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
Expand All @@ -159,18 +219,32 @@ void DavisGUI::dragEnterEvent(QDragEnterEvent* event) {
}

void DavisGUI::dropEvent(QDropEvent* event) {
QString filePath = event->mimeData()->urls().first().toLocalFile();
QList<QUrl> file_list = event->mimeData()->urls();
if(file_list.size()>1){
qDebug()<<"file list size: "<<file_list.size();
return;
}
QString filePath = file_list.first().toLocalFile();
QFileInfo info(filePath);
qDebug() << "---file path--->" << filePath;
if (info.exists()) {
qDebug() << "exist";
QFile file(filePath);
QTextStream ts(&file);
ts.setCodec("UTF-8");
if (file.open(QIODevice::ReadWrite) == false) {
dvs::showReportFileNotFounded();
return;
};

QString suffix = info.suffix();
QStringList suffixes = {"jpg","bmp","png","svg","mp4","json"};
for(int i=0;i<suffixes.size();++i){
if(suffix == suffixes[i]){
QProcess process;
process.startDetached("cmd.exe", QStringList() << "/C" << filePath);
return;
}
}

QString line;
QStringList str_lines;
while (ts.readLineInto(&line)) {
Expand All @@ -181,7 +255,9 @@ void DavisGUI::dropEvent(QDropEvent* event) {
return;
}
file.close();
readPlotText(str_lines);
if (checkDateTimeVariant(str_lines) == false) {
readPlotText(str_lines);
};
} else {
qDebug() << "not exist";
dvs::showReportFileNotFounded();
Expand Down
1 change: 1 addition & 0 deletions gui/davis_gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class DavisGUI : public QMainWindow {
bool isAboutWindowShowed;
QAction* m_copy_paste_action;
void readPlotText(QStringList& str_lines);
bool checkDateTimeVariant(const QStringList& lines);

// QWidget interface
protected:
Expand Down
82 changes: 82 additions & 0 deletions gui/json_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "json_utils.h"

#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonValue>
#include <QFile>
#include <QDebug>
#include <QGuiApplication>
#include <QDir>


namespace jsn {

bool getJsonObjectFromFile(const QString& path,
QJsonObject& object) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "File can't be opened!" << path;
return false;
};
QByteArray data = file.readAll();
QJsonParseError errorPtr;
object = QJsonDocument::fromJson(data, &errorPtr).object();
if (object.isEmpty()) {
qDebug() << "JSON IS EMPTY: " << errorPtr.errorString();
return false;
}
file.close();

return true;
}

bool getJsonArrayFromFile(const QString& path,
QJsonArray& object) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "File can't be opened!" << path;
return false;
};
QByteArray data = file.readAll();
QJsonParseError errorPtr;
object = QJsonDocument::fromJson(data, &errorPtr).array();
if (object.isEmpty()) {
qDebug() << "JSON IS EMPTY: " << errorPtr.errorString();
return false;
}
file.close();
return true;
}

bool saveJsonObjectToFile(const QString& path,
const QJsonObject& json_object,
QJsonDocument::JsonFormat format) {
QFile file(path);
if (!file.open(QIODevice::WriteOnly))
return false;
auto json_doc = QJsonDocument(json_object).toJson(format);
auto result = file.write(json_doc);
file.close();
if (result == -1)
return false;
else
return true;
}

bool saveJsonArrayToFile(const QString& path,
const QJsonArray& json_object,
QJsonDocument::JsonFormat format) {
QFile file(path);
if (!file.open(QIODevice::WriteOnly))
return false;
auto json_doc = QJsonDocument(json_object).toJson(format);
auto result = file.write(json_doc);
file.close();
if (result == -1)
return false;
else
return true;
}

} // end jsn namespace
Loading

0 comments on commit 8898ba1

Please sign in to comment.