timewidget.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include "timewidget.h"
  2. #include "ui_timewidget.h"
  3. #include <QListWidgetItem>
  4. #include <QMouseEvent>
  5. #include <QDebug>
  6. #include <QSizePolicy>
  7. #include <QFile>
  8. #include "timepartwidget.h"
  9. #include "shadowwidget.h"
  10. #include "UIStyleManager.h"
  11. #include "spdlog/spdlog.h"
  12. #include "timepopupwidget.h"
  13. TimeWidget::TimeWidget(QWidget *parent, ShowType type) :
  14. QFrame(parent),
  15. ui(new Ui::TimeWidget),
  16. m_wdgTimeArea(nullptr),
  17. m_pMainWindow(parent),
  18. m_type(type)
  19. {
  20. ui->setupUi(this);
  21. Init();
  22. }
  23. // TimeWidget::TimeWidget(ShowType type) :
  24. // QFrame(nullptr),
  25. // ui(new Ui::TimeWidget),
  26. // m_wdgTimeArea(nullptr),
  27. // m_pMainWindow(nullptr),
  28. // m_type(type)
  29. // {
  30. // ui->setupUi(this);
  31. // Init();
  32. // }
  33. TimeWidget::~TimeWidget()
  34. {
  35. delete ui;
  36. }
  37. void TimeWidget::SetMainWindow(QWidget* pWidget)
  38. {
  39. if (nullptr != m_pMainWindow) {
  40. this->removeEventFilter(m_pMainWindow);
  41. }
  42. if (nullptr != pWidget) {
  43. pWidget->installEventFilter(this);
  44. }
  45. m_pMainWindow = pWidget;
  46. }
  47. /* 返回时间 */
  48. QTime TimeWidget::getTime()
  49. {
  50. return ui->dateTimeEdit->time();
  51. }
  52. void TimeWidget::setTime(const QTime& t)
  53. {
  54. ui->dateTimeEdit->setTime(t);
  55. ui->lbl_tip->hide();
  56. ui->dateTimeEdit->show();
  57. UpdatePopupTime(ui->dateTimeEdit->dateTime());
  58. m_formerDateTime.setTime(t);
  59. }
  60. void TimeWidget::clearTime()
  61. {
  62. ui->dateTimeEdit->setTime(QTime(0, 0, 0));
  63. ui->dateTimeEdit->hide();
  64. ui->lbl_tip->show();
  65. }
  66. QString TimeWidget::tipText() const
  67. {
  68. return ui->lbl_tip->text();
  69. }
  70. /* 设置时间选择区域的大小,不能超过时间编辑栏的大小,这个主要是防止时间栏太宽,影响美观 */
  71. // void TimeWidget::setTimeAreaWidth(int w)
  72. // {
  73. // if(w < 0)
  74. // {
  75. // m_width = 0;
  76. // }
  77. // else if(w > this->width())
  78. // {
  79. // m_width = this->width();
  80. // }
  81. // else
  82. // {
  83. // m_width = w;
  84. // }
  85. // }
  86. void TimeWidget::showTimeEditArea()
  87. {
  88. this->show();
  89. ShowPopupArea(true);
  90. }
  91. /* 以弹窗的模式模态显示 */
  92. void TimeWidget::execShow()
  93. {
  94. QEventLoop loop;
  95. connect (this, &TimeWidget::signal_close, &loop, &QEventLoop::quit);
  96. this->show();
  97. ShowPopupArea(true);
  98. loop.exec();
  99. deleteLater();
  100. }
  101. /* 设置时间图标 */
  102. void TimeWidget::setIcon(const QString& icon)
  103. {
  104. /* 设置图片适应按钮大小 */
  105. QString ss = QString("border-image: url(%1)").arg(icon);
  106. ui->btn_tip->setStyleSheet(ss);
  107. ui->btn_tip->show();
  108. }
  109. /* 设置图标显示 */
  110. void TimeWidget::setIconShow(bool isShow)
  111. {
  112. if(isShow)
  113. {
  114. ui->btn_tip->show();
  115. }else {
  116. ui->btn_tip->hide();
  117. }
  118. }
  119. /* 设置图标大小 */
  120. void TimeWidget::setIconSize(int w, int h)
  121. {
  122. ui->btn_tip->setMinimumSize(w, h);
  123. /* 设置为固定大小 */
  124. ui->btn_tip->setFixedSize(w, h);
  125. // ui->btn_tip->resize(w, h);
  126. }
  127. /* 设置编辑栏大小 */
  128. void TimeWidget::setEditLine(int w, int h)
  129. {
  130. this->resize(w, h);
  131. }
  132. /* 设置禁止使用滚轮修改时间 */
  133. void TimeWidget::setWheelDisabled(bool disabled)
  134. {
  135. m_isDisableWheel = disabled;
  136. }
  137. /* 设置启用与否*/
  138. void TimeWidget::setEditLineEnable(bool enable)
  139. {
  140. this->setEnabled(enable);
  141. if(!enable)
  142. {
  143. if(m_pTimePopupWidget != nullptr && m_pTimePopupWidget->isVisible())
  144. {
  145. m_isBanPopupShow = true;
  146. ShowPopupArea(false);
  147. UpdateProperty(this, "hover", false);
  148. }
  149. ui->btn_tip->hide();
  150. }else
  151. {
  152. ui->btn_tip->show();
  153. }
  154. }
  155. /* 设置QSS */
  156. void TimeWidget::setQSS()
  157. {
  158. QString qssPath;
  159. if(UIStyle.getUIStyle() == EUIStyle::UI_Light)
  160. {
  161. qssPath = ":/Res/light/timewidget.qss";
  162. } else {
  163. qssPath = ":/Res/dark/timewidget.qss";
  164. }
  165. QFile file(qssPath);
  166. if(file.open(QFile::ReadOnly))
  167. {
  168. QString styleSheet = QLatin1String(file.readAll());
  169. setStyleSheet(styleSheet);
  170. file.close();
  171. }
  172. else
  173. {
  174. SPDLOG_ERROR("open qss file failed: {}", qssPath.toStdString());
  175. }
  176. }
  177. /* 点击了时间图标 */
  178. void TimeWidget::do_pBtn_tip_Clicked()
  179. {
  180. if(m_pTimePopupWidget == nullptr)
  181. {
  182. return;
  183. }
  184. if(m_pTimePopupWidget->isVisible())
  185. {
  186. return;
  187. }
  188. /* 展示弹窗 */
  189. showTimeEditArea();
  190. }
  191. /**
  192. * @brief 这里添加了两个信号,一个是修改过的新时间,一个是旧时间
  193. * @param obj
  194. * @param e
  195. * @return
  196. */
  197. bool TimeWidget::eventFilter(QObject* obj, QEvent* e)
  198. {
  199. if (obj == ui->dateTimeEdit)
  200. {
  201. if(e->type() == QEvent::Wheel && m_isDisableWheel)
  202. {
  203. // 禁止滚轮修改时间
  204. return true;
  205. }
  206. if (e->type() == QEvent::FocusIn && m_type == EditLine)
  207. {
  208. /* 如果Popup正在关闭 */
  209. if(m_isBanPopupShow == true)
  210. {
  211. m_isBanPopupShow = false;
  212. /* 这里需要再次失去焦点,否则下次点击编辑栏无法弹出Popup */
  213. ui->dateTimeEdit->clearFocus();
  214. return QWidget::eventFilter(obj, e);
  215. }
  216. //qInfo() << "dateTimeEdit focusIn";
  217. ShowPopupArea(true);
  218. UpdateProperty(ui->btn_tip, "selected", true);
  219. // emit signal_formerTimer(ui->dateTimeEdit->time());
  220. }
  221. return QWidget::eventFilter(obj, e);
  222. }
  223. else if (obj == ui->lbl_tip)
  224. {
  225. if (e->type() == QEvent::MouseButtonPress && m_type == EditLine)
  226. {
  227. //qInfo() << "mouseButtonPress";
  228. ui->dateTimeEdit->show();
  229. ui->lbl_tip->hide();
  230. ShowPopupArea(true);
  231. //ui->dateTimeEdit->setFocus();//
  232. return QWidget::eventFilter(obj, e);
  233. }
  234. }
  235. else if (obj == this)
  236. {
  237. if (e->type() == QEvent::Enter)
  238. {
  239. UpdateProperty(this, "hover", true);
  240. } else if (e->type() == QEvent::Leave &&
  241. ((m_wdgTimeArea.isNull() && !ui->dateTimeEdit->hasFocus()) ||
  242. (m_wdgTimeArea && m_wdgTimeArea->isHidden())) ) {
  243. UpdateProperty(this, "hover", false);
  244. }
  245. }
  246. /* 判断是不是显示区外面,是的话就隐藏 */
  247. QMouseEvent* pMouse = reinterpret_cast<QMouseEvent*>(e);
  248. if (nullptr != pMouse)
  249. {
  250. if (pMouse->type() == QEvent::MouseButtonPress)
  251. {
  252. //qInfo() << "focusOut";
  253. QPoint gtl = this->mapToGlobal(rect().topLeft());
  254. QRect rc(gtl.x(), gtl.y(), width(), height()); // 全局位置判断
  255. if (!rc.contains(pMouse->globalPos()))
  256. {
  257. if(m_wdgTimeArea == nullptr)
  258. {
  259. return QWidget::eventFilter(obj, e);
  260. }
  261. /* Pupop不可见,直接返回 */
  262. if(!m_wdgTimeArea->isVisible())
  263. {
  264. return QWidget::eventFilter(obj, e);
  265. }
  266. /* 如果焦点不在编辑栏,Popup窗口隐藏后,编辑栏会重新获取焦点,将会再次跳出Popup窗口
  267. 因此在其没有焦点的时候设置 m_isBanPopupShow ,防止Popup隐藏后编辑栏再次获取焦点再次弹出Popup */
  268. if(!ui->dateTimeEdit->hasFocus())
  269. {
  270. m_isBanPopupShow = true;
  271. }
  272. ShowPopupArea(false);
  273. UpdateProperty(this, "hover", false);
  274. if(m_type == Dialog)
  275. {
  276. this->close();
  277. }
  278. }
  279. }
  280. }
  281. return QWidget::eventFilter(obj, e);
  282. }
  283. /**
  284. * @brief m_wdgTimeArea跟随时间栏移动
  285. * @param event
  286. */
  287. void TimeWidget::moveEvent(QMoveEvent *event)
  288. {
  289. if(m_type == Dialog && m_wdgTimeArea != nullptr)
  290. {
  291. QPoint pt = this->mapTo(m_pMainWindow, QPoint(0, 0));
  292. m_wdgTimeArea->move(QPoint(pt.x() - SHADOW_MARGIN, pt.y() + this->height()));
  293. // qDebug() << "posX:" << pt.x() << "posY:" << pt.y();
  294. }
  295. }
  296. /* 这个用来实时修改编辑栏的时间 */
  297. void TimeWidget::do_dateTimeChanged(const QTime& dt)
  298. {
  299. ui->dateTimeEdit->setTime(dt);
  300. }
  301. /**
  302. * @brief QDateTimeEdit控件时间改变事件
  303. * @param dt
  304. */
  305. void TimeWidget::onDateTimeChanged(const QDateTime& dt)
  306. {
  307. if (dt.time() != QTime(0, 0, 0)) {
  308. UpdateProperty(ui->btn_tip, "selected", true);
  309. }
  310. // 同步到popupWidget
  311. if (!m_bTimeFlag) {
  312. UpdatePopupTime(dt);
  313. }
  314. m_bTimeFlag = false;
  315. }
  316. /* 关闭Popup弹窗 */
  317. void TimeWidget::do_closePopup(bool isOk)
  318. {
  319. if(isOk == false)
  320. {
  321. ui->dateTimeEdit->setTime(m_formerDateTime.time());
  322. }
  323. m_isBanPopupShow = true;
  324. ShowPopupArea(false);
  325. UpdateProperty(this, "hover", false);
  326. if(m_type == Dialog)
  327. {
  328. this->close();
  329. }
  330. }
  331. void TimeWidget::UpdateProperty(QObject* obj, const char *name, bool flag)
  332. {
  333. if (nullptr == obj || nullptr == name) {
  334. return;
  335. }
  336. obj->setProperty(name, flag);
  337. QWidget* pWdg = qobject_cast<QWidget*>(obj);
  338. if (nullptr != pWdg) {
  339. this->style()->unpolish(pWdg);
  340. this->style()->polish(pWdg);
  341. }
  342. }
  343. /**
  344. * @brief 更新popup列表选中时间
  345. * @param dt
  346. */
  347. void TimeWidget::UpdatePopupTime(const QDateTime& dt)
  348. {
  349. // 如果时间列表还没初始化就不会设置时间了
  350. if(m_pTimePopupWidget == nullptr)
  351. {
  352. return;
  353. }
  354. m_pTimePopupWidget->setTime(dt);
  355. // for (int i = 0; i < m_vecTimeSections.size(); ++i)
  356. // {
  357. // auto type = m_vecTimeSections.at(i);
  358. // TimePartWidget::emSection emType = static_cast<TimePartWidget::emSection>(type);
  359. // if (emType >= TimePartWidget::emSection::MAX_SECTION) {
  360. // continue;
  361. // }
  362. // if (i < m_vecTimePart.size())
  363. // {
  364. // TimePartWidget* pWdg = m_vecTimePart.at(i);
  365. // if (nullptr == pWdg) continue;
  366. // pWdg->SetTime(dt);
  367. // }
  368. // }
  369. }
  370. /**
  371. * @brief wdgTimeArea区域是创建出来的,不属于ui区域,他的父类是m_pMainWindow,因此移动的时候需要使用m_pMainWindow的坐标
  372. * @param bShow
  373. */
  374. void TimeWidget::ShowPopupArea(bool bShow)
  375. {
  376. if (m_wdgTimeArea.isNull())
  377. {
  378. CreatePopupWidget();
  379. }
  380. if (!m_wdgTimeArea.isNull())
  381. {
  382. if (bShow)
  383. {
  384. // 重新定位再显示
  385. QPoint pt = this->mapTo(m_pMainWindow, QPoint(0, 0));
  386. m_wdgTimeArea->move(QPoint(pt.x() - SHADOW_MARGIN, pt.y() + this->height()));
  387. /* 设置弹窗Popup的大小,使用默认的固定大小,不允许改变
  388. 关于高度:确定取消按钮所占的高度是固定的32,和一节大小一样,所以这里 */
  389. int width = 2 * SHADOW_MARGIN + TIME_AREA_WIDTH * m_vecTimeSections.size();
  390. int height = TIME_AREA_HEIGHT * 7 + 2 * SHADOW_MARGIN + 8;
  391. m_wdgTimeArea->resize(QSize(width, height));
  392. UpdatePopupTime(ui->dateTimeEdit->dateTime());
  393. /* 保存现在的时间 */
  394. m_formerDateTime = ui->dateTimeEdit->dateTime();
  395. m_wdgTimeArea->show();
  396. } else
  397. {
  398. m_wdgTimeArea->hide();
  399. emit signal_close();
  400. /* 关闭显示,发送携带时间的信号,判断一下时间是否改变,不改变不发信号 */
  401. if(m_formerDateTime.time() != ui->dateTimeEdit->time())
  402. {
  403. emit signal_timeChanged(m_formerDateTime.time(), ui->dateTimeEdit->time());
  404. }
  405. /* 让编辑栏失去焦点 */
  406. ui->dateTimeEdit->clearFocus();
  407. }
  408. }
  409. }
  410. void TimeWidget::CreatePopupWidget()
  411. {
  412. // CreateTimeArea
  413. m_vecTimeSections = {TimePartWidget::HOUR, TimePartWidget::MINUTE, TimePartWidget::SECOND};
  414. // CreateTimeVector(m_vecTimeSections);
  415. if(m_pTimePopupWidget == nullptr)
  416. {
  417. m_pTimePopupWidget = new TimePopupWidget();
  418. m_pTimePopupWidget->Init(m_vecTimeSections);
  419. connect(m_pTimePopupWidget, &TimePopupWidget::signal_timeChanged, this, &TimeWidget::do_dateTimeChanged);
  420. connect(m_pTimePopupWidget, &TimePopupWidget::signal_closePopup, this, &TimeWidget::do_closePopup);
  421. }
  422. m_wdgTimeArea.reset(new ShadowWidget(m_pMainWindow));
  423. if (!m_wdgTimeArea.isNull())
  424. {
  425. if (m_wdgTimeArea->centralWidget() != nullptr)
  426. {
  427. m_wdgTimeArea->centralWidget()->setObjectName(QLatin1String("wdg_TimeArea"));
  428. m_wdgTimeArea->centralWidget()->setStyleSheet("QWidget#wdg_TimeArea{border-radius: 2px;border: none; }");
  429. }
  430. /* 创建显示时间条的HBox */
  431. QHBoxLayout* hLayout = new QHBoxLayout();
  432. hLayout->setMargin(1);
  433. hLayout->setSpacing(0);
  434. m_wdgTimeArea->setCentralLayout(hLayout);
  435. if (nullptr == m_wdgTimeArea->getLayout())
  436. {
  437. delete hLayout;
  438. hLayout = nullptr;
  439. }
  440. m_wdgTimeArea->resize(QSize(width() + 2 * SHADOW_MARGIN, TIME_AREA_HEIGHT * 6 + 2 * SHADOW_MARGIN));
  441. m_wdgTimeArea->setMaximumWidth(width() + 2 * SHADOW_MARGIN);
  442. m_wdgTimeArea->hide();
  443. if (nullptr != m_wdgTimeArea->getLayout())
  444. {
  445. m_wdgTimeArea->getLayout()->addWidget(m_pTimePopupWidget);
  446. // foreach (auto wdg, m_vecTimePart)
  447. // {
  448. // m_wdgTimeArea->getLayout()->addWidget(wdg);
  449. // connect(wdg, &TimePartWidget::sigItemClicked, this, &TimeWidget::onListItemClicked);
  450. // }
  451. }
  452. }
  453. }
  454. /* 初始化函数 */
  455. void TimeWidget::Init()
  456. {
  457. /* 设置QSS */
  458. setQSS();
  459. // InitUI
  460. ui->dateTimeEdit->hide();
  461. ui->dateTimeEdit->installEventFilter(this);
  462. this->installEventFilter(this);
  463. ui->lbl_tip->installEventFilter(this);
  464. if (nullptr != m_pMainWindow) {
  465. m_pMainWindow->installEventFilter(this);
  466. }
  467. ui->btn_tip->setProperty("selected", false);
  468. /* 设置时间图标大小 */
  469. ui->btn_tip->setFixedSize(16, 16);
  470. /* 设置初始值 */
  471. QTime t(0, 0, 0);
  472. ui->dateTimeEdit->setTime(t);
  473. ui->lbl_tip->hide();
  474. /* 使用这个阻止show的时候获取焦点自动弹出Popup */
  475. m_isBanPopupShow = true;
  476. ui->dateTimeEdit->show();
  477. m_formerDateTime.setTime(t);
  478. connect(ui->btn_tip, &QPushButton::clicked, this, &TimeWidget::do_pBtn_tip_Clicked);
  479. connect(ui->dateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, &TimeWidget::onDateTimeChanged);
  480. // setDefaultStyle();
  481. }