timewidget.cpp 14 KB

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