Browse Source

[refactor] moved findPrev / findNext to JSonElement

isundil 9 years ago
parent
commit
c060a08f9f
4 changed files with 57 additions and 51 deletions
  1. 0 3
      include/curseOutput.hh
  2. 3 0
      include/jsonElement.hh
  3. 7 48
      src/curseOutput.cpp
  4. 47 0
      src/jsonElement.cpp

+ 0 - 3
include/curseOutput.hh

@@ -33,8 +33,6 @@ class CurseOutput
         bool redraw(std::pair<int, int> &, const std::pair<unsigned int, unsigned int> &maxWidth, const JSonElement *, const JSonContainer *);
         bool readInput();
         void getScreenSize(std::pair<unsigned int, unsigned int> &, std::pair<int, int> &) const;
-        static const JSonElement* findPrev(const JSonElement *);
-        static const JSonElement* findNext(const JSonElement *);
         void checkSelection(const JSonElement *item, const JSonElement *parent, const std::pair<int, int>&);
         static unsigned int getNbLines(float nbChar, unsigned int maxWidth);
         unsigned int write(const int &x, const int &y, const JSonElement *item, unsigned int maxWidth, bool selected =false);
@@ -54,7 +52,6 @@ class CurseOutput
         int topleft;
         const unsigned int indentLevel;
 
-        //FIXME optimize
         const JSonElement *select_up, *select_down;
         bool selectFound, selectIsLast, selectIsFirst;
 };

+ 3 - 0
include/jsonElement.hh

@@ -17,6 +17,9 @@ class JSonElement
 
         void setParent(JSonContainer *parent);
 
+        const JSonElement *findPrev() const;
+        const JSonElement *findNext() const;
+
     private:
         JSonElement();
         JSonContainer *parent;

+ 7 - 48
src/curseOutput.cpp

@@ -247,50 +247,6 @@ void CurseOutput::getScreenSize(std::pair<unsigned int, unsigned int> &screenSiz
     getbegyx(stdscr, bs.second, bs.first);
 }
 
-const JSonElement *CurseOutput::findPrev(const JSonElement *item)
-{
-    const JSonContainer *parent = item->getParent();
-    if (parent == nullptr)
-        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))
-        return nullptr; // First item
-    while ((++it) != parent->cend())
-    {
-        ent = dynamic_cast<const JSonObjectEntry *>(*it);
-        if (*it == item || (ent && **ent == item))
-            return prevElem;
-        prevElem = ent ? **ent : (*it);
-    }
-    return nullptr;
-}
-
-const JSonElement* CurseOutput::findNext(const JSonElement *item)
-{
-    const JSonContainer *parent = item->getParent();
-    if (parent == nullptr)
-        return nullptr; // Root node, can't have brothers
-    JSonContainer::const_iterator it = parent->cbegin();
-    while (it != parent->cend())
-    {
-        const JSonObjectEntry *ent = dynamic_cast<const JSonObjectEntry *>(*it);
-        if (*it == item || (ent && **ent == item))
-        {
-            it++;
-            if (it == parent->cend())
-                return findNext((const JSonElement*) parent); // Last item
-            ent = dynamic_cast<const JSonObjectEntry *>(*it);
-            if (ent)
-                return **ent;
-            return *it;
-        }
-        it++;
-    }
-    return findNext((const JSonElement*) parent);
-}
-
 void CurseOutput::checkSelection(const JSonElement *item, const JSonElement *parent, const std::pair<int, int> &cursor)
 {
     if (selection == item)
@@ -340,13 +296,16 @@ bool CurseOutput::readInput()
             case 'J':
                 if (selectIsLast)
                     topleft += 2;
-                else
+                else if (selection != select_down)
+                {
                     selection = select_down;
-                return true;
+                    return true;
+                }
+                break;
 
             case KEY_PPAGE:
             {
-                const JSonElement *brother = findPrev(selection);
+                const JSonElement *brother = selection->findPrev();
                 if (brother == nullptr)
                 {
                     const JSonContainer *parent = selection->getParent();
@@ -363,7 +322,7 @@ bool CurseOutput::readInput()
 
             case KEY_NPAGE:
             {
-                const JSonElement *brother = findNext(selection);
+                const JSonElement *brother = selection->findNext();
                 if (brother)
                 {
                     selection = brother;

+ 47 - 0
src/jsonElement.cpp

@@ -1,5 +1,6 @@
 #include "jsonElement.hh"
 #include "jsonContainer.hh"
+#include "jsonObjectEntry.hh"
 
 JSonElement::JSonElement(JSonContainer *p): parent(p)
 { }
@@ -30,3 +31,49 @@ const JSonContainer *JSonElement::getParent() const
     return parent;
 }
 
+const JSonElement *JSonElement::findPrev() const
+{
+    const JSonElement *item = this;
+    const JSonContainer *parent = item->getParent();
+    if (parent == nullptr)
+        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))
+        return nullptr; // First item
+    while ((++it) != parent->cend())
+    {
+        ent = dynamic_cast<const JSonObjectEntry *>(*it);
+        if (*it == item || (ent && **ent == item))
+            return prevElem;
+        prevElem = ent ? **ent : (*it);
+    }
+    return nullptr;
+}
+
+const JSonElement* JSonElement::findNext() const
+{
+    const JSonElement *item = this;
+    const JSonContainer *parent = item->getParent();
+    if (parent == nullptr)
+        return nullptr; // Root node, can't have brothers
+    JSonContainer::const_iterator it = parent->cbegin();
+    while (it != parent->cend())
+    {
+        const JSonObjectEntry *ent = dynamic_cast<const JSonObjectEntry *>(*it);
+        if (*it == item || (ent && **ent == item))
+        {
+            it++;
+            if (it == parent->cend())
+                return parent->findNext(); // Last item
+            ent = dynamic_cast<const JSonObjectEntry *>(*it);
+            if (ent)
+                return **ent;
+            return *it;
+        }
+        it++;
+    }
+    return parent->findNext();
+}
+