diff --git a/src/gui/activityWidget.cpp b/src/gui/activityWidget.cpp index f14e0ad..eb9bb84 100644 --- a/src/gui/activityWidget.cpp +++ b/src/gui/activityWidget.cpp @@ -18,6 +18,7 @@ ActivityWidget::ActivityWidget(QWidget* parent) : QWidget(parent), mUi(std::make mUi->cbTimeSheetParams->setItemDelegate(new TimeSheetParamsItemDelegate); connect(mUi->btStartStop, &QPushButton::clicked, this, &ActivityWidget::onBtStartStopClicked); + connect(mUi->cbTimeSheetParams, &QComboBox::currentIndexChanged, this, &ActivityWidget::onTimeSheetParamsIndexChanged); connect(&mSecondTimer, &QTimer::timeout, this, &ActivityWidget::onSecondTimeout); mSecondTimer.setInterval(std::chrono::seconds(1)); @@ -110,6 +111,7 @@ void ActivityWidget::onSessionCacheSynchronizeFinished() } mTimeSheetParamsModel.updateDataFromCache(mSession->cache()); + mUi->cbTimeSheetParams->setCurrentIndex(-1); } void ActivityWidget::onHistoryTimeSheetStartRequested(const TimeSheet& timeSheet) @@ -136,7 +138,11 @@ void ActivityWidget::onHistoryTimeSheetFillRequested(const TimeSheet& timeSheet) fillFromTimesheet(timeSheet); } -void ActivityWidget::fillFromTimesheet(const TimeSheet& timeSheet) {} +void ActivityWidget::fillFromTimesheet(const TimeSheet& timeSheet) +{ + auto index = mTimeSheetParamsModel.findIndex(timeSheet.project.id, timeSheet.activity.id); + mUi->cbTimeSheetParams->setCurrentIndex(index); +} void ActivityWidget::onBtStartStopClicked() { @@ -144,9 +150,10 @@ void ActivityWidget::onBtStartStopClicked() { stopCurrentTimeSheet(); } - else + else if (isCurrentTimeSheetParamsValid()) { TimeSheet timeSheet; + std::tie(timeSheet.project.id, timeSheet.activity.id) = getCurrentTimeSheetParams(); auto timeSheetResult = mSession->client()->startTimeSheet(timeSheet, mSession->timeSheetConfig().trackingMode); @@ -158,6 +165,11 @@ void ActivityWidget::onBtStartStopClicked() } } +void ActivityWidget::onTimeSheetParamsIndexChanged(int /*index*/) +{ + mUi->btStartStop->setEnabled(isCurrentTimeSheetParamsValid()); +} + void ActivityWidget::updateControls() { // Can be raised while clearing combobox on session changes @@ -219,3 +231,15 @@ void ActivityWidget::startPendingTimeSheet() connect(timeSheetResult, &KimaiApiBaseResult::error, [timeSheetResult]() { timeSheetResult->deleteLater(); }); } } + +bool ActivityWidget::isCurrentTimeSheetParamsValid() const +{ + return mUi->cbTimeSheetParams->currentData(TimeSheetParamsModel::ProjectIdRole).isValid() && + mUi->cbTimeSheetParams->currentData(TimeSheetParamsModel::ActivityIdRole).isValid(); +} + +std::pair ActivityWidget::getCurrentTimeSheetParams() const +{ + return {mUi->cbTimeSheetParams->currentData(TimeSheetParamsModel::ProjectIdRole).toInt(), + mUi->cbTimeSheetParams->currentData(TimeSheetParamsModel::ActivityIdRole).toInt()}; +} diff --git a/src/gui/activityWidget.h b/src/gui/activityWidget.h index ba662c6..bb24b13 100644 --- a/src/gui/activityWidget.h +++ b/src/gui/activityWidget.h @@ -26,6 +26,7 @@ class ActivityWidget : public QWidget private: void onBtStartStopClicked(); + void onTimeSheetParamsIndexChanged(int index); void onSecondTimeout(); @@ -42,6 +43,9 @@ class ActivityWidget : public QWidget void startPendingTimeSheet(); + bool isCurrentTimeSheetParamsValid() const; + std::pair getCurrentTimeSheetParams() const; + std::unique_ptr mUi; QTimer mSecondTimer; std::shared_ptr mSession; diff --git a/src/gui/timeSheetParamsItemDelegate.cpp b/src/gui/timeSheetParamsItemDelegate.cpp index 602604e..09b36a8 100644 --- a/src/gui/timeSheetParamsItemDelegate.cpp +++ b/src/gui/timeSheetParamsItemDelegate.cpp @@ -47,11 +47,6 @@ void TimeSheetParamsItemDelegate::paint(QPainter* painter, const QStyleOptionVie // Activity auto activityRect = QRect(textX + widthThird, secondLineY, 2 * widthThird, textH); qApp->style()->drawItemText(painter, activityRect, Qt::AlignLeft, option.palette, isSelected, index.data(TimeSheetParamsModel::ActivityName).toString()); - - if (option.showDecorationSelected) - { - qDebug() << index.data().toString(); - } } QSize TimeSheetParamsItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const diff --git a/src/models/timeSheetParamsModel.cpp b/src/models/timeSheetParamsModel.cpp index b8b151a..b9306b6 100644 --- a/src/models/timeSheetParamsModel.cpp +++ b/src/models/timeSheetParamsModel.cpp @@ -12,7 +12,7 @@ void TimeSheetParamsModel::updateDataFromCache(const KimaiCache& cache) { for (const auto& activity : cache.activities(project.id)) { - m_timeSheetParams.emplace_back(TimeSheetParams{ + mTimeSheetParams.emplace_back(TimeSheetParams{ .customer = customer.name, .project = project.name, .activity = activity.name, .projectId = project.id, .activityId = activity.id}); } } @@ -21,15 +21,26 @@ void TimeSheetParamsModel::updateDataFromCache(const KimaiCache& cache) endResetModel(); } -QVariant TimeSheetParamsModel::data(const QModelIndex& index, int role) const +int TimeSheetParamsModel::findIndex(int projectId, int activityId) const { + auto it = std::find_if(mTimeSheetParams.begin(), mTimeSheetParams.end(), [projectId, activityId](const auto& timeSheetParams) { + return timeSheetParams.projectId == projectId && timeSheetParams.activityId == activityId; + }); + if (it == mTimeSheetParams.end()) + { + return -1; + } + return std::distance(mTimeSheetParams.begin(), it); +} - if (!index.isValid() || (index.row() > m_timeSheetParams.size())) +QVariant TimeSheetParamsModel::data(const QModelIndex& index, int role) const +{ + if (!index.isValid() || (index.row() > mTimeSheetParams.size())) { return {}; } - const auto& params = m_timeSheetParams.at(index.row()); + const auto& params = mTimeSheetParams.at(index.row()); switch (role) { case Qt::DisplayRole: @@ -57,5 +68,5 @@ QVariant TimeSheetParamsModel::data(const QModelIndex& index, int role) const int TimeSheetParamsModel::rowCount(const QModelIndex& /*parent*/) const { - return static_cast(m_timeSheetParams.size()); + return static_cast(mTimeSheetParams.size()); } diff --git a/src/models/timeSheetParamsModel.h b/src/models/timeSheetParamsModel.h index 47525db..a671f9c 100644 --- a/src/models/timeSheetParamsModel.h +++ b/src/models/timeSheetParamsModel.h @@ -20,6 +20,9 @@ class TimeSheetParamsModel : public QAbstractListModel void updateDataFromCache(const KimaiCache& cache); + // Returns -1 if not found + int findIndex(int projectId, int activityId) const; + QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; int rowCount(const QModelIndex& parent = QModelIndex()) const override; @@ -32,7 +35,7 @@ class TimeSheetParamsModel : public QAbstractListModel int projectId; int activityId; }; - std::vector m_timeSheetParams; + std::vector mTimeSheetParams; }; } // namespace kemai