-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
325 lines (271 loc) · 10.3 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "optionsdialog.h"
#include "exportdialog.h"
#include <QSizePolicy>
#include <Qt>
#include <QStringList>
#include <QTreeWidgetItem>
#include <QMenuBar>
#include <QIcon>
#include <QKeySequence>
#include <QMessageBox>
#include <QApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
date_valid = hour_valid = min_valid = title_valid = activity_valid = false;
create_menu();
create_layout();
create_top_layout();
create_bottom_layout();
set_add_button_enabled();
}
void MainWindow::create_menu()
{
file_menu = menuBar()->addMenu(tr("&File"));
exit_act = new QAction(QIcon::fromTheme("application-exit"), tr("&Exit"), this);
exit_act->setShortcut(QKeySequence::Quit);
connect(exit_act, &QAction::triggered, this, &MainWindow::app_exit);
file_menu->addAction(exit_act);
tools_menu = menuBar()->addMenu(tr("&Tools"));
export_act = new QAction(tr("&Export entries..."), this);
export_act->setShortcut(QKeySequence(tr("Ctrl+E")));
connect(export_act, &QAction::triggered, this, &MainWindow::show_export);
tools_menu->addAction(export_act);
QAction *sep = new QAction(this);
sep->setSeparator(true);
tools_menu->addAction(sep);
options_act = new QAction(tr("&Options..."), this);
options_act->setShortcut(QKeySequence(tr("Ctrl+O")));
connect(options_act, &QAction::triggered, this, &MainWindow::show_options);
tools_menu->addAction(options_act);
help_menu = menuBar()->addMenu(tr("&Help"));
about_act = new QAction(QIcon::fromTheme("help-about"), tr("&About WDiary..."), this);
about_act->setShortcut(QKeySequence(tr("Ctrl+A")));
connect(about_act, &QAction::triggered, this, &MainWindow::show_about);
help_menu->addAction(about_act);
}
void MainWindow::create_layout()
{
main_widget = new QWidget;
main_layout = new QVBoxLayout;
top_layout = new QHBoxLayout;
new_layout = new QGridLayout;
main_layout->addLayout(top_layout);
main_widget->setLayout(main_layout);
setCentralWidget(main_widget);
}
void MainWindow::create_top_layout()
{
new_group = new QGroupBox(tr("Add new diary entry:"));
new_group->setLayout(new_layout);
top_layout->addWidget(new_group);
date_label = new QLabel(tr("Date:"));
date_label->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
title_label = new QLabel(tr("Title:"));
title_label->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
hour_label = new QLabel(tr("Hour:"));
hour_label->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
minute_label = new QLabel(tr("Minute:"));
minute_label->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
activity_label = new QLabel(tr("Activity:"));
activity_label->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
text_label = new QLabel(tr("Description of work performed:"));
date_edit = new QDateEdit;
date_edit->setDate(QDate::currentDate());
date_valid = true;
date_edit->setToolTip(tr("Enter date for diary entry, or click date on calendar."));
connect(date_edit, &QDateEdit::dateChanged, this, &MainWindow::is_date_valid);
activity_combo = new QComboBox;
activity_combo->setEditable(true);
activity_combo->addItem(tr("Documentation"));
activity_combo->addItem(tr("Project leading"));
activity_combo->addItem(tr("Programming"));
activity_combo->addItem(tr("Graphics"));
activity_combo->setCurrentText("");
connect(activity_combo, &QComboBox::currentTextChanged, this, &MainWindow::is_activity_valid);
title_edit = new QComboBox;
title_edit->setEditable(true);
title_edit->setToolTip(tr("Enter title for diary entry."));
connect(title_edit, &QComboBox::currentTextChanged, this, &MainWindow::is_title_valid);
hour_combo = new QComboBox;
hour_combo->setToolTip(tr("Used hours for job entry."));
hour_combo->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(hour_combo, SIGNAL(activated(int)), this, SLOT(is_hour_valid(int)));
minute_combo = new QComboBox;
minute_combo->setToolTip(tr("Used minutes for job entry."));
minute_combo->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(minute_combo, SIGNAL(activated(int)), this, SLOT(is_min_valid(int)));
for(int i=0; i <= 24; i++){
hour_combo->addItem(QString::number(i));
}
for(int i=0; i < 60; i+=5){
minute_combo->addItem(QString::number(i));
}
text_edit = new QTextEdit;
add_button = new QPushButton(tr("&Add new entry"));
add_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
add_button->setDisabled(true);
add_button->setToolTip(tr("Fill in information before clicking button."));
new_layout->setColumnStretch(0, -1);
new_layout->setColumnStretch(1, -1);
new_layout->setColumnStretch(2, -1);
new_layout->addWidget(date_label, 0, 0);
new_layout->addWidget(date_edit, 0, 1, 1, -1);
new_layout->addWidget(hour_label, 1, 0);
new_layout->addWidget(hour_combo, 1, 1);
new_layout->addWidget(minute_label, 1, 2);
new_layout->addWidget(minute_combo, 1, 3);
new_layout->addWidget(title_label, 2, 0);
new_layout->addWidget(title_edit, 2, 1, 1, -1);
new_layout->addWidget(activity_label, 3, 0);
new_layout->addWidget(activity_combo, 3, 1, 1, -1);
new_layout->addWidget(text_label, 4, 0, 1, -1);
new_layout->addWidget(text_edit, 5, 0, 1, -1);
new_layout->addWidget(add_button, 6, 4, 1, 1);
new_layout->setAlignment(add_button, Qt::AlignRight);
calendar = new QCalendarWidget;
calendar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
calendar_layout = new QVBoxLayout;
calendar_layout->setAlignment(Qt::AlignTop);
calendar_layout->setAlignment(calendar, Qt::AlignTop);
calendar_layout->addWidget(calendar);
top_layout->addLayout(calendar_layout);
connect(calendar, &QCalendarWidget::clicked,
date_edit, &QDateEdit::setDate);
}
void MainWindow::create_bottom_layout()
{
view_group = new QGroupBox(tr("Current diary entries:"));
main_layout->addWidget(view_group);
bottom_layout = new QVBoxLayout;
view_group->setLayout(bottom_layout);
radio_button_layout = new QHBoxLayout;
bottom_layout->addLayout(radio_button_layout);
today_radio = new QRadioButton(tr("&Todays entries"), view_group);
today_radio->setChecked(true);
today_radio->setToolTip(tr("Choose date in calendar."));
radio_button_layout->addWidget(today_radio);
radio_button_layout->setAlignment(today_radio, Qt::AlignHCenter);
week_radio = new QRadioButton(tr("This &weeks entries"), view_group);
week_radio->setToolTip(tr("Click on date in calender, that week will be shown."));
radio_button_layout->addWidget(week_radio);
radio_button_layout->setAlignment(week_radio, Qt::AlignHCenter);
month_radio = new QRadioButton(tr("This &months entries"), view_group);
month_radio->setToolTip(tr("Current month in calendar will be shown."));
radio_button_layout->addWidget(month_radio);
radio_button_layout->setAlignment(month_radio, Qt::AlignHCenter);
create_tree_view();
}
void MainWindow::create_tree_view()
{
QStringList headers;
headers << tr("Date") << tr("Time") << tr("Title") << tr("Description");
entry_view = new TreeWidgetMenu;
entry_view->setHeaderLabels(headers);
bottom_layout->addWidget(entry_view);
QTreeWidgetItem *item1 = new QTreeWidgetItem(entry_view);
item1->setText(0, "2013");
QTreeWidgetItem *item2 = new QTreeWidgetItem(item1);
item2->setText(0, "October");
item2->setText(1, "146:30");
QTreeWidgetItem *item3 = new QTreeWidgetItem(item2);
item3->setText(0, "Week 43");
item3->setText(1, "42:00");
QTreeWidgetItem *item4 = new QTreeWidgetItem(item3);
item4->setText(0, "8 October");
item4->setText(1, "2:00");
item4->setText(2, "418689 Analys nätverk");
item4->setText(3, "Sökt av nät med analysatorn.\nBandbredd bra lite trasiga paket");
}
void MainWindow::show_about()
{
QString msg = tr("<center><b>WDiary Version 1.0.0</b><br/><br/>");
msg += tr("A work diary to keep track of what you have done when.<br/>");
msg += tr("Copyright (C) 2013 Marcus Pedersén<br/></center>");
msg += tr("This program is free software: you can redistribute it and/or modify<br/>");
msg += tr("it under the terms of the GNU General Public License as published by<br/>");
msg += tr("the Free Software Foundation, either version 3 of the License, or<br/>");
msg += tr("(at your option) any later version.<br/><br/>");
msg += tr("This program is distributed in the hope that it will be useful,<br/>");
msg += tr("but WITHOUT ANY WARRANTY; without even the implied warranty of<br/>");
msg += tr("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br/>");
msg += tr("GNU General Public License for more details.<br/><br/>");
msg += tr("You should have received a copy of the GNU General Public License<br/>");
msg += tr("along with this program. If not, see http://www.gnu.org/licenses/<br/><br/>");
msg += tr("Contact: [email protected]<br/>");
msg += tr("Repro: https://github.com/xmarcux/wdiary");
QMessageBox::about(this, tr("About WDiary"), msg);
}
void MainWindow::app_exit()
{
QApplication::quit();
}
void MainWindow::show_options()
{
OptionsDialog *opt = new OptionsDialog(this);
opt->show();
}
void MainWindow::show_export()
{
ExportDialog *exp = new ExportDialog(this);
exp->show();
}
void MainWindow::is_date_valid(const QDate &d)
{
if(d.isValid())
date_valid = true;
else
date_valid = false;
set_add_button_enabled();
}
void MainWindow::is_hour_valid(int index)
{
if(index)
hour_valid = true;
else
hour_valid = false;
set_add_button_enabled();
}
void MainWindow::is_min_valid(int index)
{
if(index)
min_valid = true;
else
min_valid = false;
set_add_button_enabled();
}
void MainWindow::is_title_valid(const QString &text)
{
bool valid = false;
for(auto pos = text.begin(); pos != text.end(); pos++){
if(pos->isPrint() && !pos->isSpace()){
valid = true;
break;
}
}
title_valid = valid;
set_add_button_enabled();
}
void MainWindow::is_activity_valid(const QString &text)
{
bool valid = false;
for(auto pos = text.begin(); pos != text.end(); pos++){
if(pos->isPrint() && !pos->isSpace()){
valid = true;
break;
}
}
activity_valid = valid;
set_add_button_enabled();
}
void MainWindow::set_add_button_enabled()
{
if(date_valid && (hour_valid || min_valid) && title_valid && activity_valid)
add_button->setEnabled(true);
else
add_button->setEnabled(false);
}
MainWindow::~MainWindow()
{
}