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



ToggleButton is a class for a toggle button.
Technical facts
./CppToggleButton/CppToggleButton.pri
INCLUDEPATH += \ ../../Classes/CppToggleButton SOURCES += \ ../../Classes/CppToggleButton/togglebutton.cpp HEADERS += \ ../../Classes/CppToggleButton/togglebutton.h OTHER_FILES += \ ../../Classes/CppToggleButton/Licence.txt
./CppToggleButton/togglebutton.h
//--------------------------------------------------------------------------- /* ToggleButton, toggle button 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/CppToggleButton.htm //--------------------------------------------------------------------------- #ifndef TOGGLEBUTTON_H #define TOGGLEBUTTON_H #include <string> #include <vector> #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 <boost/checked_delete.hpp> #include <boost/signals2.hpp> #pragma GCC diagnostic pop namespace ribi { ///ToggleButton is a class for a toggle button struct ToggleButton { explicit ToggleButton( const bool pressed = false, const unsigned char red = 255, const unsigned char green = 255, const unsigned char blue = 255) noexcept; ///Get the blueness of the ToggleButton's surface unsigned char GetBlue() const noexcept { return m_blue; } ///Get the greenness of the ToggleButton's surface unsigned char GetGreen() const noexcept { return m_green; } ///Get the redness of the ToggleButton's surface unsigned char GetRed() const noexcept { return m_red; } ///Returns if the ToggleButton is pressed bool IsPressed() const noexcept { return m_pressed; } ///Press the ToggleButton void Press() noexcept; ///Release/'unpress' the ToggleButton void Release() noexcept; ///Set the ToggleButton its color void SetColor( const unsigned char red, const unsigned char green, const unsigned char blue) noexcept; ///Toggle the ToggleButton void Toggle() noexcept; ///The signal that is emitted when ToggleButton changes color mutable boost::signals2::signal<void ()> m_signal_color_changed; ///The signal that is emitted when ToggleButton changes state mutable boost::signals2::signal<void ()> m_signal_toggled; private: //ToggleButton can only be deleted by Boost smart pointers virtual ~ToggleButton() noexcept {} //ToggleButton can only be deleted by Boost smart pointers friend void boost::checked_delete<>(ToggleButton*); ///Is the toggle button pressed? bool m_pressed; ///The redness of the ToggleButton's surface unsigned char m_red; ///The greenness of the ToggleButton's surface unsigned char m_green; ///The blueness of the ToggleButton's surface unsigned char m_blue; friend std::ostream& operator<<(std::ostream& os, const ToggleButton& button) noexcept; public: static std::string GetVersion() noexcept; static std::vector<std::string> GetVersionHistory() noexcept; }; std::ostream& operator<<(std::ostream& os, const ToggleButton& button) noexcept; } //~namespace ribi #endif // TOGGLEBUTTON_H
./CppToggleButton/togglebutton.cpp
//--------------------------------------------------------------------------- /* ToggleButton, toggle button 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/CppToggleButton.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 "togglebutton.h" #include <cassert> //#include <cmath> //#include <boost/math/constants/constants.hpp> #include "trace.h" #pragma GCC diagnostic pop ribi::ToggleButton::ToggleButton( const bool pressed, const unsigned char red, const unsigned char green, const unsigned char blue) noexcept : m_signal_color_changed{}, m_signal_toggled{}, m_pressed(pressed), m_red(red), m_green(green), m_blue(blue) { } std::string ribi::ToggleButton::GetVersion() noexcept { return "1.2"; } std::vector<std::string> ribi::ToggleButton::GetVersionHistory() noexcept { return { "2011-04-11: Version 1.0: initial version", "2011-08-20: Version 1.1: added operator<<", "2011-08-31: Version 1.2: added setting the color of a ToggleButton" }; } void ribi::ToggleButton::Press() noexcept { if (!m_pressed) { Toggle(); } } void ribi::ToggleButton::Release() noexcept { if (m_pressed) { Toggle(); } } void ribi::ToggleButton::SetColor( const unsigned char red, const unsigned char green, const unsigned char blue) noexcept { if (red != m_red || green != m_green || blue != m_blue) { m_red = red; m_green = green; m_blue = blue; m_signal_color_changed(); } } void ribi::ToggleButton::Toggle() noexcept { m_pressed = !m_pressed; m_signal_toggled(); } std::ostream& ribi::operator<<(std::ostream& os, const ToggleButton& button) noexcept { os << "<ToggleButton>" << "<blue>" << static_cast<int>(button.m_blue) << "</blue>" << "<green>" << static_cast<int>(button.m_green) << "</green>" << "<pressed>" << button.m_pressed << "</pressed>" << "<red>" << static_cast<int>(button.m_red) << "</red>" << "</ToggleButton>"; return os; }