Fix GNU C++ version check
[LibreOffice.git] / dbaccess / source / ui / misc / WNameMatch.cxx
blob5eba2844b62549d8efce137abc3e734895ffac77
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 <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>
29 using namespace ::dbaui;
31 // OWizColumnSelect
32 OWizNameMatching::OWizNameMatching(weld::Container* pPage, OCopyTableWizard* pWizard)
33 : OWizardPage(pPage, pWizard, u"dbaccess/ui/namematchingpage.ui"_ustr, u"NameMatching"_ustr)
34 , m_xTABLE_LEFT(m_xBuilder->weld_label(u"leftlabel"_ustr))
35 , m_xTABLE_RIGHT(m_xBuilder->weld_label(u"rightlabel"_ustr))
36 , m_xCTRL_LEFT(m_xBuilder->weld_tree_view(u"left"_ustr))
37 , m_xCTRL_RIGHT(m_xBuilder->weld_tree_view(u"right"_ustr))
38 , m_xColumn_up(m_xBuilder->weld_button(u"up"_ustr))
39 , m_xColumn_down(m_xBuilder->weld_button(u"down"_ustr))
40 , m_xColumn_up_right(m_xBuilder->weld_button(u"up_right"_ustr))
41 , m_xColumn_down_right(m_xBuilder->weld_button(u"down_right"_ustr))
42 , m_xAll(m_xBuilder->weld_button(u"all"_ustr))
43 , m_xNone(m_xBuilder->weld_button(u"none"_ustr))
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);
52 m_xColumn_up->connect_clicked(LINK(this,OWizNameMatching,ButtonClickHdl));
53 m_xColumn_down->connect_clicked(LINK(this,OWizNameMatching,ButtonClickHdl));
55 m_xColumn_up_right->connect_clicked(LINK(this,OWizNameMatching,RightButtonClickHdl));
56 m_xColumn_down_right->connect_clicked(LINK(this,OWizNameMatching,RightButtonClickHdl));
58 m_xAll->connect_clicked(LINK(this,OWizNameMatching,AllNoneClickHdl));
59 m_xNone->connect_clicked(LINK(this,OWizNameMatching,AllNoneClickHdl));
61 m_xCTRL_LEFT->enable_toggle_buttons(weld::ColumnToggleType::Check);
63 m_xCTRL_LEFT->connect_selection_changed(LINK(this, OWizNameMatching, TableListClickHdl));
64 m_xCTRL_RIGHT->connect_selection_changed(LINK(this, OWizNameMatching, TableListRightSelectHdl));
66 m_sSourceText = m_xTABLE_LEFT->get_label() + "\n";
67 m_sDestText = m_xTABLE_RIGHT->get_label() + "\n";
70 OWizNameMatching::~OWizNameMatching()
74 void OWizNameMatching::Reset()
76 m_bFirstTime = false;
79 void OWizNameMatching::Activate( )
81 // set source table name
82 OUString aName = m_sSourceText + m_pParent->m_sSourceName;
84 m_xTABLE_LEFT->set_label(aName);
86 // set dest table name
87 aName = m_sDestText + m_pParent->m_sName;
88 m_xTABLE_RIGHT->set_label(aName);
90 FillListBox(*m_xCTRL_LEFT, m_pParent->getSrcVector(), true);
91 FillListBox(*m_xCTRL_RIGHT, m_pParent->getDestVector(), false);
93 m_xColumn_up->set_sensitive( m_xCTRL_LEFT->n_children() > 1 );
94 m_xColumn_down->set_sensitive( m_xCTRL_LEFT->n_children() > 1 );
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 );
99 m_pParent->EnableNextButton(false);
100 m_xCTRL_LEFT->grab_focus();
101 TableListClickHdl(*m_xCTRL_LEFT);
104 bool OWizNameMatching::LeavePage()
107 const ODatabaseExport::TColumnVector& rSrcColumns = m_pParent->getSrcVector();
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 );
114 std::unique_ptr<weld::TreeIter> xLeftEntry = m_xCTRL_LEFT->make_iterator();
115 std::unique_ptr<weld::TreeIter> xRightEntry = m_xCTRL_RIGHT->make_iterator();
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)
122 OFieldDescription* pSrcField = weld::fromId<OFieldDescription*>(m_xCTRL_LEFT->get_id(*xLeftEntry));
123 OSL_ENSURE(pSrcField,"OWizNameMatching: OColumn can not be null!");
125 sal_Int32 nPos = 0;
126 for (auto const& column : rSrcColumns)
128 if (column->second == pSrcField)
129 break;
130 ++nPos;
133 if (m_xCTRL_LEFT->get_toggle(*xLeftEntry) == TRISTATE_TRUE)
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)
143 if (column->second == pDestField)
145 bDestColumnFound = true;
146 typeInfoSPFound = column->second->getSpecialTypeInfo();
147 break;
149 ++nPosDest;
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;
156 TOTypeInfoSP pTypeInfo;
158 assert(bDestColumnFound);
159 if (bDestColumnFound)
161 bool bNotConvert = true;
162 pTypeInfo = m_pParent->convertType(typeInfoSPFound, bNotConvert);
165 sal_Int32 nType = css::sdbc::DataType::VARCHAR;
166 if ( pTypeInfo )
167 nType = pTypeInfo->nType;
168 m_pParent->m_vColumnTypes[nPos] = nType;
170 else
172 m_pParent->m_vColumnPositions[nPos].first = COLUMN_POSITION_NOT_FOUND;
173 m_pParent->m_vColumnPositions[nPos].second = COLUMN_POSITION_NOT_FOUND;
176 bLeftEntry = m_xCTRL_LEFT->iter_next(*xLeftEntry);
177 bRightEntry = m_xCTRL_RIGHT->iter_next(*xRightEntry);
180 return true;
183 OUString OWizNameMatching::GetTitle() const { return DBA_RES(STR_WIZ_NAME_MATCHING_TITLE); }
185 IMPL_LINK(OWizNameMatching, ButtonClickHdl, weld::Button&, rButton, void)
187 int nPos = m_xCTRL_LEFT->get_selected_index();
188 if (nPos == -1)
189 return;
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;
197 m_xCTRL_LEFT->swap(nOrigPos, nPos);
199 m_xCTRL_LEFT->scroll_to_row(nPos);
201 TableListClickHdl(*m_xCTRL_LEFT);
204 IMPL_LINK( OWizNameMatching, RightButtonClickHdl, weld::Button&, rButton, void )
206 int nPos = m_xCTRL_RIGHT->get_selected_index();
207 if (nPos == -1)
208 return;
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;
216 m_xCTRL_RIGHT->swap(nOrigPos, nPos);
218 m_xCTRL_RIGHT->scroll_to_row(nPos);
220 TableListRightSelectHdl(*m_xCTRL_RIGHT);
223 namespace
225 int GetFirstEntryInView(weld::TreeView& rTreeView)
227 int nFirstEntryInView = -1;
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;
235 return nFirstEntryInView;
239 IMPL_LINK_NOARG(OWizNameMatching, TableListClickHdl, weld::TreeView&, void)
241 int nPos = m_xCTRL_LEFT->get_selected_index();
242 if (nPos == -1)
243 return;
245 int nOldEntry = m_xCTRL_RIGHT->get_selected_index();
246 if (nOldEntry != -1 && nPos != nOldEntry)
248 m_xCTRL_RIGHT->unselect(nOldEntry);
249 if (nPos < m_xCTRL_RIGHT->n_children())
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);
258 else if (nOldEntry == -1)
260 if (nPos < m_xCTRL_RIGHT->n_children())
261 m_xCTRL_RIGHT->select(nPos);
265 IMPL_LINK_NOARG( OWizNameMatching, TableListRightSelectHdl, weld::TreeView&, void )
267 int nPos = m_xCTRL_RIGHT->get_selected_index();
268 if (nPos == -1)
269 return;
271 OFieldDescription* pColumn = weld::fromId<OFieldDescription*>(m_xCTRL_RIGHT->get_id(nPos));
272 if (pColumn->IsAutoIncrement())
274 m_xCTRL_RIGHT->unselect(nPos);
275 return;
278 int nOldEntry = m_xCTRL_LEFT->get_selected_index();
279 if (nOldEntry != -1 && nPos != nOldEntry)
281 m_xCTRL_LEFT->unselect(nOldEntry);
282 if (nPos < m_xCTRL_LEFT->n_children())
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);
291 else if (nOldEntry == -1)
293 if (nPos < m_xCTRL_LEFT->n_children())
294 m_xCTRL_LEFT->select(nPos);
298 IMPL_LINK(OWizNameMatching, AllNoneClickHdl, weld::Button&, rButton, void)
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;
307 void OWizNameMatching::FillListBox(weld::TreeView& rTreeView, const ODatabaseExport::TColumnVector& rList, bool bCheckButtons)
309 rTreeView.clear();
311 int nRow(0);
313 for (auto const& elem : rList)
315 rTreeView.append();
316 if (bCheckButtons)
318 bool bChecked = !elem->second->IsAutoIncrement();
319 rTreeView.set_toggle(nRow, bChecked ? TRISTATE_TRUE : TRISTATE_FALSE);
321 rTreeView.set_text(nRow, elem->first, 0);
322 rTreeView.set_id(nRow, weld::toId(elem->second));
323 ++nRow;
326 if (rTreeView.n_children())
327 rTreeView.select(0);
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */