Skip to content
This repository
tree: a7ace5548b
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 317 lines (252 sloc) 9.127 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
/*
* KDots
* Copyright (c) 2011-2012 Minh Ngo <nlminhtl@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "tablewidget.hpp"
#include <cmath>
#include <vector>
#include <QPainter>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <interface/iplugin.hpp>
#include <interface/irival.hpp>
#include "dottable.hpp"
#include "stepqueue.hpp"
#include "brushcombodelegate.hpp"
#include "kdots.h"
#include "graph.hpp"

namespace KDots
{
TableWidget::TableWidget (const GameConfig& config,
std::shared_ptr<IRival> rival, QWidget *parent)
: QWidget (parent)
, m_table (new DotTable (config , this))
, m_height (config.m_height + 1)
, m_width (config.m_width + 1)
, m_rival (rival)
{
setMinimumSize (400, 400);
setMouseTracking (true);

rival->setDotTable (m_table);

connect (m_table,
SIGNAL (nextPlayer (const Point&)),
rival.get (),
SLOT (nextStep (const Point&)));

connect (m_table,
SIGNAL (nextPlayer(const Point&)),
this,
SLOT (update ()));
connect (m_table,
SIGNAL (nextPlayer (const Point&)),
this,
SLOT (onStatusMessage ()));
}

namespace
{
float cell_size (const QRect& rectange, int height, int width)
{
const float cellHeight = rectange.height () / height;
const float cellWidth = rectange.width () / width;

return cellHeight > cellWidth ? cellWidth : cellHeight;
}
}

void TableWidget::calculatePoint (Point& point, QMouseEvent *event)
{
if (!m_rival->isAllow ())
return;

const QRect& rectange = rect ();

const float cellSize = cell_size (rectange, m_height, m_width);

float dx = (rectange.width () - cellSize * m_width) / 2;

float dy = (rectange.height () - cellSize * m_height) / 2;

int x = (event->x () - dx) / cellSize;

int y = (event->y () - dy) / cellSize;

const float firstPart = cellSize / 3;

const float lastPart = cellSize * 2 / 3;

dx = event->x () - dx - x * cellSize;

if (dx > firstPart && dx < lastPart)
return;
else if (dx < firstPart)
--x;

dy = event->y () - dy - y * cellSize;

if (dy > firstPart && dy < lastPart)
return;
else if (dy < firstPart)
--y;

if (x >= m_width - 1 || x < 0
|| y < 0 || y >= m_height - 1)
return;

point.setX (x);
point.setY (y);
}

void TableWidget::mouseMoveEvent (QMouseEvent *event)
{
Point point;
calculatePoint (point, event);
const bool needRepaint = (point != m_underMousePoint);
m_underMousePoint = point;

if (needRepaint)
update ();
}

void TableWidget::onStatusMessage ()
{
emit updateStatusBar (QString ("First:\t")
+ QString::number (m_table->stepQueue ()->getMarks (FIRST))
+ "\tSecond:\t"
+ QString::number (m_table->stepQueue ()->getMarks (SECOND)));
}

void TableWidget::mousePressEvent (QMouseEvent *event)
{
Point point;
calculatePoint (point, event);

if (point.isInitialized ())
m_table->pushPoint (point);
}

void TableWidget::undo ()
{
setUpdatesEnabled (false);
m_table->undo ();
setUpdatesEnabled (true);
}

void TableWidget::drawPolygons (QPainter& painter, float cellSize)
{
const QColor& firstColor = Settings::firstPointColor ();
const QColor& secondColor = Settings::secondPointColor ();

const QBrush firstBrush (firstColor), secondBrush (secondColor);
const QPen firstPen (firstColor, 1.5), secondPen (secondColor, 1.5);

const Graph& graph = m_table->graph ();

for (Graph::const_iterator itr = graph.begin (), itrEnd = graph.end ();
itr != itrEnd; ++itr)
{
if (itr->owner () == NONE)
continue;

painter.setPen (itr->owner () == FIRST
? firstPen
: secondPen);

painter.setBrush (itr->owner () == FIRST
? firstBrush
: secondBrush);

const Point& currPoint = itr.point () + 1;

painter.drawEllipse (QPointF (currPoint.x (), currPoint.y ()) * cellSize, 3, 3);

const GraphPoint::GraphEdges& edges = itr->edges ();

for (int j = 0; j < edges.size (); ++j)
{
const Point& lastPoint = edges[j] + 1;

painter.drawLine (QPointF (currPoint.x (), currPoint.y ()) * cellSize,
QPointF (lastPoint.x (), lastPoint.y ()) * cellSize);
}
}
}

void TableWidget::drawLastPoint (QPainter& painter, float cellSize)
{
const Graph& graph = m_table->graph ();
const Point& lastPoint = m_table->stepQueue ()->lastPoint ();
const QColor firstColor (Settings::firstPointColor ());
const QColor secondColor (Settings::secondPointColor ());

const QPen firtBorder (firstColor, 0.5), secondBorder (secondColor, 0.5);

if (!lastPoint.empty ())
{
painter.setPen (graph[lastPoint].owner () == FIRST
? firtBorder
: secondBorder);

painter.setBrush (Qt::NoBrush);
const Point& newPoint = lastPoint + 1;
painter.drawEllipse (QPointF (newPoint.x (), newPoint.y ()) * cellSize, 6, 6);
}

const std::vector<Point>& possiblePoints = m_rival->possibleMoves ();
for (const Point& point : possiblePoints)
{
painter.setPen (Qt::gray);

painter.setBrush (Qt::NoBrush);
const Point& newPoint = point + 1;
painter.drawEllipse (QPointF (newPoint.x (), newPoint.y ()) * cellSize, 10, 10);
}
}

void TableWidget::drawUnderMousePoint (QPainter& painter, float cellSize)
{
if (!m_underMousePoint.isInitialized ())
return;

const Graph& graph = m_table->graph ();
const QColor firstColor (Settings::firstPointColor ());
const QColor secondColor (Settings::secondPointColor ());

const QPen firtBorder (firstColor, 0.5), secondBorder (secondColor, 0.5);


painter.setPen (graph[m_underMousePoint].owner () == FIRST
? firtBorder
: secondBorder);

painter.setBrush (Qt::NoBrush);
const Point& newPoint = m_underMousePoint + 1;
painter.drawEllipse (QPointF (newPoint.x (), newPoint.y ()) * cellSize, 6, 6);
}

void TableWidget::fillPolygon (QPainter& painter, float cellSize)
{
const QColor firstColor (Settings::firstPointColor ());
const QColor secondColor (Settings::secondPointColor ());

const auto& polygonVector = m_table->polygons ();

const QBrush firstPolyBrush (firstColor,
BrushComboDelegate::getBrushStyle (Settings::firstFillStyle ()));
const QBrush secondPolyBrush (secondColor,
BrushComboDelegate::getBrushStyle (Settings::secondFillStyle ()));

for (Polygon_ptr polygon : polygonVector)
{
QPolygon qPoly;
for (const Point& point : *polygon)
{
const Point& newPoint = point + 1;
qPoly << QPoint (newPoint.x (), newPoint.y ()) * cellSize;
}
QPainterPath path;
path.addPolygon (qPoly);
painter.fillPath (path, polygon->owner () == FIRST
? firstPolyBrush
: secondPolyBrush);
}
}

void TableWidget::paintEvent (QPaintEvent *event)
{
const QRect& rectange = event->rect ();
const float cellSize = cell_size (rectange, m_height, m_width);

const float tableWidth = cellSize * m_width;
const float tableHeight = cellSize * m_height;

QPixmap pixmap (QSize (tableWidth, tableHeight));
pixmap.fill (Qt::white);

QPainter pixPainter (&pixmap);
pixPainter.setRenderHint (QPainter::Antialiasing);
pixPainter.setPen (QPen (Qt::black, 1));

for (int i = cellSize, k = m_width * cellSize; i < k; i += cellSize)
pixPainter.drawLine (i, 0, i, pixmap.height ());

for (int i = cellSize, k = m_height * cellSize; i < k; i += cellSize)
pixPainter.drawLine (0, i, pixmap.width (), i);

pixPainter.setPen (QPen (Qt::black, 3));
pixPainter.drawRect (0, 0, pixmap.width (), pixmap.height ());

fillPolygon (pixPainter, cellSize);
drawLastPoint (pixPainter, cellSize);
drawPolygons (pixPainter, cellSize);
drawUnderMousePoint (pixPainter, cellSize);

QPainter painter (this);
const int dx = (rectange.width () - tableWidth) / 2;
const int dy = (rectange.height () - tableHeight) / 2;
painter.drawPixmap (dx, dy, pixmap);
}
}

#include "tablewidget.moc"
Something went wrong with that request. Please try again.