조성현

string-id graph read and construct

......@@ -17,7 +17,8 @@ NodeItem::NodeItem(int x, int y)
{
this->x = x;
this->y = y;
this->color = QColor(0, 0, 0); //R, G, B
//this->color = QColor(Qt::green); //R, G, B
this->color = QColor(Qt::green);
setZValue((x+y)%2);
setFlags(ItemIsSelectable | ItemIsMovable);
......@@ -26,13 +27,14 @@ NodeItem::NodeItem(int x, int y)
QRectF NodeItem::boundingRect() const
{
return QRectF(0, 0, 110, 70);
return QRectF(0, 0, NODE_SIZE, NODE_SIZE);
}
QPainterPath NodeItem::shape() const
{
QPainterPath path;
path.addRect(14, 14, 82, 42);
//path.addRect(14, 14, 82, 42);
path.addRect(0, 0, NODE_SIZE, NODE_SIZE);
return path;
}
......@@ -40,43 +42,51 @@ void NodeItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option
{
Q_UNUSED(widget);
//QColor fillColor = (option->state & QStyle::State_Selected) ? color.dark(150) : color;
//if (option->state & QStyle::State_MouseOver)
// fillColor = fillColor.light(125);
//const qreal& lod = option->levelOfDetailFromTransform(painter->worldTransform());
//if (lod < 0.2) {
// if (lod < 0.125) {
// painter->fillRect(QRectF(0, 0, 110, 70), fillColor);
// return;
// }
// QBrush b = painter->brush();
// painter->setBrush(fillColor);
// painter->drawRect(13, 13, 97, 57);
// painter->setBrush(b);
// return;
//}
//QPen oldPen = painter->pen();
//QPen pen = oldPen;
//int width = 0;
//if (option->state & QStyle::State_Selected)
// width += 2;
//pen.setWidth(width);
//QBrush b = painter->brush();
//painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));
//painter->drawRect(QRect(14, 14, 79, 39));
//painter->setBrush(b);
//if (lod >= 1) {
// painter->setPen(QPen(Qt::gray, 1));
// painter->drawLine(15, 54, 94, 54);
// painter->drawLine(94, 53, 94, 15);
// painter->setPen(QPen(Qt::black, 0));
//}
QColor fillColor = (option->state & QStyle::State_Selected) ? color.dark(150) : color;
if (option->state & QStyle::State_MouseOver)
fillColor = fillColor.light(125);
const qreal& lod = option->levelOfDetailFromTransform(painter->worldTransform());
if (lod < 0.2) {
if (lod < 0.125) {
painter->fillRect(QRectF(0, 0, 110, 70), fillColor);
return;
}
QBrush b = painter->brush();
painter->setBrush(fillColor);
painter->drawRect(13, 13, 97, 57);
painter->setBrush(b);
return;
}
QPen oldPen = painter->pen();
QPen pen = oldPen;
int width = 0;
if (option->state & QStyle::State_Selected)
width += 2;
pen.setWidth(width);
QBrush b = painter->brush();
painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));
painter->drawRect(QRect(14, 14, 79, 39));
painter->setBrush(b);
if (lod >= 1) {
painter->setPen(QPen(Qt::gray, 1));
painter->drawLine(15, 54, 94, 54);
painter->drawLine(94, 53, 94, 15);
painter->setPen(QPen(Qt::black, 0));
}
if (option->state & QStyle::State_MouseOver) fillColor = fillColor.light(125);
painter->setPen(QPen(Qt::black));
painter->setBrush(QBrush(fillColor));
painter->drawRect(0, 0, NODE_SIZE, NODE_SIZE);
//painter->fillRect(QRectF(0, 0, NODE_SIZE, NODE_SIZE), fillColor);
//painter->drawRect(0, 0, NODE_SIZE, NODE_SIZE);
// Draw text
//if (lod >= 2) {
......
......@@ -28,4 +28,4 @@ public:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
};
#endif // NODEITEM_H
\ No newline at end of file
#endif // NODEITEM_H
......
......@@ -60,9 +60,13 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<IncludePath>C:\boost\boost_1_63_0;$(IncludePath)</IncludePath>
<LibraryPath>C:\boost\boost_1_63_0\stage\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<IncludePath>C:\boost\boost_1_63_0;$(IncludePath)</IncludePath>
<LibraryPath>C:\boost\boost_1_63_0\stage\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
......@@ -84,7 +88,7 @@
<TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<SubSystem>Console</SubSystem>
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
<AdditionalLibraryDirectories>$(QTDIR)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation>
......@@ -101,7 +105,7 @@
<TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<SubSystem>Console</SubSystem>
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
<AdditionalLibraryDirectories>$(QTDIR)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation>
......@@ -117,7 +121,7 @@
<TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<SubSystem>Console</SubSystem>
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
<AdditionalLibraryDirectories>$(QTDIR)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>false</GenerateDebugInformation>
......@@ -133,7 +137,7 @@
<TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<SubSystem>Console</SubSystem>
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile>
<AdditionalLibraryDirectories>$(QTDIR)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>false</GenerateDebugInformation>
......
......@@ -14,22 +14,27 @@ PaperGraphWidget::PaperGraphWidget(QWidget *parent)
layout->addWidget(view);
setLayout(layout);
setWindowTitle(tr("Paper Graph Visualization"));
setWindowTitle(tr("dblp paper graph visualization"));
}
void PaperGraphWidget::print_graph(const Graph & graph)
{
//print graph
}
void PaperGraphWidget::initscene()
{
scene = new QGraphicsScene(this);
int x = 0, y;
for (int i=-11000; i<11000; i+=110) {
++x;
y = 0;
for (int j=-7000; j<7000; j+=70) {
++y;
QGraphicsItem *item = new NodeItem(x, y);
item->setPos(QPointF(i, j));
scene->addItem(item);
}
}
//int x = 0, y;
//for (int i=-11000; i<11000; i+=110) {
// ++x;
// y = 0;
// for (int j=-7000; j<7000; j+=70) {
// ++y;
// QGraphicsItem *item = new NodeItem(x, y);
// item->setPos(QPointF(i, j));
// scene->addItem(item);
// }
//}
}
......
......@@ -3,9 +3,42 @@
#include <QtWidgets/QWidget>
#include <QGraphicsScene>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/kamada_kawai_spring_layout.hpp>
#include <boost/graph/random_layout.hpp>
#include "ui_PaperGraphWidget.h"
//class QGraphicsScene;
using namespace boost;
enum vertex_position_t { vertex_position };
//enum vertex_type_t { vertex_type };
namespace boost {
BOOST_INSTALL_PROPERTY(vertex, position);
//BOOST_INSTALL_PROPERTY(vertex, type);
}
typedef square_topology<>::point_type point;
struct simple_edge {
int first, second;
};
typedef boost::property<vertex_index_t, int,
boost::property<vertex_name_t, std::string,
boost::property<vertex_position_t, point>>
> VertexProperties;
typedef adjacency_list<
listS, //outEdgeList
listS, //VertexList
undirectedS,
//vertex properties
//VertexProperties,
VertexProperties,
//edge properties
boost::property<edge_weight_t, double>
> Graph;
class PaperGraphWidget : public QWidget
{
......@@ -13,6 +46,7 @@ class PaperGraphWidget : public QWidget
public:
PaperGraphWidget(QWidget *parent = 0);
void print_graph(const Graph& graph);
private:
void initscene();
......
#include "PaperGraphWidget.h"
#include <QtWidgets/QApplication>
#include <QDebug>
#include <algorithm>
#include <exception>
#include <fstream>
#include <iterator>
#include <string>
#include <map>
#include <vector>
#include <boost/algorithm/string.hpp> //boost::split
//#include <boost/bind.hpp>
#include <boost/bimap.hpp>
using namespace std;
const string PAPER_FILENAME = "dblp-paper.txt";
Graph read_graph(ifstream& in) {
/**
* Parse Paper dataset
* - paper_key, [author_list], publish_year
* Column Delimiter: ||
* Author list Delimiter: &&
*/
std::string line;
vector<std::string> tokens;
vector<std::string> authors;
vector<pair<string, string>> edges;
typedef boost::bimap<string, int> bm_type;
bm_type node_ids;
vector<simple_edge> edges_indexes;
int node_cnt = 0;
qDebug() << "* graph reading start" << endl;
while (std::getline(in, line) && !line.empty()) {
boost::split(tokens, line, boost::is_any_of("||"), boost::token_compress_on);
boost::split(authors, tokens[1], boost::is_any_of("&&"), boost::token_compress_on);
const string& paper_key = tokens[0];
if (node_ids.left.find(paper_key) == node_ids.left.end()) {
//node_ids[paper_key] = node_cnt++;
node_ids.insert(bm_type::value_type(paper_key, node_cnt++));
//qDebug() << paper_key.c_str() << " ";
}
for (auto author : authors) {
edges.push_back(pair<string, string>(paper_key, author));
if (node_ids.left.find(author) == node_ids.left.end()) {
//node_ids[author] = node_cnt++;
node_ids.insert(bm_type::value_type(author, node_cnt++));
//qDebug() << author.c_str() << " ";
}
}
//debug
if (node_cnt > 100) break;
}
qDebug() << "* graph reading complete" << endl;
//std::sort(node_names.begin(), node_names.end());
//Make graph
//Graph --> defined in "PaperGraphWidget.h"
for (auto edge : edges) {
edges_indexes.push_back({
node_ids.left.find(edge.first)->get_right(),
node_ids.left.find(edge.second)->get_right()
});
}
Graph graph(edges_indexes.begin(), edges_indexes.end(), node_ids.size());
//print map
//for (auto it=node_ids.left.begin(), itend=node_ids.left.end();
// it!=itend; ++it) {
// qDebug() << it->first.c_str() << " " << it->second << endl;
//}
//set index property
typedef typename graph_traits<Graph>::edge_iterator edge_iterator;
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
vertex_iterator vi, vi_end;
int i = 0;
for (boost::tie(vi, vi_end)=vertices(graph); vi!=vi_end; ++vi) {
boost::put(vertex_index, graph, *vi, i);
boost::put(vertex_name, graph, *vi,
node_ids.right.find(i)->get_left());
//VertexProperties prop = VertexProperties(i,
// boost::property<vertex_name_t, std::string,
// boost::property<vertex_position_t, point>>());
++i;
}
//for (auto edge : edges) {
// //add edge
// //VertexProperties prop = VertexProperties()
// //add_edge(node_ids[edge.first], node_ids[edge.second],);
//}
//make graph layout
return graph;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
PaperGraphWidget w;
try {
ifstream fin(PAPER_FILENAME);
w.print_graph(read_graph(fin));
fin.close();
} catch (const std::exception& e) {
qDebug() << "Error: " << e.what() << endl;
return -1;
}
w.show();
return app.exec();
......