Skip to content

Commit

Permalink
Implement Visualize window (prototype)
Browse files Browse the repository at this point in the history
  • Loading branch information
kovben2004 committed Dec 30, 2024
1 parent 59d0594 commit d089a91
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 8 deletions.
9 changes: 7 additions & 2 deletions qt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.16)
project(bibref-qt LANGUAGES CXX C)

find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Concurrent)
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Concurrent SvgWidgets)

find_package(PkgConfig REQUIRED)
pkg_check_modules(sword REQUIRED sword)
Expand All @@ -17,6 +17,8 @@ set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost CONFIG COMPONENTS system filesystem)

pkg_check_modules(gvc REQUIRED libgvc)

if(MINGW)
find_package(ICU REQUIRED uc in)
find_package(LibLZMA REQUIRED)
Expand All @@ -26,7 +28,7 @@ endif(MINGW)

pkg_check_modules (readline REQUIRED readline)

include_directories(${sword_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${readline_INCLUDE_DIRS})
include_directories(${sword_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${readline_INCLUDE_DIRS} ${gvc_INCLUDE_DIRS})

# This works when Qt >= 6.3:
# qt_standard_project_setup()
Expand Down Expand Up @@ -69,6 +71,7 @@ qt_add_executable(bibref-qt
mainwindow.cpp mainwindow.h
statementwindow.cpp statementwindow.h
highlighter.cpp highlighter.h
visualizewindow.cpp visualizewindow.h
book.cpp book.h
books.cpp books.h
cli.cpp cli.h
Expand All @@ -89,8 +92,10 @@ target_link_libraries(bibref-qt PRIVATE
Qt6::Gui
Qt6::Widgets
Qt6::Concurrent
Qt6::SvgWidgets
${sword_LIBRARIES} ${Boost_LIBRARIES} ${readline_LIBRARIES}
${ICU_LIBRARIES} ${LIBLZMA_LIBRARIES} ${ZLIB_LIBRARIES} ${BZIP2_LIBRARIES}
${gvc_LIBRARIES}
)

# If not set, XDG_DATA_HOME is set to $HOME:
Expand Down
3 changes: 1 addition & 2 deletions qt/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "mainwindow.h"
#include "statementwindow.h"

#include "books.h"
#include "cli.h"
Expand All @@ -20,8 +21,6 @@

#include <cstdio>

#include "statementwindow.h"

using namespace std;

using namespace sword;
Expand Down
27 changes: 24 additions & 3 deletions qt/statementwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "statementwindow.h"
#include "visualizewindow.h"

#include <QtWidgets>
#include <iostream>
Expand Down Expand Up @@ -99,6 +100,9 @@ void StatementWindow::parse()

int infos = 0, warnings = 0, errors = 0;
QString details;
bool dmode = false;
graphviz_input = "";
bool diagram_defined = false;

for (auto l: statementAnalysis) {
if (l.find(": info: ")!=string::npos)
Expand All @@ -111,6 +115,16 @@ void StatementWindow::parse()
details += QString::fromStdString(l);
details += "\n";
}
if (dmode) {
graphviz_input += l + "\n";
}
if (l.find("diagram: graphviz: start")!=string::npos) {
dmode = true;
diagram_defined = true;
}
if (l.find("diagram: graphviz: end")!=string::npos) {
dmode = false;
}
}

QMessageBox msgBox;
Expand All @@ -137,7 +151,13 @@ void StatementWindow::parse()

void StatementWindow::showSvg()
{

#ifdef WITH_PBRST
auto vwindow = new VisualizeWindow(this, graphviz_input);
// vwindow->resize(600, 400);
vwindow->show();
vwindow->setWindowIcon(QIcon::fromTheme("emblem-photos"));
vwindow->setWindowTitle(tr("Visualize"));
#endif // WITH_PBRST
}

void StatementWindow::setupProveMenu()
Expand All @@ -148,8 +168,9 @@ void StatementWindow::setupProveMenu()

proveMenu->addAction(QIcon::fromTheme("tools-check-spelling"), tr("&Parse"), QKeySequence::Forward,
this, &StatementWindow::parse);
// proveMenu->addAction(QIcon::fromTheme("emblem-photos"), tr("&Visualize"), QKeySequence::Print,
// this, &StatementWindow::showSvg);
proveMenu->addAction(QIcon::fromTheme("emblem-photos"), tr("&Visualize"), QKeySequence::Print,
this, &StatementWindow::showSvg);

#endif // WITH_PBRST
}

Expand Down
7 changes: 6 additions & 1 deletion qt/statementwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

#include <QMainWindow>

#include <string>

using namespace std;

QT_BEGIN_NAMESPACE
class QTextEdit;
QT_END_NAMESPACE
Expand All @@ -30,7 +34,8 @@ public slots:

QTextEdit *editor;
Highlighter *highlighter;
};

string graphviz_input;
};

#endif
35 changes: 35 additions & 0 deletions qt/visualizewindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "visualizewindow.h"

#include <QtSvg>
#include <QSvgWidget>
#include <QtWidgets>

#include <cstdio>
#include <string>
#include <graphviz/cdt.h>
#include <graphviz/cgraph.h>
#include <graphviz/gvc.h>
#include <graphviz/gvplugin.h>

using namespace std;

VisualizeWindow::VisualizeWindow(QWidget *parent, string input)
: QMainWindow(parent)
{
this->setAttribute(::Qt::WA_DeleteOnClose);
tile = new QSvgWidget(this);
setCentralWidget(tile);
GVC_t *gvc= gvContext();
Agraph_t *g = agmemread((char*)input.c_str());
gvLayout(gvc, g, "dot");
#define MAX_SVG_LENGTH ((unsigned int) 200000)
char *svg = (char*) malloc(MAX_SVG_LENGTH);
unsigned int length;
gvRenderData(gvc, g, "svg", &svg, &length);
gvFreeLayout(gvc, g);
agclose(g);
(gvFreeContext(gvc));
string svg_s = string(svg);
tile->load(QByteArray::fromStdString(svg_s));
}

21 changes: 21 additions & 0 deletions qt/visualizewindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef VISUALIZEWINDOW_H
#define VISUALIZEWINDOW_H

#include <QMainWindow>
#include <QSVgWidget>
#include <string>

using namespace std;

class VisualizeWindow : public QMainWindow
{
Q_OBJECT

public:
VisualizeWindow(QWidget *parent, string input);

private:
QSvgWidget *tile;
};

#endif

0 comments on commit d089a91

Please sign in to comment.