mirror of
git://git.savannah.nongnu.org/eliot.git
synced 2024-11-16 07:47:39 +01:00
Moved the tiles layout to a dedicated file
This commit is contained in:
parent
6bf27a4509
commit
1374304ff9
4 changed files with 171 additions and 99 deletions
|
@ -57,6 +57,7 @@ EXTRA_DIST = \
|
|||
eliot_SOURCES = \
|
||||
qtcommon.h qtcommon.cpp \
|
||||
coord_model.h coord_model.cpp \
|
||||
tile_layout.cpp tile_layout.h \
|
||||
bag_widget.cpp bag_widget.h \
|
||||
dic_tools_widget.cpp dic_tools_widget.h \
|
||||
new_game.cpp new_game.h \
|
||||
|
@ -85,6 +86,7 @@ nodist_eliot_SOURCES = \
|
|||
ui/dic_wizard_letters_def_page.ui.h \
|
||||
ui/dic_wizard_conclusion_page.ui.h \
|
||||
coord_model.moc.cpp \
|
||||
tile_layout.moc.cpp \
|
||||
new_game.moc.cpp \
|
||||
dic_tools_widget.moc.cpp \
|
||||
bag_widget.moc.cpp \
|
||||
|
|
|
@ -18,15 +18,12 @@
|
|||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*****************************************************************************/
|
||||
|
||||
#include <algorithm> // For std::transform
|
||||
#include <cmath>
|
||||
#include <QtGui/QGridLayout>
|
||||
#include <QtGui/QMouseEvent>
|
||||
#include <QtGui/QPainter>
|
||||
// XXX
|
||||
#include <iostream>
|
||||
|
||||
#include "board_widget.h"
|
||||
#include "tile_layout.h"
|
||||
#include "tile_widget.h"
|
||||
#include "qtcommon.h"
|
||||
#include "public_game.h"
|
||||
|
@ -37,99 +34,6 @@
|
|||
using namespace std;
|
||||
|
||||
|
||||
class BoardLayout : public QLayout
|
||||
{
|
||||
//Q_OBJECT
|
||||
|
||||
public:
|
||||
BoardLayout(int nbCols, int spacing): m_nbCols(nbCols), m_space(spacing)
|
||||
{
|
||||
setContentsMargins(0, 0, 0, 0);
|
||||
}
|
||||
~BoardLayout()
|
||||
{
|
||||
QLayoutItem *item;
|
||||
while ((item = takeAt(0)))
|
||||
delete item;
|
||||
}
|
||||
|
||||
QRect getBoardRect() const
|
||||
{
|
||||
if (m_items.size() < m_nbCols + 2)
|
||||
return QRect();
|
||||
return m_items.at(m_nbCols + 1)->geometry().united(m_items.back()->geometry());
|
||||
}
|
||||
|
||||
int getSquareSize() const
|
||||
{
|
||||
if (m_items.empty())
|
||||
return 0;
|
||||
return m_items.at(0)->geometry().width();
|
||||
}
|
||||
|
||||
int getSpacing() const
|
||||
{
|
||||
return m_space;
|
||||
}
|
||||
|
||||
virtual void addItem(QLayoutItem *item)
|
||||
{
|
||||
m_items.append(item);
|
||||
}
|
||||
virtual bool hasHeightForWidth() const { return true; }
|
||||
virtual int heightForWidth(int width) const { return width; }
|
||||
virtual int count() const { return m_items.size(); }
|
||||
virtual QLayoutItem *itemAt(int index) const { return m_items.value(index); }
|
||||
virtual QLayoutItem *takeAt(int index)
|
||||
{
|
||||
if (index >= 0 && index < m_items.size())
|
||||
return m_items.takeAt(index);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
virtual QSize minimumSize() const
|
||||
{
|
||||
QSize size(m_space, m_space);
|
||||
if (!m_items.empty())
|
||||
size += m_items.at(0)->minimumSize();
|
||||
return size * m_nbCols + QSize(5, 5);
|
||||
}
|
||||
virtual void setGeometry(const QRect &rect)
|
||||
{
|
||||
QLayout::setGeometry(rect);
|
||||
doLayout(rect);
|
||||
}
|
||||
virtual QSize sizeHint() const { return minimumSize(); }
|
||||
|
||||
private:
|
||||
QList<QLayoutItem *> m_items;
|
||||
int m_nbCols;
|
||||
int m_space;
|
||||
|
||||
void doLayout(const QRect &rect)
|
||||
{
|
||||
int size = std::min(rect.width(), rect.height());
|
||||
int squareSize = size / m_nbCols - m_space;
|
||||
QLayoutItem *item;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int nbInRow = 1;
|
||||
foreach (item, m_items)
|
||||
{
|
||||
QRect itemRect(QPoint(x, y), QSize(squareSize, squareSize));
|
||||
item->setGeometry(itemRect);
|
||||
x += squareSize + m_space;
|
||||
++nbInRow;
|
||||
if (nbInRow > m_nbCols)
|
||||
{
|
||||
x = 0;
|
||||
y += squareSize + m_space;
|
||||
nbInRow = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BoardWidget::BoardWidget(CoordModel &iCoordModel, QWidget *parent)
|
||||
: QFrame(parent), m_game(NULL), m_coordModel(iCoordModel),
|
||||
m_widgetsMatrix(BOARD_MAX + 1, BOARD_MAX + 1, 0)
|
||||
|
@ -142,7 +46,7 @@ BoardWidget::BoardWidget(CoordModel &iCoordModel, QWidget *parent)
|
|||
setForegroundRole(QPalette::Window);
|
||||
setBackgroundRole(QPalette::Window);
|
||||
|
||||
BoardLayout *layout = new BoardLayout(BOARD_MAX + 1, 1);
|
||||
TileLayout *layout = new TileLayout(BOARD_MAX + 1, 1);
|
||||
// Line full of coordinates
|
||||
layout->addWidget(new BasicTileWidget(this, ""));
|
||||
for (unsigned int col = BOARD_MIN; col <= BOARD_MAX; ++col)
|
||||
|
@ -246,7 +150,7 @@ QSize BoardWidget::sizeHint() const
|
|||
|
||||
void BoardWidget::paintEvent(QPaintEvent *)
|
||||
{
|
||||
const BoardLayout *boardLayout = (BoardLayout*)layout();
|
||||
const TileLayout *boardLayout = (TileLayout*)layout();
|
||||
QPainter painter(this);
|
||||
QRect rect = boardLayout->getBoardRect();
|
||||
const int size = boardLayout->getSquareSize();
|
||||
|
|
105
qt/tile_layout.cpp
Normal file
105
qt/tile_layout.cpp
Normal file
|
@ -0,0 +1,105 @@
|
|||
/*****************************************************************************
|
||||
* Eliot
|
||||
* Copyright (C) 2010 Olivier Teulière
|
||||
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
|
||||
*
|
||||
* 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 2 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, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*****************************************************************************/
|
||||
|
||||
#include "tile_layout.h"
|
||||
#include "tile_widget.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
TileLayout::TileLayout(int nbCols, int spacing)
|
||||
: m_nbCols(nbCols), m_space(spacing)
|
||||
{
|
||||
setContentsMargins(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
TileLayout::~TileLayout()
|
||||
{
|
||||
QLayoutItem *item;
|
||||
while ((item = takeAt(0)))
|
||||
delete item;
|
||||
}
|
||||
|
||||
|
||||
QRect TileLayout::getBoardRect() const
|
||||
{
|
||||
if (m_items.size() < m_nbCols + 2)
|
||||
return QRect();
|
||||
return m_items.at(m_nbCols + 1)->geometry().united(m_items.back()->geometry());
|
||||
}
|
||||
|
||||
|
||||
int TileLayout::getSquareSize() const
|
||||
{
|
||||
if (m_items.empty())
|
||||
return 0;
|
||||
return m_items.at(0)->geometry().width();
|
||||
}
|
||||
|
||||
|
||||
QLayoutItem *TileLayout::takeAt(int index)
|
||||
{
|
||||
if (index >= 0 && index < m_items.size())
|
||||
return m_items.takeAt(index);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
QSize TileLayout::minimumSize() const
|
||||
{
|
||||
QSize size(m_space, m_space);
|
||||
if (!m_items.empty())
|
||||
size += m_items.at(0)->minimumSize();
|
||||
return size * m_nbCols + QSize(5, 5);
|
||||
}
|
||||
|
||||
|
||||
void TileLayout::setGeometry(const QRect &rect)
|
||||
{
|
||||
QLayout::setGeometry(rect);
|
||||
doLayout(rect);
|
||||
}
|
||||
|
||||
|
||||
void TileLayout::doLayout(const QRect &rect)
|
||||
{
|
||||
int size = std::min(rect.width(), rect.height());
|
||||
int squareSize = size / m_nbCols - m_space;
|
||||
QLayoutItem *item;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int nbInRow = 1;
|
||||
foreach (item, m_items)
|
||||
{
|
||||
QRect itemRect(QPoint(x, y), QSize(squareSize, squareSize));
|
||||
item->setGeometry(itemRect);
|
||||
x += squareSize + m_space;
|
||||
++nbInRow;
|
||||
if (nbInRow > m_nbCols)
|
||||
{
|
||||
x = 0;
|
||||
y += squareSize + m_space;
|
||||
nbInRow = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
61
qt/tile_layout.h
Normal file
61
qt/tile_layout.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*****************************************************************************
|
||||
* Eliot
|
||||
* Copyright (C) 2010 Olivier Teulière
|
||||
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
|
||||
*
|
||||
* 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 2 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, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef TILE_LAYOUT_H_
|
||||
#define TILE_LAYOUT_H_
|
||||
|
||||
#include <QtGui/QLayout>
|
||||
//#include <QtGui/QList>
|
||||
|
||||
|
||||
class TileLayout : public QLayout
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
TileLayout(int nbCols, int spacing);
|
||||
virtual ~TileLayout();
|
||||
|
||||
QRect getBoardRect() const;
|
||||
|
||||
int getSquareSize() const;
|
||||
|
||||
int getSpacing() const { return m_space; }
|
||||
|
||||
virtual void addItem(QLayoutItem *item) { m_items.append(item); }
|
||||
virtual bool hasHeightForWidth() const { return true; }
|
||||
virtual int heightForWidth(int width) const { return width; }
|
||||
virtual int count() const { return m_items.size(); }
|
||||
virtual QLayoutItem *itemAt(int index) const { return m_items.value(index); }
|
||||
virtual QLayoutItem *takeAt(int index);
|
||||
virtual QSize minimumSize() const;
|
||||
virtual void setGeometry(const QRect &rect);
|
||||
virtual QSize sizeHint() const { return minimumSize(); }
|
||||
|
||||
private:
|
||||
QList<QLayoutItem *> m_items;
|
||||
int m_nbCols;
|
||||
int m_space;
|
||||
|
||||
void doLayout(const QRect &rect);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in a new issue