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 
12 #include <memory>
13 #include <PivotLayoutTreeList.hxx>
14 #include <PivotLayoutDialog.hxx>
15 
16 #include <vcl/event.hxx>
17 #include <pivot.hxx>
18 #include <scabstdlg.hxx>
19 
ScPivotLayoutTreeList(std::unique_ptr<weld::TreeView> xControl)20 ScPivotLayoutTreeList::ScPivotLayoutTreeList(std::unique_ptr<weld::TreeView> xControl)
21     : ScPivotLayoutTreeListBase(std::move(xControl))
22 {
23     mxControl->connect_key_press(LINK(this, ScPivotLayoutTreeList, KeyInputHdl));
24     mxControl->connect_row_activated(LINK(this, ScPivotLayoutTreeList, DoubleClickHdl));
25 }
26 
~ScPivotLayoutTreeList()27 ScPivotLayoutTreeList::~ScPivotLayoutTreeList()
28 {
29 }
30 
Setup(ScPivotLayoutDialog * pParent,SvPivotTreeListType eType)31 void ScPivotLayoutTreeList::Setup(ScPivotLayoutDialog* pParent, SvPivotTreeListType eType)
32 {
33     mpParent = pParent;
34     meType = eType;
35 }
36 
IMPL_LINK_NOARG(ScPivotLayoutTreeList,DoubleClickHdl,weld::TreeView &,bool)37 IMPL_LINK_NOARG(ScPivotLayoutTreeList, DoubleClickHdl, weld::TreeView&, bool)
38 {
39     int nEntry = mxControl->get_cursor_index();
40     if (nEntry == -1)
41         return true;
42 
43     ScItemValue* pCurrentItemValue = reinterpret_cast<ScItemValue*>(mxControl->get_id(nEntry).toInt64());
44     ScPivotFuncData& rCurrentFunctionData = pCurrentItemValue->maFunctionData;
45 
46     if (mpParent->IsDataElement(rCurrentFunctionData.mnCol))
47         return true;
48 
49     SCCOL nCurrentColumn = rCurrentFunctionData.mnCol;
50     ScDPLabelData& rCurrentLabelData = mpParent->GetLabelData(nCurrentColumn);
51 
52     ScAbstractDialogFactory* pFactory = ScAbstractDialogFactory::Create();
53 
54     std::vector<ScDPName> aDataFieldNames;
55     mpParent->PushDataFieldNames(aDataFieldNames);
56 
57     ScopedVclPtr<AbstractScDPSubtotalDlg> pDialog(
58         pFactory->CreateScDPSubtotalDlg(mxControl.get(), mpParent->maPivotTableObject, rCurrentLabelData, rCurrentFunctionData, aDataFieldNames));
59 
60     if (pDialog->Execute() == RET_OK)
61     {
62         pDialog->FillLabelData(rCurrentLabelData);
63         rCurrentFunctionData.mnFuncMask = pDialog->GetFuncMask();
64     }
65 
66     return true;
67 }
68 
FillFields(ScPivotFieldVector & rFieldVector)69 void ScPivotLayoutTreeList::FillFields(ScPivotFieldVector& rFieldVector)
70 {
71     mxControl->clear();
72     maItemValues.clear();
73 
74     for (const ScPivotField& rField : rFieldVector)
75     {
76         OUString aLabel = mpParent->GetItem( rField.nCol )->maName;
77         ScItemValue* pItemValue = new ScItemValue( aLabel, rField.nCol, rField.nFuncMask );
78         maItemValues.push_back(std::unique_ptr<ScItemValue>(pItemValue));
79         OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pItemValue)));
80         mxControl->append(sId, pItemValue->maName);
81     }
82 }
83 
InsertEntryForSourceTarget(weld::TreeView & rSource,int nTarget)84 void ScPivotLayoutTreeList::InsertEntryForSourceTarget(weld::TreeView& rSource, int nTarget)
85 {
86     ScItemValue* pItemValue = reinterpret_cast<ScItemValue*>(rSource.get_selected_id().toInt64());
87     ScItemValue* pOriginalItemValue = pItemValue->mpOriginalItemValue;
88 
89     // Don't allow to add "Data" element to page fields
90     if(meType == PAGE_LIST && mpParent->IsDataElement(pItemValue->maFunctionData.mnCol))
91         return;
92 
93     mpParent->ItemInserted(pOriginalItemValue, meType);
94 
95     InsertEntryForItem(pOriginalItemValue, nTarget);
96 }
97 
InsertEntryForItem(const ScItemValue * pItemValue,int nPosition)98 void ScPivotLayoutTreeList::InsertEntryForItem(const ScItemValue* pItemValue, int nPosition)
99 {
100     ScItemValue *pListItemValue = new ScItemValue(pItemValue);
101     maItemValues.push_back(std::unique_ptr<ScItemValue>(pListItemValue));
102     OUString sName = pListItemValue->maName;
103     OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pListItemValue)));
104     mxControl->insert(nullptr, nPosition, &sName, &sId, nullptr, nullptr, nullptr, false, nullptr);
105 }
106 
IMPL_LINK(ScPivotLayoutTreeList,KeyInputHdl,const KeyEvent &,rKeyEvent,bool)107 IMPL_LINK(ScPivotLayoutTreeList, KeyInputHdl, const KeyEvent&, rKeyEvent, bool)
108 {
109     vcl::KeyCode aCode = rKeyEvent.GetKeyCode();
110     sal_uInt16 nCode = aCode.GetCode();
111 
112     if (nCode == KEY_DELETE)
113     {
114         const int nEntry = mxControl->get_cursor_index();
115         if (nEntry != -1)
116             mxControl->remove(nEntry);
117         return true;
118     }
119 
120     return false;
121 }
122 
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
124