Skip to content

Commit

Permalink
Add CSV table preview and CSV highlighting (#680)
Browse files Browse the repository at this point in the history
* Add CSV table preview and CSV highlighting

* add toggles for enabling CSV table view and syntax highlighting
  • Loading branch information
Bartimaeus- authored Jul 10, 2022
1 parent ac15b7c commit 146d744
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 6 deletions.
2 changes: 2 additions & 0 deletions 3rdparty/QCodeEditor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(INCLUDE_FILES
include/internal/QHighlightBlockRule.hpp
include/internal/QCodeEditor.hpp
include/internal/QCXXHighlighter.hpp
include/internal/QCSVHighlighter.hpp
include/internal/QLineNumberArea.hpp
include/internal/QStyleSyntaxHighlighter.hpp
include/internal/QSyntaxStyle.hpp
Expand All @@ -47,6 +48,7 @@ set(SOURCE_FILES
src/internal/QCodeEditor.cpp
src/internal/QLineNumberArea.cpp
src/internal/QCXXHighlighter.cpp
src/internal/QCSVHighlighter.cpp
src/internal/QSyntaxStyle.cpp
src/internal/QStyleSyntaxHighlighter.cpp
src/internal/QGLSLCompleter.cpp
Expand Down
3 changes: 3 additions & 0 deletions 3rdparty/QCodeEditor/include/QCSVHighlighter
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#include <internal/QCSVHighlighter.hpp>
39 changes: 39 additions & 0 deletions 3rdparty/QCodeEditor/include/internal/QCSVHighlighter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

// QCodeEditor
#include <QStyleSyntaxHighlighter> // Required for inheritance
#include <QHighlightRule>
#include <QHighlightBlockRule>

// Qt
#include <QRegularExpression>
#include <QVector>
#include <QMap>
#include <QChar>

class QSyntaxStyle;

/**
* @brief Class, that describes C++ code
* highlighter.
*/
class QCSVHighlighter : public QStyleSyntaxHighlighter
{
Q_OBJECT
public:

/**
* @brief Constructor.
* @param document Pointer to document.
*/
explicit QCSVHighlighter(QTextDocument* document=nullptr);

QChar delimiter = QChar(',');

protected:
void highlightBlock(const QString& text) override;

private:
QRegularExpression m_delimiter;

};
38 changes: 38 additions & 0 deletions 3rdparty/QCodeEditor/src/internal/QCSVHighlighter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// QCodeEditor
#include <QCSVHighlighter>
#include <QSyntaxStyle>
#include <QLanguage>

// Qt
#include <QFile>


QCSVHighlighter::QCSVHighlighter(QTextDocument* document) :
QStyleSyntaxHighlighter(document),
m_delimiter(QRegularExpression(","))
{
Q_INIT_RESOURCE(qcodeeditor_resources);

}

void QCSVHighlighter::highlightBlock(const QString& text)
{
{ // Checking for require
QRegularExpression m_delimiter;
//m_delimiter = QRegularExpression(delimiter);
m_delimiter.setPattern(delimiter);
auto matchIterator = m_delimiter.globalMatch(text);

while (matchIterator.hasNext())
{
auto match = matchIterator.next();

setFormat(
match.capturedStart(),
match.capturedLength(),
syntaxStyle()->getFormat("VisualWhitespace")
);

}
}
}
61 changes: 61 additions & 0 deletions plotjuggler_plugins/DataLoadCSV/dataload_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <QPushButton>
#include "QSyntaxStyle"


#include <QStandardItemModel>

const int TIME_INDEX_NOT_DEFINED = -2;
const int TIME_INDEX_GENERATED = -1;

Expand Down Expand Up @@ -87,6 +90,7 @@ DataLoadCSV::DataLoadCSV()
{
_extensions.push_back("csv");
_delimiter = ',';
_csvHighlighter.delimiter = _delimiter;
// setup the dialog

_dialog = new QDialog();
Expand All @@ -113,6 +117,23 @@ DataLoadCSV::DataLoadCSV()
connect(_ui->checkBoxDateFormat, &QCheckBox::toggled, this,
[this](bool checked) { _ui->lineEditDateFormat->setEnabled(checked); });

connect(_ui->checkBoxSyntaxHilighting,&QCheckBox::toggled, this,
[this](bool checked) {
if(checked){
_ui->rawText->setHighlighter(&_csvHighlighter);
}
else{
_ui->rawText->setHighlighter(nullptr);//method for clearing the syntax highlighter as given in QCodeEditor example
}
});
_ui->tableView->setVisible(false);


QSizePolicy sp_retain = _ui->tableView->sizePolicy();
sp_retain.setRetainSizeWhenHidden(true);
_ui->tableView->setSizePolicy(sp_retain);


_ui->splitter->setStretchFactor(0, 1);
_ui->splitter->setStretchFactor(1, 2);
}
Expand All @@ -132,6 +153,18 @@ void DataLoadCSV::parseHeader(QFile& file, std::vector<std::string>& column_name
{
file.open(QFile::ReadOnly);

QStandardItemModel *model = new QStandardItemModel;
_ui->tableView->setModel(model);
model->clear();

_csvHighlighter.delimiter = _delimiter;
//if(_ui->checkBoxSyntaxHilighting->isChecked()){
// _ui->rawText->setHighlighter(&_csvHighlighter);
//}
//else{
// _ui->rawText->setHighlighter(nullptr);//method for clearing the syntax highlighter as given in QCodeEditor example
//}

column_names.clear();
_ui->listWidgetSeries->clear();

Expand Down Expand Up @@ -222,18 +255,36 @@ void DataLoadCSV::parseHeader(QFile& file, std::vector<std::string>& column_name
}
}

int x = 0;
for (const auto& name : column_names)
{
_ui->listWidgetSeries->addItem(QString::fromStdString(name));

if(_ui->checkBoxTableView->isChecked()){
QStandardItem *item = new QStandardItem(QString::fromStdString(name));
model->setItem(0, x, item);
x +=1;
}
}


int linecount = 1;
while (!inA.atEnd())
{
auto line = inA.readLine();
if (linecount++ < 100)
{
preview_lines += line + "\n";
if(_ui->checkBoxTableView->isChecked()){
// parse the read line into separate pieces(tokens) with "," as the delimiter
QStringList lineToken = line.split(_delimiter);
// load parsed data to model accordingly
for (int j = 0; j < lineToken.size(); j++) {
QString value = lineToken.at(j);
QStandardItem *item = new QStandardItem(value);
model->setItem(linecount-1, j, item);
}
}
}
else
{
Expand All @@ -248,6 +299,9 @@ void DataLoadCSV::parseHeader(QFile& file, std::vector<std::string>& column_name
int DataLoadCSV::launchDialog(QFile& file, std::vector<std::string>* column_names)
{
column_names->clear();
_ui->checkBoxSyntaxHilighting->setChecked(false);
_ui->checkBoxTableView->setChecked(false);
_ui->tabWidget->setCurrentIndex(0);

QSettings settings;
_dialog->restoreGeometry(settings.value("DataLoadCSV.geometry").toByteArray());
Expand Down Expand Up @@ -319,8 +373,15 @@ int DataLoadCSV::launchDialog(QFile& file, std::vector<std::string>* column_name
_delimiter = ' ';
break;
}
_csvHighlighter.delimiter = _delimiter;
parseHeader(file, *column_names);
});
QObject::connect(_ui->checkBoxTableView, &QCheckBox::toggled, context,
[&](bool checked) {
_ui->tableView->setVisible(checked);
parseHeader(file, *column_names);

});

// parse the header once and launch the dialog
parseHeader(file, *column_names);
Expand Down
3 changes: 3 additions & 0 deletions plotjuggler_plugins/DataLoadCSV/dataload_csv.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QtPlugin>
#include "PlotJuggler/dataloader_base.h"
#include "ui_dataload_csv.h"
#include "QCSVHighlighter"

using namespace PJ;

Expand Down Expand Up @@ -44,6 +45,8 @@ class DataLoadCSV : public DataLoader

QChar _delimiter;

QCSVHighlighter _csvHighlighter;

QDialog* _dialog;
Ui::DialogCSV* _ui;

Expand Down
90 changes: 84 additions & 6 deletions plotjuggler_plugins/DataLoadCSV/dataload_csv.ui
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,91 @@
</widget>
</item>
<item>
<widget class="QCodeEditor" name="rawText">
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
<property name="readOnly">
<bool>true</bool>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Raw Text</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_9">
<item>
<widget class="QCheckBox" name="checkBoxSyntaxHilighting">
<property name="toolTip">
<string>Enable syntax highlighting for csv preview. May be slow for large files with many fields</string>
</property>
<property name="text">
<string>Enable Syntax Highlighting</string>
</property>
</widget>
</item>
<item>
<widget class="QCodeEditor" name="rawText">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>1</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_4">
<attribute name="title">
<string>Table View</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_7">
<item>
<widget class="QCheckBox" name="checkBoxTableView">
<property name="toolTip">
<string>Enable the table view. May be slow for files with many fields</string>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>Enable Table View</string>
</property>
</widget>
</item>
<item>
<widget class="QTableView" name="tableView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>1</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="dragDropOverwriteMode">
<bool>false</bool>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<attribute name="verticalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
Expand Down

0 comments on commit 146d744

Please sign in to comment.