1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <com/sun/star/chart2/XChartDocument.hpp>
21 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 
23 #include <dlg_DataSource.hxx>
24 #include <ChartTypeTemplateProvider.hxx>
25 #include <DiagramHelper.hxx>
26 #include "DialogModel.hxx"
27 
28 #include "tp_RangeChooser.hxx"
29 #include "tp_DataSource.hxx"
30 
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::chart2;
33 using namespace ::chart;
34 
35 using ::com::sun::star::uno::Reference;
36 
37 namespace chart
38 {
39 
40 namespace {
41 
42 class DocumentChartTypeTemplateProvider : public ChartTypeTemplateProvider
43 {
44 public:
45     explicit DocumentChartTypeTemplateProvider(
46         const Reference< chart2::XChartDocument > & xDoc );
47 
48     // ____ ChartTypeTemplateProvider ____
49     virtual Reference< chart2::XChartTypeTemplate > getCurrentTemplate() const override;
50 
51 private:
52     Reference< chart2::XChartTypeTemplate > m_xTemplate;
53 };
54 
55 }
56 
DocumentChartTypeTemplateProvider(const Reference<chart2::XChartDocument> & xDoc)57 DocumentChartTypeTemplateProvider::DocumentChartTypeTemplateProvider(
58     const Reference< chart2::XChartDocument > & xDoc )
59 {
60     if( !xDoc.is())
61         return;
62 
63     Reference< chart2::XDiagram > xDia( xDoc->getFirstDiagram());
64     if( xDia.is())
65     {
66         DiagramHelper::tTemplateWithServiceName aResult(
67             DiagramHelper::getTemplateForDiagram(
68                 xDia,
69                 Reference< lang::XMultiServiceFactory >(
70                     xDoc->getChartTypeManager(), uno::UNO_QUERY ) ));
71         m_xTemplate.set( aResult.first );
72     }
73 }
74 
getCurrentTemplate() const75 Reference< chart2::XChartTypeTemplate > DocumentChartTypeTemplateProvider::getCurrentTemplate() const
76 {
77     return m_xTemplate;
78 }
79 
80 sal_uInt16 DataSourceDialog::m_nLastPageId = 0;
81 
DataSourceDialog(weld::Window * pParent,const Reference<XChartDocument> & xChartDocument,const Reference<uno::XComponentContext> & xContext)82 DataSourceDialog::DataSourceDialog(weld::Window * pParent,
83     const Reference< XChartDocument > & xChartDocument,
84     const Reference< uno::XComponentContext > & xContext)
85     : GenericDialogController(pParent, "modules/schart/ui/datarangedialog.ui",
86                               "DataRangeDialog")
87     , m_apDocTemplateProvider(new DocumentChartTypeTemplateProvider(xChartDocument))
88     , m_apDialogModel(new DialogModel(xChartDocument, xContext))
89     , m_bRangeChooserTabIsValid(true)
90     , m_bDataSourceTabIsValid(true)
91     , m_bTogglingEnabled(true)
92     , m_xTabControl(m_xBuilder->weld_notebook("tabcontrol"))
93     , m_xBtnOK(m_xBuilder->weld_button("ok"))
94 {
95     m_xRangeChooserTabPage = std::make_unique<RangeChooserTabPage>(m_xTabControl->get_page("range"), this,
96                                      *m_apDialogModel,
97                                      m_apDocTemplateProvider.get(), true /* bHideDescription */ );
98     m_xDataSourceTabPage = std::make_unique<DataSourceTabPage>(m_xTabControl->get_page("series"), this,
99                                     *m_apDialogModel,
100                                     m_apDocTemplateProvider.get(), true /* bHideDescription */ );
101     m_xTabControl->connect_enter_page(LINK(this, DataSourceDialog, ActivatePageHdl));
102     m_xTabControl->connect_leave_page(LINK(this, DataSourceDialog, DeactivatePageHdl));
103     ActivatePageHdl(m_xTabControl->get_current_page_ident());
104     if (m_nLastPageId != 0)
105     {
106         m_xTabControl->set_current_page(m_nLastPageId);
107         ActivatePageHdl(m_xTabControl->get_current_page_ident());
108     }
109 }
110 
~DataSourceDialog()111 DataSourceDialog::~DataSourceDialog()
112 {
113     m_xRangeChooserTabPage.reset();
114     m_xDataSourceTabPage.reset();
115     m_nLastPageId = m_xTabControl->get_current_page();
116 }
117 
run()118 short DataSourceDialog::run()
119 {
120     short nResult = GenericDialogController::run();
121     if( nResult == RET_OK )
122     {
123         if( m_xRangeChooserTabPage )
124             m_xRangeChooserTabPage->commitPage();
125         if( m_xDataSourceTabPage )
126             m_xDataSourceTabPage->commitPage();
127     }
128     return nResult;
129 }
130 
IMPL_LINK(DataSourceDialog,ActivatePageHdl,const OString &,rPage,void)131 IMPL_LINK(DataSourceDialog, ActivatePageHdl, const OString&, rPage, void)
132 {
133     if (rPage == "range")
134         m_xRangeChooserTabPage->Activate();
135     else if (rPage == "series")
136         m_xDataSourceTabPage->Activate();
137 }
138 
139 // allow/disallow user to leave page
IMPL_LINK_NOARG(DataSourceDialog,DeactivatePageHdl,const OString &,bool)140 IMPL_LINK_NOARG(DataSourceDialog, DeactivatePageHdl, const OString&, bool)
141 {
142     return m_bTogglingEnabled;
143 }
144 
setInvalidPage(BuilderPage * pTabPage)145 void DataSourceDialog::setInvalidPage(BuilderPage* pTabPage)
146 {
147     if (pTabPage == m_xRangeChooserTabPage.get())
148         m_bRangeChooserTabIsValid = false;
149     else if (pTabPage == m_xDataSourceTabPage.get())
150         m_bDataSourceTabIsValid = false;
151 
152     if (!(m_bRangeChooserTabIsValid && m_bDataSourceTabIsValid))
153     {
154         m_xBtnOK->set_sensitive(false);
155         // note: there seems to be no suitable mechanism to address pages by
156         // identifier, at least it is unclear what the page identifiers are.
157         // @todo: change the fixed numbers to identifiers
158         if( m_bRangeChooserTabIsValid )
159             m_xTabControl->set_current_page(1);
160         else if( m_bDataSourceTabIsValid )
161             m_xTabControl->set_current_page(0);
162         m_bTogglingEnabled = false;
163     }
164 }
165 
setValidPage(BuilderPage * pTabPage)166 void DataSourceDialog::setValidPage(BuilderPage* pTabPage)
167 {
168     if( pTabPage == m_xRangeChooserTabPage.get() )
169         m_bRangeChooserTabIsValid = true;
170     else if( pTabPage == m_xDataSourceTabPage.get() )
171         m_bDataSourceTabIsValid = true;
172 
173     if (m_bRangeChooserTabIsValid && m_bDataSourceTabIsValid)
174     {
175         m_xBtnOK->set_sensitive(true);
176         m_bTogglingEnabled = true;
177     }
178 }
179 
180 } //  namespace chart
181 
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
183