#pragma once #include "global.h" namespace Flex_Qt { template class Roll_Object : QObject { public: explicit Roll_Object(QObject* parent = nullptr) : QObject(parent) {} explicit Roll_Object(std::vector values, std::function enter, std::function leave, QObject* parent) : QObject(parent), widgets(std::move(values)), enter_func(std::move(enter)), leave_func(std::move(leave)) { if (enter_func && !widgets.empty()) enter_func(widgets.front()); } int index = 0; std::vector widgets; std::function enter_func; std::function leave_func; T* cur() { if (widgets.empty()) return nullptr; return widgets[static_cast(index)]; } void set_cur(T* value) { if (widgets.empty()) return; auto found = std::find(widgets.begin(), widgets.end(), value); if (found == widgets.end()) return; if (leave_func) leave_func(widgets[static_cast(index)]); index = static_cast(std::distance(widgets.begin(), found)); if (enter_func) enter_func(widgets[static_cast(index)]); widgets[static_cast(index)]->update(); } void roll_right() { if (widgets.empty()) return; if (leave_func) leave_func(widgets[static_cast(index)]); widgets[static_cast(index)]->update(); index = (index + 1) % static_cast(widgets.size()); if (enter_func) enter_func(widgets[static_cast(index)]); widgets[static_cast(index)]->update(); } void roll_left() { if (widgets.empty()) return; if (leave_func) leave_func(widgets[static_cast(index)]); widgets[static_cast(index)]->update(); index = index ? index - 1 : static_cast(widgets.size()) - 1; if (enter_func) enter_func(widgets[static_cast(index)]); widgets[static_cast(index)]->update(); } QHBoxLayout* hbox_layout(int space) { auto ret = new QHBoxLayout; ret->setMargin(0); ret->setSpacing(space); for (T* widget : widgets) ret->addWidget(widget, 1); return ret; } }; } // Flex_Qt