-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98795aa
commit 6de20d3
Showing
9 changed files
with
192 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "timeSheetParamsItemDelegate.h" | ||
|
||
#include <QApplication> | ||
#include <QPainter> | ||
#include <QStyle> | ||
|
||
#include "models/timeSheetParamsModel.h" | ||
|
||
using namespace kemai; | ||
|
||
static const auto gIndicatorWidth = 2; | ||
static const auto gTextSpacing = 2; | ||
static const auto gTextLeftOffset = gIndicatorWidth + gTextSpacing; | ||
static const auto gPadding = 4; | ||
|
||
// gIndicatorWidth + gPadding (1 x blue rect + 1 x empty) | ||
// | | | ||
// || < gPadding > | ||
// || Customer name | ||
// || < gTextSpacing > | ||
// || Project name (on gProjectMaxWidth) < gTextSpacing > Activity name | ||
// || < gPadding > | ||
|
||
void TimeSheetParamsItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const | ||
{ | ||
// Indicate selection | ||
auto isSelected = (option.state & QStyle::State_Selected) != 0; | ||
if (isSelected) | ||
{ | ||
painter->fillRect(option.rect, option.palette.mid()); | ||
painter->fillRect(QRect(option.rect.topLeft(), QSize(gIndicatorWidth, option.rect.height())), option.palette.highlight()); | ||
} | ||
|
||
const auto textX = option.rect.left() + gTextLeftOffset; | ||
const auto textH = option.fontMetrics.height(); | ||
const auto widthThird = option.rect.width() / 3; | ||
const auto secondLineY = option.rect.top() + gPadding + option.fontMetrics.height() + gTextSpacing; | ||
|
||
// Customer | ||
auto headerRect = QRect(textX, option.rect.top() + gPadding, option.rect.width(), textH); | ||
qApp->style()->drawItemText(painter, headerRect, Qt::AlignLeft, option.palette, isSelected, index.data(TimeSheetParamsModel::CustomerName).toString()); | ||
|
||
// Project | ||
auto projectRect = QRect(textX, secondLineY, widthThird, textH); | ||
qApp->style()->drawItemText(painter, projectRect, Qt::AlignLeft, option.palette, isSelected, index.data(TimeSheetParamsModel::ProjectName).toString()); | ||
|
||
// 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 | ||
{ | ||
return {option.rect.width(), option.fontMetrics.height() * 2 + gTextSpacing + 2 * gPadding}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <QStyledItemDelegate> | ||
|
||
namespace kemai { | ||
|
||
class TimeSheetParamsItemDelegate : public QStyledItemDelegate | ||
{ | ||
public: | ||
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override; | ||
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override; | ||
}; | ||
|
||
} // namespace kemai |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "timeSheetParamsModel.h" | ||
|
||
using namespace kemai; | ||
|
||
void TimeSheetParamsModel::updateDataFromCache(const KimaiCache& cache) | ||
{ | ||
beginResetModel(); | ||
|
||
for (const auto& customer : cache.customers()) | ||
{ | ||
for (const auto& project : cache.projects(customer.id)) | ||
{ | ||
for (const auto& activity : cache.activities(project.id)) | ||
{ | ||
m_timeSheetParams.emplace_back(TimeSheetParams{ | ||
.customer = customer.name, .project = project.name, .activity = activity.name, .projectId = project.id, .activityId = activity.id}); | ||
} | ||
} | ||
} | ||
|
||
endResetModel(); | ||
} | ||
|
||
QVariant TimeSheetParamsModel::data(const QModelIndex& index, int role) const | ||
{ | ||
|
||
if (!index.isValid() || (index.row() > m_timeSheetParams.size())) | ||
{ | ||
return {}; | ||
} | ||
|
||
const auto& params = m_timeSheetParams.at(index.row()); | ||
switch (role) | ||
{ | ||
case Qt::DisplayRole: | ||
return QString("%1 / %2 / %3").arg(params.customer, params.project, params.activity); | ||
|
||
case CustomerName: | ||
return params.customer; | ||
|
||
case ProjectName: | ||
return params.project; | ||
|
||
case ActivityName: | ||
return params.activity; | ||
|
||
case ProjectIdRole: | ||
return params.projectId; | ||
|
||
case ActivityIdRole: | ||
return params.activityId; | ||
|
||
default: | ||
return {}; | ||
} | ||
} | ||
|
||
int TimeSheetParamsModel::rowCount(const QModelIndex& /*parent*/) const | ||
{ | ||
return static_cast<int>(m_timeSheetParams.size()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include <QAbstractListModel> | ||
|
||
#include <client/kimaiCache.h> | ||
|
||
namespace kemai { | ||
|
||
class TimeSheetParamsModel : public QAbstractListModel | ||
{ | ||
public: | ||
enum TimeSheetParamsModelRole | ||
{ | ||
CustomerName = Qt::UserRole + 1, | ||
ProjectName, | ||
ActivityName, | ||
ProjectIdRole, | ||
ActivityIdRole | ||
}; | ||
|
||
void updateDataFromCache(const KimaiCache& cache); | ||
|
||
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; | ||
int rowCount(const QModelIndex& parent = QModelIndex()) const override; | ||
|
||
private: | ||
struct TimeSheetParams | ||
{ | ||
QString customer; | ||
QString project; | ||
QString activity; | ||
int projectId; | ||
int activityId; | ||
}; | ||
std::vector<TimeSheetParams> m_timeSheetParams; | ||
}; | ||
|
||
} // namespace kemai |