Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QtToggleButtonWidget

 

QtToggleButtonWidget is a Qt widget class to display an ToggleButtonWidget.

 

 

 

 

 

qttogglebuttonwidget.h

 

//---------------------------------------------------------------------------
/*
QtToggleButtonWidget, Qt widget for displaying the ToggleButtonWidget class
Copyright (C) 2011 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/CppQtToggleButtonWidget.htm
//---------------------------------------------------------------------------
#ifndef QTTOGGLEBUTTONWIDGET_H
#define QTTOGGLEBUTTONWIDGET_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include <QWidget>
//---------------------------------------------------------------------------
struct ToggleButton;
struct ToggleButtonWidget;
//---------------------------------------------------------------------------
///QtToggleButtonWidget displays a ToggleButton
struct QtToggleButtonWidget : public QWidget
{
  ///A QtToggleButtonWidget is created by its toggle state and its color
  QtToggleButtonWidget(
    const bool toggled = false,
    const unsigned char red = 255,
    const unsigned char green = 255,
    const unsigned char blue = 255);

  ///Obtain a read-only pointer to the ToggleButton
  const ToggleButtonWidget * GetWidget() const { return m_widget.get(); }

  ///Obtain a read-and-write pointer to the ToggleButton
  ToggleButtonWidget * GetWidget() { return m_widget.get(); }

  ///Signal that is emitted when a QtToggleButtonWidget is toggled
  mutable boost::signals2::signal<void ()> m_signal_toggled;

  ///Obtain the QtToggleButtonWidget its version
  static const std::string GetVersion();

  ///Obtain the QtToggleButtonWidget its version history
  static const std::vector<std::string> GetVersionHistory();

  ///Draw a ToggleButton from a ToggleButton
  static void DrawToggleButton(
    QPainter& painter,
    const int left, const int top,
    const int width, const int height,
    const ToggleButton * const toggle_button);

  ///Draw a ToggleButton from a ToggleButtonWidget
  static void DrawToggleButton(
    QPainter& painter,
    const ToggleButtonWidget * const widget);

  protected:
  ///Paint the QtToggleButtonWidget
  void paintEvent(QPaintEvent * event);

  ///Resize the QtToggleButtonWidget
  void resizeEvent(QResizeEvent *);

  private:

  ///The ToggleButton
  boost::scoped_ptr<ToggleButtonWidget> m_widget;

  ///Repaint the QtToggleButtonWidget
  void DoRepaint();

  ///Respond to mouse click
  void mousePressEvent(QMouseEvent * e);
};
//---------------------------------------------------------------------------
#endif // QTTOGGLEBUTTONWIDGET_H

 

 

 

 

 

qttogglebuttonwidget.cpp

 

//---------------------------------------------------------------------------
/*
QtToggleButtonWidget, Qt widget for displaying the ToggleButtonWidget class
Copyright (C) 2011 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/CppQtToggleButtonWidget.htm
//---------------------------------------------------------------------------
#include <iostream>
#include <boost/bind.hpp>
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include <QBrush>
#include <QMouseEvent>
#include <QPainter>
#include <QPen>
//---------------------------------------------------------------------------
#include "togglebutton.h"
#include "togglebuttonwidget.h"
#include "qttogglebuttonwidget.h"
//---------------------------------------------------------------------------
QtToggleButtonWidget::QtToggleButtonWidget(
  const bool toggled,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
  : m_widget(new ToggleButtonWidget(toggled,red,green,blue))
{
  assert(m_widget);
  m_widget->GetToggleButton()->m_signal_toggled.connect(
    boost::bind(
      &QtToggleButtonWidget::DoRepaint,
      this));

  this->resize(200,100);
  this->update();
}
//---------------------------------------------------------------------------
void QtToggleButtonWidget::DoRepaint()
{
  this->update();
}
//---------------------------------------------------------------------------
void QtToggleButtonWidget::DrawToggleButton(
  QPainter& painter,
  const int left, const int top,
  const int width, const int height,
  const ToggleButton * const button)
{
  painter.setBrush(QColor(
    button->GetRed(),
    button->GetGreen(),
    button->GetBlue()));
  //Draw base
  painter.drawArc(
    left + 0,
    top + (height * 1 / 3),
    width,
    height * 2 / 3,
    180 * 16,
    180 * 16);
  //Draw top
  painter.drawEllipse(
    left + 0,
    top + (button->IsPressed() ? (height * 1 / 3) - 2 : 0.0),
    width,
    height * 2 / 3);
  painter.drawLine(
    left,
    top + (button->IsPressed() ? (height * 2 / 3) - 2 : (height * 1 / 3)),
    left,
    top + (height * 2 / 3));
  painter.drawLine(
    left + (width),
    top + (button->IsPressed() ? (height * 2 / 3) - 2 : (height * 1 / 3)),
    left + width,
    top + (height * 2 / 3));
}
//---------------------------------------------------------------------------
void QtToggleButtonWidget::DrawToggleButton(
  QPainter& painter,
  const ToggleButtonWidget * const widget)
{
  DrawToggleButton(
    painter,
    widget->GetGeometry().GetX(),
    widget->GetGeometry().GetY(),
    widget->GetGeometry().GetWidth(),
    widget->GetGeometry().GetHeight(),
    widget->GetToggleButton());
}
//---------------------------------------------------------------------------
const std::string QtToggleButtonWidget::GetVersion()
{
  return "1.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> QtToggleButtonWidget::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("YYYY-MM-DD: version X.Y: [description]");
  v.push_back("2011-06-16: version 1.0: initial version");
  return v;
}
//---------------------------------------------------------------------------
void QtToggleButtonWidget::mousePressEvent(QMouseEvent * e)
{
  //m_widget its observers for a repaint
  m_widget->Click(e->x(),e->y());
}
//---------------------------------------------------------------------------
void QtToggleButtonWidget::paintEvent(QPaintEvent *)
{
  QPainter painter(this);
  DrawToggleButton(painter,m_widget.get());
}
//---------------------------------------------------------------------------
void QtToggleButtonWidget::resizeEvent(QResizeEvent *)
{
  this->m_widget->SetGeometry(Rect(0,0,this->width(),this->height()));
  repaint();
}
//---------------------------------------------------------------------------

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml