timewidget.cpp 14 KB

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