LibreOffice Module svx (master)  1
InspectorTextPanel.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 <o3tl/safeint.hxx>
21 #include <sal/config.h>
22 
23 #include <svx/dialmgr.hxx>
24 
26 
27 #include <svl/cjkoptions.hxx>
28 #include <svl/ctloptions.hxx>
29 #include <com/sun/star/awt/FontSlant.hpp>
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <inspectorvalues.hrc>
32 
33 using namespace css;
34 
35 const int MinimumPanelWidth = 250;
36 
37 namespace svx::sidebar
38 {
39 std::unique_ptr<PanelLayout> InspectorTextPanel::Create(weld::Widget* pParent)
40 {
41  if (pParent == nullptr)
42  throw lang::IllegalArgumentException("no parent Window given to InspectorTextPanel::Create",
43  nullptr, 0);
44  return std::make_unique<InspectorTextPanel>(pParent);
45 }
46 
47 InspectorTextPanel::InspectorTextPanel(weld::Widget* pParent)
48  : PanelLayout(pParent, "InspectorTextPanel", "svx/ui/inspectortextpanel.ui")
49  , mpListBoxStyles(m_xBuilder->weld_tree_view("listbox_fonts"))
50 {
51  mpListBoxStyles->set_size_request(MinimumPanelWidth, -1);
52  float fWidth = mpListBoxStyles->get_approximate_digit_width();
53  std::vector<int> aWidths{ o3tl::narrowing<int>(fWidth * 29) };
54  // 2nd column will fill remaining space
55  mpListBoxStyles->set_column_fixed_widths(aWidths);
56 }
57 
58 static bool GetPropertyValues(std::u16string_view rPropName, const uno::Any& rAny,
59  OUString& rString)
60 {
61  // Hide Asian and Complex properties
62  if (!SvtCJKOptions::IsCJKFontEnabled() && rPropName.find(u"Asian") != std::u16string_view::npos)
63  return false;
65  && rPropName.find(u"Complex") != std::u16string_view::npos)
66  return false;
67 
68  if (bool bValue; rAny >>= bValue)
69  {
70  rString = SvxResId(bValue ? RID_TRUE : RID_FALSE); // tdf#139136
71  }
72  else if (OUString aValue; (rAny >>= aValue) && !(aValue.isEmpty()))
73  {
74  rString = aValue;
75  }
76  else if (awt::FontSlant eValue; rAny >>= eValue)
77  {
78  rString = SvxResId(eValue == awt::FontSlant_ITALIC ? RID_ITALIC : RID_NORMAL);
79  }
80  else if (tools::Long nValueLong; rAny >>= nValueLong)
81  {
82  if (rPropName.find(u"Color") != std::u16string_view::npos)
83  rString = "0x" + OUString::number(nValueLong, 16);
84  else
85  rString = OUString::number(nValueLong);
86  }
87  else if (double fValue; rAny >>= fValue)
88  {
89  if (rPropName.find(u"Weight") != std::u16string_view::npos)
90  rString = SvxResId(fValue > 100 ? RID_BOLD : RID_NORMAL);
91  else
92  rString = OUString::number((round(fValue * 100)) / 100.00);
93  }
94  else if (short nValueShort; rAny >>= nValueShort)
95  {
96  rString = OUString::number(nValueShort);
97  }
98  else
99  return false;
100 
101  return true;
102 }
103 
104 static void FillBox_Impl(weld::TreeView& rListBoxStyles, const TreeNode& rCurrent,
105  const weld::TreeIter* pParent)
106 {
107  std::unique_ptr<weld::TreeIter> pResult = rListBoxStyles.make_iterator();
108  const OUString& rName = rCurrent.sNodeName;
109  OUString sPairValue;
110 
111  if (!(rCurrent.NodeType != TreeNode::SimpleProperty
112  || GetPropertyValues(rName, rCurrent.aValue, sPairValue)))
113  return;
114 
115  rListBoxStyles.insert(pParent, -1, &rName, nullptr, nullptr, nullptr, false, pResult.get());
116  rListBoxStyles.set_sensitive(*pResult, !rCurrent.isGrey, 0);
117  rListBoxStyles.set_text_emphasis(*pResult, rCurrent.NodeType == TreeNode::Category, 0);
118 
119  if (rCurrent.NodeType == TreeNode::SimpleProperty)
120  {
121  rListBoxStyles.set_text(*pResult, sPairValue, 1);
122  rListBoxStyles.set_sensitive(*pResult, !rCurrent.isGrey, 1);
123  rListBoxStyles.set_text_emphasis(*pResult, false, 1);
124  }
125  else
126  {
127  // Necessary, without this the selection line will be truncated.
128  rListBoxStyles.set_text(*pResult, "", 1);
129  }
130 
131  for (const TreeNode& rChildNode : rCurrent.children)
132  FillBox_Impl(rListBoxStyles, rChildNode, pResult.get());
133 }
134 
135 void InspectorTextPanel::updateEntries(const std::vector<TreeNode>& rStore, const sal_Int32 nParIdx)
136 {
137  mpListBoxStyles->freeze();
138  mpListBoxStyles->clear();
139  for (const TreeNode& rChildNode : rStore)
140  {
141  FillBox_Impl(*mpListBoxStyles, rChildNode, nullptr);
142  }
143 
144  mpListBoxStyles->thaw();
145 
146  weld::TreeView* pTreeDiagram = mpListBoxStyles.get();
147  pTreeDiagram->all_foreach([pTreeDiagram](weld::TreeIter& rEntry) {
148  pTreeDiagram->expand_row(rEntry);
149  return false;
150  });
151 
152  // Collapse "Default Paragraph Style"
153 
154  std::unique_ptr<weld::TreeIter> pEntry = mpListBoxStyles->make_iterator();
155  if (!mpListBoxStyles->get_iter_first(*pEntry))
156  return;
157  // skip the optional metadata items before "Default Paragraph Style"
158  for (sal_Int32 i = 0; i < nParIdx; ++i)
159  {
160  if (!mpListBoxStyles->iter_next_sibling(*pEntry))
161  return;
162  }
163  if (!mpListBoxStyles->iter_next(*pEntry))
164  return;
165 
166  mpListBoxStyles->collapse_row(*pEntry);
167 }
168 
170 
171 } // end of namespace svx::sidebar
172 
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void set_text(int row, const OUString &rText, int col=-1)=0
bool IsCJKFontEnabled()
enum svx::sidebar::TreeNode::@4 NodeType
virtual void insert(const TreeIter *pParent, int pos, const OUString *pStr, const OUString *pId, const OUString *pIconName, VirtualDevice *pImageSurface, bool bChildrenOnDemand, TreeIter *pRet)=0
long Long
bool IsCTLFontEnabled() const
virtual std::unique_ptr< TreeIter > make_iterator(const TreeIter *pOrig=nullptr) const =0
virtual void set_text_emphasis(int row, bool bOn, int col)=0
void updateEntries(const std::vector< TreeNode > &rStore, const sal_Int32 nParIdx)
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
int i
static void FillBox_Impl(weld::TreeView &rListBoxStyles, const TreeNode &rCurrent, const weld::TreeIter *pParent)
void Create(SvxOrientationItem &rItem, SvStream &rStrm, sal_uInt16)
Definition: legacyitem.cxx:34
float u
static bool GetPropertyValues(std::u16string_view rPropName, const uno::Any &rAny, OUString &rString)
const int MinimumPanelWidth
std::vector< TreeNode > children
virtual void expand_row(const TreeIter &rIter)=0
virtual void all_foreach(const std::function< bool(TreeIter &)> &func)=0
std::unique_ptr< weld::TreeView > mpListBoxStyles
virtual void set_sensitive(int row, bool bSensitive, int col=-1)=0