1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SD_SLIDESORTER_SLIDE_SORTER_MODEL_HXX
25 #define SD_SLIDESORTER_SLIDE_SORTER_MODEL_HXX
26 
27 class SdDrawDocument;
28 
29 #include "model/SlsPageEnumeration.hxx"
30 #include "model/SlsSharedPageDescriptor.hxx"
31 
32 #include "pres.hxx"
33 #include <com/sun/star/drawing/XDrawPage.hpp>
34 #include <osl/mutex.hxx>
35 #include <vcl/region.hxx>
36 
37 #include <memory>
38 #include <vector>
39 #include <functional>
40 
41 namespace css = ::com::sun::star;
42 
43 class SdrPage;
44 class SdPage;
45 
46 namespace sd { namespace slidesorter {
47 class SlideSorter;
48 } }
49 
50 namespace sd { namespace slidesorter { namespace controller {
51 class PageObjectFactory;
52 } } }
53 
54 namespace sd { namespace slidesorter { namespace model {
55 
56 class DocumentPageContainer;
57 
FromCoreIndex(const sal_uInt16 nCoreIndex)58 inline sal_Int32 FromCoreIndex (const sal_uInt16 nCoreIndex) { return (nCoreIndex-1)/2; }
ToCoreIndex(const sal_Int32 nIndex)59 inline sal_uInt16 ToCoreIndex (const sal_Int32 nIndex) { return static_cast<sal_uInt16>(nIndex*2+1); }
60 
61 /** The model of the slide sorter gives access to the slides that are to be
62     displayed in the slide sorter view.  Via the SetDocumentSlides() method
63     this set of slides can be modified (but do not call it directly, use
64     SlideSorterController::SetDocumentSlides() instead.)
65 */
66 class SlideSorterModel
67 {
68 public:
69     SlideSorterModel (SlideSorter& rSlideSorter);
70     void Init (void);
71 
72     virtual ~SlideSorterModel (void);
73     void Dispose (void);
74 
75     /** This method is present to let the view create a ShowView for
76         displaying slides.
77     */
78     SdDrawDocument* GetDocument (void);
79 
80     /** Set a new edit mode and return whether the edit mode really
81         has been changed.  When the edit mode is changed then the
82         previous page descriptor list is replaced by a new one which
83         has to be repainted.
84         @return
85             A return value of <TRUE/> indicates that the edit mode has
86             changed and thus the page descriptor list has been set up
87             to reflect that change.  A repaint is necessary.
88     */
89     bool SetEditMode (EditMode eEditMode);
90 
91     /** Set the edit mode to that currently used by the controller.
92     */
93     bool SetEditModeFromController (void);
94     EditMode GetEditMode (void) const;
95     PageKind GetPageType (void) const;
96 
97     /** Return the number of slides in the document regardless of whether
98         they are visible or not or whether they are hidden or not.
99         The number of slides depends on the set of slides available through
100         the XIndexAccess given to SetDocumentSlides().
101     */
102     sal_Int32 GetPageCount (void) const;
103 
104     /** Return a page descriptor for the page with the specified index.
105         Page descriptors are created on demand.  The page descriptor is
106         found (or not found) in constant time.
107         @param nPageIndex
108             The index of the requested slide.  The valid values
109             are 0 to GetPageCount()-1.
110         @param bCreate
111             When <TRUE/> and the requested page descriptor is missing then
112             it is created.  When <FALSE/> then an empty reference is
113             returned for missing descriptors.
114         @return
115             When the given index is not valid, i.e. lower then zero or
116             larger than or equal to the number of pages then an empty
117             reference is returned. Note that the page count may change
118             between calls to GetPageCount() and GetPageDescriptor().
119     */
120     SharedPageDescriptor GetPageDescriptor (
121         const sal_Int32 nPageIndex,
122         const bool bCreate = true) const;
123 
124     /** Return a page descriptor for the given XDrawPage.  Page descriptors
125         are created on demand.  The page descriptor is found (or not found)
126         in (at most) linear time.  Note that all page descriptors in front of
127         the one associated with the given XDrawPage are created when not yet
128         present. When the XDrawPage is not found then all descriptors are
129         created.
130         @return
131             Returns the index to the requested page descriptor or -1 when
132             there is no such page descriptor.
133     */
134     sal_Int32 GetIndex (
135         const ::com::sun::star::uno::Reference<com::sun::star::drawing::XDrawPage>& rxSlide) const;
136 
137     /** Return a page descriptor for the given SdrPage.  Page descriptors
138         are created on demand.  The page descriptor is found (or not found)
139         in (at most) linear time.  Note that all page descriptors in front of
140         the one associated with the given XDrawPage are created when not yet
141         present. When the SdrPage is not found then all descriptors are
142         created.
143         @return
144             Returns the index to the requested page descriptor or -1 when
145             there is no such page descriptor.
146     */
147     sal_Int32 GetIndex (const SdrPage* pPage) const;
148 
149     /** Return an index for accessing an SdrModel that corresponds to the
150         given SlideSorterModel index.  In many cases we just have to apply
151         the n*2+1 magic.  Only when a special model is set, like a custom
152         slide show, then the returned value is different.
153     */
154     sal_uInt16 GetCoreIndex (const sal_Int32 nIndex) const;
155 
156     /** Call this method after the document has changed its structure.  This
157         will get the model in sync with the SdDrawDocument.  This method
158         tries not to throw away to much information already gathered.  This
159         is especially important for previews of complex pages that take some
160         time to create.
161     */
162     void Resync (void);
163 
164     /** Delete all descriptors that currently are in the container.  The size
165         of the container, however, is not altered.  Use the AdaptSize
166         method for that.
167     */
168     void ClearDescriptorList (void);
169 
170     /** Set the selection of the document to exactly that of the called model.
171     */
172     void SynchronizeDocumentSelection (void);
173 
174     /** Set the selection of the called model to exactly that of the document.
175     */
176     void SynchronizeModelSelection (void);
177 
178     /** Return the mutex so that the caller can lock it and then safely
179         access the model.
180     */
181     ::osl::Mutex& GetMutex (void);
182 
183     /** Set the XIndexAccess from which the called SlideSorterModel takes
184         its pages.
185         @param rxSlides
186             The set of slides accessible through this XIndexAccess are not
187             necessarily the same as the ones of the XModel of the
188             XController (although it typically is a subset).
189     */
190     void SetDocumentSlides (const css::uno::Reference<css::container::XIndexAccess>& rxSlides);
191 
192     /** Return the set of pages that is currently displayed by the slide sorter.
193     */
194     css::uno::Reference<css::container::XIndexAccess> GetDocumentSlides (void) const;
195 
196     /** This method is called when the edit mode has changed.  It calls
197         SetDocumentSlides() with the set of slides or master pages obtained
198         from the model of the XController.
199     */
200     void UpdatePageList (void);
201 
202     bool IsReadOnly (void) const;
203 
204     /** The current selection is saved by copying the ST_Selected state into
205         ST_WasSelected for slides.
206     */
207     void SaveCurrentSelection (void);
208 
209     /** The current selection is restored from the ST_WasSelected state from
210         the slides.
211         @returns
212             The returned region has to be repainted to reflect the updated
213             selection states.
214     */
215     Region RestoreSelection (void);
216 
217     /** Typically called from controller::Listener this method handles the
218         insertion and deletion of single pages.
219         @return
220             Returns <TRUE/> when the given page is relevant for the current
221             page kind and edit mode.
222     */
223     bool NotifyPageEvent (const SdrPage* pPage);
224 
225 private:
226     mutable ::osl::Mutex maMutex;
227     SlideSorter& mrSlideSorter;
228     ::com::sun::star::uno::Reference<com::sun::star::container::XIndexAccess> mxSlides;
229     PageKind mePageKind;
230     EditMode meEditMode;
231     typedef ::std::vector<SharedPageDescriptor> DescriptorContainer;
232     mutable DescriptorContainer maPageDescriptors;
233 
234     /** Resize the descriptor container according to current values of
235         page kind and edit mode.
236     */
237     void AdaptSize (void);
238 
239     SdPage* GetPage (const sal_Int32 nCoreIndex) const;
240     void InsertSlide (SdPage* pPage);
241     void DeleteSlide (const SdPage* pPage);
242     void UpdateIndices (const sal_Int32 nFirstIndex);
243 };
244 
245 } } } // end of namespace ::sd::slidesorter::model
246 
247 #endif
248