98 lines
3.4 KiB
C++
98 lines
3.4 KiB
C++
#include "panels/columns/DateRangeEdit.hpp"
|
||
|
||
#include <QCalendarWidget>
|
||
#include <QFrame>
|
||
#include <QHBoxLayout>
|
||
#include <QLabel>
|
||
#include <QPushButton>
|
||
#include <QToolButton>
|
||
#include <QVBoxLayout>
|
||
|
||
#include "Theme.hpp"
|
||
|
||
namespace geopro::app {
|
||
|
||
DateRangeEdit::DateRangeEdit(QWidget* parent) : QWidget(parent) {
|
||
auto* lay = new QHBoxLayout(this);
|
||
lay->setContentsMargins(0, 0, 0, 0);
|
||
lay->setSpacing(0);
|
||
btn_ = new QToolButton(this);
|
||
btn_->setToolButtonStyle(Qt::ToolButtonTextOnly);
|
||
btn_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||
btn_->setPopupMode(QToolButton::InstantPopup);
|
||
btn_->setToolTip(QStringLiteral("按采集时间筛选(起 ~ 止),可清空"));
|
||
connect(btn_, &QToolButton::clicked, this, &DateRangeEdit::openPopup);
|
||
lay->addWidget(btn_);
|
||
updateText();
|
||
}
|
||
|
||
void DateRangeEdit::openPopup() {
|
||
if (!popup_) {
|
||
popup_ = new QFrame(this, Qt::Popup);
|
||
popup_->setFrameShape(QFrame::StyledPanel);
|
||
applyTokenizedStyleSheet(
|
||
popup_, QStringLiteral("QFrame{background:{{bg/panel}};border:1px solid {{border/default}};"
|
||
"border-radius:8px;}"));
|
||
auto* v = new QVBoxLayout(popup_);
|
||
v->setContentsMargins(space::kSm, space::kSm, space::kSm, space::kSm);
|
||
|
||
auto* cals = new QHBoxLayout();
|
||
auto* fromCol = new QVBoxLayout();
|
||
fromCol->addWidget(new QLabel(QStringLiteral("起始"), popup_));
|
||
fromCal_ = new QCalendarWidget(popup_);
|
||
fromCol->addWidget(fromCal_);
|
||
cals->addLayout(fromCol);
|
||
auto* toCol = new QVBoxLayout();
|
||
toCol->addWidget(new QLabel(QStringLiteral("结束"), popup_));
|
||
toCal_ = new QCalendarWidget(popup_);
|
||
toCol->addWidget(toCal_);
|
||
cals->addLayout(toCol);
|
||
v->addLayout(cals);
|
||
|
||
auto* btns = new QHBoxLayout();
|
||
auto* clr = new QPushButton(QStringLiteral("清空"), popup_);
|
||
auto* ok = new QPushButton(QStringLiteral("确定"), popup_);
|
||
connect(clr, &QPushButton::clicked, this, &DateRangeEdit::clearAndClose);
|
||
connect(ok, &QPushButton::clicked, this, &DateRangeEdit::applyAndClose);
|
||
btns->addWidget(clr);
|
||
btns->addStretch(1);
|
||
btns->addWidget(ok);
|
||
v->addLayout(btns);
|
||
}
|
||
// 已有范围则定位到当前值,否则日历默认今天(不再 1752)。
|
||
if (from_.isValid()) fromCal_->setSelectedDate(from_);
|
||
if (to_.isValid()) toCal_->setSelectedDate(to_);
|
||
popup_->adjustSize();
|
||
popup_->move(mapToGlobal(QPoint(0, height())));
|
||
popup_->show();
|
||
}
|
||
|
||
void DateRangeEdit::applyAndClose() {
|
||
from_ = fromCal_->selectedDate();
|
||
to_ = toCal_->selectedDate();
|
||
if (from_.isValid() && to_.isValid() && from_ > to_) std::swap(from_, to_); // 起>止则交换
|
||
if (popup_) popup_->hide();
|
||
updateText();
|
||
emit rangeChanged();
|
||
}
|
||
|
||
void DateRangeEdit::clearAndClose() {
|
||
from_ = QDate();
|
||
to_ = QDate();
|
||
if (popup_) popup_->hide();
|
||
updateText();
|
||
emit rangeChanged();
|
||
}
|
||
|
||
void DateRangeEdit::updateText() {
|
||
if (!from_.isValid() && !to_.isValid()) {
|
||
btn_->setText(QStringLiteral("全部时间 ▾"));
|
||
return;
|
||
}
|
||
const QString f = from_.isValid() ? from_.toString(QStringLiteral("yyyy-MM-dd")) : QStringLiteral("不限");
|
||
const QString t = to_.isValid() ? to_.toString(QStringLiteral("yyyy-MM-dd")) : QStringLiteral("不限");
|
||
btn_->setText(QStringLiteral("%1 ~ %2 ▾").arg(f, t));
|
||
}
|
||
|
||
} // namespace geopro::app
|