Commit c887b11d authored by Yuri Chornoivan's avatar Yuri Chornoivan

Remove foreach in KmPlot in favor of range-based loops

Summary: foreach is obsolete

Test Plan:
1. Compile, install, run.
2. Create some plot (sin(x)), save it, export it, close it, load it.
3. Use equation editor.

Reviewers: #kde_edu, aacid

Reviewed By: aacid

Subscribers: aacid, kde-edu

Tags: #kde_edu

Differential Revision: https://phabricator.kde.org/D24212
parent 2d9541db
......@@ -45,8 +45,8 @@ EquationEditorWidget::EquationEditorWidget(QWidget* parent)
QFont buttonFont;
buttonFont.setPointSizeF(font.pointSizeF() * 1.1);
QList<QToolButton*> buttons = findChildren<QToolButton*>();
foreach (QToolButton* w, buttons) {
const QList<QToolButton*> buttons = findChildren<QToolButton*>();
for (QToolButton* w : buttons) {
KAcceleratorManager::setNoAccel(w);
connect(w, &QToolButton::clicked, this, &EquationEditorWidget::characterButtonClicked);
......
......@@ -69,15 +69,15 @@ void EquationHighlighter::highlightBlock(const QString& text) {
QTextCharFormat other;
QStringList variables = m_parent->m_equation->variables();
QStringList functions = XParser::self()->predefinedFunctions(true) + XParser::self()->userFunctions();
const QStringList variables = m_parent->m_equation->variables();
const QStringList functions = XParser::self()->predefinedFunctions(true) + XParser::self()->userFunctions();
for (int i = 0; i < text.length(); ++i) {
QString remaining = text.right(text.length() - i);
bool found = false;
foreach (const QString& var, variables) {
for (const QString& var : variables) {
if (remaining.startsWith(var)) {
setFormat(i, var.length(), variable);
i += var.length() - 1;
......@@ -88,7 +88,7 @@ void EquationHighlighter::highlightBlock(const QString& text) {
if (found)
continue;
foreach (const QString& f, functions) {
for (const QString& f : functions) {
if (remaining.startsWith(f)) {
setFormat(i, f.length(), function);
i += f.length() - 1;
......
......@@ -386,12 +386,10 @@ void Equation::updateVariables()
int p1 = m_fstr.indexOf( '(' );
int p2 = m_fstr.indexOf( ')' );
QStringList listSplit;
if ( (p1 != -1) && (p2 != -1) )
listSplit = m_fstr.mid( p1+1, p2-p1-1 ).split( ',', QString::SkipEmptyParts );
const QStringList listSplit = ( (p1 != -1) && (p2 != -1) ) ? m_fstr.mid( p1+1, p2-p1-1 ).split( ',', QString::SkipEmptyParts ) : QStringList();
// Variables shouldn't contain spaces!
foreach ( QString s, listSplit ) { //krazy:exclude=foreach
for ( QString s : listSplit ) {
s = s.remove(' ');
if ( !s.isEmpty() )
m_variables << s;
......@@ -619,7 +617,7 @@ Function::Function( Type type )
Function::~Function()
{
foreach ( Equation * e, eq )
for ( Equation * e : qAsConst(eq) )
delete e;
}
......@@ -831,7 +829,7 @@ QList< Plot > Function::plots( PlotCombinations combinations ) const
for ( PMode p = Derivative0; p <= Integral; p = PMode(p+1) )
{
foreach ( Plot plot, list ) { //krazy:exclude=foreach
for ( Plot plot : qAsConst(list) ) {
if ( !plotAppearance(p).visible )
continue;
plot.plotMode = p;
......@@ -848,7 +846,7 @@ QList< Plot > Function::plots( PlotCombinations combinations ) const
for ( int i = 0; i < eq[0]->differentialStates.size(); ++i )
{
foreach ( Plot plot, list ) { //krazy:exclude=foreach
for ( Plot plot : qAsConst(list) ) {
plot.stateNumber = i;
duplicated << plot;
}
......@@ -860,7 +858,7 @@ QList< Plot > Function::plots( PlotCombinations combinations ) const
if ( combinations & DifferentPMSignatures )
{
int size = 0;
foreach ( Equation * equation, eq )
for ( Equation * equation : qAsConst(eq) )
size += equation->pmCount();
unsigned max = unsigned( std::pow( 2.0, (double)size ) );
......@@ -878,12 +876,12 @@ QList< Plot > Function::plots( PlotCombinations combinations ) const
// Generate a plot for each signature in signatures
QList< Plot > duplicated;
foreach ( const QVector<bool> &signature, signatures )
for ( const QVector<bool> &signature : qAsConst(signatures) )
{
int at = 0;
QList< QVector<bool> > pmSignature;
foreach ( Equation * equation, eq )
for ( Equation * equation : qAsConst(eq) )
{
int pmCount = equation->pmCount();
QVector<bool> sig( pmCount );
......@@ -894,7 +892,7 @@ QList< Plot > Function::plots( PlotCombinations combinations ) const
pmSignature << sig;
}
foreach ( Plot plot, list ) { //krazy:exclude=foreach
for ( Plot plot : qAsConst(list) ) {
plot.pmSignature = pmSignature;
duplicated << plot;
}
......@@ -925,7 +923,7 @@ bool Function::dependsOn( Function * function ) const
if ( m_dependencies.contains( function->id() ) )
return true;
foreach ( int functionId, m_dependencies )
for ( int functionId : qAsConst(m_dependencies) )
{
Function * f = XParser::self()->functionWithID( functionId );
......
......@@ -123,7 +123,7 @@ FunctionEditor::FunctionEditor( QMenu * createNewPlotsMenu, QWidget * parent )
#define CONNECT_WIDGETS( name, signal ) \
{ \
QList<name *> widgets = m_editor->findChildren<name *>(); \
foreach ( name * w, widgets ) \
for ( name * w : qAsConst(widgets) ) \
connect( w, SIGNAL(signal), this, SLOT(save()) ); \
}
......@@ -230,7 +230,7 @@ void FunctionEditor::syncFunctionList()
// Now, any IDs left in currentIDs are of functions that have been deleted
foreach ( FunctionListItem * item, currentFunctionItems )
for ( FunctionListItem * item : qAsConst(currentFunctionItems) )
{
if ( m_functionID == item->function() )
m_functionID = -1;
......@@ -726,7 +726,7 @@ void FunctionEditor::saveFunction( Function * tempFunction )
if ( !f || !functionListItem )
return;
foreach ( Equation * eq, f->eq )
for ( Equation * eq : qAsConst(f->eq) )
eq->differentialStates.resetToInitial();
//save all settings in the function now when we know no errors have appeared
......@@ -769,7 +769,7 @@ QMimeData * FunctionListWidget::mimeData( const QList<QListWidgetItem *> items )
KmPlotIO io;
foreach ( QListWidgetItem * item, items )
for ( QListWidgetItem * item : qAsConst(items) )
{
int f = static_cast<FunctionListItem*>(item)->function();
......
......@@ -106,7 +106,7 @@ void FunctionTools::updateEquationList()
m_widget->list->clear();
m_equations.clear();
foreach ( Function * function, XParser::self()->m_ufkt )
for ( Function * function : qAsConst(XParser::self()->m_ufkt) )
{
if ( function->type() != Function::Cartesian && function->type() != Function::Differential )
continue;
......@@ -115,12 +115,12 @@ void FunctionTools::updateEquationList()
for ( int i = 0; i < function->eq.size(); ++i )
{
foreach ( const Plot &plot, plots )
for ( const Plot &plot : qAsConst(plots) )
m_equations << EquationPair( plot, i );
}
}
foreach ( const EquationPair &eq, m_equations )
for ( const EquationPair &eq : qAsConst(m_equations) )
{
Equation * equation = eq.first.function()->eq[ eq.second ];
QListWidgetItem * item = new QListWidgetItem( equation->fstr(), m_widget->list );
......
......@@ -320,14 +320,14 @@ void InitialConditionsEditor::add()
void InitialConditionsEditor::remove()
{
QModelIndexList selected = view->selectionModel()->selectedIndexes();
const QModelIndexList selected = view->selectionModel()->selectedIndexes();
QMap< int, void * > sorted;
foreach ( const QModelIndex &index, selected )
for ( const QModelIndex &index : selected )
sorted.insert( -index.row(), 0l );
QList<int> indexes = sorted.keys();
const QList<int> indexes = sorted.keys();
foreach ( int row, indexes )
for ( int row : indexes )
m_model->removeRows( -row, 1, QModelIndex() );
emit dataChanged();
......
......@@ -132,8 +132,8 @@ void KGradientEditor::paintEvent( QPaintEvent * )
//BEGIN draw arrows
painter.setRenderHint( QPainter::Antialiasing, true );
QGradientStops stops = m_gradient.stops();
foreach ( const QGradientStop &stop, stops )
const QGradientStops stops = m_gradient.stops();
for ( const QGradientStop &stop : stops )
drawArrow( & painter, stop );
//END draw arrows
}
......
......@@ -106,8 +106,9 @@ KmPlot::KmPlot( const QCommandLineParser& parser )
setAutoSaveSettings();
{
bool exit = false;
bool first = true;
foreach(const QString& arg, parser.positionalArguments())
bool first = true;
const auto arguments = parser.positionalArguments();
for (const QString& arg : arguments)
{
QUrl url = urlFromArg(arg);
if (first)
......
......@@ -111,7 +111,7 @@ QDomDocument KmPlotIO::currentState()
root.appendChild( tag );
foreach ( Function *f, XParser::self()->m_ufkt )
for ( Function *f : qAsConst(XParser::self()->m_ufkt) )
addFunction( doc, root, f );
addConstants( doc, root );
......@@ -214,7 +214,7 @@ void KmPlotIO::addFunction( QDomDocument & doc, QDomElement & root, Function * f
tag.setAttribute( "use-parameter-list", function->m_parameters.useList );
QStringList str_parameters;
foreach ( const Value &k, function->m_parameters.list )
for ( const Value &k : qAsConst(function->m_parameters.list) )
str_parameters << k.expression();
if( !str_parameters.isEmpty() )
......@@ -241,7 +241,7 @@ void KmPlotIO::addFunction( QDomDocument & doc, QDomElement & root, Function * f
bool first = true;
QString ys;
foreach ( const Value &y, state->y0 )
for ( const Value &y : qAsConst(state->y0) )
{
if ( !first )
ys += ';';
......@@ -609,7 +609,7 @@ void KmPlotIO::parseDifferentialStates( const QDomElement & n, Equation * equati
QDomElement e = node.toElement();
QString x = e.attribute( "x" );
QStringList y = e.attribute( "y" ).split( ';' );
const QStringList y = e.attribute( "y" ).split( ';' );
DifferentialState * state = equation->differentialStates.add();
if ( state->y0.size() != y.size() )
......@@ -621,7 +621,7 @@ void KmPlotIO::parseDifferentialStates( const QDomElement & n, Equation * equati
state->x0.updateExpression( x );
int at = 0;
foreach ( const QString &f, y )
for ( const QString &f : y )
state->y0[at++] = f;
}
node = node.nextSibling();
......@@ -834,7 +834,7 @@ void KmPlotIO::oldParseFunction( const QDomElement & n )
QString KmPlotIO::gradientToString( const QGradientStops & stops )
{
QString string;
foreach ( const QGradientStop &stop, stops )
for ( const QGradientStop &stop : qAsConst(stops) )
string += QString( "%1;%2," ).arg( stop.first ).arg( stop.second.name() );
return string;
}
......@@ -843,10 +843,10 @@ QString KmPlotIO::gradientToString( const QGradientStops & stops )
// static
QGradientStops KmPlotIO::stringToGradient( const QString & string )
{
QStringList stopStrings = string.split( ',', QString::SkipEmptyParts );
const QStringList stopStrings = string.split( ',', QString::SkipEmptyParts );
QGradientStops stops;
foreach ( const QString &stopString, stopStrings )
for ( const QString &stopString : stopStrings )
{
QString pos = stopString.section( ';', 0, 0 );
QString color = stopString.section( ';', 1, 1 );
......
......@@ -79,7 +79,7 @@ KParameterEditor::KParameterEditor( QList<Value> *l, QWidget *parent )
connect(m_mainWidget->value, &EquationEdit::upPressed, this, &KParameterEditor::prev);
connect(m_mainWidget->value, &EquationEdit::downPressed, this, &KParameterEditor::next);
foreach ( const Value &v, *m_parameter )
for ( const Value &v : qAsConst(*m_parameter) )
m_mainWidget->list->addItem( v.expression() );
connect(m_mainWidget->cmdNew, &QPushButton::clicked, this, &KParameterEditor::cmdNew_clicked);
......
......@@ -555,7 +555,7 @@ void MainDlg::slotExport()
{
QString filters;
QMimeDatabase mimeDatabase;
foreach (const QByteArray &mimeType, QImageWriter::supportedMimeTypes()) {
for (const QByteArray &mimeType : QImageWriter::supportedMimeTypes()) {
const QString filter = mimeDatabase.mimeTypeForName(QLatin1String(mimeType)).filterString();
if (!filter.isEmpty()) {
if (mimeType == QByteArrayLiteral("image/png")) {
......
......@@ -84,7 +84,7 @@ void ParametersWidget::updateEquationEdits( )
return;
}
foreach ( EquationEdit * edit, m_equationEdits )
for ( EquationEdit * edit : qAsConst(m_equationEdits) )
{
if ( edit->equation()->usesParameter() || !edit->equation()->looksLikeFunction() )
continue;
......
......@@ -162,7 +162,7 @@ Parser::Parser()
Parser::~Parser()
{
foreach ( Function * function, m_ufkt )
for ( Function * function : qAsConst(m_ufkt) )
delete function;
delete m_ownEquation;
delete m_constants;
......@@ -192,9 +192,9 @@ QStringList Parser::userFunctions( ) const
{
QStringList names;
foreach ( Function * f, m_ufkt )
for ( Function * f : qAsConst(m_ufkt) )
{
foreach ( Equation * eq, f->eq )
for ( Equation * eq : qAsConst(f->eq) )
{
if ( !eq->name().isEmpty() )
names << eq->name();
......@@ -208,9 +208,9 @@ QStringList Parser::userFunctions( ) const
void Parser::reparseAllFunctions()
{
foreach ( Function * f, m_ufkt )
for ( Function * f : m_ufkt )
{
foreach ( Equation * eq, f->eq )
for ( Equation * eq : f->eq )
initEquation( eq );
}
}
......@@ -669,12 +669,12 @@ bool Parser::removeFunction( Function * item )
while ( ! newFunctions.isEmpty() )
{
QList<Function *> currentFunctions = newFunctions;
const QList<Function *> currentFunctions = newFunctions;
newFunctions.clear();
foreach ( Function *f, currentFunctions )
for ( Function *f : currentFunctions )
{
foreach ( Function *other, m_ufkt )
for ( Function *other : qAsConst(m_ufkt) )
{
if ( (other==f) || toRemove.contains(other) )
continue;
......@@ -700,7 +700,7 @@ bool Parser::removeFunction( Function * item )
return false;
}
foreach ( Function *f, toRemove )
for ( Function *f : qAsConst(toRemove) )
{
uint id = f->id();
m_ufkt.remove( id );
......@@ -988,16 +988,16 @@ bool Parser::tryPredefinedFunction()
bool Parser::tryVariable()
{
QStringList variables = m_currentEquation->variables();
const QStringList variables = m_currentEquation->variables();
// Sort the parameters by size, so that when identifying parameters, want to
// match e.g. "ab" before "a"
typedef QMultiMap <int, QString> ISMap;
ISMap sorted;
foreach ( const QString &var, variables )
for ( const QString &var : variables )
sorted.insert( -var.length(), var );
foreach ( const QString &var, sorted )
for ( const QString &var : qAsConst(sorted) )
{
if ( match( var ) )
{
......@@ -1013,7 +1013,7 @@ bool Parser::tryVariable()
bool Parser::tryUserFunction()
{
foreach ( Function * it, m_ufkt )
for ( Function * it : qAsConst(m_ufkt) )
{
for ( int i = 0; i < it->eq.size(); ++i )
{
......@@ -1198,9 +1198,9 @@ void Parser::addfptr( uint id, uint eq_id, uint args )
int Parser::fnameToID(const QString &name)
{
foreach ( Function * it, m_ufkt )
for ( Function * it : qAsConst(m_ufkt) )
{
foreach ( Equation * eq, it->eq )
for ( Equation * eq : qAsConst(it->eq) )
{
if ( eq->looksLikeFunction() && (name == eq->name()) )
return it->id();
......@@ -1589,18 +1589,18 @@ void ExpressionSanitizer::fixExpression( QString * str )
//BEGIN build up strings
QMap< LengthOrderedString, StringType > strings;
QStringList predefinedFunctions = XParser::self()->predefinedFunctions( true );
foreach ( const QString &f, predefinedFunctions )
const QStringList predefinedFunctions = XParser::self()->predefinedFunctions( true );
for ( const QString &f : predefinedFunctions )
strings[f] = FunctionString;
foreach ( Function * it, m_parser->m_ufkt )
for ( Function * it : qAsConst(m_parser->m_ufkt) )
{
foreach ( Equation * eq, it->eq )
for ( Equation * eq : qAsConst(it->eq) )
strings[eq->name()] = FunctionString;
}
QStringList constantNames = m_parser->constants()->names();
foreach ( const QString &name, constantNames )
const QStringList constantNames = m_parser->constants()->names();
for ( const QString &name : constantNames )
strings[name] = ConstantString;
strings[QStringLiteral("pi")] = ConstantString;
//END build up strings
......
......@@ -402,7 +402,7 @@ void View::draw( QPaintDevice * dev, PlotMedium medium )
painter.setRenderHint( QPainter::Antialiasing, m_zoomMode != Translating );
double at = -1;
foreach ( Function * function, XParser::self()->m_ufkt )
for ( Function * function : qAsConst(XParser::self()->m_ufkt) )
{
at += 1;
......@@ -1261,7 +1261,7 @@ void View::drawImplicit( Function * function, QPainter * painter )
assert( circular.function() );
const QList< Plot > plots = function->plots();
foreach ( const Plot &plot, plots )
for ( const Plot &plot : plots )
{
bool setAliased = false;
if ( plot.parameter.type() == Parameter::Animated )
......@@ -1286,7 +1286,7 @@ void View::drawImplicit( Function * function, QPainter * painter )
function->m_implicitMode = Function::FixedY;
QList<double> roots = findRoots( plot, m_xmin, m_xmax, RoughRoot );
foreach ( double x, roots )
for ( double x : qAsConst(roots) )
{
#ifdef DEBUG_IMPLICIT
painter->setPen( QPen( Qt::red, painter->pen().width() ) );
......@@ -1302,7 +1302,7 @@ void View::drawImplicit( Function * function, QPainter * painter )
function->m_implicitMode = Function::FixedX;
roots = findRoots( plot, m_ymin, m_ymax, RoughRoot );
foreach ( double y, roots )
for ( double y : qAsConst(roots) )
{
#ifdef DEBUG_IMPLICIT
painter->setPen( QPen( Qt::blue, painter->pen().width() ) );
......@@ -1315,11 +1315,11 @@ void View::drawImplicit( Function * function, QPainter * painter )
FuzzyPointMap singularSorted;
FuzzyPoint::dx = (m_xmax-m_xmin) * SegmentMin * 0.1 / m_clipRect.width();
FuzzyPoint::dy = (m_ymax-m_ymin) * SegmentMin * 0.1 / m_clipRect.height();
foreach ( const QPointF &point, singular )
for ( const QPointF &point : qAsConst(singular) )
singularSorted.insert( point, point );
singular = singularSorted.values();
foreach ( const QPointF &point, singular )
for ( const QPointF &point : qAsConst(singular) )
{
// radius of circle around singular point
double epsilon = qMin( FuzzyPoint::dx, FuzzyPoint::dy );
......@@ -1345,7 +1345,7 @@ void View::drawImplicit( Function * function, QPainter * painter )
qDebug() << "Found " << roots.size() << " roots.\n";
#endif
foreach ( double t, roots )
for ( double t : qAsConst(roots) )
{
#ifdef DEBUG_IMPLICIT
painter->setPen( QPen( Qt::green, painter->pen().width() ) );
......@@ -1655,13 +1655,13 @@ void View::drawFunction( Function * function, QPainter * painter )
(function->eq[0]->order() == 1) &&
function->plotAppearance( Function::Derivative0 ).showTangentField )
{
QList<Plot> plots = function->plots( Function::PlotCombinations(Function::AllCombinations) & ~Function::DifferentInitialStates );
foreach ( const Plot &plot, plots )
const QList<Plot> plots = function->plots( Function::PlotCombinations(Function::AllCombinations) & ~Function::DifferentInitialStates );
for ( const Plot &plot : plots )
drawTangentField( plot, painter );
}
QList<Plot> plots = function->plots();
foreach ( const Plot &plot, plots )
const QList<Plot> plots = function->plots();
for ( const Plot &plot : plots )
drawPlot( plot, painter );
}
......@@ -2028,20 +2028,20 @@ void View::drawFunctionInfo( QPainter * painter )
// Used for determining where to draw the next label indicating the plot name
int plotNameAt = 0;
foreach ( Function * function, XParser::self()->m_ufkt )
for ( Function * function : qAsConst(XParser::self()->m_ufkt) )
{
if ( m_stopCalculating )
break;
foreach ( const Plot &plot, function->plots() )
for ( const Plot &plot : function->plots() )
{
plot.updateFunction();
// Draw extrema points?
if ( (function->type() == Function::Cartesian) && function->plotAppearance( plot.plotMode ).showExtrema )
{
QList<QPointF> stationaryPoints = findStationaryPoints( plot );
foreach ( const QPointF &realValue, stationaryPoints )
const QList<QPointF> stationaryPoints = findStationaryPoints( plot );
for ( const QPointF &realValue : stationaryPoints )
{
painter->setPen( QPen( Qt::black, millimetersToPixels( 1.5, painter->device() ) ) );
painter->drawPoint( toPixel( realValue ) );
......@@ -2397,7 +2397,7 @@ void View::drawHeaderTable( QPainter *painter )
text += "<br><br><b>" + i18n("Functions:") + "</b><ul>";
foreach ( Function * function, XParser::self()->m_ufkt )
for ( Function * function : qAsConst(XParser::self()->m_ufkt) )
text += "<li>" + function->name().replace( '\n', "<br>" ) + "</li>";
text += "</ul>";
......@@ -2415,11 +2415,11 @@ QList< QPointF > View::findStationaryPoints( const Plot & plot )
Plot plot2 = plot;
plot2.differentiate();
QList< double > roots = findRoots( plot2, getXmin( plot.function() ), getXmax( plot.function() ), RoughRoot );
const QList< double > roots = findRoots( plot2, getXmin( plot.function() ), getXmax( plot.function() ), RoughRoot );
plot.updateFunction();
QList< QPointF > stationaryPoints;
foreach ( double x, roots )
for ( double x : roots )
{
QPointF real = realValue( plot, x, false );
if ( real.y() >= m_ymin && real.y() <= m_ymax )
......@@ -3090,10 +3090,10 @@ QPointF View::getPlotUnderMouse()
double best_distance = 1e30; // a nice large number
QPointF best_cspos;
foreach ( Function * function, XParser::self()->m_ufkt )
for ( Function * function : qAsConst(XParser::self()->m_ufkt) )
{
const QList< Plot > plots = function->plots();
foreach ( const Plot &plot, plots )
for ( const Plot &plot : plots )
{
plot.updateFunction();
......@@ -3757,7 +3757,7 @@ QPointF View::findMinMaxValue( const Plot & plot, ExtremaType type, double dmin,
double best = (type == Maximum) ? -HUGE_VAL : +HUGE_VAL;
QPointF bestPoint;
foreach ( double root, roots )
for ( double root : qAsConst(roots) )
{
QPointF rv = realValue( plot, root, false );
if ( (type == Maximum && rv.y() > best) || (type == Minimum && rv.y() < best) )
......@@ -3958,7 +3958,7 @@ bool View::isCalculationStopped()
void View::updateSliders()
{
bool needSliderWindow = false;
foreach ( Function * it, XParser::self()->m_ufkt )
for ( Function * it : qAsConst(XParser::self()->m_ufkt) )
{
if ( it->m_parameters.useSlider && !it->allPlotsAreHidden() )
{
......
......@@ -211,14 +211,14 @@ QString XParser::findFunctionName( const QString & preferredName, int id, const
bool ok = true;
name[pos] = lastChar;
foreach ( Function * it, m_ufkt )
for ( Function * it : qAsConst(m_ufkt) )
{
if ( int(it->id()) == id )
continue;
foreach ( Equation * eq, it->eq )
for ( Equation * eq : qAsConst(it->eq) )
{
foreach ( const QString& pattern, neededPatterns) {
for ( const QString& pattern : neededPatterns) {
if ( eq->name