Use QFlags::setFlag where prudent in qtbase

QFlags::setFlag is most useful to replace explicit constructs like

if (condition) {
   someFlags |= TheConditionFlag;
} else {
   someFlags &= ~TheConditionFlag;
}

with

someFlags.setFlag(TheConditionFlag, condition);

Change-Id: Ie4586681c83e0af812d5bbf14965aad51941a960
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Andre Somers 2016-02-02 14:40:29 +01:00 committed by Andre Somers
parent c5d4972577
commit 0df5c366e5
25 changed files with 54 additions and 185 deletions

View File

@ -3130,10 +3130,7 @@ void QTextStream::setGenerateByteOrderMark(bool generate)
{
Q_D(QTextStream);
if (d->writeBuffer.isEmpty()) {
if (generate)
d->writeConverterState.flags &= ~QTextCodec::IgnoreHeader;
else
d->writeConverterState.flags |= QTextCodec::IgnoreHeader;
d->writeConverterState.flags.setFlag(QTextCodec::IgnoreHeader, !generate);
}
}

View File

@ -282,11 +282,8 @@ QSurfaceFormat::~QSurfaceFormat()
void QSurfaceFormat::setStereo(bool enable)
{
QSurfaceFormat::FormatOptions newOptions = d->opts;
if (enable) {
newOptions |= QSurfaceFormat::StereoBuffers;
} else {
newOptions &= ~QSurfaceFormat::StereoBuffers;
}
newOptions.setFlag(QSurfaceFormat::StereoBuffers, enable);
if (int(newOptions) != int(d->opts)) {
detach();
d->opts = newOptions;

View File

@ -414,10 +414,7 @@ void QGlyphRun::setFlag(GlyphRunFlag flag, bool enabled)
return;
detach();
if (enabled)
d->flags |= flag;
else
d->flags &= ~flag;
d->flags.setFlag(flag, enabled);
}
/*!

View File

@ -664,10 +664,7 @@ QList<QSslCertificate> QSslConfiguration::systemCaCertificates()
*/
void QSslConfiguration::setSslOption(QSsl::SslOption option, bool on)
{
if (on)
d->sslOptions |= option;
else
d->sslOptions &= ~option;
d->sslOptions.setFlag(option, on);
}
/*!

View File

@ -5553,10 +5553,7 @@ QSize QGLTexture::bindCompressedTexturePVR(const char *buf, int len)
// Set the invert flag for the texture. The "vertical flip"
// flag in PVR is the opposite sense to our sense of inversion.
if ((pvrHeader->flags & PVR_VERTICAL_FLIP) != 0)
options &= ~QGLContext::InvertedYBindOption;
else
options |= QGLContext::InvertedYBindOption;
options.setFlag(QGLContext::InvertedYBindOption, !(pvrHeader->flags & PVR_VERTICAL_FLIP));
return QSize(pvrHeader->width, pvrHeader->height);
}

View File

@ -266,10 +266,7 @@ void QEvdevMouseHandler::readMouseData()
case 0x11e: button = Qt::ExtraButton12; break;
case 0x11f: button = Qt::ExtraButton13; break;
}
if (data->value)
m_buttons |= button;
else
m_buttons &= ~button;
m_buttons.setFlag(button, data->value);
btnChanged = true;
} else if (data->type == EV_SYN && data->code == SYN_REPORT) {
if (btnChanged) {

View File

@ -76,10 +76,7 @@ void QLibInputPointer::processButton(libinput_event_pointer *e)
case 0x11f: button = Qt::ExtraButton13; break;
}
if (pressed)
m_buttons |= button;
else
m_buttons &= ~button;
m_buttons.setFlag(button, pressed);
QWindowSystemInterface::handleMouseEvent(Q_NULLPTR, m_pos, m_pos, m_buttons, QGuiApplication::keyboardModifiers());
}

View File

@ -472,7 +472,7 @@ public:
xcb_window_t getSelectionOwner(xcb_atom_t atom) const;
xcb_window_t getQtSelectionOwner();
void setButton(Qt::MouseButton button, bool down) { if (down) m_buttons |= button; else m_buttons &= ~button; }
void setButton(Qt::MouseButton button, bool down) { m_buttons.setFlag(button, down); }
Qt::MouseButtons buttons() const { return m_buttons; }
Qt::MouseButton translateMouseButton(xcb_button_t s);

View File

@ -786,10 +786,8 @@ void QFileDialog::setOptions(Options options)
if (changed & DontUseCustomDirectoryIcons) {
QFileIconProvider::Options providerOptions = iconProvider()->options();
if (options & DontUseCustomDirectoryIcons)
providerOptions |= QFileIconProvider::DontUseCustomDirectoryIcons;
else
providerOptions &= ~QFileIconProvider::DontUseCustomDirectoryIcons;
providerOptions.setFlag(QFileIconProvider::DontUseCustomDirectoryIcons,
options & DontUseCustomDirectoryIcons);
iconProvider()->setOptions(providerOptions);
}
}
@ -3717,10 +3715,7 @@ void QFileDialogPrivate::_q_showHidden()
{
Q_Q(QFileDialog);
QDir::Filters dirFilters = q->filter();
if (showHiddenAction->isChecked())
dirFilters |= QDir::Hidden;
else
dirFilters &= ~QDir::Hidden;
dirFilters.setFlag(QDir::Hidden, showHiddenAction->isChecked());
q->setFilter(dirFilters);
}

View File

@ -1297,10 +1297,7 @@ void QGraphicsView::setRenderHint(QPainter::RenderHint hint, bool enabled)
{
Q_D(QGraphicsView);
QPainter::RenderHints oldHints = d->renderHints;
if (enabled)
d->renderHints |= hint;
else
d->renderHints &= ~hint;
d->renderHints.setFlag(hint, enabled);
if (oldHints != d->renderHints)
d->updateAll();
}
@ -1463,10 +1460,7 @@ void QGraphicsView::setOptimizationFlags(OptimizationFlags flags)
void QGraphicsView::setOptimizationFlag(OptimizationFlag flag, bool enabled)
{
Q_D(QGraphicsView);
if (enabled)
d->optimizationFlags |= flag;
else
d->optimizationFlags &= ~flag;
d->optimizationFlags.setFlag(flag, enabled);
}
/*!

View File

@ -2272,15 +2272,8 @@ void QGraphicsWidget::paintWindowFrame(QPainter *painter, const QStyleOptionGrap
bar.QStyleOption::operator=(*option);
d->initStyleOptionTitleBar(&bar); // this clear flags in bar.state
d->ensureWindowData();
if (d->windowData->buttonMouseOver)
bar.state |= QStyle::State_MouseOver;
else
bar.state &= ~QStyle::State_MouseOver;
if (d->windowData->buttonSunken)
bar.state |= QStyle::State_Sunken;
else
bar.state &= ~QStyle::State_Sunken;
bar.state.setFlag(QStyle::State_MouseOver, d->windowData->buttonMouseOver);
bar.state.setFlag(QStyle::State_Sunken, d->windowData->buttonSunken);
bar.rect = windowFrameRect;
// translate painter to make the style happy
@ -2337,17 +2330,9 @@ void QGraphicsWidget::paintWindowFrame(QPainter *painter, const QStyleOptionGrap
initStyleOption(&frameOptions);
if (!hasBorder)
painter->setClipRect(windowFrameRect.adjusted(0, +height, 0, 0), Qt::IntersectClip);
if (hasFocus()) {
frameOptions.state |= QStyle::State_HasFocus;
} else {
frameOptions.state &= ~QStyle::State_HasFocus;
}
frameOptions.state.setFlag(QStyle::State_HasFocus, hasFocus());
bool isActive = isActiveWindow();
if (isActive) {
frameOptions.state |= QStyle::State_Active;
} else {
frameOptions.state &= ~QStyle::State_Active;
}
frameOptions.state.setFlag(QStyle::State_Active, isActive);
frameOptions.palette.setCurrentColorGroup(isActive ? QPalette::Active : QPalette::Normal);
frameOptions.rect = windowFrameRect;

View File

@ -327,12 +327,11 @@ void QGraphicsWidgetPrivate::initStyleOptionTitleBar(QStyleOptionTitleBar *optio
option->subControls = QStyle::SC_TitleBarCloseButton | QStyle::SC_TitleBarLabel | QStyle::SC_TitleBarSysMenu;
option->activeSubControls = windowData->hoveredSubControl;
bool isActive = q->isActiveWindow();
option->state.setFlag(QStyle::State_Active, isActive);
if (isActive) {
option->state |= QStyle::State_Active;
option->titleBarState = Qt::WindowActive;
option->titleBarState |= QStyle::State_Active;
} else {
option->state &= ~QStyle::State_Active;
option->titleBarState = Qt::WindowNoState;
}
QFont windowTitleFont = QApplication::font("QMdiSubWindowTitleBar");

View File

@ -1015,10 +1015,7 @@ void QListView::paintEvent(QPaintEvent *e)
if (viewState == EditingState)
option.state |= QStyle::State_Editing;
}
if (*it == hover)
option.state |= QStyle::State_MouseOver;
else
option.state &= ~QStyle::State_MouseOver;
option.state.setFlag(QStyle::State_MouseOver, *it == hover);
if (alternate) {
int row = (*it).row();
@ -1033,11 +1030,7 @@ void QListView::paintEvent(QPaintEvent *e)
alternateBase = (row & 1) != 0;
}
}
if (alternateBase) {
option.features |= QStyleOptionViewItem::Alternate;
} else {
option.features &= ~QStyleOptionViewItem::Alternate;
}
option.features.setFlag(QStyleOptionViewItem::Alternate, alternateBase);
// draw background of the item (only alternate row). rest of the background
// is provided by the delegate

View File

@ -824,10 +824,7 @@ void QTableViewPrivate::drawAndClipSpans(const QRegion &area, QPainter *painter,
QStyleOptionViewItem opt = option;
opt.rect = rect;
alternateBase = alternatingColors && (span->top() & 1);
if (alternateBase)
opt.features |= QStyleOptionViewItem::Alternate;
else
opt.features &= ~QStyleOptionViewItem::Alternate;
opt.features.setFlag(QStyleOptionViewItem::Alternate, alternateBase);
drawCell(painter, opt, index);
region -= rect;
for (int r = span->top(); r <= span->bottom(); ++r) {

View File

@ -1359,11 +1359,7 @@ void QTreeViewPrivate::paintAlternatingRowColors(QPainter *painter, QStyleOption
}
while (y <= bottom) {
option->rect.setRect(0, y, viewport->width(), rowHeight);
if (current & 1) {
option->features |= QStyleOptionViewItem::Alternate;
} else {
option->features &= ~QStyleOptionViewItem::Alternate;
}
option->features.setFlag(QStyleOptionViewItem::Alternate, current & 1);
++current;
q->style()->drawPrimitive(QStyle::PE_PanelItemViewRow, option, painter, q);
y += rowHeight;
@ -1709,11 +1705,9 @@ void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option,
else
opt.state |= QStyle::State_HasFocus;
}
if ((hoverRow || modelIndex == hover)
&& (option.showDecorationSelected || (d->hoverBranch == -1)))
opt.state |= QStyle::State_MouseOver;
else
opt.state &= ~QStyle::State_MouseOver;
opt.state.setFlag(QStyle::State_MouseOver,
(hoverRow || modelIndex == hover)
&& (option.showDecorationSelected || d->hoverBranch == -1));
if (enabled) {
QPalette::ColorGroup cg;
@ -1729,11 +1723,7 @@ void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option,
}
if (alternate) {
if (d->current & 1) {
opt.features |= QStyleOptionViewItem::Alternate;
} else {
opt.features &= ~QStyleOptionViewItem::Alternate;
}
opt.features.setFlag(QStyleOptionViewItem::Alternate, d->current & 1);
}
/* Prior to Qt 4.3, the background of the branch (in selected state and
@ -1832,11 +1822,7 @@ void QTreeView::drawBranches(QPainter *painter, const QRect &rect,
painter->setBrushOrigin(QPoint(0, verticalOffset()));
if (d->alternatingColors) {
if (d->current & 1) {
opt.features |= QStyleOptionViewItem::Alternate;
} else {
opt.features &= ~QStyleOptionViewItem::Alternate;
}
opt.features.setFlag(QStyleOptionViewItem::Alternate, d->current & 1);
}
// When hovering over a row, pass State_Hover for painting the branch
@ -1862,10 +1848,8 @@ void QTreeView::drawBranches(QPainter *painter, const QRect &rect,
| (moreSiblings ? QStyle::State_Sibling : QStyle::State_None)
| (children ? QStyle::State_Children : QStyle::State_None)
| (expanded ? QStyle::State_Open : QStyle::State_None);
if (hoverRow || item == d->hoverBranch)
opt.state |= QStyle::State_MouseOver;
else
opt.state &= ~QStyle::State_MouseOver;
opt.state.setFlag(QStyle::State_MouseOver, hoverRow || item == d->hoverBranch);
style()->drawPrimitive(QStyle::PE_IndicatorBranch, &opt, painter, this);
}
// then go out level by level
@ -1890,10 +1874,8 @@ void QTreeView::drawBranches(QPainter *painter, const QRect &rect,
}
if (moreSiblings)
opt.state |= QStyle::State_Sibling;
if (hoverRow || item == d->hoverBranch)
opt.state |= QStyle::State_MouseOver;
else
opt.state &= ~QStyle::State_MouseOver;
opt.state.setFlag(QStyle::State_MouseOver, hoverRow || item == d->hoverBranch);
style()->drawPrimitive(QStyle::PE_IndicatorBranch, &opt, painter, this);
current = ancestor;
ancestor = current.parent();

View File

@ -1233,30 +1233,21 @@ const QAndroidStyle::AndroidDrawable * QAndroidStyle::AndroidStateDrawable::best
int QAndroidStyle::AndroidStateDrawable::extractState(const QVariantMap &value)
{
int state = QStyle::State_Enabled | QStyle::State_Active;;
QStyle::State state = QStyle::State_Enabled | QStyle::State_Active;;
foreach (const QString &key, value.keys()) {
bool val = value.value(key).toString() == QLatin1String("true");
if (key == QLatin1String("enabled")) {
if (val)
state |= QStyle::State_Enabled;
else
state &= ~QStyle::State_Enabled;
state.setFlag(QStyle::State_Enabled, val);
continue;
}
if (key == QLatin1String("window_focused")) {
if (val)
state |= QStyle::State_Active;
else
state &= ~QStyle::State_Active;
state.setFlag(QStyle::State_Active, val);
continue;
}
if (key == QLatin1String("focused")) {
if (val)
state |= QStyle::State_HasFocus;
else
state &= ~QStyle::State_HasFocus;
state.setFlag(QStyle::State_HasFocus, val);
continue;
}
@ -1271,18 +1262,12 @@ int QAndroidStyle::AndroidStateDrawable::extractState(const QVariantMap &value)
}
if (key == QLatin1String("selected")) {
if (val)
state |= QStyle::State_Selected;
else
state &= ~QStyle::State_Selected;
state.setFlag(QStyle::State_Selected, val);
continue;
}
if (key == QLatin1String("active")) {
if (val)
state |= QStyle::State_Active;
else
state &= ~QStyle::State_Active;
state.setFlag(QStyle::State_Active, val);
continue;
}
@ -1292,7 +1277,7 @@ int QAndroidStyle::AndroidStateDrawable::extractState(const QVariantMap &value)
if (key == QLatin1String("background") && val)
return -1;
}
return state;
return static_cast<int>(state);
}
void QAndroidStyle::AndroidStateDrawable::setPaddingLeftToSizeWidth()

View File

@ -684,10 +684,7 @@ void QFusionStyle::drawPrimitive(PrimitiveElement elem,
{
QStyleOption dockWidgetHandle = *option;
bool horizontal = option->state & State_Horizontal;
if (horizontal)
dockWidgetHandle.state &= ~State_Horizontal;
else
dockWidgetHandle.state |= State_Horizontal;
dockWidgetHandle.state.setFlag(State_Horizontal, !horizontal);
proxy()->drawControl(CE_Splitter, &dockWidgetHandle, painter, widget);
}
break;

View File

@ -177,10 +177,7 @@ void QCheckBox::initStyleOption(QStyleOptionButton *option) const
else
option->state |= d->checked ? QStyle::State_On : QStyle::State_Off;
if (testAttribute(Qt::WA_Hover) && underMouse()) {
if (d->hovering)
option->state |= QStyle::State_MouseOver;
else
option->state &= ~QStyle::State_MouseOver;
option->state.setFlag(QStyle::State_MouseOver, d->hovering);
}
option->text = d->text;
option->icon = d->icon;

View File

@ -103,11 +103,7 @@ void QGroupBox::initStyleOption(QStyleOptionGroupBox *option) const
option->activeSubControls |= d->pressedControl;
option->subControls = QStyle::SC_GroupBoxFrame;
if (d->hover)
option->state |= QStyle::State_MouseOver;
else
option->state &= ~QStyle::State_MouseOver;
option->state.setFlag(QStyle::State_MouseOver, d->hover);
if (d->flat)
option->features |= QStyleOptionFrame::Flat;

View File

@ -568,16 +568,10 @@ void QLineEdit::setEchoMode(EchoMode mode)
if (mode == (EchoMode)d->control->echoMode())
return;
Qt::InputMethodHints imHints = inputMethodHints();
if (mode == Password || mode == NoEcho) {
imHints |= Qt::ImhHiddenText;
} else {
imHints &= ~Qt::ImhHiddenText;
}
if (mode != Normal) {
imHints |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText | Qt::ImhSensitiveData);
} else {
imHints &= ~(Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText | Qt::ImhSensitiveData);
}
imHints.setFlag(Qt::ImhHiddenText, mode == Password || mode == NoEcho);
imHints.setFlag(Qt::ImhNoAutoUppercase, mode != Normal);
imHints.setFlag(Qt::ImhNoPredictiveText, mode != Normal);
imHints.setFlag(Qt::ImhSensitiveData, mode != Normal);
setInputMethodHints(imHints);
d->control->setEchoMode(mode);
update();

View File

@ -922,10 +922,7 @@ void QMainWindow::setAnimated(bool enabled)
Q_D(QMainWindow);
DockOptions opts = d->layout->dockOptions;
if (enabled)
opts |= AnimatedDocks;
else
opts &= ~AnimatedDocks;
opts.setFlag(AnimatedDocks, enabled);
d->layout->setDockOptions(opts);
}
@ -961,10 +958,7 @@ void QMainWindow::setDockNestingEnabled(bool enabled)
Q_D(QMainWindow);
DockOptions opts = d->layout->dockOptions;
if (enabled)
opts |= AllowNestedDocks;
else
opts &= ~AllowNestedDocks;
opts.setFlag(AllowNestedDocks, enabled);
d->layout->setDockOptions(opts);
}

View File

@ -375,10 +375,7 @@ void QDockWidgetGroupWindow::adjustFlags()
Qt::WindowFlags flags = oldFlags;
if (nativeDeco) {
flags |= Qt::CustomizeWindowHint | Qt::WindowTitleHint;
if (top->features() & QDockWidget::DockWidgetClosable)
flags |= Qt::WindowCloseButtonHint;
else
flags &= ~Qt::WindowCloseButtonHint;
flags.setFlag(Qt::WindowCloseButtonHint, top->features() & QDockWidget::DockWidgetClosable);
flags &= ~Qt::FramelessWindowHint;
} else {
flags |= Qt::FramelessWindowHint;

View File

@ -2104,10 +2104,7 @@ void QMdiArea::setActivationOrder(WindowOrder order)
void QMdiArea::setOption(AreaOption option, bool on)
{
Q_D(QMdiArea);
if (on && !(d->options & option))
d->options |= option;
else if (!on && (d->options & option))
d->options &= ~option;
d->options.setFlag(option, on);
}
/*!

View File

@ -575,10 +575,7 @@ void ControllerWidget::setControlVisible(QMdiSubWindowPrivate::WindowStateAction
if (subControl == QStyle::SC_None)
return;
if (visible && !(visibleControls & subControl))
visibleControls |= subControl;
else if (!visible && (visibleControls & subControl))
visibleControls &= ~subControl;
visibleControls.setFlag(subControl, visible && !(visibleControls & subControl));
}
/*
@ -2417,10 +2414,7 @@ bool QMdiSubWindow::isShaded() const
void QMdiSubWindow::setOption(SubWindowOption option, bool on)
{
Q_D(QMdiSubWindow);
if (on && !(d->options & option))
d->options |= option;
else if (!on && (d->options & option))
d->options &= ~option;
d->options.setFlag(option, on);
#ifndef QT_NO_RUBBERBAND
if ((option & (RubberBandResize | RubberBandMove)) && !on && d->isInRubberBandMode)
@ -3172,10 +3166,7 @@ void QMdiSubWindow::paintEvent(QPaintEvent *paintEvent)
QStyleOptionFrame frameOptions;
frameOptions.initFrom(this);
frameOptions.lineWidth = style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth, 0, this);
if (d->isActive)
frameOptions.state |= QStyle::State_Active;
else
frameOptions.state &= ~QStyle::State_Active;
frameOptions.state.setFlag(QStyle::State_Active, d->isActive);
// ### Ensure that we do not require setting the cliprect for 4.4
if (!isMinimized() && !d->hasBorder(d->cachedStyleOptions))

View File

@ -182,10 +182,7 @@ void QRadioButton::initStyleOption(QStyleOptionButton *option) const
option->state |= QStyle::State_Sunken;
option->state |= (d->checked) ? QStyle::State_On : QStyle::State_Off;
if (testAttribute(Qt::WA_Hover) && underMouse()) {
if (d->hovering)
option->state |= QStyle::State_MouseOver;
else
option->state &= ~QStyle::State_MouseOver;
option->state.setFlag(QStyle::State_MouseOver, d->hovering);
}
}