Skip to content

Commit

Permalink
WIP Timesheet start/stop
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrePTJ committed Jun 9, 2024
1 parent 6de20d3 commit 16f1d6d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
28 changes: 26 additions & 2 deletions src/gui/activityWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -110,6 +111,7 @@ void ActivityWidget::onSessionCacheSynchronizeFinished()
}

mTimeSheetParamsModel.updateDataFromCache(mSession->cache());
mUi->cbTimeSheetParams->setCurrentIndex(-1);
}

void ActivityWidget::onHistoryTimeSheetStartRequested(const TimeSheet& timeSheet)
Expand All @@ -136,17 +138,22 @@ 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()
{
if (mSession->hasCurrentTimeSheet())
{
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);

Expand All @@ -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
Expand Down Expand Up @@ -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<int, int> ActivityWidget::getCurrentTimeSheetParams() const
{
return {mUi->cbTimeSheetParams->currentData(TimeSheetParamsModel::ProjectIdRole).toInt(),
mUi->cbTimeSheetParams->currentData(TimeSheetParamsModel::ActivityIdRole).toInt()};
}
4 changes: 4 additions & 0 deletions src/gui/activityWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ActivityWidget : public QWidget

private:
void onBtStartStopClicked();
void onTimeSheetParamsIndexChanged(int index);

void onSecondTimeout();

Expand All @@ -42,6 +43,9 @@ class ActivityWidget : public QWidget

void startPendingTimeSheet();

bool isCurrentTimeSheetParamsValid() const;
std::pair<int, int> getCurrentTimeSheetParams() const;

std::unique_ptr<Ui::ActivityWidget> mUi;
QTimer mSecondTimer;
std::shared_ptr<KemaiSession> mSession;
Expand Down
5 changes: 0 additions & 5 deletions src/gui/timeSheetParamsItemDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 16 additions & 5 deletions src/models/timeSheetParamsModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}
Expand All @@ -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:
Expand Down Expand Up @@ -57,5 +68,5 @@ QVariant TimeSheetParamsModel::data(const QModelIndex& index, int role) const

int TimeSheetParamsModel::rowCount(const QModelIndex& /*parent*/) const
{
return static_cast<int>(m_timeSheetParams.size());
return static_cast<int>(mTimeSheetParams.size());
}
5 changes: 4 additions & 1 deletion src/models/timeSheetParamsModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,7 +35,7 @@ class TimeSheetParamsModel : public QAbstractListModel
int projectId;
int activityId;
};
std::vector<TimeSheetParams> m_timeSheetParams;
std::vector<TimeSheetParams> mTimeSheetParams;
};

} // namespace kemai

0 comments on commit 16f1d6d

Please sign in to comment.