-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalendarWidget.cpp
More file actions
203 lines (173 loc) · 6.5 KB
/
Copy pathCalendarWidget.cpp
File metadata and controls
203 lines (173 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "CalendarWidget.h"
#include <QToolTip>
#include <QMap>
#include <QPainter>
#include <QModelIndex>
#include <QDebug>
#include <QMouseEvent>
#include <QContextMenuEvent>
#include <QHeaderView>
#include <QMenu>
#include <QAction>
CalendarWidget::CalendarWidget(QWidget *parent)
: QCalendarWidget(parent)
{
setMouseTracking(true);
installEventFilter(this);
QTableView *tableView = findChild<QTableView*>();
if (tableView) {
tableView->setMouseTracking(true);
tableView->installEventFilter(this);
}
}
void CalendarWidget::setBikeDates(const QVector<QPair<QDateTime, QStringList>> &data)
{
m_bikeDates.clear();
m_originalData = data;
for (const auto &entry : data) {
m_bikeDates[entry.first.date()] += entry.second;
}
updateCells();
}
void CalendarWidget::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{
QCalendarWidget::paintCell(painter, rect, date);
if (m_bikeDates.contains(date)) {
painter->save();
painter->setCompositionMode(QPainter::CompositionMode_Multiply);
QColor highlight = QBrush(this->palette().highlight()).color();
highlight.setAlpha(128);
painter->fillRect(rect.adjusted(1, 1, -1, -1), highlight);
painter->restore();
}
}
bool CalendarWidget::eventFilter(QObject *watched, QEvent *event)
{
QTableView *tableView = findChild<QTableView*>();
if (watched == tableView) {
if (event->type() == QEvent::MouseMove) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
handleMouseMoveOnTable(mouseEvent, tableView);
} else if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if (mouseEvent->button() == Qt::RightButton) {
handleContextMenuOnTable(mouseEvent, tableView);
return true;
}
} else if (event->type() == QEvent::ContextMenu) {
QContextMenuEvent *contextEvent = static_cast<QContextMenuEvent*>(event);
QMouseEvent fakeMouseEvent(QEvent::MouseButtonPress,
contextEvent->pos(),
Qt::RightButton,
Qt::RightButton,
contextEvent->modifiers());
handleContextMenuOnTable(&fakeMouseEvent, tableView);
return true;
}
}
return QCalendarWidget::eventFilter(watched, event);
}
void CalendarWidget::handleMouseMoveOnTable(QMouseEvent *event, QTableView *tableView)
{
QModelIndex index = tableView->indexAt(event->pos());
if (index.isValid()) {
QDate date = getDateFromModel(tableView, index);
if (date.isValid() && m_bikeDates.contains(date)) {
QString tooltipText = formatTooltipText(m_bikeDates[date]);
QPoint globalPos = tableView->mapToGlobal(event->pos());
QToolTip::showText(globalPos, tooltipText, this);
} else {
QToolTip::hideText();
}
} else {
QToolTip::hideText();
}
}
void CalendarWidget::handleContextMenuOnTable(QMouseEvent *event, QTableView *tableView)
{
QModelIndex index = tableView->indexAt(event->pos());
if (index.isValid()) {
QDate date = getDateFromModel(tableView, index);
if (date.isValid() && m_bikeDates.contains(date)) {
m_contextMenuDate = date;
QMenu contextMenu(this);
// find all entries for this date
QVector<QPair<QDateTime, QString>> entriesForDate;
for (const auto &entry : m_originalData) {
if (entry.first.date() == date) {
for (const QString &bikeName : entry.second) {
entriesForDate.append(qMakePair(entry.first, bikeName));
}
}
}
// create actions for each entry
for (int i = entriesForDate.size() - 1; i >= 0 ; i--) {
const auto &entry = entriesForDate[i];
QString actionText = QString("%1 (%2)").arg(entry.second, entry.first.time().toString("hh:mm"));
QAction *action = contextMenu.addAction(actionText);
action->setData(i); // Speichere den Index
connect(action, &QAction::triggered, this, &CalendarWidget::onBikeActionTriggered);
}
// save entries for later use in slot
m_contextMenuEntries = entriesForDate;
// show context menu
QPoint globalPos = tableView->mapToGlobal(event->pos());
contextMenu.exec(globalPos);
}
}
}
void CalendarWidget::onBikeActionTriggered()
{
QAction *action = qobject_cast<QAction*>(sender());
if (action) {
int index = action->data().toInt();
if (index >= 0 && index < m_contextMenuEntries.size()) {
const auto &entry = m_contextMenuEntries[index];
emit bikeEntryClicked(entry.second, entry.first); // bikeName, dateTime
}
}
}
QString CalendarWidget::formatTooltipText(const QStringList &names) const
{
QMap<QString, int> counts;
for (const QString &name : names) {
counts[name]++;
}
QStringList formattedList;
for (auto it = counts.begin(); it != counts.end(); ++it) {
const QString &name = it.key();
int count = it.value();
if (count == 1) {
formattedList << name;
} else {
formattedList << QString("%1 (%2x)").arg(name).arg(count);
}
}
return formattedList.join(", ");
}
QDate CalendarWidget::getDateFromModel(QTableView *tableView, const QModelIndex &index) const
{
QAbstractItemModel *model = tableView->model();
if (!model) {
return QDate();
}
QVariant data = model->data(index, Qt::DisplayRole);
QString dayStr = data.toString();
bool ok;
int day = dayStr.toInt(&ok);
if (ok && day >= 1 && day <= 31) {
int year = yearShown();
int month = monthShown();
int row = index.row();
if (row <= 1 && day > 15) {
QDate prevMonth = QDate(year, month, 1).addMonths(-1);
return QDate(prevMonth.year(), prevMonth.month(), day);
} else if (row >= 4 && day <= 15) {
QDate nextMonth = QDate(year, month, 1).addMonths(1);
return QDate(nextMonth.year(), nextMonth.month(), day);
} else {
return QDate(year, month, day);
}
}
return QDate();
}