tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / extensions / source / dbpilots / gridwizard.cxx
blob5bb9625773e2eb030efa04233a012661a2a2f4ff
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 <sal/config.h>
22 #include <vector>
24 #include "gridwizard.hxx"
25 #include <com/sun/star/sdbc/DataType.hpp>
26 #include <com/sun/star/form/XGridColumnFactory.hpp>
27 #include <com/sun/star/awt/MouseWheelBehavior.hpp>
28 #include <com/sun/star/container/XNameContainer.hpp>
29 #include <sal/log.hxx>
30 #include <tools/debug.hxx>
31 #include "dbptools.hxx"
32 #include <helpids.h>
34 #define GW_STATE_DATASOURCE_SELECTION 0
35 #define GW_STATE_FIELDSELECTION 1
38 namespace dbp
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::sdbc;
45 using namespace ::com::sun::star::container;
46 using namespace ::com::sun::star::form;
47 using namespace ::com::sun::star::awt;
49 OGridWizard::OGridWizard(weld::Window* _pParent,
50 const Reference< XPropertySet >& _rxObjectModel, const Reference< XComponentContext >& _rxContext )
51 : OControlWizard(_pParent, _rxObjectModel, _rxContext)
52 , m_bHadDataSelection(true)
54 initControlSettings(&m_aSettings);
56 m_xPrevPage->set_help_id(HID_GRIDWIZARD_PREVIOUS);
57 m_xNextPage->set_help_id(HID_GRIDWIZARD_NEXT);
58 m_xCancel->set_help_id(HID_GRIDWIZARD_CANCEL);
59 m_xFinish->set_help_id(HID_GRIDWIZARD_FINISH);
60 setTitleBase(compmodule::ModuleRes(RID_STR_GRIDWIZARD_TITLE));
62 // if we do not need the data source selection page ...
63 if (!needDatasourceSelection())
64 { // ... skip it!
65 skip();
66 m_bHadDataSelection = false;
70 bool OGridWizard::approveControl(sal_Int16 _nClassId)
72 if (FormComponentType::GRIDCONTROL != _nClassId)
73 return false;
75 Reference< XGridColumnFactory > xColumnFactory(getContext().xObjectModel, UNO_QUERY);
76 return xColumnFactory.is();
79 void OGridWizard::implApplySettings()
81 const OControlWizardContext& rContext = getContext();
83 // the factory for the columns
84 Reference< XGridColumnFactory > xColumnFactory(rContext.xObjectModel, UNO_QUERY);
85 DBG_ASSERT(xColumnFactory.is(), "OGridWizard::implApplySettings: should never have made it 'til here!");
86 // (if we're here, what the hell happened in approveControl??)
88 // the container for the columns
89 Reference< XNameContainer > xColumnContainer(rContext.xObjectModel, UNO_QUERY);
90 DBG_ASSERT(xColumnContainer.is(), "OGridWizard::implApplySettings: no container!");
92 if (!xColumnFactory.is() || !xColumnContainer.is())
93 return;
95 static constexpr OUString s_sMouseWheelBehavior = u"MouseWheelBehavior"_ustr;
96 static constexpr OUString s_sEmptyString = u""_ustr;
98 // collect "descriptors" for the to-be-created (grid)columns
99 std::vector< OUString > aColumnServiceNames; // service names to be used with the XGridColumnFactory
100 std::vector< OUString > aColumnLabelPostfixes; // postfixes to append to the column labels
101 std::vector< OUString > aFormFieldNames; // data field names
103 aColumnServiceNames.reserve(getSettings().aSelectedFields.getLength());
104 aColumnLabelPostfixes.reserve(getSettings().aSelectedFields.getLength());
105 aFormFieldNames.reserve(getSettings().aSelectedFields.getLength());
107 // loop through the selected field names
108 for (auto& selectedField : getSettings().aSelectedFields)
110 // get the information for the selected column
111 sal_Int32 nFieldType = DataType::OTHER;
112 OControlWizardContext::TNameTypeMap::const_iterator aFind = rContext.aTypes.find(selectedField);
113 if ( aFind != rContext.aTypes.end() )
114 nFieldType = aFind->second;
116 aFormFieldNames.push_back(selectedField);
117 switch (nFieldType)
119 case DataType::BIT:
120 case DataType::BOOLEAN:
121 aColumnServiceNames.emplace_back("CheckBox");
122 aColumnLabelPostfixes.push_back(s_sEmptyString);
123 break;
125 case DataType::TINYINT:
126 case DataType::SMALLINT:
127 case DataType::INTEGER:
128 aColumnServiceNames.emplace_back("NumericField");
129 aColumnLabelPostfixes.push_back(s_sEmptyString);
130 break;
132 case DataType::FLOAT:
133 case DataType::REAL:
134 case DataType::DOUBLE:
135 case DataType::NUMERIC:
136 case DataType::DECIMAL:
137 aColumnServiceNames.emplace_back("FormattedField");
138 aColumnLabelPostfixes.push_back(s_sEmptyString);
139 break;
141 case DataType::DATE:
142 aColumnServiceNames.emplace_back("DateField");
143 aColumnLabelPostfixes.push_back(s_sEmptyString);
144 break;
146 case DataType::TIME:
147 aColumnServiceNames.emplace_back("TimeField");
148 aColumnLabelPostfixes.push_back(s_sEmptyString);
149 break;
151 case DataType::TIMESTAMP:
152 aColumnServiceNames.emplace_back("DateField");
153 aColumnLabelPostfixes.push_back(compmodule::ModuleRes(RID_STR_DATEPOSTFIX));
155 aFormFieldNames.push_back(selectedField);
156 aColumnServiceNames.emplace_back("TimeField");
157 aColumnLabelPostfixes.push_back(compmodule::ModuleRes(RID_STR_TIMEPOSTFIX));
158 break;
160 default:
161 aColumnServiceNames.emplace_back("TextField");
162 aColumnLabelPostfixes.push_back(s_sEmptyString);
166 DBG_ASSERT( aFormFieldNames.size() == aColumnServiceNames.size()
167 && aColumnServiceNames.size() == aColumnLabelPostfixes.size(),
168 "OGridWizard::implApplySettings: inconsistent descriptor sequences!");
170 // now loop through the descriptions and create the (grid)columns out of th descriptors
172 Reference< XNameAccess > xExistenceChecker(xColumnContainer);
174 std::vector< OUString >::const_iterator pColumnLabelPostfix = aColumnLabelPostfixes.begin();
175 std::vector< OUString >::const_iterator pFormFieldName = aFormFieldNames.begin();
177 for (const auto& rColumnServiceName : aColumnServiceNames)
179 // create a (grid)column for the (resultset)column
182 Reference< XPropertySet > xColumn( xColumnFactory->createColumn(rColumnServiceName), UNO_SET_THROW );
183 Reference< XPropertySetInfo > xColumnPSI( xColumn->getPropertySetInfo(), UNO_SET_THROW );
185 OUString sColumnName(rColumnServiceName);
186 disambiguateName(xExistenceChecker, sColumnName);
188 // the data field the column should be bound to
189 xColumn->setPropertyValue(u"DataField"_ustr, Any(*pFormFieldName));
190 // the label
191 xColumn->setPropertyValue(u"Label"_ustr, Any(*pFormFieldName + *pColumnLabelPostfix));
192 // the width (<void/> => column will be auto-sized)
193 xColumn->setPropertyValue(u"Width"_ustr, Any());
195 if ( xColumnPSI->hasPropertyByName( s_sMouseWheelBehavior ) )
196 xColumn->setPropertyValue( s_sMouseWheelBehavior, Any( MouseWheelBehavior::SCROLL_DISABLED ) );
198 // insert the column
199 xColumnContainer->insertByName(sColumnName, Any(xColumn));
201 catch(const Exception&)
203 SAL_WARN( "extensions.dbpilots", "OGridWizard::implApplySettings: "
204 "unexpected exception while creating the grid column for field " <<
205 *pFormFieldName );
208 ++pColumnLabelPostfix;
209 ++pFormFieldName;
214 std::unique_ptr<BuilderPage> OGridWizard::createPage(WizardState _nState)
216 OUString sIdent(OUString::number(_nState));
217 weld::Container* pPageContainer = m_xAssistant->append_page(sIdent);
219 switch (_nState)
221 case GW_STATE_DATASOURCE_SELECTION:
222 return std::make_unique<OTableSelectionPage>(pPageContainer, this);
223 case GW_STATE_FIELDSELECTION:
224 return std::make_unique<OGridFieldsSelection>(pPageContainer, this);
227 return nullptr;
230 vcl::WizardTypes::WizardState OGridWizard::determineNextState( WizardState _nCurrentState ) const
232 switch (_nCurrentState)
234 case GW_STATE_DATASOURCE_SELECTION:
235 return GW_STATE_FIELDSELECTION;
236 case GW_STATE_FIELDSELECTION:
237 return WZS_INVALID_STATE;
240 return WZS_INVALID_STATE;
243 void OGridWizard::enterState(WizardState _nState)
245 OControlWizard::enterState(_nState);
247 enableButtons(WizardButtonFlags::PREVIOUS, m_bHadDataSelection ? (GW_STATE_DATASOURCE_SELECTION < _nState) : GW_STATE_FIELDSELECTION < _nState);
248 enableButtons(WizardButtonFlags::NEXT, GW_STATE_FIELDSELECTION != _nState);
249 if (_nState < GW_STATE_FIELDSELECTION)
250 enableButtons(WizardButtonFlags::FINISH, false);
252 if (GW_STATE_FIELDSELECTION == _nState)
253 defaultButton(WizardButtonFlags::FINISH);
257 bool OGridWizard::leaveState(WizardState _nState)
259 if (!OControlWizard::leaveState(_nState))
260 return false;
262 if (GW_STATE_FIELDSELECTION == _nState)
263 defaultButton(WizardButtonFlags::NEXT);
265 return true;
269 bool OGridWizard::onFinish()
271 if ( !OControlWizard::onFinish() )
272 return false;
274 implApplySettings();
276 return true;
279 OGridFieldsSelection::OGridFieldsSelection(weld::Container* pPage, OGridWizard* pWizard)
280 : OGridPage(pPage, pWizard, u"modules/sabpilot/ui/gridfieldsselectionpage.ui"_ustr, u"GridFieldsSelection"_ustr)
281 , m_xExistFields(m_xBuilder->weld_tree_view(u"existingfields"_ustr))
282 , m_xSelectOne(m_xBuilder->weld_button(u"fieldright"_ustr))
283 , m_xSelectAll(m_xBuilder->weld_button(u"allfieldsright"_ustr))
284 , m_xDeselectOne(m_xBuilder->weld_button(u"fieldleft"_ustr))
285 , m_xDeselectAll(m_xBuilder->weld_button(u"allfieldsleft"_ustr))
286 , m_xSelFields(m_xBuilder->weld_tree_view(u"selectedfields"_ustr))
288 enableFormDatasourceDisplay();
290 m_xSelectOne->connect_clicked(LINK(this, OGridFieldsSelection, OnMoveOneEntry));
291 m_xSelectAll->connect_clicked(LINK(this, OGridFieldsSelection, OnMoveAllEntries));
292 m_xDeselectOne->connect_clicked(LINK(this, OGridFieldsSelection, OnMoveOneEntry));
293 m_xDeselectAll->connect_clicked(LINK(this, OGridFieldsSelection, OnMoveAllEntries));
295 m_xExistFields->connect_selection_changed(
296 LINK(this, OGridFieldsSelection, OnEntrySelected));
297 m_xSelFields->connect_selection_changed(LINK(this, OGridFieldsSelection, OnEntrySelected));
298 m_xExistFields->connect_row_activated(LINK(this, OGridFieldsSelection, OnEntryDoubleClicked));
299 m_xSelFields->connect_row_activated(LINK(this, OGridFieldsSelection, OnEntryDoubleClicked));
302 OGridFieldsSelection::~OGridFieldsSelection()
306 void OGridFieldsSelection::Activate()
308 OGridPage::Activate();
309 m_xExistFields->grab_focus();
312 bool OGridFieldsSelection::canAdvance() const
314 return false;
315 // we're the last page in our wizard
318 void OGridFieldsSelection::initializePage()
320 OGridPage::initializePage();
322 const OControlWizardContext& rContext = getContext();
323 fillListBox(*m_xExistFields, rContext.aFieldNames);
325 m_xSelFields->clear();
326 for (auto& field : getSettings().aSelectedFields)
328 m_xSelFields->append_text(field);
329 m_xExistFields->remove_text(field);
332 implCheckButtons();
335 bool OGridFieldsSelection::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason )
337 if (!OGridPage::commitPage(_eReason))
338 return false;
340 OGridSettings& rSettings = getSettings();
341 const sal_Int32 nSelected = m_xSelFields->n_children();
343 rSettings.aSelectedFields.realloc(nSelected);
344 OUString* pSelected = rSettings.aSelectedFields.getArray();
346 for (sal_Int32 i=0; i<nSelected; ++i, ++pSelected)
347 *pSelected = m_xSelFields->get_text(i);
349 return true;
352 void OGridFieldsSelection::implCheckButtons()
354 m_xSelectOne->set_sensitive(m_xExistFields->count_selected_rows() != 0);
355 m_xSelectAll->set_sensitive(m_xExistFields->n_children() != 0);
357 m_xDeselectOne->set_sensitive(m_xSelFields->count_selected_rows() != 0);
358 m_xDeselectAll->set_sensitive(m_xSelFields->n_children() != 0);
360 getDialog()->enableButtons(WizardButtonFlags::FINISH, 0 != m_xSelFields->n_children());
363 IMPL_LINK(OGridFieldsSelection, OnEntryDoubleClicked, weld::TreeView&, rList, bool)
365 weld::Button* pSimulateButton = m_xExistFields.get() == &rList ? m_xSelectOne.get() : m_xDeselectOne.get();
366 if (pSimulateButton->get_sensitive())
367 OnMoveOneEntry(*pSimulateButton);
368 return true;
371 IMPL_LINK_NOARG(OGridFieldsSelection, OnEntrySelected, weld::TreeView&, void)
373 implCheckButtons();
376 IMPL_LINK(OGridFieldsSelection, OnMoveOneEntry, weld::Button&, rButton, void)
378 bool bMoveRight = (m_xSelectOne.get() == &rButton);
379 weld::TreeView& rMoveTo = bMoveRight ? *m_xSelFields : *m_xExistFields;
381 // the index of the selected entry
382 const sal_Int32 nSelected = bMoveRight ? m_xExistFields->get_selected_index() : m_xSelFields->get_selected_index();
383 // the (original) relative position of the entry
384 int nRelativeIndex = bMoveRight ? m_xExistFields->get_id(nSelected).toInt32() : m_xSelFields->get_id(nSelected).toInt32();
386 sal_Int32 nInsertPos = -1;
387 if (!bMoveRight)
388 { // need to determine an insert pos which reflects the original
389 nInsertPos = 0;
390 while (nInsertPos < rMoveTo.n_children())
392 if (rMoveTo.get_id(nInsertPos).toInt32() > nRelativeIndex)
393 break;
394 ++nInsertPos;
398 // the text of the entry to move
399 OUString sMovingEntry = bMoveRight ? m_xExistFields->get_text(nSelected) : m_xSelFields->get_text(nSelected);
401 // insert the entry preserving it's "relative position" entry data
402 OUString sId(OUString::number(nRelativeIndex));
403 rMoveTo.insert(nullptr, nInsertPos, &sMovingEntry, &sId, nullptr, nullptr, false, nullptr);
405 // remove the entry from its old list
406 if (bMoveRight)
408 sal_Int32 nSelectPos = m_xExistFields->get_selected_index();
409 m_xExistFields->remove(nSelected);
410 if ((nSelectPos != -1) && (nSelectPos < m_xExistFields->n_children()))
411 m_xExistFields->select(nSelectPos);
413 m_xExistFields->grab_focus();
415 else
417 sal_Int32 nSelectPos = m_xSelFields->get_selected_index();
418 m_xSelFields->remove(nSelected);
419 if ((nSelectPos != -1) && (nSelectPos < m_xSelFields->n_children()))
420 m_xSelFields->select(nSelectPos);
422 m_xSelFields->grab_focus();
425 implCheckButtons();
428 IMPL_LINK(OGridFieldsSelection, OnMoveAllEntries, weld::Button&, rButton, void)
430 bool bMoveRight = (m_xSelectAll.get() == &rButton);
431 m_xExistFields->clear();
432 m_xSelFields->clear();
433 fillListBox(bMoveRight ? *m_xSelFields : *m_xExistFields, getContext().aFieldNames);
435 implCheckButtons();
438 } // namespace dbp
441 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */