Skip to content

Commit

Permalink
tdf#107097 invoke internal DP and correctly handle "range" names
Browse files Browse the repository at this point in the history
When we copy/paste a pivot chart to another (new) document, we
"send" a chart data as ODC to the other document. In the new
document we can't use the pivot table (as there is none in this
document) so we read-in the table data from the document to the
internal data provider.
The problem was that we didn't match the (fake) range names from
the pivot table correctly in the internal data provider and the
data wasn't populated. This commit fixes that and changes the fake
range names to something that is easy to parse and matches the
names in internal data provider.

Change-Id: I9872160cca68abd91738a25bf9b3b27bc77ce38d
Reviewed-on: https://gerrit.libreoffice.org/37086
Tested-by: Jenkins <[email protected]>
Reviewed-by: Tomaž Vajngerl <[email protected]>
  • Loading branch information
quikee committed Apr 29, 2017
1 parent 0a3ded1 commit 9e5314f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ void ModelState::update( const Reference< frame::XModel > & xModel )
{
ChartModel& rModel = dynamic_cast<ChartModel&>(*xChartDoc.get());
bHasOwnData = rModel.hasInternalDataProvider();
bHasDataFromPivotTable = rModel.isDataFromPivotTable();
bHasDataFromPivotTable = !bHasOwnData && rModel.isDataFromPivotTable();
}

bHasMainTitle = TitleHelper::getTitle( TitleHelper::MAIN_TITLE, xModel ).is();
Expand Down
4 changes: 4 additions & 0 deletions chart2/source/tools/InternalDataProvider.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,10 @@ OUString SAL_CALL InternalDataProvider::convertRangeToXML( const OUString& aRang

OUString SAL_CALL InternalDataProvider::convertRangeFromXML( const OUString& aXMLRange )
{
const OUString aPivotTableID("PT@");
if (aXMLRange.startsWith(aPivotTableID))
return aXMLRange.copy(aPivotTableID.getLength(), aXMLRange.getLength() - aPivotTableID.getLength());

XMLRangeHelper::CellRange aRange( XMLRangeHelper::getCellRangeFromXMLString( aXMLRange ));
if( aRange.aUpperLeft.bIsEmpty )
{
Expand Down
4 changes: 4 additions & 0 deletions offapi/com/sun/star/chart2/data/XPivotTableDataProvider.idl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ interface XPivotTableDataProvider : com::sun::star::uno::XInterface
*/
void setPivotTableName([in] string sPivotTableName);

/** check if the associated pivot table exists
*/
boolean hasPivotTable();

/** creates a single data sequence of values for the given data series index.
*
* @param nIndex
Expand Down
8 changes: 2 additions & 6 deletions sc/inc/PivotTableDataProvider.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public:

virtual void SAL_CALL setPivotTableName(const OUString& sPivotTableName) override;

virtual sal_Bool SAL_CALL hasPivotTable() override;

virtual css::uno::Reference<css::chart2::data::XDataSequence> SAL_CALL
createDataSequenceOfValuesByIndex(sal_Int32 nIndex) override;
virtual css::uno::Reference<css::chart2::data::XDataSequence> SAL_CALL
Expand Down Expand Up @@ -147,12 +149,6 @@ private:
OUString const & sRoleValues, OUString const & sIdValues,
std::vector<ValueAndFormat> const & rValues);

void setLabeledDataSequence(css::uno::Reference<css::chart2::data::XLabeledDataSequence> & xResult,
OUString const & sRoleValues, OUString const & sIdValues,
std::vector<ValueAndFormat> const & rValues,
OUString const & sRoleLabel, OUString const & sIdLabel,
std::vector<ValueAndFormat> const & rLabel);

css::uno::Reference<css::chart2::data::XDataSequence> assignLabelsToDataSequence(size_t nIndex);

css::uno::Reference<css::chart2::data::XDataSequence> assignValuesToDataSequence(size_t nIndex);
Expand Down
62 changes: 38 additions & 24 deletions sc/source/ui/unoobj/PivotTableDataProvider.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ namespace sc
{
namespace
{
const OUString constIdCategories("categories");
const OUString constIdLabel("label");
const OUString constIdData("data");

const SfxItemPropertyMapEntry* lcl_GetDataProviderPropertyMap()
{
Expand All @@ -70,12 +73,17 @@ uno::Reference<frame::XModel> lcl_GetXModel(ScDocument * pDoc)

OUString lcl_identifierForData(sal_Int32 index)
{
return "Data " + OUString::number(index + 1);
return "PT@" + constIdData + " " + OUString::number(index);
}

OUString lcl_identifierForLabel(sal_Int32 index)
{
return "Label " + OUString::number(index + 1);
return "PT@" + constIdLabel + " " + OUString::number(index);
}

OUString lcl_identifierForCategories()
{
return "PT@" + constIdCategories;
}

} // end anonymous namespace
Expand Down Expand Up @@ -146,6 +154,7 @@ uno::Reference<chart2::data::XDataSource> SAL_CALL
PivotTableDataProvider::createDataSource(const uno::Sequence<beans::PropertyValue>& aArguments)
{
SolarMutexGuard aGuard;

if (!m_pDocument)
throw uno::RuntimeException();

Expand Down Expand Up @@ -186,7 +195,7 @@ uno::Reference<chart2::data::XDataSource> SAL_CALL

uno::Reference<chart2::data::XDataSource> xResult;

if (aRangeRepresentation == "Categories")
if (aRangeRepresentation == lcl_identifierForCategories())
xResult = createCategoriesDataSource(bOrientCol);
else
xResult = createValuesDataSource();
Expand Down Expand Up @@ -223,23 +232,14 @@ PivotTableDataProvider::createCategoriesDataSource(bool bOrientationIsColumn)
uno::Reference<chart2::data::XDataSource> xDataSource;
std::vector<uno::Reference<chart2::data::XLabeledDataSequence>> aLabeledSequences;

if (bOrientationIsColumn)
{
for (std::vector<ValueAndFormat> const & rCategories : m_aCategoriesColumnOrientation)
{
uno::Reference<chart2::data::XLabeledDataSequence> xResult = newLabeledDataSequence();
setLabeledDataSequenceValues(xResult, "categories", "Categories", rCategories);
aLabeledSequences.push_back(xResult);
}
}
else
std::vector<std::vector<ValueAndFormat>> const & rCategoriesVector = bOrientationIsColumn ? m_aCategoriesColumnOrientation
: m_aCategoriesRowOrientation;

for (std::vector<ValueAndFormat> const & rCategories : rCategoriesVector)
{
for (std::vector<ValueAndFormat> const & rCategories : m_aCategoriesRowOrientation)
{
uno::Reference<chart2::data::XLabeledDataSequence> xResult = newLabeledDataSequence();
setLabeledDataSequenceValues(xResult, "categories", "Categories", rCategories);
aLabeledSequences.push_back(xResult);
}
uno::Reference<chart2::data::XLabeledDataSequence> xResult = newLabeledDataSequence();
setLabeledDataSequenceValues(xResult, "categories", lcl_identifierForCategories(), rCategories);
aLabeledSequences.push_back(xResult);
}

xDataSource.set(new PivotTableDataSource(aLabeledSequences));
Expand Down Expand Up @@ -527,8 +527,8 @@ PivotTableDataProvider::assignValuesToDataSequence(size_t nIndex)
OUString sDataID = lcl_identifierForData(nIndex);

std::vector<ValueAndFormat> const & rRowOfData = m_aDataRowVector[size_t(nIndex)];
std::unique_ptr<PivotTableDataSequence> pSequence(new PivotTableDataSequence(m_pDocument, m_sPivotTableName,
sDataID, rRowOfData));
std::unique_ptr<PivotTableDataSequence> pSequence;
pSequence.reset(new PivotTableDataSequence(m_pDocument, m_sPivotTableName, sDataID, rRowOfData));
pSequence->setRole("values-y");
xDataSequence.set(pSequence.release());
return xDataSequence;
Expand Down Expand Up @@ -591,7 +591,7 @@ uno::Reference<chart2::data::XDataSource>
std::back_inserter(aFirstCategories));
}
uno::Reference<chart2::data::XLabeledDataSequence> xResult = newLabeledDataSequence();
setLabeledDataSequenceValues(xResult, "categories", "Categories", aFirstCategories);
setLabeledDataSequenceValues(xResult, "categories", lcl_identifierForCategories(), aFirstCategories);
aLabeledSequences.push_back(xResult);
}

Expand Down Expand Up @@ -704,6 +704,20 @@ void PivotTableDataProvider::setPivotTableName(const OUString& sPivotTableName)
m_sPivotTableName = sPivotTableName;
}

sal_Bool PivotTableDataProvider::hasPivotTable()
{
if (m_sPivotTableName.isEmpty())
return false;

ScDPCollection* pDPCollection = m_pDocument->GetDPCollection();
ScDPObject* pDPObject = pDPCollection->GetByName(m_sPivotTableName);

if (pDPObject)
return true;

return false;
}

uno::Reference<chart2::data::XDataSequence>
PivotTableDataProvider::createDataSequenceOfValuesByIndex(sal_Int32 nIndex)
{
Expand Down Expand Up @@ -741,8 +755,8 @@ uno::Reference<css::chart2::data::XDataSequence>

std::vector<ValueAndFormat> const & rCategories = m_aCategoriesColumnOrientation[0];

std::unique_ptr<PivotTableDataSequence> pSequence(new PivotTableDataSequence(m_pDocument, m_sPivotTableName,
"Categories", rCategories));
std::unique_ptr<PivotTableDataSequence> pSequence;
pSequence.reset(new PivotTableDataSequence(m_pDocument, m_sPivotTableName, lcl_identifierForCategories(), rCategories));
pSequence->setRole("categories");
xDataSequence.set(uno::Reference<chart2::data::XDataSequence>(pSequence.release()));

Expand Down
20 changes: 15 additions & 5 deletions xmloff/source/chart/SchXMLChartContext.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ SchXMLChartContext::SchXMLChartContext( SchXMLImportHelper& rImpHelper,
SchXMLChartContext::~SchXMLChartContext()
{}

bool lcl_hasServiceName(Reference<lang::XMultiServiceFactory> & xFactory, OUString const & rServiceName)
{
const uno::Sequence<OUString> aServiceNames(xFactory->getAvailableServiceNames());

return std::find(aServiceNames.begin(), aServiceNames.end(), rServiceName) != aServiceNames.end();
}

void lcl_setDataProvider(uno::Reference<chart2::XChartDocument> const & xChartDoc, OUString const & sDataPilotSource)
{
if (!xChartDoc.is())
Expand All @@ -264,21 +271,24 @@ void lcl_setDataProvider(uno::Reference<chart2::XChartDocument> const & xChartDo
if (bHasDataPilotSource)
aDataProviderServiceName = "com.sun.star.chart2.data.PivotTableDataProvider";

const uno::Sequence<OUString> aServiceNames(xFact->getAvailableServiceNames());

if (std::find(aServiceNames.begin(), aServiceNames.end(), aDataProviderServiceName) != aServiceNames.end())
if (lcl_hasServiceName(xFact, aDataProviderServiceName))
{
Reference<chart2::data::XDataProvider> xProvider(xFact->createInstance(aDataProviderServiceName), uno::UNO_QUERY);

if (xProvider.is())
{
xDataReceiver->attachDataProvider(xProvider);
if (bHasDataPilotSource)
{
Reference<chart2::data::XPivotTableDataProvider> xPivotTableDataProvider(xProvider, uno::UNO_QUERY);
xPivotTableDataProvider->setPivotTableName(sDataPilotSource);
xDataReceiver->attachDataProvider(xProvider);
bHasOwnData = !xPivotTableDataProvider->hasPivotTable();
}
else
{
xDataReceiver->attachDataProvider(xProvider);
bHasOwnData = false;
}
bHasOwnData = false;
}
}
}
Expand Down

0 comments on commit 9e5314f

Please sign in to comment.