-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatisticslabel.cpp
More file actions
102 lines (80 loc) · 2.63 KB
/
statisticslabel.cpp
File metadata and controls
102 lines (80 loc) · 2.63 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
#include "statisticslabel.h"
StatisticsLabel::StatisticsLabel(Settings *settings, TextAnalyzer *statistics, QWidget *parent)
: QLabel(parent)
{
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
this->setAlignment(Qt::AlignRight);
this->setTextFormat(Qt::TextFormat::MarkdownText);
this->setContentsMargins(20, 5, 20, 5);
pagecount = 0;
readtime = 0;
difficulty = 100;
this->settings = settings;
this->statistics = statistics;
}
void StatisticsLabel::statisticsChanged(const bool selection) {
TextStatistics data;
if(selection) {
data = statistics->selectionText();
} else {
data = statistics->wholeText();
}
QString showntext = "";
// wordcount
if(settings->getShowWordcount()) {
if(data.getWordCount() == 1) {
showntext += tr("1 word");
} else {
showntext += QString::number(data.getWordCount()) + tr(" words");
}
}
// pagecount
if(settings->getPagecountFromCharacters()) {
pagecount = data.getCharacterCount() / settings->getCharactersPerPage();
} else {
pagecount = data.getWordCount() / settings->getWordsPerPage();
}
if(settings->getShowPagecount()) {
if(showntext.size() > 0) {
showntext += " - ";
}
if(pagecount == 1) {
showntext += tr("1 page");
} else {
showntext += QString::number(pagecount) + tr(" pages");
}
}
// readtime
readtime = data.getWordCount() / settings->getWordsPerMinute();
if(settings->getShowReadtime()) {
if(showntext.size() > 0) {
showntext += " - ";
}
showntext += QString::number(readtime / 60) + tr("h ")
+ QString::number(readtime % 60) + tr("m");
}
// difficulty
double avg_sentence_length = data.getWordCount() / data.getSentenceCount();
double avg_word_length = data.getSyllableCount();
if(data.getWordCount() > 0) {
avg_word_length /= data.getWordCount();
}
if(settings->getLocale().startsWith("de")) {
difficulty = static_cast<int>(180.0 - avg_sentence_length - (58.5 * avg_word_length));
} else {
difficulty = static_cast<int>(206.835 - (1.015 * avg_sentence_length) - (84.6 * avg_word_length));
}
if(difficulty > 100) {
difficulty = 100;
}
if(difficulty < 0) {
difficulty = 0;
}
if(settings->getShowDifficulty()) {
if(showntext.size() > 0) {
showntext += " - ";
}
showntext += tr(" level: ") + QString::number(difficulty);
}
this->setText(showntext);
}