Jelajahi Sumber

Update drawing mechanism

Alexey Edelev 8 tahun lalu
induk
melakukan
383bd60df1
4 mengubah file dengan 92 tambahan dan 18 penghapusan
  1. 33 9
      commitgraph.cpp
  2. 7 0
      graphpoint.cpp
  3. 7 4
      graphpoint.h
  4. 45 5
      qml/MainView.qml

+ 33 - 9
commitgraph.cpp

@@ -36,9 +36,33 @@ void CommitGraph::addHead(const GitOid &oid)
 
     git_revwalk_free(walk);
 
+    QList<GitOid> openedBranches;
     qDebug() << "Update Y coordinate after head added";
     for(int i = 0; i < m_sortedPoints.count(); i++) {
-        static_cast<GraphPoint*>(m_sortedPoints.at(i))->setY(i);
+        GraphPoint* point = static_cast<GraphPoint*>(m_sortedPoints.at(i));
+        point->setY(m_sortedPoints.count() - i - 1);
+        GitCommit *commit = GitCommit::fromOid(point->oid());
+        git_commit* commitRaw = nullptr;
+        int parentCount = git_commit_parentcount(commit->raw());
+        int minX = point->x();
+        for(int j = 0; j < parentCount; j++) {
+            git_commit_parent(&commitRaw, commit->raw(), j);
+            GitOid oidParent(git_commit_id(commitRaw), oid.repository());
+            GraphPoint* parentPoint = m_points.value(oidParent);
+
+            int x = parentPoint->x() + parentPoint->childPointsCount();
+
+            qDebug() << "MinX = " << minX << " x:" << x;
+            if(parentPoint->childPoints().indexOf(point) < 0) {
+                minX = x;
+//                if(x > minX) {
+//                }
+                point->setX(minX);
+            }
+            parentPoint->addChildPoint(point);
+        }
+        qDebug() << "New commit: " << point->oid().toString() << point->x() << point->y();
+        point->setX(minX);
     }
 }
 
@@ -56,7 +80,7 @@ void CommitGraph::findParents(GitCommit* commit)
             break;
         }
 
-        qDebug() << "Add commit to reverselist" << parentOid.toString();
+//        qDebug() << "Add commit to reverselist" << parentOid.toString();
         commitRaw = nullptr;
         git_commit_parent(&commitRaw, commit->raw(), 0);
     }
@@ -85,19 +109,19 @@ void CommitGraph::addCommits(QList<GitOid>& reversList)
 
         point = m_points.value(childIter, nullptr);
         if(point == nullptr) {
-            int x = parentPoint->x() + parentPoint->childPointsCount();
-            point = new GraphPoint(childIter, x, 0, m_color, this);
-            parentPoint->addChildPoint(point);
-
+            int parentPosition = m_sortedPoints.indexOf(parentPoint);
+//            int x = parentPoint->x() + parentPoint->childPointsCount();
+//            point = new GraphPoint(childIter, x, 0, m_color, this);
+            point = new GraphPoint(childIter, 0, 0, m_color, this);
             m_points.insert(point->oid(), point);
+//            parentPoint->addChildPoint(point);
 
             //Ordered commits
-            int parentPosition = m_sortedPoints.indexOf(parentPoint);
             if(parentPosition >= 0) {
                 m_sortedPoints.insert(parentPosition + 1, point);
             }
-            qDebug() << "New commit: " << point->oid().toString() << point->x() << point->y();
-            qDebug() << "New commit: " << parentPoint->oid().toString() << parentPoint->x() << parentPoint->y();
+//            qDebug() << "New commit: " << point->oid().toString() << point->x() << point->y();
+//            qDebug() << "New commit parent: " << parentPoint->oid().toString() << parentPoint->x() << parentPoint->y();
         }
     }
 }

+ 7 - 0
graphpoint.cpp

@@ -50,3 +50,10 @@ void GraphPoint::setColor(const QString& color)
     emit colorChanged(color);
 }
 
+void GraphPoint::addChildPoint(GraphPoint* point)
+{
+    if(m_childPoints.indexOf(point) < 0) {
+        m_childPoints.append(point);
+    }
+}
+

+ 7 - 4
graphpoint.h

@@ -11,6 +11,7 @@ class GraphPoint : public QObject
     Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged)
     Q_PROPERTY(int y READ y WRITE setY NOTIFY yChanged)
     Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
+    Q_PROPERTY(QString sha1 READ sha1 CONSTANT)
     Q_PROPERTY(QList<QObject*> childPoints READ childPoints CONSTANT)
 
 public:
@@ -38,16 +39,18 @@ public:
         return m_commitOid;
     }
 
-    int childPointsCount() const
+    QString sha1() const
     {
-        return m_childPoints.count();
+        return m_commitOid.toString();
     }
 
-    void addChildPoint(GraphPoint* point)
+    int childPointsCount() const
     {
-        m_childPoints.append(point);
+        return m_childPoints.count();
     }
 
+    void addChildPoint(GraphPoint* point);
+
     QList<QObject*> childPoints() const
     {
         return m_childPoints;

+ 45 - 5
qml/MainView.qml

@@ -34,12 +34,35 @@ Item {
         anchors.right: parent.right
         contentWidth: innerItem.width
         contentHeight: innerItem.height
+        Column {
+            width: parent.width
+            spacing: 20
+            Repeater {
+                model: _handler.graph.points
+                Rectangle {
+                    width: parent.width
+                    height: innerItem.elementHeight
+                    color: textSelector.containsMouse ? "#9999ff" : "#009999ff"
+                    Text {
+                        anchors.fill: parent
+                        verticalAlignment: Text.AlignVCenter
+                        horizontalAlignment: Text.AlignRight
+                        text: model.modelData.sha1
+                    }
+                    MouseArea {
+                        id: textSelector
+                        anchors.fill: parent
+                        hoverEnabled: true
+                    }
+                }
+            }
+        }
         Canvas {
             id: innerItem
             width: root.width/2
             property int elementWidth: 20
             property int elementHeight: 20
-            height: _handler.graph.points.length*(elementWidth + 10)
+            height: _handler.graph.points.length*(elementWidth + 20)
             onPaint: {
                 var ctx = getContext("2d")
                 for(var i = 0; i < _handler.graph.points.length; i++) {
@@ -47,7 +70,7 @@ Item {
 
                     ctx.beginPath()
                     ctx.fillStyle = "#"+point.color
-                    ctx.roundedRect(point.x*(elementWidth + 10), point.y*(elementHeight + 10), elementWidth, elementHeight, elementWidth, elementHeight)
+                    ctx.roundedRect(point.x*(elementWidth + 20), point.y*(elementHeight + 20), elementWidth, elementHeight, elementWidth, elementHeight)
                     ctx.fill()
                     ctx.closePath()
 
@@ -56,10 +79,27 @@ Item {
                     for(var j = 0; j < childPoints.length; j++) {
                         var childPoint = childPoints[j]
                         ctx.beginPath()
-                        ctx.strokeStyle = "#"+point.color
+                        ctx.strokeStyle = "#" + childPoint.color
                         ctx.lineWidth = 2
-                        ctx.moveTo(point.x*(elementWidth + 10) + elementWidth/2, point.y*(elementHeight + 10) + elementHeight/2)
-                        ctx.lineTo(childPoint.x*(elementWidth + 10) + elementWidth/2, childPoint.y*(elementHeight + 10) + elementHeight/2)
+                        if(point.x !== childPoint.x) {
+                            if(point.x < childPoint.x) {
+                                ctx.moveTo(point.x*(elementWidth + 20) + elementWidth/2, childPoint.y*(elementHeight + 20) + elementHeight + 20)
+                                ctx.bezierCurveTo(
+                                                  point.x*(elementWidth + 20) + elementWidth/2, childPoint.y*(elementHeight + 20) + elementHeight,
+                                                  childPoint.x*(elementWidth + 20) + elementWidth/2, childPoint.y*(elementHeight + 20) + elementHeight + 20 + elementHeight/2,
+                                                  childPoint.x*(elementWidth + 20) + elementWidth/2, childPoint.y*(elementHeight + 20) + elementHeight/2)
+                            } else {
+                                ctx.moveTo(point.x*(elementWidth + 20) + elementWidth/2, point.y*(elementHeight + 20) + elementHeight/2)
+                                ctx.lineTo(point.x*(elementWidth + 20) + elementWidth/2, childPoint.y*(elementHeight + 20) + elementHeight + 20 + elementHeight)
+                                ctx.bezierCurveTo(
+                                                  point.x*(elementWidth + 20) + elementWidth/2, childPoint.y*(elementHeight + 20) + elementHeight/2,
+                                                  childPoint.x*(elementWidth + 20) + elementWidth/2, childPoint.y*(elementHeight + 20) + elementHeight + 20  + elementHeight/2,
+                                                  childPoint.x*(elementWidth + 20) + elementWidth/2, childPoint.y*(elementHeight + 20) + elementHeight/2)
+                            }
+                        } else {
+                            ctx.moveTo(point.x*(elementWidth + 20) + elementWidth/2, point.y*(elementHeight + 20) + elementHeight/2)
+                            ctx.lineTo(childPoint.x*(elementWidth + 20) + elementWidth/2, childPoint.y*(elementHeight + 20) + elementHeight/2)
+                        }
                         ctx.stroke()
                         ctx.closePath()
                     }