timewidget.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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) && (m_pTimePopupWidget->isVisible()))
  181. {
  182. return;
  183. }
  184. /* 展示弹窗 */
  185. showTimeEditArea();
  186. }
  187. /**
  188. * @brief 这里添加了两个信号,一个是修改过的新时间,一个是旧时间
  189. * @param obj
  190. * @param e
  191. * @return
  192. */
  193. bool TimeWidget::eventFilter(QObject* obj, QEvent* e)
  194. {
  195. /* 判断是否被禁用了 */
  196. if(!this->isEnabled())
  197. {
  198. return QWidget::eventFilter(obj, e);
  199. }
  200. /* 判断是否是鼠标点击事件 */
  201. if(e == nullptr || obj == nullptr)
  202. {
  203. return QWidget::eventFilter(obj, e);
  204. }
  205. if (obj == ui->dateTimeEdit)
  206. {
  207. if(e->type() == QEvent::Wheel && m_isDisableWheel)
  208. {
  209. // 禁止滚轮修改时间
  210. return true;
  211. }
  212. if (e->type() == QEvent::FocusIn && m_type == EditLine)
  213. {
  214. /* 如果Popup正在关闭 */
  215. // if(m_isBanPopupShow == true)
  216. // {
  217. // m_isBanPopupShow = false;
  218. // /* 这里需要再次失去焦点,否则下次点击编辑栏无法弹出Popup */
  219. // ui->dateTimeEdit->clearFocus();
  220. // return QWidget::eventFilter(obj, e);
  221. // }
  222. //qInfo() << "dateTimeEdit focusIn";
  223. ShowPopupArea(true);
  224. UpdateProperty(ui->btn_tip, "selected", true);
  225. // emit signal_formerTimer(ui->dateTimeEdit->time());
  226. }
  227. return QWidget::eventFilter(obj, e);
  228. }
  229. else if (obj == ui->lbl_tip)
  230. {
  231. if (e->type() == QEvent::MouseButtonPress && m_type == EditLine)
  232. {
  233. //qInfo() << "mouseButtonPress";
  234. ui->dateTimeEdit->show();
  235. ui->lbl_tip->hide();
  236. ShowPopupArea(true);
  237. //ui->dateTimeEdit->setFocus();//
  238. return QWidget::eventFilter(obj, e);
  239. }
  240. }
  241. else if (obj == this)
  242. {
  243. if (e->type() == QEvent::Enter)
  244. {
  245. UpdateProperty(this, "hover", true);
  246. } else if (e->type() == QEvent::Leave &&
  247. ((m_wdgTimeArea.isNull() && !ui->dateTimeEdit->hasFocus()) ||
  248. (m_wdgTimeArea && m_wdgTimeArea->isHidden())) ) {
  249. UpdateProperty(this, "hover", false);
  250. }
  251. }
  252. /* 鼠标点击事件,判断是不是显示区外面,是的话就隐藏,是内部就显示弹窗 */
  253. if(e->type() == QEvent::MouseButtonPress)
  254. {
  255. QMouseEvent* pMouse = reinterpret_cast<QMouseEvent*>(e);
  256. if (nullptr != pMouse)
  257. {
  258. QPoint gtl = this->mapToGlobal(rect().topLeft());
  259. QRect rc(gtl.x(), gtl.y(), width(), height()); // 全局位置判断
  260. if (!rc.contains(pMouse->globalPos()))
  261. {
  262. if(m_wdgTimeArea == nullptr)
  263. {
  264. return QWidget::eventFilter(obj, e);
  265. }
  266. /* Pupop不可见,直接返回 */
  267. if(!m_wdgTimeArea->isVisible())
  268. {
  269. return QWidget::eventFilter(obj, e);
  270. }
  271. /* 如果焦点不在编辑栏,Popup窗口隐藏后,编辑栏会重新获取焦点,将会再次跳出Popup窗口
  272. 因此在其没有焦点的时候设置 m_isBanPopupShow ,防止Popup隐藏后编辑栏再次获取焦点再次弹出Popup */
  273. // if(!ui->dateTimeEdit->hasFocus())
  274. // {
  275. // m_isBanPopupShow = true;
  276. // }
  277. ShowPopupArea(false);
  278. UpdateProperty(this, "hover", false);
  279. if(m_type == Dialog)
  280. {
  281. this->close();
  282. }
  283. }
  284. else
  285. {
  286. /* 鼠标点击在了编辑栏内部 */
  287. if(m_wdgTimeArea != nullptr && m_wdgTimeArea->isVisible())
  288. {
  289. return QWidget::eventFilter(obj, e);
  290. }
  291. ShowPopupArea(true);
  292. UpdateProperty(this, "hover", true);
  293. }
  294. }
  295. }
  296. return QWidget::eventFilter(obj, e);
  297. }
  298. /**
  299. * @brief m_wdgTimeArea跟随时间栏移动
  300. * @param event
  301. */
  302. void TimeWidget::moveEvent(QMoveEvent *event)
  303. {
  304. if(m_type == Dialog && m_wdgTimeArea != nullptr)
  305. {
  306. QPoint pt = this->mapTo(m_pMainWindow, QPoint(0, 0));
  307. m_wdgTimeArea->move(QPoint(pt.x() - SHADOW_MARGIN, pt.y() + this->height()));
  308. // qDebug() << "posX:" << pt.x() << "posY:" << pt.y();
  309. }
  310. }
  311. /* 这个用来实时修改编辑栏的时间 */
  312. void TimeWidget::do_dateTimeChanged(const QTime& dt)
  313. {
  314. ui->dateTimeEdit->setTime(dt);
  315. }
  316. /**
  317. * @brief QDateTimeEdit控件时间改变事件
  318. * @param dt
  319. */
  320. void TimeWidget::onDateTimeChanged(const QDateTime& dt)
  321. {
  322. if (dt.time() != QTime(0, 0, 0)) {
  323. UpdateProperty(ui->btn_tip, "selected", true);
  324. }
  325. // 同步到popupWidget
  326. if (!m_bTimeFlag) {
  327. UpdatePopupTime(dt);
  328. }
  329. m_bTimeFlag = false;
  330. }
  331. /* 关闭Popup弹窗 */
  332. void TimeWidget::do_closePopup(bool isOk)
  333. {
  334. if(isOk == false)
  335. {
  336. ui->dateTimeEdit->setTime(m_formerDateTime.time());
  337. }
  338. // m_isBanPopupShow = true;
  339. ShowPopupArea(false);
  340. UpdateProperty(this, "hover", false);
  341. if(m_type == Dialog)
  342. {
  343. this->close();
  344. }
  345. }
  346. void TimeWidget::UpdateProperty(QObject* obj, const char *name, bool flag)
  347. {
  348. if (nullptr == obj || nullptr == name) {
  349. return;
  350. }
  351. obj->setProperty(name, flag);
  352. QWidget* pWdg = qobject_cast<QWidget*>(obj);
  353. if (nullptr != pWdg) {
  354. this->style()->unpolish(pWdg);
  355. this->style()->polish(pWdg);
  356. }
  357. }
  358. /**
  359. * @brief 更新popup列表选中时间
  360. * @param dt
  361. */
  362. void TimeWidget::UpdatePopupTime(const QDateTime& dt)
  363. {
  364. // 如果时间列表还没初始化就不会设置时间了
  365. if(m_pTimePopupWidget == nullptr)
  366. {
  367. return;
  368. }
  369. m_pTimePopupWidget->setTime(dt);
  370. // for (int i = 0; i < m_vecTimeSections.size(); ++i)
  371. // {
  372. // auto type = m_vecTimeSections.at(i);
  373. // TimePartWidget::emSection emType = static_cast<TimePartWidget::emSection>(type);
  374. // if (emType >= TimePartWidget::emSection::MAX_SECTION) {
  375. // continue;
  376. // }
  377. // if (i < m_vecTimePart.size())
  378. // {
  379. // TimePartWidget* pWdg = m_vecTimePart.at(i);
  380. // if (nullptr == pWdg) continue;
  381. // pWdg->SetTime(dt);
  382. // }
  383. // }
  384. }
  385. /**
  386. * @brief wdgTimeArea区域是创建出来的,不属于ui区域,他的父类是m_pMainWindow,因此移动的时候需要使用m_pMainWindow的坐标
  387. * @param bShow
  388. */
  389. void TimeWidget::ShowPopupArea(bool bShow)
  390. {
  391. if (m_wdgTimeArea.isNull())
  392. {
  393. CreatePopupWidget();
  394. }
  395. if (!m_wdgTimeArea.isNull())
  396. {
  397. if (bShow)
  398. {
  399. // 重新定位再显示
  400. QPoint pt = this->mapTo(m_pMainWindow, QPoint(0, 0));
  401. m_wdgTimeArea->move(QPoint(pt.x() - SHADOW_MARGIN, pt.y() + this->height()));
  402. /* 设置弹窗Popup的大小,使用默认的固定大小,不允许改变
  403. 关于高度:确定取消按钮所占的高度是固定的32,和一节大小一样,所以这里 */
  404. int width = 2 * SHADOW_MARGIN + TIME_AREA_WIDTH * m_vecTimeSections.size();
  405. int height = TIME_AREA_HEIGHT * 7 + 2 * SHADOW_MARGIN + 8;
  406. m_wdgTimeArea->resize(QSize(width, height));
  407. UpdatePopupTime(ui->dateTimeEdit->dateTime());
  408. /* 保存现在的时间 */
  409. m_formerDateTime = ui->dateTimeEdit->dateTime();
  410. m_wdgTimeArea->show();
  411. } else
  412. {
  413. m_wdgTimeArea->hide();
  414. emit signal_close();
  415. /* 关闭显示,发送携带时间的信号,判断一下时间是否改变,不改变不发信号 */
  416. if(m_formerDateTime.time() != ui->dateTimeEdit->time())
  417. {
  418. emit signal_timeChanged(m_formerDateTime.time(), ui->dateTimeEdit->time());
  419. }
  420. /* 让编辑栏失去焦点 */
  421. ui->dateTimeEdit->clearFocus();
  422. }
  423. }
  424. }
  425. void TimeWidget::CreatePopupWidget()
  426. {
  427. // CreateTimeArea
  428. m_vecTimeSections = {TimePartWidget::HOUR, TimePartWidget::MINUTE, TimePartWidget::SECOND};
  429. // CreateTimeVector(m_vecTimeSections);
  430. if(m_pTimePopupWidget == nullptr)
  431. {
  432. m_pTimePopupWidget = new TimePopupWidget();
  433. m_pTimePopupWidget->Init(m_vecTimeSections);
  434. connect(m_pTimePopupWidget, &TimePopupWidget::signal_timeChanged, this, &TimeWidget::do_dateTimeChanged);
  435. connect(m_pTimePopupWidget, &TimePopupWidget::signal_closePopup, this, &TimeWidget::do_closePopup);
  436. }
  437. m_wdgTimeArea.reset(new ShadowWidget(m_pMainWindow));
  438. if (!m_wdgTimeArea.isNull())
  439. {
  440. if (m_wdgTimeArea->centralWidget() != nullptr)
  441. {
  442. m_wdgTimeArea->centralWidget()->setObjectName(QLatin1String("wdg_TimeArea"));
  443. m_wdgTimeArea->centralWidget()->setStyleSheet("QWidget#wdg_TimeArea{border-radius: 2px;border: none; }");
  444. }
  445. /* 创建显示时间条的HBox */
  446. QHBoxLayout* hLayout = new QHBoxLayout();
  447. hLayout->setMargin(1);
  448. hLayout->setSpacing(0);
  449. m_wdgTimeArea->setCentralLayout(hLayout);
  450. if (nullptr == m_wdgTimeArea->getLayout())
  451. {
  452. delete hLayout;
  453. hLayout = nullptr;
  454. }
  455. m_wdgTimeArea->resize(QSize(width() + 2 * SHADOW_MARGIN, TIME_AREA_HEIGHT * 6 + 2 * SHADOW_MARGIN));
  456. m_wdgTimeArea->setMaximumWidth(width() + 2 * SHADOW_MARGIN);
  457. m_wdgTimeArea->hide();
  458. if (nullptr != m_wdgTimeArea->getLayout())
  459. {
  460. m_wdgTimeArea->getLayout()->addWidget(m_pTimePopupWidget);
  461. // foreach (auto wdg, m_vecTimePart)
  462. // {
  463. // m_wdgTimeArea->getLayout()->addWidget(wdg);
  464. // connect(wdg, &TimePartWidget::sigItemClicked, this, &TimeWidget::onListItemClicked);
  465. // }
  466. }
  467. }
  468. }
  469. /* 初始化函数 */
  470. void TimeWidget::Init()
  471. {
  472. /* 设置QSS */
  473. setQSS();
  474. // InitUI
  475. ui->dateTimeEdit->hide();
  476. ui->dateTimeEdit->installEventFilter(this);
  477. this->installEventFilter(this);
  478. ui->lbl_tip->installEventFilter(this);
  479. if (nullptr != m_pMainWindow) {
  480. m_pMainWindow->installEventFilter(this);
  481. }
  482. ui->btn_tip->setProperty("selected", false);
  483. /* 设置时间图标大小 */
  484. ui->btn_tip->setFixedSize(16, 16);
  485. /* 设置初始值 */
  486. QTime t(0, 0, 0);
  487. ui->dateTimeEdit->setTime(t);
  488. ui->lbl_tip->hide();
  489. /* 使用这个阻止show的时候获取焦点自动弹出Popup */
  490. // m_isBanPopupShow = true;
  491. ui->dateTimeEdit->show();
  492. m_formerDateTime.setTime(t);
  493. connect(ui->btn_tip, &QPushButton::clicked, this, &TimeWidget::do_pBtn_tip_Clicked);
  494. connect(ui->dateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, &TimeWidget::onDateTimeChanged);
  495. // setDefaultStyle();
  496. }