-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
373 lines (314 loc) · 11.9 KB
/
main.cpp
File metadata and controls
373 lines (314 loc) · 11.9 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <GLFW/glfw3.h>
#include <nlohmann/json.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <map>
#include <filesystem>
// 使用简短别名
using json = nlohmann::json;
using namespace std::chrono;
// --- 数据结构 ---
struct Task {
std::string name;
system_clock::time_point start_time;
};
struct LogEntry {
std::string task_name;
std::string duration_str;
};
// --- 全局状态 ---
std::vector<Task> active_tasks;
std::map<std::string, std::vector<LogEntry>> all_logs;
const std::string DATA_FILE = "todo_log.json";
// UI 状态
char input_buffer[256] = "";
bool show_history = false;
int cal_year = 0;
int cal_month = 0; // 1-12
std::string selected_date_key = "";
// --- 辅助函数 ---
std::string get_current_time_str() {
auto now = system_clock::to_time_t(system_clock::now());
std::tm tm_info;
#ifdef _WIN32
localtime_s(&tm_info, &now);
#else
localtime_r(&now, &tm_info);
#endif
char buf[16];
std::strftime(buf, sizeof(buf), "%H:%M:%S", &tm_info);
return std::string(buf);
}
std::string format_date(int y, int m, int d) {
char buf[16];
snprintf(buf, sizeof(buf), "%04d-%02d-%02d", y, m, d);
return std::string(buf);
}
std::string get_today_str() {
auto now = system_clock::to_time_t(system_clock::now());
std::tm tm_info;
#ifdef _WIN32
localtime_s(&tm_info, &now);
#else
localtime_r(&now, &tm_info);
#endif
return format_date(tm_info.tm_year + 1900, tm_info.tm_mon + 1, tm_info.tm_mday);
}
std::string format_duration(system_clock::time_point start) {
auto end = system_clock::now();
// 强制转换为 int 解决 C4244 警告
long long seconds = duration_cast<std::chrono::seconds>(end - start).count();
int h = (int)(seconds / 3600);
int m = (int)((seconds % 3600) / 60);
int s = (int)(seconds % 60);
if (h == 0 && m == 0) return std::to_string(s) + "s";
std::string res = "";
if (h > 0) res += std::to_string(h) + "h ";
if (m > 0) res += std::to_string(m) + "m";
return res;
}
// --- 文件 I/O ---
void load_logs() {
if (!std::filesystem::exists(DATA_FILE)) return;
std::ifstream f(DATA_FILE);
json j;
try {
f >> j;
for (auto& [date, entries] : j.items()) {
for (auto& entry : entries) {
all_logs[date].push_back({entry["task"], entry["duration"]});
}
}
} catch (...) {}
}
void save_logs() {
json j;
for (const auto& [date, entries] : all_logs) {
for (const auto& entry : entries) {
j[date].push_back({
{"task", entry.task_name},
{"duration", entry.duration_str}
});
}
}
std::ofstream f(DATA_FILE);
f << j.dump(4);
}
// --- 业务逻辑 ---
void finish_task(int index) {
if (index < 0 || index >= active_tasks.size()) return;
Task& t = active_tasks[index];
std::string dur = format_duration(t.start_time);
std::string today = get_today_str();
all_logs[today].push_back({t.name, dur});
save_logs();
active_tasks.erase(active_tasks.begin() + index);
}
// --- 日历逻辑 ---
void draw_calendar() {
ImGui::BeginGroup();
if (ImGui::Button("<")) {
cal_month--;
if (cal_month < 1) { cal_month = 12; cal_year--; }
}
ImGui::SameLine();
ImGui::Text("%04d - %02d", cal_year, cal_month);
ImGui::SameLine();
if (ImGui::Button(">")) {
cal_month++;
if (cal_month > 12) { cal_month = 1; cal_year++; }
}
ImGui::EndGroup();
std::tm time_in = {0};
time_in.tm_year = cal_year - 1900;
time_in.tm_mon = cal_month - 1;
time_in.tm_mday = 1;
std::mktime(&time_in);
int first_weekday = time_in.tm_wday;
int days_in_month = 31;
if (cal_month == 4 || cal_month == 6 || cal_month == 9 || cal_month == 11) days_in_month = 30;
if (cal_month == 2) {
bool is_leap = (cal_year % 4 == 0 && cal_year % 100 != 0) || (cal_year % 400 == 0);
days_in_month = is_leap ? 29 : 28;
}
if (ImGui::BeginTable("Calendar", 7, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) {
const char* days[] = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
for (auto d : days) ImGui::TableSetupColumn(d);
ImGui::TableHeadersRow();
ImGui::TableNextRow();
int column = 0;
for (int i = 0; i < first_weekday; i++) ImGui::TableSetColumnIndex(column++);
for (int day = 1; day <= days_in_month; day++) {
if (column > 6) {
ImGui::TableNextRow();
column = 0;
}
ImGui::TableSetColumnIndex(column++);
std::string date_key = format_date(cal_year, cal_month, day);
bool is_selected = (date_key == selected_date_key);
bool has_data = all_logs.find(date_key) != all_logs.end();
if (is_selected) ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.2f, 0.6f, 1.0f, 1.0f));
else if (has_data) ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.3f, 0.3f, 0.3f, 1.0f));
if (ImGui::Button((std::to_string(day) + "##btn").c_str(), ImVec2(-FLT_MIN, 0))) {
selected_date_key = date_key;
}
if (is_selected || has_data) ImGui::PopStyleColor();
}
ImGui::EndTable();
}
}
// --- 主程序 ---
int main(int, char**) {
if (!glfwInit()) return 1;
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
// 初始窗口设大一点,适应放大后的字体
GLFWwindow* window = glfwCreateWindow(600, 800, "C++ Todo", NULL, NULL);
if (window == NULL) return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
// --- 字体与样式配置 (修正版) ---
// 1. 加载字体
float fontSize = 24.0f; // 字体大小
ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\windows\\fonts\\msyh.ttc", fontSize, NULL, io.Fonts->GetGlyphRangesChineseFull());
// 字体加载失败回退
if (font == NULL) {
io.Fonts->AddFontDefault();
io.FontGlobalScale = 2.0f;
}
// 2. 统一设置样式 (只声明一次 style 变量)
ImGui::StyleColorsDark();
ImGuiStyle& style = ImGui::GetStyle();
// 在这里统一设置所有样式属性
style.ScaleAllSizes(1.5f); // UI 整体放大 1.5 倍
style.WindowRounding = 8.0f; // 圆角
style.FrameRounding = 5.0f; // 输入框/按钮圆角
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
// 数据加载
load_logs();
auto now = system_clock::to_time_t(system_clock::now());
std::tm tm_now;
#ifdef _WIN32
localtime_s(&tm_now, &now);
#else
localtime_r(&now, &tm_now);
#endif
cal_year = tm_now.tm_year + 1900;
cal_month = tm_now.tm_mon + 1;
selected_date_key = format_date(cal_year, cal_month, tm_now.tm_mday);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
static bool last_history_state = false;
if (show_history != last_history_state) {
// 调整窗口大小时也考虑缩放比例
if (show_history) glfwSetWindowSize(window, 1200, 800);
else glfwSetWindowSize(window, 600, 800);
last_history_state = show_history;
}
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->WorkPos);
ImGui::SetNextWindowSize(viewport->WorkSize);
ImGui::Begin("Main", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings);
if (show_history) ImGui::BeginChild("LeftPanel", ImVec2(580, 0), true); // 调整左侧宽度
else ImGui::BeginChild("LeftPanel", ImVec2(0, 0), false);
std::string time_str = get_current_time_str();
float text_width = ImGui::CalcTextSize(time_str.c_str()).x * 3.0f;
ImGui::SetCursorPosX((ImGui::GetContentRegionAvail().x - text_width) * 0.5f);
ImGui::SetWindowFontScale(3.0f);
ImGui::Text("%s", time_str.c_str());
ImGui::SetWindowFontScale(1.0f);
ImGui::Separator();
ImGui::TextDisabled("ACTIVE TASKS");
ImGui::Spacing();
ImGui::BeginChild("TaskList", ImVec2(0, -90), true);
for (int i = 0; i < active_tasks.size(); ++i) {
ImGui::PushID(i);
ImGui::Text("%s", active_tasks[i].name.c_str());
ImGui::SameLine();
float space = ImGui::GetContentRegionAvail().x;
ImGui::SameLine(space - 40);
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.2f, 0.7f, 0.3f, 1.0f));
if (ImGui::Button("Done", ImVec2(40, 30))) {
finish_task(i);
i--;
}
ImGui::PopStyleColor();
ImGui::PopID();
}
ImGui::EndChild();
ImGui::PushItemWidth(-60);
if (ImGui::InputText("##taskinput", input_buffer, IM_ARRAYSIZE(input_buffer), ImGuiInputTextFlags_EnterReturnsTrue)) {
if (strlen(input_buffer) > 0) {
active_tasks.push_back({std::string(input_buffer), system_clock::now()});
input_buffer[0] = '\0';
ImGui::SetKeyboardFocusHere(-1);
}
}
ImGui::PopItemWidth();
ImGui::SameLine();
if (ImGui::Button("+", ImVec2(50, 0))) {
if (strlen(input_buffer) > 0) {
active_tasks.push_back({std::string(input_buffer), system_clock::now()});
input_buffer[0] = '\0';
}
}
ImGui::Spacing();
if (ImGui::Button(show_history ? "Hide History << " : "Show History >> ", ImVec2(-FLT_MIN, 40))) {
show_history = !show_history;
}
ImGui::EndChild();
if (show_history) {
ImGui::SameLine();
ImGui::BeginChild("RightPanel", ImVec2(0, 0), true);
ImGui::TextDisabled("CALENDAR");
draw_calendar();
ImGui::Separator();
ImGui::Text("Logs for %s", selected_date_key.c_str());
ImGui::BeginChild("LogList", ImVec2(0, 0), false);
if (all_logs.find(selected_date_key) != all_logs.end()) {
for (const auto& entry : all_logs[selected_date_key]) {
ImGui::Text("%s", entry.task_name.c_str());
ImGui::SameLine();
ImGui::SameLine(ImGui::GetContentRegionAvail().x - 80);
ImGui::TextColored(ImVec4(0.2f, 1.0f, 0.2f, 1.0f), "%s", entry.duration_str.c_str());
ImGui::Separator();
}
} else {
ImGui::TextDisabled("No records found.");
}
ImGui::EndChild();
ImGui::EndChild();
}
ImGui::End();
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}