timepartwidget.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "timepartwidget.h"
  2. #include "ui_timepartwidget.h"
  3. #include <QListWidgetItem>
  4. #include <QScrollBar>
  5. #include <QDateTime>
  6. #include <QMouseEvent>
  7. #include <QDebug>
  8. #include <QFile>
  9. #include "mytimedelegate.h"
  10. #include "lhstylemanager.h"
  11. #include "LHQLogApi.h"
  12. TimePartWidget::TimePartWidget(QWidget *parent) :
  13. QWidget(parent),
  14. ui(new Ui::TimePartWidget)
  15. {
  16. ui->setupUi(this);
  17. }
  18. TimePartWidget::TimePartWidget(TimePartWidget::emSection type, QWidget* parent) :
  19. TimePartWidget(parent)
  20. {
  21. m_type = type;
  22. // 设置代理
  23. m_delegate.reset(new MyTimeDelegate(this));
  24. if (nullptr != m_delegate.data()) {
  25. ui->listWidget->setItemDelegate(m_delegate.data());
  26. }
  27. // 设置滚动条
  28. m_pListBar.reset(new QScrollBar(Qt::Orientation::Vertical, ui->listWidget));
  29. if (nullptr != m_pListBar.data()) {
  30. m_pListBar->move(width() - 4, 1); // 宽度4,与右侧距离2
  31. m_pListBar->setMinimumHeight(this->height() - 2);
  32. m_pListBar->setMaximumHeight(this->height() - 2);
  33. m_pListBar->hide();
  34. connect(ui->listWidget->verticalScrollBar(), &QScrollBar::valueChanged, m_pListBar.data(), &QScrollBar::setValue);
  35. connect(ui->listWidget->verticalScrollBar(), &QScrollBar::rangeChanged, m_pListBar.data(), &QScrollBar::setRange);
  36. connect(m_pListBar.data(), &QScrollBar::valueChanged, ui->listWidget->verticalScrollBar(), &QScrollBar::setValue);
  37. }
  38. /* 设置QSS */
  39. setQSS();
  40. switch (type) {
  41. case MINUTE:
  42. case SECOND: {
  43. InitListWidget(60);
  44. break;
  45. }
  46. case HOUR: {
  47. InitListWidget(24);
  48. break;
  49. }
  50. default:
  51. break;
  52. }
  53. ui->listWidget->setCurrentRow(0);
  54. ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  55. auto pf = [this](QListWidgetItem* item){
  56. ui->listWidget->scrollToItem(item, QAbstractItemView::PositionAtTop); // 滚到最顶部
  57. };
  58. connect(ui->listWidget, &QListWidget::itemPressed, this, pf);
  59. connect(ui->listWidget, &QListWidget::itemPressed, this, &TimePartWidget::sigItemClicked);
  60. }
  61. TimePartWidget::~TimePartWidget()
  62. {
  63. delete ui;
  64. }
  65. void TimePartWidget::SetTime(const QDateTime &dt)
  66. {
  67. switch (m_type) {
  68. case HOUR:
  69. SelectListItem(dt.time().hour());
  70. break;
  71. case MINUTE:
  72. SelectListItem(dt.time().minute());
  73. break;
  74. case SECOND:
  75. SelectListItem(dt.time().second());
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. void TimePartWidget::SetMaxWidth(int w)
  82. {
  83. // 更新滚动条位置
  84. if (!m_pListBar.isNull()) {
  85. m_pListBar->move(w - 4, 1);
  86. }
  87. }
  88. void TimePartWidget::ScrollToSelect()
  89. {
  90. auto items = ui->listWidget->selectedItems();
  91. if (items.count() > 0) {
  92. ui->listWidget->scrollToItem(items.at(0), QAbstractItemView::PositionAtTop);
  93. }
  94. }
  95. /* 设置QSS */
  96. void TimePartWidget::setQSS()
  97. {
  98. QString appPath = QCoreApplication::applicationDirPath();
  99. QString qssPath;
  100. if(LHStyleManager::Instance()->GetCurSkinStyle() == SkinStyle::eWhiteStyle)
  101. {
  102. qssPath = appPath + "/white/SelectTime/timepartwidget.qss";
  103. }
  104. else if(LHStyleManager::Instance()->GetCurSkinStyle() == SkinStyle::eBlackStyle)
  105. {
  106. qssPath = appPath + "/black/SelectTime/timepartwidget.qss";
  107. }
  108. QFile file(qssPath);
  109. if(file.open(QFile::ReadOnly))
  110. {
  111. QString styleSheet = QLatin1String(file.readAll());
  112. setStyleSheet(styleSheet);
  113. file.close();
  114. }else
  115. {
  116. LH_WRITE_ERROR("open qss file failed: " + qssPath);
  117. }
  118. }
  119. void TimePartWidget::enterEvent(QEvent *event)
  120. {
  121. if (!m_pListBar.isNull()) {
  122. m_pListBar->show();
  123. }
  124. QWidget::enterEvent(event);
  125. }
  126. void TimePartWidget::leaveEvent(QEvent *event)
  127. {
  128. if (!m_pListBar.isNull()) {
  129. m_pListBar->hide();
  130. }
  131. QWidget::leaveEvent(event);
  132. }
  133. void TimePartWidget::InitListWidget(int total)
  134. {
  135. ui->listWidget->clear();
  136. for (int i = 0; i < total; ++i) {
  137. QListWidgetItem* pItem = new QListWidgetItem;
  138. if (nullptr == pItem) {
  139. continue;
  140. }
  141. pItem->setTextAlignment(Qt::AlignCenter);
  142. pItem->setText(QString("%1").arg(i, 2, 10, QLatin1Char('0')));
  143. pItem->setSizeHint(QSize(32, 32));
  144. ui->listWidget->addItem(pItem);
  145. }
  146. // 填充4个不可选空白项
  147. for (int i = 0; i < 4; ++i) {
  148. QListWidgetItem* pItem = new QListWidgetItem;
  149. if (nullptr == pItem) continue;
  150. pItem->setSizeHint(QSize(32, 32));
  151. pItem->setFlags(pItem->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
  152. ui->listWidget->addItem(pItem);
  153. }
  154. }
  155. void TimePartWidget::SelectListItem(int time)
  156. {
  157. for (int i = 0; i < ui->listWidget->count(); ++i) {
  158. QListWidgetItem* pItem = ui->listWidget->item(i);
  159. if (nullptr == pItem) continue;
  160. // 注意时间填充了0
  161. if (pItem->text() == QString("%1").arg(time, 2, 10, QLatin1Char('0'))) {
  162. //qInfo() << "SelectListItem: " << "滚动到最顶部, time: " << time;
  163. pItem->setSelected(true);
  164. ui->listWidget->scrollToItem(pItem, QAbstractItemView::PositionAtTop); // 滚到最顶部
  165. break;
  166. }
  167. }
  168. }