1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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:
12 #include "PivotLayoutTreeListData.hxx"
13 #include "PivotLayoutDialog.hxx"
15 #include <vcl/builderfactory.hxx>
16 #include <svtools/treelistentry.hxx>
18 #include "scabstdlg.hxx"
22 VCL_BUILDER_FACTORY_ARGS(ScPivotLayoutTreeListData
,
23 WB_BORDER
| WB_TABSTOP
| WB_CLIPCHILDREN
|
29 OUString
lclGetFunctionMaskName(const PivotFunc nFunctionMask
)
31 switch (nFunctionMask
)
33 case PivotFunc::Sum
: return OUString("Sum");
34 case PivotFunc::Count
: return OUString("Count");
35 case PivotFunc::Average
: return OUString("Mean");
36 case PivotFunc::Max
: return OUString("Max");
37 case PivotFunc::Min
: return OUString("Min");
38 case PivotFunc::Product
: return OUString("Product");
39 case PivotFunc::CountNum
: return OUString("Count");
40 case PivotFunc::StdDev
: return OUString("StDev");
41 case PivotFunc::StdDevP
: return OUString("StDevP");
42 case PivotFunc::StdVar
: return OUString("Var");
43 case PivotFunc::StdVarP
: return OUString("VarP");
50 OUString
lclCreateDataItemName(const PivotFunc nFunctionMask
, const OUString
& rName
, const sal_uInt8 nDuplicationCount
)
52 OUString aBuffer
= lclGetFunctionMaskName(nFunctionMask
) + " - " + rName
;
53 if(nDuplicationCount
> 0)
55 aBuffer
+= " " + OUString::number(nDuplicationCount
);
60 } // anonymous namespace
62 ScPivotLayoutTreeListData::ScPivotLayoutTreeListData(vcl::Window
* pParent
, WinBits nBits
) :
63 ScPivotLayoutTreeListBase(pParent
, nBits
, DATA_LIST
)
66 ScPivotLayoutTreeListData::~ScPivotLayoutTreeListData()
69 bool ScPivotLayoutTreeListData::DoubleClickHdl()
71 ScItemValue
* pCurrentItemValue
= static_cast<ScItemValue
*>(GetCurEntry()->GetUserData());
72 ScPivotFuncData
& rCurrentFunctionData
= pCurrentItemValue
->maFunctionData
;
74 SCCOL nCurrentColumn
= rCurrentFunctionData
.mnCol
;
75 ScDPLabelData
& rCurrentLabelData
= mpParent
->GetLabelData(nCurrentColumn
);
77 ScAbstractDialogFactory
* pFactory
= ScAbstractDialogFactory::Create();
79 std::unique_ptr
<AbstractScDPFunctionDlg
> pDialog(
80 pFactory
->CreateScDPFunctionDlg(this, mpParent
->GetLabelDataVector(), rCurrentLabelData
, rCurrentFunctionData
));
82 if (pDialog
->Execute() == RET_OK
)
84 rCurrentFunctionData
.mnFuncMask
= pDialog
->GetFuncMask();
85 rCurrentLabelData
.mnFuncMask
= pDialog
->GetFuncMask();
87 rCurrentFunctionData
.maFieldRef
= pDialog
->GetFieldRef();
89 ScDPLabelData
& rDFData
= mpParent
->GetLabelData(rCurrentFunctionData
.mnCol
);
91 AdjustDuplicateCount(pCurrentItemValue
);
93 OUString sDataItemName
= lclCreateDataItemName(
94 rCurrentFunctionData
.mnFuncMask
,
96 rCurrentFunctionData
.mnDupCount
);
98 SetEntryText(GetCurEntry(), sDataItemName
);
104 void ScPivotLayoutTreeListData::FillDataField(ScPivotFieldVector
& rDataFields
)
107 maDataItemValues
.clear();
109 ScPivotFieldVector::iterator it
;
110 for (it
= rDataFields
.begin(); it
!= rDataFields
.end(); ++it
)
112 ScPivotField
& rField
= *it
;
114 if (rField
.nCol
== PIVOT_DATA_FIELD
)
118 if (rField
.mnOriginalDim
>= 0)
119 nColumn
= rField
.mnOriginalDim
;
121 nColumn
= rField
.nCol
;
123 ScItemValue
* pOriginalItemValue
= mpParent
->GetItem(nColumn
);
124 ScItemValue
* pItemValue
= new ScItemValue(pOriginalItemValue
->maName
, nColumn
, rField
.nFuncMask
);
126 pItemValue
->mpOriginalItemValue
= pOriginalItemValue
;
127 pItemValue
->maFunctionData
.mnOriginalDim
= rField
.mnOriginalDim
;
128 pItemValue
->maFunctionData
.maFieldRef
= rField
.maFieldRef
;
130 AdjustDuplicateCount(pItemValue
);
131 OUString sDataItemName
= lclCreateDataItemName(pItemValue
->maFunctionData
.mnFuncMask
,
133 pItemValue
->maFunctionData
.mnDupCount
);
135 maDataItemValues
.push_back(std::unique_ptr
<ScItemValue
>(pItemValue
));
136 InsertEntry(sDataItemName
, nullptr, false, TREELIST_APPEND
, pItemValue
);
140 void ScPivotLayoutTreeListData::PushDataFieldNames(vector
<ScDPName
>& rDataFieldNames
)
142 SvTreeListEntry
* pLoopEntry
;
143 for (pLoopEntry
= First(); pLoopEntry
!= nullptr; pLoopEntry
= Next(pLoopEntry
))
145 ScItemValue
* pEachItemValue
= static_cast<ScItemValue
*>(pLoopEntry
->GetUserData());
146 SCCOL nColumn
= pEachItemValue
->maFunctionData
.mnCol
;
148 ScDPLabelData
& rLabelData
= mpParent
->GetLabelData(nColumn
);
150 if (rLabelData
.maName
.isEmpty())
153 OUString sLayoutName
= rLabelData
.maLayoutName
;
154 if (sLayoutName
.isEmpty())
156 sLayoutName
= lclCreateDataItemName(
157 pEachItemValue
->maFunctionData
.mnFuncMask
,
158 pEachItemValue
->maName
,
159 pEachItemValue
->maFunctionData
.mnDupCount
);
162 rDataFieldNames
.push_back(ScDPName(rLabelData
.maName
, sLayoutName
, rLabelData
.mnDupCount
));
166 void ScPivotLayoutTreeListData::InsertEntryForSourceTarget(SvTreeListEntry
* pSource
, SvTreeListEntry
* pTarget
)
168 ScItemValue
* pItemValue
= static_cast<ScItemValue
*>(pSource
->GetUserData());
170 if(mpParent
->IsDataElement(pItemValue
->maFunctionData
.mnCol
))
173 if (HasEntry(pSource
))
175 OUString rText
= GetEntryText(pSource
);
176 GetModel()->Remove(pSource
);
177 sal_uLong nPosition
= (pTarget
== nullptr) ? TREELIST_APPEND
: GetModel()->GetAbsPos(pTarget
) + 1;
178 InsertEntry(rText
, nullptr, false, nPosition
, pItemValue
);
182 sal_uLong nPosition
= (pTarget
== nullptr) ? TREELIST_APPEND
: GetModel()->GetAbsPos(pTarget
) + 1;
183 InsertEntryForItem(pItemValue
->mpOriginalItemValue
, nPosition
);
187 void ScPivotLayoutTreeListData::InsertEntryForItem(ScItemValue
* pItemValue
, sal_uLong nPosition
)
189 ScItemValue
* pDataItemValue
= new ScItemValue(pItemValue
);
190 pDataItemValue
->mpOriginalItemValue
= pItemValue
;
191 maDataItemValues
.push_back(std::unique_ptr
<ScItemValue
>(pDataItemValue
));
193 ScPivotFuncData
& rFunctionData
= pDataItemValue
->maFunctionData
;
195 if (rFunctionData
.mnFuncMask
== PivotFunc::NONE
||
196 rFunctionData
.mnFuncMask
== PivotFunc::Auto
)
198 rFunctionData
.mnFuncMask
= PivotFunc::Sum
;
201 AdjustDuplicateCount(pDataItemValue
);
203 OUString sDataName
= lclCreateDataItemName(
204 rFunctionData
.mnFuncMask
,
205 pDataItemValue
->maName
,
206 rFunctionData
.mnDupCount
);
208 InsertEntry(sDataName
, nullptr, false, nPosition
, pDataItemValue
);
211 void ScPivotLayoutTreeListData::AdjustDuplicateCount(ScItemValue
* pInputItemValue
)
213 ScPivotFuncData
& rInputFunctionData
= pInputItemValue
->maFunctionData
;
215 bool bFoundDuplicate
= false;
217 rInputFunctionData
.mnDupCount
= 0;
218 sal_uInt8 nMaxDuplicateCount
= 0;
220 SvTreeListEntry
* pEachEntry
;
221 for (pEachEntry
= First(); pEachEntry
!= nullptr; pEachEntry
= Next(pEachEntry
))
223 ScItemValue
* pItemValue
= static_cast<ScItemValue
*>(pEachEntry
->GetUserData());
224 if (pItemValue
== pInputItemValue
)
227 ScPivotFuncData
& rFunctionData
= pItemValue
->maFunctionData
;
229 if (rFunctionData
.mnCol
== rInputFunctionData
.mnCol
&&
230 rFunctionData
.mnFuncMask
== rInputFunctionData
.mnFuncMask
)
232 bFoundDuplicate
= true;
233 if(rFunctionData
.mnDupCount
> nMaxDuplicateCount
)
234 nMaxDuplicateCount
= rFunctionData
.mnDupCount
;
240 rInputFunctionData
.mnDupCount
= nMaxDuplicateCount
+ 1;
244 void ScPivotLayoutTreeListData::KeyInput(const KeyEvent
& rKeyEvent
)
246 vcl::KeyCode aCode
= rKeyEvent
.GetKeyCode();
247 sal_uInt16 nCode
= aCode
.GetCode();
249 if (nCode
== KEY_DELETE
)
251 const SvTreeListEntry
* pEntry
= GetCurEntry();
253 GetModel()->Remove(pEntry
);
257 SvTreeListBox::KeyInput(rKeyEvent
);
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */