]> Some of my projects - graph.git/commitdiff
Sortable views and PMC start.
authorAPTX <marek321@gmail.com>
Wed, 14 Dec 2011 00:05:36 +0000 (01:05 +0100)
committerAPTX <marek321@gmail.com>
Wed, 14 Dec 2011 00:05:36 +0000 (01:05 +0100)
graph.cpp
graph.pro
mainwindow.cpp
mainwindow.h
mainwindow.ui
pmc.cpp [new file with mode: 0644]
pmc.h [new file with mode: 0644]

index 3931c6a16dd07a3339deda3c78b439c50122fe29..8c632024f60985c9e2f36f91f65f1384ada0d282 100644 (file)
--- 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);
index 4ae7defe3698ba81357d1c4ba873780215e17a12..c5902b070a12b48710b9e7ab3779a0a0c0b5314c 100644 (file)
--- 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
 
index 330372e9c6a807b15503cdf4ed4256dc676e45d4..ab5f00a1709d0602c362b63baf430300ed5fa7fa 100644 (file)
@@ -1,6 +1,7 @@
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
 
+#include <QSortFilterProxyModel>
 #include <QItemEditorFactory>
 #include <QFile>
 #include <QFileDialog>
@@ -10,6 +11,8 @@
 #include "node.h"
 #include "edge.h"
 
+#include "pmc.h"
+
 #include <QDebug>
 
 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<ColorListEditor>();
+       setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
+
+//     QItemEditorCreatorBase *colorListCreator = new QStandardItemEditorCreator<ColorListEditor>();
 //     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"));
+}
index 70f46491d1b2af0f4d47fe2573f281fecf28b2e8..67c0983d15436018c099f3f57f00984ad62d7b03 100644 (file)
@@ -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;
 
index d8f9f9a9b67c14c9cdf4192cd379b4eac57e0ac3..d60cdba551d1224902aec6dc55f507ccaf570d8f 100644 (file)
@@ -6,13 +6,16 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>781</width>
-    <height>598</height>
+    <width>865</width>
+    <height>756</height>
    </rect>
   </property>
   <property name="windowTitle">
    <string>PMC</string>
   </property>
+  <property name="dockNestingEnabled">
+   <bool>true</bool>
+  </property>
   <widget class="QWidget" name="centralWidget">
    <layout class="QVBoxLayout" name="verticalLayout">
     <property name="margin">
@@ -32,7 +35,7 @@
     <rect>
      <x>0</x>
      <y>0</y>
-     <width>781</width>
+     <width>865</width>
      <height>21</height>
     </rect>
    </property>
     </layout>
    </widget>
   </widget>
+  <widget class="QDockWidget" name="dockWidget">
+   <property name="minimumSize">
+    <size>
+     <width>80</width>
+     <height>195</height>
+    </size>
+   </property>
+   <property name="windowTitle">
+    <string>PMC</string>
+   </property>
+   <attribute name="dockWidgetArea">
+    <number>8</number>
+   </attribute>
+   <widget class="QWidget" name="dockWidgetContents_3">
+    <widget class="QPushButton" name="pmcStep">
+     <property name="geometry">
+      <rect>
+       <x>70</x>
+       <y>0</y>
+       <width>75</width>
+       <height>23</height>
+      </rect>
+     </property>
+     <property name="text">
+      <string>Step</string>
+     </property>
+    </widget>
+   </widget>
+  </widget>
   <action name="actionEditMode">
    <property name="checkable">
     <bool>true</bool>
diff --git a/pmc.cpp b/pmc.cpp
new file mode 100644 (file)
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 (file)
index 0000000..85b7844
--- /dev/null
+++ b/pmc.h
@@ -0,0 +1,28 @@
+#ifndef PMC_H
+#define PMC_H
+
+#include <QObject>
+
+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