思路:
我们首先遍历m_mBubbleTextForTimerIndex
,查找是否存在同样的文案。如果找到了相同的文案,我们会先删除旧的定时器索引项,然后创建一个新的定时器,并将文案和对应的弹窗项和定时器索引添加到m_mBubbleTextForTimerIndex
和m_mBubbleWidgetTimerIndex
中。
如果没有找到相同的文案,我们会继续执行原本的逻辑,创建新的弹窗项和定时器,并将它们添加到对应的映射中。
这样做的效果是,如果同样的文案进来,它会刷新上一次出现的弹窗的显示时间;如果是新的文案,它会创建新的弹窗项和定时器。
void ZRUiBubble::AddBubbleItem(BubbleType type, const QString& text)
{
// 查找已有的同样文案的定时器索引
std::map<int, QString>::iterator iter = m_mBubbleTextForTimerIndex.begin();
for (; iter != m_mBubbleTextForTimerIndex.end(); ++iter)
{
if (iter->second == text)
{
// 更新定时器的时间
int timerIndex = iter->first;
killTimer(timerIndex);
int newTimerIndex = startTimer(kiBubbleShowTime);
m_mBubbleTextForTimerIndex[newTimerIndex] = iter->second;
m_mBubbleWidgetTimerIndex[newTimerIndex] = m_mBubbleWidgetTimerIndex[timerIndex];
// 删除旧的定时器索引项
m_mBubbleTextForTimerIndex.erase(iter);
m_mBubbleWidgetTimerIndex.erase(timerIndex);
return;
}
}
// 创建新的弹窗项和定时器
QHBoxLayout* bubbleItemLayout = CreateBubbleItem(type, text);
if (bubbleItemLayout == nullptr)
{
ST_STF::STF_LOG_ERROR("Create Bubble Item Failed !");
return;
}
m_pVBoxLayout->addLayout(bubbleItemLayout);
int timerIndex = startTimer(kiBubbleShowTime);
m_mBubbleWidgetTimerIndex[timerIndex] = m_vBubbleWidget.back();
m_mBubbleTextForTimerIndex[timerIndex] = text;
}