QCalendarWidget: hold SectionToken by-value
There really is no point in allocating a struct containing a pointer and an int on the heap. Allocate by-value instead. Since QList would be inefficient then, switch to QVector. A pointer to the current token was replaced by its index. That saves looking it up on every toPreviousToken()/ toNextToken(). This saves 816 bytes of text size on optimized AMD64 / GCC builds, even though QVector expands to more code than QList. Change-Id: I030ee3f6acabe76168a518495bd4462711519e54 Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
This commit is contained in:
parent
8e43f25edd
commit
c5239ec12d
@ -423,6 +423,20 @@ QString QCalendarYearValidator::text(const QDate &date, int repeat) const
|
|||||||
|
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
|
|
||||||
|
struct SectionToken {
|
||||||
|
Q_DECL_CONSTEXPR SectionToken() : validator(Q_NULLPTR), repeat(0) {}
|
||||||
|
Q_DECL_CONSTEXPR SectionToken(QCalendarDateSectionValidator *v, int rep)
|
||||||
|
: validator(v), repeat(rep) {}
|
||||||
|
|
||||||
|
QCalendarDateSectionValidator *validator;
|
||||||
|
int repeat;
|
||||||
|
|
||||||
|
Q_DECL_CONSTEXPR bool isNull() const { return !validator; }
|
||||||
|
};
|
||||||
|
} // unnamed namespace
|
||||||
|
Q_DECLARE_TYPEINFO(SectionToken, Q_PRIMITIVE_TYPE);
|
||||||
|
namespace {
|
||||||
|
|
||||||
class QCalendarDateValidator
|
class QCalendarDateValidator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -438,13 +452,6 @@ public:
|
|||||||
void setLocale(const QLocale &locale);
|
void setLocale(const QLocale &locale);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
struct SectionToken {
|
|
||||||
SectionToken(QCalendarDateSectionValidator *val, int rep) : validator(val), repeat(rep) {}
|
|
||||||
QCalendarDateSectionValidator *validator;
|
|
||||||
int repeat;
|
|
||||||
};
|
|
||||||
|
|
||||||
void toNextToken();
|
void toNextToken();
|
||||||
void toPreviousToken();
|
void toPreviousToken();
|
||||||
void applyToDate();
|
void applyToDate();
|
||||||
@ -453,12 +460,12 @@ private:
|
|||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
QStringList m_separators;
|
QStringList m_separators;
|
||||||
QList<SectionToken *> m_tokens;
|
QVector<SectionToken> m_tokens;
|
||||||
QCalendarYearValidator m_yearValidator;
|
QCalendarYearValidator m_yearValidator;
|
||||||
QCalendarMonthValidator m_monthValidator;
|
QCalendarMonthValidator m_monthValidator;
|
||||||
QCalendarDayValidator m_dayValidator;
|
QCalendarDayValidator m_dayValidator;
|
||||||
|
|
||||||
SectionToken *m_currentToken;
|
int m_currentToken;
|
||||||
|
|
||||||
QDate m_initialDate;
|
QDate m_initialDate;
|
||||||
QDate m_currentDate;
|
QDate m_currentDate;
|
||||||
@ -467,7 +474,7 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
QCalendarDateValidator::QCalendarDateValidator()
|
QCalendarDateValidator::QCalendarDateValidator()
|
||||||
: m_currentToken(Q_NULLPTR),
|
: m_currentToken(-1),
|
||||||
m_initialDate(QDate::currentDate()),
|
m_initialDate(QDate::currentDate()),
|
||||||
m_currentDate(m_initialDate),
|
m_currentDate(m_initialDate),
|
||||||
m_lastSectionMove(QCalendarDateSectionValidator::ThisSection)
|
m_lastSectionMove(QCalendarDateSectionValidator::ThisSection)
|
||||||
@ -510,17 +517,16 @@ void QCalendarDateValidator::setInitialDate(const QDate &date)
|
|||||||
QString QCalendarDateValidator::currentText() const
|
QString QCalendarDateValidator::currentText() const
|
||||||
{
|
{
|
||||||
QString str;
|
QString str;
|
||||||
QStringListIterator itSep(m_separators);
|
const int numSeps = m_separators.size();
|
||||||
QListIterator<SectionToken *> itTok(m_tokens);
|
const int numTokens = m_tokens.size();
|
||||||
while (itSep.hasNext()) {
|
for (int i = 0; i < numSeps; ++i) {
|
||||||
str += itSep.next();
|
str += m_separators.at(i);
|
||||||
if (itTok.hasNext()) {
|
if (i < numTokens) {
|
||||||
SectionToken *token = itTok.next();
|
const SectionToken &token = m_tokens.at(i);
|
||||||
QCalendarDateSectionValidator *validator = token->validator;
|
if (i == m_currentToken)
|
||||||
if (m_currentToken == token)
|
str += token.validator->text();
|
||||||
str += validator->text();
|
|
||||||
else
|
else
|
||||||
str += validator->text(m_currentDate, token->repeat);
|
str += token.validator->text(m_currentDate, token.repeat);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
@ -528,14 +534,10 @@ QString QCalendarDateValidator::currentText() const
|
|||||||
|
|
||||||
void QCalendarDateValidator::clear()
|
void QCalendarDateValidator::clear()
|
||||||
{
|
{
|
||||||
QListIterator<SectionToken *> it(m_tokens);
|
|
||||||
while (it.hasNext())
|
|
||||||
delete it.next();
|
|
||||||
|
|
||||||
m_tokens.clear();
|
m_tokens.clear();
|
||||||
m_separators.clear();
|
m_separators.clear();
|
||||||
|
|
||||||
m_currentToken = 0;
|
m_currentToken = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void QCalendarDateValidator::setFormat(const QString &format)
|
void QCalendarDateValidator::setFormat(const QString &format)
|
||||||
@ -558,25 +560,25 @@ void QCalendarDateValidator::setFormat(const QString &format)
|
|||||||
separator += nextChar;
|
separator += nextChar;
|
||||||
quoting = false;
|
quoting = false;
|
||||||
} else {
|
} else {
|
||||||
SectionToken *token = 0;
|
SectionToken token;
|
||||||
if (nextChar == QLatin1Char('d')) {
|
if (nextChar == QLatin1Char('d')) {
|
||||||
offset = qMin(4, countRepeat(format, pos));
|
offset = qMin(4, countRepeat(format, pos));
|
||||||
token = new SectionToken(&m_dayValidator, offset);
|
token = SectionToken(&m_dayValidator, offset);
|
||||||
} else if (nextChar == QLatin1Char('M')) {
|
} else if (nextChar == QLatin1Char('M')) {
|
||||||
offset = qMin(4, countRepeat(format, pos));
|
offset = qMin(4, countRepeat(format, pos));
|
||||||
token = new SectionToken(&m_monthValidator, offset);
|
token = SectionToken(&m_monthValidator, offset);
|
||||||
} else if (nextChar == QLatin1Char('y')) {
|
} else if (nextChar == QLatin1Char('y')) {
|
||||||
offset = qMin(4, countRepeat(format, pos));
|
offset = qMin(4, countRepeat(format, pos));
|
||||||
token = new SectionToken(&m_yearValidator, offset);
|
token = SectionToken(&m_yearValidator, offset);
|
||||||
} else {
|
} else {
|
||||||
separator += nextChar;
|
separator += nextChar;
|
||||||
}
|
}
|
||||||
if (token) {
|
if (!token.isNull()) {
|
||||||
m_tokens.append(token);
|
m_tokens.append(token);
|
||||||
m_separators.append(separator);
|
m_separators.append(separator);
|
||||||
separator = QString();
|
separator = QString();
|
||||||
if (!m_currentToken)
|
if (m_currentToken < 0)
|
||||||
m_currentToken = token;
|
m_currentToken = m_tokens.size() - 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -595,29 +597,23 @@ void QCalendarDateValidator::applyToDate()
|
|||||||
|
|
||||||
void QCalendarDateValidator::toNextToken()
|
void QCalendarDateValidator::toNextToken()
|
||||||
{
|
{
|
||||||
const int idx = m_tokens.indexOf(m_currentToken);
|
if (m_currentToken < 0)
|
||||||
if (idx == -1)
|
|
||||||
return;
|
return;
|
||||||
if (idx + 1 >= m_tokens.count())
|
++m_currentToken;
|
||||||
m_currentToken = m_tokens.first();
|
m_currentToken %= m_tokens.size();
|
||||||
else
|
|
||||||
m_currentToken = m_tokens.at(idx + 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void QCalendarDateValidator::toPreviousToken()
|
void QCalendarDateValidator::toPreviousToken()
|
||||||
{
|
{
|
||||||
const int idx = m_tokens.indexOf(m_currentToken);
|
if (m_currentToken < 0)
|
||||||
if (idx == -1)
|
|
||||||
return;
|
return;
|
||||||
if (idx - 1 < 0)
|
--m_currentToken;
|
||||||
m_currentToken = m_tokens.last();
|
m_currentToken %= m_tokens.size();
|
||||||
else
|
|
||||||
m_currentToken = m_tokens.at(idx - 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void QCalendarDateValidator::handleKeyEvent(QKeyEvent *keyEvent)
|
void QCalendarDateValidator::handleKeyEvent(QKeyEvent *keyEvent)
|
||||||
{
|
{
|
||||||
if (!m_currentToken)
|
if (m_currentToken < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int key = keyEvent->key();
|
int key = keyEvent->key();
|
||||||
@ -630,7 +626,7 @@ void QCalendarDateValidator::handleKeyEvent(QKeyEvent *keyEvent)
|
|||||||
else if (key == Qt::Key_Left)
|
else if (key == Qt::Key_Left)
|
||||||
toPreviousToken();
|
toPreviousToken();
|
||||||
|
|
||||||
m_lastSectionMove = m_currentToken->validator->handleKey(key);
|
m_lastSectionMove = m_tokens.at(m_currentToken).validator->handleKey(key);
|
||||||
|
|
||||||
applyToDate();
|
applyToDate();
|
||||||
if (m_lastSectionMove == QCalendarDateSectionValidator::NextSection)
|
if (m_lastSectionMove == QCalendarDateSectionValidator::NextSection)
|
||||||
|
Loading…
Reference in New Issue
Block a user