ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
TabControl.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <imgui.h>
11 
12 #include <algorithm>
13 #include <cmath>
14 
16 
17 namespace cloudViewer {
18 namespace visualization {
19 namespace gui {
20 
21 namespace {
22 static int g_next_tab_control_id = 1;
23 
24 int CalcTabHeight(const Theme& theme) {
25  auto em = std::ceil(ImGui::GetTextLineHeight());
26  return int(std::ceil(em + 2.0f * ImGui::GetStyle().FramePadding.y));
27 }
28 } // namespace
29 
31  std::vector<std::string> tab_names_;
32  std::string imgui_id_;
33  int current_index_ = 0;
34  std::function<void(int)> on_changed_;
35 };
36 
38  impl_->imgui_id_ =
39  "##tabcontrol_" + std::to_string(g_next_tab_control_id++);
40 }
41 
43 
44 void TabControl::AddTab(const char* name, std::shared_ptr<Widget> panel) {
45  AddChild(panel);
46  // Add spaces around the name to add padding
47  impl_->tab_names_.push_back(std::string(" ") + name + std::string(" "));
48 }
49 
50 void TabControl::SetOnSelectedTabChanged(std::function<void(int)> on_changed) {
51  impl_->on_changed_ = on_changed;
52 }
53 
55  const Constraints& constraints) const {
56  int width = 0, height = 0;
57  for (auto& child : GetChildren()) {
58  auto size = child->CalcPreferredSize(context, constraints);
59  width = std::max(width, size.width);
60  height = std::max(height, size.height);
61  }
62 
63  return Size(width, height + CalcTabHeight(context.theme) + 2);
64 }
65 
67  auto tabHeight = CalcTabHeight(context.theme);
68  auto frame = GetFrame();
69  auto child_rect = Rect(frame.x, frame.y + tabHeight, frame.width,
70  frame.height - tabHeight);
71 
72  for (auto& child : GetChildren()) {
73  child->SetFrame(child_rect);
74  }
75 
77 }
78 
80  auto& frame = GetFrame();
81  ImGui::SetCursorScreenPos(
82  ImVec2(float(frame.x), float(frame.y) - ImGui::GetScrollY()));
83 
86  ImGui::PushItemWidth(float(GetFrame().width));
87  if (ImGui::BeginTabBar(impl_->imgui_id_.c_str())) {
88  for (int i = 0; i < int(impl_->tab_names_.size()); ++i) {
89  if (ImGui::BeginTabItem(impl_->tab_names_[i].c_str())) {
90  auto r = GetChildren()[i]->Draw(context);
91  if (r != Widget::DrawResult::NONE) {
92  result = r;
93  }
94  ImGui::EndTabItem();
95 
96  if (i != impl_->current_index_) {
97  impl_->current_index_ = i;
98  if (impl_->on_changed_) {
99  impl_->on_changed_(i);
100  }
101  }
102  }
103  }
104  ImGui::EndTabBar();
105  }
106  ImGui::PopItemWidth();
108 
109  return result;
110 }
111 
112 } // namespace gui
113 } // namespace visualization
114 } // namespace cloudViewer
Rect frame
int width
int size
std::string name
int height
core::Tensor result
Definition: VtkUtils.cpp:76
DrawResult Draw(const DrawContext &context) override
Definition: TabControl.cpp:79
void AddTab(const char *name, std::shared_ptr< Widget > panel)
Definition: TabControl.cpp:44
void SetOnSelectedTabChanged(std::function< void(int)> on_changed)
Definition: TabControl.cpp:50
void Layout(const LayoutContext &context) override
Definition: TabControl.cpp:66
Size CalcPreferredSize(const LayoutContext &context, const Constraints &constraints) const override
Definition: TabControl.cpp:54
virtual const std::vector< std::shared_ptr< Widget > > GetChildren() const
Definition: Widget.cpp:47
virtual void Layout(const LayoutContext &context)
Definition: Widget.cpp:86
virtual const Rect & GetFrame() const
Returns the frame size in pixels.
Definition: Widget.cpp:51
virtual void AddChild(std::shared_ptr< Widget > child)
Definition: Widget.cpp:43
int max(int a, int b)
Definition: cutil_math.h:48
ImGuiContext * context
Definition: Window.cpp:76
const Theme * theme
Definition: Window.cpp:74
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:89
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20