mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-26 21:51:13 +01:00
Adds bounds checking to `set_selected`, `set_visible`, and `set_tab_name` methods to prevent out-of-bounds access on the tab data structures. Removes unnecessary resizing logic as tab size is defined in constructor.
97 lines
2.3 KiB
C++
97 lines
2.3 KiB
C++
#include "ui_tab_widget.hpp"
|
|
|
|
UI_tab_widget::UI_tab_widget(QObject *parent) : UI_item(UI_TAB_WIDGET_TYPE, parent)
|
|
{
|
|
setup_item(false, false, false);
|
|
}
|
|
|
|
UI_tab_widget::UI_tab_widget(std::vector<std::string> tab_names, size_t selected, QObject *parent)
|
|
: UI_item(UI_TAB_WIDGET_TYPE, parent), m_selected_tab(selected), m_tab_names(tab_names)
|
|
{
|
|
setup_item(false, false, false);
|
|
m_tab_visible.resize(m_tab_names.size(), true);
|
|
}
|
|
|
|
void UI_tab_widget::set_selected(size_t selected)
|
|
{
|
|
if (selected >= m_tab_names.size())
|
|
return;
|
|
m_selected_tab = selected;
|
|
emit ui_changed();
|
|
emit changed(m_selected_tab);
|
|
}
|
|
size_t UI_tab_widget::selected() const
|
|
{
|
|
return m_selected_tab;
|
|
}
|
|
|
|
void UI_tab_widget::set_visible(size_t index, bool visible)
|
|
{
|
|
if (index >= m_tab_visible.size())
|
|
{
|
|
// Only allow known tab amount from constructor
|
|
return;
|
|
}
|
|
m_tab_visible.at(index) = visible;
|
|
emit ui_changed();
|
|
}
|
|
bool UI_tab_widget::visible(size_t index) const
|
|
{
|
|
return m_tab_visible.at(index);
|
|
}
|
|
|
|
void UI_tab_widget::set_tab_name(size_t index, std::string name)
|
|
{
|
|
if (index >= m_tab_names.size() || index >= m_tab_visible.size())
|
|
{
|
|
// Only allow known tab amount from constructor
|
|
return;
|
|
}
|
|
m_tab_names.at(index) = name;
|
|
emit ui_changed();
|
|
}
|
|
std::string UI_tab_widget::tab_name(size_t index) const
|
|
{
|
|
return m_tab_names.at(index);
|
|
}
|
|
|
|
void UI_tab_widget::advance_tab()
|
|
{
|
|
if (m_tab_names.size() == 0)
|
|
return;
|
|
m_selected_tab = (m_selected_tab + 1) % m_tab_names.size();
|
|
emit ui_changed();
|
|
emit changed(m_selected_tab);
|
|
}
|
|
|
|
void UI_tab_widget::from_json(const json &j)
|
|
{
|
|
UI_item::from_json(j);
|
|
if (j.contains("tab_names"))
|
|
{
|
|
this->m_tab_names.clear();
|
|
for (auto option : j["tab_names"])
|
|
{
|
|
this->m_tab_names.push_back(option);
|
|
}
|
|
}
|
|
if (j.contains("tab_visible"))
|
|
{
|
|
this->m_tab_visible.clear();
|
|
for (auto option : j["tab_visible"])
|
|
{
|
|
this->m_tab_visible.push_back(option);
|
|
}
|
|
}
|
|
m_selected_tab = j.at("selected");
|
|
}
|
|
|
|
json UI_tab_widget::to_json(size_t id) const
|
|
{
|
|
json j = UI_item::to_json(id);
|
|
j["tab_names"] = m_tab_names;
|
|
j["tab_visible"] = m_tab_visible;
|
|
j["selected"] = m_selected_tab;
|
|
return j;
|
|
}
|