-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtexttemplateparser.cpp
More file actions
107 lines (89 loc) · 4.9 KB
/
texttemplateparser.cpp
File metadata and controls
107 lines (89 loc) · 4.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
#include "texttemplateparser.h"
#include <QTextDocument>
#include <QTextCursor>
#include <QDateTime>
#include <QDebug>
TextTemplateParser::TextTemplateParser( QObject * parent )
: QObject( parent )
{
}
QString TextTemplateParser::get( const QString & text )
{
QTextDocument document( text );
QTextCursor textCursor;
QTextDocument::FindFlags flags = QTextDocument::FindCaseSensitively;
QRegExp expression( "%[^ %]+%" );
QDateTime dateTime = QDateTime::currentDateTime();
do
{
textCursor = document.find( expression, textCursor, flags );
if ( textCursor.hasSelection() )
{
QString selected = textCursor.selectedText();
QString replaceText;
// Удаление символов %, с начала и с конца строки
selected = selected.remove( 0, 1 );
selected = selected.remove( selected.size() - 1, 1 );
if ( selected == "td" )
replaceText = dateTime.toString( Qt::TextDate );
else if ( selected == "iso" )
replaceText = dateTime.toString( Qt::ISODate );
else if ( selected == "short" )
replaceText = dateTime.toString( Qt::SystemLocaleShortDate );
else if ( selected == "long" )
replaceText = dateTime.toString( Qt::SystemLocaleLongDate );
else
replaceText = dateTime.toString( selected );
if ( !replaceText.isEmpty() )
textCursor.insertText( replaceText );
}
} while ( !textCursor.isNull() );
return document.toPlainText();
}
#include <QTranslator>
QStringList TextTemplateParser::description()
{
QStringList list;
list << QString( "d" ) + "=" + QTranslator::tr( "the day as number without a leading zero (1 to 31)" )
<< QString( "dd" ) + "=" + QTranslator::tr( "the day as number with a leading zero (01 to 31)" )
<< QString( "ddd" ) + "=" + QTranslator::tr( "the abbreviated localized day name (e.g. 'Mon')" )
<< QString( "dddd" ) + "=" + QTranslator::tr( "the long localized day name (e.g. 'Monday')" )
<< QString( "M" ) + "=" + QTranslator::tr( "the month as number without a leading zero (1-12)" )
<< QString( "MM" ) + "=" + QTranslator::tr( "the month as number with a leading zero (01-12)" )
<< QString( "MMM" ) + "=" + QTranslator::tr( "the abbreviated localized month name (e.g. 'Jan')" )
<< QString( "MMMM" ) + "=" + QTranslator::tr( "the long localized month name (e.g. 'January')" )
<< QString( "yy" ) + "=" + QTranslator::tr( "the year as two digit number (00-99)" )
<< QString( "yyyy" ) + "=" + QTranslator::tr( "the year as four digit number" );
list << QString( "h" ) + "=" + QTranslator::tr( "the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)" )
<< QString( "hh" ) + "=" + QTranslator::tr( "the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)" )
<< QString( "m" ) + "=" + QTranslator::tr( "the minute without a leading zero (0 to 59)" )
<< QString( "mm" ) + "=" + QTranslator::tr( "the minute with a leading zero (00 to 59)" )
<< QString( "s" ) + "=" + QTranslator::tr( "the second without a leading zero (0 to 59)" )
<< QString( "ss" ) + "=" + QTranslator::tr( "the second with a leading zero (00 to 59)" )
<< QString( "z" ) + "=" + QTranslator::tr( "the milliseconds without leading zeroes (0 to 999)" )
<< QString( "zzz" ) + "=" + QTranslator::tr( "the milliseconds with leading zeroes (000 to 999)" )
<< QString( "AP" ) + "=" + QTranslator::tr( "use AM/PM display. AP will be replaced by either \"AM\" or \"PM\"" )
<< QString( "ap" ) + "=" + QTranslator::tr( "use am/pm display. ap will be replaced by either \"am\" or \"pm\"" );
list << QString( "td" ) + "=" + QTranslator::tr( "the format, which includes the day and month name,"
"the day number in the month, and the year in full."
"The day and month names will be short, localized names" )
<< QString( "iso" ) + "=" + QTranslator::tr( "ISO 8601 extended format" )
<< QString( "short" ) + "=" + QTranslator::tr( "the short format used by the operating system" )
<< QString( "long" ) + "=" + QTranslator::tr( "the long format used by the operating system" );
return list;
}
QString TextTemplateParser::descriptionToHtmlTable()
{
QString table = "<table>";
foreach ( QString row, description() )
{
QStringList columns = row.split( "=" );
QString value = columns.at( 0 );
QString desc = columns.at( 1 );
table += "<tr>";
table += QString( "<td><b>%1</b></td><td>%2</td>" ).arg( value ).arg( desc );
table += "</tr>";
}
table += "</table>";
return table;
}