diff --git a/CMakeLists.txt b/CMakeLists.txt index 432657f32ef65c3433e835e6e049f946de860e64..1ce416771f7504ff94709194e5a6644bc47172e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,22 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${INCLUDE_DIR}) target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Widgets) +include(CTest) +if(BUILD_TESTING) + add_executable(curveeditor_test + tests/curveeditor_test.cpp + src/curveeditor.cpp + include/curveeditor.h + ) + target_include_directories(curveeditor_test PRIVATE ${INCLUDE_DIR}) + target_link_libraries(curveeditor_test PRIVATE Qt5::Widgets) + add_test(NAME curveeditor_test COMMAND curveeditor_test) + set_tests_properties(curveeditor_test PROPERTIES + ENVIRONMENT "QT_QPA_PLATFORM=offscreen" + TIMEOUT 10 + ) +endif() + qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES}) install(TARGETS ${PROJECT_NAME} DESTINATION bin) diff --git a/include/curveeditor.h b/include/curveeditor.h index 1092ea75943ae9a2de12a588fb88c9e849790804..3b97eebf173490173a540a93d0d5034fac3ca675 100644 --- a/include/curveeditor.h +++ b/include/curveeditor.h @@ -111,14 +111,14 @@ private: QRect m_contentRect; static constexpr int POINT_RADIUS = 6; - int gridWidth; + int gridWidth = 10; - int gridHeight; + int gridHeight = 10; - int xTickMin; - int xTickMax; - int yTickMin; - int yTickMax; + int xTickMin = 0; + int xTickMax = 100; + int yTickMin = 0; + int yTickMax = 100; std::string xTickTag; std::string yTickTag; @@ -126,12 +126,12 @@ private: /** * @brief 网格X轴刻度步长 */ - int xTickStep; + int xTickStep = 10; /** * @brief 网格Y轴刻度步长 */ - int yTickStep; + int yTickStep = 10; }; -#endif // CURVEEDITOR_H \ No newline at end of file +#endif // CURVEEDITOR_H diff --git a/src/curveeditor.cpp b/src/curveeditor.cpp index 2c6e679dfe59c8b44972f5396dc305a3591da063..9510f2baec2bb477a92c2d0d733118ed7c12e1af 100644 --- a/src/curveeditor.cpp +++ b/src/curveeditor.cpp @@ -42,13 +42,13 @@ QVector CurveEditor::getCurrentPoints() const void CurveEditor::setGridWidth(int width) { - gridWidth = width; + gridWidth = qMax(1, width); update(); } void CurveEditor::setGridHeight(int height) { - gridHeight = height; + gridHeight = qMax(1, height); update(); } @@ -66,27 +66,27 @@ void CurveEditor::setYTickTag(std::string tag) void CurveEditor::setYTickStep(int step) { - yTickStep = step; + yTickStep = qMax(1, step); update(); } void CurveEditor::setXTickStep(int step) { - xTickStep = step; + xTickStep = qMax(1, step); update(); } void CurveEditor::setYTickRange(int min, int max) { - yTickMin = min; - yTickMax = max; + yTickMin = qMin(min, max); + yTickMax = qMax(min, max); update(); } void CurveEditor::setXTickRange(int min, int max) { - xTickMin = min; - xTickMax = max; + xTickMin = qMin(min, max); + xTickMax = qMax(min, max); update(); } @@ -349,4 +349,4 @@ void CurveEditor::validateCurve() for (int i = 0; i < m_points.size(); ++i) { adjustPointPosition(i); } -} \ No newline at end of file +} diff --git a/tests/curveeditor_test.cpp b/tests/curveeditor_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8fe4d8b36c442d8ae7f29273796f4a13281a1f1 --- /dev/null +++ b/tests/curveeditor_test.cpp @@ -0,0 +1,39 @@ +#include "curveeditor.h" + +#include +#include +#include + +#include + +namespace { + +bool renderWidget(CurveEditor &editor) +{ + QImage image(editor.size(), QImage::Format_ARGB32_Premultiplied); + image.fill(Qt::transparent); + QPainter painter(&image); + editor.render(&painter); + return !image.isNull(); +} + +} // namespace + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + CurveEditor editor; + if (!renderWidget(editor)) { + return EXIT_FAILURE; + } + + editor.setGridWidth(0); + editor.setGridHeight(-1); + editor.setXTickStep(0); + editor.setYTickStep(-10); + editor.setXTickRange(100, 0); + editor.setYTickRange(100, 0); + + return renderWidget(editor) ? EXIT_SUCCESS : EXIT_FAILURE; +}