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