diff options
author | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2012-05-18 10:33:16 +0200 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2012-05-18 10:51:21 +0200 |
commit | d961b694348a0dd048761a6046de7393ac077f31 (patch) | |
tree | 1c18cdf513c5bc765fadefa1825d03e1be05dc83 /sc | |
parent | 862f56d19e1c57d3d648275877fc434fb119e80f (diff) |
implement databar import from oox
Change-Id: Id510acbc7291b94610e0e2c769d8cee582baa9a7
Diffstat (limited to 'sc')
-rw-r--r-- | sc/source/filter/inc/condformatbuffer.hxx | 21 | ||||
-rw-r--r-- | sc/source/filter/inc/condformatcontext.hxx | 13 | ||||
-rw-r--r-- | sc/source/filter/oox/condformatbuffer.cxx | 142 | ||||
-rw-r--r-- | sc/source/filter/oox/condformatcontext.cxx | 37 |
4 files changed, 203 insertions, 10 deletions
diff --git a/sc/source/filter/inc/condformatbuffer.hxx b/sc/source/filter/inc/condformatbuffer.hxx index b8b2d653fb41..de2c906166cc 100644 --- a/sc/source/filter/inc/condformatbuffer.hxx +++ b/sc/source/filter/inc/condformatbuffer.hxx @@ -39,6 +39,8 @@ namespace com { namespace sun { namespace star { namespace sheet { class XSheetConditionalEntries; } } } } class ScColorScaleFormat; +class ScDataBarFormat; +struct ScDataBarFormatData; namespace oox { namespace xls { @@ -112,6 +114,23 @@ private: sal_uInt32 mnCol; }; +class DataBarRule : public WorksheetHelper +{ +public: + DataBarRule( const CondFormat& rFormat ); + void importCfvo( const AttributeList& rAttribs ); + void importColor( const AttributeList& rAttribs ); + + void SetData( ScDataBarFormat* pFormat, ScDocument* pDoc, const ScAddress& rAddr ); + +private: + const CondFormat& mrCondFormat; + ScDataBarFormatData* mpFormat; + + boost::scoped_ptr<ColorScaleRuleModelEntry> mpUpperLimit; + boost::scoped_ptr<ColorScaleRuleModelEntry> mpLowerLimit; +}; + // ============================================================================ @@ -138,11 +157,13 @@ public: inline sal_Int32 getPriority() const { return maModel.mnPriority; } ColorScaleRule* getColorScale(); + DataBarRule* getDataBar(); private: const CondFormat& mrCondFormat; CondFormatRuleModel maModel; boost::scoped_ptr<ColorScaleRule> mpColor; + boost::scoped_ptr<DataBarRule> mpDataBar; }; typedef ::boost::shared_ptr< CondFormatRule > CondFormatRuleRef; diff --git a/sc/source/filter/inc/condformatcontext.hxx b/sc/source/filter/inc/condformatcontext.hxx index 7af625808bb4..08f53418fa59 100644 --- a/sc/source/filter/inc/condformatcontext.hxx +++ b/sc/source/filter/inc/condformatcontext.hxx @@ -48,8 +48,17 @@ public: virtual void onStartElement( const AttributeList& rAttribs ); virtual void onCharacters( const ::rtl::OUString& rChars ); - virtual ::oox::core::ContextHandlerRef onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm ); - virtual void onStartRecord( SequenceInputStream& rStrm ); +private: + CondFormatRuleRef mxRule; +}; + +class DataBarContext : public WorksheetContextBase +{ +public: + explicit DataBarContext( CondFormatContext& rFormat, CondFormatRuleRef xRule ); + + virtual ::oox::core::ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ); + virtual void onStartElement( const AttributeList& rAttribs ); private: CondFormatRuleRef mxRule; diff --git a/sc/source/filter/oox/condformatbuffer.cxx b/sc/source/filter/oox/condformatbuffer.cxx index 719f3a5d1c43..655b264364bc 100644 --- a/sc/source/filter/oox/condformatbuffer.cxx +++ b/sc/source/filter/oox/condformatbuffer.cxx @@ -224,12 +224,11 @@ void ColorScaleRule::importColor( const AttributeList& rAttribs ) ++mnCol; } -void ColorScaleRule::AddEntries( ScColorScaleFormat* pFormat, ScDocument* pDoc, const ScAddress& rAddr ) +namespace { + +ScColorScaleEntry* ConvertToModel( const ColorScaleRuleModelEntry& rEntry, ScDocument* pDoc, const ScAddress& rAddr ) { - for(size_t i = 0; i < maColorScaleRuleEntries.size(); ++i) - { - ScColorScaleEntry* pEntry = new ScColorScaleEntry(maColorScaleRuleEntries[i].mnVal, maColorScaleRuleEntries[i].maColor); - const ColorScaleRuleModelEntry& rEntry = maColorScaleRuleEntries[i]; + ScColorScaleEntry* pEntry = new ScColorScaleEntry(rEntry.mnVal, rEntry.maColor); if(rEntry.mbMin) pEntry->SetMin(true); @@ -241,11 +240,105 @@ void ColorScaleRule::AddEntries( ScColorScaleFormat* pFormat, ScDocument* pDoc, if(!rEntry.maFormula.isEmpty()) pEntry->SetFormula(rEntry.maFormula, pDoc, rAddr, formula::FormulaGrammar::GRAM_ENGLISH_XL_A1); + return pEntry; +} + +} + +void ColorScaleRule::AddEntries( ScColorScaleFormat* pFormat, ScDocument* pDoc, const ScAddress& rAddr ) +{ + for(size_t i = 0; i < maColorScaleRuleEntries.size(); ++i) + { + const ColorScaleRuleModelEntry& rEntry = maColorScaleRuleEntries[i]; + + ScColorScaleEntry* pEntry = ConvertToModel( rEntry, pDoc, rAddr ); + pFormat->AddEntry( pEntry ); } } // ============================================================================ +// +DataBarRule::DataBarRule( const CondFormat& rFormat ): + WorksheetHelper( rFormat ), + mrCondFormat( rFormat ), + mpFormat(new ScDataBarFormatData) +{ +} + +void DataBarRule::importColor( const AttributeList& rAttribs ) +{ + sal_Int32 nColor = 0; + if( rAttribs.hasAttribute( XML_rgb ) ) + nColor = rAttribs.getIntegerHex( XML_rgb, API_RGB_TRANSPARENT ); + else if( rAttribs.hasAttribute( XML_theme ) ) + { + sal_uInt32 nThemeIndex = rAttribs.getUnsigned( XML_theme, 0 ); + nColor = getTheme().getColorByToken( nThemeIndex ); + } + + ::Color aColor = RgbToRgbComponents( nColor ); + + mpFormat->maPositiveColor = aColor; +} + +void DataBarRule::importCfvo( const AttributeList& rAttribs ) +{ + ColorScaleRuleModelEntry* pEntry; + if(!mpLowerLimit) + { + mpLowerLimit.reset(new ColorScaleRuleModelEntry); + pEntry = mpLowerLimit.get(); + } + else + { + mpUpperLimit.reset(new ColorScaleRuleModelEntry); + pEntry = mpUpperLimit.get(); + } + rtl::OUString aType = rAttribs.getString( XML_type, rtl::OUString() ); + + double nVal = rAttribs.getDouble( XML_val, 0.0 ); + pEntry->mnVal = nVal; + if (aType == "num") + { + // nothing to do + } + else if( aType == "min" ) + { + pEntry->mbMin = true; + } + else if( aType == "max" ) + { + pEntry->mbMax = true; + } + else if( aType == "percent" ) + { + pEntry->mbPercent = true; + } + else if( aType == "percentile" ) + { + // this is most likely wrong but I have no idea what the difference + // between percent and percentile should be when calculating colors + pEntry->mbPercent = true; + } + else if( aType == "formula" ) + { + rtl::OUString aFormula = rAttribs.getString( XML_val, rtl::OUString() ); + pEntry->maFormula = aFormula; + } +} + +void DataBarRule::SetData( ScDataBarFormat* pFormat, ScDocument* pDoc, const ScAddress& rAddr ) +{ + ScColorScaleEntry* pUpperEntry = ConvertToModel( *mpUpperLimit.get(), pDoc, rAddr); + ScColorScaleEntry* pLowerEntry = ConvertToModel( *mpLowerLimit.get(), pDoc, rAddr); + + mpFormat->mpUpperLimit.reset( pUpperEntry ); + mpFormat->mpLowerLimit.reset( pLowerEntry ); + pFormat->SetDataBarData(mpFormat); +} + +// ============================================================================ CondFormatRuleModel::CondFormatRuleModel() : mnPriority( -1 ), @@ -712,7 +805,7 @@ void CondFormatRule::finalizeImport( const Reference< XSheetConditionalEntries > ScDocument& rDoc = getScDocument(); ScColorScaleFormat* pFormat = new ScColorScaleFormat(&rDoc); - sal_Int32 nIndex = rDoc.AddColorScaleFormat(pFormat); + sal_Int32 nIndex = rDoc.AddColorFormat(pFormat); ScRangeList aList; // apply attributes to cells @@ -736,15 +829,52 @@ void CondFormatRule::finalizeImport( const Reference< XSheetConditionalEntries > else mpColor->AddEntries( pFormat, &rDoc, ScAddress() ); } + else if (mpDataBar) + { + ScRangeList aList; + + ScDocument& rDoc = getScDocument(); + ScDataBarFormat* pFormat = new ScDataBarFormat(&rDoc); + + sal_Int32 nIndex = rDoc.AddColorFormat(pFormat); + + // apply attributes to cells + // + const ApiCellRangeList& rRanges = mrCondFormat.getRanges(); + for( ApiCellRangeList::const_iterator itr = rRanges.begin(); itr != rRanges.end(); ++itr) + { + ScRange aRange; + ScUnoConversion::FillScRange(aRange, *itr); + ScPatternAttr aPattern( rDoc.GetPool() ); + aPattern.GetItemSet().Put( SfxUInt32Item( ATTR_COLORSCALE, nIndex ) ); + ScMarkData aMarkData; + aMarkData.SetMarkArea(aRange); + rDoc.ApplySelectionPattern( aPattern , aMarkData); + + aList.Append(aRange); + } + pFormat->SetRange(aList); + + mpDataBar->SetData( pFormat, &rDoc, aList.front()->aStart ); + } } ColorScaleRule* CondFormatRule::getColorScale() { if(!mpColor) mpColor.reset( new ColorScaleRule(mrCondFormat) ); + return mpColor.get(); } +DataBarRule* CondFormatRule::getDataBar() +{ + if(!mpDataBar) + mpDataBar.reset( new DataBarRule(mrCondFormat) ); + + return mpDataBar.get(); +} + // ============================================================================ CondFormatModel::CondFormatModel() : diff --git a/sc/source/filter/oox/condformatcontext.cxx b/sc/source/filter/oox/condformatcontext.cxx index ff73a8ee21ab..01ad6a46e245 100644 --- a/sc/source/filter/oox/condformatcontext.cxx +++ b/sc/source/filter/oox/condformatcontext.cxx @@ -79,15 +79,46 @@ void ColorScaleContext::onCharacters( const OUString& ) } -ContextHandlerRef ColorScaleContext::onCreateRecordContext( sal_Int32, SequenceInputStream& ) +// ============================================================================ + +DataBarContext::DataBarContext( CondFormatContext& rFragment, CondFormatRuleRef xRule ) : + WorksheetContextBase( rFragment ), + mxRule( xRule ) +{ +} + +ContextHandlerRef DataBarContext::onCreateContext( sal_Int32 nElement, const AttributeList& ) { + switch( getCurrentElement() ) + { + case XLS_TOKEN( cfRule ): + return (nElement == XLS_TOKEN( colorScale )) ? this : 0; + case XLS_TOKEN( dataBar ): + if (nElement == XLS_TOKEN( cfvo )) + return this; + else if (nElement == XLS_TOKEN( color )) + return this; + else + return 0; + } return 0; } -void ColorScaleContext::onStartRecord( SequenceInputStream& ) +void DataBarContext::onStartElement( const AttributeList& rAttribs ) { + switch( getCurrentElement() ) + { + case XLS_TOKEN( cfvo ): + mxRule->getDataBar()->importCfvo( rAttribs ); + break; + case XLS_TOKEN( color ): + mxRule->getDataBar()->importColor( rAttribs ); + break; + } } +// ============================================================================ + CondFormatContext::CondFormatContext( WorksheetFragmentBase& rFragment ) : WorksheetContextBase( rFragment ) { @@ -104,6 +135,8 @@ ContextHandlerRef CondFormatContext::onCreateContext( sal_Int32 nElement, const return this; else if (nElement == XLS_TOKEN( colorScale ) ) return new ColorScaleContext( *this, mxRule ); + else if (nElement == XLS_TOKEN( dataBar ) ) + return new DataBarContext( *this, mxRule ); else return 0; } |