timewidget.cpp 12 KB

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