LibreOffice Module dbaccess (master) 1
WNameMatch.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 <WNameMatch.hxx>
21#include <osl/diagnose.h>
22#include <FieldDescriptions.hxx>
23#include <WCopyTable.hxx>
24#include <core_resource.hxx>
25#include <strings.hrc>
26#include <bitmaps.hlst>
27#include <com/sun/star/sdbc/DataType.hpp>
28
29using namespace ::dbaui;
30
31// OWizColumnSelect
32OWizNameMatching::OWizNameMatching(weld::Container* pPage, OCopyTableWizard* pWizard)
33 : OWizardPage(pPage, pWizard, "dbaccess/ui/namematchingpage.ui", "NameMatching")
34 , m_xTABLE_LEFT(m_xBuilder->weld_label("leftlabel"))
35 , m_xTABLE_RIGHT(m_xBuilder->weld_label("rightlabel"))
36 , m_xCTRL_LEFT(m_xBuilder->weld_tree_view("left"))
37 , m_xCTRL_RIGHT(m_xBuilder->weld_tree_view("right"))
38 , m_xColumn_up(m_xBuilder->weld_button("up"))
39 , m_xColumn_down(m_xBuilder->weld_button("down"))
40 , m_xColumn_up_right(m_xBuilder->weld_button("up_right"))
41 , m_xColumn_down_right(m_xBuilder->weld_button("down_right"))
42 , m_xAll(m_xBuilder->weld_button("all"))
43 , m_xNone(m_xBuilder->weld_button("none"))
44{
45 OUString aImgUp(BMP_UP);
46 OUString aImgDown(BMP_DOWN);
47 m_xColumn_up->set_from_icon_name(aImgUp);
48 m_xColumn_down->set_from_icon_name(aImgDown);
49 m_xColumn_up_right->set_from_icon_name(aImgUp);
50 m_xColumn_down_right->set_from_icon_name(aImgDown);
51
52 m_xColumn_up->connect_clicked(LINK(this,OWizNameMatching,ButtonClickHdl));
53 m_xColumn_down->connect_clicked(LINK(this,OWizNameMatching,ButtonClickHdl));
54
55 m_xColumn_up_right->connect_clicked(LINK(this,OWizNameMatching,RightButtonClickHdl));
56 m_xColumn_down_right->connect_clicked(LINK(this,OWizNameMatching,RightButtonClickHdl));
57
58 m_xAll->connect_clicked(LINK(this,OWizNameMatching,AllNoneClickHdl));
59 m_xNone->connect_clicked(LINK(this,OWizNameMatching,AllNoneClickHdl));
60
61 m_xCTRL_LEFT->enable_toggle_buttons(weld::ColumnToggleType::Check);
62
63 m_xCTRL_LEFT->connect_changed(LINK(this,OWizNameMatching,TableListClickHdl));
64 m_xCTRL_RIGHT->connect_changed(LINK(this,OWizNameMatching,TableListRightSelectHdl));
65
66 m_sSourceText = m_xTABLE_LEFT->get_label() + "\n";
67 m_sDestText = m_xTABLE_RIGHT->get_label() + "\n";
68}
69
71{
72}
73
75{
76 m_bFirstTime = false;
77}
78
80{
81 // set source table name
82 OUString aName = m_sSourceText + m_pParent->m_sSourceName;
83
84 m_xTABLE_LEFT->set_label(aName);
85
86 // set dest table name
87 aName = m_sDestText + m_pParent->m_sName;
88 m_xTABLE_RIGHT->set_label(aName);
89
90 FillListBox(*m_xCTRL_LEFT, m_pParent->getSrcVector(), true);
91 FillListBox(*m_xCTRL_RIGHT, m_pParent->getDestVector(), false);
92
93 m_xColumn_up->set_sensitive( m_xCTRL_LEFT->n_children() > 1 );
94 m_xColumn_down->set_sensitive( m_xCTRL_LEFT->n_children() > 1 );
95
96 m_xColumn_up_right->set_sensitive( m_xCTRL_RIGHT->n_children() > 1 );
97 m_xColumn_down_right->set_sensitive( m_xCTRL_RIGHT->n_children() > 1 );
98
99 m_pParent->EnableNextButton(false);
100 m_xCTRL_LEFT->grab_focus();
101 TableListClickHdl(*m_xCTRL_LEFT);
102}
103
105{
106
107 const ODatabaseExport::TColumnVector& rSrcColumns = m_pParent->getSrcVector();
108
109 m_pParent->m_vColumnPositions.clear();
110 m_pParent->m_vColumnTypes.clear();
111 m_pParent->m_vColumnPositions.resize( rSrcColumns.size(), ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND, COLUMN_POSITION_NOT_FOUND ) );
112 m_pParent->m_vColumnTypes.resize( rSrcColumns.size(), COLUMN_POSITION_NOT_FOUND );
113
114 std::unique_ptr<weld::TreeIter> xLeftEntry = m_xCTRL_LEFT->make_iterator();
115 std::unique_ptr<weld::TreeIter> xRightEntry = m_xCTRL_RIGHT->make_iterator();
116
117 sal_Int32 nParamPos = 0;
118 bool bLeftEntry = m_xCTRL_LEFT->get_iter_first(*xLeftEntry);
119 bool bRightEntry = m_xCTRL_RIGHT->get_iter_first(*xRightEntry);
120 while (bLeftEntry && bRightEntry)
121 {
122 OFieldDescription* pSrcField = weld::fromId<OFieldDescription*>(m_xCTRL_LEFT->get_id(*xLeftEntry));
123 OSL_ENSURE(pSrcField,"OWizNameMatching: OColumn can not be null!");
124
125 sal_Int32 nPos = 0;
126 for (auto const& column : rSrcColumns)
127 {
128 if (column->second == pSrcField)
129 break;
130 ++nPos;
131 }
132
133 if (m_xCTRL_LEFT->get_toggle(*xLeftEntry) == TRISTATE_TRUE)
134 {
135 OFieldDescription* pDestField = weld::fromId<OFieldDescription*>(m_xCTRL_RIGHT->get_id(*xRightEntry));
136 OSL_ENSURE(pDestField,"OWizNameMatching: OColumn can not be null!");
137 const ODatabaseExport::TColumnVector& rDestColumns = m_pParent->getDestVector();
138 sal_Int32 nPosDest = 1;
139 bool bDestColumnFound = false;
140 TOTypeInfoSP typeInfoSPFound;
141 for (auto const& column : rDestColumns)
142 {
143 if (column->second == pDestField)
144 {
145 bDestColumnFound = true;
146 typeInfoSPFound = column->second->getSpecialTypeInfo();
147 break;
148 }
149 ++nPosDest;
150 }
151
152 OSL_ENSURE((nPos) < static_cast<sal_Int32>(m_pParent->m_vColumnPositions.size()),"m_pParent->m_vColumnPositions: Illegal index for vector");
153 m_pParent->m_vColumnPositions[nPos].first = ++nParamPos;
154 m_pParent->m_vColumnPositions[nPos].second = nPosDest;
155
156 TOTypeInfoSP pTypeInfo;
157
158 assert(bDestColumnFound);
159 if (bDestColumnFound)
160 {
161 bool bNotConvert = true;
162 pTypeInfo = m_pParent->convertType(typeInfoSPFound, bNotConvert);
163 }
164
165 sal_Int32 nType = css::sdbc::DataType::VARCHAR;
166 if ( pTypeInfo )
167 nType = pTypeInfo->nType;
168 m_pParent->m_vColumnTypes[nPos] = nType;
169 }
170 else
171 {
172 m_pParent->m_vColumnPositions[nPos].first = COLUMN_POSITION_NOT_FOUND;
173 m_pParent->m_vColumnPositions[nPos].second = COLUMN_POSITION_NOT_FOUND;
174 }
175
176 bLeftEntry = m_xCTRL_LEFT->iter_next(*xLeftEntry);
177 bRightEntry = m_xCTRL_RIGHT->iter_next(*xRightEntry);
178 }
179
180 return true;
181}
182
183OUString OWizNameMatching::GetTitle() const { return DBA_RES(STR_WIZ_NAME_MATCHING_TITLE); }
184
185IMPL_LINK(OWizNameMatching, ButtonClickHdl, weld::Button&, rButton, void)
186{
187 int nPos = m_xCTRL_LEFT->get_selected_index();
188 if (nPos == -1)
189 return;
190
191 int nOrigPos = nPos;
192 if (&rButton == m_xColumn_up.get() && nPos)
193 --nPos;
194 else if (&rButton == m_xColumn_down.get() && nPos < m_xCTRL_LEFT->n_children() - 1)
195 ++nPos;
196
197 m_xCTRL_LEFT->swap(nOrigPos, nPos);
198
199 m_xCTRL_LEFT->scroll_to_row(nPos);
200
201 TableListClickHdl(*m_xCTRL_LEFT);
202}
203
204IMPL_LINK( OWizNameMatching, RightButtonClickHdl, weld::Button&, rButton, void )
205{
206 int nPos = m_xCTRL_RIGHT->get_selected_index();
207 if (nPos == -1)
208 return;
209
210 int nOrigPos = nPos;
211 if (&rButton == m_xColumn_up_right.get() && nPos)
212 --nPos;
213 else if (&rButton == m_xColumn_down_right.get() && nPos < m_xCTRL_RIGHT->n_children() - 1)
214 ++nPos;
215
216 m_xCTRL_RIGHT->swap(nOrigPos, nPos);
217
218 m_xCTRL_RIGHT->scroll_to_row(nPos);
219
220 TableListRightSelectHdl(*m_xCTRL_RIGHT);
221}
222
223namespace
224{
225 int GetFirstEntryInView(weld::TreeView& rTreeView)
226 {
227 int nFirstEntryInView = -1;
228
229 rTreeView.visible_foreach([&nFirstEntryInView, &rTreeView](weld::TreeIter& rEntry){
230 nFirstEntryInView = rTreeView.get_iter_index_in_parent(rEntry);
231 // stop after first entry
232 return true;
233 });
234
235 return nFirstEntryInView;
236 }
237}
238
239IMPL_LINK_NOARG(OWizNameMatching, TableListClickHdl, weld::TreeView&, void)
240{
241 int nPos = m_xCTRL_LEFT->get_selected_index();
242 if (nPos == -1)
243 return;
244
245 int nOldEntry = m_xCTRL_RIGHT->get_selected_index();
246 if (nOldEntry != -1 && nPos != nOldEntry)
247 {
248 m_xCTRL_RIGHT->unselect(nOldEntry);
249 if (nPos < m_xCTRL_RIGHT->n_children())
250 {
251 int nNewPos = GetFirstEntryInView(*m_xCTRL_LEFT);
252 if ( nNewPos - nPos == 1 )
253 --nNewPos;
254 m_xCTRL_RIGHT->scroll_to_row(nNewPos);
255 m_xCTRL_RIGHT->select(nPos);
256 }
257 }
258 else if (nOldEntry == -1)
259 {
260 if (nPos < m_xCTRL_RIGHT->n_children())
261 m_xCTRL_RIGHT->select(nPos);
262 }
263}
264
265IMPL_LINK_NOARG( OWizNameMatching, TableListRightSelectHdl, weld::TreeView&, void )
266{
267 int nPos = m_xCTRL_RIGHT->get_selected_index();
268 if (nPos == -1)
269 return;
270
271 OFieldDescription* pColumn = weld::fromId<OFieldDescription*>(m_xCTRL_RIGHT->get_id(nPos));
272 if (pColumn->IsAutoIncrement())
273 {
274 m_xCTRL_RIGHT->unselect(nPos);
275 return;
276 }
277
278 int nOldEntry = m_xCTRL_LEFT->get_selected_index();
279 if (nOldEntry != -1 && nPos != nOldEntry)
280 {
281 m_xCTRL_LEFT->unselect(nOldEntry);
282 if (nPos < m_xCTRL_LEFT->n_children())
283 {
284 int nNewPos = GetFirstEntryInView(*m_xCTRL_RIGHT);
285 if ( nNewPos - nPos == 1 )
286 nNewPos--;
287 m_xCTRL_LEFT->scroll_to_row(nNewPos);
288 m_xCTRL_LEFT->select(nPos);
289 }
290 }
291 else if (nOldEntry == -1)
292 {
293 if (nPos < m_xCTRL_LEFT->n_children())
294 m_xCTRL_LEFT->select(nPos);
295 }
296}
297
298IMPL_LINK(OWizNameMatching, AllNoneClickHdl, weld::Button&, rButton, void)
299{
300 bool bAll = &rButton == m_xAll.get();
301 m_xCTRL_LEFT->all_foreach([this, bAll](weld::TreeIter& rEntry){
302 m_xCTRL_LEFT->set_toggle(rEntry, bAll ? TRISTATE_TRUE : TRISTATE_FALSE);
303 return false;
304 });
305}
306
307void OWizNameMatching::FillListBox(weld::TreeView& rTreeView, const ODatabaseExport::TColumnVector& rList, bool bCheckButtons)
308{
309 rTreeView.clear();
310
311 int nRow(0);
312
313 for (auto const& elem : rList)
314 {
315 rTreeView.append();
316 if (bCheckButtons)
317 {
318 bool bChecked = !elem->second->IsAutoIncrement();
319 rTreeView.set_toggle(nRow, bChecked ? TRISTATE_TRUE : TRISTATE_FALSE);
320 }
321 rTreeView.set_text(nRow, elem->first, 0);
322 rTreeView.set_id(nRow, weld::toId(elem->second));
323 ++nRow;
324 }
325
326 if (rTreeView.n_children())
327 rTreeView.select(0);
328}
329
330/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define COLUMN_POSITION_NOT_FOUND
Definition: DExport.hxx:48
std::vector< TColumns::const_iterator > TColumnVector
Definition: DExport.hxx:58
virtual void Reset() override
Definition: WNameMatch.cxx:74
std::unique_ptr< weld::Button > m_xNone
Definition: WNameMatch.hxx:40
std::unique_ptr< weld::Button > m_xColumn_down_right
Definition: WNameMatch.hxx:38
std::unique_ptr< weld::Label > m_xTABLE_LEFT
Definition: WNameMatch.hxx:31
std::unique_ptr< weld::Label > m_xTABLE_RIGHT
Definition: WNameMatch.hxx:32
virtual OUString GetTitle() const override
Definition: WNameMatch.cxx:183
static void FillListBox(weld::TreeView &rTreeView, const ODatabaseExport::TColumnVector &rList, bool bCheckButtons)
Definition: WNameMatch.cxx:307
std::unique_ptr< weld::Button > m_xColumn_down
Definition: WNameMatch.hxx:36
std::unique_ptr< weld::TreeView > m_xCTRL_RIGHT
Definition: WNameMatch.hxx:34
virtual bool LeavePage() override
Definition: WNameMatch.cxx:104
virtual ~OWizNameMatching() override
Definition: WNameMatch.cxx:70
std::unique_ptr< weld::Button > m_xColumn_up
Definition: WNameMatch.hxx:35
virtual void Activate() override
Definition: WNameMatch.cxx:79
std::unique_ptr< weld::Button > m_xColumn_up_right
Definition: WNameMatch.hxx:37
std::unique_ptr< weld::TreeView > m_xCTRL_LEFT
Definition: WNameMatch.hxx:33
std::unique_ptr< weld::Button > m_xAll
Definition: WNameMatch.hxx:39
virtual int get_iter_index_in_parent(const TreeIter &rIter) const=0
virtual void set_text(int row, const OUString &rText, int col=-1)=0
virtual void visible_foreach(const std::function< bool(TreeIter &)> &func)=0
virtual void set_toggle(int row, TriState eState, int col=-1)=0
virtual int n_children() const=0
virtual void clear()=0
void append(TreeIter *pRet=nullptr)
virtual void select(int pos)=0
virtual void set_id(int row, const OUString &rId)=0
#define DBA_RES(id)
TRISTATE_FALSE
TRISTATE_TRUE
OUString aName
sal_uInt16 nPos
IMPL_LINK_NOARG(OApplicationController, OnClipboardChanged, TransferableDataHelper *, void)
IMPL_LINK(OApplicationController, OnSelectContainer, void *, _pType, void)
std::shared_ptr< OTypeInfo > TOTypeInfoSP
Definition: TypeInfo.hxx:99
OUString toId(const void *pValue)
QPRO_FUNC_TYPE nType