LibreOffice Module sw (master)  1
customizeaddresslistdialog.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
22 
24  weld::Window* pParent, const SwCSVData& rOldData)
25  : SfxDialogController(pParent, "modules/swriter/ui/customizeaddrlistdialog.ui",
26  "CustomizeAddrListDialog")
27  , m_xNewData(new SwCSVData(rOldData))
28  , m_xFieldsLB(m_xBuilder->weld_tree_view("treeview"))
29  , m_xAddPB(m_xBuilder->weld_button("add"))
30  , m_xDeletePB(m_xBuilder->weld_button("delete"))
31  , m_xRenamePB(m_xBuilder->weld_button("rename"))
32  , m_xUpPB(m_xBuilder->weld_button("up"))
33  , m_xDownPB(m_xBuilder->weld_button("down"))
34 {
35  m_xFieldsLB->set_size_request(-1, m_xFieldsLB->get_height_rows(14));
36 
37  m_xFieldsLB->connect_changed(LINK(this, SwCustomizeAddressListDialog, ListBoxSelectHdl_Impl));
38  Link<weld::Button&,void> aAddRenameLk = LINK(this, SwCustomizeAddressListDialog, AddRenameHdl_Impl );
39  m_xAddPB->connect_clicked(aAddRenameLk);
40  m_xRenamePB->connect_clicked(aAddRenameLk);
41  m_xDeletePB->connect_clicked(LINK(this, SwCustomizeAddressListDialog, DeleteHdl_Impl ));
42  Link<weld::Button&,void> aUpDownLk = LINK(this, SwCustomizeAddressListDialog, UpDownHdl_Impl);
43  m_xUpPB->connect_clicked(aUpDownLk);
44  m_xDownPB->connect_clicked(aUpDownLk);
45 
46  for (const auto& rHeader : m_xNewData->aDBColumnHeaders)
47  m_xFieldsLB->append_text(rHeader);
48 
49  m_xFieldsLB->select(0);
50  UpdateButtons();
51 }
52 
54 {
55 }
56 
58 {
59  UpdateButtons();
60 }
61 
62 IMPL_LINK(SwCustomizeAddressListDialog, AddRenameHdl_Impl, weld::Button&, rButton, void)
63 {
64  bool bRename = &rButton == m_xRenamePB.get();
65  auto nPos = m_xFieldsLB->get_selected_index();
66  if (nPos == -1)
67  nPos = 0;
68 
69  std::unique_ptr<SwAddRenameEntryDialog> xDlg;
70  if (bRename)
71  xDlg.reset(new SwRenameEntryDialog(m_xDialog.get(), m_xNewData->aDBColumnHeaders));
72  else
73  xDlg.reset(new SwAddEntryDialog(m_xDialog.get(), m_xNewData->aDBColumnHeaders));
74  if (bRename)
75  {
76  OUString aTemp = m_xFieldsLB->get_text(nPos);
77  xDlg->SetFieldName(aTemp);
78  }
79  if (xDlg->run() == RET_OK)
80  {
81  OUString sNew = xDlg->GetFieldName();
82  if(bRename)
83  {
84  m_xNewData->aDBColumnHeaders[nPos] = sNew;
85  m_xFieldsLB->remove(nPos);
86  }
87  else
88  {
89  if (m_xFieldsLB->get_selected_index() != -1)
90  ++nPos; // append the new entry behind the selected
91  //add the new column
92  m_xNewData->aDBColumnHeaders.insert(m_xNewData->aDBColumnHeaders.begin() + nPos, sNew);
93  //add a new entry into all data arrays
94  for (auto& rData : m_xNewData->aDBData)
95  rData.insert(rData.begin() + nPos, OUString());
96 
97  }
98 
99  m_xFieldsLB->insert_text(nPos, sNew);
100  m_xFieldsLB->select(nPos);
101  }
102  UpdateButtons();
103 }
104 
106 {
107  auto nPos = m_xFieldsLB->get_selected_index();
108  m_xFieldsLB->remove(nPos);
109  m_xFieldsLB->select(nPos > m_xFieldsLB->n_children() - 1 ? nPos - 1 : nPos);
110 
111  //remove the column
112  m_xNewData->aDBColumnHeaders.erase(m_xNewData->aDBColumnHeaders.begin() + nPos);
113  //remove the data
114  for (auto& rData : m_xNewData->aDBData)
115  rData.erase(rData.begin() + nPos);
116 
117  UpdateButtons();
118 }
119 
120 IMPL_LINK(SwCustomizeAddressListDialog, UpDownHdl_Impl, weld::Button&, rButton, void)
121 {
122  auto nPos = m_xFieldsLB->get_selected_index();
123  auto nOldPos = nPos;
124  OUString aTemp = m_xFieldsLB->get_text(nPos);
125  m_xFieldsLB->remove(nPos);
126  if (&rButton == m_xUpPB.get())
127  --nPos;
128  else
129  ++nPos;
130  m_xFieldsLB->insert_text(nPos, aTemp);
131  m_xFieldsLB->select(nPos);
132  //align m_xNewData
133  OUString sHeader = m_xNewData->aDBColumnHeaders[nOldPos];
134  m_xNewData->aDBColumnHeaders.erase(m_xNewData->aDBColumnHeaders.begin() + nOldPos);
135  m_xNewData->aDBColumnHeaders.insert(m_xNewData->aDBColumnHeaders.begin() + nPos, sHeader);
136  for (auto& rData : m_xNewData->aDBData)
137  {
138  OUString sData = rData[nOldPos];
139  rData.erase(rData.begin() + nOldPos);
140  rData.insert(rData.begin() + nPos, sData);
141  }
142 
143  UpdateButtons();
144 }
145 
147 {
148  auto nPos = m_xFieldsLB->get_selected_index();
149  auto nEntries = m_xFieldsLB->n_children();
150  m_xUpPB->set_sensitive(nPos > 0 && nEntries > 0);
151  m_xDownPB->set_sensitive(nPos < nEntries -1);
152  m_xDeletePB->set_sensitive(nEntries > 0);
153  m_xRenamePB->set_sensitive(nEntries > 0);
154 }
155 
157  weld::Window* pParent, const OUString& rUIXMLDescription, const OString& rID,
158  const std::vector< OUString >& rCSVHeader)
159  : SfxDialogController(pParent, rUIXMLDescription, rID)
160  , m_rCSVHeader(rCSVHeader)
161  , m_xFieldNameED(m_xBuilder->weld_entry("entry"))
162  , m_xOK(m_xBuilder->weld_button("ok"))
163 {
164  m_xFieldNameED->connect_changed(LINK(this, SwAddRenameEntryDialog, ModifyHdl_Impl));
165  ModifyHdl_Impl(*m_xFieldNameED);
166 }
167 
168 IMPL_LINK(SwAddRenameEntryDialog, ModifyHdl_Impl, weld::Entry&, rEdit, void)
169 {
170  OUString sEntry = rEdit.get_text();
171  bool bFound = sEntry.isEmpty();
172 
173  if(!bFound)
174  {
175  bFound = std::any_of(m_rCSVHeader.begin(), m_rCSVHeader.end(),
176  [&sEntry](const OUString& rHeader) { return rHeader == sEntry; });
177  }
178  m_xOK->set_sensitive(!bFound);
179 }
180 
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::TreeView > m_xFieldsLB
std::unique_ptr< weld::Button > m_xRenamePB
const ContentProperties & rData
std::unique_ptr< weld::Button > m_xDeletePB
std::unique_ptr< weld::Button > m_xAddPB
std::unique_ptr< weld::Entry > m_xFieldNameED
SwCustomizeAddressListDialog(weld::Window *pParent, const SwCSVData &rOldData)
SwAddRenameEntryDialog(weld::Window *pParent, const OUString &rUIXMLDescription, const OString &rID, const std::vector< OUString > &rCSVHeader)
std::unique_ptr< SwCSVData > m_xNewData
IMPL_LINK_NOARG(SwCustomizeAddressListDialog, ListBoxSelectHdl_Impl, weld::TreeView &, void)
virtual OUString get_text() const =0
std::unique_ptr< weld::Button > m_xUpPB
weld::Entry & rEdit
RET_OK
IMPL_LINK(SwCustomizeAddressListDialog, AddRenameHdl_Impl, weld::Button &, rButton, void)
Reference< XExecutableDialog > m_xDialog
std::unique_ptr< weld::Button > m_xDownPB
sal_uInt16 nPos