Changeset 24

Show
Ignore:
Timestamp:
04/11/06 11:10:26 (3 years ago)
Author:
jordi
Message:

The main presenter now can open files and changes the window's title to show the file name or the document's title, if it has title. Also changes the the control's sensitiveness.

Location:
trunk
Files:
9 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/IMainView.h

    r23 r24  
    3030            virtual ~IMainView () {} 
    3131 
     32            virtual gchar *openFileDialog () = 0; 
    3233            virtual void sensitiveGoToFirstPage (gboolean sensitive) = 0; 
    3334            virtual void sensitiveGoToLastPage (gboolean sensitive) = 0; 
     
    3940            virtual void sensitiveZoomFit (gboolean sensitive) = 0; 
    4041            virtual void sensitiveZoomWidth (gboolean sensitive) = 0; 
    41             virtual void show (void) = 0;  
     42            virtual void show (void) = 0; 
     43            virtual void showPage (DocumentPage *page) = 0; 
    4244            virtual void setTitle (const gchar *title) = 0; 
    4345 
  • trunk/src/MainPter.cxx

    r23 r24  
    2525MainPter::MainPter () 
    2626{ 
     27    m_Document = new PDFDocument (); 
    2728} 
    2829 
     
    3637MainPter::MainPter (IDocument *document) 
    3738{ 
     39    g_assert (NULL != document && "Tried to set a NULL document"); 
     40 
     41    m_Document = document; 
    3842} 
    3943 
     
    5458MainPter::setInitialState () 
    5559{ 
     60    g_assert ( NULL != m_Document && "The document is NULL."); 
     61     
    5662    IMainView &view = getView (); 
    57     view.setTitle (_("PDF Viewer")); 
    58     view.sensitiveGoToFirstPage (FALSE); 
    59     view.sensitiveGoToLastPage (FALSE); 
    60     view.sensitiveGoToNextPage (FALSE); 
    61     view.sensitiveGoToPage (FALSE); 
    62     view.sensitiveGoToPreviousPage (FALSE); 
    63     view.sensitiveZoomIn (FALSE); 
    64     view.sensitiveZoomOut (FALSE); 
    65     view.sensitiveZoomFit (FALSE); 
    66     view.sensitiveZoomWidth (FALSE); 
     63    if ( m_Document->isLoaded () ) 
     64    { 
     65        if ( 0 == g_ascii_strcasecmp ("", m_Document->getTitle ()) ) 
     66        { 
     67            view.setTitle (m_Document->getFileName ()); 
     68        } 
     69        else 
     70        { 
     71            view.setTitle (m_Document->getTitle ()); 
     72        } 
     73        view.sensitiveGoToFirstPage (FALSE); 
     74        view.sensitiveGoToLastPage (TRUE); 
     75        view.sensitiveGoToNextPage (TRUE); 
     76        view.sensitiveGoToPage (TRUE); 
     77        view.sensitiveGoToPreviousPage (FALSE); 
     78        view.sensitiveZoomIn (TRUE); 
     79        view.sensitiveZoomOut (TRUE); 
     80        view.sensitiveZoomFit (TRUE); 
     81        view.sensitiveZoomWidth (TRUE); 
     82        showPage (); 
     83    } 
     84    else 
     85    { 
     86        view.setTitle (_("PDF Viewer")); 
     87        view.sensitiveGoToFirstPage (FALSE); 
     88        view.sensitiveGoToLastPage (FALSE); 
     89        view.sensitiveGoToNextPage (FALSE); 
     90        view.sensitiveGoToPage (FALSE); 
     91        view.sensitiveGoToPreviousPage (FALSE); 
     92        view.sensitiveZoomIn (FALSE); 
     93        view.sensitiveZoomOut (FALSE); 
     94        view.sensitiveZoomFit (FALSE); 
     95        view.sensitiveZoomWidth (FALSE); 
     96    } 
     97     
    6798    view.show ();  
    6899} 
     
    97128} 
    98129 
     130/// 
     131/// @brief The Open File action was activated. 
     132/// 
     133/// This means that the user wants to open a new file. The presenter 
     134/// asks the view to show the Open File dialog and return a filename. 
     135/// Then we'll try to open the file and if it's correct we'll show it,  
     136/// otherwise it will request the view to show an error dialog or a  
     137/// password dialog, if the document is encrypted. 
     138/// 
     139void 
     140MainPter::openFileActivated () 
     141{ 
     142    IMainView &view = getView (); 
     143    gchar *fileName = view.openFileDialog (); 
     144    GError *error; 
     145    if ( m_Document->loadFile (fileName, NULL, &error) ) 
     146    { 
     147        // Now that the document has been loaded, just reset the initial 
     148        // state. 
     149        setInitialState (); 
     150        g_free (fileName); 
     151    } 
     152} 
    99153 
     154/// 
     155/// @brief Shows the current page. 
     156/// 
     157/// This function is called when the presenter needs to show a document's 
     158/// page. It only gets the rendered page from the document and then 
     159/// pass the resultant DocumentPage to the view. 
     160/// 
     161void 
     162MainPter::showPage () 
     163{ 
     164    g_assert (NULL != m_Document &&  
     165              "Tried to show a page from a NULL document."); 
     166    g_assert (m_Document->isLoaded () &&  
     167              "Tried to show a page from a not loaded document."); 
     168             
     169    DocumentPage *page = m_Document->renderPage (); 
     170    if ( NULL != page ) 
     171    { 
     172        getView ().showPage (page); 
     173        delete page; 
     174    } 
     175} 
  • trunk/src/MainPter.h

    r23 r24  
    2828            ~MainPter (); 
    2929 
    30             void setInitialState (void); 
     30            void setInitialState (void);             
    3131            IMainView &getView (void); 
    3232            void setView (IMainView *view); 
    3333 
     34            void openFileActivated (void); 
     35 
    3436        protected: 
     37            void showPage (void); 
     38 
     39            IDocument *m_Document; 
    3540            IMainView *m_View; 
    3641    }; 
  • trunk/tests/DumbDocument.cxx

    r23 r24  
    2828    IDocument () 
    2929{ 
     30    m_Loaded = FALSE; 
    3031} 
    3132 
     
    3738DumbDocument::isLoaded () 
    3839{ 
    39     return false; 
     40    return m_Loaded; 
    4041} 
    4142 
     
    4445                        GError **error) 
    4546{ 
    46     return false; 
     47    setFileName (filename); 
     48    m_Loaded = TRUE; 
     49    return m_Loaded; 
    4750} 
    4851 
  • trunk/tests/DumbDocument.h

    r23 r24  
    3333            void getPageSize (gdouble *width, gdouble *height); 
    3434            DocumentPage *renderPage (void); 
     35 
     36        private: 
     37            gboolean m_Loaded; 
    3538    }; 
    3639} 
  • trunk/tests/DumbMainView.cxx

    r23 r24  
    2929    // This won't be deleted because it's presenter responsability. 
    3030    m_DocumentPage = NULL; 
     31    m_OpenFileName = g_strdup (""); 
    3132    m_SensitiveGoToFirstPage = TRUE; 
    3233    m_SensitiveGoToLastPage = TRUE; 
     
    4445DumbMainView::~DumbMainView () 
    4546{ 
     47    g_free (m_OpenFileName); 
    4648    g_free (m_Title); 
    4749} 
     50             
     51gchar * 
     52DumbMainView::openFileDialog (void) 
     53{ 
     54    return g_strdup (m_OpenFileName); 
     55} 
    4856 
    4957void 
     
    105113{ 
    106114    m_Shown = TRUE; 
     115} 
     116 
     117void 
     118DumbMainView::showPage (DocumentPage *page) 
     119{ 
     120    m_DocumentPage = page; 
    107121} 
    108122 
     
    120134 
    121135gboolean 
     136DumbMainView::hasImagePageView () 
     137{ 
     138    return NULL != m_DocumentPage; 
     139} 
     140 
     141gboolean 
    122142DumbMainView::isShown () 
    123143{ 
     
    185205} 
    186206 
    187 gboolean 
    188 DumbMainView::hasImagePageView () 
    189 { 
    190     return NULL != m_DocumentPage; 
    191 } 
     207void 
     208DumbMainView::setOpenFileName (const gchar *fileName) 
     209{ 
     210    g_free (m_OpenFileName); 
     211    m_OpenFileName = g_strdup (fileName); 
     212} 
  • trunk/tests/DumbMainView.h

    r23 r24  
    2828            ~DumbMainView (); 
    2929 
     30            gchar *openFileDialog (void); 
    3031            void sensitiveGoToFirstPage (gboolean sensitive); 
    3132            void sensitiveGoToLastPage (gboolean sensitive); 
     
    3738            void sensitiveZoomFit (gboolean sensitive); 
    3839            void sensitiveZoomWidth (gboolean sensitive); 
    39             void show (void);  
     40            void show (void); 
     41            void showPage (DocumentPage *page); 
    4042            void setTitle (const gchar *title); 
    4143 
    4244            // Methods for test purposes. 
    4345            const gchar *getTitle (void); 
     46            gboolean hasImagePageView (void); 
    4447            gboolean isShown (void); 
    4548            gboolean isSensitiveGoToFirstPage (void); 
     
    5255            gboolean isSensitiveZoomFit (void); 
    5356            gboolean isSensitiveZoomWidth (void); 
    54             gboolean hasImagePageView (void); 
     57            void setOpenFileName (const gchar *fileName); 
    5558 
    5659        protected: 
    5760            DocumentPage *m_DocumentPage; 
     61            gchar *m_OpenFileName; 
    5862            gboolean m_SensitiveGoToFirstPage; 
    5963            gboolean m_SensitiveGoToLastPage; 
  • trunk/tests/MainPterTest.cxx

    r23 r24  
    7474    CPPUNIT_ASSERT (!m_View->hasImagePageView ()); 
    7575} 
     76 
     77/// 
     78/// @brief Test loading a document. 
     79/// 
     80/// Loading a document will set the title to the document's title or the 
     81/// document's file name, if it has no title. Also will sensitive the 
     82/// go to next page, go to last page and all zoom actions. Also will 
     83/// show the document's first page. 
     84/// 
     85void 
     86MainPterTest::loadDocument () 
     87{ 
     88    m_View->setOpenFileName ("/tmp/test.pdf"); 
     89    m_MainPter->openFileActivated (); 
     90    CPPUNIT_ASSERT_EQUAL (0,  
     91            g_ascii_strcasecmp ("/tmp/test.pdf", m_View->getTitle ())); 
     92    CPPUNIT_ASSERT (!m_View->isSensitiveGoToFirstPage ()); 
     93    CPPUNIT_ASSERT (m_View->isSensitiveGoToLastPage ()); 
     94    CPPUNIT_ASSERT (m_View->isSensitiveGoToNextPage ()); 
     95    CPPUNIT_ASSERT (m_View->isSensitiveGoToPage ()); 
     96    CPPUNIT_ASSERT (!m_View->isSensitiveGoToPreviousPage ()); 
     97    CPPUNIT_ASSERT (m_View->isSensitiveZoomIn ()); 
     98    CPPUNIT_ASSERT (m_View->isSensitiveZoomOut ()); 
     99    CPPUNIT_ASSERT (m_View->isSensitiveZoomFit ()); 
     100    CPPUNIT_ASSERT (m_View->isSensitiveZoomWidth ()); 
     101    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
     102 
     103    // Now try a document with a title. 
     104    m_Document->setTitle ("Test PDF"); 
     105    m_MainPter->openFileActivated (); 
     106    CPPUNIT_ASSERT_EQUAL (0,  
     107            g_ascii_strcasecmp ("Test PDF", m_View->getTitle ())); 
     108    CPPUNIT_ASSERT (!m_View->isSensitiveGoToFirstPage ()); 
     109    CPPUNIT_ASSERT (m_View->isSensitiveGoToLastPage ()); 
     110    CPPUNIT_ASSERT (m_View->isSensitiveGoToNextPage ()); 
     111    CPPUNIT_ASSERT (m_View->isSensitiveGoToPage ()); 
     112    CPPUNIT_ASSERT (!m_View->isSensitiveGoToPreviousPage ()); 
     113    CPPUNIT_ASSERT (m_View->isSensitiveZoomIn ()); 
     114    CPPUNIT_ASSERT (m_View->isSensitiveZoomOut ()); 
     115    CPPUNIT_ASSERT (m_View->isSensitiveZoomFit ()); 
     116    CPPUNIT_ASSERT (m_View->isSensitiveZoomWidth ()); 
     117    CPPUNIT_ASSERT (m_View->hasImagePageView ()); 
     118} 
  • trunk/tests/MainPterTest.h

    r23 r24  
    2727        CPPUNIT_TEST_SUITE (MainPterTest); 
    2828        CPPUNIT_TEST (initialStatus); 
     29        CPPUNIT_TEST (loadDocument); 
    2930        CPPUNIT_TEST_SUITE_END(); 
    3031 
     
    3435 
    3536            void initialStatus (void); 
     37            void loadDocument (void); 
    3638 
    3739        private: