tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / miscdlgs / duplicaterecordsdlg.cxx
blob0817d3a75914a1b7771d9613a48734d20a9a5ba8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <scresid.hxx>
21 #include <dbdata.hxx>
22 #include <cellvalue.hxx>
23 #include <duplicaterecordsdlg.hxx>
24 #include <string>
25 #include <strings.hrc>
26 #include <tabprotection.hxx>
27 #include <gridwin.hxx>
28 #include <vector>
29 #include <officecfg/Office/Calc.hxx>
31 ScDuplicateRecordsDlg::ScDuplicateRecordsDlg(weld::Window* pParent,
32 css::uno::Sequence<uno::Sequence<uno::Any>>& rData,
33 ScViewData& rViewData, ScRange& rRange)
34 : weld::GenericDialogController(pParent, "modules/scalc/ui/duplicaterecordsdlg.ui",
35 "DuplicateRecordsDialog")
36 , m_xIncludesHeaders(m_xBuilder->weld_check_button("includesheaders"))
37 , m_xRadioRow(m_xBuilder->weld_radio_button("row"))
38 , m_xRadioColumn(m_xBuilder->weld_radio_button("column"))
39 , m_xRadioSelect(m_xBuilder->weld_radio_button("select"))
40 , m_xRadioRemove(m_xBuilder->weld_radio_button("remove"))
41 , m_xCheckList(m_xBuilder->weld_tree_view("checklist"))
42 , m_xAllChkBtn(m_xBuilder->weld_check_button("allcheckbtn"))
43 , m_xOkBtn(m_xBuilder->weld_button("okbtn"))
44 , mrCellData(rData)
45 , mrRange(rRange)
46 , mrViewData(rViewData)
48 m_xCheckList->enable_toggle_buttons(weld::ColumnToggleType::Check);
49 m_xCheckList->connect_toggled(LINK(this, ScDuplicateRecordsDlg, RecordsChkHdl));
50 Init();
53 ScDuplicateRecordsDlg::~ScDuplicateRecordsDlg()
55 auto pChange(comphelper::ConfigurationChanges::create());
56 officecfg::Office::Calc::Misc::HandleDuplicateRecords::RemoveDuplicateRows::set(
57 m_xRadioRow->get_active(), pChange);
58 pChange->commit();
60 officecfg::Office::Calc::Misc::HandleDuplicateRecords::DataIncludesHeaders::set(
61 m_xIncludesHeaders->get_active(), pChange);
62 pChange->commit();
64 officecfg::Office::Calc::Misc::HandleDuplicateRecords::RemoveRecords::set(
65 m_xRadioRemove->get_active(), pChange);
66 pChange->commit();
69 void ScDuplicateRecordsDlg::SetDialogData(bool bToggle)
71 m_xCheckList->freeze();
72 m_xCheckList->clear();
74 if (m_xRadioRow->get_active())
76 if (m_xIncludesHeaders->get_active())
78 // insert the first row's contents
79 ScRefCellValue aCell;
80 for (SCCOL i = mrRange.aStart.Col(); i <= mrRange.aEnd.Col(); ++i)
82 aCell.assign(mrViewData.GetDocument(),
83 ScAddress{ i, mrRange.aStart.Row(), mrRange.aStart.Tab() });
84 InsertEntry(aCell.getString(&mrViewData.GetDocument()), bToggle);
87 else
89 for (int i = mrRange.aStart.Col(); i <= mrRange.aEnd.Col(); ++i)
91 OUString aStr(ScAddress(i, 0, mrViewData.GetTabNo())
92 .Format(ScRefFlags::COL_VALID, &mrViewData.GetDocument()));
93 InsertEntry(aStr, bToggle);
97 else
99 // insert row names
100 if (m_xIncludesHeaders->get_active())
102 ScRefCellValue aCell;
103 for (SCROW i = mrRange.aStart.Row(); i <= mrRange.aEnd.Row(); ++i)
105 aCell.assign(mrViewData.GetDocument(),
106 ScAddress{ mrRange.aStart.Col(), i, mrRange.aStart.Tab() });
107 InsertEntry(aCell.getString(&mrViewData.GetDocument()), bToggle);
110 else
112 for (int i = mrRange.aStart.Row() + 1; i <= mrRange.aEnd.Row() + 1; ++i)
114 std::string aStr = std::to_string(i);
115 InsertEntry(rtl::OUString::fromUtf8(aStr), bToggle);
119 m_xCheckList->thaw();
122 void ScDuplicateRecordsDlg::SetDialogLabels()
124 if (m_xRadioRow->get_active())
125 m_xIncludesHeaders->set_label(ScResId(STR_DUPLICATERECORDS_DATACONATINSCOLUMNHEADERS));
126 else
127 m_xIncludesHeaders->set_label(ScResId(STR_DUPLICATERECORDS_DATACONATINSROWHEADERS));
130 void ScDuplicateRecordsDlg::InsertEntry(const OUString& rTxt, bool bToggle)
132 m_xCheckList->append();
133 const int nRow = m_xCheckList->n_children() - 1;
134 m_xCheckList->set_toggle(nRow, bToggle ? TRISTATE_TRUE : TRISTATE_FALSE);
135 m_xCheckList->set_text(nRow, rTxt, 0);
136 m_xCheckList->set_sensitive(m_xAllChkBtn->get_state() != TRISTATE_TRUE);
139 void ScDuplicateRecordsDlg::Init()
141 m_xIncludesHeaders->connect_toggled(LINK(this, ScDuplicateRecordsDlg, HeaderCkbHdl));
142 m_xRadioRow->connect_toggled(LINK(this, ScDuplicateRecordsDlg, OrientationHdl));
143 // m_xRadioColumn->connect_toggled(LINK(this, ScDuplicateRecordsDlg, OrientationHdl));
144 m_xOkBtn->connect_clicked(LINK(this, ScDuplicateRecordsDlg, OkHdl));
145 m_xAllChkBtn->connect_toggled(LINK(this, ScDuplicateRecordsDlg, AllCheckBtnHdl));
147 const ScDocument& rDoc = mrViewData.GetDocument();
148 bool bIncludeHeaders
149 = officecfg::Office::Calc::Misc::HandleDuplicateRecords::DataIncludesHeaders::get();
151 ScDBCollection* pDBColl = rDoc.GetDBCollection();
152 const SCTAB nCurTab = mrViewData.GetTabNo();
153 if (pDBColl)
155 ScDBData* pDBData
156 = pDBColl->GetDBAtArea(nCurTab, mrRange.aStart.Col(), mrRange.aStart.Row(),
157 mrRange.aEnd.Col(), mrRange.aEnd.Row());
158 if (pDBData)
159 bIncludeHeaders = pDBData->HasHeader();
162 // defaults (find duplicate rows | data doesn't include headers)
163 m_xIncludesHeaders->set_active(bIncludeHeaders);
164 m_xRadioRow->set_active(
165 officecfg::Office::Calc::Misc::HandleDuplicateRecords::RemoveDuplicateRows::get());
166 m_xRadioColumn->set_active(
167 !officecfg::Office::Calc::Misc::HandleDuplicateRecords::RemoveDuplicateRows::get());
168 m_xRadioRemove->set_active(
169 officecfg::Office::Calc::Misc::HandleDuplicateRecords::RemoveRecords::get());
170 m_xRadioSelect->set_active(
171 !officecfg::Office::Calc::Misc::HandleDuplicateRecords::RemoveRecords::get());
173 SetDialogLabels();
174 m_xAllChkBtn->set_state(TRISTATE_FALSE);
175 SetDialogData(true);
178 IMPL_LINK_NOARG(ScDuplicateRecordsDlg, OrientationHdl, weld::Toggleable&, void)
180 SetDialogLabels();
181 SetDialogData(true);
184 IMPL_LINK_NOARG(ScDuplicateRecordsDlg, HeaderCkbHdl, weld::Toggleable&, void)
186 SetDialogData(true);
189 IMPL_LINK_NOARG(ScDuplicateRecordsDlg, RecordsChkHdl, const weld::TreeView::iter_col&, void)
191 int nRet = 0;
192 int nTotalCount = 0;
194 m_xCheckList->all_foreach([this, &nRet, &nTotalCount](weld::TreeIter& rEntry) {
195 ++nTotalCount;
196 if (m_xCheckList->get_toggle(rEntry) == TRISTATE_TRUE)
197 ++nRet;
198 return false;
201 if (nRet == nTotalCount)
202 m_xAllChkBtn->set_state(TRISTATE_TRUE);
203 else
204 m_xAllChkBtn->set_state(TRISTATE_FALSE);
207 IMPL_LINK_NOARG(ScDuplicateRecordsDlg, AllCheckBtnHdl, weld::Toggleable&, void)
209 SetDialogData(true);
212 IMPL_LINK_NOARG(ScDuplicateRecordsDlg, OkHdl, weld::Button&, void)
214 maResponse.bRemove = m_xRadioRemove->get_active();
215 maResponse.bDuplicateRows = m_xRadioRow->get_active();
216 maResponse.bIncludesHeaders = m_xIncludesHeaders->get_active();
217 int nCount = (maResponse.bDuplicateRows ? mrCellData[0].size() : mrCellData.size());
219 for (int i = 0; i < nCount; ++i)
221 if (m_xCheckList->get_toggle(i))
222 maResponse.vEntries.push_back(i);
225 m_xDialog->response(RET_OK);
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */