timewidget.cpp 14 KB

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