Files
ts-qt/components/view.cc
2025-10-21 17:00:45 +08:00

199 lines
7.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by Lenn on 2025/10/14.
//
#include <qsize.h>
#include <random>
#include "component.hh"
#include "modern-qt/utility/theme/theme.hh"
#include "modern-qt/utility/wrapper/layout.hh"
#include "modern-qt/utility/wrapper/widget.hh"
#include "components/charts/heatmap.hh"
#include <modern-qt/layout/flow.hh>
#include <modern-qt/layout/linear.hh>
#include <modern-qt/utility/material-icon.hh>
#include <modern-qt/utility/wrapper/mutable-value.hh>
#include <modern-qt/widget/buttons/icon-button.hh>
#include <modern-qt/widget/cards/filled-card.hh>
#include <modern-qt/widget/cards/outlined-card.hh>
#include <modern-qt/widget/image.hh>
#include <modern-qt/widget/shape/wave-circle.hh>
#include <modern-qt/widget/sliders.hh>
#include <modern-qt/widget/switch.hh>
#include <modern-qt/widget/text-fields.hh>
#include <modern-qt/widget/text.hh>
#include <modern-qt/widget/select.hh>
using namespace creeper;
namespace capro = card::pro;
namespace lnpro = linear::pro;
namespace impro = image::pro;
namespace ibpro = icon_button::pro;
namespace slpro = select_widget::pro;
namespace pwpro = plot_widget::pro;
static auto ComConfigComponent(ThemeManager& manager, auto&& callback) {
auto slogen_context = std::make_shared<MutableValue<QString>>();
slogen_context->set_silent("BanG Bream! It's MyGo!!!");
auto select_com_context = std::make_shared<MutableValue<QStringList>>();
select_com_context->set_silent(QStringList {});
auto select_baud_context = std::make_shared<MutableValue<QStringList>>();
select_baud_context->set_silent(QStringList {"9600", "115200"});
const auto row = new Row {
// lnpro::Item<FilledTextField> {
// text_field::pro::ThemeManager {manager},
// text_field::pro::LeadingIcon {material::icon::kSearch, material::regular::font},
// MutableForward {
// text_field::pro::LabelText {},
// slogen_context,
// },
// },
lnpro::Item<MatSelect> {
slpro::ThemeManager {manager},
slpro::LeadingIcon {material::icon::kArrowDropDown, material::regular::font},
slpro::IndexChanged {[&](auto& self){ qDebug() << self.currentIndex();}},
slpro::LeadingText {"COM"},
},
lnpro::Item<MatSelect> {
slpro::ThemeManager {manager },
slpro::LeadingIcon { material::icon::kArrowDropDown, material::regular::font},
slpro::IndexChanged {[&](auto& self){ qDebug() << self.currentIndex();}},
slpro::LeadingText {"Baud"},
// slpro::MutableItems {select_baud_context},
MutableForward {
slpro::SelectItems {},
select_baud_context,
}
},
// lnpro::Item<MatSelect> {
// // slpro::ThemeManager {manager},
// // slpro::LeadingIcon {material::icon::kArrowDropDown, material::regular::font},
// // slpro::IndexChanged {}
// // }
lnpro::SpacingItem {20},
lnpro::Item<IconButton> {
ibpro::ThemeManager {manager},
ibpro::FixedSize {40, 40},
ibpro::Color { IconButton::Color::TONAL },
ibpro::Font { material::kRoundSmallFont },
// ibpro::FontIcon { material::icon::kFavorite },
ibpro::FontIcon { material::icon::kAddLink },
ibpro::Clickable {[slogen_context] {
constexpr auto random_slogen = [] {
constexpr auto slogens = std::array {
"为什么要演奏《春日影》!",
"我从来不觉得玩乐队开心过。",
"我好想…成为人啊!",
"那你愿意……跟我组一辈子的乐队吗?",
"过去软弱的我…已经死了。",
};
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(0, slogens.size() - 1);
return QString::fromUtf8(slogens[dist(gen)]);
};
*slogen_context = random_slogen();
}},
},
lnpro::Item<IconButton> {
ibpro::ThemeManager { manager },
ibpro::FixedSize { 40, 40 },
ibpro::Color { IconButton::Color::TONAL },
ibpro::Font { material::kRoundSmallFont },
ibpro::FontIcon { material::icon::kRefresh },
ibpro::Clickable {[select_baud_context] {
// 定义两组不同的选项
static constexpr auto options_group1 = std::array {
"第一组选项1", "第一组选项2", "第一组选项3"
};
static constexpr auto options_group2 = std::array {
"第二组选项A", "第二组选项B", "第二组选项C", "第二组选项D"
};
// 随机选择一组选项
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(0, 1);
QStringList new_options;
if (dist(gen) == 0) {
for (const auto& option : options_group1) {
new_options << QString::fromUtf8(option);
}
} else {
for (const auto& option : options_group2) {
new_options << QString::fromUtf8(option);
}
}
qDebug() << new_options;
// 更新选项列表MatSelect会自动刷新
*select_baud_context = new_options;
}},
},
};
return new Widget {
widget::pro::Layout {row},
};
}
static auto DisplayComponent(ThemeManager& manager, int index = 0) noexcept {
const auto row = new Row{
lnpro::Item<HeatMapPlot> {
pwpro::MatrixSize {
QSize{3, 4}
},
},
};
return new Widget {
widget::pro::Layout{row},
};
}
auto ViewComponent(ViewComponentState& state) noexcept -> raw_pointer<QWidget> {
const auto texts = std::array {
std::make_shared<MutableValue<QString>>("0.500"),
std::make_shared<MutableValue<QString>>("0.500"),
std::make_shared<MutableValue<QString>>("0.500"),
};
const auto progresses = std::array {
std::make_shared<MutableValue<double>>(0.5),
std::make_shared<MutableValue<double>>(0.5),
std::make_shared<MutableValue<double>>(0.5),
};
return new FilledCard {
capro::ThemeManager { state.manager },
capro::SizePolicy {QSizePolicy::Expanding},
capro::Layout<Col> {
lnpro::Alignment {Qt::AlignTop},
lnpro::Margin {10},
lnpro::Spacing {10},
lnpro::Item {
ComConfigComponent(state.manager,
[texts, progresses] {
constexpr auto random_unit = []() {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_real_distribution<double> dist(0.0, 1.0);
return dist(gen);
};
for (auto&& [string, number] : std::views::zip(texts, progresses)) {
auto v = random_unit();
*number = v;
*string = QString::number(v, 'f', 3);
}
}),
},
lnpro::Item {
DisplayComponent(state.manager),
},
},
};
}