InfoBar.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. #include "InfoBar.h"
  2. #include "Common/StyleSheet.h"
  3. #include "QFluentWidgets.h"
  4. #include "Common/AutoWrap.h"
  5. #include <QPainter>
  6. #include <QResizeEvent>
  7. #include <QTimer>
  8. InfoBarCloseButton::InfoBarCloseButton(QWidget *parent) : QToolButton(parent)
  9. {
  10. setFixedSize(36, 36);
  11. setIconSize(QSize(12, 12));
  12. setCursor(Qt::PointingHandCursor);
  13. setObjectName("infoBarCloseButton");
  14. FluentStyleSheet::apply("INFO_BAR", this);
  15. m_ficon.reset(NEWFLICON(FluentIcon, CLOSE));
  16. }
  17. void InfoBarCloseButton::paintEvent(QPaintEvent *event)
  18. {
  19. QToolButton::paintEvent(event);
  20. QPainter painter(this);
  21. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  22. m_ficon->render(&painter, QRect(12, 12, 12, 12));
  23. }
  24. QString InfoBarIcon::iconName(InfoBarIcon::IconType type)
  25. {
  26. switch (type) {
  27. case INFORMATION:
  28. return "Info";
  29. case SUCCESS:
  30. return "Success";
  31. case WARNING:
  32. return "Warning";
  33. case ERROR:
  34. return "Error";
  35. }
  36. return "Unknown";
  37. }
  38. InfoBarIcon::InfoBarIcon(IconType type, Qfw::Theme t) : FluentIconBase(""), m_theme(t), m_type(type)
  39. {
  40. iconEngine->setIconPath(iconPath());
  41. }
  42. InfoBarIcon::~InfoBarIcon() { }
  43. QString InfoBarIcon::iconPath()
  44. {
  45. QString colorName;
  46. if (m_theme == Qfw::Theme::AUTO) {
  47. colorName = QFWIns.isDarkTheme() ? "dark" : "light";
  48. } else {
  49. colorName = Qfw::ThemeString(m_theme).toLower();
  50. }
  51. return QString(":/qfluentwidgets/images/info_bar/%1_%2.svg").arg(iconName(m_type)).arg(colorName);
  52. }
  53. QIcon InfoBarIcon::icon()
  54. {
  55. return QIcon(iconEngine->clone());
  56. }
  57. QString InfoBarIcon::typeName() const
  58. {
  59. return iconName(m_type);
  60. }
  61. QString InfoBarIcon::enumName() const
  62. {
  63. QMetaEnum metaEnum = QMetaEnum::fromType<IconType>();
  64. return metaEnum.valueToKey(m_type);
  65. }
  66. FluentIconBase *InfoBarIcon::clone()
  67. {
  68. return new InfoBarIcon(m_type, m_theme);
  69. }
  70. Qfw::Theme InfoBarIcon::theme() const
  71. {
  72. return m_theme;
  73. }
  74. void InfoBarIcon::setTheme(const Qfw::Theme &theme)
  75. {
  76. m_theme = theme;
  77. iconEngine->setIconPath(iconPath());
  78. }
  79. InfoIconWidget::InfoIconWidget(FluentIconBase *icon, QWidget *parent) : QWidget(parent), m_icon(icon)
  80. {
  81. setFixedSize(15, 15);
  82. }
  83. void InfoIconWidget::paintEvent(QPaintEvent * /*event*/)
  84. {
  85. if (!m_icon) {
  86. return;
  87. }
  88. QPainter painter(this);
  89. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  90. InfoBarIcon *barIcon = dynamic_cast<InfoBarIcon *>(m_icon.data());
  91. bool isInformation = false;
  92. if (barIcon) {
  93. isInformation = barIcon->iconEngine->iconPath().split("/").last().contains("Info");
  94. }
  95. if (isInformation) {
  96. m_icon->render(&painter, rect());
  97. } else {
  98. QHash<QString, QString> attributes;
  99. attributes.insert("indexes", "0");
  100. attributes.insert("fill", themeColor().name());
  101. m_icon->render(&painter, rect(), QVector<int>(), attributes);
  102. }
  103. }
  104. InfoBar::InfoBar(FluentIconBase *ficon, const QString &title, const QString &content, Qt::Orientation orient,
  105. bool isClosable, int duration, InfoBarPosition position, QWidget *parent)
  106. : QFrame(parent), m_ficon(ficon)
  107. {
  108. m_title = title;
  109. m_content = content;
  110. m_orient = orient;
  111. m_duration = duration;
  112. m_isClosable = isClosable;
  113. m_position = position;
  114. m_titleLabel = new QLabel(title, this);
  115. m_contentLabel = new QLabel(this);
  116. m_closeButton = new InfoBarCloseButton(this);
  117. m_iconWidget = new InfoIconWidget(m_ficon->clone());
  118. m_vBoxLayout = new QVBoxLayout(this);
  119. m_hBoxLayout = new QHBoxLayout();
  120. if (m_orient == Qt::Horizontal) {
  121. m_contentLayout = new QHBoxLayout();
  122. } else {
  123. m_contentLayout = new QVBoxLayout();
  124. }
  125. m_opacityEffect = new QGraphicsOpacityEffect(this);
  126. m_opacityAni = new QPropertyAnimation(m_opacityEffect, "opacity", this);
  127. m_lightBackgroundColor = QColor();
  128. m_darkBackgroundColor = QColor();
  129. initWidget();
  130. }
  131. void InfoBar::adjustSize()
  132. {
  133. QFrame::adjustSize();
  134. resize(width(), qMax(height(), 50));
  135. }
  136. /// add widget to info bar
  137. void InfoBar::addWidget(QWidget *widget, int stretch)
  138. {
  139. m_contentLayout->addWidget(widget, stretch, Qt::AlignLeft);
  140. }
  141. void InfoBar::setCustomBackgroundColor(const QColor &light, const QColor &dark)
  142. {
  143. m_darkBackgroundColor = dark;
  144. m_lightBackgroundColor = light;
  145. update();
  146. }
  147. InfoBar *InfoBar::creator(FluentIconBase *ficon, const QString &title, const QString &content, Qt::Orientation orient,
  148. bool isClosable, int duration, InfoBarPosition position, QWidget *parent)
  149. {
  150. InfoBar *cls = new InfoBar(ficon, title, content, orient, isClosable, duration, position, parent);
  151. cls->setAttribute(Qt::WA_DeleteOnClose);
  152. cls->show();
  153. return cls;
  154. }
  155. InfoBar *InfoBar::info(const QString &title, const QString &content, Qt::Orientation orient, bool isClosable,
  156. int duration, InfoBarPosition position, QWidget *parent)
  157. {
  158. return creator(NEWFLICON(InfoBarIcon, INFORMATION), title, content, orient, isClosable, duration, position, parent);
  159. }
  160. InfoBar *InfoBar::success(const QString &title, const QString &content, Qt::Orientation orient, bool isClosable,
  161. int duration, InfoBarPosition position, QWidget *parent)
  162. {
  163. return creator(NEWFLICON(InfoBarIcon, SUCCESS), title, content, orient, isClosable, duration, position, parent);
  164. }
  165. InfoBar *InfoBar::warning(const QString &title, const QString &content, Qt::Orientation orient, bool isClosable,
  166. int duration, InfoBarPosition position, QWidget *parent)
  167. {
  168. return creator(NEWFLICON(InfoBarIcon, WARNING), title, content, orient, isClosable, duration, position, parent);
  169. }
  170. InfoBar *InfoBar::error(const QString &title, const QString &content, Qt::Orientation orient, bool isClosable,
  171. int duration, InfoBarPosition position, QWidget *parent)
  172. {
  173. return creator(NEWFLICON(InfoBarIcon, ERROR), title, content, orient, isClosable, duration, position, parent);
  174. }
  175. void InfoBar::initWidget()
  176. {
  177. m_titleLabel->setMinimumHeight(36);
  178. m_opacityEffect->setOpacity(1);
  179. setGraphicsEffect(m_opacityEffect);
  180. m_closeButton->setVisible(m_isClosable);
  181. initLayout();
  182. setQss();
  183. connect(m_closeButton, &InfoBarCloseButton::clicked, this, &InfoBar::close);
  184. }
  185. void InfoBar::setQss()
  186. {
  187. m_titleLabel->setObjectName("titleLabel");
  188. m_contentLabel->setObjectName("contentLabel");
  189. InfoBarIcon *barIcon = dynamic_cast<InfoBarIcon *>(m_ficon.data());
  190. if (barIcon) {
  191. QString typeName = barIcon->iconEngine->iconPath().split("/").last().split("_").first();
  192. setProperty("type", typeName);
  193. } else {
  194. FluentIcon *fluentIcon = dynamic_cast<FluentIcon *>(m_ficon.data());
  195. if (fluentIcon) {
  196. QString typeName = fluentIcon->iconEngine->iconPath().split("/").last().split("_").first();
  197. setProperty("type", typeName);
  198. }
  199. }
  200. FluentStyleSheet::apply("INFO_BAR", this);
  201. }
  202. void InfoBar::initLayout()
  203. {
  204. m_vBoxLayout->setContentsMargins(0, 0, 0, 0);
  205. m_vBoxLayout->setSizeConstraint(QVBoxLayout::SetMinimumSize);
  206. m_contentLayout->setSizeConstraint(QHBoxLayout::SetMinimumSize);
  207. m_vBoxLayout->setSpacing(0);
  208. m_hBoxLayout->setSpacing(0);
  209. m_vBoxLayout->addLayout(m_hBoxLayout);
  210. m_hBoxLayout->addWidget(m_iconWidget);
  211. if (!m_title.isEmpty()) {
  212. m_hBoxLayout->addSpacing(15);
  213. m_hBoxLayout->addWidget(m_titleLabel);
  214. } else {
  215. m_titleLabel->hide();
  216. }
  217. // add content label to layout
  218. if (m_orient == Qt::Horizontal) {
  219. m_vBoxLayout->setAlignment(Qt::AlignVCenter);
  220. m_hBoxLayout->addSpacing(12);
  221. m_contentLayout->setContentsMargins(0, 0, 20 * (m_isClosable ? 1 : 0), 0);
  222. m_contentLayout->setSpacing(12);
  223. m_hBoxLayout->addLayout(m_contentLayout);
  224. } else {
  225. m_vBoxLayout->setAlignment(Qt::AlignTop);
  226. m_contentLayout->setContentsMargins(47, 0, 40, 18);
  227. m_contentLayout->setSpacing(14);
  228. m_vBoxLayout->addLayout(m_contentLayout);
  229. m_contentLayout->setAlignment(Qt::AlignTop);
  230. }
  231. adjustText();
  232. m_contentLayout->addWidget(m_contentLabel);
  233. // add close button to layout
  234. if (m_isClosable) {
  235. m_hBoxLayout->addWidget(m_closeButton);
  236. int mb = (m_orient == Qt::Horizontal) ? 6 : 0;
  237. m_hBoxLayout->setContentsMargins(16, 6, 7, mb);
  238. } else {
  239. m_hBoxLayout->setContentsMargins(16, 6, 16, 6);
  240. }
  241. }
  242. void InfoBar::adjustText()
  243. {
  244. int w = 0;
  245. if (!this->parent()) {
  246. w = 900;
  247. } else {
  248. w = static_cast<QWidget *>(this->parent())->width() - 50;
  249. }
  250. int chars = qMax(qMin(w / 9, 120), 30);
  251. m_contentLabel->setText(TextWrap::wrap(m_content, chars, false).first);
  252. this->adjustSize();
  253. }
  254. bool InfoBar::eventFilter(QObject *watched, QEvent *event)
  255. {
  256. if (watched == this->parent()) {
  257. if (event->type() == QEvent::Resize || event->type() == QEvent::WindowStateChange) {
  258. adjustText();
  259. }
  260. }
  261. return QFrame::eventFilter(watched, event);
  262. }
  263. void InfoBar::paintEvent(QPaintEvent *event)
  264. {
  265. QFrame::paintEvent(event);
  266. if (!m_lightBackgroundColor.isValid()) {
  267. return;
  268. }
  269. QPainter painter(this);
  270. painter.setRenderHints(QPainter::Antialiasing);
  271. painter.setPen(Qt::NoPen);
  272. if (QFWIns.isDarkTheme()) {
  273. painter.setBrush(m_darkBackgroundColor);
  274. } else {
  275. painter.setBrush(m_lightBackgroundColor);
  276. }
  277. QRect rect = this->rect().adjusted(1, 1, -1, -1);
  278. painter.drawRoundedRect(rect, 6, 6);
  279. }
  280. void InfoBar::closeEvent(QCloseEvent * /*event*/)
  281. {
  282. emit closedSignal();
  283. this->deleteLater();
  284. }
  285. void InfoBar::showEvent(QShowEvent *event)
  286. {
  287. adjustText();
  288. QFrame::showEvent(event);
  289. if (m_duration >= 0) {
  290. QTimer::singleShot(m_duration, this, &InfoBar::fadeOut);
  291. }
  292. if (m_position != InfoBarPosition::NONE) {
  293. InfoBarManagerFactory::factory(m_position).add(this);
  294. }
  295. if (this->parent()) {
  296. this->parent()->installEventFilter(this);
  297. }
  298. }
  299. void InfoBar::fadeOut()
  300. {
  301. m_opacityAni->setDuration(200);
  302. m_opacityAni->setStartValue(1);
  303. m_opacityAni->setEndValue(0);
  304. connect(m_opacityAni, &QPropertyAnimation::finished, this, &InfoBar::close);
  305. m_opacityAni->start();
  306. }
  307. InfoBarManagerBase::InfoBarManagerBase(QObject *parent) : QObject(parent)
  308. {
  309. spacing = 16;
  310. margin = 24;
  311. }
  312. void InfoBarManagerBase::add(InfoBar *infoBar)
  313. {
  314. QWidget *p = static_cast<QWidget *>(infoBar->parent());
  315. if (!p) {
  316. return;
  317. }
  318. if (!infoBars.keys().contains(p)) {
  319. p->installEventFilter(this);
  320. infoBars.insert(p, {});
  321. aniGroups.insert(p, new QParallelAnimationGroup(this));
  322. }
  323. if (infoBars[p].contains(infoBar)) {
  324. return;
  325. }
  326. // add drop animation
  327. if (!infoBars[p].isEmpty()) {
  328. QPropertyAnimation *dropAni = new QPropertyAnimation(infoBar, "pos");
  329. dropAni->setDuration(200);
  330. aniGroups[p]->addAnimation(dropAni);
  331. dropAnis.append(dropAni);
  332. infoBar->setProperty("dropAni", QVariant::fromValue<QPropertyAnimation *>(dropAni));
  333. }
  334. // add slide animation
  335. infoBars[p].append(infoBar);
  336. QPropertyAnimation *slideAni = createSlideAni(infoBar);
  337. slideAnis.append(slideAni);
  338. infoBar->setProperty("slideAni", QVariant::fromValue<QPropertyAnimation *>(slideAni));
  339. connect(infoBar, &InfoBar::closedSignal, [=]() { remove(infoBar); });
  340. slideAni->start();
  341. }
  342. void InfoBarManagerBase::remove(InfoBar *infoBar)
  343. {
  344. QWidget *p = static_cast<QWidget *>(infoBar->parent());
  345. if (!p) {
  346. return;
  347. }
  348. if (!infoBars.keys().contains(p)) {
  349. return;
  350. }
  351. infoBars[p].removeAll(infoBar);
  352. // remove drop animation
  353. QPropertyAnimation *dropAni = infoBar->property("dropAni").value<QPropertyAnimation *>();
  354. if (dropAni) {
  355. aniGroups[p]->removeAnimation(dropAni);
  356. dropAnis.removeAll(dropAni);
  357. dropAni->deleteLater();
  358. }
  359. // remove slider animation
  360. QPropertyAnimation *slideAni = infoBar->property("slideAni").value<QPropertyAnimation *>();
  361. if (slideAni) {
  362. slideAnis.removeAll(slideAni);
  363. slideAni->deleteLater();
  364. }
  365. infoBar->deleteLater();
  366. // adjust the position of the remaining info bars
  367. updateDropAni(p);
  368. aniGroups[p]->start();
  369. }
  370. QPropertyAnimation *InfoBarManagerBase::createSlideAni(InfoBar *infoBar)
  371. {
  372. QPropertyAnimation *slideAni = new QPropertyAnimation(infoBar, "pos");
  373. slideAni->setEasingCurve(QEasingCurve::OutQuad);
  374. slideAni->setDuration(200);
  375. slideAni->setStartValue(this->slideStartPos(infoBar));
  376. slideAni->setEndValue(this->pos(infoBar));
  377. return slideAni;
  378. }
  379. void InfoBarManagerBase::updateDropAni(QWidget *parent)
  380. {
  381. for (auto bar : infoBars[parent]) {
  382. QPropertyAnimation *ani = bar->property("dropAni").value<QPropertyAnimation *>();
  383. if (!ani) {
  384. continue;
  385. }
  386. ani->setStartValue(bar->pos());
  387. ani->setEndValue(this->pos(bar));
  388. }
  389. }
  390. bool InfoBarManagerBase::eventFilter(QObject *watched, QEvent *event)
  391. {
  392. QWidget *obj = static_cast<QWidget *>(watched);
  393. if (!obj || !infoBars.keys().contains(obj)) {
  394. return false;
  395. }
  396. if (event->type() == QEvent::Resize || event->type() == QEvent::WindowStateChange) {
  397. QSize size = (event->type() == QEvent::Resize) ? static_cast<QResizeEvent *>(event)->size() : QSize();
  398. for (auto bar : infoBars[obj]) {
  399. bar->move(this->pos(bar, size));
  400. }
  401. }
  402. return QObject::eventFilter(watched, event);
  403. }
  404. TopInfoBarManager::TopInfoBarManager(QObject *parent) : InfoBarManagerBase(parent) { }
  405. QPoint TopInfoBarManager::pos(InfoBar *infoBar, const QSize & /*parentSize*/)
  406. {
  407. QWidget *p = static_cast<QWidget *>(infoBar->parent());
  408. if (!p) {
  409. return QPoint();
  410. }
  411. // QSize size = (parentSize.isValid()) ? parentSize : p->size();
  412. int x = (p->width() - infoBar->width()) / 2;
  413. int y = this->margin;
  414. int index = this->infoBars[p].indexOf(infoBar);
  415. for (int i = 0; i < index; ++i) {
  416. y += this->infoBars[p].at(i)->height() + this->spacing;
  417. }
  418. return QPoint(x, y);
  419. }
  420. QPoint TopInfoBarManager::slideStartPos(InfoBar *infoBar)
  421. {
  422. QPoint pos = this->pos(infoBar);
  423. return QPoint(pos.x(), pos.y() - 16);
  424. }
  425. TopRightInfoBarManager::TopRightInfoBarManager(QObject *parent) : InfoBarManagerBase(parent) { }
  426. QPoint TopRightInfoBarManager::pos(InfoBar *infoBar, const QSize &parentSize)
  427. {
  428. QWidget *p = static_cast<QWidget *>(infoBar->parent());
  429. if (!p) {
  430. return QPoint();
  431. }
  432. QSize size = (parentSize.isValid()) ? parentSize : p->size();
  433. int x = size.width() - infoBar->width() - this->margin;
  434. int y = this->margin;
  435. int index = this->infoBars[p].indexOf(infoBar);
  436. for (int i = 0; i < index; ++i) {
  437. y += this->infoBars[p].at(i)->height() + this->spacing;
  438. }
  439. return QPoint(x, y);
  440. }
  441. QPoint TopRightInfoBarManager::slideStartPos(InfoBar *infoBar)
  442. {
  443. QWidget *p = static_cast<QWidget *>(infoBar->parent());
  444. if (!p) {
  445. return QPoint();
  446. }
  447. return QPoint(p->width(), this->pos(infoBar).y());
  448. }
  449. BottomRightInfoBarManager::BottomRightInfoBarManager(QObject *parent) : InfoBarManagerBase(parent) { }
  450. QPoint BottomRightInfoBarManager::pos(InfoBar *infoBar, const QSize &parentSize)
  451. {
  452. QWidget *p = static_cast<QWidget *>(infoBar->parent());
  453. if (!p) {
  454. return QPoint();
  455. }
  456. QSize size = (parentSize.isValid()) ? parentSize : p->size();
  457. int x = size.width() - infoBar->width() - this->margin;
  458. int y = size.height() - infoBar->height() - this->margin;
  459. int index = this->infoBars[p].indexOf(infoBar);
  460. for (int i = 0; i < index; ++i) {
  461. y -= this->infoBars[p].at(i)->height() + this->spacing;
  462. }
  463. return QPoint(x, y);
  464. }
  465. QPoint BottomRightInfoBarManager::slideStartPos(InfoBar *infoBar)
  466. {
  467. QWidget *p = static_cast<QWidget *>(infoBar->parent());
  468. if (!p) {
  469. return QPoint();
  470. }
  471. return QPoint(p->width(), this->pos(infoBar).y());
  472. }
  473. TopLeftInfoBarManager::TopLeftInfoBarManager(QObject *parent) : InfoBarManagerBase(parent) { }
  474. QPoint TopLeftInfoBarManager::pos(InfoBar *infoBar, const QSize & /*parentSize*/)
  475. {
  476. QWidget *p = static_cast<QWidget *>(infoBar->parent());
  477. if (!p) {
  478. return QPoint();
  479. }
  480. // QSize size = (parentSize.isValid()) ? parentSize : p->size();
  481. int y = this->margin;
  482. int index = this->infoBars[p].indexOf(infoBar);
  483. for (int i = 0; i < index; ++i) {
  484. y += this->infoBars[p].at(i)->height() + this->spacing;
  485. }
  486. return QPoint(this->margin, y);
  487. }
  488. QPoint TopLeftInfoBarManager::slideStartPos(InfoBar *infoBar)
  489. {
  490. return QPoint(-infoBar->width(), this->pos(infoBar).y());
  491. }
  492. BottomLeftInfoBarManager::BottomLeftInfoBarManager(QObject *parent) : InfoBarManagerBase(parent) { }
  493. QPoint BottomLeftInfoBarManager::pos(InfoBar *infoBar, const QSize &parentSize)
  494. {
  495. QWidget *p = static_cast<QWidget *>(infoBar->parent());
  496. if (!p) {
  497. return QPoint();
  498. }
  499. QSize size = (parentSize.isValid()) ? parentSize : p->size();
  500. int y = size.height() - infoBar->height() - this->margin;
  501. int index = this->infoBars[p].indexOf(infoBar);
  502. for (int i = 0; i < index; ++i) {
  503. y -= this->infoBars[p].at(i)->height() + this->spacing;
  504. }
  505. return QPoint(this->margin, y);
  506. }
  507. QPoint BottomLeftInfoBarManager::slideStartPos(InfoBar *infoBar)
  508. {
  509. return QPoint(-infoBar->width(), this->pos(infoBar).y());
  510. }
  511. BottomInfoBarManager::BottomInfoBarManager(QObject *parent) : InfoBarManagerBase(parent) { }
  512. QPoint BottomInfoBarManager::pos(InfoBar *infoBar, const QSize &parentSize)
  513. {
  514. QWidget *p = static_cast<QWidget *>(infoBar->parent());
  515. if (!p) {
  516. return QPoint();
  517. }
  518. QSize size = (parentSize.isValid()) ? parentSize : p->size();
  519. int x = (size.width() - infoBar->width()) / 2;
  520. int y = size.height() - infoBar->height() - this->margin;
  521. int index = this->infoBars[p].indexOf(infoBar);
  522. for (int i = 0; i < index; ++i) {
  523. y -= this->infoBars[p].at(i)->height() + this->spacing;
  524. }
  525. return QPoint(x, y);
  526. }
  527. QPoint BottomInfoBarManager::slideStartPos(InfoBar *infoBar)
  528. {
  529. QPoint pos = this->pos(infoBar);
  530. return QPoint(pos.x(), pos.y() + 16);
  531. }
  532. InfoBarManagerBase &InfoBarManagerFactory::factory(InfoBarPosition postion)
  533. {
  534. static TopInfoBarManager topInfoBarManager;
  535. static BottomInfoBarManager bottomInfoBarManager;
  536. static TopRightInfoBarManager topRightInfoBarManager;
  537. static BottomRightInfoBarManager bottomRightInfoBarManager;
  538. static TopLeftInfoBarManager topLeftInfoBarManager;
  539. static BottomLeftInfoBarManager bottomLeftInfoBarManager;
  540. switch (postion) {
  541. case InfoBarPosition::TOP:
  542. return topInfoBarManager;
  543. case InfoBarPosition::BOTTOM:
  544. return bottomInfoBarManager;
  545. case InfoBarPosition::TOP_RIGHT:
  546. return topRightInfoBarManager;
  547. case InfoBarPosition::BOTTOM_RIGHT:
  548. return bottomRightInfoBarManager;
  549. case InfoBarPosition::TOP_LEFT:
  550. return topLeftInfoBarManager;
  551. case InfoBarPosition::BOTTOM_LEFT:
  552. return bottomLeftInfoBarManager;
  553. default:
  554. return topInfoBarManager;
  555. }
  556. }