From: APTX Date: Wed, 14 Dec 2011 00:05:36 +0000 (+0100) Subject: Sortable views and PMC start. X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=d9ded39139992bd4bd3c61d38e5c2a49646077e9;p=graph.git Sortable views and PMC start. --- diff --git a/graph.cpp b/graph.cpp index 3931c6a..8c63202 100644 --- a/graph.cpp +++ b/graph.cpp @@ -81,6 +81,9 @@ Edge *Graph::addEdge(Node *start, Node *end, int weight, const QColor &color) { if (start == 0 || end == 0) return 0; + if (start == end) + return 0; + Edge *edge = new Edge(start, end); edge->setWeight(weight); edge->setColor(color); diff --git a/graph.pro b/graph.pro index 4ae7def..c5902b0 100644 --- a/graph.pro +++ b/graph.pro @@ -17,7 +17,8 @@ SOURCES += main.cpp\ edge.cpp \ nodemodel.cpp \ edgemodel.cpp \ - colorlisteditor.cpp + colorlisteditor.cpp \ + pmc.cpp HEADERS += mainwindow.h \ graph.h \ @@ -26,7 +27,8 @@ HEADERS += mainwindow.h \ defines.h \ nodemodel.h \ edgemodel.h \ - colorlisteditor.h + colorlisteditor.h \ + pmc.h FORMS += mainwindow.ui diff --git a/mainwindow.cpp b/mainwindow.cpp index 330372e..ab5f00a 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,6 +1,7 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include #include #include #include @@ -10,6 +11,8 @@ #include "node.h" #include "edge.h" +#include "pmc.h" + #include MainWindow::MainWindow(QWidget *parent) : @@ -26,10 +29,13 @@ MainWindow::MainWindow(QWidget *parent) : ui->actionEditMode->setChecked(true); ui->modeToolBar->addActions(modes->actions()); - QItemEditorCreatorBase *colorListCreator = new QStandardItemEditorCreator(); + setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea); + +// QItemEditorCreatorBase *colorListCreator = new QStandardItemEditorCreator(); // QItemEditorFactory::defaultFactory()->registerEditor(QVariant::Color, colorListCreator); graph = new Graph(this); + pmc = new PMC(graph, this); connect(modes, SIGNAL(triggered(QAction*)), this, SLOT(modeChanged(QAction*))); @@ -44,9 +50,18 @@ MainWindow::MainWindow(QWidget *parent) : e = graph->addEdge(n1, n2); e->setColor(QColor(0,0,255)); + nodeProxy = new QSortFilterProxyModel(this); + edgeProxy = new QSortFilterProxyModel(this); + + nodeProxy->setSourceModel(graph->nodeModel()); + edgeProxy->setSourceModel(graph->edgeModel()); + ui->graphView->setScene(graph); - ui->nodeView->setModel(graph->nodeModel()); - ui->edgeView->setModel(graph->edgeModel()); + ui->nodeView->setModel(nodeProxy); + ui->edgeView->setModel(edgeProxy); + + ui->nodeView->setSortingEnabled(true); + ui->edgeView->setSortingEnabled(true); } MainWindow::~MainWindow() @@ -161,3 +176,13 @@ void MainWindow::on_actionClearAll_triggered() { graph->clear(); } + + + +void MainWindow::on_pmcStep_clicked() +{ + if (pmc->step()) + ui->statusBar->showMessage(tr("Last Step complete")); + else + ui->statusBar->showMessage(tr("Step complete")); +} diff --git a/mainwindow.h b/mainwindow.h index 70f4649..67c0983 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -8,6 +8,9 @@ class MainWindow; } class Graph; +class QSortFilterProxyModel; +class PMC; + class QActionGroup; class MainWindow : public QMainWindow @@ -32,10 +35,16 @@ private slots: void on_actionGenerateGraph_triggered(); void on_actionClearAll_triggered(); + void on_pmcStep_clicked(); + private: Ui::MainWindow *ui; + QSortFilterProxyModel *nodeProxy; + QSortFilterProxyModel *edgeProxy; + Graph *graph; + PMC *pmc; QActionGroup *modes; diff --git a/mainwindow.ui b/mainwindow.ui index d8f9f9a..d60cdba 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -6,13 +6,16 @@ 0 0 - 781 - 598 + 865 + 756 PMC + + true + @@ -32,7 +35,7 @@ 0 0 - 781 + 865 21 @@ -107,6 +110,35 @@ + + + + 80 + 195 + + + + PMC + + + 8 + + + + + + 70 + 0 + 75 + 23 + + + + Step + + + + true diff --git a/pmc.cpp b/pmc.cpp new file mode 100644 index 0000000..8505781 --- /dev/null +++ b/pmc.cpp @@ -0,0 +1,62 @@ +#include "pmc.h" + +#include "graph.h" +#include "node.h" +#include "edge.h" + +PMC::PMC(Graph *g, QObject *parent) : + QObject(parent), m_graph(g) +{ +} + +bool PMC::step() +{ + Node *next = findHighestDegree(); + + if (!next) + return true; + + + Edge *e = next->incomingEdges().first(); +// Node *otherNode = e->startNode(); + m_graph->removeEdge(e); + + if (next->incomingEdges().count() == m()) + next->setColor(Qt::green); + else + next->setColor(Qt::yellow); + next->update(); + return false; +} + +Node *PMC::findHighestDegree() const +{ + Node *ret = 0; + foreach(Node *n, m_graph->nodes()) + { + if (n->incomingEdges().count() <= m()) + { + if (n->incomingEdges().count() < m()) + { + n->setColor(Qt::red); + n->update(); + } + continue; + } + if (ret == 0) + ret = n; + if (n->incomingEdges().count() > n->outgoingEdges().count()) + ret = n; + } + return ret; +} + +int PMC::m() const +{ + return (m_graph->nodes().count() - 1) / 2; +} + +int PMC::p() const +{ + return m_graph->nodes().count() - 2 * m(); +} diff --git a/pmc.h b/pmc.h new file mode 100644 index 0000000..85b7844 --- /dev/null +++ b/pmc.h @@ -0,0 +1,28 @@ +#ifndef PMC_H +#define PMC_H + +#include + +class Graph; +class Node; + +class PMC : public QObject +{ + Q_OBJECT +public: + explicit PMC(Graph *g, QObject *parent = 0); + +signals: + +public slots: + bool step(); + +private: + Node *findHighestDegree() const; + int m() const; + int p() const; + + Graph *m_graph; +}; + +#endif // PMC_H