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