geopro/src/app/ToastOverlay.hpp

43 lines
1.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.

#pragma once
// 浮动轻提示(规范 §7.7 Toast底部居中浮出的小卡片bg/panel 底 + 1px border/default 边框
// + 左侧 3px 状态色竖条(默认 status/info+ 文案;~3500ms 自动消失,点击立即关闭。
// 纯外观组件,不触碰任何业务/渲染/数据逻辑。
//
// 实现取「单例可复用」方案:每个顶层窗口共用一个 ToastOverlay 子控件,新消息直接替换当前文案
// 并重置计时器——避免多实例叠放带来的悬垂指针风险。作为锚窗口的子控件,随窗口移动/缩放自动跟随。
// Qt QSS 画不出阴影,故以「边框 + 半透明底」近似浮层高度,不强行模拟 box-shadow。
#include <QWidget>
class QLabel;
class QTimer;
namespace geopro::app {
// 浮动 Toast 卡片:作为锚窗口的子控件存在,定位在窗口内容区底部居中。
class ToastOverlay : public QWidget {
Q_OBJECT
public:
explicit ToastOverlay(QWidget* parent);
// 显示一条提示(替换当前内容并重置自动消失计时器)。
void showMessage(const QString& msg);
protected:
bool eventFilter(QObject* obj, QEvent* event) override; // 跟随锚窗口尺寸/移动变化重定位
void mousePressEvent(QMouseEvent* event) override; // 点击立即关闭
private:
void reposition(); // 依据父窗口内容区,置于底部居中
QLabel* label_ = nullptr;
QTimer* timer_ = nullptr;
};
// 便捷自由函数:在 anchorWindow 上显示一条 Toast。多次调用复用同一个 overlay 实例。
// anchorWindow 取传入控件的顶层窗口,保证 toast 浮在主窗口内容区上。
void showToast(QWidget* anchorWindow, const QString& msg);
} // namespace geopro::app