#pragma once #include #include namespace YSG { template class RollObject : QObject { public: explicit RollObject(QObject *parent = nullptr) : QObject(parent) {} explicit RollObject(QVector widgets, std::function enterFunc, std::function leaveFunc, QObject *parent) : mWidgets(std::move(widgets)), mEnterFunc(enterFunc), mLeaveFunc(leaveFunc), QObject(parent) { if (mEnterFunc) mEnterFunc(mWidgets[mIndex]); } int mIndex = 0; QVector mWidgets; std::function mEnterFunc; std::function mLeaveFunc; T *cur() { if (mWidgets.empty()) return nullptr; return mWidgets[mIndex]; } void setCur(T *cur) { if (mWidgets.empty()) return; int index = mWidgets.indexOf(cur); if (mLeaveFunc) mLeaveFunc(mWidgets[mIndex]); mIndex = index; if (mEnterFunc) mEnterFunc(mWidgets[mIndex]); mWidgets[mIndex]->update(); } void rollRight() { if (mWidgets.empty()) return; if (mLeaveFunc) mLeaveFunc(mWidgets[mIndex]); mWidgets[mIndex]->update(); if (mWidgets.last() != mWidgets[mIndex]) { mIndex++; } else { mIndex = 0; } if (mEnterFunc) mEnterFunc(mWidgets[mIndex]); mWidgets[mIndex]->update(); } void rollLeft() { if (mWidgets.empty()) return; if (mLeaveFunc) mLeaveFunc(mWidgets[mIndex]); mWidgets[mIndex]->update(); if (mWidgets.first() != mWidgets[mIndex]) { mIndex--; } else { mIndex = mWidgets.size() - 1; } if (mEnterFunc) mEnterFunc(mWidgets[mIndex]); mWidgets[mIndex]->update(); } QHBoxLayout *hBoxLayout(int space) { auto ret = new QHBoxLayout; ret->setMargin(0); ret->setSpacing(space); for (auto widget: mWidgets) { ret->addWidget(widget, 1); } return ret; } }; }