Changeset 60
- Timestamp:
- 04/14/06 12:08:30 (2 years ago)
- Location:
- trunk
- Files:
-
- 6 modified
-
src/DocumentOutline.cxx (modified) (6 diffs)
-
src/DocumentOutline.h (modified) (2 diffs)
-
src/IDocument.cxx (modified) (1 diff)
-
src/PDFDocument.cxx (modified) (1 diff)
-
tests/DocumentOutlineTest.cxx (modified) (3 diffs)
-
tests/DocumentOutlineTest.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/DocumentOutline.cxx
r58 r60 17 17 18 18 #include <config.h> 19 20 // Poppler headers 21 #include <goo/GooList.h> 22 #include <Outline.h> 23 #include <PDFDoc.h> 24 #include <Link.h> 25 19 26 #include "epdfview.h" 20 27 21 28 using namespace ePDFView; 22 29 30 /// Forward declarations. 31 static void deleteChild (gpointer child, gpointer userData); 32 23 33 /// 24 34 /// @brief Constructs a new DocumentOutline. 25 35 /// 26 DocumentOutline::DocumentOutline () 27 { 36 DocumentOutline::DocumentOutline (PDFDoc *document) 37 { 38 m_Destination = 1; 39 m_Document = document; 40 m_Children = NULL; 41 m_LastReturnedChild = NULL; 42 m_Parent = NULL; 43 m_Title = NULL; 28 44 } 29 45 … … 33 49 DocumentOutline::~DocumentOutline () 34 50 { 51 m_Document = NULL; 52 m_Parent = NULL; 53 g_free (m_Title); 54 /// Delete the children. 55 g_list_foreach (m_Children, deleteChild, NULL); 56 g_list_free (m_Children); 35 57 } 36 58 … … 50 72 DocumentOutline::getDestinationPage () 51 73 { 52 return 1;74 return m_Destination; 53 75 } 54 76 … … 62 84 DocumentOutline::getFirstChild () 63 85 { 86 m_LastReturnedChild = g_list_first (m_Children); 87 if ( NULL != m_LastReturnedChild ) 88 { 89 return (DocumentOutline *)m_LastReturnedChild->data; 90 } 91 return NULL; 92 } 93 94 /// 95 /// @brief Gets the next outline's child. 96 /// 97 /// @returns The next child after the one returned by the last call to 98 /// getFirstChild() or getNextChild(). 99 /// If no child has been returned yet (i.e., getFirstChild() hasn't 100 /// been called yet) or there's no next child returns NULL. 101 /// 102 DocumentOutline * 103 DocumentOutline::getNextChild () 104 { 105 m_LastReturnedChild = g_list_next (m_LastReturnedChild); 106 if ( NULL != m_LastReturnedChild ) 107 { 108 return (DocumentOutline *)m_LastReturnedChild->data; 109 } 64 110 return NULL; 65 111 } … … 73 119 DocumentOutline::getNumChildren () 74 120 { 75 return 0;121 return g_list_length (m_Children); 76 122 } 77 123 … … 85 131 DocumentOutline::getTitle () 86 132 { 87 return ""; 88 } 133 if ( NULL == m_Title ) 134 { 135 return ""; 136 } 137 return m_Title; 138 } 139 140 void 141 DocumentOutline::setDestination (gint destination) 142 { 143 m_Destination = destination; 144 } 145 146 void 147 DocumentOutline::setChildren (GooList *childrenList) 148 { 149 if ( NULL != childrenList ) 150 { 151 int numChildren = childrenList->getLength (); 152 for ( int childIndex = 0; childIndex < numChildren; childIndex++) 153 { 154 OutlineItem *item = (OutlineItem *)childrenList->get (childIndex); 155 // Get the title. 156 gchar *title = g_ucs4_to_utf8 (item->getTitle (), 157 item->getTitleLength (), 158 NULL, NULL, NULL); 159 // The page destination. 160 gint destination = 1; 161 LinkAction *action = item->getAction (); 162 if ( NULL != action ) 163 { 164 if ( action->isOk () && actionGoTo == action->getKind () ) 165 { 166 LinkDest *dest = ((LinkGoTo *)action)->getDest (); 167 if ( dest->isOk () && dest->isPageRef () ) 168 { 169 Ref ref = dest->getPageRef (); 170 destination = m_Document->findPage (ref.num, ref.gen); 171 } 172 else if ( dest->isOk () ) 173 { 174 destination = dest->getPageNum (); 175 } 176 } 177 } 178 // Create the child. 179 DocumentOutline *child = new DocumentOutline (m_Document); 180 child->setParent (this); 181 child->setTitle (title); 182 g_free (title); 183 child->setDestination (destination); 184 // Add to the list of children. 185 m_Children = g_list_append (m_Children, child); 186 // And add its own children. 187 if ( item->hasKids () ) 188 { 189 // I need to open the outline because if it's not 190 // open then the getKids() function returns NULL. 191 item->open (); 192 child->setChildren (item->getKids ()); 193 } 194 } 195 } 196 } 197 198 /// 199 /// @brief Sets the outline. 200 /// 201 /// This is only called for the root outline element when loading a document. 202 /// Fills in the list of outline items from the document. 203 /// 204 /// @param outline The outline from the documents. 205 /// 206 void 207 DocumentOutline::setOutline (Outline *outline) 208 { 209 g_assert (NULL != outline && "Tried to set a NULL outline."); 210 211 setTitle (""); 212 setDestination (1); 213 214 setChildren (outline->getItems ()); 215 } 216 217 void 218 DocumentOutline::setParent (DocumentOutline *parent) 219 { 220 m_Parent = parent; 221 } 222 223 void 224 DocumentOutline::setTitle (const char *title) 225 { 226 g_assert (NULL != title && "Tried to set a NULL title."); 227 228 g_free (m_Title); 229 m_Title = g_strdup (title); 230 } 231 232 void 233 deleteChild (gpointer child, gpointer userData) 234 { 235 g_assert ( NULL != child && "An outline's child is NULL!"); 236 237 DocumentOutline *outline = (DocumentOutline *)child; 238 delete outline; 239 } -
trunk/src/DocumentOutline.h
r58 r60 19 19 #define __DOCUMENT_OUTLINE_H__ 20 20 21 // Forward declarations. 22 class PDFDoc; 23 class Outline; 24 class GooList; 25 21 26 namespace ePDFView 22 27 { … … 37 42 { 38 43 public: 39 DocumentOutline ( void);44 DocumentOutline (PDFDoc *document); 40 45 ~DocumentOutline (void); 41 46 42 47 gint getDestinationPage (void); 43 48 DocumentOutline *getFirstChild (void); 49 DocumentOutline *getNextChild (void); 44 50 gint getNumChildren (void); 45 51 const gchar *getTitle (void); 52 void setOutline (Outline *outline); 53 54 private: 55 GList *m_Children; 56 gint m_Destination; 57 PDFDoc *m_Document; 58 GList *m_LastReturnedChild; 59 DocumentOutline *m_Parent; 60 gchar *m_Title; 61 62 void setChildren (GooList *childrenList); 63 void setParent (DocumentOutline *parent); 64 void setTitle (const gchar *title); 65 void setDestination (gint destination); 46 66 }; 47 67 } -
trunk/src/IDocument.cxx
r58 r60 127 127 m_Creator = NULL; 128 128 m_CurrentPage = 0; 129 m_Outline = new DocumentOutline ();129 m_Outline = NULL; 130 130 m_FileName = NULL; 131 131 m_Format = NULL; -
trunk/src/PDFDocument.cxx
r58 r60 124 124 delete m_Document; 125 125 m_Document = newDocument; 126 // Load the document's information and outline. 126 127 loadMetadata (); 128 m_Outline = new DocumentOutline (m_Document); 129 m_Outline->setOutline (m_Document->getOutline ()); 127 130 // Set the output device. 128 131 delete m_OutputDevice; -
trunk/tests/DocumentOutlineTest.cxx
r59 r60 51 51 { 52 52 CPPUNIT_ASSERT ( !m_Document->isLoaded ()); 53 DocumentOutline *outline = m_Document->getOutline (); 54 // The initial state of the outline is no outline at all. 55 CPPUNIT_ASSERT_EQUAL (0, outline->getNumChildren ()); 56 CPPUNIT_ASSERT (0 == g_ascii_strcasecmp ("", outline->getTitle ())); 57 CPPUNIT_ASSERT_EQUAL (1, outline->getDestinationPage ()); 58 CPPUNIT_ASSERT (NULL == outline->getFirstChild ()); 53 CPPUNIT_ASSERT ( NULL == m_Document->getOutline ()); 59 54 } 60 55 … … 63 58 /// 64 59 /// When a loaded document doesn't have an outline, the DocumentOutline's 65 /// behaviour is exactly the same as the initial status.60 /// is empty. 66 61 /// 67 62 void … … 72 67 CPPUNIT_ASSERT (m_Document->isLoaded ()); 73 68 DocumentOutline *outline = m_Document->getOutline (); 69 CPPUNIT_ASSERT ( NULL != outline ); 74 70 CPPUNIT_ASSERT_EQUAL (0, outline->getNumChildren ()); 75 71 CPPUNIT_ASSERT (0 == g_ascii_strcasecmp ("", outline->getTitle ())); 76 72 CPPUNIT_ASSERT_EQUAL (1, outline->getDestinationPage ()); 77 73 CPPUNIT_ASSERT (NULL == outline->getFirstChild ()); 74 CPPUNIT_ASSERT (NULL == outline->getNextChild ()); 78 75 } 76 77 /// 78 /// @brief Test a file that has outline. 79 /// 80 /// The document I've gone test has the following outline: 81 /// 82 /// * 83 /// | 84 /// +- Test PDF 1 / page 3 85 /// | 86 /// +- Table of Contents / page 4 87 /// | 88 /// +- Chapter 1. First Chapter / page 5 89 /// | 90 /// + First Section / page 5 91 /// 92 void 93 DocumentOutlineTest::hasOutline () 94 { 95 CPPUNIT_ASSERT (m_Document->loadFile (TEST_DATA_DIR "test1.pdf", 96 NULL, NULL)); 97 CPPUNIT_ASSERT (m_Document->isLoaded ()); 98 DocumentOutline *outline = m_Document->getOutline (); 99 CPPUNIT_ASSERT ( NULL != outline ); 100 // The root outline must be the same empy one, except it has children. 101 CPPUNIT_ASSERT_EQUAL (3, outline->getNumChildren ()); 102 CPPUNIT_ASSERT (0 == g_ascii_strcasecmp ("", outline->getTitle ())); 103 CPPUNIT_ASSERT_EQUAL (1, outline->getDestinationPage ()); 104 105 { 106 DocumentOutline *child = outline->getFirstChild(); 107 CPPUNIT_ASSERT ( NULL != child ); 108 CPPUNIT_ASSERT_EQUAL (0, child->getNumChildren ()); 109 CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("Test PDF 1", 110 child->getTitle ())); 111 CPPUNIT_ASSERT_EQUAL (3, child->getDestinationPage ()); 112 } 113 114 { 115 DocumentOutline *child = outline->getNextChild(); 116 CPPUNIT_ASSERT ( NULL != child ); 117 CPPUNIT_ASSERT_EQUAL (0, child->getNumChildren ()); 118 CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("Table of Contents", 119 child->getTitle ())); 120 CPPUNIT_ASSERT_EQUAL (4, child->getDestinationPage ()); 121 } 122 123 { 124 DocumentOutline *child = outline->getNextChild(); 125 CPPUNIT_ASSERT ( NULL != child ); 126 CPPUNIT_ASSERT_EQUAL (1, child->getNumChildren ()); 127 CPPUNIT_ASSERT_EQUAL (0, 128 g_ascii_strcasecmp ("Chapter 1. First Chapter", 129 child->getTitle ())); 130 CPPUNIT_ASSERT_EQUAL (5, child->getDestinationPage ()); 131 132 DocumentOutline *greatChild = child->getFirstChild (); 133 CPPUNIT_ASSERT ( NULL != greatChild ); 134 CPPUNIT_ASSERT_EQUAL (0, greatChild->getNumChildren ()); 135 CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("First Section", 136 greatChild->getTitle ())); 137 CPPUNIT_ASSERT_EQUAL (5, greatChild->getDestinationPage ()); 138 } 139 140 // After the last child, it should return NULL. 141 CPPUNIT_ASSERT (NULL == outline->getNextChild ()); 142 143 // Try again the "no outlined" file. 144 noOutline (); 145 } -
trunk/tests/DocumentOutlineTest.h
r59 r60 28 28 CPPUNIT_TEST (initialStatus); 29 29 CPPUNIT_TEST (noOutline); 30 CPPUNIT_TEST (hasOutline); 30 31 CPPUNIT_TEST_SUITE_END (); 31 32 … … 36 37 void initialStatus (void); 37 38 void noOutline (void); 39 void hasOutline (void); 38 40 39 41 protected:
