Skip to content

Commit

Permalink
move generation of test data out of widget
Browse files Browse the repository at this point in the history
  • Loading branch information
sandsmark committed Mar 2, 2016
1 parent b1fcebe commit f0970f8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 54 deletions.
67 changes: 16 additions & 51 deletions hivewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,29 @@
#include <QElapsedTimer>
#include <QMouseEvent>

// For generating test data
QString createNode()
{
QString type;
int r = qrand() % 3;
int num = 0;
switch (r) {
case 0:
type = "marker %1";
num = qrand() % 75;
break;
case 1:
type = "program %1";
num = qrand() % 100;
break;
default:
type = "reduction %1";
num = qrand() % 50;
break;
}
return type.arg(num);
}

HiveWidget::HiveWidget(QWidget *parent)
: QOpenGLWidget(parent)
{
setWindowFlags(Qt::Dialog);
resize(1600, 1200);
setMouseTracking(true);
}

// Generate some test data to work on
for(int i = 0; i<75; i++) {
m_nodes.insert("markers", QString("marker %1").arg(i));
}
for(int i = 0; i<100; i++) {
m_nodes.insert("programs", QString("program %1").arg(i));
}
for(int i = 0; i<50; i++) {
m_nodes.insert("reductions", QString("reduction %1").arg(i));
}

QMap<QString, QString> nodeGroups;
for (const QString &group : m_nodes.keys()) {
for (const QString &node : m_nodes.values(group)) {
nodeGroups[node] = group;
}
}
HiveWidget::~HiveWidget()
{

for (int i=0; i<50; i++) {
QString nodeA = createNode();
QString nodeB = createNode();
if (m_edges.value(nodeA) != nodeB && nodeGroups[nodeA] != nodeGroups[nodeB]) {
m_edges.insert(nodeA, nodeB);
}
}
}

void HiveWidget::setNodes(const QMultiMap<QString, QString> &nodes)
{
m_nodes = nodes;
calculate();
update();
}

HiveWidget::~HiveWidget()
void HiveWidget::setEdges(const QMultiMap<QString, QString> &edges)
{

m_edges = edges;
calculate();
update();
}

void HiveWidget::paintEvent(QPaintEvent *)
Expand Down Expand Up @@ -156,6 +117,10 @@ void HiveWidget::calculate()
m_nodeColors.clear();
m_edgePaths.clear();

if (m_nodes.isEmpty() || m_edges.isEmpty()) {
return;
}

int maxGroupSize = 0;
const int numGroups = m_nodes.keys().count();

Expand Down
3 changes: 2 additions & 1 deletion hivewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class HiveWidget : public QOpenGLWidget
HiveWidget(QWidget *parent = 0);
~HiveWidget();

void setNodes(const QMap<QString, QList<QString>> nodes);
void setNodes(const QMultiMap<QString, QString> &nodes);
void setEdges(const QMultiMap<QString, QString> &edges);

protected:
virtual void paintEvent(QPaintEvent *);
Expand Down
59 changes: 57 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,70 @@
#include <QApplication>
#include <QSurfaceFormat>

// For generating test data
QString createNode()
{
QString type;
int r = qrand() % 3;
int num = 0;
switch (r) {
case 0:
type = "marker %1";
num = qrand() % 75;
break;
case 1:
type = "program %1";
num = qrand() % 100;
break;
default:
type = "reduction %1";
num = qrand() % 50;
break;
}
return type.arg(num);
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSurfaceFormat fmt;
fmt.setSamples(2);
QSurfaceFormat::setDefaultFormat(fmt);

HiveWidget w;
w.show();
// Generate some test data to work on
QMultiMap<QString, QString> nodes;
for(int i = 0; i<75; i++) {
nodes.insert("markers", QString("marker %1").arg(i));
}
for(int i = 0; i<100; i++) {
nodes.insert("programs", QString("program %1").arg(i));
}
for(int i = 0; i<50; i++) {
nodes.insert("reductions", QString("reduction %1").arg(i));
}

QMap<QString, QString> nodeGroups;
for (const QString &group : nodes.keys()) {
for (const QString &node : nodes.values(group)) {
nodeGroups[node] = group;
}
}

QMultiMap<QString, QString> edges;
for (int i=0; i<50; i++) {
QString nodeA = createNode();
QString nodeB = createNode();
if (edges.value(nodeA) != nodeB && nodeGroups[nodeA] != nodeGroups[nodeB]) {
edges.insert(nodeA, nodeB);
}
}

HiveWidget hiveWidget;
hiveWidget.setNodes(nodes);
hiveWidget.setEdges(edges);
hiveWidget.setWindowFlags(Qt::Dialog);
hiveWidget.resize(1600, 1200);
hiveWidget.show();

return a.exec();
}

0 comments on commit f0970f8

Please sign in to comment.