| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 | 
							- #include "timepartwidget.h"
 
- #include "ui_timepartwidget.h"
 
- #include <QListWidgetItem>
 
- #include <QScrollBar>
 
- #include <QDateTime>
 
- #include <QMouseEvent>
 
- #include <QDebug>
 
- #include <QFile>
 
- #include "mytimedelegate.h"
 
- // #include "lhstylemanager.h"
 
- // #include "LHQLogApi.h"
 
- #include "UIStyleManager.h"
 
- #include "spdlog/spdlog.h"
 
- TimePartWidget::TimePartWidget(QWidget *parent) :
 
-     QWidget(parent),
 
-     ui(new Ui::TimePartWidget)
 
- {
 
-     ui->setupUi(this);
 
- }
 
- TimePartWidget::TimePartWidget(TimePartWidget::emSection type, QWidget* parent) :
 
-     TimePartWidget(parent)
 
- {
 
-     m_type = type;
 
-     // 设置代理
 
-     m_delegate.reset(new MyTimeDelegate(this));
 
-     if (nullptr != m_delegate.data()) {
 
-         ui->listWidget->setItemDelegate(m_delegate.data());
 
-     }
 
-     // 设置滚动条
 
-     m_pListBar.reset(new QScrollBar(Qt::Orientation::Vertical, ui->listWidget));
 
-     if (nullptr != m_pListBar.data()) {
 
-         m_pListBar->move(width() - 4, 1); // 宽度4,与右侧距离2
 
-         m_pListBar->setMinimumHeight(this->height() - 2);
 
-         m_pListBar->setMaximumHeight(this->height() - 2);
 
-         m_pListBar->hide();
 
-         connect(ui->listWidget->verticalScrollBar(), &QScrollBar::valueChanged, m_pListBar.data(), &QScrollBar::setValue);
 
-         connect(ui->listWidget->verticalScrollBar(), &QScrollBar::rangeChanged, m_pListBar.data(), &QScrollBar::setRange);
 
-         connect(m_pListBar.data(), &QScrollBar::valueChanged, ui->listWidget->verticalScrollBar(), &QScrollBar::setValue);
 
-     }
 
-     /* 设置QSS */
 
-     setQSS();
 
-     switch (type) {
 
-     case MINUTE:
 
-     case SECOND: {
 
-         InitListWidget(60);
 
-         break;
 
-     }
 
-     case HOUR: {
 
-         InitListWidget(24);
 
-         break;
 
-     }
 
-     default:
 
-         break;
 
-     }
 
-     ui->listWidget->setCurrentRow(0);
 
-     ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
-     auto pf = [this](QListWidgetItem* item){
 
-         ui->listWidget->scrollToItem(item, QAbstractItemView::PositionAtTop); // 滚到最顶部
 
-     };
 
-     connect(ui->listWidget, &QListWidget::itemPressed, this, pf);
 
-     connect(ui->listWidget, &QListWidget::itemPressed, this, &TimePartWidget::sigItemClicked);
 
- }
 
- TimePartWidget::~TimePartWidget()
 
- {
 
-     delete ui;
 
- }
 
- void TimePartWidget::SetTime(const QDateTime &dt)
 
- {
 
-     switch (m_type) {
 
-     case HOUR:
 
-         SelectListItem(dt.time().hour());
 
-         break;
 
-     case MINUTE:
 
-         SelectListItem(dt.time().minute());
 
-         break;
 
-     case SECOND:
 
-         SelectListItem(dt.time().second());
 
-         break;
 
-     default:
 
-         break;
 
-     }
 
- }
 
- void TimePartWidget::SetMaxWidth(int w)
 
- {
 
-     // 更新滚动条位置
 
-     if (!m_pListBar.isNull()) {
 
-         m_pListBar->move(w - 4, 1);
 
-     }
 
- }
 
- void TimePartWidget::ScrollToSelect()
 
- {
 
-     auto items = ui->listWidget->selectedItems();
 
-     if (items.count() > 0) {
 
-         ui->listWidget->scrollToItem(items.at(0), QAbstractItemView::PositionAtTop);
 
-     }
 
- }
 
- /* 设置QSS */
 
- void TimePartWidget::setQSS()
 
- {
 
-     QString qssPath;
 
-     if(EPUIStyle.getUIStyle() == enum_UIStyle::UI_Light)
 
-     {
 
-         qssPath = QString(":/Res/light/timepartwidget.qss");
 
-     }
 
-     else if(EPUIStyle.getUIStyle() == enum_UIStyle::UI_Dark)
 
-     {
 
-         qssPath = QString(":/Res/dark/timepartwidget.qss");
 
-     }
 
-     QFile file(qssPath);
 
-     if(file.open(QFile::ReadOnly))
 
-     {
 
-         QString styleSheet = QLatin1String(file.readAll());
 
-         setStyleSheet(styleSheet);
 
-         file.close();
 
-     }
 
-     else 
 
-     {
 
-         SPDLOG_ERROR("open qss file failed: {}", qssPath.toStdString());
 
-     }
 
- }
 
- void TimePartWidget::enterEvent(QEvent *event)
 
- {
 
-     if (!m_pListBar.isNull()) {
 
-         m_pListBar->show();
 
-     }
 
-     QWidget::enterEvent(event);
 
- }
 
- void TimePartWidget::leaveEvent(QEvent *event)
 
- {
 
-     if (!m_pListBar.isNull()) {
 
-         m_pListBar->hide();
 
-     }
 
-     QWidget::leaveEvent(event);
 
- }
 
- void TimePartWidget::InitListWidget(int total)
 
- {
 
-     ui->listWidget->clear();
 
-     for (int i = 0; i < total; ++i) {
 
-         QListWidgetItem* pItem = new QListWidgetItem;
 
-         if (nullptr == pItem) {
 
-             continue;
 
-         }
 
-         pItem->setTextAlignment(Qt::AlignCenter);
 
-         pItem->setText(QString("%1").arg(i, 2, 10, QLatin1Char('0')));
 
-         pItem->setSizeHint(QSize(32, 32));
 
-         ui->listWidget->addItem(pItem);
 
-     }
 
-     // 填充4个不可选空白项
 
-     for (int i = 0; i < 4; ++i) {
 
-         QListWidgetItem* pItem = new QListWidgetItem;
 
-         if (nullptr == pItem) continue;
 
-         pItem->setSizeHint(QSize(32, 32));
 
-         pItem->setFlags(pItem->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
 
-         ui->listWidget->addItem(pItem);
 
-     }
 
- }
 
- void TimePartWidget::SelectListItem(int time)
 
- {
 
-     for (int i = 0; i < ui->listWidget->count(); ++i) {
 
-         QListWidgetItem* pItem = ui->listWidget->item(i);
 
-         if (nullptr == pItem) continue;
 
-         // 注意时间填充了0
 
-         if (pItem->text() == QString("%1").arg(time, 2, 10, QLatin1Char('0'))) {
 
-             //qInfo() << "SelectListItem: " << "滚动到最顶部, time: " << time;
 
-             pItem->setSelected(true);
 
-             ui->listWidget->scrollToItem(pItem, QAbstractItemView::PositionAtTop); // 滚到最顶部
 
-             break;
 
-         }
 
-     }
 
- }
 
 
  |