(C++) QtMysteryMachineWidget
January 25, 2018 · View on GitHub
(C++) QtMysteryMachineWidget



QtMysteryMachineWidget is a Qt class to display an CppMysteryMachineWidget.
Technical facts
./CppQtMysteryMachineWidget/CppQtMysteryMachineWidget.pri
INCLUDEPATH += \ ../../Classes/CppQtMysteryMachineWidget SOURCES += \ ../../Classes/CppQtMysteryMachineWidget/qtmysterymachinewidget.cpp HEADERS += \ ../../Classes/CppQtMysteryMachineWidget/qtmysterymachinewidget.h OTHER_FILES += \ ../../Classes/CppQtMysteryMachineWidget/Licence.txt
./CppQtMysteryMachineWidget/qtmysterymachinewidget.h
//--------------------------------------------------------------------------- /* QtMysteryMachineWidget, Qt widget for displaying the MysteryMachine class 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/CppQtMysteryMachineWidget.htm //--------------------------------------------------------------------------- #ifndef QTMYSTERYMACHINEWIDGET_H #define QTMYSTERYMACHINEWIDGET_H #include <string> #include <vector> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <boost/signals2.hpp> #include <QWidget> #include "dial.h" //For MOC #include "dialwidget.h" //For MOC #include "led.h" //For MOC #include "ledwidget.h" //For MOC #include "mysterymachine.h" //For MOC #include "mysterymachinewidget.h" //For MOC #include "togglebutton.h" //For MOC #include "togglebuttonwidget.h" //For MOC #pragma GCC diagnostic pop namespace ribi { struct QtToggleButtonWidget; ///QtMysteryMachineWidget displays a MysteryMachine struct QtMysteryMachineWidget : public QWidget { Q_OBJECT public: explicit QtMysteryMachineWidget(QWidget *parent = 0); ///A QtMysteryMachineWidget is created by its width and height QtMysteryMachineWidget(const int width, const int height, QWidget *parent = 0); ///Obtain a read-only pointer to the MysteryMachine const MysteryMachineWidget * GetWidget() const { return m_widget.get(); } ///Obtain a read-and-write pointer to the MysteryMachine MysteryMachineWidget * GetWidget() { return m_widget.get(); } ///Signal that is emitted when a QtMysteryMachineWidget is toggled mutable boost::signals2::signal<void ()> m_signal_changed; ///Obtain the QtMysteryMachineWidget its version static std::string GetVersion() noexcept; ///Obtain the QtMysteryMachineWidget its version history static std::vector<std::string> GetVersionHistory() noexcept; protected: ///Respond to mouse clicks void mousePressEvent(QMouseEvent *); ///Paint the QtMysteryMachineWidget void paintEvent(QPaintEvent *); ///Resize the QtMysteryMachineWidget void resizeEvent(QResizeEvent *); private: ///The MysteryMachine boost::scoped_ptr<MysteryMachineWidget> m_widget; ///Repaint the QtMysteryMachineWidget void DoRepaint(); ///Respond to mouse click //void OnClicked(const Wt::WMouseEvent& e); }; } //~namespace ribi #endif // WTMYSTERYMACHINEWIDGET_H
./CppQtMysteryMachineWidget/qtmysterymachinewidget.cpp
//--------------------------------------------------------------------------- /* QtMysteryMachineWidget, Wt widget for displaying the MysteryMachine class 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/CppQtMysteryMachineWidget.htm //--------------------------------------------------------------------------- #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #pragma GCC diagnostic ignored "-Wunused-but-set-parameter" #include "qtmysterymachinewidget.h" #include <iostream> #include <boost/bind.hpp> #include <boost/numeric/conversion/cast.hpp> #include <QMouseEvent> #include <QPainter> #include "dial.h" #include "dialwidget.h" #include "led.h" #include "ledwidget.h" #include "togglebutton.h" #include "togglebuttonwidget.h" //#include "trace.h" #include "mysterymachine.h" #include "mysterymachinewidget.h" #include "qtdialwidget.h" #include "qtledwidget.h" #include "qttogglebuttonwidget.h" #pragma GCC diagnostic pop ribi::QtMysteryMachineWidget::QtMysteryMachineWidget(QWidget *parent) : QWidget(parent), m_signal_changed{}, m_widget(new MysteryMachineWidget( Widget::CreateRect(0,0,200,400))) { assert(m_widget); m_widget->GetMachine()->GetDialBack()->GetDial()->m_signal_position_changed.connect(boost::bind( &ribi::QtMysteryMachineWidget::DoRepaint,this)); m_widget->GetMachine()->GetDialFront()->GetDial()->m_signal_position_changed.connect(boost::bind( &ribi::QtMysteryMachineWidget::DoRepaint,this)); m_widget->GetMachine()->GetToggleButton()->GetToggleButton()->m_signal_toggled.connect(boost::bind( &ribi::QtMysteryMachineWidget::DoRepaint,this)); m_widget->m_signal_geometry_changed.connect( boost::bind( &ribi::QtMysteryMachineWidget::DoRepaint, this)); resize(200,400); } ribi::QtMysteryMachineWidget::QtMysteryMachineWidget( const int width, const int height, QWidget *parent) : QWidget(parent), m_signal_changed{}, m_widget{new MysteryMachineWidget( Widget::CreateRect(0,0,width,height)) } { assert(m_widget); m_widget->GetMachine()->GetDialBack()->GetDial()->m_signal_position_changed.connect(boost::bind( &ribi::QtMysteryMachineWidget::DoRepaint,this)); m_widget->GetMachine()->GetDialFront()->GetDial()->m_signal_position_changed.connect(boost::bind( &ribi::QtMysteryMachineWidget::DoRepaint,this)); m_widget->GetMachine()->GetToggleButton()->GetToggleButton()->m_signal_toggled.connect(boost::bind( &ribi::QtMysteryMachineWidget::DoRepaint,this)); m_widget->m_signal_geometry_changed.connect( boost::bind( &ribi::QtMysteryMachineWidget::DoRepaint, this)); resize(width,height); } void ribi::QtMysteryMachineWidget::DoRepaint() { this->repaint(); } std::string ribi::QtMysteryMachineWidget::GetVersion() noexcept { return "2.1"; } std::vector<std::string> ribi::QtMysteryMachineWidget::GetVersionHistory() noexcept { return { "2011-07-04: version 1.0: initial version", "2011-09-15: version 2.0: made QtMysteryMachineWidget same as WtMysteryMachineWidget", "2014-03-28: version 2.1: replaced Rect by Boost.Geometry its box class" }; } void ribi::QtMysteryMachineWidget::mousePressEvent(QMouseEvent * e) { m_widget->Click(e->x(),e->y()); this->m_signal_changed(); } void ribi::QtMysteryMachineWidget::paintEvent(QPaintEvent *) { QPainter painter(this); QtDialWidget::DrawDial(painter,GetWidget()->GetMachine()->GetDialBack()); QtDialWidget::DrawDial(painter,GetWidget()->GetMachine()->GetDialFront()); QtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedBack1()); QtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedBack2()); QtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedBack3()); QtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedFront1()); QtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedFront2()); QtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedFront3()); QtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedTopBack()); QtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedTopFront()); QtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedTopMiddle()); QtToggleButtonWidget::DrawToggleButton(painter,GetWidget()->GetMachine()->GetToggleButton()); } void ribi::QtMysteryMachineWidget::resizeEvent(QResizeEvent *) { m_widget->SetGeometry(0,0,width(),height()); }