Cleanup Widgets examples - foreach

Cleanup the Widgets examples - replace foreach with range-based for loop
in mainwindows and painting subdirectories

Change-Id: I3c1556dffd22e29dd0a5ba960e699291c496278a
Reviewed-by: Konstantin Shegunov <kshegunov@gmail.com>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Luca Beldi <v.ronin@yahoo.it>
Reviewed-by: Martin Smith <martin.smith@qt.io>
This commit is contained in:
Christian Ehrlicher 2018-12-07 11:58:03 +01:00
parent 770b4afeed
commit bce32c8ab8
18 changed files with 80 additions and 67 deletions

View File

@ -454,8 +454,8 @@ void ColorSwatch::updateContextMenu()
tabMenu->clear();
splitHMenu->clear();
splitVMenu->clear();
QList<ColorSwatch*> dock_list = mainWindow->findChildren<ColorSwatch*>();
foreach (ColorSwatch *dock, dock_list) {
const QList<ColorSwatch *> dockList = mainWindow->findChildren<ColorSwatch*>();
for (const ColorSwatch *dock : dockList) {
tabMenu->addAction(dock->objectName());
splitHMenu->addAction(dock->objectName());
splitVMenu->addAction(dock->objectName());
@ -464,7 +464,8 @@ void ColorSwatch::updateContextMenu()
static ColorSwatch *findByName(const QMainWindow *mainWindow, const QString &name)
{
foreach (ColorSwatch *dock, mainWindow->findChildren<ColorSwatch*>()) {
const QList<ColorSwatch *> dockList = mainWindow->findChildren<ColorSwatch*>();
for (ColorSwatch *dock : dockList) {
if (name == dock->objectName())
return dock;
}

View File

@ -244,7 +244,8 @@ void ToolBar::order()
{
QList<QAction *> ordered;
QList<QAction *> actions1 = actions();
foreach (QAction *action, findChildren<QAction *>()) {
const QList<QAction *> childActions = findChildren<QAction *>();
for (QAction *action : childActions) {
if (!actions1.contains(action))
continue;
actions1.removeAll(action);

View File

@ -70,7 +70,8 @@ int main(int argc, char *argv[])
parser.process(app);
MainWindow mainWin;
foreach (const QString &fileName, parser.positionalArguments())
const QStringList posArgs = parser.positionalArguments();
for (const QString &fileName : posArgs)
mainWin.openFile(fileName);
mainWin.show();
return app.exec();

View File

@ -490,7 +490,8 @@ QMdiSubWindow *MainWindow::findMdiChild(const QString &fileName) const
{
QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
foreach (QMdiSubWindow *window, mdiArea->subWindowList()) {
const QList<QMdiSubWindow *> subWindows = mdiArea->subWindowList();
for (QMdiSubWindow *window : subWindows) {
MdiChild *mdiChild = qobject_cast<MdiChild *>(window->widget());
if (mdiChild->currentFile() == canonicalFilePath)
return window;

View File

@ -68,7 +68,8 @@ int main(int argc, char *argv[])
parser.process(app);
MainWindow *mainWin = nullptr;
foreach (const QString &file, parser.positionalArguments()) {
const QStringList posArgs = parser.positionalArguments();
for (const QString &file : posArgs) {
MainWindow *newWin = new MainWindow(file);
newWin->tile(mainWin);
newWin->show();

View File

@ -472,7 +472,8 @@ MainWindow *MainWindow::findMainWindow(const QString &fileName) const
{
QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
const QList<QWidget *> topLevelWidgets = QApplication::topLevelWidgets();
for (QWidget *widget : topLevelWidgets) {
MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
if (mainWin && mainWin->curFile == canonicalFilePath)
return mainWin;

View File

@ -58,12 +58,12 @@ int main(int argc, char **argv)
QApplication app(argc, argv);
XFormWidget xformWidget(0);
QStyle *arthurStyle = new ArthurStyle();
XFormWidget xformWidget(nullptr);
QStyle *arthurStyle = new ArthurStyle;
xformWidget.setStyle(arthurStyle);
QList<QWidget *> widgets = xformWidget.findChildren<QWidget *>();
foreach (QWidget *w, widgets) {
const QList<QWidget *> widgets = xformWidget.findChildren<QWidget *>();
for (QWidget *w : widgets) {
w->setStyle(arthurStyle);
w->setAttribute(Qt::WA_AcceptTouchEvents);
}

View File

@ -60,8 +60,8 @@ int main(int argc, char *argv[])
QStyle *arthurStyle = new ArthurStyle();
compWidget.setStyle(arthurStyle);
QList<QWidget *> widgets = compWidget.findChildren<QWidget *>();
foreach (QWidget *w, widgets)
const QList<QWidget *> widgets = compWidget.findChildren<QWidget *>();
for (QWidget *w : widgets)
w->setStyle(arthurStyle);
compWidget.show();

View File

@ -60,12 +60,12 @@ int main(int argc, char **argv)
bool smallScreen = QApplication::arguments().contains("-small-screen");
PathDeformWidget deformWidget(0, smallScreen);
PathDeformWidget deformWidget(nullptr, smallScreen);
QStyle *arthurStyle = new ArthurStyle();
QStyle *arthurStyle = new ArthurStyle;
deformWidget.setStyle(arthurStyle);
QList<QWidget *> widgets = deformWidget.findChildren<QWidget *>();
foreach (QWidget *w, widgets)
const QList<QWidget *> widgets = deformWidget.findChildren<QWidget *>();
for (QWidget *w : widgets)
w->setStyle(arthurStyle);
if (smallScreen)

View File

@ -291,16 +291,16 @@ void PathDeformWidget::hideControls()
m_controls->hide();
}
void PathDeformWidget::setStyle( QStyle * style )
void PathDeformWidget::setStyle(QStyle *style)
{
QWidget::setStyle(style);
if (m_controls == 0)
if (!m_controls)
return;
m_controls->setStyle(style);
QList<QWidget *> widgets = m_controls->findChildren<QWidget *>();
foreach (QWidget *w, widgets)
const QList<QWidget *> widgets = m_controls->findChildren<QWidget *>();
for (QWidget *w : widgets)
w->setStyle(style);
}

View File

@ -135,7 +135,7 @@ signals:
void okPressed();
void quitPressed();
private:
PathDeformRenderer* m_renderer;
PathDeformRenderer *m_renderer;
void layoutForDesktop();
void layoutForSmallScreen();
};
@ -145,7 +145,7 @@ class PathDeformWidget : public QWidget
Q_OBJECT
public:
PathDeformWidget(QWidget *parent, bool smallScreen);
void setStyle (QStyle * style );
void setStyle(QStyle *style);
private:
PathDeformRenderer *m_renderer;

View File

@ -86,9 +86,10 @@ void MainWindow::setupFontTree()
{
QFontDatabase database;
fontTree->setColumnCount(1);
fontTree->setHeaderLabels(QStringList() << tr("Font"));
fontTree->setHeaderLabels({ tr("Font") });
foreach (QString family, database.families()) {
const QStringList fontFamilies = database.families();
for (const QString &family : fontFamilies) {
const QStringList styles = database.styles(family);
if (styles.isEmpty())
continue;
@ -98,7 +99,7 @@ void MainWindow::setupFontTree()
familyItem->setCheckState(0, Qt::Unchecked);
familyItem->setFlags(familyItem->flags() | Qt::ItemIsAutoTristate);
foreach (QString style, styles) {
for (const QString &style : styles) {
QTreeWidgetItem *styleItem = new QTreeWidgetItem(familyItem);
styleItem->setText(0, style);
styleItem->setCheckState(0, Qt::Unchecked);
@ -110,10 +111,10 @@ void MainWindow::setupFontTree()
void MainWindow::on_clearAction_triggered()
{
QTreeWidgetItem *currentItem = fontTree->currentItem();
foreach (QTreeWidgetItem *item, fontTree->selectedItems())
fontTree->setItemSelected(item, false);
fontTree->setItemSelected(currentItem, true);
const QList<QTreeWidgetItem *> items = fontTree->selectedItems();
for (QTreeWidgetItem *item : items)
item->setSelected(false);
fontTree->currentItem()->setSelected(true);
}
void MainWindow::on_markAction_triggered()
@ -128,8 +129,8 @@ void MainWindow::on_unmarkAction_triggered()
void MainWindow::markUnmarkFonts(Qt::CheckState state)
{
QList<QTreeWidgetItem *> items = fontTree->selectedItems();
foreach (QTreeWidgetItem *item, items) {
const QList<QTreeWidgetItem *> items = fontTree->selectedItems();
for (QTreeWidgetItem *item : items) {
if (item->checkState(0) != state)
item->setCheckState(0, state);
}
@ -295,19 +296,19 @@ void MainWindow::on_printPreviewAction_triggered()
void MainWindow::printPage(int index, QPainter *painter, QPrinter *printer)
{
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
QString family = pageMap.keys()[index];
StyleItems items = pageMap[family];
const QString family = (pageMap.begin() + index).key();
const StyleItems items = pageMap.value(family);
// Find the dimensions of the text on each page.
qreal width = 0.0;
qreal height = 0.0;
foreach (QTreeWidgetItem *item, items) {
for (const QTreeWidgetItem *item : items) {
QString style = item->text(0);
int weight = item->data(0, Qt::UserRole).toInt();
bool italic = item->data(0, Qt::UserRole + 1).toBool();
// Calculate the maximum width and total height of the text.
foreach (int size, sampleSizes) {
for (int size : qAsConst(sampleSizes)) {
QFont font(family, size, weight, italic);
font.setStyleName(style);
font = QFont(font, painter->device());
@ -335,13 +336,13 @@ void MainWindow::printPage(int index, QPainter *painter, QPrinter *printer)
qreal x = -width / 2.0;
qreal y = -height / 2.0 - remainingHeight / 4.0 + spaceHeight;
foreach (QTreeWidgetItem *item, items) {
for (const QTreeWidgetItem *item : items) {
QString style = item->text(0);
int weight = item->data(0, Qt::UserRole).toInt();
bool italic = item->data(0, Qt::UserRole + 1).toBool();
// Draw each line of text.
foreach (int size, sampleSizes) {
for (int size : qAsConst(sampleSizes)) {
QFont font(family, size, weight, italic);
font.setStyleName(style);
font = QFont(font, painter->device());

View File

@ -61,8 +61,8 @@ int main(int argc, char *argv[])
GradientWidget gradientWidget(0);
QStyle *arthurStyle = new ArthurStyle();
gradientWidget.setStyle(arthurStyle);
QList<QWidget *> widgets = gradientWidget.findChildren<QWidget *>();
foreach (QWidget *w, widgets) {
const QList<QWidget *> widgets = gradientWidget.findChildren<QWidget *>();
for (QWidget *w : widgets) {
w->setStyle(arthurStyle);
w->setAttribute(Qt::WA_AcceptTouchEvents);
}

View File

@ -280,8 +280,8 @@ void Window::penColorChanged()
//! [22]
void Window::populateWithColors(QComboBox *comboBox)
{
QStringList colorNames = QColor::colorNames();
foreach (QString name, colorNames)
const QStringList colorNames = QColor::colorNames();
for (const QString &name : colorNames)
comboBox->addItem(name, QColor(name));
}
//! [22]

View File

@ -63,8 +63,8 @@ int main(int argc, char **argv)
PathStrokeWidget pathStrokeWidget(smallScreen);
QStyle *arthurStyle = new ArthurStyle();
pathStrokeWidget.setStyle(arthurStyle);
QList<QWidget *> widgets = pathStrokeWidget.findChildren<QWidget *>();
foreach (QWidget *w, widgets) {
const QList<QWidget *> widgets = pathStrokeWidget.findChildren<QWidget *>();
for (QWidget *w : widgets) {
w->setStyle(arthurStyle);
w->setAttribute(Qt::WA_AcceptTouchEvents);
}

View File

@ -390,8 +390,8 @@ void PathStrokeWidget::setStyle( QStyle * style )
{
m_controls->setStyle(style);
QList<QWidget *> widgets = m_controls->findChildren<QWidget *>();
foreach (QWidget *w, widgets)
const QList<QWidget *> widgets = m_controls->findChildren<QWidget *>();
for (QWidget *w : widgets)
w->setStyle(style);
}
}
@ -605,7 +605,7 @@ bool PathStrokeRenderer::event(QEvent *e)
{
const QTouchEvent *const event = static_cast<const QTouchEvent*>(e);
const QList<QTouchEvent::TouchPoint> points = event->touchPoints();
foreach (const QTouchEvent::TouchPoint &touchPoint, points) {
for (const QTouchEvent::TouchPoint &touchPoint : points) {
const int id = touchPoint.id();
switch (touchPoint.state()) {
case Qt::TouchPointPressed:

View File

@ -330,32 +330,40 @@ void ArthurFrame::showSource()
QString contents;
if (m_sourceFileName.isEmpty()) {
contents = QString("No source for widget: '%1'").arg(objectName());
contents = tr("No source for widget: '%1'").arg(objectName());
} else {
QFile f(m_sourceFileName);
if (!f.open(QFile::ReadOnly))
contents = QString("Could not open file: '%1'").arg(m_sourceFileName);
contents = tr("Could not open file: '%1'").arg(m_sourceFileName);
else
contents = f.readAll();
}
contents.replace('&', "&amp;");
contents.replace('<', "&lt;");
contents.replace('>', "&gt;");
contents.replace(QLatin1Char('&'), QStringLiteral("&amp;"));
contents.replace(QLatin1Char('<'), QStringLiteral("&lt;"));
contents.replace(QLatin1Char('>'), QStringLiteral("&gt;"));
QStringList keywords;
keywords << "for " << "if " << "switch " << " int " << "#include " << "const"
<< "void " << "uint " << "case " << "double " << "#define " << "static"
<< "new" << "this";
static const QString keywords[] = {
QStringLiteral("for "), QStringLiteral("if "),
QStringLiteral("switch "), QStringLiteral(" int "),
QStringLiteral("#include "), QStringLiteral("const"),
QStringLiteral("void "), QStringLiteral("uint "),
QStringLiteral("case "), QStringLiteral("double "),
QStringLiteral("#define "), QStringLiteral("static"),
QStringLiteral("new"), QStringLiteral("this")
};
foreach (QString keyword, keywords)
for (const QString &keyword : keywords)
contents.replace(keyword, QLatin1String("<font color=olive>") + keyword + QLatin1String("</font>"));
contents.replace("(int ", "(<font color=olive><b>int </b></font>");
contents.replace(QStringLiteral("(int "), QStringLiteral("(<font color=olive><b>int </b></font>"));
QStringList ppKeywords;
ppKeywords << "#ifdef" << "#ifndef" << "#if" << "#endif" << "#else";
static const QString ppKeywords[] = {
QStringLiteral("#ifdef"), QStringLiteral("#ifndef"),
QStringLiteral("#if"), QStringLiteral("#endif"),
QStringLiteral("#else")
};
foreach (QString keyword, ppKeywords)
for (const QString &keyword : ppKeywords)
contents.replace(keyword, QLatin1String("<font color=navy>") + keyword + QLatin1String("</font>"));
contents.replace(QRegularExpression("(\\d\\d?)"), QLatin1String("<font color=navy>\\1</font>"));
@ -366,12 +374,10 @@ void ArthurFrame::showSource()
QRegularExpression stringLiteralRe("(\".+?\")");
contents.replace(stringLiteralRe, QLatin1String("<font color=green>\\1</font>"));
QString html = contents;
html.prepend("<html><pre>");
html.append("</pre></html>");
const QString html = QStringLiteral("<html><pre>") + contents + QStringLiteral("</pre></html>");
QTextBrowser *sourceViewer = new QTextBrowser(0);
sourceViewer->setWindowTitle("Source: " + m_sourceFileName.mid(5));
sourceViewer->setWindowTitle(tr("Source: %1").arg(m_sourceFileName.midRef(5)));
sourceViewer->setParent(this, Qt::Dialog);
sourceViewer->setAttribute(Qt::WA_DeleteOnClose);
sourceViewer->setLineWrapMode(QTextEdit::NoWrap);

View File

@ -174,7 +174,7 @@ bool HoverPoints::eventFilter(QObject *object, QEvent *event)
const QTouchEvent *const touchEvent = static_cast<const QTouchEvent*>(event);
const QList<QTouchEvent::TouchPoint> points = touchEvent->touchPoints();
const qreal pointSize = qMax(m_pointSize.width(), m_pointSize.height());
foreach (const QTouchEvent::TouchPoint &touchPoint, points) {
for (const QTouchEvent::TouchPoint &touchPoint : points) {
const int id = touchPoint.id();
switch (touchPoint.state()) {
case Qt::TouchPointPressed: