Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) TestCanvas

 

TestCanvas is a tool to test the Canvas class.

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./ToolTestCanvas/ToolTestCanvasConsole.pro

 

include(../../ConsoleApplication.pri)

include(../../Libraries/Boost.pri)

include(../../Classes/CppAbout/CppAbout.pri)
include(../../Classes/CppFileIo/CppFileIo.pri)
include(../../Classes/CppHelp/CppHelp.pri)
include(../../Classes/CppMenuDialog/CppMenuDialog.pri)
include(../../Classes/CppRichelBilderbeekProgram/CppRichelBilderbeekProgram.pri)
include(../../Classes/CppTrace/CppTrace.pri)

#Specific
include(../../Tools/ToolTestCanvas/ToolTestCanvasConsole.pri)
include(../../Classes/CppCanvas/CppCanvas.pri)
include(../../Classes/CppDotMatrix/CppDotMatrix.pri)

SOURCES += main.cpp

 

 

 

 

 

Qt project file: ./ToolTestCanvas/ToolTestCanvasDesktop.pro

 

include(../../DesktopApplication.pri)

#Libs
include(../../Libraries/Boost.pri)

#Console
include(../../Classes/CppAbout/CppAbout.pri)
include(../../Classes/CppFileIo/CppFileIo.pri)
include(../../Classes/CppHelp/CppHelp.pri)
include(../../Classes/CppMenuDialog/CppMenuDialog.pri)
include(../../Classes/CppRichelBilderbeekProgram/CppRichelBilderbeekProgram.pri)
include(../../Classes/CppTrace/CppTrace.pri)

#Desktop
include(../../Classes/CppQtAboutDialog/CppQtAboutDialog.pri)
include(../../Classes/CppQtHideAndShowDialog/CppQtHideAndShowDialog.pri)

#Specific for this application
#Console
include(../../Classes/CppCanvas/CppCanvas.pri)
#Desktop
include(../../Tools/ToolTestCanvas/ToolTestCanvasDesktop.pri)

SOURCES += qtmain.cpp

 

 

 

 

 

./ToolTestCanvas/ToolTestCanvasConsole.pri

 

INCLUDEPATH += \
    ../../Tools/ToolTestCanvas

SOURCES += \
    ../../Tools/ToolTestCanvas/tooltestcanvasmenudialog.cpp

HEADERS += \
    ../../Tools/ToolTestCanvas/tooltestcanvasmenudialog.h

OTHER_FILES += \
    ../../Tools/ToolTestCanvas/Licence.txt

 

 

 

 

 

./ToolTestCanvas/ToolTestCanvasDesktop.pri

 

include(../../Tools/ToolTestCanvas/ToolTestCanvasConsole.pri)

RESOURCES += \
    ../../Tools/ToolTestCanvas/ToolTestCanvas.qrc

FORMS += \
    ../../Tools/ToolTestCanvas/qttooltestcanvasmenudialog.ui \
    ../../Tools/ToolTestCanvas/qttooltestcanvasmaindialog.ui

SOURCES += \
    ../../Tools/ToolTestCanvas/qttooltestcanvasmenudialog.cpp \
    ../../Tools/ToolTestCanvas/qttooltestcanvasmaindialog.cpp

HEADERS += \
    ../../Tools/ToolTestCanvas/qttooltestcanvasmenudialog.h \
    ../../Tools/ToolTestCanvas/qttooltestcanvasmaindialog.h

 

 

 

 

 

./ToolTestCanvas/main.cpp

 

#include "tooltestcanvasmenudialog.h"
#include "trace.h"

int main(int argc, char* argv[])
{
  START_TRACE();
  const std::vector<std::string> args { ribi::MenuDialog::ConvertArguments(argc,argv) };
  return ribi::ToolTestCanvasMenuDialog().Execute(args);
}

 

 

 

 

 

./ToolTestCanvas/qtmain.cpp

 

//---------------------------------------------------------------------------
/*
ToolTestCanvas, tests the Canvas class
Copyright (C) 2013  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/ToolTestCanvas.htm
//---------------------------------------------------------------------------
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QApplication>

#include "qttooltestcanvasmenudialog.h"
#include "trace.h"
#pragma GCC diagnostic pop

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  START_TRACE();
  ribi::QtToolTestCanvasMenuDialog d;
  d.show();
  return a.exec();
}

 

 

 

 

 

./ToolTestCanvas/qttooltestcanvasmaindialog.h

 

#ifndef QTTOOLTESTCANVASMAINDIALOG_H
#define QTTOOLTESTCANVASMAINDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <boost/shared_ptr.hpp>
#include "qthideandshowdialog.h"
#pragma GCC diagnostic pop

namespace Ui {
  class QtToolTestCanvasMainDialog;
}

namespace ribi {

struct Canvas;

class QtToolTestCanvasMainDialog : public QtHideAndShowDialog
{
  Q_OBJECT
  
public:
  explicit QtToolTestCanvasMainDialog(QWidget *parent = 0);
  QtToolTestCanvasMainDialog(const QtToolTestCanvasMainDialog&) = delete;
  QtToolTestCanvasMainDialog& operator=(const QtToolTestCanvasMainDialog&) = delete;
  ~QtToolTestCanvasMainDialog() noexcept;
  
private slots:

  void on_box_color_system_currentIndexChanged(int index);

  void on_box_coordinat_system_currentIndexChanged(int index);

  void on_button_dot_clicked();

  void on_button_clear_clicked();

  void on_button_circle_clicked();

  void on_button_line_clicked();

private:
  Ui::QtToolTestCanvasMainDialog *ui;
  boost::shared_ptr<Canvas> m_canvas;

  static const boost::shared_ptr<Canvas> CreateCanvas();
  void ShowCanvas(const ribi::Canvas * const canvas = nullptr);

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // QTTOOLTESTCANVASMAINDIALOG_H

 

 

 

 

 

./ToolTestCanvas/qttooltestcanvasmaindialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include "qttooltestcanvasmaindialog.h"

#include <cassert>
#include <sstream>

#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>

#include "canvas.h"
#include "trace.h"
#include "ui_qttooltestcanvasmaindialog.h"
#pragma GCC diagnostic pop

ribi::QtToolTestCanvasMainDialog::QtToolTestCanvasMainDialog(QWidget *parent) :
  QtHideAndShowDialog(parent),
  ui(new Ui::QtToolTestCanvasMainDialog),
  m_canvas(CreateCanvas())
{
  #ifndef NDEBUG
  Test();
  #endif
  ui->setupUi(this);

  m_canvas->m_signal_changed.connect(
    boost::bind(
      &ribi::QtToolTestCanvasMainDialog::ShowCanvas,this,
      boost::lambda::_1)
    );

  {
    const double w = m_canvas->GetWidth();
    const double h = m_canvas->GetHeight();
    ui->box_circle_x->setValue(0.5 * w);
    ui->box_circle_y->setValue(0.5 * h);
    ui->box_circle_r->setValue(0.4 * h);
    ui->box_dot_x->setValue(0.5 * w);
    ui->box_dot_y->setValue(0.5 * h);
    ui->box_line_x1->setValue(0.25 * w);
    ui->box_line_y1->setValue(0.25 * h);
    ui->box_line_x2->setValue(0.75 * w);
    ui->box_line_y2->setValue(0.75 * h);
  }

  ShowCanvas(0);
}

ribi::QtToolTestCanvasMainDialog::~QtToolTestCanvasMainDialog() noexcept
{
  delete ui;
}

const boost::shared_ptr<ribi::Canvas> ribi::QtToolTestCanvasMainDialog::CreateCanvas()
{
  const int maxx = 79;
  const int maxy = 23;
  boost::shared_ptr<ribi::Canvas> canvas(new Canvas(maxx,maxy));
  //Determine and calculate dimensions and coordinats of smiley
  const double maxxD = static_cast<double>(maxx);
  const double maxyD = static_cast<double>(maxy);
  const double midX        = 0.50 * maxxD;
  const double midY        = 0.50 * maxyD;
  const double headRay     = 0.50 * maxyD;
  const double eyeLeftX    = 0.50 * maxxD - (0.35 * headRay) ;
  const double eyeLeftY    = 0.50 * maxyD - (0.25 * headRay) ;
  const double eyeRightX   = 0.50 * maxxD + (0.35 * headRay) ;
  const double eyeRightY   = 0.50 * maxyD - (0.25 * headRay) ;
  const double eyeRay      = 0.30 * headRay;
  const double mouthLeftX  = 0.50 * maxxD - (0.7 * headRay) ;
  const double mouthMidX   = 0.50 * maxxD;
  const double mouthRightX = 0.50 * maxxD + (0.7 * headRay) ;
  const double mouthLeftY  = 0.50 * maxyD + (0.2 * headRay) ;
  const double mouthMidY   = 0.50 * maxyD + (0.7 * headRay) ;
  const double mouthRightY = 0.50 * maxyD + (0.2 * headRay) ;
  //Draw the image on Canvas
  canvas->DrawCircle(midX, midY, headRay);
  canvas->DrawCircle(eyeLeftX, eyeLeftY, eyeRay);
  canvas->DrawDot(eyeLeftX, eyeLeftY);
  canvas->DrawCircle(eyeRightX, eyeRightY, eyeRay);
  canvas->DrawDot(eyeRightX, eyeRightY);
  canvas->DrawLine(mouthLeftX, mouthLeftY, mouthMidX, mouthMidY);
  canvas->DrawLine(mouthMidX, mouthMidY, mouthRightX, mouthRightY);
  canvas->DrawLine(mouthRightX, mouthRightY, mouthLeftX, mouthLeftY);
  return canvas;
}


void ribi::QtToolTestCanvasMainDialog::on_box_color_system_currentIndexChanged(int )
{
  const Canvas::ColorSystem color_system
    = ui->box_color_system->currentIndex() == 0
    ? Canvas::ColorSystem::normal
    : Canvas::ColorSystem::invert;
  this->m_canvas->SetColorSystem(color_system);
  //Should redraw automatically
}

void ribi::QtToolTestCanvasMainDialog::on_box_coordinat_system_currentIndexChanged(int )
{
  const Canvas::CoordinatSystem coordinat_system
    = ui->box_coordinat_system->currentIndex() == 0
    ? Canvas::CoordinatSystem::screen : Canvas::CoordinatSystem::graph;
  this->m_canvas->SetCoordinatSystem(coordinat_system);
  //Should redraw automatically
}

void ribi::QtToolTestCanvasMainDialog::ShowCanvas(const ribi::Canvas * const)
{
  //Display the image
  std::stringstream s;
  s << (*m_canvas);
  ui->text->setPlainText(s.str().c_str());

}

void ribi::QtToolTestCanvasMainDialog::on_button_clear_clicked()
{
  m_canvas->Clear();
  //Should redraw automatically
}

void ribi::QtToolTestCanvasMainDialog::on_button_circle_clicked()
{
  m_canvas->DrawCircle(
    ui->box_circle_x->value(),
    ui->box_circle_y->value(),
    ui->box_circle_r->value()
  );
}

void ribi::QtToolTestCanvasMainDialog::on_button_dot_clicked()
{
   m_canvas->DrawDot(
     ui->box_dot_x->value(),
     ui->box_dot_y->value()
   );
}

void ribi::QtToolTestCanvasMainDialog::on_button_line_clicked()
{
  m_canvas->DrawLine(
    ui->box_line_x1->value(),
    ui->box_line_y1->value(),
    ui->box_line_x2->value(),
    ui->box_line_y2->value()
  );
}

#ifndef NDEBUG
void ribi::QtToolTestCanvasMainDialog::Test() noexcept
{
  {
    static bool is_tested = false;
    if (is_tested) return;
    is_tested = true;
  }
  TRACE("Starting ribi::QtToolTestCanvasMainDialog::Test");
  TRACE("Finished ribi::QtToolTestCanvasMainDialog::Test successfully");
}
#endif

 

 

 

 

 

./ToolTestCanvas/qttooltestcanvasmenudialog.h

 

#ifndef QTTOOLTESTCANVASMENUDIALOG_H
#define QTTOOLTESTCANVASMENUDIALOG_H

#include "qthideandshowdialog.h"

namespace Ui {
  class QtToolTestCanvasMenuDialog;
}

namespace ribi {

class QtToolTestCanvasMenuDialog : public QtHideAndShowDialog
{
  Q_OBJECT

public:
  explicit QtToolTestCanvasMenuDialog(QWidget *parent = 0);
  QtToolTestCanvasMenuDialog(const QtToolTestCanvasMenuDialog&) = delete;
  QtToolTestCanvasMenuDialog& operator=(const QtToolTestCanvasMenuDialog&) = delete;
  ~QtToolTestCanvasMenuDialog() noexcept;

protected:
  void keyPressEvent(QKeyEvent * event);

private:
  Ui::QtToolTestCanvasMenuDialog *ui;

private slots:
  void on_button_about_clicked();
  void on_button_quit_clicked();
  void on_button_start_canvas_clicked();

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // QTTOOLTESTCANVASMENUDIALOG_H

 

 

 

 

 

./ToolTestCanvas/qttooltestcanvasmenudialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include "qttooltestcanvasmenudialog.h"

#include <QDesktopWidget>
#include <QKeyEvent>

#include "tooltestcanvasmenudialog.h"
#include "qtaboutdialog.h"
#include "qttooltestcanvasmaindialog.h"
#include "qthideandshowdialog.h"
#include "trace.h"
#include "ui_qttooltestcanvasmenudialog.h"
#pragma GCC diagnostic pop

ribi::QtToolTestCanvasMenuDialog::QtToolTestCanvasMenuDialog(
  QWidget *parent)
  : QtHideAndShowDialog(parent),
    ui(new Ui::QtToolTestCanvasMenuDialog)
{
  #ifndef NDEBUG
  Test();
  #endif
  ui->setupUi(this);
}

ribi::QtToolTestCanvasMenuDialog::~QtToolTestCanvasMenuDialog() noexcept
{
  delete ui;
}

void ribi::QtToolTestCanvasMenuDialog::keyPressEvent(QKeyEvent * event)
{
  if (event->key() == Qt::Key_Escape) { close(); return; }
}

void ribi::QtToolTestCanvasMenuDialog::on_button_about_clicked()
{
  About a = ToolTestCanvasMenuDialog().GetAbout();
  a.AddLibrary("QtHideAndShowDialog version: " + QtHideAndShowDialog::GetVersion());
  QtAboutDialog d(a);
  d.setWindowIcon(this->windowIcon());
  d.setStyleSheet(this->styleSheet());
  this->ShowChild(&d);
}

void ribi::QtToolTestCanvasMenuDialog::on_button_quit_clicked()
{
  close();
}

void ribi::QtToolTestCanvasMenuDialog::on_button_start_canvas_clicked()
{
  QtToolTestCanvasMainDialog d;
  d.setWindowIcon(this->windowIcon());
  //d.setStyleSheet(this->styleSheet());
  ShowChild(&d);
}

#ifndef NDEBUG
void ribi::QtToolTestCanvasMenuDialog::Test() noexcept
{
  {
    static bool is_tested = false;
    if (is_tested) return;
    is_tested = true;
  }
  TRACE("Starting ribi::QtToolTestCanvasMenuDialog::Test");
  QtToolTestCanvasMainDialog();
  TRACE("Finished ribi::QtToolTestCanvasMenuDialog::Test successfully");
}
#endif

 

 

 

 

 

./ToolTestCanvas/tooltestcanvasmenudialog.h

 

#ifndef TOOLTESTCANVASMENUDIALOG_H
#define TOOLTESTCANVASMENUDIALOG_H

#include "menudialog.h"

namespace ribi {

///GUI independent ToolTestCanvas menu dialog
struct ToolTestCanvasMenuDialog : public MenuDialog
{
  const About GetAbout() const noexcept;
  const Help GetHelp() const noexcept;
  const boost::shared_ptr<const Program> GetProgram() const noexcept;
  const std::string GetVersion() const noexcept;
  const std::vector<std::string> GetVersionHistory() const noexcept;

  private:
  int ExecuteSpecific(const std::vector<std::string>& argv) noexcept;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // TOOLTESTCANVASMENUDIALOG_H

 

 

 

 

 

./ToolTestCanvas/tooltestcanvasmenudialog.cpp

 

#include "tooltestcanvasmenudialog.h"

#include <cassert>
#include <iostream>

#include "canvas.h"
#include "trace.h"

int ribi::ToolTestCanvasMenuDialog::ExecuteSpecific(const std::vector<std::string>& argv) noexcept
{
  #ifndef NDEBUG
  Test();
  #endif
  const int argc = static_cast<int>(argv.size());
  if (argc != 1)
  {
    std::cout << GetHelp() << '\n';
    return 1;
  }
  //Specify the dimensions of the canvas equal to the screen
  //Coordinat (0,0) is the top-left coordinat
  //Coordinat (79,23) is the bottom-right coordinat
  const int maxx = 79;
  const int maxy = 23;

  for (int i=0; i!=4; ++i)
  {
    const ribi::Canvas::ColorSystem color_system
      = i % 2 ? ribi::Canvas::ColorSystem::normal : ribi::Canvas::ColorSystem::invert;
    const ribi::Canvas::CoordinatSystem coordinat_system
      = i / 2 ? ribi::Canvas::CoordinatSystem::graph : ribi::Canvas::CoordinatSystem::screen;
    ribi::Canvas c( maxx, maxy, color_system, coordinat_system);

    //Draw smiley to Canvas

    //Determine and calculate dimensions and coordinats of smiley
    const double maxxD = static_cast<double>(maxx);
    const double maxyD = static_cast<double>(maxy);
    const double midX        = 0.50 * maxxD;
    const double midY        = 0.50 * maxyD;
    const double headRay     = 0.50 * maxyD;
    const double eyeLeftX    = 0.50 * maxxD - (0.35 * headRay) ;
    const double eyeLeftY    = 0.50 * maxyD - (0.25 * headRay) ;
    const double eyeRightX   = 0.50 * maxxD + (0.35 * headRay) ;
    const double eyeRightY   = 0.50 * maxyD - (0.25 * headRay) ;
    const double eyeRay      = 0.30 * headRay;
    const double mouthLeftX  = 0.50 * maxxD - (0.7 * headRay) ;
    const double mouthMidX   = 0.50 * maxxD;
    const double mouthRightX = 0.50 * maxxD + (0.7 * headRay) ;
    const double mouthLeftY  = 0.50 * maxyD + (0.2 * headRay) ;
    const double mouthMidY   = 0.50 * maxyD + (0.7 * headRay) ;
    const double mouthRightY = 0.50 * maxyD + (0.2 * headRay) ;
    //Draw the image on Canvas
    c.DrawCircle(midX, midY, headRay);
    c.DrawCircle(eyeLeftX, eyeLeftY, eyeRay);
    c.DrawDot(eyeLeftX, eyeLeftY);
    c.DrawCircle(eyeRightX, eyeRightY, eyeRay);
    c.DrawDot(eyeRightX, eyeRightY);
    c.DrawLine(mouthLeftX, mouthLeftY, mouthMidX, mouthMidY);
    c.DrawLine(mouthMidX, mouthMidY, mouthRightX, mouthRightY);
    c.DrawLine(mouthRightX, mouthRightY, mouthLeftX, mouthLeftY);
    //Display the image
    std::cout << c;
    std::cout << std::endl;
  }
  return 0;
}

const ribi::About ribi::ToolTestCanvasMenuDialog::GetAbout() const noexcept
{
  About a(
    "Richel Bilderbeek",
    "ToolTestCanvas",
    "tests the Canvas class",
    "the 28th of August 2013",
    "2013-2014",
    "http://www.richelbilderbeek.nl/ToolTestCanvas.htm",
    GetVersion(),
    GetVersionHistory());
  a.AddLibrary("Canvas version: " + Canvas::GetVersion());
  a.AddLibrary("Trace version: " + Trace::GetVersion());
  return a;
}

const ribi::Help ribi::ToolTestCanvasMenuDialog::GetHelp() const noexcept
{
  return Help(
    this->GetAbout().GetFileTitle(),
    this->GetAbout().GetFileDescription(),
    {

    },
    {

    }
  );
}

const boost::shared_ptr<const ribi::Program> ribi::ToolTestCanvasMenuDialog::GetProgram() const noexcept
{
  const boost::shared_ptr<const Program> p {
    new ProgramTestCanvas
  };
  assert(p);
  return p;
}

const std::string ribi::ToolTestCanvasMenuDialog::GetVersion() const noexcept
{
  return "1.1";
}

const std::vector<std::string> ribi::ToolTestCanvasMenuDialog::GetVersionHistory() const noexcept
{
  return {
    "2013-08-28: version 1.0: initial desktop version",
    "2013-11-05: version 1.1: conformized for ProjectRichelBilderbeekConsole"
  };
}

#ifndef NDEBUG
void ribi::ToolTestCanvasMenuDialog::Test() noexcept
{
  {
    static bool is_tested = false;
    if (is_tested) return;
    is_tested = true;
  }
  TRACE("Starting ribi::ToolTestCanvasMenuDialog::Test");
  ribi::Canvas();
  TRACE("Finished ribi::ToolTestCanvasMenuDialog::Test successfully");
}
#endif

 

 

 

 

 

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