Skip to content

Commit

Permalink
GUI: Единый виджет для отображения данных.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmpas committed Apr 4, 2018
1 parent bf45d62 commit 91c7d26
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 155 deletions.
104 changes: 93 additions & 11 deletions src/gtool1cd/BlobViewer/blob_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include "ui_blob_viewer.h"
#include <Parse_tree.h>
#include "../models/skobka_tree_model.h"
#include "../models/v8catalog_tree_model.h"
#include "../QHexEdit/qhexedit.h"
#include "../models/stream_device.h"
#include <Table.h>

BlobViewer::BlobViewer(QWidget *parent) :
QWidget(parent),
Expand All @@ -17,27 +21,105 @@ BlobViewer::~BlobViewer()

void BlobViewer::setText(const QString &textData)
{
ui->tabWidget->removeTab(0);
ui->tabWidget->removeTab(0);
ui->tabWidget->removeTab(0);

QTextDocument *qd = new QTextDocument(textData);
qd->setDocumentLayout(new QPlainTextDocumentLayout(qd));
ui->plainTextEdit->setDocument(qd);

ui->textDataTab->setVisible(true);
ui->tabWidget->addTab(ui->textDataTab, tr("Текст"));
ui->tabWidget->setCurrentWidget(ui->textDataTab);
ui->blobDataTab->setVisible(false);

try {
tree* t = parse_1Ctext(textData.toStdString(), "");
if (t != nullptr) {
ui->treeView->setModel(new SkobkaTreeModel(t));
ui->parsedDataTab->setVisible(true);
ui->tabWidget->setCurrentWidget(ui->parsedDataTab);
if (textData.startsWith("{")) {
try {
tree* t = parse_1Ctext(textData.toStdString(), "");
if (t != nullptr) {
ui->treeView->setModel(new SkobkaTreeModel(t));
ui->tabWidget->addTab(ui->parsedDataTab, tr("Дерево"));
ui->tabWidget->setCurrentWidget(ui->parsedDataTab);
}
} catch (...) {
}
} catch (...) {
ui->parsedDataTab->setVisible(false);
}
}

void BlobViewer::setStream(TStream *stream)
// проверяет наличие UTF-8 BOM
bool is_text_stream(QIODevice *stream)
{
auto buf = stream->read(3);
if (buf.size() != 3) {
return false;
}
return buf[0] == '\xEF'
&& buf[1] == '\xBB'
&& buf[2] == '\xBF';
}

void BlobViewer::setStream(TStream *stream, const QString &rootName)
{
ui->tabWidget->removeTab(0);
ui->tabWidget->removeTab(0);
ui->tabWidget->removeTab(0);

auto cat = new V8Catalog(stream, false, true);
V8CatalogTreeModel *catalog_model = nullptr;
if (cat->isOpen() && cat->is_catalog()) {
catalog_model = new V8CatalogTreeModel(cat, rootName);
} else {
delete cat;
}

auto device = new StreamDevice(stream);

QString textData;
{
device->open(QIODevice::ReadOnly);
if (is_text_stream(device)) {
textData = QString(device->readAll());
}
}
device->close();

auto doc = QHexDocument::fromDevice(device);
ui->frame->setDocument(doc);

ui->tabWidget->addTab(ui->blobDataTab, tr("BLOB"));
ui->tabWidget->setCurrentWidget(ui->blobDataTab);

{
if (catalog_model) {
ui->tabWidget->addTab(ui->parsedDataTab, tr("Дерево"));
ui->tabWidget->setCurrentWidget(ui->parsedDataTab);
ui->treeView->setModel(catalog_model);
ui->treeView->expandAll();
return;
}

if (textData.size() != 0) {

ui->tabWidget->addTab(ui->textDataTab, tr("Текст"));
ui->tabWidget->setCurrentWidget(ui->textDataTab);

QTextDocument *qd = new QTextDocument(textData);
qd->setDocumentLayout(new QPlainTextDocumentLayout(qd));
ui->plainTextEdit->setDocument(qd);
ui->plainTextEdit->setDocumentTitle(rootName);

if (textData.startsWith("{")) {
try {
tree* t = parse_1Ctext(textData.toStdString(), "");
if (t != nullptr) {
ui->treeView->setModel(new SkobkaTreeModel(t));
ui->treeView->expandAll();
ui->tabWidget->addTab(ui->parsedDataTab, tr("Дерево"));
ui->tabWidget->setCurrentWidget(ui->parsedDataTab);
}
} catch (...) {
}
}
}
}

}
3 changes: 2 additions & 1 deletion src/gtool1cd/BlobViewer/blob_viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class BlobViewer : public QWidget

void setText(const QString &textData);

void setStream(TStream *stream);
void setStream(TStream *stream, const QString &rootName);

private:

Ui::BlobViewer *ui;
};

Expand Down
28 changes: 25 additions & 3 deletions src/gtool1cd/BlobViewer/blob_viewer.ui
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
Expand All @@ -31,8 +31,11 @@
</property>
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="tabPosition">
<enum>QTabWidget::South</enum>
</property>
<property name="currentIndex">
<number>0</number>
<number>2</number>
</property>
<property name="tabBarAutoHide">
<bool>true</bool>
Expand Down Expand Up @@ -91,12 +94,31 @@
<attribute name="title">
<string>BLOB</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_3"/>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QHexEdit" name="frame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QHexEdit</class>
<extends>QFrame</extends>
<header>QHexEdit/qhexedit.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
5 changes: 5 additions & 0 deletions src/gtool1cd/models/stream_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ void StreamDevice::close()
setOpenMode(NotOpen);
}

bool StreamDevice::isSequential() const
{
return false;
}

4 changes: 3 additions & 1 deletion src/gtool1cd/models/stream_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ class StreamDevice : public QIODevice
explicit StreamDevice(TStream *stream);
virtual ~StreamDevice();

bool isSequential() const override;
void close() override;

protected:

qint64 readData(char *data, qint64 maxlen) override;
qint64 writeData(const char *data, qint64 len) override;
qint64 size() const override;
qint64 pos() const override;
bool seek(qint64 pos) override;
void close() override;

private:
TStream *stream;
Expand Down
4 changes: 2 additions & 2 deletions src/gtool1cd/models/table_data_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const TableRecord *TableDataModel::getRecord(const QModelIndex &index) const

}

QIODevice *TableDataModel::getBlobStream(const QModelIndex &index) const
TStream *TableDataModel::getBlobStream(const QModelIndex &index) const
{
Field *f = table->get_field(index.column());
TableRecord *record = _index == nullptr
Expand All @@ -186,5 +186,5 @@ QIODevice *TableDataModel::getBlobStream(const QModelIndex &index) const
if (!record->try_store_blob_data(f, out, true)) {
return nullptr;
}
return new StreamDevice(out);
return out;
}
4 changes: 2 additions & 2 deletions src/gtool1cd/models/table_data_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <QAbstractItemModel>
#include <Table.h>
#include <TableIterator.h>
#include <QIODevice>
#include <TStream.hpp>

class TableDataModel : public QAbstractItemModel
{
Expand Down Expand Up @@ -33,7 +33,7 @@ class TableDataModel : public QAbstractItemModel

const TableRecord *getRecord(const QModelIndex &index) const;

QIODevice *getBlobStream(const QModelIndex &index) const;
TStream *getBlobStream(const QModelIndex &index) const;

private:
Table *table;
Expand Down
49 changes: 10 additions & 39 deletions src/gtool1cd/table_data_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ TableDataWindow::TableDataWindow(QWidget *parent, Table *table)
setWindowTitle(QString::fromStdString(table->get_name()));
ui->dataView->setModel(new TableDataModel(table));

hexedit = new QHexEdit();
auto layout = new QVBoxLayout();
layout->addWidget(hexedit);
ui->hexeditFrame->setLayout(layout);

QVector<Index*> indexes;
indexes.push_back(nullptr); // PK

Expand Down Expand Up @@ -138,55 +133,31 @@ void TableDataWindow::dataView_selection_changed(const QItemSelection &selection

auto data = ui->dataView->model()->data(index, Qt::EditRole);

QTextDocument *qd = new QTextDocument(data.toString());
qd->setDocumentLayout(new QPlainTextDocumentLayout(qd));
ui->plainTextEdit->setDocument(qd);
ui->plainTextEdit->setVisible(true);

ui->treeView->setVisible(false);
hexedit->setVisible(false);

if (model->isBlobValue(index)) {

auto cat = model->getCatalog(index);
if (cat != nullptr) {
QString catalog_name("{catalog}");
QString catalog_name("{catalog}");

if (table->find_field("FILENAME") != nullptr) {
if (table->find_field("FILENAME") != nullptr) {

auto rec = model->getRecord(index);
catalog_name = QString::fromStdString(rec->get_string("FILENAME"));
auto rec = model->getRecord(index);
catalog_name = QString::fromStdString(rec->get_string("FILENAME"));

} else if (table->find_field("EXTNAME") != nullptr) {
} else if (table->find_field("EXTNAME") != nullptr) {

auto rec = model->getRecord(index);
catalog_name = QString::fromStdString(rec->get_string("EXTNAME"));
auto rec = model->getRecord(index);
catalog_name = QString::fromStdString(rec->get_string("EXTNAME"));

}
ui->plainTextEdit->setVisible(false);
ui->treeView->setModel(new V8CatalogTreeModel(cat, catalog_name));
ui->treeView->setVisible(true);
ui->treeView->expandAll();
return;
}

auto stream = model->getBlobStream(index);

if (stream != nullptr) {
auto doc = QHexDocument::fromDevice(stream);
hexedit->setDocument(doc);
hexedit->setVisible(true);
ui->plainTextEdit->setVisible(false);
ui->dataWidget->setStream(stream, catalog_name);
return;
}
}

tree *data_tree = try_parse_tree(data);
if (data_tree != nullptr) {
ui->treeView->setModel(new SkobkaTreeModel(data_tree));
ui->treeView->setVisible(true);
ui->treeView->expandAll();
return;
}
ui->dataWidget->setText(data.toString());

}

Expand Down
Loading

0 comments on commit 91c7d26

Please sign in to comment.