tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / miscdlgs / protectiondlg.cxx
blob041f50dfa0db4f2fad45a2a152426c361dfbd9e4
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 <protectiondlg.hxx>
21 #include <tabprotection.hxx>
22 #include <svl/PasswordHelper.hxx>
24 #include <vector>
26 namespace {
28 // The order must match that of the list box.
29 const std::vector<ScTableProtection::Option> aOptions = {
30 ScTableProtection::SELECT_LOCKED_CELLS,
31 ScTableProtection::SELECT_UNLOCKED_CELLS,
32 ScTableProtection::INSERT_COLUMNS,
33 ScTableProtection::INSERT_ROWS,
34 ScTableProtection::DELETE_COLUMNS,
35 ScTableProtection::DELETE_ROWS,
36 ScTableProtection::AUTOFILTER,
37 ScTableProtection::PIVOT_TABLES,
42 ScTableProtectionDlg::ScTableProtectionDlg(weld::Window* pParent)
43 : weld::GenericDialogController(pParent, u"modules/scalc/ui/protectsheetdlg.ui"_ustr, u"ProtectSheetDialog"_ustr)
44 , m_xBtnProtect(m_xBuilder->weld_check_button(u"protect"_ustr))
45 , m_xPasswords(m_xBuilder->weld_container(u"passwords"_ustr))
46 , m_xOptions(m_xBuilder->weld_container(u"options"_ustr))
47 , m_xPassword1Edit(m_xBuilder->weld_entry(u"password1"_ustr))
48 , m_xPassword2Edit(m_xBuilder->weld_entry(u"password2"_ustr))
49 , m_xPasswordStrengthBar(m_xBuilder->weld_level_bar(u"passwordbar"_ustr))
50 , m_xOptionsListBox(m_xBuilder->weld_tree_view(u"checklist"_ustr))
51 , m_xBtnOk(m_xBuilder->weld_button(u"ok"_ustr))
52 , m_xProtected(m_xBuilder->weld_label(u"protected"_ustr))
53 , m_xUnprotected(m_xBuilder->weld_label(u"unprotected"_ustr))
54 , m_xInsertColumns(m_xBuilder->weld_label(u"insert-columns"_ustr))
55 , m_xInsertRows(m_xBuilder->weld_label(u"insert-rows"_ustr))
56 , m_xDeleteColumns(m_xBuilder->weld_label(u"delete-columns"_ustr))
57 , m_xDeleteRows(m_xBuilder->weld_label(u"delete-rows"_ustr))
58 , m_xAutoFilter(m_xBuilder->weld_label(u"useautofilter"_ustr))
59 , m_xPivot(m_xBuilder->weld_label(u"usepivot"_ustr))
61 m_aSelectLockedCells = m_xProtected->get_label();
62 m_aSelectUnlockedCells = m_xUnprotected->get_label();
63 m_aInsertColumns = m_xInsertColumns->get_label();
64 m_aInsertRows = m_xInsertRows->get_label();
65 m_aDeleteColumns = m_xDeleteColumns->get_label();
66 m_aDeleteRows = m_xDeleteRows->get_label();
67 m_aAutoFilter = m_xAutoFilter->get_label();
68 m_aPivot = m_xPivot->get_label();
70 m_xOptionsListBox->enable_toggle_buttons(weld::ColumnToggleType::Check);
72 Init();
75 ScTableProtectionDlg::~ScTableProtectionDlg()
79 void ScTableProtectionDlg::SetDialogData(const ScTableProtection& rData)
81 for (size_t i = 0; i < aOptions.size(); ++i)
82 m_xOptionsListBox->set_toggle(i, rData.isOptionEnabled(aOptions[i]) ? TRISTATE_TRUE : TRISTATE_FALSE);
85 void ScTableProtectionDlg::WriteData(ScTableProtection& rData) const
87 rData.setProtected(m_xBtnProtect->get_active());
89 // We assume that the two password texts match.
90 rData.setPassword(m_xPassword1Edit->get_text());
92 for (size_t i = 0; i < aOptions.size(); ++i)
93 rData.setOption(aOptions[i], m_xOptionsListBox->get_toggle(i) == TRISTATE_TRUE);
96 void ScTableProtectionDlg::InsertEntry(const OUString& rTxt)
98 m_xOptionsListBox->append();
99 const int nRow = m_xOptionsListBox->n_children() - 1;
100 m_xOptionsListBox->set_toggle(nRow, TRISTATE_FALSE);
101 m_xOptionsListBox->set_text(nRow, rTxt, 0);
104 void ScTableProtectionDlg::Init()
106 m_xBtnProtect->connect_toggled(LINK(this, ScTableProtectionDlg, CheckBoxHdl));
108 m_xBtnOk->connect_clicked(LINK(this, ScTableProtectionDlg, OKHdl));
110 Link<weld::Entry&,void> aLink = LINK(this, ScTableProtectionDlg, PasswordModifyHdl);
111 m_xPassword1Edit->connect_changed(aLink);
112 m_xPassword2Edit->connect_changed(aLink);
114 m_xOptionsListBox->freeze();
115 m_xOptionsListBox->clear();
117 InsertEntry(m_aSelectLockedCells);
118 InsertEntry(m_aSelectUnlockedCells);
119 InsertEntry(m_aInsertColumns);
120 InsertEntry(m_aInsertRows);
121 InsertEntry(m_aDeleteColumns);
122 InsertEntry(m_aDeleteRows);
123 InsertEntry(m_aAutoFilter);
124 InsertEntry(m_aPivot);
126 m_xOptionsListBox->set_toggle(0, TRISTATE_TRUE);
127 m_xOptionsListBox->set_toggle(1, TRISTATE_TRUE);
129 m_xOptionsListBox->thaw();
131 // Set the default state of the dialog.
132 m_xBtnProtect->set_active(true);
133 m_xPassword1Edit->grab_focus();
136 void ScTableProtectionDlg::EnableOptionalWidgets(bool bEnable)
138 m_xPasswords->set_sensitive(bEnable);
139 m_xOptions->set_sensitive(bEnable);
142 IMPL_LINK(ScTableProtectionDlg, CheckBoxHdl, weld::Toggleable&, rBtn, void)
144 if (&rBtn == m_xBtnProtect.get())
146 bool bChecked = m_xBtnProtect->get_active();
147 EnableOptionalWidgets(bChecked);
148 m_xBtnOk->set_sensitive(bChecked);
152 IMPL_LINK_NOARG(ScTableProtectionDlg, OKHdl, weld::Button&, void)
154 m_xDialog->response(RET_OK);
157 IMPL_LINK(ScTableProtectionDlg, PasswordModifyHdl, weld::Entry&, rEntry, void)
159 OUString aPass1 = m_xPassword1Edit->get_text();
160 if (&rEntry == m_xPassword1Edit.get())
162 m_xPasswordStrengthBar->set_percentage(
163 SvPasswordHelper::GetPasswordStrengthPercentage(aPass1));
165 OUString aPass2 = m_xPassword2Edit->get_text();
166 m_xBtnOk->set_sensitive(aPass1 == aPass2);
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */