QLineEdit: hold SideWidgetEntry in std::vector, not QVector
This is private implementation, so there's no BC issue here. The collections ported here also do not benefit from CoW, because they are never copied. Adapt to STL API and replace foreach with C++11 range-for loops, because the former deep-copies STL containers. Also replace index-based for loops with C++11 range-for, to evade the int/size_t problem on MSVC. Saves a bit more than 1KiB in text size on optimized GCC 4.9 Linux AMD64 builds, not all of which can be attributed to the ports to range-for. Change-Id: I240030180bd1b2ca40c002b03ab72319a99a87c3 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>bb10
parent
66441d7a82
commit
6066af7f09
|
|
@ -2180,9 +2180,10 @@ void QLineEdit::changeEvent(QEvent *ev)
|
|||
update();
|
||||
break;
|
||||
case QEvent::LayoutDirectionChange:
|
||||
foreach (const QLineEditPrivate::SideWidgetEntry &e, d->trailingSideWidgets) // Refresh icon to show arrow in right direction.
|
||||
for (const auto &e : d->trailingSideWidgets) { // Refresh icon to show arrow in right direction.
|
||||
if (e.flags & QLineEditPrivate::SideWidgetClearButton)
|
||||
static_cast<QLineEditIconButton *>(e.widget)->setIcon(d->clearButtonIcon());
|
||||
}
|
||||
d->positionSideWidgets();
|
||||
break;
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -382,11 +382,11 @@ void QLineEditPrivate::_q_textChanged(const QString &text)
|
|||
lastTextSize = newTextSize;
|
||||
#ifndef QT_NO_ANIMATION
|
||||
const bool fadeIn = newTextSize > 0;
|
||||
foreach (const SideWidgetEntry &e, leadingSideWidgets) {
|
||||
for (const SideWidgetEntry &e : leadingSideWidgets) {
|
||||
if (e.flags & SideWidgetFadeInWithText)
|
||||
static_cast<QLineEditIconButton *>(e.widget)->animateShow(fadeIn);
|
||||
}
|
||||
foreach (const SideWidgetEntry &e, trailingSideWidgets) {
|
||||
for (const SideWidgetEntry &e : trailingSideWidgets) {
|
||||
if (e.flags & SideWidgetFadeInWithText)
|
||||
static_cast<QLineEditIconButton *>(e.widget)->animateShow(fadeIn);
|
||||
}
|
||||
|
|
@ -453,13 +453,17 @@ void QLineEditPrivate::positionSideWidgets()
|
|||
|
||||
QLineEditPrivate::PositionIndexPair QLineEditPrivate::findSideWidget(const QAction *a) const
|
||||
{
|
||||
for (int i = 0; i < leadingSideWidgets.size(); ++i) {
|
||||
if (a == leadingSideWidgets.at(i).action)
|
||||
int i = 0;
|
||||
for (const auto &e : leadingSideWidgets) {
|
||||
if (a == e.action)
|
||||
return PositionIndexPair(QLineEdit::LeadingPosition, i);
|
||||
++i;
|
||||
}
|
||||
for (int i = 0; i < trailingSideWidgets.size(); ++i) {
|
||||
if (a == trailingSideWidgets.at(i).action)
|
||||
i = 0;
|
||||
for (const auto &e : trailingSideWidgets) {
|
||||
if (a == e.action)
|
||||
return PositionIndexPair(QLineEdit::TrailingPosition, i);
|
||||
++i;
|
||||
}
|
||||
return PositionIndexPair(QLineEdit::LeadingPosition, -1);
|
||||
}
|
||||
|
|
@ -493,8 +497,8 @@ QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineE
|
|||
PositionIndexPair positionIndex = before ? findSideWidget(before) : PositionIndexPair(position, -1);
|
||||
SideWidgetEntryList &list = positionIndex.first == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets;
|
||||
if (positionIndex.second < 0)
|
||||
positionIndex.second = list.size();
|
||||
list.insert(positionIndex.second, SideWidgetEntry(w, newAction, flags));
|
||||
positionIndex.second = int(list.size());
|
||||
list.insert(list.begin() + positionIndex.second, SideWidgetEntry(w, newAction, flags));
|
||||
positionSideWidgets();
|
||||
w->show();
|
||||
return w;
|
||||
|
|
@ -507,7 +511,8 @@ void QLineEditPrivate::removeAction(QAction *action)
|
|||
if (positionIndex.second == -1)
|
||||
return;
|
||||
SideWidgetEntryList &list = positionIndex.first == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets;
|
||||
SideWidgetEntry entry = list.takeAt(positionIndex.second);
|
||||
SideWidgetEntry entry = list[positionIndex.second];
|
||||
list.erase(list.begin() + positionIndex.second);
|
||||
if (entry.flags & SideWidgetCreatedByWidgetAction)
|
||||
static_cast<QWidgetAction *>(entry.action)->releaseWidget(entry.widget);
|
||||
else
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ public:
|
|||
QAction *action;
|
||||
int flags;
|
||||
};
|
||||
typedef QVector<SideWidgetEntry> SideWidgetEntryList;
|
||||
typedef std::vector<SideWidgetEntry> SideWidgetEntryList;
|
||||
|
||||
QLineEditPrivate()
|
||||
: control(0), frame(1), contextMenuEnabled(1), cursorVisible(0),
|
||||
|
|
@ -210,7 +210,7 @@ public:
|
|||
QIcon clearButtonIcon() const;
|
||||
void setClearButtonEnabled(bool enabled);
|
||||
void positionSideWidgets();
|
||||
inline bool hasSideWidgets() const { return !leadingSideWidgets.isEmpty() || !trailingSideWidgets.isEmpty(); }
|
||||
inline bool hasSideWidgets() const { return !leadingSideWidgets.empty() || !trailingSideWidgets.empty(); }
|
||||
inline const SideWidgetEntryList &leftSideWidgetList() const
|
||||
{ return q_func()->layoutDirection() == Qt::LeftToRight ? leadingSideWidgets : trailingSideWidgets; }
|
||||
inline const SideWidgetEntryList &rightSideWidgetList() const
|
||||
|
|
@ -238,15 +238,17 @@ static bool isSideWidgetVisible(const QLineEditPrivate::SideWidgetEntry &e)
|
|||
|
||||
inline int QLineEditPrivate::effectiveLeftTextMargin() const
|
||||
{
|
||||
const auto &list = leftSideWidgetList();
|
||||
return leftTextMargin + (QLineEditIconButton::IconMargin + iconSize().width())
|
||||
* int(std::count_if(leftSideWidgetList().constBegin(), leftSideWidgetList().constEnd(),
|
||||
* int(std::count_if(list.begin(), list.end(),
|
||||
isSideWidgetVisible));
|
||||
}
|
||||
|
||||
inline int QLineEditPrivate::effectiveRightTextMargin() const
|
||||
{
|
||||
const auto &list = rightSideWidgetList();
|
||||
return rightTextMargin + (QLineEditIconButton::IconMargin + iconSize().width())
|
||||
* int(std::count_if(rightSideWidgetList().constBegin(), rightSideWidgetList().constEnd(),
|
||||
* int(std::count_if(list.begin(), list.end(),
|
||||
isSideWidgetVisible));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue