timewidget.cpp 14 KB

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