-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.cpp
More file actions
138 lines (117 loc) · 3.38 KB
/
widget.cpp
File metadata and controls
138 lines (117 loc) · 3.38 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
#include "widget.h"
#include "ui_widget.h"
#include <QtGlobal>
#include <QDateTime>
#include <QMessageBox>
#include <QRegExp>
#include <QIcon>
#include <QDate>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
ui->lineEdit->setClearButtonEnabled(true);
#endif // QT_VERSION
parser = new MsnParser(this);
checkAndSetDateRange();
// MSN RegExp
QRegExp regExp("^[A-Za-z0-9]{4}[A-Za-z]{2}[A-Za-z0-9]{5}$");
validator = new QRegExpValidator(regExp, this);
ui->lineEdit->setValidator(validator);
// Window Settings
adjustSize();
setFixedSize(size());
setWindowIcon(QIcon(":/MSN-parser.png"));
}
void Widget::checkAndSetDateRange()
{
int currentYear = QDateTime::currentDateTime().toString("yyyy").toInt();
if (currentYear >= 1976 && currentYear <= 1999) {
ui->radioButton1->setChecked(true);
} else if (currentYear >= 2000 && currentYear <= 2023) {
ui->radioButton2->setChecked(true);
} else if (currentYear >= 2024 && currentYear <= 2047) {
ui->radioButton3->setChecked(true);
} else {
QMessageBox::warning(this, tr("Wrong Date!"), tr("Please check Date and Time on your computer.\n"
"Please set Date Range manually."));
ui->radioButton1->setChecked(true);
}
}
void Widget::on_radioButton1_toggled(bool toggle)
{
if (toggle) {
parser->setDateRange(y_76_99);
on_lineEdit_textChanged(ui->lineEdit->text());
}
}
void Widget::on_radioButton2_toggled(bool toggle)
{
if (toggle) {
parser->setDateRange(y_00_23);
on_lineEdit_textChanged(ui->lineEdit->text());
}
}
void Widget::on_radioButton3_toggled(bool toggle)
{
if (toggle) {
parser->setDateRange(y_24_47);
on_lineEdit_textChanged(ui->lineEdit->text());
}
}
void Widget::on_lineEdit_textChanged(const QString &text)
{
ui->lineEdit->setText(text.toUpper());
if (text.length() > 5) {
ui->labelDate->setText("<strong>" + parser->parseMsn(text) + "</strong>");
} else {
ui->labelDate->setText("<strong>" + tr("Need more characters") + "</strong>");
}
}
Widget::~Widget()
{
// Will be deleted on destroying parent object.
// delete parser;
// delete validator;
delete ui;
}
// ----------------------------------------------------- //
MsnParser::MsnParser(QObject *parent) :
QObject(parent)
{
dateRange = y_76_99;
fillTables();
}
void MsnParser::setDateRange(DateRange value)
{
dateRange = value;
}
void MsnParser::fillTables()
{
int i = 0;
for (char ch = 'A'; ch <= 'Z'; ++ch) {
if (ch == 'I' || ch == 'O') {
continue;
} else {
table.insert(ch, i);
++i;
}
}
}
QString MsnParser::getHalf(bool qHalf) const
{
return (qHalf) ? tr("First half of ") : tr("Last half of ");
}
QString MsnParser::parseMsn(const QString &msn) const
{
QString answer = QString::number(table[msn[5]] / 2 + 1) + "-"; // Month
answer += QString::number(table[msn[4]] + dateRange); // Year
return QDate::fromString(answer, "M-yyyy") // Date Format
.toString(getHalf(table[msn[5]] % 2 == 0) + "MMMM yyyy"); // For "January" instead "1"
}
MsnParser::~MsnParser()
{
/* Empty destructor */
}