timewidget.cpp 14 KB

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