فهرست منبع

[refactor] Changed parent type (now including JSonObjectEntry)
[refactor] Changed JSonObject hierarchy
[bugfix] Fix #1 scroll issue on objects

isundil 9 سال پیش
والد
کامیت
7ace1c1b3c
5فایلهای تغییر یافته به همراه139 افزوده شده و 67 حذف شده
  1. 5 5
      include/jsonElement.hh
  2. 67 35
      src/curseOutput.cpp
  3. 18 24
      src/jsonElement.cpp
  4. 4 2
      src/jsonObject.cpp
  5. 45 1
      src/jsonObjectEntry.cpp

+ 5 - 5
include/jsonElement.hh

@@ -7,21 +7,21 @@ class JSonContainer;
 class JSonElement
 {
     public:
-        JSonElement(JSonContainer *parent);
+        JSonElement(JSonElement *parent);
         virtual ~JSonElement();
 
         virtual std::string stringify() const =0;
         unsigned int getLevel() const;
-        JSonContainer *getParent();
-        const JSonContainer *getParent() const;
+        JSonElement *getParent();
+        const JSonElement *getParent() const;
 
-        void setParent(JSonContainer *parent);
+        void setParent(JSonElement *parent);
 
         const JSonElement *findPrev() const;
         const JSonElement *findNext() const;
 
     private:
         JSonElement();
-        JSonContainer *parent;
+        JSonElement *parent;
 };
 

+ 67 - 35
src/curseOutput.cpp

@@ -75,8 +75,7 @@ bool CurseOutput::redraw()
     select_up = select_down = nullptr;
     selectFound = selectIsLast = selectIsFirst = false;
     getScreenSize(screenSize, cursor);
-    cursor.first = 0;
-    cursor.second = 0;
+    cursor.first = cursor.second = 0;
     clear();
     result = redraw(cursor, screenSize, data, dynamic_cast<const JSonContainer *> (data));
     if (!result && !select_down)
@@ -151,35 +150,50 @@ bool CurseOutput::writeContent(std::pair<int, int> &cursor, const std::pair<unsi
     cursor.first += indentLevel /2;
     for (std::list<JSonElement *>::const_iterator i = item->cbegin(); i != item->cend(); ++i)
     {
-        bool isObject = (dynamic_cast<const JSonObject *>(*i) != nullptr);
         if (containerIsObject)
         {
             JSonObjectEntry *ent = (JSonObjectEntry*) *i;
+            bool isContainer = (dynamic_cast<const JSonContainer *>(**ent) != nullptr);
             std::string key = ent->stringify();
-            checkSelection(**ent, (JSonContainer*) item, cursor);
-            if (collapsed.find((const JSonContainer*)(**ent)) != collapsed.cend())
+            checkSelection(ent, (JSonContainer*) item, cursor);
+            if (isContainer && collapsed.find((const JSonContainer*)(**ent)) != collapsed.cend())
             {
-                if (isObject)
-                    cursor.second += write(cursor.first, cursor.second, key + ": { ... }", maxSize.first, selection == **ent);
+                if (dynamic_cast<const JSonObject *>(**ent))
+                    cursor.second += write(cursor.first, cursor.second, key + ": { ... }", maxSize.first, selection == ent);
                 else
-                    cursor.second += write(cursor.first, cursor.second, key + ": [ ... ]", maxSize.first, selection == **ent);
+                    cursor.second += write(cursor.first, cursor.second, key + ": [ ... ]", maxSize.first, selection == ent);
+                if (cursor.second - topleft > 0 && (unsigned)(cursor.second - topleft) > maxSize.second -1)
+                        return false;
+            }
+            else if (!isContainer)
+            {
+                cursor.second += write(cursor.first, cursor.second, key + ": " +((**ent)->stringify()), maxSize.first, selection == ent);
+                if (cursor.second - topleft > 0 && (unsigned)(cursor.second - topleft) > maxSize.second -1)
+                        return false;
             }
-            else if (dynamic_cast<const JSonContainer*> (**ent) == nullptr)
-                cursor.second += write(cursor.first, cursor.second, key + ": " +((**ent)->stringify()), maxSize.first, selection == **ent);
             else if (((JSonContainer*)(**ent))->size() == 0)
             {
-                if (isObject)
-                    cursor.second += write(cursor.first, cursor.second, key + ": { }", maxSize.first, selection == **ent);
+                if (dynamic_cast<const JSonObject *>(**ent) )
+                    cursor.second += write(cursor.first, cursor.second, key + ": { }", maxSize.first, selection == ent);
                 else
-                    cursor.second += write(cursor.first, cursor.second, key + ": [ ]", maxSize.first, selection == **ent);
+                    cursor.second += write(cursor.first, cursor.second, key + ": [ ]", maxSize.first, selection == ent);
+                if (cursor.second - topleft > 0 && (unsigned)(cursor.second - topleft) > maxSize.second -1)
+                        return false;
             }
             else
             {
-                if (!writeKey(key, cursor, maxSize, selection == ent || selection == **ent))
+                if (!writeKey(key, cursor, maxSize, selection == ent))
                     return false;
                 cursor.first -= indentLevel /2;
+                const JSonElement *saveSelection = selection;
+                if (selection == ent)
+                    selection = **ent;
                 if (!redraw(cursor, maxSize, **ent, (const JSonContainer *)item))
+                {
+                    selection = saveSelection;
                     return false;
+                }
+                selection = saveSelection;
                 cursor.first -= indentLevel /2;
             }
         }
@@ -258,16 +272,25 @@ void CurseOutput::getScreenSize(std::pair<unsigned int, unsigned int> &screenSiz
 
 void CurseOutput::checkSelection(const JSonElement *item, const JSonElement *parent, const std::pair<int, int> &cursor)
 {
-    if (selection == item)
+    if (!selectFound)
     {
-        if (cursor.second <= topleft)
-            selectIsFirst = true;
-        selectFound = true;
+        if (selection == item)
+        {
+            if (cursor.second <= topleft)
+                selectIsFirst = true;
+            selectFound = true;
+        }
+        else if (!item->getParent() || !dynamic_cast<const JSonObjectEntry*>(item->getParent()))
+            select_up = item;
     }
-    else if (!selectFound)
-        select_up = item;
     else if (!select_down)
-        select_down = item;
+    {
+        const JSonElement *parent = item->getParent();
+        if (!dynamic_cast<const JSonContainer*>(item) && parent && selection != parent && dynamic_cast<const JSonObjectEntry*>(parent))
+            item = parent;
+        if (!parent || !dynamic_cast<const JSonObjectEntry*>(parent))
+            select_down = item;
+    }
 }
 
 /**
@@ -316,8 +339,8 @@ bool CurseOutput::readInput()
                 const JSonElement *brother = selection->findPrev();
                 if (brother == nullptr)
                 {
-                    const JSonContainer *parent = selection->getParent();
-                    if (parent)
+                    const JSonElement *parent = selection->getParent();
+                    if (parent && dynamic_cast<const JSonContainer*>(parent))
                         selection = parent;
                     else
                         break;
@@ -343,15 +366,17 @@ bool CurseOutput::readInput()
             case 'L':
             case KEY_RIGHT:
             {
-                const JSonContainer *_selection = dynamic_cast<const JSonContainer *>(selection);
-                if (!_selection)
+                const JSonElement *_selection = selection;
+                if (dynamic_cast<const JSonObjectEntry*>(selection))
+                    _selection = **((const JSonObjectEntry*)_selection);
+                if (!dynamic_cast<const JSonContainer*>(_selection))
                     break;
 
-                if (collapsed.erase((const JSonContainer *) selection))
+                if (collapsed.erase((const JSonContainer *)_selection))
                     return true;
-                if (!_selection->size())
+                if (!((const JSonContainer*)_selection)->size())
                     break;
-                selection = _selection->firstChild();
+                selection = select_down;
                 return true;
             }
 
@@ -359,16 +384,23 @@ bool CurseOutput::readInput()
             case 'H':
             case KEY_LEFT:
             {
-                const JSonContainer *_selection = dynamic_cast<const JSonContainer *>(selection);
-                if (!_selection
-                        || collapsed.find((const JSonContainer *) selection) != collapsed.end()
-                        || (_selection && _selection->size() == 0))
+                const JSonElement *_selection = selection;
+                if (dynamic_cast<const JSonObjectEntry*>(_selection))
+                    _selection = **((const JSonObjectEntry*)_selection);
+                if (selection->getParent() && (!dynamic_cast<const JSonContainer*>(_selection)
+                        || collapsed.find((const JSonContainer *)_selection) != collapsed.end()
+                        || (dynamic_cast<const JSonContainer*>(_selection) && ((const JSonContainer*)_selection)->size() == 0)))
                 {
-                    const JSonContainer *parent = selection->getParent();
-                    selection = parent ? parent : selection;
+                    selection = selection->getParent();
+                    if (selection->getParent() && dynamic_cast<const JSonObjectEntry*>(selection->getParent()))
+                        selection = selection->getParent();
                 }
                 else if (_selection)
-                    collapsed.insert((const JSonContainer *)selection);
+                {
+                    collapsed.insert((const JSonContainer *)_selection);
+                    if (dynamic_cast<const JSonContainer*>(selection) && selection->getParent())
+                        selection = selection->getParent();
+                }
                 else
                     break;
                 return true;

+ 18 - 24
src/jsonElement.cpp

@@ -2,13 +2,13 @@
 #include "jsonContainer.hh"
 #include "jsonObjectEntry.hh"
 
-JSonElement::JSonElement(JSonContainer *p): parent(p)
+JSonElement::JSonElement(JSonElement *p): parent(p)
 { }
 
 JSonElement::~JSonElement()
 { }
 
-void JSonElement::setParent(JSonContainer *p)
+void JSonElement::setParent(JSonElement *p)
 {
     parent = p;
 }
@@ -21,12 +21,12 @@ unsigned int JSonElement::getLevel() const
     return level;
 }
 
-JSonContainer *JSonElement::getParent()
+JSonElement *JSonElement::getParent()
 {
     return parent;
 }
 
-const JSonContainer *JSonElement::getParent() const
+const JSonElement *JSonElement::getParent() const
 {
     return parent;
 }
@@ -34,20 +34,18 @@ const JSonContainer *JSonElement::getParent() const
 const JSonElement *JSonElement::findPrev() const
 {
     const JSonElement *item = this;
-    const JSonContainer *parent = item->getParent();
-    if (parent == nullptr)
+    const JSonElement *parent = item->getParent();
+    if (parent == nullptr || !dynamic_cast<const JSonContainer*>(parent))
         return nullptr; // Root node, can't have brothers
-    std::list<JSonElement *>::const_iterator it = parent->cbegin();
-    const JSonObjectEntry *ent = dynamic_cast<const JSonObjectEntry *>(*it);
-    const JSonElement *prevElem = ent ? **ent : (*it);
-    if (prevElem == item || (ent && **ent == item))
+    std::list<JSonElement *>::const_iterator it = ((const JSonContainer*)parent)->cbegin();
+    const JSonElement *prevElem = *it;
+    if (prevElem == item)
         return nullptr; // First item
-    while ((++it) != parent->cend())
+    while ((++it) != ((const JSonContainer*)parent)->cend())
     {
-        ent = dynamic_cast<const JSonObjectEntry *>(*it);
-        if (*it == item || (ent && **ent == item))
+        if (*it == item)
             return prevElem;
-        prevElem = ent ? **ent : (*it);
+        prevElem = *it;
     }
     return nullptr;
 }
@@ -55,21 +53,17 @@ const JSonElement *JSonElement::findPrev() const
 const JSonElement* JSonElement::findNext() const
 {
     const JSonElement *item = this;
-    const JSonContainer *parent = item->getParent();
-    if (parent == nullptr)
+    const JSonElement *parent = item->getParent();
+    if (parent == nullptr || !dynamic_cast<const JSonContainer*>(parent))
         return nullptr; // Root node, can't have brothers
-    JSonContainer::const_iterator it = parent->cbegin();
-    while (it != parent->cend())
+    JSonContainer::const_iterator it = ((const JSonContainer*)parent)->cbegin();
+    while (it != ((const JSonContainer*)parent)->cend())
     {
-        const JSonObjectEntry *ent = dynamic_cast<const JSonObjectEntry *>(*it);
-        if (*it == item || (ent && **ent == item))
+        if (*it == item)
         {
             it++;
-            if (it == parent->cend())
+            if (it == ((const JSonContainer*)parent)->cend())
                 return parent->findNext(); // Last item
-            ent = dynamic_cast<const JSonObjectEntry *>(*it);
-            if (ent)
-                return **ent;
             return *it;
         }
         it++;

+ 4 - 2
src/jsonObject.cpp

@@ -45,14 +45,16 @@ JSonElement *JSonObject::firstChild()
 {
     if (begin() == end())
         return nullptr;
-    return *begin();
+    JSonObjectEntry *elem = (JSonObjectEntry *) *begin();
+    return **elem;
 }
 
 const JSonElement *JSonObject::firstChild() const
 {
     if (cbegin() == cend())
         return nullptr;
-    return *cbegin();
+    const JSonObjectEntry *elem = (const JSonObjectEntry *) *cbegin();
+    return **elem;
 }
 
 std::string JSonObject::stringify() const

+ 45 - 1
src/jsonObjectEntry.cpp

@@ -2,7 +2,9 @@
 #include "jsonObject.hh"
 
 JSonObjectEntry::JSonObjectEntry(JSonObject *parent, const std::string &k, JSonElement *v): JSonElement(parent), key(k), value(v)
-{ }
+{
+    v->setParent(this);
+}
 
 JSonObjectEntry::~JSonObjectEntry()
 {
@@ -29,3 +31,45 @@ std::string JSonObjectEntry::stringify() const
     return key;
 }
 
+/*
+const JSonElement *JSonObjectEntry::findPrev() const
+{
+    const JSonObject *parent = (JSonObject*) getParent();
+    if (parent == nullptr)
+        return nullptr; // Root node, can't have brothers
+    std::list<JSonElement *>::const_iterator it = parent->cbegin();
+    const JSonObjectEntry *ent = (const JSonObjectEntry *)(*it);
+    const JSonObjectEntry *prevElem = ent;
+    if (prevElem == this)
+        return nullptr; // First item
+    while ((++it) != parent->cend())
+    {
+        ent = (const JSonObjectEntry *)(*it);
+        if (*it == this)
+            return prevElem;
+        prevElem = ent;
+    }
+    return nullptr;
+}
+
+const JSonElement* JSonObjectEntry::findNext() const
+{
+    const JSonObject *parent = (const JSonObject*) this->getParent();
+    if (parent == nullptr)
+        return nullptr; // Root node, can't have brothers
+    JSonContainer::const_iterator it = parent->cbegin();
+    while (it != parent->cend())
+    {
+        if (*it == this)
+        {
+            it++;
+            if (it == parent->cend())
+                return parent->findNext(); // Last item
+            return *it;
+        }
+        it++;
+    }
+    return parent->findNext();
+}
+*/
+