timewidget.cpp 13 KB

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