timewidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. /* 设置禁止使用滚轮修改时间 */
  205. void TimeWidget::setWheelDisabled(bool disabled)
  206. {
  207. m_isDisableWheel = disabled;
  208. }
  209. /* 设置QSS */
  210. void TimeWidget::setQSS()
  211. {
  212. QString qssPath;
  213. if(UIStyle.getUIStyle() == EUIStyle::UI_Light)
  214. {
  215. qssPath = ":/Res/light/timewidget.qss";
  216. } else {
  217. qssPath = ":/Res/dark/timewidget.qss";
  218. }
  219. QFile file(qssPath);
  220. if(file.open(QFile::ReadOnly))
  221. {
  222. QString styleSheet = QLatin1String(file.readAll());
  223. setStyleSheet(styleSheet);
  224. file.close();
  225. }
  226. else
  227. {
  228. SPDLOG_ERROR("open qss file failed: {}", qssPath.toStdString());
  229. }
  230. }
  231. /**
  232. * @brief 点击提示信息
  233. */
  234. void TimeWidget::onBtnTipClicked()
  235. {
  236. bool isSelected = ui->btn_tip->property("selected").toBool();
  237. if (!isSelected) {
  238. // 显示日期
  239. ui->lbl_tip->hide();
  240. ui->dateTimeEdit->show();
  241. ShowTimeArea(true);
  242. } else {
  243. if(m_isDisableClear) { return; } // 如果禁止清除时间,就不执行清除操作
  244. // 清除时间
  245. ui->dateTimeEdit->setTime(QTime(0, 0, 0));
  246. UpdateProperty(ui->btn_tip, "selected", false);
  247. QDateTime dt;
  248. dt.setTime(QTime(0, 0, 0));
  249. UpdatePopupTime(dt);
  250. ShowTimeArea(false);
  251. ui->dateTimeEdit->hide();
  252. ui->lbl_tip->show();
  253. }
  254. }
  255. /**
  256. * @brief 这里添加了两个信号,一个是修改过的新时间,一个是旧时间
  257. * @param obj
  258. * @param e
  259. * @return
  260. */
  261. bool TimeWidget::eventFilter(QObject* obj, QEvent* e)
  262. {
  263. if (obj == ui->dateTimeEdit) {
  264. if(e->type() == QEvent::Wheel && m_isDisableWheel)
  265. {
  266. // 禁止滚轮修改时间
  267. return true;
  268. }
  269. if (e->type() == QEvent::FocusIn && m_type == EditLine)
  270. {
  271. //qInfo() << "dateTimeEdit focusIn";
  272. ShowTimeArea(true);
  273. UpdateProperty(ui->btn_tip, "selected", true);
  274. emit signal_formerTimer(ui->dateTimeEdit->time());
  275. }
  276. return QWidget::eventFilter(obj, e);
  277. } else if (obj == ui->lbl_tip) {
  278. if (e->type() == QEvent::MouseButtonPress && m_type == EditLine)
  279. {
  280. //qInfo() << "mouseButtonPress";
  281. ui->dateTimeEdit->show();
  282. ui->lbl_tip->hide();
  283. ShowTimeArea(true);
  284. //ui->dateTimeEdit->setFocus();//
  285. return QWidget::eventFilter(obj, e);
  286. }
  287. } else if (obj == this) {
  288. if (e->type() == QEvent::Enter) {
  289. UpdateProperty(this, "hover", true);
  290. } else if (e->type() == QEvent::Leave &&
  291. ((m_wdgTimeArea.isNull() && !ui->dateTimeEdit->hasFocus()) ||
  292. (m_wdgTimeArea && m_wdgTimeArea->isHidden())) ) {
  293. UpdateProperty(this, "hover", false);
  294. }
  295. }
  296. /* 判断是不是显示区外面,是的话就隐藏 */
  297. QMouseEvent* pMouse = reinterpret_cast<QMouseEvent*>(e);
  298. if (nullptr != pMouse) {
  299. if (pMouse->type() == QEvent::MouseButtonPress) {
  300. //qInfo() << "focusOut";
  301. QPoint gtl = this->mapToGlobal(rect().topLeft());
  302. QRect rc(gtl.x(), gtl.y(), width(), height()); // 全局位置判断
  303. if (!rc.contains(pMouse->globalPos())) {
  304. ShowTimeArea(false);
  305. ui->dateTimeEdit->clearFocus();
  306. UpdateProperty(this, "hover", false);
  307. /* 关闭显示,发送携带时间的信号 */
  308. emit signal_nowTime(ui->dateTimeEdit->time());
  309. if(m_type == Dialog)
  310. {
  311. this->close();
  312. }
  313. }
  314. }
  315. }
  316. return QWidget::eventFilter(obj, e);
  317. }
  318. /**
  319. * @brief m_wdgTimeArea跟随时间栏移动
  320. * @param event
  321. */
  322. void TimeWidget::moveEvent(QMoveEvent *event)
  323. {
  324. if(m_type == Dialog && m_wdgTimeArea != nullptr)
  325. {
  326. QPoint pt = this->mapTo(m_pMainWindow, QPoint(0, 0));
  327. m_wdgTimeArea->move(QPoint(pt.x() - SHADOW_MARGIN, pt.y() + this->height()));
  328. // qDebug() << "posX:" << pt.x() << "posY:" << pt.y();
  329. }
  330. }
  331. /**
  332. * @brief 时间列表选中事件
  333. * @param item
  334. */
  335. void TimeWidget::onListItemClicked(QListWidgetItem *item)
  336. {
  337. if (nullptr == item) return;
  338. TimePartWidget* pWdg = reinterpret_cast<TimePartWidget*>(QObject::sender());
  339. if (nullptr == pWdg) return;
  340. QString data(item->text());
  341. if (data.isEmpty()) return; // 过滤空白项
  342. QDateTime oldDt(ui->dateTimeEdit->dateTime());
  343. QTime t(ui->dateTimeEdit->time());
  344. switch (pWdg->GetType()) {
  345. case TimePartWidget::HOUR: {
  346. t.setHMS(data.toInt(), t.minute(), t.second());
  347. break;
  348. }
  349. case TimePartWidget::MINUTE: {
  350. t.setHMS(t.hour(), data.toInt(), t.second());
  351. break;
  352. }
  353. case TimePartWidget::SECOND: {
  354. t.setHMS(t.hour(), t.minute(), data.toInt());
  355. break;
  356. }
  357. default:
  358. break;
  359. }
  360. if (oldDt.time() != t) {
  361. m_bTimeFlag = true;
  362. ui->dateTimeEdit->setTime(t);
  363. }
  364. }
  365. /**
  366. * @brief QDateTimeEdit控件时间改变事件
  367. * @param dt
  368. */
  369. void TimeWidget::onDateTimeChanged(const QDateTime& dt)
  370. {
  371. if (dt.time() != QTime(0, 0, 0)) {
  372. UpdateProperty(ui->btn_tip, "selected", true);
  373. }
  374. // 同步到popupWidget
  375. if (!m_bTimeFlag) {
  376. UpdatePopupTime(dt);
  377. }
  378. m_bTimeFlag = false;
  379. }
  380. void TimeWidget::UpdateProperty(QObject* obj, const char *name, bool flag)
  381. {
  382. if (nullptr == obj || nullptr == name) {
  383. return;
  384. }
  385. obj->setProperty(name, flag);
  386. QWidget* pWdg = qobject_cast<QWidget*>(obj);
  387. if (nullptr != pWdg) {
  388. this->style()->unpolish(pWdg);
  389. this->style()->polish(pWdg);
  390. }
  391. }
  392. /**
  393. * @brief 更新popup列表选中时间
  394. * @param dt
  395. */
  396. void TimeWidget::UpdatePopupTime(const QDateTime& dt)
  397. {
  398. // 如果时间列表还没初始化就不会设置时间了
  399. for (int i = 0; i < m_vecTimeSections.size(); ++i) {
  400. auto type = m_vecTimeSections.at(i);
  401. TimePartWidget::emSection emType = static_cast<TimePartWidget::emSection>(type);
  402. if (emType >= TimePartWidget::emSection::MAX_SECTION) {
  403. continue;
  404. }
  405. if (i < m_vecTimePart.size()) {
  406. TimePartWidget* pWdg = m_vecTimePart.at(i);
  407. if (nullptr == pWdg) continue;
  408. pWdg->SetTime(dt);
  409. }
  410. }
  411. }
  412. /**
  413. * @brief wdgTimeArea区域是创建出来的,不属于ui区域,他的父类是m_pMainWindow,因此移动的时候需要使用m_pMainWindow的坐标
  414. * @param bShow
  415. */
  416. void TimeWidget::ShowTimeArea(bool bShow)
  417. {
  418. if (m_wdgTimeArea.isNull()) {
  419. CreatePopupWidget();
  420. }
  421. if (!m_wdgTimeArea.isNull()) {
  422. if (bShow) {
  423. //UpdatePopupTime(ui->dateTimeEdit->dateTime());
  424. // 重新定位再显示
  425. QPoint pt = this->mapTo(m_pMainWindow, QPoint(0, 0));
  426. m_wdgTimeArea->move(QPoint(pt.x() - SHADOW_MARGIN, pt.y() + this->height()));
  427. /* 设置选择条的大小,如果没有设置m_width,就是用时间编辑栏的宽度 */
  428. m_wdgTimeArea->resize((m_width == 0 ? width() : m_width) + 2 * SHADOW_MARGIN, TIME_AREA_HEIGHT * 6 + 2 * SHADOW_MARGIN);
  429. m_wdgTimeArea->setMaximumWidth(width() + 2 * SHADOW_MARGIN);
  430. m_wdgTimeArea->show();
  431. UpdatePopupTime(ui->dateTimeEdit->dateTime());
  432. } else {
  433. m_wdgTimeArea->hide();
  434. emit signal_close();
  435. }
  436. }
  437. }
  438. void TimeWidget::CreatePopupWidget()
  439. {
  440. // CreateTimeArea
  441. m_vecTimeSections = {TimePartWidget::HOUR, TimePartWidget::MINUTE, TimePartWidget::SECOND};
  442. CreateTimeVector(m_vecTimeSections);
  443. m_wdgTimeArea.reset(new ShadowWidget(m_pMainWindow));
  444. if (!m_wdgTimeArea.isNull()) {
  445. if (m_wdgTimeArea->centralWidget() != nullptr) {
  446. m_wdgTimeArea->centralWidget()->setObjectName(QLatin1String("wdg_TimeArea"));
  447. m_wdgTimeArea->centralWidget()->setStyleSheet("QWidget#wdg_TimeArea{border-radius: 2px;border: none; }");
  448. }
  449. QHBoxLayout* hLayout = new QHBoxLayout();
  450. hLayout->setMargin(1);
  451. hLayout->setSpacing(0);
  452. m_wdgTimeArea->setCentralLayout(hLayout);
  453. if (nullptr == m_wdgTimeArea->getLayout()) {
  454. delete hLayout;
  455. hLayout = nullptr;
  456. }
  457. m_wdgTimeArea->resize(QSize(width() + 2 * SHADOW_MARGIN, TIME_AREA_HEIGHT * 6 + 2 * SHADOW_MARGIN));
  458. m_wdgTimeArea->setMaximumWidth(width() + 2 * SHADOW_MARGIN);
  459. m_wdgTimeArea->hide();
  460. if (nullptr != m_wdgTimeArea->getLayout()) {
  461. foreach (auto wdg, m_vecTimePart) {
  462. m_wdgTimeArea->getLayout()->addWidget(wdg);
  463. connect(wdg, &TimePartWidget::sigItemClicked, this, &TimeWidget::onListItemClicked);
  464. }
  465. }
  466. }
  467. }
  468. /* 初始化函数 */
  469. void TimeWidget::Init()
  470. {
  471. /* 设置QSS */
  472. setQSS();
  473. // InitUI
  474. ui->dateTimeEdit->hide();
  475. ui->dateTimeEdit->installEventFilter(this);
  476. this->installEventFilter(this);
  477. ui->lbl_tip->installEventFilter(this);
  478. if (nullptr != m_pMainWindow) {
  479. m_pMainWindow->installEventFilter(this);
  480. }
  481. ui->btn_tip->setProperty("selected", false);
  482. connect(ui->btn_tip, &QPushButton::clicked, this, &TimeWidget::onBtnTipClicked);
  483. connect(ui->dateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, &TimeWidget::onDateTimeChanged);
  484. setDefaultStyle();
  485. }