TimeWidget.cpp
158 lines
| 3.8 KiB
| text/x-c
|
CppLexer
r134 | #include "TimeWidget/TimeWidget.h" | |||
#include "ui_TimeWidget.h" | ||||
Alexandre Leroux
|
r489 | #include <Common/DateUtils.h> | ||
r878 | #include <Common/MimeTypesDef.h> | |||
r1075 | #include <DragAndDrop/DragDropGuiController.h> | |||
r302 | #include <SqpApplication.h> | |||
#include <Time/TimeController.h> | ||||
r192 | ||||
r879 | #include <QDrag> | |||
r878 | #include <QDragEnterEvent> | |||
#include <QDropEvent> | ||||
#include <QMimeData> | ||||
r1347 | #include <QStyle> | |||
r878 | ||||
r879 | ||||
r1462 | struct TimeWidget::TimeWidgetPrivate | |||
{ | ||||
r879 | ||||
explicit TimeWidgetPrivate() {} | ||||
QPoint m_DragStartPosition; | ||||
}; | ||||
r1462 | TimeWidget::TimeWidget(QWidget* parent) | |||
: QWidget { parent } | ||||
, ui { new Ui::TimeWidget } | ||||
, impl { spimpl::make_unique_impl<TimeWidgetPrivate>() } | ||||
r134 | { | |||
ui->setupUi(this); | ||||
r192 | ||||
// Connection | ||||
connect(ui->startDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, | ||||
r1462 | &TimeWidget::onTimeUpdateRequested); | |||
r192 | ||||
connect(ui->endDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, | ||||
r1462 | &TimeWidget::onTimeUpdateRequested); | |||
r304 | ||||
// Initialisation | ||||
Alexandre Leroux
|
r489 | auto endDateTime = QDateTime::currentDateTimeUtc(); | ||
auto startDateTime = endDateTime.addSecs(-3600); // one hour before | ||||
ui->startDateTimeEdit->setDateTime(startDateTime); | ||||
ui->endDateTimeEdit->setDateTime(endDateTime); | ||||
r1462 | auto dateTime = DateTimeRange { DateUtils::secondsSinceEpoch(startDateTime), | |||
DateUtils::secondsSinceEpoch(endDateTime) }; | ||||
r304 | ||||
r1345 | sqpApp->timeController().setDateTimeRange(dateTime); | |||
r134 | } | |||
r302 | ||||
r134 | TimeWidget::~TimeWidget() | |||
{ | ||||
delete ui; | ||||
} | ||||
r192 | ||||
r1346 | void TimeWidget::setTimeRange(DateTimeRange time) | |||
r878 | { | |||
auto startDateTime = DateUtils::dateTime(time.m_TStart); | ||||
auto endDateTime = DateUtils::dateTime(time.m_TEnd); | ||||
ui->startDateTimeEdit->setDateTime(startDateTime); | ||||
ui->endDateTimeEdit->setDateTime(endDateTime); | ||||
} | ||||
r1346 | DateTimeRange TimeWidget::timeRange() const | |||
r192 | { | |||
r1462 | return DateTimeRange { DateUtils::secondsSinceEpoch(ui->startDateTimeEdit->dateTime()), | |||
DateUtils::secondsSinceEpoch(ui->endDateTimeEdit->dateTime()) }; | ||||
r879 | } | |||
r192 | ||||
r879 | void TimeWidget::onTimeUpdateRequested() | |||
{ | ||||
auto dateTime = timeRange(); | ||||
r1467 | emit timeUpdated(dateTime); | |||
r1351 | sqpApp->timeController().setDateTimeRange(dateTime); | |||
r192 | } | |||
r878 | ||||
r1462 | void TimeWidget::dragEnterEvent(QDragEnterEvent* event) | |||
r878 | { | |||
r1462 | if (event->mimeData()->hasFormat(MIME_TYPE_TIME_RANGE)) | |||
{ | ||||
r878 | event->acceptProposedAction(); | |||
setStyleSheet("QDateTimeEdit{background-color: #BBD5EE; border:2px solid #2A7FD4}"); | ||||
} | ||||
r1462 | else | |||
{ | ||||
r878 | event->ignore(); | |||
} | ||||
} | ||||
r1462 | void TimeWidget::dragLeaveEvent(QDragLeaveEvent* event) | |||
r878 | { | |||
r1462 | setStyleSheet(QString {}); | |||
r878 | } | |||
r1462 | void TimeWidget::dropEvent(QDropEvent* event) | |||
r878 | { | |||
r1462 | if (event->mimeData()->hasFormat(MIME_TYPE_TIME_RANGE)) | |||
{ | ||||
r878 | auto mimeData = event->mimeData()->data(MIME_TYPE_TIME_RANGE); | |||
auto timeRange = TimeController::timeRangeForMimeData(mimeData); | ||||
setTimeRange(timeRange); | ||||
} | ||||
r1462 | else | |||
{ | ||||
r878 | event->ignore(); | |||
} | ||||
r1462 | setStyleSheet(QString {}); | |||
r878 | } | |||
r879 | ||||
r1462 | void TimeWidget::mousePressEvent(QMouseEvent* event) | |||
r879 | { | |||
r1462 | if (event->button() == Qt::LeftButton) | |||
{ | ||||
r879 | impl->m_DragStartPosition = event->pos(); | |||
} | ||||
QWidget::mousePressEvent(event); | ||||
} | ||||
r1462 | void TimeWidget::mouseMoveEvent(QMouseEvent* event) | |||
r879 | { | |||
r1462 | if (!(event->buttons() & Qt::LeftButton)) | |||
{ | ||||
r879 | return; | |||
} | ||||
if ((event->pos() - impl->m_DragStartPosition).manhattanLength() | ||||
r1462 | < QApplication::startDragDistance()) | |||
{ | ||||
r879 | return; | |||
} | ||||
// Note: The management of the drag object is done by Qt | ||||
r1462 | auto drag = new QDrag { this }; | |||
r879 | ||||
auto mimeData = new QMimeData; | ||||
auto timeData = TimeController::mimeDataForTimeRange(timeRange()); | ||||
mimeData->setData(MIME_TYPE_TIME_RANGE, timeData); | ||||
drag->setMimeData(mimeData); | ||||
r1462 | auto pixmap = QPixmap { ":/icones/time.png" }; | |||
r935 | drag->setPixmap(pixmap.scaledToWidth(22)); | |||
r879 | ||||
r1075 | sqpApp->dragDropGuiController().resetDragAndDrop(); | |||
r879 | ||||
// Note: The exec() is blocking on windows but not on linux and macOS | ||||
drag->exec(Qt::MoveAction | Qt::CopyAction); | ||||
QWidget::mouseMoveEvent(event); | ||||
} | ||||