QtShapeWidget is a Qt widget class to display an ShapeWidget.
INCLUDEPATH += \ ../../Classes/CppQtShapeWidget SOURCES += \ ../../Classes/CppQtShapeWidget/qtshapewidget.cpp HEADERS += \ ../../Classes/CppQtShapeWidget/qtshapewidget.h OTHER_FILES += \ ../../Classes/CppQtShapeWidget/Licence.txt
//--------------------------------------------------------------------------- /* QtShapeWidget, Qt class for displaying a ShapeWidget Copyright 2011-2015 Richel Bilderbeek This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ //--------------------------------------------------------------------------- //From http://www.richelbilderbeek.nl/CppQtShapeWidget.htm //--------------------------------------------------------------------------- #ifndef QTSHAPEWIDGET_H #define QTSHAPEWIDGET_H #include <string> #include <vector> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <boost/scoped_ptr.hpp> #include <boost/signals2.hpp> #include <QWidget> #include "shape.h" //For MOC #include "shapewidget.h" //For MOC #pragma GCC diagnostic pop namespace ribi { ///QtShapeWidget manages and displays a Shape class QtShapeWidget : public QWidget { Q_OBJECT public: explicit QtShapeWidget(QWidget *parent = 0); QtShapeWidget(boost::shared_ptr<ShapeWidget> widget, QWidget *parent = 0); void paintEvent(QPaintEvent *); mutable boost::signals2::signal<void ()> m_signal_position_changed; ///Draw a shape from a Shape static void DrawShape( QPainter& painter, const int left, const int top, const int width, const int height, const Shape * const shape); ///Draw a shape from a ShapeWidget static void DrawShape( QPainter& painter, const ShapeWidget * const widget); ShapeWidget * GetWidget() const { return m_widget.get(); } const ShapeWidget * GetWidget() { return m_widget.get(); } private: boost::shared_ptr<ShapeWidget> m_widget; public: static std::string GetVersion() noexcept; static std::vector<std::string> GetVersionHistory() noexcept; }; } //~namespace ribi #endif // QTSHAPEWIDGET_H
//--------------------------------------------------------------------------- /* QtShapeWidget, Qt class for displaying a ShapeWidget Copyright 2011-2015 Richel Bilderbeek This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ //--------------------------------------------------------------------------- //From http://www.richelbilderbeek.nl/CppQtShapeWidget.htm //--------------------------------------------------------------------------- #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include "qtshapewidget.h" #include <cassert> #include <cmath> #include <boost/math/constants/constants.hpp> #include <boost/numeric/conversion/cast.hpp> #include <QMouseEvent> #include <QPainter> #include <QPolygon> #include "shape.h" #include "trace.h" #pragma GCC diagnostic pop ribi::QtShapeWidget::QtShapeWidget(QWidget *parent) : QWidget(parent), m_signal_position_changed{}, m_widget(new ShapeWidget(0,0.0)) { //m_widget->m_signal_position_changed.connect( // boost::bind( // &ribi::QtShapeWidget::repaint, // this)); } ribi::QtShapeWidget::QtShapeWidget( boost::shared_ptr<ShapeWidget> widget, QWidget *parent) : QWidget(parent), m_signal_position_changed{}, m_widget(widget) { //m_widget->m_signal_position_changed.connect( // boost::bind( // &ribi::QtShapeWidget::repaint, // this)); } void ribi::QtShapeWidget::DrawShape( QPainter& painter, const ShapeWidget * const widget) { DrawShape( painter, widget->GetLeft(), widget->GetTop(), widget->GetWidth(), widget->GetHeight(), widget->GetShape() ); } void ribi::QtShapeWidget::DrawShape( QPainter& painter, const int left, const int top, const int width, const int height, const Shape * const shape ) { const double pi = boost::math::constants::pi<double>(); const unsigned char red = shape->GetRed(); const unsigned char green = shape->GetGreen(); const unsigned char blue = shape->GetBlue(); painter.setBrush(QColor(red,green,blue)); const int n_corners = shape->GetNumberOfCorners(); if (n_corners == 0) { //Draw a circle painter.drawEllipse(left,top,width-1,height-1); return; } const double rotation = shape->GetRotation(); const double midx = boost::numeric_cast<double>(width) / 2.0; const double midy = boost::numeric_cast<double>(height) / 2.0; if (n_corners == 1) { //Draw a line const int x1 = boost::numeric_cast<int>( midx - (std::cos(rotation) * midx)); const int y1 = boost::numeric_cast<int>( midy + (std::sin(rotation) * midy)); const int x2 = boost::numeric_cast<int>( midx - (std::cos(rotation + pi) * midx)); const int y2 = boost::numeric_cast<int>( midy + (std::sin(rotation + pi) * midy)); painter.drawLine(x1,y1,x2,y2); return; } const double d_angle = 2.0 * pi / boost::numeric_cast<double>(n_corners); QPolygon polygon; for (int i=0; i!=n_corners; ++i) { const double angle = rotation + (boost::numeric_cast<double>(i) * d_angle); const int x = boost::numeric_cast<int>( midx - (std::cos(angle) * midx)); const int y = boost::numeric_cast<int>( midy + (std::sin(angle) * midy)); polygon.append(QPoint(x,y)); } painter.drawConvexPolygon(polygon); } std::string ribi::QtShapeWidget::GetVersion() noexcept { return "2.1"; } std::vector<std::string> ribi::QtShapeWidget::GetVersionHistory() noexcept { return { "2011-07-13: Version 1.0: initial version", "2011-08-08: Version 2.0: conformized architecture to MysteryMachineWidget", "2014-03-28: Version 2.1: replaced custom Rect class by Boost.Geometry" }; } void ribi::QtShapeWidget::paintEvent(QPaintEvent *) { QPainter painter(this); DrawShape( painter, 0,0,width(),height(), this->m_widget->GetShape() ); }