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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 
27 #include "dlg_DataSource.hxx"
28 #include "dlg_DataSource.hrc"
29 #include "Strings.hrc"
30 #include "ResId.hxx"
31 #include "ChartTypeTemplateProvider.hxx"
32 #include "DiagramHelper.hxx"
33 #include "DialogModel.hxx"
34 #include "HelpIds.hrc"
35 
36 #include "tp_RangeChooser.hxx"
37 #include "tp_DataSource.hxx"
38 
39 // for RET_OK
40 #include <vcl/msgbox.hxx>
41 
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::chart2;
44 using namespace ::chart;
45 
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::uno::Sequence;
48 using ::rtl::OUString;
49 
50 // --------------------------------------------------------------------------------
51 
52 
53 namespace chart
54 {
55 
56 // ----------------------------------------
57 
58 class DocumentChartTypeTemplateProvider : public ChartTypeTemplateProvider
59 {
60 public:
61     DocumentChartTypeTemplateProvider(
62         const Reference< chart2::XChartDocument > & xDoc );
63     virtual ~DocumentChartTypeTemplateProvider();
64 
65     // ____ ChartTypeTemplateProvider ____
66     virtual Reference< chart2::XChartTypeTemplate > getCurrentTemplate() const;
67 
68 private:
69     Reference< chart2::XChartTypeTemplate > m_xTemplate;
70 };
71 
DocumentChartTypeTemplateProvider(const Reference<chart2::XChartDocument> & xDoc)72 DocumentChartTypeTemplateProvider::DocumentChartTypeTemplateProvider(
73     const Reference< chart2::XChartDocument > & xDoc )
74 {
75     if( xDoc.is())
76     {
77         Reference< chart2::XDiagram > xDia( xDoc->getFirstDiagram());
78         if( xDia.is())
79         {
80             DiagramHelper::tTemplateWithServiceName aResult(
81                 DiagramHelper::getTemplateForDiagram(
82                     xDia,
83                     Reference< lang::XMultiServiceFactory >(
84                         xDoc->getChartTypeManager(), uno::UNO_QUERY ) ));
85             m_xTemplate.set( aResult.first );
86         }
87     }
88 }
89 
~DocumentChartTypeTemplateProvider()90 DocumentChartTypeTemplateProvider::~DocumentChartTypeTemplateProvider()
91 {}
92 
getCurrentTemplate() const93 Reference< chart2::XChartTypeTemplate > DocumentChartTypeTemplateProvider::getCurrentTemplate() const
94 {
95     return m_xTemplate;
96 }
97 
98 // ----------------------------------------
99 
100 class DataSourceTabControl : public TabControl
101 {
102 public:
103     DataSourceTabControl( Window* pParent, const ResId& rResId );
104     ~DataSourceTabControl();
105 
106     virtual long DeactivatePage();
107 
108     void DisableTabToggling();
109     void EnableTabToggling();
110 
111 private:
112     bool m_bTogglingEnabled;
113 };
114 
DataSourceTabControl(Window * pParent,const ResId & rResId)115 DataSourceTabControl::DataSourceTabControl( Window* pParent, const ResId& rResId ) :
116         TabControl( pParent, rResId ),
117         m_bTogglingEnabled( true )
118 {}
119 
~DataSourceTabControl()120 DataSourceTabControl::~DataSourceTabControl()
121 {}
122 
123 // Note that the result is long, but is intended to be a bool
DeactivatePage()124 long DataSourceTabControl::DeactivatePage()
125 {
126     bool bCanDeactivate( TabControl::DeactivatePage() != 0 );
127 
128     bCanDeactivate = (bCanDeactivate && m_bTogglingEnabled);
129 
130     return (bCanDeactivate ? 1 : 0 );
131 }
132 
DisableTabToggling()133 void DataSourceTabControl::DisableTabToggling()
134 {
135     m_bTogglingEnabled = false;
136 }
137 
EnableTabToggling()138 void DataSourceTabControl::EnableTabToggling()
139 {
140     m_bTogglingEnabled = true;
141 }
142 
143 // ----------------------------------------
144 
145 sal_uInt16 DataSourceDialog::m_nLastPageId = 0;
146 
DataSourceDialog(Window * pParent,const Reference<XChartDocument> & xChartDocument,const Reference<uno::XComponentContext> & xContext)147 DataSourceDialog::DataSourceDialog(
148     Window * pParent,
149     const Reference< XChartDocument > & xChartDocument,
150     const Reference< uno::XComponentContext > & xContext ) :
151 
152         TabDialog( pParent, SchResId( DLG_DATA_SOURCE )),
153 
154         m_xChartDocument( xChartDocument ),
155         m_xContext( xContext ),
156         m_apDocTemplateProvider( new DocumentChartTypeTemplateProvider( xChartDocument )),
157         m_apDialogModel( new DialogModel( xChartDocument, xContext )),
158 
159         m_pTabControl( new DataSourceTabControl( this, SchResId( TABCTRL ) )),
160         m_aBtnOK( this, SchResId( BTN_OK ) ),
161         m_aBtnCancel( this, SchResId( BTN_CANCEL ) ),
162         m_aBtnHelp( this, SchResId( BTN_HELP ) ),
163 
164         m_pRangeChooserTabePage(0),
165         m_pDataSourceTabPage(0),
166         m_bRangeChooserTabIsValid( true ),
167         m_bDataSourceTabIsValid( true )
168 {
169     FreeResource();
170 
171     //don't create the tabpages before FreeResource, otherwise the help ids are not matched correctly
172     m_pRangeChooserTabePage = new RangeChooserTabPage( m_pTabControl, *(m_apDialogModel.get()),
173                                      m_apDocTemplateProvider.get(), this, true /* bHideDescription */ );
174     m_pDataSourceTabPage = new DataSourceTabPage( m_pTabControl, *(m_apDialogModel.get()),
175                                     m_apDocTemplateProvider.get(), this, true /* bHideDescription */ );
176 
177     m_pTabControl->InsertPage( TP_RANGECHOOSER, String( SchResId( STR_PAGE_DATA_RANGE )));
178     m_pTabControl->InsertPage( TP_DATA_SOURCE,  String( SchResId( STR_OBJECT_DATASERIES_PLURAL )));
179 
180     m_pTabControl->SetTabPage( TP_RANGECHOOSER, m_pRangeChooserTabePage );
181     m_pTabControl->SetTabPage( TP_DATA_SOURCE,  m_pDataSourceTabPage );
182 
183     m_pTabControl->SelectTabPage( m_nLastPageId );
184 
185     SetHelpId( HID_SCH_DLG_RANGES );
186 }
187 
~DataSourceDialog()188 DataSourceDialog::~DataSourceDialog()
189 {
190     delete m_pRangeChooserTabePage;
191     delete m_pDataSourceTabPage;
192 
193     m_nLastPageId = m_pTabControl->GetCurPageId();
194     delete m_pTabControl;
195 }
196 
Execute()197 short DataSourceDialog::Execute()
198 {
199     short nResult = TabDialog::Execute();
200     if( nResult == RET_OK )
201     {
202         if( m_pRangeChooserTabePage )
203             m_pRangeChooserTabePage->commitPage();
204         if( m_pDataSourceTabPage )
205             m_pDataSourceTabPage->commitPage();
206     }
207     return nResult;
208 }
209 
setInvalidPage(TabPage * pTabPage)210 void DataSourceDialog::setInvalidPage( TabPage * pTabPage )
211 {
212     if( pTabPage == m_pRangeChooserTabePage )
213         m_bRangeChooserTabIsValid = false;
214     else if( pTabPage == m_pDataSourceTabPage )
215         m_bDataSourceTabIsValid = false;
216 
217     if( ! (m_bRangeChooserTabIsValid && m_bDataSourceTabIsValid ))
218     {
219         m_aBtnOK.Enable( sal_False );
220         OSL_ASSERT( m_pTabControl );
221         // note: there seems to be no suitable mechanism to address pages by
222         // identifier, at least it is unclear what the page identifiers are.
223         // @todo: change the fixed numbers to identifiers
224         if( m_bRangeChooserTabIsValid )
225             m_pTabControl->SetCurPageId( m_pTabControl->GetPageId( 1 ));
226         else if( m_bDataSourceTabIsValid )
227             m_pTabControl->SetCurPageId( m_pTabControl->GetPageId( 0 ));
228         m_pTabControl->DisableTabToggling();
229     }
230 }
231 
setValidPage(TabPage * pTabPage)232 void DataSourceDialog::setValidPage( TabPage * pTabPage )
233 {
234     if( pTabPage == m_pRangeChooserTabePage )
235         m_bRangeChooserTabIsValid = true;
236     else if( pTabPage == m_pDataSourceTabPage )
237         m_bDataSourceTabIsValid = true;
238 
239     if( m_bRangeChooserTabIsValid && m_bDataSourceTabIsValid )
240     {
241         m_aBtnOK.Enable( sal_True );
242         OSL_ASSERT( m_pTabControl );
243         m_pTabControl->EnableTabToggling();
244     }
245 }
246 
247 
248 } //  namespace chart
249