qt_path.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* qt_path.h - Qt <-> lib2geom path conversion
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef STUDIO_UTIL_QT_PATH_H_3FCA40F9_C398_5905_B884_1EA23BEF81F4
  18. #define STUDIO_UTIL_QT_PATH_H_3FCA40F9_C398_5905_B884_1EA23BEF81F4
  19. /**
  20. * TODO: move to separate Qt lib2geom wrapper library, perhaps?
  21. * TODO: namespace
  22. */
  23. #include <QPainterPath>
  24. #include <2geom/path-sink.h>
  25. #include <geom_helpers/knots.h>
  26. namespace rainynite::util {
  27. class QtPathSink : public Geom::PathSink {
  28. public:
  29. void moveTo(Geom::Point const& p) override {
  30. target.moveTo(p.x(), p.y());
  31. }
  32. void lineTo(Geom::Point const& p) override {
  33. target.lineTo(p.x(), p.y());
  34. }
  35. void curveTo(Geom::Point const& c0, Geom::Point const& c1, Geom::Point const& p) override {
  36. target.cubicTo(
  37. c0.x(), c0.y(),
  38. c1.x(), c1.y(),
  39. p.x(), p.y()
  40. );
  41. }
  42. void quadTo(Geom::Point const& c, Geom::Point const& p) override {
  43. target.quadTo(
  44. c.x(), c.y(),
  45. p.x(), p.y()
  46. );
  47. }
  48. void arcTo(Geom::Coord /*rx*/, Geom::Coord /*ry*/, Geom::Coord /*angle*/, bool /*large_arc*/, bool /*sweep*/, Geom::Point const& /*p*/) override {
  49. throw std::runtime_error("QtPathSink: arcs not supported");
  50. }
  51. void closePath() override {
  52. target.closeSubpath();
  53. }
  54. void flush() override {
  55. }
  56. QPainterPath get() {
  57. return target;
  58. }
  59. private:
  60. QPainterPath target;
  61. };
  62. inline QPainterPath path_to_qt(Geom::BezierKnots const& path) {
  63. QtPathSink sink;
  64. sink.feed(Geom::knots_to_path(path));
  65. return sink.get();
  66. }
  67. } // namespace rainynite::util
  68. #endif