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 sal_uInt16 nFunctionMask
)
31 switch (nFunctionMask
)
33 case PIVOT_FUNC_SUM
: return OUString("Sum");
34 case PIVOT_FUNC_COUNT
: return OUString("Count");
35 case PIVOT_FUNC_AVERAGE
: return OUString("Mean");
36 case PIVOT_FUNC_MAX
: return OUString("Max");
37 case PIVOT_FUNC_MIN
: return OUString("Min");
38 case PIVOT_FUNC_PRODUCT
: return OUString("Product");
39 case PIVOT_FUNC_COUNT_NUM
: return OUString("Count");
40 case PIVOT_FUNC_STD_DEV
: return OUString("StDev");
41 case PIVOT_FUNC_STD_DEVP
: return OUString("StDevP");
42 case PIVOT_FUNC_STD_VAR
: return OUString("Var");
43 case PIVOT_FUNC_STD_VARP
: return OUString("VarP");
50 OUString
lclCreateDataItemName(const sal_uInt16 nFunctionMask
, const OUString
& rName
, const sal_uInt8 nDuplicationCount
)
52 OUStringBuffer aBuffer
;
53 aBuffer
.append(lclGetFunctionMaskName(nFunctionMask
));
54 aBuffer
.append(" - ");
55 aBuffer
.append(rName
);
56 if(nDuplicationCount
> 0)
59 aBuffer
.append(OUString::number(nDuplicationCount
));
62 return aBuffer
.makeStringAndClear();
65 } // anonymous namespace
67 ScPivotLayoutTreeListData::ScPivotLayoutTreeListData(vcl::Window
* pParent
, WinBits nBits
) :
68 ScPivotLayoutTreeListBase(pParent
, nBits
, DATA_LIST
)
71 ScPivotLayoutTreeListData::~ScPivotLayoutTreeListData()
74 bool ScPivotLayoutTreeListData::DoubleClickHdl()
76 ScItemValue
* pCurrentItemValue
= static_cast<ScItemValue
*>(GetCurEntry()->GetUserData());
77 ScPivotFuncData
& rCurrentFunctionData
= pCurrentItemValue
->maFunctionData
;
79 SCCOL nCurrentColumn
= rCurrentFunctionData
.mnCol
;
80 ScDPLabelData
* pCurrentLabelData
= mpParent
->GetLabelData(nCurrentColumn
);
81 if (!pCurrentLabelData
)
84 ScAbstractDialogFactory
* pFactory
= ScAbstractDialogFactory::Create();
86 boost::scoped_ptr
<AbstractScDPFunctionDlg
> pDialog(
87 pFactory
->CreateScDPFunctionDlg(this, mpParent
->GetLabelDataVector(), *pCurrentLabelData
, rCurrentFunctionData
));
89 if (pDialog
->Execute() == RET_OK
)
91 rCurrentFunctionData
.mnFuncMask
= pDialog
->GetFuncMask();
92 pCurrentLabelData
->mnFuncMask
= pDialog
->GetFuncMask();
94 rCurrentFunctionData
.maFieldRef
= pDialog
->GetFieldRef();
96 ScDPLabelData
* pDFData
= mpParent
->GetLabelData(rCurrentFunctionData
.mnCol
);
98 AdjustDuplicateCount(pCurrentItemValue
);
100 OUString sDataItemName
= lclCreateDataItemName(
101 rCurrentFunctionData
.mnFuncMask
,
103 rCurrentFunctionData
.mnDupCount
);
105 SetEntryText(GetCurEntry(), sDataItemName
);
111 void ScPivotLayoutTreeListData::FillDataField(ScPivotFieldVector
& rDataFields
)
114 maDataItemValues
.clear();
116 ScPivotFieldVector::iterator it
;
117 for (it
= rDataFields
.begin(); it
!= rDataFields
.end(); ++it
)
119 ScPivotField
& rField
= *it
;
121 if (rField
.nCol
== PIVOT_DATA_FIELD
)
125 if (rField
.mnOriginalDim
>= 0)
126 nColumn
= rField
.mnOriginalDim
;
128 nColumn
= rField
.nCol
;
130 ScItemValue
* pOriginalItemValue
= mpParent
->GetItem(nColumn
);
131 ScItemValue
* pItemValue
= new ScItemValue(pOriginalItemValue
->maName
, nColumn
, rField
.nFuncMask
);
133 pItemValue
->mpOriginalItemValue
= pOriginalItemValue
;
134 pItemValue
->maFunctionData
.mnOriginalDim
= rField
.mnOriginalDim
;
135 pItemValue
->maFunctionData
.maFieldRef
= rField
.maFieldRef
;
137 AdjustDuplicateCount(pItemValue
);
138 OUString sDataItemName
= lclCreateDataItemName(pItemValue
->maFunctionData
.mnFuncMask
,
140 pItemValue
->maFunctionData
.mnDupCount
);
142 maDataItemValues
.push_back(pItemValue
);
143 InsertEntry(sDataItemName
, NULL
, false, TREELIST_APPEND
, pItemValue
);
147 void ScPivotLayoutTreeListData::PushDataFieldNames(vector
<ScDPName
>& rDataFieldNames
)
149 SvTreeListEntry
* pLoopEntry
;
150 for (pLoopEntry
= First(); pLoopEntry
!= NULL
; pLoopEntry
= Next(pLoopEntry
))
152 ScItemValue
* pEachItemValue
= static_cast<ScItemValue
*>(pLoopEntry
->GetUserData());
153 SCCOL nColumn
= pEachItemValue
->maFunctionData
.mnCol
;
155 ScDPLabelData
* pLabelData
= mpParent
->GetLabelData(nColumn
);
157 if (pLabelData
== NULL
|| pLabelData
->maName
.isEmpty())
160 OUString sLayoutName
= pLabelData
->maLayoutName
;
161 if (sLayoutName
.isEmpty())
163 sLayoutName
= lclCreateDataItemName(
164 pEachItemValue
->maFunctionData
.mnFuncMask
,
165 pEachItemValue
->maName
,
166 pEachItemValue
->maFunctionData
.mnDupCount
);
169 rDataFieldNames
.push_back(ScDPName(pLabelData
->maName
, sLayoutName
, pLabelData
->mnDupCount
));
173 void ScPivotLayoutTreeListData::InsertEntryForSourceTarget(SvTreeListEntry
* pSource
, SvTreeListEntry
* pTarget
)
175 ScItemValue
* pItemValue
= static_cast<ScItemValue
*>(pSource
->GetUserData());
177 if(mpParent
->IsDataElement(pItemValue
->maFunctionData
.mnCol
))
180 if (HasEntry(pSource
))
182 OUString rText
= GetEntryText(pSource
);
183 GetModel()->Remove(pSource
);
184 sal_uLong nPosition
= (pTarget
== NULL
) ? TREELIST_APPEND
: GetModel()->GetAbsPos(pTarget
) + 1;
185 InsertEntry(rText
, NULL
, false, nPosition
, pItemValue
);
189 sal_uLong nPosition
= (pTarget
== NULL
) ? TREELIST_APPEND
: GetModel()->GetAbsPos(pTarget
) + 1;
190 InsertEntryForItem(pItemValue
->mpOriginalItemValue
, nPosition
);
194 void ScPivotLayoutTreeListData::InsertEntryForItem(ScItemValue
* pItemValue
, sal_uLong nPosition
)
196 ScItemValue
* pDataItemValue
= new ScItemValue(pItemValue
);
197 pDataItemValue
->mpOriginalItemValue
= pItemValue
;
198 maDataItemValues
.push_back(pDataItemValue
);
200 ScPivotFuncData
& rFunctionData
= pDataItemValue
->maFunctionData
;
202 if (rFunctionData
.mnFuncMask
== PIVOT_FUNC_NONE
||
203 rFunctionData
.mnFuncMask
== PIVOT_FUNC_AUTO
)
205 rFunctionData
.mnFuncMask
= PIVOT_FUNC_SUM
;
208 AdjustDuplicateCount(pDataItemValue
);
210 OUString sDataName
= lclCreateDataItemName(
211 rFunctionData
.mnFuncMask
,
212 pDataItemValue
->maName
,
213 rFunctionData
.mnDupCount
);
215 InsertEntry(sDataName
, NULL
, false, nPosition
, pDataItemValue
);
218 void ScPivotLayoutTreeListData::AdjustDuplicateCount(ScItemValue
* pInputItemValue
)
220 ScPivotFuncData
& rInputFunctionData
= pInputItemValue
->maFunctionData
;
222 bool bFoundDuplicate
= false;
224 rInputFunctionData
.mnDupCount
= 0;
225 sal_uInt8 nMaxDuplicateCount
= 0;
227 SvTreeListEntry
* pEachEntry
;
228 for (pEachEntry
= First(); pEachEntry
!= NULL
; pEachEntry
= Next(pEachEntry
))
230 ScItemValue
* pItemValue
= static_cast<ScItemValue
*>(pEachEntry
->GetUserData());
231 if (pItemValue
== pInputItemValue
)
234 ScPivotFuncData
& rFunctionData
= pItemValue
->maFunctionData
;
236 if (rFunctionData
.mnCol
== rInputFunctionData
.mnCol
&&
237 rFunctionData
.mnFuncMask
== rInputFunctionData
.mnFuncMask
)
239 bFoundDuplicate
= true;
240 if(rFunctionData
.mnDupCount
> nMaxDuplicateCount
)
241 nMaxDuplicateCount
= rFunctionData
.mnDupCount
;
247 rInputFunctionData
.mnDupCount
= nMaxDuplicateCount
+ 1;
251 void ScPivotLayoutTreeListData::KeyInput(const KeyEvent
& rKeyEvent
)
253 vcl::KeyCode aCode
= rKeyEvent
.GetKeyCode();
254 sal_uInt16 nCode
= aCode
.GetCode();
256 if (nCode
== KEY_DELETE
)
258 const SvTreeListEntry
* pEntry
= GetCurEntry();
260 GetModel()->Remove(pEntry
);
264 SvTreeListBox::KeyInput(rKeyEvent
);
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */