LibreOffice Module extensions (master) 1
gridwizard.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 <sal/config.h>
21
22#include <vector>
23
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>
33
34#define GW_STATE_DATASOURCE_SELECTION 0
35#define GW_STATE_FIELDSELECTION 1
36
37
38namespace dbp
39{
40
41
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star::sdbc;
46 using namespace ::com::sun::star::container;
47 using namespace ::com::sun::star::form;
48 using namespace ::com::sun::star::awt;
49
51 const Reference< XPropertySet >& _rxObjectModel, const Reference< XComponentContext >& _rxContext )
52 : OControlWizard(_pParent, _rxObjectModel, _rxContext)
53 , m_bHadDataSelection(true)
54 {
56
58 m_xNextPage->set_help_id(HID_GRIDWIZARD_NEXT);
59 m_xCancel->set_help_id(HID_GRIDWIZARD_CANCEL);
60 m_xFinish->set_help_id(HID_GRIDWIZARD_FINISH);
61 setTitleBase(compmodule::ModuleRes(RID_STR_GRIDWIZARD_TITLE));
62
63 // if we do not need the data source selection page ...
65 { // ... skip it!
66 skip();
67 m_bHadDataSelection = false;
68 }
69 }
70
71 bool OGridWizard::approveControl(sal_Int16 _nClassId)
72 {
73 if (FormComponentType::GRIDCONTROL != _nClassId)
74 return false;
75
76 Reference< XGridColumnFactory > xColumnFactory(getContext().xObjectModel, UNO_QUERY);
77 return xColumnFactory.is();
78 }
79
81 {
82 const OControlWizardContext& rContext = getContext();
83
84 // the factory for the columns
85 Reference< XGridColumnFactory > xColumnFactory(rContext.xObjectModel, UNO_QUERY);
86 DBG_ASSERT(xColumnFactory.is(), "OGridWizard::implApplySettings: should never have made it 'til here!");
87 // (if we're here, what the hell happened in approveControl??)
88
89 // the container for the columns
90 Reference< XNameContainer > xColumnContainer(rContext.xObjectModel, UNO_QUERY);
91 DBG_ASSERT(xColumnContainer.is(), "OGridWizard::implApplySettings: no container!");
92
93 if (!xColumnFactory.is() || !xColumnContainer.is())
94 return;
95
96 static constexpr OUStringLiteral s_sMouseWheelBehavior = u"MouseWheelBehavior";
97 static constexpr OUStringLiteral s_sEmptyString = u"";
98
99 // collect "descriptors" for the to-be-created (grid)columns
100 std::vector< OUString > aColumnServiceNames; // service names to be used with the XGridColumnFactory
101 std::vector< OUString > aColumnLabelPostfixes; // postfixes to append to the column labels
102 std::vector< OUString > aFormFieldNames; // data field names
103
104 aColumnServiceNames.reserve(getSettings().aSelectedFields.getLength());
105 aColumnLabelPostfixes.reserve(getSettings().aSelectedFields.getLength());
106 aFormFieldNames.reserve(getSettings().aSelectedFields.getLength());
107
108 // loop through the selected field names
109 const OUString* pSelectedFields = getSettings().aSelectedFields.getConstArray();
110 const OUString* pEnd = pSelectedFields + getSettings().aSelectedFields.getLength();
111 for (;pSelectedFields < pEnd; ++pSelectedFields)
112 {
113 // get the information for the selected column
114 sal_Int32 nFieldType = DataType::OTHER;
115 OControlWizardContext::TNameTypeMap::const_iterator aFind = rContext.aTypes.find(*pSelectedFields);
116 if ( aFind != rContext.aTypes.end() )
117 nFieldType = aFind->second;
118
119 aFormFieldNames.push_back(*pSelectedFields);
120 switch (nFieldType)
121 {
122 case DataType::BIT:
123 case DataType::BOOLEAN:
124 aColumnServiceNames.push_back(OUString("CheckBox"));
125 aColumnLabelPostfixes.push_back(s_sEmptyString);
126 break;
127
128 case DataType::TINYINT:
129 case DataType::SMALLINT:
130 case DataType::INTEGER:
131 aColumnServiceNames.push_back(OUString("NumericField"));
132 aColumnLabelPostfixes.push_back(s_sEmptyString);
133 break;
134
135 case DataType::FLOAT:
136 case DataType::REAL:
137 case DataType::DOUBLE:
138 case DataType::NUMERIC:
139 case DataType::DECIMAL:
140 aColumnServiceNames.push_back(OUString("FormattedField"));
141 aColumnLabelPostfixes.push_back(s_sEmptyString);
142 break;
143
144 case DataType::DATE:
145 aColumnServiceNames.push_back(OUString("DateField"));
146 aColumnLabelPostfixes.push_back(s_sEmptyString);
147 break;
148
149 case DataType::TIME:
150 aColumnServiceNames.push_back(OUString("TimeField"));
151 aColumnLabelPostfixes.push_back(s_sEmptyString);
152 break;
153
154 case DataType::TIMESTAMP:
155 aColumnServiceNames.push_back(OUString("DateField"));
156 aColumnLabelPostfixes.push_back(compmodule::ModuleRes(RID_STR_DATEPOSTFIX));
157
158 aFormFieldNames.push_back(*pSelectedFields);
159 aColumnServiceNames.push_back(OUString("TimeField"));
160 aColumnLabelPostfixes.push_back(compmodule::ModuleRes(RID_STR_TIMEPOSTFIX));
161 break;
162
163 default:
164 aColumnServiceNames.push_back(OUString("TextField"));
165 aColumnLabelPostfixes.push_back(s_sEmptyString);
166 }
167 }
168
169 DBG_ASSERT( aFormFieldNames.size() == aColumnServiceNames.size()
170 && aColumnServiceNames.size() == aColumnLabelPostfixes.size(),
171 "OGridWizard::implApplySettings: inconsistent descriptor sequences!");
172
173 // now loop through the descriptions and create the (grid)columns out of th descriptors
174 {
175 Reference< XNameAccess > xExistenceChecker(xColumnContainer);
176
177 std::vector< OUString >::const_iterator pColumnLabelPostfix = aColumnLabelPostfixes.begin();
178 std::vector< OUString >::const_iterator pFormFieldName = aFormFieldNames.begin();
179
180 for (const auto& rColumnServiceName : aColumnServiceNames)
181 {
182 // create a (grid)column for the (resultset)column
183 try
184 {
185 Reference< XPropertySet > xColumn( xColumnFactory->createColumn(rColumnServiceName), UNO_SET_THROW );
186 Reference< XPropertySetInfo > xColumnPSI( xColumn->getPropertySetInfo(), UNO_SET_THROW );
187
188 OUString sColumnName(rColumnServiceName);
189 disambiguateName(xExistenceChecker, sColumnName);
190
191 // the data field the column should be bound to
192 xColumn->setPropertyValue("DataField", Any(*pFormFieldName));
193 // the label
194 xColumn->setPropertyValue("Label", Any(*pFormFieldName + *pColumnLabelPostfix));
195 // the width (<void/> => column will be auto-sized)
196 xColumn->setPropertyValue("Width", Any());
197
198 if ( xColumnPSI->hasPropertyByName( s_sMouseWheelBehavior ) )
199 xColumn->setPropertyValue( s_sMouseWheelBehavior, Any( MouseWheelBehavior::SCROLL_DISABLED ) );
200
201 // insert the column
202 xColumnContainer->insertByName(sColumnName, Any(xColumn));
203 }
204 catch(const Exception&)
205 {
206 SAL_WARN( "extensions.dbpilots", "OGridWizard::implApplySettings: "
207 "unexpected exception while creating the grid column for field " <<
208 *pFormFieldName );
209 }
210
211 ++pColumnLabelPostfix;
212 ++pFormFieldName;
213 }
214 }
215 }
216
217 std::unique_ptr<BuilderPage> OGridWizard::createPage(WizardState _nState)
218 {
219 OUString sIdent(OUString::number(_nState));
220 weld::Container* pPageContainer = m_xAssistant->append_page(sIdent);
221
222 switch (_nState)
223 {
225 return std::make_unique<OTableSelectionPage>(pPageContainer, this);
227 return std::make_unique<OGridFieldsSelection>(pPageContainer, this);
228 }
229
230 return nullptr;
231 }
232
234 {
235 switch (_nCurrentState)
236 {
240 return WZS_INVALID_STATE;
241 }
242
243 return WZS_INVALID_STATE;
244 }
245
247 {
249
250 enableButtons(WizardButtonFlags::PREVIOUS, m_bHadDataSelection ? (GW_STATE_DATASOURCE_SELECTION < _nState) : GW_STATE_FIELDSELECTION < _nState);
251 enableButtons(WizardButtonFlags::NEXT, GW_STATE_FIELDSELECTION != _nState);
252 if (_nState < GW_STATE_FIELDSELECTION)
253 enableButtons(WizardButtonFlags::FINISH, false);
254
255 if (GW_STATE_FIELDSELECTION == _nState)
256 defaultButton(WizardButtonFlags::FINISH);
257 }
258
259
261 {
262 if (!OControlWizard::leaveState(_nState))
263 return false;
264
265 if (GW_STATE_FIELDSELECTION == _nState)
266 defaultButton(WizardButtonFlags::NEXT);
267
268 return true;
269 }
270
271
273 {
275 return false;
276
278
279 return true;
280 }
281
283 : OGridPage(pPage, pWizard, "modules/sabpilot/ui/gridfieldsselectionpage.ui", "GridFieldsSelection")
284 , m_xExistFields(m_xBuilder->weld_tree_view("existingfields"))
285 , m_xSelectOne(m_xBuilder->weld_button("fieldright"))
286 , m_xSelectAll(m_xBuilder->weld_button("allfieldsright"))
287 , m_xDeselectOne(m_xBuilder->weld_button("fieldleft"))
288 , m_xDeselectAll(m_xBuilder->weld_button("allfieldsleft"))
289 , m_xSelFields(m_xBuilder->weld_tree_view("selectedfields"))
290 {
292
293 m_xSelectOne->connect_clicked(LINK(this, OGridFieldsSelection, OnMoveOneEntry));
294 m_xSelectAll->connect_clicked(LINK(this, OGridFieldsSelection, OnMoveAllEntries));
295 m_xDeselectOne->connect_clicked(LINK(this, OGridFieldsSelection, OnMoveOneEntry));
296 m_xDeselectAll->connect_clicked(LINK(this, OGridFieldsSelection, OnMoveAllEntries));
297
298 m_xExistFields->connect_changed(LINK(this, OGridFieldsSelection, OnEntrySelected));
299 m_xSelFields->connect_changed(LINK(this, OGridFieldsSelection, OnEntrySelected));
300 m_xExistFields->connect_row_activated(LINK(this, OGridFieldsSelection, OnEntryDoubleClicked));
301 m_xSelFields->connect_row_activated(LINK(this, OGridFieldsSelection, OnEntryDoubleClicked));
302 }
303
305 {
306 }
307
309 {
311 m_xExistFields->grab_focus();
312 }
313
315 {
316 return false;
317 // we're the last page in our wizard
318 }
319
321 {
323
324 const OControlWizardContext& rContext = getContext();
326
327 m_xSelFields->clear();
328 const OGridSettings& rSettings = getSettings();
329 const OUString* pSelected = rSettings.aSelectedFields.getConstArray();
330 const OUString* pEnd = pSelected + rSettings.aSelectedFields.getLength();
331 for (; pSelected < pEnd; ++pSelected)
332 {
333 m_xSelFields->append_text(*pSelected);
334 m_xExistFields->remove_text(*pSelected);
335 }
336
338 }
339
341 {
342 if (!OGridPage::commitPage(_eReason))
343 return false;
344
345 OGridSettings& rSettings = getSettings();
346 const sal_Int32 nSelected = m_xSelFields->n_children();
347
348 rSettings.aSelectedFields.realloc(nSelected);
349 OUString* pSelected = rSettings.aSelectedFields.getArray();
350
351 for (sal_Int32 i=0; i<nSelected; ++i, ++pSelected)
352 *pSelected = m_xSelFields->get_text(i);
353
354 return true;
355 }
356
358 {
359 m_xSelectOne->set_sensitive(m_xExistFields->count_selected_rows() != 0);
360 m_xSelectAll->set_sensitive(m_xExistFields->n_children() != 0);
361
362 m_xDeselectOne->set_sensitive(m_xSelFields->count_selected_rows() != 0);
363 m_xDeselectAll->set_sensitive(m_xSelFields->n_children() != 0);
364
365 getDialog()->enableButtons(WizardButtonFlags::FINISH, 0 != m_xSelFields->n_children());
366 }
367
368 IMPL_LINK(OGridFieldsSelection, OnEntryDoubleClicked, weld::TreeView&, rList, bool)
369 {
370 weld::Button* pSimulateButton = m_xExistFields.get() == &rList ? m_xSelectOne.get() : m_xDeselectOne.get();
371 if (pSimulateButton->get_sensitive())
372 OnMoveOneEntry(*pSimulateButton);
373 return true;
374 }
375
377 {
378 implCheckButtons();
379 }
380
381 IMPL_LINK(OGridFieldsSelection, OnMoveOneEntry, weld::Button&, rButton, void)
382 {
383 bool bMoveRight = (m_xSelectOne.get() == &rButton);
384 weld::TreeView& rMoveTo = bMoveRight ? *m_xSelFields : *m_xExistFields;
385
386 // the index of the selected entry
387 const sal_Int32 nSelected = bMoveRight ? m_xExistFields->get_selected_index() : m_xSelFields->get_selected_index();
388 // the (original) relative position of the entry
389 int nRelativeIndex = bMoveRight ? m_xExistFields->get_id(nSelected).toInt32() : m_xSelFields->get_id(nSelected).toInt32();
390
391 sal_Int32 nInsertPos = -1;
392 if (!bMoveRight)
393 { // need to determine an insert pos which reflects the original
394 nInsertPos = 0;
395 while (nInsertPos < rMoveTo.n_children())
396 {
397 if (rMoveTo.get_id(nInsertPos).toInt32() > nRelativeIndex)
398 break;
399 ++nInsertPos;
400 }
401 }
402
403 // the text of the entry to move
404 OUString sMovingEntry = bMoveRight ? m_xExistFields->get_text(nSelected) : m_xSelFields->get_text(nSelected);
405
406 // insert the entry preserving it's "relative position" entry data
407 OUString sId(OUString::number(nRelativeIndex));
408 rMoveTo.insert(nullptr, nInsertPos, &sMovingEntry, &sId, nullptr, nullptr, false, nullptr);
409
410 // remove the entry from its old list
411 if (bMoveRight)
412 {
413 sal_Int32 nSelectPos = m_xExistFields->get_selected_index();
414 m_xExistFields->remove(nSelected);
415 if ((nSelectPos != -1) && (nSelectPos < m_xExistFields->n_children()))
416 m_xExistFields->select(nSelectPos);
417
418 m_xExistFields->grab_focus();
419 }
420 else
421 {
422 sal_Int32 nSelectPos = m_xSelFields->get_selected_index();
423 m_xSelFields->remove(nSelected);
424 if ((nSelectPos != -1) && (nSelectPos < m_xSelFields->n_children()))
425 m_xSelFields->select(nSelectPos);
426
427 m_xSelFields->grab_focus();
428 }
429
430 implCheckButtons();
431 }
432
433 IMPL_LINK(OGridFieldsSelection, OnMoveAllEntries, weld::Button&, rButton, void)
434 {
435 bool bMoveRight = (m_xSelectAll.get() == &rButton);
436 m_xExistFields->clear();
437 m_xSelFields->clear();
438 fillListBox(bMoveRight ? *m_xSelFields : *m_xExistFields, getContext().aFieldNames);
439
440 implCheckButtons();
441 }
442
443} // namespace dbp
444
445
446/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char *const aFieldNames[]
OControlWizard * getDialog()
virtual void initializePage() override
const OControlWizardContext & getContext() const
static void fillListBox(weld::TreeView &_rList, const css::uno::Sequence< OUString > &_rItems)
void initControlSettings(OControlWizardSettings *_pSettings)
const OControlWizardContext & getContext() const
std::unique_ptr< weld::Button > m_xDeselectAll
Definition: gridwizard.hxx:77
std::unique_ptr< weld::TreeView > m_xExistFields
Definition: gridwizard.hxx:73
virtual ~OGridFieldsSelection() override
Definition: gridwizard.cxx:304
OGridFieldsSelection(weld::Container *pPage, OGridWizard *pWizard)
Definition: gridwizard.cxx:282
virtual void Activate() override
Definition: gridwizard.cxx:308
std::unique_ptr< weld::TreeView > m_xSelFields
Definition: gridwizard.hxx:78
std::unique_ptr< weld::Button > m_xDeselectOne
Definition: gridwizard.hxx:76
virtual bool commitPage(::vcl::WizardTypes::CommitPageReason _eReason) override
Definition: gridwizard.cxx:340
virtual bool canAdvance() const override
Definition: gridwizard.cxx:314
virtual void initializePage() override
Definition: gridwizard.cxx:320
std::unique_ptr< weld::Button > m_xSelectOne
Definition: gridwizard.hxx:74
std::unique_ptr< weld::Button > m_xSelectAll
Definition: gridwizard.hxx:75
OGridSettings & getSettings()
Definition: gridwizard.hxx:68
virtual bool leaveState(WizardState _nState) override
Definition: gridwizard.cxx:260
bool m_bHadDataSelection
Definition: gridwizard.hxx:38
virtual void enterState(WizardState _nState) override
Definition: gridwizard.cxx:246
OGridSettings & getSettings()
Definition: gridwizard.hxx:45
void implApplySettings()
Definition: gridwizard.cxx:80
virtual bool onFinish() override
Definition: gridwizard.cxx:272
virtual std::unique_ptr< BuilderPage > createPage(WizardState _nState) override
Definition: gridwizard.cxx:217
virtual WizardState determineNextState(WizardState _nCurrentState) const override
Definition: gridwizard.cxx:233
virtual bool approveControl(sal_Int16 _nClassId) override
Definition: gridwizard.cxx:71
OGridWizard(weld::Window *_pParent, const css::uno::Reference< css::beans::XPropertySet > &_rxObjectModel, const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
Definition: gridwizard.cxx:50
OGridSettings m_aSettings
Definition: gridwizard.hxx:37
virtual bool commitPage(WizardTypes::CommitPageReason _eReason) override
virtual void Activate() override
virtual bool leaveState(WizardTypes::WizardState nState)
std::unique_ptr< weld::Button > m_xNextPage
std::unique_ptr< weld::Button > m_xCancel
virtual bool onFinish()
void defaultButton(WizardButtonFlags _nWizardButtonFlags)
std::unique_ptr< weld::Button > m_xPrevPage
void enableButtons(WizardButtonFlags _nWizardButtonFlags, bool _bEnable)
std::unique_ptr< weld::Button > m_xFinish
virtual void enterState(WizardTypes::WizardState _nState)
void setTitleBase(const OUString &_rTitleBase)
std::unique_ptr< weld::Assistant > m_xAssistant
virtual int n_children() const=0
virtual void insert(const TreeIter *pParent, int pos, const OUString *pStr, const OUString *pId, const OUString *pIconName, VirtualDevice *pImageSurface, bool bChildrenOnDemand, TreeIter *pRet)=0
virtual int get_selected_index() const=0
virtual OUString get_id(int pos) const=0
virtual bool get_sensitive() const=0
#define DBG_ASSERT(sCon, aError)
float u
Reference< XColumn > xColumn
#define GW_STATE_FIELDSELECTION
Definition: gridwizard.cxx:35
#define GW_STATE_DATASOURCE_SELECTION
Definition: gridwizard.cxx:34
constexpr OUStringLiteral HID_GRIDWIZARD_NEXT
Definition: helpids.h:75
constexpr OUStringLiteral HID_GRIDWIZARD_FINISH
Definition: helpids.h:77
constexpr OUStringLiteral HID_GRIDWIZARD_PREVIOUS
Definition: helpids.h:74
constexpr OUStringLiteral HID_GRIDWIZARD_CANCEL
Definition: helpids.h:76
#define SAL_WARN(area, stream)
@ Exception
OUString ModuleRes(TranslateId pId)
IMPL_LINK_NOARG(OTableSelectionPage, OnSearchClicked, weld::Button &, void)
void disambiguateName(const Reference< XNameAccess > &_rxContainer, OUString &_rElementsName)
Definition: dbptools.cxx:33
IMPL_LINK(OTableSelectionPage, OnListboxDoubleClicked, weld::TreeView &, _rBox, bool)
int i
sal_Int16 WizardState
css::uno::Reference< css::beans::XPropertySet > xObjectModel
css::uno::Sequence< OUString > aFieldNames
css::uno::Sequence< OUString > aSelectedFields
Definition: gridwizard.hxx:32
OUString sId
#define WZS_INVALID_STATE