forked from qt-creator/qt-creator
Utils: Move DiffUtils::interpolate into MathUtils
Change-Id: Iac4a21a6c760d8fbf0dce380b1a9a587a9d3468e Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -89,6 +89,7 @@ add_qtc_library(Utils
|
|||||||
listutils.h
|
listutils.h
|
||||||
macroexpander.cpp macroexpander.h
|
macroexpander.cpp macroexpander.h
|
||||||
mapreduce.h
|
mapreduce.h
|
||||||
|
mathutils.cpp mathutils.h
|
||||||
mimeutils.h
|
mimeutils.h
|
||||||
minimizableinfobars.cpp
|
minimizableinfobars.cpp
|
||||||
minimizableinfobars.h
|
minimizableinfobars.h
|
||||||
|
30
src/libs/utils/mathutils.cpp
Normal file
30
src/libs/utils/mathutils.cpp
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
// Copyright (C) 2022 The Qt Company Ltd.
|
||||||
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
||||||
|
|
||||||
|
#include "mathutils.h"
|
||||||
|
|
||||||
|
#include <QtMath>
|
||||||
|
|
||||||
|
namespace Utils::MathUtils {
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Linear interpolation:
|
||||||
|
For x = x1 it returns y1.
|
||||||
|
For x = x2 it returns y2.
|
||||||
|
*/
|
||||||
|
int interpolate(int x, int x1, int x2, int y1, int y2)
|
||||||
|
{
|
||||||
|
if (x1 == x2)
|
||||||
|
return y1; // or the middle point between y1 and y2?
|
||||||
|
if (y1 == y2)
|
||||||
|
return y1;
|
||||||
|
if (x == x1)
|
||||||
|
return y1;
|
||||||
|
if (x == x2)
|
||||||
|
return y2;
|
||||||
|
const int numerator = (y2 - y1) * x + x2 * y1 - x1 * y2;
|
||||||
|
const int denominator = x2 - x1;
|
||||||
|
return qRound((double)numerator / denominator);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Utils::Math
|
12
src/libs/utils/mathutils.h
Normal file
12
src/libs/utils/mathutils.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
// Copyright (C) 2022 The Qt Company Ltd.
|
||||||
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "utils_global.h"
|
||||||
|
|
||||||
|
namespace Utils::MathUtils {
|
||||||
|
|
||||||
|
QTCREATOR_UTILS_EXPORT int interpolate(int x, int x1, int x2, int y1, int y2);
|
||||||
|
|
||||||
|
} // namespace Utils::Math
|
@@ -187,6 +187,8 @@ Project {
|
|||||||
"macroexpander.cpp",
|
"macroexpander.cpp",
|
||||||
"macroexpander.h",
|
"macroexpander.h",
|
||||||
"mapreduce.h",
|
"mapreduce.h",
|
||||||
|
"mathutils.cpp",
|
||||||
|
"mathutils.h",
|
||||||
"mimeutils.h",
|
"mimeutils.h",
|
||||||
"minimizableinfobars.cpp",
|
"minimizableinfobars.cpp",
|
||||||
"minimizableinfobars.h",
|
"minimizableinfobars.h",
|
||||||
|
@@ -1342,17 +1342,4 @@ QList<FileData> DiffUtils::readPatch(const QString &patch, bool *ok,
|
|||||||
return fileDataList;
|
return fileDataList;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DiffUtils::interpolate(int x, int x1, int x2, int y1, int y2)
|
|
||||||
{
|
|
||||||
if (x1 == x2)
|
|
||||||
return x1;
|
|
||||||
if (x == x1)
|
|
||||||
return y1;
|
|
||||||
if (x == x2)
|
|
||||||
return y2;
|
|
||||||
const int numerator = (y2 - y1) * x + x2 * y1 - x1 * y2;
|
|
||||||
const int denominator = x2 - x1;
|
|
||||||
return qRound((double)numerator / denominator);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace DiffEditor
|
} // namespace DiffEditor
|
||||||
|
@@ -151,8 +151,6 @@ public:
|
|||||||
static QList<FileData> readPatch(const QString &patch,
|
static QList<FileData> readPatch(const QString &patch,
|
||||||
bool *ok = nullptr,
|
bool *ok = nullptr,
|
||||||
QFutureInterfaceBase *jobController = nullptr);
|
QFutureInterfaceBase *jobController = nullptr);
|
||||||
// For progress reporting
|
|
||||||
static int interpolate(int x, int x1, int x2, int y1, int y2);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace DiffEditor
|
} // namespace DiffEditor
|
||||||
|
@@ -6,7 +6,6 @@
|
|||||||
#include "diffeditorconstants.h"
|
#include "diffeditorconstants.h"
|
||||||
#include "diffeditordocument.h"
|
#include "diffeditordocument.h"
|
||||||
#include "diffeditorplugin.h"
|
#include "diffeditorplugin.h"
|
||||||
#include "diffutils.h"
|
|
||||||
|
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
@@ -26,6 +25,7 @@
|
|||||||
#include <texteditor/texteditorsettings.h>
|
#include <texteditor/texteditorsettings.h>
|
||||||
|
|
||||||
#include <utils/asynctask.h>
|
#include <utils/asynctask.h>
|
||||||
|
#include <utils/mathutils.h>
|
||||||
#include <utils/tooltip/tooltip.h>
|
#include <utils/tooltip/tooltip.h>
|
||||||
|
|
||||||
using namespace Core;
|
using namespace Core;
|
||||||
@@ -365,7 +365,7 @@ SideBySideDiffOutput SideDiffData::diffOutput(QFutureInterface<void> &fi, int pr
|
|||||||
diffText[RightSide].replace('\r', ' ');
|
diffText[RightSide].replace('\r', ' ');
|
||||||
output.side[LeftSide].diffText += diffText[LeftSide];
|
output.side[LeftSide].diffText += diffText[LeftSide];
|
||||||
output.side[RightSide].diffText += diffText[RightSide];
|
output.side[RightSide].diffText += diffText[RightSide];
|
||||||
fi.setProgressValue(DiffUtils::interpolate(++i, 0, count, progressMin, progressMax));
|
fi.setProgressValue(MathUtils::interpolate(++i, 0, count, progressMin, progressMax));
|
||||||
if (fi.isCanceled())
|
if (fi.isCanceled())
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@@ -952,7 +952,7 @@ void SideBySideDiffEditorWidget::showDiff()
|
|||||||
const QString package = output.side[side].diffText.mid(currentPos, packageSize);
|
const QString package = output.side[side].diffText.mid(currentPos, packageSize);
|
||||||
cursor.insertText(package);
|
cursor.insertText(package);
|
||||||
currentPos += package.size();
|
currentPos += package.size();
|
||||||
fi.setProgressValue(DiffUtils::interpolate(currentPos, 0, diffSize, progressMin, progressMax));
|
fi.setProgressValue(MathUtils::interpolate(currentPos, 0, diffSize, progressMin, progressMax));
|
||||||
if (fi.isCanceled())
|
if (fi.isCanceled())
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,6 @@
|
|||||||
#include "diffeditorconstants.h"
|
#include "diffeditorconstants.h"
|
||||||
#include "diffeditordocument.h"
|
#include "diffeditordocument.h"
|
||||||
#include "diffeditorplugin.h"
|
#include "diffeditorplugin.h"
|
||||||
#include "diffutils.h"
|
|
||||||
|
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
@@ -22,6 +21,7 @@
|
|||||||
#include <texteditor/texteditorsettings.h>
|
#include <texteditor/texteditorsettings.h>
|
||||||
|
|
||||||
#include <utils/asynctask.h>
|
#include <utils/asynctask.h>
|
||||||
|
#include <utils/mathutils.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
#include <utils/tooltip/tooltip.h>
|
#include <utils/tooltip/tooltip.h>
|
||||||
|
|
||||||
@@ -436,7 +436,7 @@ UnifiedDiffOutput UnifiedDiffData::diffOutput(QFutureInterface<void> &fi, int pr
|
|||||||
output.diffData.m_chunkInfo.setChunkIndex(oldBlock, blockNumber - oldBlock, j);
|
output.diffData.m_chunkInfo.setChunkIndex(oldBlock, blockNumber - oldBlock, j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fi.setProgressValue(DiffUtils::interpolate(++i, 0, count, progressMin, progressMax));
|
fi.setProgressValue(MathUtils::interpolate(++i, 0, count, progressMin, progressMax));
|
||||||
if (fi.isCanceled())
|
if (fi.isCanceled())
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@@ -511,7 +511,7 @@ void UnifiedDiffEditorWidget::showDiff()
|
|||||||
const QString package = output.diffText.mid(currentPos, packageSize);
|
const QString package = output.diffText.mid(currentPos, packageSize);
|
||||||
cursor.insertText(package);
|
cursor.insertText(package);
|
||||||
currentPos += package.size();
|
currentPos += package.size();
|
||||||
fi.setProgressValue(DiffUtils::interpolate(currentPos, 0, diffSize, firstPartMax, progressMax));
|
fi.setProgressValue(MathUtils::interpolate(currentPos, 0, diffSize, firstPartMax, progressMax));
|
||||||
if (futureInterface.isCanceled())
|
if (futureInterface.isCanceled())
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user