tdf#155344 sw tracked table column: 1-click Accept/Reject

Now context menu of a changed table column shows Accept Change/
Reject Change menu items, which accept or reject
the deletion or insertion of the table column(s).
Also Accept Change/Reject Change icons of the Track Changes
toolbar are enabled, and do the same, if the cursor is not
before or in a tracked change.

Add the following labels to the Undo actions:

"Accept change: Column Inserted"
"Accept change: Column Deleted"
"Reject change: Column Inserted"
"Reject change: Column Deleted"

Follow-up to commit ffd8d20d368a885d6d786749278fa438573227a7
"tdf#150673 sw xmloff: import/export tracked table column".

Change-Id: Idff570dfcf8c3ce35b6223f801608779bf1cd28e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/152626
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/qa/uitest/table/tdf146145.py b/sw/qa/uitest/table/tdf146145.py
index 66cb749..84cda61 100644
--- a/sw/qa/uitest/table/tdf146145.py
+++ b/sw/qa/uitest/table/tdf146145.py
@@ -154,4 +154,100 @@ class tdf146145(UITestCase):
            # disabled Accept Track Change
            self.assertFalse(self.is_enabled_Accept_Track_Change())

   def test_tdf155344(self):
        with self.ui_test.load_file(get_url_for_data_file("TC-table-del-add.docx")) as self.document:

            # accept all tracked changes
            self.xUITest.executeCommand(".uno:AcceptAllTrackedChanges")
            # delete first table column
            self.xUITest.executeCommand(".uno:DeleteColumns")

            # Check enabling Accept/Reject Track Change icons
            # and Accept Change/Reject Change context menu items
            # on table columns with tracked deletion or insertion

            # enable Track Changes toolbar
            self.xUITest.executeCommand(".uno:AvailableToolbars?Toolbar:string=changes")

            xToolkit = self.xContext.ServiceManager.createInstance('com.sun.star.awt.Toolkit')
            xToolkit.processEventsToIdle()

            # cursor at changed text: Accept Track Change is enabled
            self.assertTrue(self.is_enabled_Accept_Track_Change())

            # cursor in a changed column, but not at changed text: Accept Track Change is enabled now
            self.xUITest.executeCommand(".uno:GoRight")
            xToolkit.processEventsToIdle()
            # This was false
            self.assertTrue(self.is_enabled_Accept_Track_Change())

            # cursor in a not changed column: Accept Track Change is disabled
            self.xUITest.executeCommand(".uno:GoRight")
            xToolkit.processEventsToIdle()
            while self.is_enabled_Accept_Track_Change():
                time.sleep(0.1)
            self.assertFalse(self.is_enabled_Accept_Track_Change())

            # check the fix again to avoid of the asynchron state changes
            self.xUITest.executeCommand(".uno:GoLeft")
            xToolkit.processEventsToIdle()
            while not self.is_enabled_Accept_Track_Change():
                time.sleep(0.1)
            self.assertTrue(self.is_enabled_Accept_Track_Change())

            # check 1-click accept of table column deletion (3 redlines in the column)

            # not at changed text, but Accept Track Change removes the whole column now

            tables = self.document.getTextTables()
            self.assertEqual(len(tables[0].getColumns()), 3)

            self.xUITest.executeCommand(".uno:AcceptTrackedChange")
            xToolkit.processEventsToIdle()
            self.assertEqual(len(tables[0].getColumns()), 2)

            self.xUITest.executeCommand(".uno:Undo")
            xToolkit.processEventsToIdle()
            self.assertEqual(len(tables[0].getColumns()), 3)

            self.xUITest.executeCommand(".uno:Redo")
            xToolkit.processEventsToIdle()
            self.assertEqual(len(tables[0].getColumns()), 2)

            # check 1-click reject of table column insertion (9 redlines in the 3 columns)

            self.xUITest.executeCommand(".uno:InsertColumnsBefore")
            self.xUITest.executeCommand(".uno:InsertColumnsBefore")
            self.xUITest.executeCommand(".uno:InsertColumnsBefore")
            xToolkit.processEventsToIdle()
            while self.is_enabled_Accept_Track_Change():
                time.sleep(0.1)
            self.assertFalse(self.is_enabled_Accept_Track_Change())

            self.assertEqual(len(tables[0].getColumns()), 5)

            # check the fix again to avoid of the asynchron state changes
            self.xUITest.executeCommand(".uno:GoLeft")
            xToolkit.processEventsToIdle()
            while not self.is_enabled_Accept_Track_Change():
                time.sleep(0.1)
            # This was false
            self.assertTrue(self.is_enabled_Accept_Track_Change())

            self.xUITest.executeCommand(".uno:RejectTrackedChange")

            self.assertEqual(len(tables[0].getColumns()), 2)

            self.xUITest.executeCommand(".uno:Undo")
            xToolkit.processEventsToIdle()
            self.assertEqual(len(tables[0].getColumns()), 5)

            self.xUITest.executeCommand(".uno:Redo")
            xToolkit.processEventsToIdle()
            self.assertEqual(len(tables[0].getColumns()), 2)

            self.xUITest.executeCommand(".uno:Undo")
            xToolkit.processEventsToIdle()
            self.assertEqual(len(tables[0].getColumns()), 5)

# vim: set shiftwidth=4 softtabstop=4 expandtab:
diff --git a/sw/source/uibase/uiview/view2.cxx b/sw/source/uibase/uiview/view2.cxx
index 45dcfdd..02823bb 100644
--- a/sw/source/uibase/uiview/view2.cxx
+++ b/sw/source/uibase/uiview/view2.cxx
@@ -927,7 +927,9 @@ void SwView::Execute(SfxRequest &rReq)
                if ( !pRedline && m_pWrtShell->IsCursorInTable() )
                {
                    nRedline = 0;
                    auto pTabLine = pCursor->Start()->GetNode().GetTableBox()->GetUpper();
                    auto pTabBox = pCursor->Start()->GetNode().GetTableBox();
                    auto pTabLine = pTabBox->GetUpper();
                    const SwTableNode* pTableNd = pCursor->Start()->GetNode().FindTableNode();

                    if ( RedlineType::None != pTabLine->GetRedlineType() )
                    {
@@ -968,6 +970,75 @@ void SwView::Execute(SfxRequest &rReq)
                            rSh.EndUndo( eUndoId, &aRewriter);
                        }
                    }
                    else if ( RedlineType::None != pTabBox->GetRedlineType() )
                    {
                        nRedline = pTabBox->GetRedline();

                        if ( nRedline != SwRedlineTable::npos )
                        {
                            bTableChange = true;

                            SwWrtShell& rSh = GetWrtShell();
                            SwRewriter aRewriter;

                            aRewriter.AddRule(UndoArg1, SwResId(
                                rRedlineTable[nRedline]->GetType() == RedlineType::Delete
                                    ? STR_REDLINE_TABLE_COLUMN_DELETE
                                    : STR_REDLINE_TABLE_COLUMN_INSERT ));

                            SwUndoId eUndoId =
                                (FN_REDLINE_ACCEPT_DIRECT == nSlot || FN_REDLINE_ACCEPT_TONEXT == nSlot)
                                    ? SwUndoId::ACCEPT_REDLINE
                                    : SwUndoId::REJECT_REDLINE;

                            // change only the cells with the same data
                            SwRedlineData aData(rRedlineTable[nRedline]->GetRedlineData(0));

                            // start from the first redline of the table to handle all the
                            // cells of the changed column(s)
                            while ( nRedline )
                            {
                                pRedline = rRedlineTable[nRedline-1];
                                SwTableBox* pTableBox = pRedline->Start()->GetNode().GetTableBox();
                                SwTableNode* pTableNode = pRedline->Start()->GetNode().FindTableNode();

                                // previous redline is not in the same table
                                if ( !pTableBox || pTableNode != pTableNd )
                                    break;

                                --nRedline;
                            }

                            rSh.StartUndo( eUndoId, &aRewriter);
                            while ( nRedline != SwRedlineTable::npos && nRedline < rRedlineTable.size() )
                            {
                                pRedline = rRedlineTable[nRedline];

                                // until next redline is not in the same table
                                SwTableBox* pTableBox = pRedline->Start()->GetNode().GetTableBox();
                                SwTableNode* pTableNode = pRedline->Start()->GetNode().FindTableNode();
                                if ( !pTableBox || pTableNode != pTableNd )
                                    break;

                                // skip cells which are not from the same author, same type change
                                // or timestamp, i.e. keep only the cells of the same tracked
                                // column insertion or deletion
                                if ( !pRedline->GetRedlineData(0).CanCombine(aData) ||
                                     // not a tracked cell change
                                     RedlineType::None == pTableBox->GetRedlineType() )
                                {
                                    ++nRedline;
                                    continue;
                                }

                                if (FN_REDLINE_ACCEPT_DIRECT == nSlot || FN_REDLINE_ACCEPT_TONEXT == nSlot)
                                    m_pWrtShell->AcceptRedline(nRedline);
                                else
                                    m_pWrtShell->RejectRedline(nRedline);
                            }
                            rSh.EndUndo( eUndoId, &aRewriter);
                        }
                    }
                }
                else
                {
diff --git a/sw/source/uibase/uiview/viewstat.cxx b/sw/source/uibase/uiview/viewstat.cxx
index 2bd7535..9f44aa2 100644
--- a/sw/source/uibase/uiview/viewstat.cxx
+++ b/sw/source/uibase/uiview/viewstat.cxx
@@ -429,7 +429,8 @@ void SwView::GetState(SfxItemSet &rSet)
                       // except in the case of an inserted or deleted table row
                       ( !m_pWrtShell->IsCursorInTable() ||
                           (pTableBox = pCursor->Start()->GetNode().GetTableBox() ) == nullptr ||
                           RedlineType::None == pTableBox->GetUpper()->GetRedlineType() ) )
                           (RedlineType::None == pTableBox->GetRedlineType() &&
                           RedlineType::None == pTableBox->GetUpper()->GetRedlineType()) ) )
                    {
                        bDisable = true;
                    }