Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QtChess

 

QtQt CreatorLubuntu

 

QtChess has many Qt chess classes

Technical facts

 

 

 

 

 

 

./CppQtChess/CppQtChess.pri

 

INCLUDEPATH += \
    ../../Classes/CppQtChess

SOURCES += \
    ../../Classes/CppQtChess/qtchessresources.cpp \
    ../../Classes/CppQtChess/qtchessboardwidget.cpp

HEADERS  += \
    ../../Classes/CppQtChess/qtchessresources.h \
    ../../Classes/CppQtChess/qtchessboardwidget.h

OTHER_FILES += \
    ../../Classes/CppQtChess/Licence.txt

 

 

 

 

 

./CppQtChess/qtchessboardwidget.h

 

//---------------------------------------------------------------------------
/*
QtChessBoardWidget, Qt widget for displaying the Chess::Board class
Copyright (C) 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/CppQtChessBoardWidget.htm
//---------------------------------------------------------------------------
#ifndef QTCHESSBOARDWIDGET_H
#define QTCHESSBOARDWIDGET_H

//#include "chessfwd.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 "chessboardwidget.h"    //For MOC
#include "chesssquare.h"         //For MOC
#include "chesssquareselector.h" //For MOC
#include "qtchessresources.h"    //For MOC
#pragma GCC diagnostic pop

namespace ribi {
namespace Chess {

///QtChessBoardWidget displays a Chess::Board (with a ChessBoardWidget in between)
struct QtChessBoardWidget : public QWidget
{
  Q_OBJECT

  public:

  explicit QtChessBoardWidget(QWidget *parent = 0);

  ///A QtChessBoardWidget is created by its width and height
  QtChessBoardWidget(const int width, const int height,
    QWidget *parent = 0);

  ///Draw a ChessBoard from a BoardWidget
  static void DrawChessBoard(
    QPainter& painter,
    const Chess::BoardWidget * const widget);

  ///Draw a ChessBoard
  static void DrawChessBoard(
    QPainter& painter,
    const int left, const int top,
    const int width, const int height,
    const Chess::Board * const board);


  ///Obtain a read-only pointer to the Chess::Board
  const Chess::BoardWidget * GetWidget() const { return m_widget.get(); }

  ///Obtain a read-and-write pointer to the Chess::Board
  Chess::BoardWidget * GetWidget() { return m_widget.get(); }

  ///Signal that is emitted when a QtChessBoardWidget is changed
  mutable boost::signals2::signal<void ()> m_signal_changed;

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

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

  protected:

  ///Respond to a key press
  void keyPressEvent(QKeyEvent *);

  ///Respond to mouse clicks
  void mousePressEvent(QMouseEvent *);

  ///Paint the QtChessBoardWidget
  void paintEvent(QPaintEvent *);

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

  private:

  ///The resources used
  const boost::scoped_ptr<const Chess::QtResources> m_resources;

  ///The Chess::Board
  boost::scoped_ptr<Chess::BoardWidget> m_widget;


  ///Repaint the QtChessBoardWidget
  void DoRepaint();

  static void Test() noexcept;
  ///Respond to mouse click
  //void OnClicked(const Wt::WMouseEvent& e);
};

} //~namespace Chess
} //~namespace ribi

#endif // QTCHESSBOARDWIDGET_H

 

 

 

 

 

./CppQtChess/qtchessboardwidget.cpp

 

//---------------------------------------------------------------------------
/*
QtChessBoardWidget, Qt widget for displaying the Chess::Board class
Copyright (C) 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/CppQtChessBoardWidget.htm
//---------------------------------------------------------------------------
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include "qtchessboardwidget.h"

#include <iostream>
#include <boost/bind.hpp>
#include <boost/numeric/conversion/cast.hpp>

#include <QBitmap>
#include <QMouseEvent>
#include <QPainter>

#include "chessboard.h"
#include "chessboardwidget.h"
#include "chessmove.h"
#include "chessresources.h"
#include "chesssquare.h"
#include "chesssquarefactory.h"
#include "chesssquareselector.h"
#include "chesswidget.h"
#include "fileio.h"
#include "chessboardfactory.h"
#include "qtchessresources.h"
#include "trace.h"
#include "widget.h"
#pragma GCC diagnostic pop

ribi::Chess::QtChessBoardWidget::QtChessBoardWidget(QWidget *parent)
  : QWidget(parent),
    m_signal_changed{},
    m_resources(new Chess::QtResources),
    m_widget(new BoardWidget(BoardFactory().Create(),Widget::CreateRect(0,0,400,400)))
{
  #ifndef NDEBUG
  Test();
  #endif
  assert(m_widget);

  m_widget->m_signal_geometry_changed.connect(
    boost::bind(
      &ribi::Chess::QtChessBoardWidget::DoRepaint,
      this));

  m_widget->m_signal_graphic_changed.connect(
    boost::bind(
      &ribi::Chess::QtChessBoardWidget::DoRepaint,
      this));

  resize(200,200);
}

ribi::Chess::QtChessBoardWidget::QtChessBoardWidget(
  const int width, const int height,
  QWidget *parent)
  : QWidget(parent),
    m_signal_changed{},
    m_resources(new Chess::QtResources),
    m_widget(new Chess::BoardWidget(
      BoardFactory().Create(),Widget::CreateRect(0,0,width,height)))
{
  assert(m_widget);

  //m_widget->GetMachine()->GetDialBack()->GetDial()->m_signal_position_changed.connect(boost::bind(
  //  &ribi::Chess::QtChessBoardWidget::DoRepaint,this));
  //m_widget->GetMachine()->GetDialFront()->GetDial()->m_signal_position_changed.connect(boost::bind(
  //  &ribi::Chess::QtChessBoardWidget::DoRepaint,this));
  //m_widget->GetMachine()->GetToggleButton()->GetToggleButton()->m_signal_toggled.connect(boost::bind(
  //  &ribi::Chess::QtChessBoardWidget::DoRepaint,this));
  m_widget->m_signal_geometry_changed.connect(
    boost::bind(
      &ribi::Chess::QtChessBoardWidget::DoRepaint,
      this));

  resize(width,height);
}

void ribi::Chess::QtChessBoardWidget::DoRepaint()
{
  this->repaint();
}

void ribi::Chess::QtChessBoardWidget::DrawChessBoard(
  QPainter& painter,
  const Chess::BoardWidget * const widget)
{
  const int w = widget->GetWidth();
  const int h = widget->GetHeight();

  //Draw the plain chessboard
  DrawChessBoard(
    painter,
    widget->GetLeft(),
    widget->GetTop(),
    w,
    h,
    widget->GetBoard().get());
  //Draw the selected square
  static const Chess::QtResources r;
  const int square_w = w / 8;
  const int square_h = h / 8;


  const boost::shared_ptr<const Chess::Square> selected = widget->GetSelector()->GetSelected();
  if (selected)
  {
    TRACE_FUNC();
    const int x_co = selected->GetFile().ToInt() * square_w;
    const int y_co = selected->GetRank().ToInt() * square_h;
    if (widget->GetBoard()->GetPiece(selected))
    {
      const std::string filename = Chess::Resources::Find(
        widget->GetBoard()->GetPiece(selected),
        Chess::SquareSelector::m_selected_color,
        true);
      TRACE(filename);
      assert(fileio::FileIo().IsRegularFile(filename));
      const QPixmap p(filename.c_str());
      painter.drawPixmap(x_co,y_co,square_w,square_h,p);
    }
    else
    {
      assert(!"Should not get here");
    }

    //Draw the possible moves

    const std::vector<boost::shared_ptr<Move> > moves = widget->GetBoard()->GetMoves(selected);
    for(const boost::shared_ptr<Move> move: moves)
    {
      if (move->To())
      {
        const int x_co = move->To()->GetFile().ToInt() * square_w;
        const int y_co = move->To()->GetRank().ToInt() * square_h;
        if (widget->GetBoard()->GetPiece(move->To()))
        {
          const std::string filename = Chess::Resources::Find(
            widget->GetBoard()->GetPiece(move->To()),
            Chess::SquareSelector::m_moves_color);
          assert(fileio::FileIo().IsRegularFile(filename));
          const QPixmap p(filename.c_str());
          painter.drawPixmap(x_co,y_co,square_w,square_h,p);
        }
        else
        {
          const boost::shared_ptr<Square> square {
            SquareFactory().Create(move->To()->GetFile(),move->To()->GetRank())
          };
          const std::string filename
            = Chess::Resources::Find(
              square,
              Chess::SquareSelector::m_moves_color);
          assert(fileio::FileIo().IsRegularFile(filename));
          const QPixmap p(filename.c_str());
          painter.drawPixmap(x_co,y_co,square_w,square_h,p);
        }
      }
    }
  }
  //Draw cursor
  const boost::shared_ptr<const Chess::Square> cursor = widget->GetSelector()->GetCursor();
  assert(cursor);
  {
    const int x_co = cursor->GetFile().ToInt() * square_w;
    const int y_co = cursor->GetRank().ToInt() * square_h;
    if (widget->GetBoard()->GetPiece(cursor))
    {
      const std::string filename = Chess::Resources::Find(widget->GetBoard()->GetPiece(cursor),
        Chess::SquareSelector::m_cursor_color,
        selected && *selected == *cursor );
      assert(fileio::FileIo().IsRegularFile(filename));
      const QPixmap p(filename.c_str());
      painter.drawPixmap(x_co,y_co,square_w,square_h,p);
    }
    else
    {
      const std::string filename
        = Chess::Resources::Find(
          cursor,
          Chess::SquareSelector::m_cursor_color);
      assert(fileio::FileIo().IsRegularFile(filename));
      const QPixmap p(filename.c_str());
      painter.drawPixmap(x_co,y_co,square_w,square_h,p);
    }
  }
}

void ribi::Chess::QtChessBoardWidget::DrawChessBoard(
  QPainter& painter,
  const int left, const int top,
  const int width, const int height,
  const Chess::Board * const board)
{
  static const Chess::QtResources r;
  const int square_w = width  / 8;
  const int square_h = height / 8;
  for (int y=0; y!=8; ++y)
  {
    for (int x=0; x!=8; ++x)
    {
      const int x_co = x * square_w;
      const int y_co = y * square_h;
      const boost::shared_ptr<Square> square {
        SquareFactory().Create(File(x),Rank(y))
      };
      if (board->GetPiece(square))
      {
        const QPixmap p(r.Find(board->GetPiece(square)).c_str());
        painter.drawPixmap(left + x_co,top + y_co,square_w,square_h,p);
      }
      else
      {
        const QPixmap p(r.Find(square).c_str());
        painter.drawPixmap(left + x_co,top + y_co,square_w,square_h,p);
      }
    }
  }
}

std::string ribi::Chess::QtChessBoardWidget::GetVersion()
{
  return "1.0";
}

std::vector<std::string> ribi::Chess::QtChessBoardWidget::GetVersionHistory()
{
  return {
    "2012-01-26: version 1.0: initial version"
  };
}

void ribi::Chess::QtChessBoardWidget::keyPressEvent(QKeyEvent * e)
{
  TRACE_FUNC();
  switch (e->key())
  {
    case Qt::Key_Up: this->m_widget->GetSelector()->MoveUp(); break;
    case Qt::Key_Right: this->m_widget->GetSelector()->MoveRight(); break;
    case Qt::Key_Down: this->m_widget->GetSelector()->MoveDown(); break;
    case Qt::Key_Left: this->m_widget->GetSelector()->MoveLeft(); break;
    case Qt::Key_Space: this->m_widget->GetSelector()->DoSelect(); break;
  }
}

void ribi::Chess::QtChessBoardWidget::mousePressEvent(QMouseEvent * e)
{
  m_widget->ClickPixel(e->x(),e->y());
  //m_widget->Click(e->x(),e->y());
}

void ribi::Chess::QtChessBoardWidget::paintEvent(QPaintEvent *)
{
  QPainter painter(this);
  DrawChessBoard( painter,this->m_widget.get());
}

void ribi::Chess::QtChessBoardWidget::resizeEvent(QResizeEvent *)
{
  m_widget->SetGeometry(0,0,width(),height());
}


void ribi::Chess::QtChessBoardWidget::Test() noexcept
{
  {
    static bool is_tested = false;
    if (is_tested) return;
    is_tested = true;
  }
  {
    QtChessBoardWidget();
  }
}

 

 

 

 

 

./CppQtChess/qtchessresources.h

 

#ifndef QTCHESSRESOURCES_H
#define QTCHESSRESOURCES_H

#include <boost/shared_ptr.hpp>
#include "chessfwd.h"
#include "chessresources.h"

namespace Qt { struct QPixmap; }

namespace ribi {
namespace Chess {

///QtResources uses Qt for generating the chess resources
struct QtResources : public Chess::Resources
{
  //QtResources constructor does all the work
  QtResources();

  ///Obtain the QPixmap of a Piece
  const Qt::QPixmap& GetPiece(const Square& s) const;

  ///Obtain the QPixmap of a Square
  const Qt::QPixmap& GetSquare(const Square& s) const;

  ///Obtain the version of this class
  static std::string GetVersion();

  ///Obtain the version history of this class
  static std::vector<std::string> GetVersionHistory();

  private:
  boost::shared_ptr<Qt::QPixmap> m_square_black;
  boost::shared_ptr<Qt::QPixmap> m_square_white;
};

} //~namespace Chess
} //~namespace ribi

#endif // QTCHESSRESOURCES_H

 

 

 

 

 

./CppQtChess/qtchessresources.cpp

 


#include <cassert>
#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"

#include <QFile>
#include "fileio.h"
#include "chessresources.h"
#include "qtchessresources.h"
//#include "trace.h"
#pragma GCC diagnostic pop


ribi::Chess::QtResources::QtResources()
  : m_square_black{},
    m_square_white{}
{
  const std::vector<std::string> v = Resources::GetFilenames();
  std::for_each(v.begin(),v.end(),
    [](const std::string& s)
    {
      if (!fileio::FileIo().IsRegularFile(s))
      {
        QFile f( (":/images/" + s).c_str() );
        f.copy(s.c_str());
        if (!fileio::FileIo().IsRegularFile(s))
        {
          const std::string error = "File not found: " + s;
          std::cerr << error << '\n';
          std::clog << error << '\n';
          std::cout << error << '\n';
        }
      }
      assert(fileio::FileIo().IsRegularFile(s));
    }
  );

  #ifndef NDEBUG
  this->Test();
  #endif
}

std::string ribi::Chess::QtResources::GetVersion()
{
  return "1.0";
}

std::vector<std::string> ribi::Chess::QtResources::GetVersionHistory()
{
  return {
    "2012-01-27: version 1.0: initial version"
  };
}

 

 

 

 

 

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