sc/qa/unit/dataproviders_test.cxx | 2 sc/source/ui/docshell/dataprovider.cxx | 98 ++++++++++++++++++++++++++++++--- sc/source/ui/inc/dataprovider.hxx | 33 ++++++++++- 3 files changed, 122 insertions(+), 11 deletions(-)
New commits: commit 991346d570713c230a81485c55e29318c2cc175d Author: Jaskaran Singh <jvsg1...@gmail.com> Date: Tue Jun 27 23:45:42 2017 +0530 Add a way to resize ScDBData Change-Id: I6e0e1a7f58873c012e0e8015ed69da38a32a44f4 Reviewed-on: https://gerrit.libreoffice.org/39323 Tested-by: Jenkins <c...@libreoffice.org> Reviewed-by: Markus Mohrhard <markus.mohrh...@googlemail.com> diff --git a/sc/qa/unit/dataproviders_test.cxx b/sc/qa/unit/dataproviders_test.cxx index 397fbfe4ce99..4dd967376f58 100644 --- a/sc/qa/unit/dataproviders_test.cxx +++ b/sc/qa/unit/dataproviders_test.cxx @@ -62,7 +62,7 @@ void ScDataProvidersTest::testCSVImport() OUString aCSVPath; createCSVPath( "dataprovider.", aCSVPath ); OUString aDBName = "TEST"; - sc::ExternalDataMapper aExternalDataMapper (&getDocShell(), aCSVPath, aDBName, 0, 0, 0, 5, 5, success); + sc::ExternalDataMapper aExternalDataMapper (&getDocShell(), aCSVPath, aDBName, 0, 0, 0, 5, 5, false, success); aExternalDataMapper.StartImport(); Scheduler::ProcessEventsToIdle(); CPPUNIT_ASSERT_EQUAL (-2012.0, m_pDoc->GetValue(0, 0, 0)); diff --git a/sc/source/ui/docshell/dataprovider.cxx b/sc/source/ui/docshell/dataprovider.cxx index a6c4d69c9b91..31b0732d8e73 100644 --- a/sc/source/ui/docshell/dataprovider.cxx +++ b/sc/source/ui/docshell/dataprovider.cxx @@ -57,16 +57,20 @@ std::unique_ptr<SvStream> FetchStreamFromURL(const OUString& rURL, OStringBuffer } ExternalDataMapper::ExternalDataMapper(ScDocShell* pDocShell, const OUString& rURL, const OUString& rName, SCTAB nTab, - SCCOL nCol1,SCROW nRow1, SCCOL nCol2, SCROW nRow2, bool& bSuccess): + SCCOL nCol1,SCROW nRow1, SCCOL nCol2, SCROW nRow2, bool bAllowResize, bool& bSuccess): maRange (ScRange(nCol1, nRow1, nTab, nCol2, nRow2, nTab)), mpDocShell(pDocShell), - mpDataProvider (new CSVDataProvider(mpDocShell, rURL, maRange)), mpDBCollection (pDocShell->GetDocument().GetDBCollection()) { bSuccess = true; ScDBCollection::NamedDBs& rNamedDBS = mpDBCollection->getNamedDBs(); - if(!rNamedDBS.insert (new ScDBData (rName, nTab, nCol1, nRow1, nCol2, nRow2))) + ScDBData* aDBData = new ScDBData (rName, nTab, nCol1, nRow1, nCol2, nRow2); + if(!rNamedDBS.insert (aDBData)) bSuccess = false; + mpDBDataManager = std::shared_ptr<ScDBDataManager>(new ScDBDataManager(aDBData, bAllowResize)); + mpDBDataManager->SetDestinationRange(maRange); + + mpDataProvider = std::unique_ptr<DataProvider> (new CSVDataProvider(mpDocShell, rURL, maRange, mpDBDataManager.get())); } ExternalDataMapper::~ExternalDataMapper() @@ -133,12 +137,13 @@ public: } }; -CSVFetchThread::CSVFetchThread(ScDocument& rDoc, const OUString& mrURL, size_t nColCount): +CSVFetchThread::CSVFetchThread(ScDocument& rDoc, ScDBDataManager* pBDDataManager, const OUString& mrURL, size_t nColCount): Thread("ReaderThread"), mpStream(nullptr), mrDocument(rDoc), maURL (mrURL), mnColCount(nColCount), + mpDBDataManager(pBDDataManager), mbTerminate(false) { maConfig.delimiters.push_back(','); @@ -175,6 +180,7 @@ void CSVFetchThread::execute() { LinesType aLines(10); SCROW nCurRow = 0; + SCCOL nCol = 0; for (Line & rLine : aLines) { rLine.maCells.clear(); @@ -188,7 +194,7 @@ void CSVFetchThread::execute() return; } - SCCOL nCol = 0; + nCol = 0; const char* pLineHead = rLine.maLine.getStr(); for (auto& rCell : rLine.maCells) { @@ -204,6 +210,8 @@ void CSVFetchThread::execute() } nCurRow++; } + mpDBDataManager->SetSourceRange(nCol, nCurRow); + } } @@ -235,15 +243,17 @@ void CSVFetchThread::ResumeFetchStream() maCondReadStream.set(); } -CSVDataProvider::CSVDataProvider(ScDocShell* pDocShell, const OUString& rURL, const ScRange& rRange): +CSVDataProvider::CSVDataProvider(ScDocShell* pDocShell, const OUString& rURL, ScRange& rRange, ScDBDataManager* pBDDataManager): maURL(rURL), mrRange(rRange), mpDocShell(pDocShell), mpDocument(&pDocShell->GetDocument()), + mpDBDataManager(pBDDataManager), mpLines(nullptr), mnLineCount(0), mbImportUnderway(false) { + mpDBDataManager->SetDestinationRange(rRange); } CSVDataProvider::~CSVDataProvider() @@ -258,7 +268,7 @@ void CSVDataProvider::StartImport() if (!mxCSVFetchThread.is()) { ScDocument aDoc; - mxCSVFetchThread = new CSVFetchThread(aDoc, maURL, mrRange.aEnd.Col() - mrRange.aStart.Col() + 1); + mxCSVFetchThread = new CSVFetchThread(aDoc, mpDBDataManager, maURL, mrRange.aEnd.Col() - mrRange.aStart.Col() + 1); mxCSVFetchThread->launch(); if (mxCSVFetchThread.is()) { @@ -302,6 +312,9 @@ Line CSVDataProvider::GetLine() void CSVDataProvider::WriteToDoc(ScDocument& rDoc) { + if (mpDBDataManager->Resize()) + mrRange = mpDBDataManager->GetDestinationRange(); + double* pfValue; for (int nRow = mrRange.aStart.Row(); nRow < mrRange.aEnd.Row(); ++nRow) { @@ -323,6 +336,77 @@ void CSVDataProvider::WriteToDoc(ScDocument& rDoc) } } +ScDBDataManager::ScDBDataManager(ScDBData* pDBData, bool bAllowResize = false): +mpDBData(pDBData), +mbAllowResize(bAllowResize) +{ +} + +ScDBDataManager::~ScDBDataManager() +{ +} + +void ScDBDataManager::SetDatabase(ScDBData* pDbData) +{ + mpDBData = pDbData; +} + +bool ScDBDataManager::IsResizeAllowed() +{ + return mbAllowResize; +} + +bool ScDBDataManager::RequiresResize(SCROW& RowDifference, SCCOL& ColDifference) +{ + SCROW nTotalSourceRows = maSourceRange.aStart.Row() - maSourceRange.aEnd.Row(); + SCCOL nTotalSourceCols = maSourceRange.aStart.Col() - maSourceRange.aEnd.Col(); + + SCROW nTotalDestinationRows = maDestinationRange.aStart.Row() - maDestinationRange.aEnd.Row(); + SCCOL nTotalDestinationCols = maDestinationRange.aStart.Col() - maDestinationRange.aEnd.Col(); + + RowDifference = nTotalSourceRows - nTotalDestinationRows; + ColDifference = nTotalSourceCols - nTotalDestinationCols; + + if (nTotalSourceRows != nTotalDestinationRows || nTotalSourceCols != nTotalDestinationCols) + return true; + + return false; +} + +bool ScDBDataManager::Resize() +{ + SCROW RowDifference =0; + SCCOL ColDifference = 0; + + if (IsResizeAllowed() && RequiresResize(RowDifference, ColDifference)) + { + maDestinationRange.aEnd = ScAddress(maDestinationRange.aEnd.Row() + RowDifference, maDestinationRange.aEnd.Col() + ColDifference, maDestinationRange.aEnd.Tab()); + + return true; + } + return false; +} + +void ScDBDataManager::SetSourceRange(SCCOL nCol, SCROW nRow) +{ + maSourceRange = ScRange(0, 0, 0, nCol, nRow, 0); +} + +void ScDBDataManager::SetDestinationRange(ScRange& aRange) +{ + maDestinationRange = aRange; +} + +ScRange& ScDBDataManager::GetSourceRange() +{ + return maSourceRange; +} + +ScRange& ScDBDataManager::GetDestinationRange() +{ + return maDestinationRange; +} + } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/inc/dataprovider.hxx b/sc/source/ui/inc/dataprovider.hxx index 8763cd1ab530..8f943ec082bc 100644 --- a/sc/source/ui/inc/dataprovider.hxx +++ b/sc/source/ui/inc/dataprovider.hxx @@ -37,6 +37,7 @@ namespace sc { class DataProvider; class CSVDataProvider; +class ScDBDataManager; class SC_DLLPUBLIC ExternalDataMapper { @@ -45,10 +46,11 @@ class SC_DLLPUBLIC ExternalDataMapper std::unique_ptr<DataProvider> mpDataProvider; ScDocument maDocument; ScDBCollection* mpDBCollection; + std::shared_ptr<ScDBDataManager> mpDBDataManager; public: ExternalDataMapper(ScDocShell* pDocShell, const OUString& rUrl, const OUString& rName, - SCTAB nTab, SCCOL nCol1,SCROW nRow1, SCCOL nCOL2, SCROW nRow2, bool& bSuccess); + SCTAB nTab, SCCOL nCol1,SCROW nRow1, SCCOL nCOL2, SCROW nRow2, bool bAllowResize, bool& bSuccess); ~ExternalDataMapper(); @@ -89,6 +91,7 @@ class CSVFetchThread : public salhelper::Thread ScDocument& mrDocument; OUString maURL; size_t mnColCount; + ScDBDataManager* mpDBDataManager; bool mbTerminate; osl::Mutex maMtxTerminate; @@ -104,7 +107,7 @@ class CSVFetchThread : public salhelper::Thread virtual void execute() override; public: - CSVFetchThread(ScDocument& rDoc, const OUString&, size_t); + CSVFetchThread(ScDocument& rDoc, ScDBDataManager*, const OUString&, size_t); virtual ~CSVFetchThread() override; void RequestTerminate(); @@ -139,6 +142,7 @@ class CSVDataProvider : public DataProvider rtl::Reference<CSVFetchThread> mxCSVFetchThread; ScDocShell* mpDocShell; ScDocument* mpDocument; + ScDBDataManager* mpDBDataManager; LinesType* mpLines; size_t mnLineCount; @@ -146,7 +150,7 @@ class CSVDataProvider : public DataProvider public: - CSVDataProvider (ScDocShell* pDocShell, const OUString& rUrl, const ScRange& rRange); + CSVDataProvider (ScDocShell* pDocShell, const OUString& rUrl, ScRange& rRange, ScDBDataManager*); virtual ~CSVDataProvider() override; virtual void StartImport() override; @@ -161,6 +165,29 @@ public: const OUString& GetURL() const override { return maURL; } }; +class ScDBDataManager +{ + ScDBData* mpDBData; + ScRange maSourceRange; + ScRange maDestinationRange; + bool mbAllowResize; + +public: + ScDBDataManager(ScDBData*, bool); + ~ScDBDataManager(); + + bool IsResizeAllowed(); + bool Resize(); + bool RequiresResize(SCROW&, SCCOL&); + + void SetDatabase(ScDBData*); + void SetSourceRange(SCCOL, SCROW); + void SetDestinationRange(ScRange&); + + ScRange& GetDestinationRange(); + ScRange& GetSourceRange(); +}; + } #endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits