Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
openedlist.cpp
Go to the documentation of this file.
1
// Copyright (C) 2021 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4
#
include
"openedlist.h"
5
6
#
include
"atom.h"
7
8
#
include
<
QtCore
/
qregularexpression
.
h
>
9
10
QT_BEGIN_NAMESPACE
11
12
static
const
char
roman
[] =
"m\2d\5c\2l\5x\2v\5i"
;
13
14
OpenedList
::
OpenedList
(
ListStyle
style) : sty(style), ini(1), nex(0) {}
15
16
OpenedList
::
OpenedList
(
const
Location
&location,
const
QString &hint) : sty(
Bullet
), ini(1)
17
{
18
static
const
QRegularExpression hintSyntax(
"^(\\W*)([0-9]+|[A-Z]+|[a-z]+)(\\W*)$"
);
19
20
auto
match = hintSyntax.match(hint);
21
if
(match.hasMatch()) {
22
bool
ok;
23
int
asNumeric = hint.toInt(&ok);
24
int
asRoman = fromRoman(match.captured(2));
25
int
asAlpha = fromAlpha(match.captured(2));
26
27
if
(ok) {
28
sty =
Numeric
;
29
ini = asNumeric;
30
}
else
if
(asRoman > 0 && asRoman != 100 && asRoman != 500) {
31
sty = (hint == hint.toLower()) ?
LowerRoman
:
UpperRoman
;
32
ini = asRoman;
33
}
else
{
34
sty = (hint == hint.toLower()) ?
LowerAlpha
:
UpperAlpha
;
35
ini = asAlpha;
36
}
37
pref = match.captured(1);
38
suff = match.captured(3);
39
}
else
if
(!hint.isEmpty()) {
40
location.warning(QStringLiteral(
"Unrecognized list style '%1'"
).arg(hint));
41
}
42
nex = ini - 1;
43
}
44
45
QString
OpenedList
::
styleString
()
const
46
{
47
switch
(
style
(
)
) {
48
case
Bullet
:
49
default
:
50
return
ATOM_LIST_BULLET
;
51
case
Tag
:
52
return
ATOM_LIST_TAG
;
53
case
Value
:
54
return
ATOM_LIST_VALUE
;
55
case
Numeric
:
56
return
ATOM_LIST_NUMERIC
;
57
case
UpperAlpha
:
58
return
ATOM_LIST_UPPERALPHA
;
59
case
LowerAlpha
:
60
return
ATOM_LIST_LOWERALPHA
;
61
case
UpperRoman
:
62
return
ATOM_LIST_UPPERROMAN
;
63
case
LowerRoman
:
64
return
ATOM_LIST_LOWERROMAN
;
65
}
66
}
67
68
QString
OpenedList
::
numberString
()
const
69
{
70
return
QString::number(number());
71
/*
72
switch ( style() ) {
73
case Numeric:
74
return QString::number( number() );
75
case UpperAlpha:
76
return toAlpha( number() ).toUpper();
77
case LowerAlpha:
78
return toAlpha( number() );
79
case UpperRoman:
80
return toRoman( number() ).toUpper();
81
case LowerRoman:
82
return toRoman( number() );
83
case Bullet:
84
default:
85
return "*";
86
}*/
87
}
88
89
int
OpenedList
::fromAlpha(
const
QString &str)
90
{
91
int
n = 0;
92
int
u;
93
94
for
(
const
QChar &character : str) {
95
u = character.toLower().unicode();
96
if
(u >=
'a'
&& u <=
'z'
) {
97
n *= 26;
98
n += u -
'a'
+ 1;
99
}
else
{
100
return
0;
101
}
102
}
103
return
n;
104
}
105
106
QString
OpenedList
::toRoman(
int
n)
107
{
108
/*
109
See p. 30 of Donald E. Knuth's "TeX: The Program".
110
*/
111
QString str;
112
int
j = 0;
113
int
k;
114
int
u;
115
int
v = 1000;
116
117
for
(;;) {
118
while
(n >= v) {
119
str +=
roman
[j];
120
n -= v;
121
}
122
123
if
(n <= 0)
124
break
;
125
126
k = j + 2;
127
u = v /
roman
[k - 1];
128
if
(
roman
[k - 1] == 2) {
129
k += 2;
130
u /= 5;
131
}
132
if
(n + u >= v) {
133
str +=
roman
[k];
134
n += u;
135
}
else
{
136
j += 2;
137
v /=
roman
[j - 1];
138
}
139
}
140
return
str;
141
}
142
143
int
OpenedList
::fromRoman(
const
QString &str)
144
{
145
int
n = 0;
146
int
j;
147
int
u;
148
int
v = 0;
149
150
for
(
const
QChar &character : str) {
151
j = 0;
152
u = 1000;
153
while
(roman[j] !=
'i'
&& roman[j] != character.toLower()) {
154
j += 2;
155
u /= roman[j - 1];
156
}
157
if
(u < v) {
158
n -= u;
159
}
else
{
160
n += u;
161
}
162
v = u;
163
}
164
165
if
(str.toLower() == toRoman(n)) {
166
return
n;
167
}
else
{
168
return
0;
169
}
170
}
171
172
QT_END_NAMESPACE
ATOM_LIST_BULLET
#define ATOM_LIST_BULLET
Definition
atom.h:209
ATOM_LIST_LOWERALPHA
#define ATOM_LIST_LOWERALPHA
Definition
atom.h:212
ATOM_LIST_UPPERALPHA
#define ATOM_LIST_UPPERALPHA
Definition
atom.h:215
ATOM_LIST_TAG
#define ATOM_LIST_TAG
Definition
atom.h:210
ATOM_LIST_LOWERROMAN
#define ATOM_LIST_LOWERROMAN
Definition
atom.h:213
ATOM_LIST_NUMERIC
#define ATOM_LIST_NUMERIC
Definition
atom.h:214
ATOM_LIST_VALUE
#define ATOM_LIST_VALUE
Definition
atom.h:211
ATOM_LIST_UPPERROMAN
#define ATOM_LIST_UPPERROMAN
Definition
atom.h:216
Location
The Location class provides a way to mark a location in a file.
Definition
location.h:20
OpenedList
Definition
openedlist.h:14
OpenedList::ListStyle
ListStyle
Definition
openedlist.h:16
OpenedList::UpperAlpha
@ UpperAlpha
Definition
openedlist.h:16
OpenedList::Numeric
@ Numeric
Definition
openedlist.h:16
OpenedList::Tag
@ Tag
Definition
openedlist.h:16
OpenedList::UpperRoman
@ UpperRoman
Definition
openedlist.h:16
OpenedList::Value
@ Value
Definition
openedlist.h:16
OpenedList::Bullet
@ Bullet
Definition
openedlist.h:16
OpenedList::LowerRoman
@ LowerRoman
Definition
openedlist.h:16
OpenedList::LowerAlpha
@ LowerAlpha
Definition
openedlist.h:16
OpenedList::numberString
QString numberString() const
Definition
openedlist.cpp:68
OpenedList::style
ListStyle style() const
Definition
openedlist.h:25
OpenedList::OpenedList
OpenedList(const Location &location, const QString &hint)
Definition
openedlist.cpp:16
OpenedList::styleString
QString styleString() const
Definition
openedlist.cpp:45
OpenedList::OpenedList
OpenedList(ListStyle style)
Definition
openedlist.cpp:14
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
roman
static QT_BEGIN_NAMESPACE const char roman[]
Definition
openedlist.cpp:12
qttools
src
qdoc
qdoc
src
qdoc
openedlist.cpp
Generated on
for Qt by
1.18.0