tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / miscdlgs / mvtabdlg.cxx
blob822313b046f6d2838667187859fdf86c7814e0c9
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 #undef SC_DLLIMPLEMENTATION
22 #include <mvtabdlg.hxx>
23 #include <document.hxx>
24 #include <docsh.hxx>
25 #include <globstr.hrc>
26 #include <scresid.hxx>
27 #include <comphelper/lok.hxx>
28 #include <utility>
29 #include <tabvwsh.hxx>
31 ScMoveTableDlg::ScMoveTableDlg(weld::Window* pParent, OUString aDefault)
32 : GenericDialogController(pParent, u"modules/scalc/ui/movecopysheet.ui"_ustr,
33 u"MoveCopySheetDialog"_ustr)
34 , maDefaultName(std::move(aDefault))
35 , mnCurrentDocPos(0)
36 , nDocument(0)
37 , nTable(0)
38 , bCopyTable(false)
39 , bRenameTable(false)
40 , mbEverEdited(false)
41 , m_xBtnMove(m_xBuilder->weld_radio_button(u"move"_ustr))
42 , m_xBtnCopy(m_xBuilder->weld_radio_button(u"copy"_ustr))
43 , m_xFtDoc(m_xBuilder->weld_label(u"toDocumentLabel"_ustr))
44 , m_xLbDoc(m_xBuilder->weld_combo_box(u"toDocument"_ustr))
45 , m_xLbTable(m_xBuilder->weld_tree_view(u"insertBefore"_ustr))
46 , m_xEdTabName(m_xBuilder->weld_entry(u"newName"_ustr))
47 , m_xFtWarn(m_xBuilder->weld_label(u"newNameWarn"_ustr))
48 , m_xBtnOk(m_xBuilder->weld_button(u"ok"_ustr))
49 , m_xUnusedLabel(m_xBuilder->weld_label(u"warnunused"_ustr))
50 , m_xEmptyLabel(m_xBuilder->weld_label(u"warnempty"_ustr))
51 , m_xInvalidLabel(m_xBuilder->weld_label(u"warninvalid"_ustr))
53 assert(m_xLbDoc->get_count() == 2);
54 msCurrentDoc = m_xLbDoc->get_text(0);
55 msNewDoc = m_xLbDoc->get_text(1);
56 m_xLbDoc->clear();
57 assert(m_xLbDoc->get_count() == 0);
59 m_xLbTable->set_size_request(-1, m_xLbTable->get_height_rows(8));
61 msStrTabNameUsed = m_xUnusedLabel->get_label();
62 msStrTabNameEmpty = m_xEmptyLabel->get_label();
63 msStrTabNameInvalid = m_xInvalidLabel->get_label();
65 Init();
68 ScMoveTableDlg::~ScMoveTableDlg() {}
70 void ScMoveTableDlg::GetTabNameString(OUString& rString) const
72 rString = m_xEdTabName->get_text();
75 void ScMoveTableDlg::SetForceCopyTable()
77 m_xBtnCopy->set_active(true);
78 m_xBtnMove->set_sensitive(false);
79 SetOkBtnLabel();
82 void ScMoveTableDlg::EnableRenameTable(bool bFlag)
84 bRenameTable = bFlag;
85 m_xEdTabName->set_sensitive(bFlag);
86 ResetRenameInput();
89 void ScMoveTableDlg::ResetRenameInput()
91 if (mbEverEdited)
93 // Don't reset the name when the sheet name has ever been edited.
94 // But check the name, as this is also called for change of copy/move
95 // buttons and document listbox selection.
96 CheckNewTabName();
97 return;
100 if (!m_xEdTabName->get_sensitive())
102 m_xEdTabName->set_text(OUString());
103 return;
106 bool bVal = m_xBtnCopy->get_active();
107 if (bVal)
109 // copy
110 ScDocument* pDoc = GetSelectedDoc();
111 if (pDoc)
113 OUString aStr = maDefaultName;
114 pDoc->CreateValidTabName(aStr);
115 m_xEdTabName->set_text(aStr);
117 else
118 m_xEdTabName->set_text(maDefaultName);
120 else
122 // move
123 m_xEdTabName->set_text(maDefaultName);
126 CheckNewTabName();
129 void ScMoveTableDlg::CheckNewTabName()
131 const OUString aNewName = m_xEdTabName->get_text();
132 if (aNewName.isEmpty())
134 // New sheet name is empty. This is not good.
135 m_xFtWarn->show();
136 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
137 m_xFtWarn->set_label(msStrTabNameEmpty);
138 m_xBtnOk->set_sensitive(false);
139 return;
142 if (!ScDocument::ValidTabName(aNewName))
144 // New sheet name contains invalid characters.
145 m_xFtWarn->show();
146 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
147 m_xFtWarn->set_label(msStrTabNameInvalid);
148 m_xBtnOk->set_sensitive(false);
149 return;
152 bool bMoveInCurrentDoc = m_xBtnMove->get_active() && m_xLbDoc->get_active() == mnCurrentDocPos;
153 bool bFound = false;
154 const int nLast = m_xLbTable->n_children();
155 for (int i = 0; i < nLast && !bFound; ++i)
157 if (aNewName == m_xLbTable->get_text(i))
159 // Only for move within same document the same name is allowed.
160 if (!bMoveInCurrentDoc || maDefaultName != m_xEdTabName->get_text())
161 bFound = true;
165 if (bFound)
167 m_xFtWarn->show();
168 //TODO m_xFtWarn->SetControlBackground(COL_YELLOW);
169 m_xFtWarn->set_label(msStrTabNameUsed);
170 m_xBtnOk->set_sensitive(false);
172 else
174 m_xFtWarn->hide();
175 //TODO m_xFtWarn->SetControlBackground();
176 m_xFtWarn->set_label(OUString());
177 m_xBtnOk->set_sensitive(true);
181 ScDocument* ScMoveTableDlg::GetSelectedDoc()
183 return weld::fromId<ScDocument*>(m_xLbDoc->get_active_id());
186 void ScMoveTableDlg::Init()
188 m_xBtnOk->connect_clicked(LINK(this, ScMoveTableDlg, OkHdl));
189 m_xLbDoc->connect_changed(LINK(this, ScMoveTableDlg, SelHdl));
190 m_xBtnCopy->connect_toggled(LINK(this, ScMoveTableDlg, CheckBtnHdl));
191 m_xBtnMove->connect_toggled(LINK(this, ScMoveTableDlg, CheckBtnHdl));
192 m_xEdTabName->connect_changed(LINK(this, ScMoveTableDlg, CheckNameHdl));
194 // tdf#96854 - remember last used option for copy/move sheet
195 bool bIsCopyActive = false;
196 if (ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell())
197 bIsCopyActive = pViewSh->GetViewData().GetOptions().GetOption(VOPT_COPY_SHEET);
198 m_xBtnMove->set_active(!bIsCopyActive);
199 m_xBtnCopy->set_active(bIsCopyActive);
200 m_xEdTabName->set_sensitive(false);
201 m_xFtWarn->hide();
202 InitDocListBox();
203 SelHdl(*m_xLbDoc);
204 if (comphelper::LibreOfficeKit::isActive())
206 m_xFtDoc->hide();
207 m_xLbDoc->hide();
209 SetOkBtnLabel();
212 void ScMoveTableDlg::InitDocListBox()
214 SfxObjectShell* pSh = SfxObjectShell::GetFirst();
215 ScDocShell* pScSh = nullptr;
216 sal_uInt16 nSelPos = 0;
217 sal_uInt16 i = 0;
219 m_xLbDoc->clear();
220 m_xLbDoc->freeze();
222 while (pSh)
224 pScSh = dynamic_cast<ScDocShell*>(pSh);
226 if (pScSh)
228 OUString aEntryName = pScSh->GetTitle();
230 if (pScSh == SfxObjectShell::Current())
232 mnCurrentDocPos = nSelPos = i;
233 aEntryName += " " + msCurrentDoc;
236 OUString sId(weld::toId(&pScSh->GetDocument()));
237 m_xLbDoc->insert(i, aEntryName, &sId, nullptr, nullptr);
239 i++;
241 pSh = SfxObjectShell::GetNext(*pSh);
244 m_xLbDoc->thaw();
245 m_xLbDoc->append_text(msNewDoc);
246 m_xLbDoc->set_active(nSelPos);
249 void ScMoveTableDlg::SetOkBtnLabel()
251 const bool bIsCopyActive = m_xBtnCopy->get_active();
252 // tdf#139464 Write "Copy" or "Move" on OK button
253 m_xBtnOk->set_label(bIsCopyActive ? m_xBtnCopy->get_label() : m_xBtnMove->get_label());
254 // tdf#96854 - remember last used option for copy/move sheet
255 if (ScTabViewShell* pScViewShell = ScTabViewShell::GetActiveViewShell())
257 ScViewOptions aViewOpt(pScViewShell->GetViewData().GetOptions());
258 aViewOpt.SetOption(VOPT_COPY_SHEET, bIsCopyActive);
259 pScViewShell->GetViewData().SetOptions(aViewOpt);
263 // Handler:
265 IMPL_LINK(ScMoveTableDlg, CheckBtnHdl, weld::Toggleable&, rBtn, void)
267 if (&rBtn == m_xBtnCopy.get())
268 ResetRenameInput();
269 SetOkBtnLabel();
272 IMPL_LINK_NOARG(ScMoveTableDlg, OkHdl, weld::Button&, void)
274 const sal_Int32 nDocSel = m_xLbDoc->get_active();
275 const sal_Int32 nDocLast = m_xLbDoc->get_count() - 1;
276 const sal_Int32 nTabSel = m_xLbTable->get_selected_index();
277 const sal_Int32 nTabLast = m_xLbTable->n_children() - 1;
279 nDocument = (nDocSel != nDocLast) ? nDocSel : SC_DOC_NEW;
280 nTable = (nTabSel != nTabLast) ? static_cast<SCTAB>(nTabSel) : SC_TAB_APPEND;
281 bCopyTable = m_xBtnCopy->get_active();
283 if (bCopyTable)
285 // Return an empty string when the new name is the same as the
286 // automatic name assigned by the document.
287 OUString aCopyName = maDefaultName;
288 ScDocument* pDoc = GetSelectedDoc();
289 if (pDoc)
290 pDoc->CreateValidTabName(aCopyName);
291 if (aCopyName == m_xEdTabName->get_text())
292 m_xEdTabName->set_text(OUString());
294 else
296 // Return an empty string, when the new name is the same as the
297 // original name.
298 if (maDefaultName == m_xEdTabName->get_text())
299 m_xEdTabName->set_text(OUString());
302 m_xDialog->response(RET_OK);
305 IMPL_LINK_NOARG(ScMoveTableDlg, SelHdl, weld::ComboBox&, void)
307 ScDocument* pDoc = GetSelectedDoc();
308 OUString aName;
310 m_xLbTable->clear();
311 m_xLbTable->freeze();
312 if (pDoc)
314 SCTAB nLast = pDoc->GetTableCount() - 1;
315 for (SCTAB i = 0; i <= nLast; ++i)
317 pDoc->GetName(i, aName);
318 m_xLbTable->append_text(aName);
321 m_xLbTable->append_text(ScResId(STR_MOVE_TO_END));
322 m_xLbTable->thaw();
323 m_xLbTable->select(0);
324 ResetRenameInput();
327 IMPL_LINK_NOARG(ScMoveTableDlg, CheckNameHdl, weld::Entry&, void)
329 mbEverEdited = true;
330 CheckNewTabName();
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */