From 629a0173ea46bc9b8115e47a5138b19d07cdf52e Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Thu, 22 Aug 2019 09:15:15 -0400 Subject: sc: LOK: commit cell edits before saveas Users typically don't recognize that changes done to a cell need to be committed (typically by hitting RETURN) before they are saved to file. This is especially true on the web. This patch commits any in-flight changes before SaveAs. This is currently done only for LOK and unconditionally at that. This can be controlled via a flag, if there is such a use-case. Change-Id: I2a88b2f1df47be764058f4505561b22830d9d67a Reviewed-on: https://gerrit.libreoffice.org/78012 Reviewed-by: Ashod Nakashian Tested-by: Ashod Nakashian (cherry picked from commit cc7eb4d345e3fede698a3f255c1938d275305c14) Reviewed-on: https://gerrit.libreoffice.org/78149 Reviewed-by: Andras Timar Tested-by: Andras Timar --- desktop/qa/desktop_lib/test_desktop_lib.cxx | 56 ++++++++++++++++++++++++++--- include/sfx2/objsh.hxx | 2 ++ sc/source/ui/docshell/docsh.cxx | 6 ++++ sc/source/ui/inc/docsh.hxx | 2 ++ sfx2/source/doc/objserv.cxx | 1 - sfx2/source/doc/objstor.cxx | 7 ++++ 6 files changed, 69 insertions(+), 5 deletions(-) diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx index bc44c5a274eb..46b2dcb431fc 100644 --- a/desktop/qa/desktop_lib/test_desktop_lib.cxx +++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx @@ -80,6 +80,7 @@ public: UnoApiTest::tearDown(); }; + LibLODocument_Impl* loadDocUrl(const OUString& rFileURL, LibreOfficeKitDocumentType eType); LibLODocument_Impl* loadDoc(const char* pName, LibreOfficeKitDocumentType eType = LOK_DOCTYPE_TEXT); void closeDoc(); static void callback(int nType, const char* pPayload, void* pData); @@ -133,6 +134,7 @@ public: void testSignDocument_PEM_PDF(); void testTextSelectionHandles(); void testDialogPaste(); + void testCalcSaveAs(); void testABI(); CPPUNIT_TEST_SUITE(DesktopLOKTest); @@ -186,6 +188,7 @@ public: #endif CPPUNIT_TEST(testTextSelectionHandles); CPPUNIT_TEST(testDialogPaste); + CPPUNIT_TEST(testCalcSaveAs); CPPUNIT_TEST(testABI); CPPUNIT_TEST_SUITE_END(); @@ -230,10 +233,8 @@ static Control* GetFocusControl(vcl::Window const * pParent) return nullptr; } -LibLODocument_Impl* DesktopLOKTest::loadDoc(const char* pName, LibreOfficeKitDocumentType eType) +LibLODocument_Impl* DesktopLOKTest::loadDocUrl(const OUString& rFileURL, LibreOfficeKitDocumentType eType) { - OUString aFileURL; - createFileURL(OUString::createFromAscii(pName), aFileURL); OUString aService; switch (eType) { @@ -250,7 +251,7 @@ LibLODocument_Impl* DesktopLOKTest::loadDoc(const char* pName, LibreOfficeKitDoc CPPUNIT_ASSERT(false); break; } - mxComponent = loadFromDesktop(aFileURL, aService); + mxComponent = loadFromDesktop(rFileURL, aService); if (!mxComponent.is()) { CPPUNIT_ASSERT(false); @@ -258,6 +259,13 @@ LibLODocument_Impl* DesktopLOKTest::loadDoc(const char* pName, LibreOfficeKitDoc return new LibLODocument_Impl(mxComponent); } +LibLODocument_Impl* DesktopLOKTest::loadDoc(const char* pName, LibreOfficeKitDocumentType eType) +{ + OUString aFileURL; + createFileURL(OUString::createFromAscii(pName), aFileURL); + return loadDocUrl(aFileURL, eType); +} + void DesktopLOKTest::closeDoc() { if (mxComponent.is()) @@ -1827,6 +1835,7 @@ void DesktopLOKTest::testRedlineCalc() class ViewCallback { public: + OString m_aCellFormula; bool m_bTilesInvalidated; tools::Rectangle m_aOwnCursor; boost::property_tree::ptree m_aCommentCallbackResult; @@ -1871,6 +1880,11 @@ public: m_aCommentCallbackResult = m_aCommentCallbackResult.get_child("comment"); } break; + case LOK_CALLBACK_CELL_FORMULA: + { + m_aCellFormula = aPayload; + } + break; } } }; @@ -2653,6 +2667,40 @@ void DesktopLOKTest::testDialogPaste() comphelper::LibreOfficeKit::setActive(false); } +void DesktopLOKTest::testCalcSaveAs() +{ + comphelper::LibreOfficeKit::setActive(); + + LibLODocument_Impl* pDocument = loadDoc("sheets.ods"); + CPPUNIT_ASSERT(pDocument); + + // Enter some text, but don't commit. + pDocument->pClass->postKeyEvent(pDocument, LOK_KEYEVENT_KEYINPUT, 'X', 0); + pDocument->pClass->postKeyEvent(pDocument, LOK_KEYEVENT_KEYUP, 'X', 0); + Scheduler::ProcessEventsToIdle(); + + // Save as a new file. + OUString aNewFileUrl = "file:///tmp/saveas.ods"; + pDocument->pClass->saveAs(pDocument, aNewFileUrl.toUtf8().getStr(), nullptr, nullptr); + closeDoc(); + + // Load the new document and verify that the in-flight changes are saved. + pDocument = loadDocUrl(aNewFileUrl, LOK_DOCTYPE_SPREADSHEET); + CPPUNIT_ASSERT(pDocument); + + ViewCallback aView; + pDocument->m_pDocumentClass->initializeForRendering(pDocument, "{}"); + pDocument->m_pDocumentClass->registerCallback(pDocument, &ViewCallback::callback, &aView); + + pDocument->pClass->postKeyEvent(pDocument, LOK_KEYEVENT_KEYINPUT, 0, KEY_RIGHT); + pDocument->pClass->postKeyEvent(pDocument, LOK_KEYEVENT_KEYUP, 0, KEY_RIGHT); + pDocument->pClass->postKeyEvent(pDocument, LOK_KEYEVENT_KEYINPUT, 0, KEY_LEFT); + pDocument->pClass->postKeyEvent(pDocument, LOK_KEYEVENT_KEYUP, 0, KEY_LEFT); + Scheduler::ProcessEventsToIdle(); + + CPPUNIT_ASSERT_EQUAL(OString("X"), aView.m_aCellFormula); +} + namespace { size_t documentClassOffset(int i) diff --git a/include/sfx2/objsh.hxx b/include/sfx2/objsh.hxx index 64edfd781af3..aea0c7aa38d8 100644 --- a/include/sfx2/objsh.hxx +++ b/include/sfx2/objsh.hxx @@ -316,6 +316,8 @@ public: // TODO/LATER: currently only overridden in Calc, should be made non-virtual virtual bool DoSaveCompleted( SfxMedium* pNewStor=nullptr, bool bRegisterRecent=true ); + /// Terminate any in-flight editing. Used before saving, primarily by Calc to commit cell changes. + virtual void TerminateEditing() {} bool LoadOwnFormat( SfxMedium& pMedium ); virtual bool SaveAsOwnFormat( SfxMedium& pMedium ); diff --git a/sc/source/ui/docshell/docsh.cxx b/sc/source/ui/docshell/docsh.cxx index a2c6a5a69eb3..39f4770ef8c8 100644 --- a/sc/source/ui/docshell/docsh.cxx +++ b/sc/source/ui/docshell/docsh.cxx @@ -1766,6 +1766,12 @@ void popFileName(OUString& rPath) } +void ScDocShell::TerminateEditing() +{ + // Commit any cell changes before saving. + SC_MOD()->InputEnterHandler(); +} + bool ScDocShell::SaveAs( SfxMedium& rMedium ) { OUString aCurPath; // empty for new document that hasn't been saved. diff --git a/sc/source/ui/inc/docsh.hxx b/sc/source/ui/inc/docsh.hxx index 90c5292c828b..349dc278c3b3 100644 --- a/sc/source/ui/inc/docsh.hxx +++ b/sc/source/ui/inc/docsh.hxx @@ -210,6 +210,8 @@ public: virtual void SetVisArea( const tools::Rectangle & rVisArea ) override; + virtual void TerminateEditing() override; + using SfxObjectShell::GetVisArea; virtual tools::Rectangle GetVisArea( sal_uInt16 nAspect ) const override; diff --git a/sfx2/source/doc/objserv.cxx b/sfx2/source/doc/objserv.cxx index 8d1dc78c0ff8..dff7ccd79fe9 100644 --- a/sfx2/source/doc/objserv.cxx +++ b/sfx2/source/doc/objserv.cxx @@ -280,7 +280,6 @@ bool SfxObjectShell::APISaveAs_Impl(const OUString& aFileName, SfxItemSet& rItem { bool bOk = false; - if ( GetMedium() ) { OUString aFilterName; diff --git a/sfx2/source/doc/objstor.cxx b/sfx2/source/doc/objstor.cxx index 1b04019c95c3..5fa3d10a96f4 100644 --- a/sfx2/source/doc/objstor.cxx +++ b/sfx2/source/doc/objstor.cxx @@ -2672,6 +2672,7 @@ bool SfxObjectShell::CommonSaveAs_Impl(const INetURLObject& aURL, const OUString return false; } + const SfxBoolItem* pCopyStreamItem = rItemSet.GetItem(SID_COPY_STREAM_IF_POSSIBLE, false); if ( bSaveTo && pCopyStreamItem && pCopyStreamItem->GetValue() && !IsModified() ) { @@ -2818,6 +2819,12 @@ bool SfxObjectShell::PreDoSaveAs_Impl(const OUString& rFileName, const OUString& return false; } + if (comphelper::LibreOfficeKit::isActive()) + { + // Before saving, commit in-flight changes. + TerminateEditing(); + } + // check if a "SaveTo" is wanted, no "SaveAs" const SfxBoolItem* pSaveToItem = pMergedParams->GetItem(SID_SAVETO, false); bool bCopyTo = GetCreateMode() == SfxObjectCreateMode::EMBEDDED || (pSaveToItem && pSaveToItem->GetValue()); -- cgit