2010-10-17 23:28:01 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* 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 <algorithm> // For std::transform
|
|
|
|
#include <QtGui/QPainter>
|
|
|
|
#include <QtGui/QPaintEvent>
|
|
|
|
#include <QtCore/QSettings>
|
|
|
|
|
|
|
|
#include "tile_widget.h"
|
|
|
|
#include "prefs_dialog.h"
|
|
|
|
#include "qtcommon.h"
|
|
|
|
#include "tile.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2012-01-28 13:16:59 +01:00
|
|
|
INIT_LOGGER(qt, BasicTileWidget);
|
|
|
|
INIT_LOGGER(qt, TileWidget);
|
|
|
|
INIT_LOGGER(qt, TileWidgetDecorator);
|
|
|
|
|
2010-10-17 23:28:01 +02:00
|
|
|
|
|
|
|
const QColor TileWidget::EmptyColour(Qt::white);
|
|
|
|
const QColor TileWidget::L2Colour(34, 189, 240);
|
|
|
|
const QColor TileWidget::L3Colour(29, 104, 240);
|
|
|
|
const QColor TileWidget::W2Colour(255, 147, 196);
|
|
|
|
const QColor TileWidget::W3Colour(240, 80, 94);
|
|
|
|
const QColor TileWidget::TileColour(255, 235, 205);
|
|
|
|
const QColor TileWidget::PreviewColour(183, 183, 123);
|
2010-11-01 18:49:55 +01:00
|
|
|
const QColor TileWidget::PlayedColour(Qt::white);
|
2010-10-17 23:28:01 +02:00
|
|
|
const QColor TileWidget::NormalColour(0, 0, 0);
|
|
|
|
const QColor TileWidget::JokerColour(255, 0, 0);
|
|
|
|
const QColor TileWidget::ArrowColour(10, 10, 10);
|
|
|
|
|
|
|
|
|
2010-10-17 23:45:10 +02:00
|
|
|
BasicTileWidget::BasicTileWidget(QWidget *parent, QString text)
|
|
|
|
: QWidget(parent), m_text(text)
|
|
|
|
{
|
2010-10-22 18:59:43 +02:00
|
|
|
setMinimumSize(10, 10);
|
2010-10-17 23:45:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int BasicTileWidget::heightForWidth(int width) const
|
|
|
|
{
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QSize BasicTileWidget::sizeHint() const
|
|
|
|
{
|
|
|
|
return QSize(30, 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int BasicTileWidget::getSquareSize() const
|
|
|
|
{
|
|
|
|
return std::min(width(), height());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BasicTileWidget::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
const int squareSize = getSquareSize();
|
|
|
|
QFont letterFont = font();
|
|
|
|
letterFont.setPixelSize(squareSize * 2 / 3);
|
|
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setFont(letterFont);
|
2010-10-22 18:58:37 +02:00
|
|
|
painter.drawText(1, 1, squareSize, squareSize, Qt::AlignCenter, m_text);
|
2010-10-17 23:45:10 +02:00
|
|
|
}
|
|
|
|
|
2010-11-01 18:49:55 +01:00
|
|
|
// --------------
|
2010-10-17 23:45:10 +02:00
|
|
|
|
2010-10-22 18:58:37 +02:00
|
|
|
TileWidget::TileWidget(QWidget *parent, Multiplier multiplier,
|
|
|
|
int row, int col)
|
|
|
|
: BasicTileWidget(parent), m_multiplier(multiplier),
|
|
|
|
m_row(row), m_col(col), m_isJoker(false),
|
2010-11-01 18:49:55 +01:00
|
|
|
m_state(NORMAL), m_showArrow(false), m_horizontalArrow(true)
|
2010-10-17 23:28:01 +02:00
|
|
|
{
|
|
|
|
QSizePolicy policy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|
|
|
policy.setHeightForWidth(true);
|
|
|
|
setSizePolicy(policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-01 18:49:55 +01:00
|
|
|
void TileWidget::tileChanged(const Tile &iTile, bool isJoker, State state)
|
2010-10-17 23:28:01 +02:00
|
|
|
{
|
|
|
|
m_tile = iTile;
|
|
|
|
m_isJoker = isJoker;
|
2010-11-01 18:49:55 +01:00
|
|
|
m_state = state;
|
2010-10-22 18:58:37 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TileWidget::arrowChanged(bool showArrow, bool horizontalArrow)
|
|
|
|
{
|
2010-10-17 23:28:01 +02:00
|
|
|
m_showArrow = showArrow;
|
|
|
|
m_horizontalArrow = horizontalArrow;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TileWidget::paintEvent(QPaintEvent *)
|
|
|
|
{
|
2010-10-17 23:45:10 +02:00
|
|
|
const int squareSize = getSquareSize();
|
2010-10-17 23:28:01 +02:00
|
|
|
|
|
|
|
// The font must grow with the square size
|
|
|
|
QFont letterFont = font();
|
|
|
|
letterFont.setPixelSize(squareSize * 2 / 3);
|
|
|
|
|
|
|
|
QFont pointsFont = font();
|
|
|
|
const double pointsCoeff = 8. / 25.;
|
|
|
|
pointsFont.setPixelSize(squareSize * pointsCoeff);
|
|
|
|
|
|
|
|
// XXX: Naive implementation: we repaint everything every time
|
|
|
|
QPainter painter(this);
|
|
|
|
|
2010-10-22 18:59:43 +02:00
|
|
|
// Set the square color
|
|
|
|
QColor color;
|
2010-10-17 23:28:01 +02:00
|
|
|
if (!m_tile.isEmpty())
|
|
|
|
{
|
2010-11-01 18:49:55 +01:00
|
|
|
if (m_state == PREVIEW)
|
2010-10-22 18:59:43 +02:00
|
|
|
color = PreviewColour;
|
2010-11-01 18:49:55 +01:00
|
|
|
else if (m_state == PLAYED)
|
|
|
|
color = PlayedColour;
|
2010-10-17 23:28:01 +02:00
|
|
|
else
|
2010-10-22 18:59:43 +02:00
|
|
|
color = TileColour;
|
2010-10-17 23:28:01 +02:00
|
|
|
}
|
|
|
|
else if (m_multiplier == WORD_TRIPLE)
|
2010-10-22 18:59:43 +02:00
|
|
|
color = W3Colour;
|
2010-10-17 23:28:01 +02:00
|
|
|
else if (m_multiplier == WORD_DOUBLE)
|
2010-10-22 18:59:43 +02:00
|
|
|
color = W2Colour;
|
2010-10-17 23:28:01 +02:00
|
|
|
else if (m_multiplier == LETTER_TRIPLE)
|
2010-10-22 18:59:43 +02:00
|
|
|
color = L3Colour;
|
2010-10-17 23:28:01 +02:00
|
|
|
else if (m_multiplier == LETTER_DOUBLE)
|
2010-10-22 18:59:43 +02:00
|
|
|
color = L2Colour;
|
2010-10-17 23:28:01 +02:00
|
|
|
else
|
2010-10-22 18:59:43 +02:00
|
|
|
color = EmptyColour;
|
|
|
|
painter.fillRect(0, 0, squareSize, squareSize, color);
|
2010-10-17 23:28:01 +02:00
|
|
|
|
|
|
|
// Draw the letter
|
|
|
|
if (!m_tile.isEmpty())
|
|
|
|
{
|
|
|
|
wstring chr = m_tile.getDisplayStr();
|
|
|
|
// Make the display char in upper case
|
|
|
|
std::transform(chr.begin(), chr.end(), chr.begin(), towupper);
|
|
|
|
if (m_isJoker)
|
|
|
|
painter.setPen(JokerColour);
|
|
|
|
painter.setFont(letterFont);
|
2010-10-22 18:59:43 +02:00
|
|
|
painter.drawText(0, 0, squareSize, squareSize,
|
2010-10-17 23:28:01 +02:00
|
|
|
Qt::AlignCenter, qfw(chr));
|
|
|
|
painter.setPen(NormalColour);
|
|
|
|
|
|
|
|
// Should we display the tiles points?
|
|
|
|
QSettings qs(ORGANIZATION, PACKAGE_NAME);
|
|
|
|
const bool showPoints = qs.value(PrefsDialog::kINTF_SHOW_TILES_POINTS, true).toBool();
|
|
|
|
|
|
|
|
// Draw the points of the tile
|
2010-11-01 18:49:55 +01:00
|
|
|
if (showPoints && !m_isJoker && !m_tile.isJoker())
|
2010-10-17 23:28:01 +02:00
|
|
|
{
|
|
|
|
painter.setFont(pointsFont);
|
2010-10-22 18:59:43 +02:00
|
|
|
painter.drawText(0,
|
|
|
|
squareSize * (1 - pointsCoeff),
|
|
|
|
squareSize - 1, squareSize * pointsCoeff + 3,
|
2010-10-17 23:28:01 +02:00
|
|
|
Qt::AlignRight | Qt::AlignBottom,
|
|
|
|
QString("%1").arg(m_tile.getPoints()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Draw the arrow
|
|
|
|
if (m_showArrow)
|
|
|
|
{
|
2010-10-22 18:57:13 +02:00
|
|
|
painter.setPen(QPen(painter.brush().color(), 0));
|
2010-10-17 23:28:01 +02:00
|
|
|
painter.setBrush(ArrowColour);
|
|
|
|
const int mid = squareSize / 2;
|
|
|
|
const int fifth = squareSize / 5;
|
|
|
|
const int width = squareSize / 16;
|
2010-10-22 18:59:43 +02:00
|
|
|
painter.translate(mid, mid);
|
2010-10-17 23:28:01 +02:00
|
|
|
if (m_horizontalArrow)
|
|
|
|
painter.rotate(90);
|
|
|
|
const QPoint points[] =
|
|
|
|
{
|
2010-10-22 18:57:13 +02:00
|
|
|
QPoint(-mid + fifth - 1, -width - 1),
|
|
|
|
QPoint(-mid + 3*fifth - 1, -width - 1),
|
|
|
|
QPoint(-mid + 3*fifth - 1, -fifth - 2),
|
|
|
|
QPoint(-mid + 4*fifth + 1, 0),
|
|
|
|
QPoint(-mid + 3*fifth - 1, fifth + 2),
|
|
|
|
QPoint(-mid + 3*fifth - 1, width + 1),
|
|
|
|
QPoint(-mid + fifth - 1, width + 1)
|
2010-10-17 23:28:01 +02:00
|
|
|
};
|
|
|
|
painter.drawPolygon(points, 7);
|
|
|
|
|
|
|
|
painter.setPen(QPen());
|
|
|
|
painter.setBrush(NormalColour);
|
|
|
|
}
|
2010-11-01 18:49:55 +01:00
|
|
|
if (m_state == PLAYED)
|
|
|
|
{
|
|
|
|
painter.drawLine(QLine(0, 0, squareSize, squareSize));
|
|
|
|
painter.drawLine(QLine(0, squareSize, squareSize, 0));
|
|
|
|
}
|
2010-10-17 23:28:01 +02:00
|
|
|
}
|
|
|
|
|
2010-10-22 18:58:37 +02:00
|
|
|
|
|
|
|
void TileWidget::mousePressEvent(QMouseEvent *iEvent)
|
|
|
|
{
|
|
|
|
emit mousePressed(m_row, m_col, iEvent);
|
|
|
|
}
|
|
|
|
|
2010-11-01 18:49:55 +01:00
|
|
|
// --------------
|
|
|
|
|
|
|
|
TileWidgetDecorator::TileWidgetDecorator(QWidget *parent, TileWidget &wrapped)
|
|
|
|
: TileWidget(parent), m_wrapped(wrapped)
|
|
|
|
{
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout;
|
|
|
|
layout->setContentsMargins(1, 1, 1, 1);
|
|
|
|
layout->addWidget(&m_wrapped);
|
|
|
|
setLayout(layout);
|
|
|
|
|
|
|
|
QObject::connect(&m_wrapped, SIGNAL(mousePressed(int, int, QMouseEvent*)),
|
|
|
|
this, SIGNAL(mousePressed(int, int, QMouseEvent*)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TileWidgetDecorator::tileChanged(const Tile &iTile, bool isJoker, State state)
|
|
|
|
{
|
|
|
|
m_wrapped.tileChanged(iTile, isJoker, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TileWidgetDecorator::arrowChanged(bool showArrow, bool horizontalArrow)
|
|
|
|
{
|
|
|
|
m_wrapped.arrowChanged(showArrow, horizontalArrow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TileWidgetDecorator::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.drawRect(0, 0, width() - 1, height() - 1);
|
|
|
|
}
|
|
|
|
|