[Libreoffice-commits] core.git: Branch 'feature/unitver' - 2 commits - sc/inc sc/source

Andrzej Hunt andrzej at ahunt.org
Fri Aug 7 00:26:49 PDT 2015


 sc/inc/sc.hrc                  |    5 +
 sc/source/ui/inc/viewfunc.hxx  |    3 -
 sc/source/ui/src/units.src     |    5 +
 sc/source/ui/view/viewfunc.cxx |  114 +++++++++++++++++++++++++++++++++--------
 4 files changed, 104 insertions(+), 23 deletions(-)

New commits:
commit 54fc383bee9fe07388a9f9d5b6ae13a1e0e9eca0
Author: Andrzej Hunt <andrzej at ahunt.org>
Date:   Fri Aug 7 08:25:48 2015 +0100

    Implement undoing conversion of individual locally annotated cells
    
    Change-Id: I16b62f1105f7839b047a96fdab0e3e6089d400ee

diff --git a/sc/inc/sc.hrc b/sc/inc/sc.hrc
index 42a0561..fe78cd8 100644
--- a/sc/inc/sc.hrc
+++ b/sc/inc/sc.hrc
@@ -1005,7 +1005,10 @@
 #define STR_UNITS_CONVERSION_RECOMMENDED (STR_START + 455)
 #define BT_UNITS_CONVERT_THIS_CELL  (STR_START + 456)
 #define BT_UNITS_CONV_ALL       (STR_START + 457)
-#define STR_END                 (BT_UNITS_CONV_ALL)
+
+#define STR_UNDO_UNITSCONVERSION (STR_START + 458)
+
+#define STR_END                 (STR_UNDO_UNITSCONVERSION)
 
 #define BMP_START               (STR_END)
 
diff --git a/sc/source/ui/src/units.src b/sc/source/ui/src/units.src
index d666923..bc9a315 100644
--- a/sc/source/ui/src/units.src
+++ b/sc/source/ui/src/units.src
@@ -55,4 +55,9 @@ PushButton BT_UNITS_CONV_ALL
     Text [ en-US ] = "Convert (automatically for all cells needing $2)" ;
 };
 
+String STR_UNDO_UNITSCONVERSION
+{
+    Text [ en-US ] = "Units conversion";
+};
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/view/viewfunc.cxx b/sc/source/ui/view/viewfunc.cxx
index 063a3a8..a6f66e7 100644
--- a/sc/source/ui/view/viewfunc.cxx
+++ b/sc/source/ui/view/viewfunc.cxx
@@ -2976,8 +2976,7 @@ void ScViewFunc::NotifyUnitConversionRecommended( const ScAddress& rCellAddress,
                                                                                  rCellAddress,
                                                                                  pDoc,
                                                                                  rsHeaderUnit,
-                                                                                 rsCellUnit,
-                                                                                 pDocSh );
+                                                                                 rsCellUnit );
     pButtonConvertCell->SetClickHdl( LINK( this, ScViewFunc, UnitConversionRecommendedHandler ) );
 
     OUString sConvertText = pButtonConvertCell->GetText();
@@ -2994,32 +2993,108 @@ IMPL_LINK( ScViewFunc, UnitConversionRecommendedHandler, UnitConversionPushButto
 #ifdef ENABLE_CALC_UNITVERIFICATION
     boost::shared_ptr< Units > pUnits = Units::GetUnits();
 
-    ScDocShell* pDocShell = static_cast<ScDocShell*>(pButton->mpDoc->GetDocumentShell());
+    ScDocument* pDoc = pButton->mpDoc;
+    ScDocShell* pDocShell = static_cast<ScDocShell*>(pDoc->GetDocumentShell());
 
     ScDocShellModificator aModificator( *pDocShell );
+    bool bUndo = pDoc->IsUndoEnabled();
+    ScCellValue aOldVal;
+    ScDocument* pUndoDoc;
+    ScMarkData aMark;
 
-    OUString sOriginalValue = pButton->mpDoc->GetString( pButton->aCellAddress );
+    svl::IUndoManager* pUndoManager;
+    ScDrawLayer* pDrawLayer;
+
+    if ( bUndo )
+    {
+        pUndoManager = pDocShell->GetUndoManager();
+
+        aOldVal.assign( *pDoc, pButton->aCellAddress );
+
+        aMark.SetMarkArea( pButton->aCellAddress );
+        pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        pUndoDoc->InitUndo( pDoc, pButton->aCellAddress.Tab(), pButton->aCellAddress.Tab() );
+        pDoc->CopyToDocument( ScRange( pButton->aCellAddress ), IDF_ATTRIB, false, pUndoDoc, &aMark );
+
+        // This commences logging of changes to the drawing layer
+        // (i.e. notes) for storing in an undo. (See more below.)
+        pDrawLayer = pDoc->GetDrawLayer();
+        pDrawLayer->BeginCalcUndo(false);
+    }
+
+    OUString sOriginalValue = pDoc->GetString( pButton->aCellAddress );
 
     pUnits->convertCellToHeaderUnit( pButton->aCellAddress,
-                                     pButton->mpDoc,
+                                     pDoc,
                                      pButton->sHeaderUnit,
                                      pButton->sCellUnit );
 
-    ScPostIt* pNote = pButton->mpDoc->GetOrCreateNote( pButton->aCellAddress );
-    OUString sCurrentNote = pNote->GetText();
-
-    OUString sConversionNote("Original input: " + sOriginalValue);
+    const OUString sCurrentNoteText = pDoc->GetOrCreateNote( pButton->aCellAddress )->GetText();
+    const OUString sConversionNote("Original input: " + sOriginalValue);
+    OUString sNewNoteText;
 
-    if (sCurrentNote.isEmpty())
+    if (sCurrentNoteText.isEmpty())
     {
-        pNote->SetText( pButton->aCellAddress, sConversionNote );
+        sNewNoteText = sConversionNote;
     }
     else
     {
-        pNote->SetText( pButton->aCellAddress, sCurrentNote + "\n\n" + sConversionNote );
+        sNewNoteText = sCurrentNoteText + "\n\n" + sConversionNote;
     }
 
+    // ensure existing caption object before draw undo tracking starts
+    ScPostIt* pOldNote = pDoc->ReleaseNote( pButton->aCellAddress );
+    ScNoteData aOldNoteData = pOldNote->GetNoteData();
+    delete pOldNote;
+
+    ScPostIt* pNewNote = ScNoteUtil::CreateNoteFromString( *pDoc, pButton->aCellAddress, sNewNoteText, false, true );
+    assert( pNewNote );
+
+    if ( bUndo )
+    {
+        const OUString aUndo( ScResId( STR_UNDO_UNITSCONVERSION ) );
+        pUndoManager->EnterListAction( aUndo, aUndo );
+
+        ScCellValue aNewVal;
+        aNewVal.assign( *pDoc, pButton->aCellAddress );
+        const ScPatternAttr* pNewPat = pDoc->GetPattern( pButton->aCellAddress );
+
+        pUndoManager->AddUndoAction( new ScUndoSetCell( pDocShell, pButton->aCellAddress, aOldVal, aNewVal ) );
+        pUndoManager->AddUndoAction( new ScUndoSelectionAttr( pDocShell,
+                                                              aMark,
+                                                              pButton->aCellAddress.Col(), pButton->aCellAddress.Row(), pButton->aCellAddress.Tab(),
+                                                              pButton->aCellAddress.Col(), pButton->aCellAddress.Row(), pButton->aCellAddress.Tab(),
+                                                              pUndoDoc,
+                                                              false,
+                                                              pNewPat) );
+        ScNoteData aNewNoteData = pNewNote->GetNoteData();
+
+        // We specifically need to differentiate between a completely new
+        // note, or editing an existing note. ScUndoReplaceNote has two
+        // different constructors for each case.
+        if ( sCurrentNoteText.isEmpty() )
+        {
+            pUndoManager->AddUndoAction( new ScUndoReplaceNote( *pDocShell, pButton->aCellAddress,
+                                                                aNewNoteData, true, nullptr ) );
+        }
+        else
+        {
+            // Actual note content changes are stored in the DrawLayer CalcUndo,
+            // as opposed to being the NoteData (which just seems to store note
+            // metadata) - hence we need to make sure that we save that too.
+            pUndoManager->AddUndoAction( new ScUndoReplaceNote( *pDocShell, pButton->aCellAddress,
+                                                                aOldNoteData, aNewNoteData,
+                                                                pDrawLayer->GetCalcUndo() ) );
+        }
+        pUndoManager->LeaveListAction();
+    }
+
+    pDocShell->PostPaint(ScRange( pButton->aCellAddress), PAINT_GRID);
+
     aModificator.SetDocumentModified();
+    SfxGetpApp()->Broadcast(SfxSimpleHint(SC_HINT_AREAS_CHANGED));
+
+
 #endif
 
     OUString sAddress;
commit cbf554b8241a7e4fe033ef67ef9ee2a9e30f7cea
Author: Andrzej Hunt <andrzej at ahunt.org>
Date:   Thu Aug 6 09:20:40 2015 +0100

    Remove redundant parameters.
    
    Change-Id: I0230fdd69d4f60058ca4f0258c5098f77099a3e4

diff --git a/sc/source/ui/inc/viewfunc.hxx b/sc/source/ui/inc/viewfunc.hxx
index 5574fcf..c952c5a 100644
--- a/sc/source/ui/inc/viewfunc.hxx
+++ b/sc/source/ui/inc/viewfunc.hxx
@@ -386,8 +386,7 @@ private:
                                                      ScDocument* pDoc,
                                                      const OUString& sHeaderUnit,
                                                      const ScAddress& rHeaderAddress,
-                                                     const OUString& sCellUnit,
-                                                     ScDocShell* pDocSh );
+                                                     const OUString& sCellUnit );
     DECL_LINK( UnitConversionRecommendedHandler, UnitConversionPushButton* );
 };
 
diff --git a/sc/source/ui/view/viewfunc.cxx b/sc/source/ui/view/viewfunc.cxx
index 7220246..063a3a8 100644
--- a/sc/source/ui/view/viewfunc.cxx
+++ b/sc/source/ui/view/viewfunc.cxx
@@ -594,7 +594,7 @@ void ScViewFunc::EnterData( SCCOL nCol, SCROW nRow, SCTAB nTab,
                 ScAddress aHeaderAddress;
 
                 if ( pUnits->isCellConversionRecommended( aAddress, pDoc, sHeaderUnit, aHeaderAddress, sCellUnit ) )
-                    NotifyUnitConversionRecommended( aAddress, pDoc, sHeaderUnit, aHeaderAddress, sCellUnit, pDocSh );
+                    NotifyUnitConversionRecommended( aAddress, pDoc, sHeaderUnit, aHeaderAddress, sCellUnit );
                 else
                 {
                     SfxViewFrame* pViewFrame = GetViewData().GetViewShell()->GetFrame();
@@ -2932,21 +2932,18 @@ struct UnitConversionPushButton: public PushButton
     ScDocument* mpDoc;
     const OUString sHeaderUnit;
     const OUString sCellUnit;
-    ScDocShell* mpDocSh;
 
     UnitConversionPushButton( vcl::Window* pParent,
                               const ResId& rResId,
                               const ScAddress& rCellAddress,
                               ScDocument* pDoc,
                               const OUString& rsHeaderUnit,
-                              const OUString& rsCellUnit,
-                              ScDocShell* pDocSh ):
+                              const OUString& rsCellUnit ):
         PushButton( pParent, rResId ),
         aCellAddress( rCellAddress ),
         mpDoc( pDoc ),
         sHeaderUnit( rsHeaderUnit ),
-        sCellUnit( rsCellUnit ),
-        mpDocSh( pDocSh )
+        sCellUnit( rsCellUnit )
         {}
 };
 
@@ -2954,8 +2951,8 @@ void ScViewFunc::NotifyUnitConversionRecommended( const ScAddress& rCellAddress,
                                                   ScDocument* pDoc,
                                                   const OUString& rsHeaderUnit,
                                                   const ScAddress& rHeaderAddress,
-                                                  const OUString& rsCellUnit,
-                                                  ScDocShell* pDocSh ) {
+                                                  const OUString& rsCellUnit )
+{
     SfxViewFrame* pViewFrame = GetViewData().GetViewShell()->GetFrame();
 
     // As with NotifyUnitErrorInFormula we use the cell address as the infobar id.
@@ -2997,7 +2994,9 @@ IMPL_LINK( ScViewFunc, UnitConversionRecommendedHandler, UnitConversionPushButto
 #ifdef ENABLE_CALC_UNITVERIFICATION
     boost::shared_ptr< Units > pUnits = Units::GetUnits();
 
-    ScDocShellModificator aModificator( *pButton->mpDocSh );
+    ScDocShell* pDocShell = static_cast<ScDocShell*>(pButton->mpDoc->GetDocumentShell());
+
+    ScDocShellModificator aModificator( *pDocShell );
 
     OUString sOriginalValue = pButton->mpDoc->GetString( pButton->aCellAddress );
 


More information about the Libreoffice-commits mailing list