Browse Source

Updates in repository handling

- Major changes in UniversalListModel
- Fixed last repository load
- Fixed existing repository load
Alexey Edelev 5 years ago
parent
commit
ddea471214
11 changed files with 265 additions and 170 deletions
  1. 4 2
      CuteGit.pro
  2. 15 6
      githandler.cpp
  3. 1 14
      graphlistmodel.h
  4. 5 2
      qml/InitialWizard.qml
  5. 18 138
      qml/MainView.qml
  6. 148 0
      qml/RepositoryView.qml
  7. 1 0
      resources.qrc
  8. 3 0
      settings.cpp
  9. 41 8
      universallistmodel.h
  10. 6 0
      universallistmodelbase.cpp
  11. 23 0
      universallistmodelbase.h

+ 4 - 2
CuteGit.pro

@@ -42,7 +42,8 @@ SOURCES += \
     gitconsole.cpp \
     colorhandler.cpp \
     diffmodel.cpp \
-    settings.cpp
+    settings.cpp \
+    universallistmodelbase.cpp
 
 HEADERS += \
     githandler.h \
@@ -69,7 +70,8 @@ HEADERS += \
     gitconsole.h \
     colorhandler.h \
     diffmodel.h \
-    settings.h
+    settings.h \
+    universallistmodelbase.h
 
 RESOURCES += \
     resources.qrc

+ 15 - 6
githandler.cpp

@@ -65,16 +65,24 @@ void GitHandler::open(const QUrl &url, bool activate)
 void GitHandler::open(const QString &path, bool activate)
 {
     qDebug() << "path" << path;
-    git_buf root = {0,0,0};
+    git_buf root = {0, 0, 0};
     if(git_repository_discover(&root, path.toUtf8().data(), 0, NULL) != 0) {
         qDebug() << lastError();
         return;
     }
 
-    GitRepository* repo = new GitRepository(QString::fromUtf8(root.ptr, root.size));
+    QString rootStr = QString::fromUtf8(root.ptr, root.size);
+    GitRepository* repo = new GitRepository(rootStr);
+
+    if (!m_repositories->findByProperty("path", QVariant::fromValue(repo->path())).isNull()) {
+        qDebug() << "Repository is already in list";
+        delete repo;
+        return;
+    }
+
     Settings::instance()->add(repo);
     m_repositories->addRepository(repo);
-    if(activate) {
+    if (activate) {
         setActiveRepo(repo);
     }
 }
@@ -87,12 +95,12 @@ void GitHandler::activateRepository(int i)
 
 void GitHandler::setActiveRepo(GitRepository* repo)
 {
-    if(repo == nullptr || !repo->isValid()) {
+    if (repo == nullptr || !repo->isValid()) {
         qDebug() << lastError();
         return;
     }
 
-    if(m_activeRepo) {
+    if (m_activeRepo) {
         disconnect(m_activeRepoWatcher, &QFileSystemWatcher::directoryChanged, m_activeRepo, &GitRepository::readBranches);
         disconnect(m_activeRepoWatcher, &QFileSystemWatcher::directoryChanged, m_activeRepo, &GitRepository::readRemotes);
         disconnect(m_activeRepoWatcher, &QFileSystemWatcher::directoryChanged, m_activeRepo, &GitRepository::readTags);
@@ -269,5 +277,6 @@ void GitHandler::loadCachedRepos()
     foreach (QString repoPath, cachedRepos) {
         open(repoPath, false);
     }
-//    m_repositories->ac(m_repositories->indexOf(m_activeRepo));
+    QPointer<GitRepository> repo = m_repositories->findByProperty("id", QVariant::fromValue<QString>(activeRepo));
+    setActiveRepo(repo);
 }

+ 1 - 14
graphlistmodel.h

@@ -8,25 +8,12 @@
 class GraphListModel : public UniversalListModel<GraphPoint>
 {
     Q_OBJECT
-    Q_PROPERTY(int count READ count NOTIFY countChanged)
-
 public:
     GraphListModel(QObject* parent = 0);
 
-    QList<QPointer<GraphPoint> > container() const {
-        return m_container;
-    }
-
     Q_INVOKABLE GraphPoint* at(int i) const {
-        return m_container.at(i).data();
+        return UniversalListModel::at(i);
     }
-
-    int count() const {
-        return m_container.count();
-    }
-
-signals:
-    void countChanged(int count);
 };
 
 #endif // GRAPHLISTMODEL_H

+ 5 - 2
qml/InitialWizard.qml

@@ -4,14 +4,17 @@ import QtQuick.Controls 1.4
 import QtQuick.Controls.Styles 1.4
 
 FocusScope {
+    anchors.fill: parent
     Rectangle {
         anchors.fill: parent
     }
 
     Text {
+        anchors.centerIn: parent
         textFormat: Text.RichText
-        font.pointSize: 10
-        text: qsTr("Hi!<\br> Seems you started CuteGit for the first time. So lets <a href=\"#open\">open</a> one of your repository.");
+        color: "#bbbbbb"
+        font.pointSize: 14
+        text: qsTr("Hi!<\br> Seems you started CuteGit for the first time. So lets <a href=\"#open\"><font color=\"#666666\">open</font></a> one of your repository.");
         onLinkActivated: {
             if(link == "#open") {
                 repoOpenDialog.open()

+ 18 - 138
qml/MainView.qml

@@ -7,148 +7,28 @@ FocusScope {
     property var commitsForDiff: null
     property bool controlActive: false
     focus: true
-    TopBar {
-        id: topBar
-        onCloseClicked: {
-            _handler.diffReset()
-            commitPlane.commit = null
-            commitList.state = "full"
-            commitList.activeCommit = null
-        }
-        closeVisible: commitPlane.diff != null
-    }
 
-    Rectangle {
-        id: bg
-        color: "#eeeeee"
-        anchors.fill: commitList
+    Loader {
+        id: mainLoader
+        anchors.fill: parent
     }
 
-    CommitList {
-        id: commitList
-        anchors.top: topBar.bottom
-        anchors.bottom: consoleContol.top
-        anchors.left: parent.left
-
-        commitsModel: _handler.commits
-        graphModel: _handler.graph
-        onCommitClicked: {
-            if(commit == null) {
-                commitPlane.commit = null
-                _handler.diff()
-                commitList.state = "commitsOnly"
-                return;
-            }
-
-            if(commit.diff === null) {
-                commitPlane.commit = null
-                _handler.diffReset()
-                commitList.state = "full"
-                return
-            }
-
-            commitList.state = "commitsOnly"
-            if(!root.controlActive) {
-                commitPlane.commit = commit
-                root.commitsForDiff=[]
-            } else {
-                console.log("root.controlActive: " + root.controlActive)
-                if(root.commitsForDiff === null) {
-                    root.commitsForDiff = new Array(0);
-                }
-
-                console.log("Length" + root.commitsForDiff.length)
-                root.commitsForDiff.push(commit)
-                if(root.commitsForDiff.length === 2) {
-                    commitPlane.commit = root.commitsForDiff[1]
-                    _handler.diff(root.commitsForDiff[0], root.commitsForDiff[1])
-                    root.commitsForDiff=[]
-                }
+    states: [
+        State {
+            name: "initial"
+            when: _handler.repositories.count <=0
+            PropertyChanges {
+                target: mainLoader
+                source: "InitialWizard.qml"
             }
-        }
-    }
-
-    CommitPlane {
-        id: commitPlane
-        anchors.left: commitList.right
-        anchors.right: parent.right
-        anchors.top: topBar.bottom
-        anchors.bottom: consoleContol.top
-        Binding {
-            target: commitPlane
-            property: "diff"
-            value: _handler.activeDiff
-        }
-    }
-
-    Rectangle {
-        id: dimmingPlane
-        anchors.fill: commitList
-        MouseArea {
-            anchors.fill: parent
-        }
-        color: "black"
-        opacity: _handler.isBusy ? 0.4 : 0.0
-        visible: opacity > 0
-        Behavior on opacity {
-            NumberAnimation {
-                duration: 500
+        },
+        State {
+            name: "main"
+            when: _handler.repositories.count > 0
+            PropertyChanges {
+                target: mainLoader
+                source: "RepositoryView.qml"
             }
         }
-    }
-
-    ConsoleControl {
-        id: consoleContol
-        anchors.left: parent.left
-        anchors.right: parent.right
-        anchors.bottom: parent.bottom
-    }
-
-    Connections {
-        target: _handler
-        onActiveRepoChanged: {
-            commitPlane.diff = null
-            commitPlane.commit = null
-        }
-    }
-
-    Keys.onPressed: {
-        event.accepted = true
-        switch(event.key) {
-        case Qt.Key_Control:
-            root.controlActive = true
-            console.log("control pressed")
-            break
-        case Qt.Key_F4:
-            consoleContol.state = consoleContol.state === "closed" ? "opened" : "closed"
-            break
-        case Qt.Key_G://Found the G point. Let's open console using this action.
-            //TODO: Later has to be part of Console C++ class to read settings
-            consoleContol.state = "opened"
-            _handler.console.requestAutocomplete("g");
-            break
-        default:
-            event.accepted = false
-        }
-    }
-
-    Keys.onReleased: {
-        if(event.key === Qt.Key_Control) {
-            root.controlActive = false
-            console.log("control released");
-        }
-    }
-
-    onActiveFocusChanged: {
-        root.controlActive = false
-        console.log("control released");
-    }
-
-    Tooltip {
-    }
-
-    InitialWizard {
-        visible: _handler.repositories.count <= 0
-        anchors.fill: parent
-    }
+    ]
 }

+ 148 - 0
qml/RepositoryView.qml

@@ -0,0 +1,148 @@
+import QtQuick 2.0
+import QtQuick.Controls 1.4
+import org.semlanik.cutegit 1.0
+
+FocusScope {
+    id: root
+    anchors.fill: parent
+    TopBar {
+        id: topBar
+        onCloseClicked: {
+            _handler.diffReset()
+            commitPlane.commit = null
+            commitList.state = "full"
+            commitList.activeCommit = null
+        }
+        closeVisible: commitPlane.diff != null
+    }
+
+    Rectangle {
+        id: bg
+        color: "#eeeeee"
+        anchors.fill: commitList
+    }
+
+    CommitList {
+        id: commitList
+        anchors.top: topBar.bottom
+        anchors.bottom: consoleContol.top
+        anchors.left: parent.left
+
+        commitsModel: _handler.commits
+        graphModel: _handler.graph
+        onCommitClicked: {
+            if(commit == null) {
+                commitPlane.commit = null
+                _handler.diff()
+                commitList.state = "commitsOnly"
+                return;
+            }
+
+            if(commit.diff === null) {
+                commitPlane.commit = null
+                _handler.diffReset()
+                commitList.state = "full"
+                return
+            }
+
+            commitList.state = "commitsOnly"
+            if(!root.controlActive) {
+                commitPlane.commit = commit
+                root.commitsForDiff=[]
+            } else {
+                console.log("root.controlActive: " + root.controlActive)
+                if(root.commitsForDiff === null) {
+                    root.commitsForDiff = new Array(0);
+                }
+
+                console.log("Length" + root.commitsForDiff.length)
+                root.commitsForDiff.push(commit)
+                if(root.commitsForDiff.length === 2) {
+                    commitPlane.commit = root.commitsForDiff[1]
+                    _handler.diff(root.commitsForDiff[0], root.commitsForDiff[1])
+                    root.commitsForDiff=[]
+                }
+            }
+        }
+    }
+
+    CommitPlane {
+        id: commitPlane
+        anchors.left: commitList.right
+        anchors.right: parent.right
+        anchors.top: topBar.bottom
+        anchors.bottom: consoleContol.top
+        Binding {
+            target: commitPlane
+            property: "diff"
+            value: _handler.activeDiff
+        }
+    }
+
+    Rectangle {
+        id: dimmingPlane
+        anchors.fill: commitList
+        MouseArea {
+            anchors.fill: parent
+        }
+        color: "black"
+        opacity: _handler.isBusy ? 0.4 : 0.0
+        visible: opacity > 0
+        Behavior on opacity {
+            NumberAnimation {
+                duration: 500
+            }
+        }
+    }
+
+    ConsoleControl {
+        id: consoleContol
+        anchors.left: parent.left
+        anchors.right: parent.right
+        anchors.bottom: parent.bottom
+    }
+
+    Connections {
+        target: _handler
+        onActiveRepoChanged: {
+            commitPlane.diff = null
+            commitPlane.commit = null
+        }
+    }
+
+    Keys.onPressed: {
+        event.accepted = true
+        switch(event.key) {
+        case Qt.Key_Control:
+            root.controlActive = true
+            console.log("control pressed")
+            break
+        case Qt.Key_F4:
+            consoleContol.state = consoleContol.state === "closed" ? "opened" : "closed"
+            break
+        case Qt.Key_G://Found the G point. Let's open console using this action.
+            //TODO: Later has to be part of Console C++ class to read settings
+            consoleContol.state = "opened"
+            _handler.console.requestAutocomplete("g");
+            break
+        default:
+            event.accepted = false
+        }
+    }
+
+    Keys.onReleased: {
+        if(event.key === Qt.Key_Control) {
+            root.controlActive = false
+            console.log("control released");
+        }
+    }
+
+    onActiveFocusChanged: {
+        root.controlActive = false
+        console.log("control released");
+    }
+
+    Tooltip {
+    }
+
+}

+ 1 - 0
resources.qrc

@@ -54,5 +54,6 @@
         <file>images/question-mark-4-24.png</file>
         <file>images/question-mark-4-24_active.png</file>
         <file>qml/InitialWizard.qml</file>
+        <file>qml/RepositoryView.qml</file>
     </qresource>
 </RCC>

+ 3 - 0
settings.cpp

@@ -22,6 +22,9 @@ void Settings::load(QStringList &repos)
     QStringList groups = m_settings.childGroups();
 
     foreach (QString group, groups) {
+        if(group == GeneralGroupKey) {
+            continue;
+        }
         m_settings.beginGroup(group);
         QString path = m_settings.value(RepoPathKey).toString();
         QFileInfo fileInfo(path);

+ 41 - 8
universallistmodel.h

@@ -1,6 +1,6 @@
 #pragma once
 
-#include <QAbstractListModel>
+#include <universallistmodelbase.h>
 
 #include <QObject>
 #include <QList>
@@ -12,25 +12,29 @@
 #include <QDebug>
 
 
-/*! Universal list model is QObject-base list model abstraction.
+/*!
+ * \brief Universal list model is QObject-base list model abstraction.
  * It exposes all objects properties as data-roles.
- *
  */
 template <typename T>
-class UniversalListModel : public QAbstractListModel
+class UniversalListModel : public UniversalListModelBase
 {
 public:
-    UniversalListModel(QObject* parent = 0) : QAbstractListModel(parent) {}
+    UniversalListModel(QObject* parent = 0) : UniversalListModelBase(parent) {}
     ~UniversalListModel() {
         clear();
     }
 
-    int rowCount(const QModelIndex &parent) const {
+    int rowCount(const QModelIndex &parent) const override {
         Q_UNUSED(parent)
+        return count();
+    }
+
+    int count() const override {
         return m_container.count();
     }
 
-    QHash<int, QByteArray> roleNames() const {
+    QHash<int, QByteArray> roleNames() const override {
         if(s_roleNames.isEmpty()) {
             int propertyCount = T::staticMetaObject.propertyCount();
             for(int i = 1; i < propertyCount; i++) {
@@ -41,7 +45,7 @@ public:
         return s_roleNames;
     }
 
-    QVariant data(const QModelIndex &index, int role) const
+    QVariant data(const QModelIndex &index, int role) const override
     {
         int row = index.row();
 
@@ -73,6 +77,7 @@ public:
         }
         beginInsertRows(QModelIndex(), m_container.count(), m_container.count());
         m_container.append(QPointer<T>(value));
+        emit countChanged();
         endInsertRows();
         return m_container.count() - 1;
     }
@@ -93,6 +98,7 @@ public:
         }
         beginInsertRows(QModelIndex(), 0, 0);
         m_container.prepend(QPointer<T>(value));
+        emit countChanged();
         endInsertRows();
         return 0;
     }
@@ -109,6 +115,7 @@ public:
         if(valueIndex >= 0) {
             beginRemoveRows(QModelIndex(), valueIndex, valueIndex);
             m_container.removeAt(valueIndex);
+            emit countChanged();
             endRemoveRows();
         }
     }
@@ -122,6 +129,7 @@ public:
         beginResetModel();
         clear();
         m_container = container;
+        emit countChanged();
         endResetModel();
     }
 
@@ -144,10 +152,35 @@ public:
         return m_container.indexOf(value);
     }
 
+    /*!
+     * \brief findByProperty method finds item in internal container property of that is provided value
+     * \param propertyName Latin1 name of looking property
+     * \param value property of corresponded type inside QVariant container
+     * \return
+     */
+    QPointer<T> findByProperty(const char* propertyName, const QVariant& value) const {
+        auto iter = std::find_if(m_container.begin(), m_container.end(), [=](const QPointer<T> &item) -> bool {
+            return item->property(propertyName) == value;
+        });
+
+        if(iter != m_container.end()) {
+            return *iter;
+        }
+        return QPointer<T>();
+    }
+
+    /*!
+     * \brief container returns internal container
+     * \return
+     */
+    QList<QPointer<T> > container() const {
+        return m_container;
+    }
 
 protected:
     void clear() {
         m_container.clear();
+        emit countChanged();
     }
 
     QList<QPointer<T> > m_container;

+ 6 - 0
universallistmodelbase.cpp

@@ -0,0 +1,6 @@
+#include "universallistmodelbase.h"
+
+UniversalListModelBase::UniversalListModelBase(QObject *parent) : QAbstractListModel(parent)
+{
+
+}

+ 23 - 0
universallistmodelbase.h

@@ -0,0 +1,23 @@
+#pragma once
+
+#include <QAbstractListModel>
+/*!
+ * \brief The UniversalListModelBase class to make prossible properties definition for UniversalListModel
+ * This class should not be used as is, but leaves this possibility.
+ */
+class UniversalListModelBase : public QAbstractListModel
+{
+    Q_OBJECT
+    Q_PROPERTY(int count READ count NOTIFY countChanged)
+public:
+    explicit UniversalListModelBase(QObject *parent = nullptr);
+
+    /*!
+     * \brief count property that declares row count of UniversalListModel
+     * \return
+     */
+    virtual int count() const = 0;
+
+signals:
+    void countChanged();
+};