summaryrefslogtreecommitdiff
path: root/quacker/rackdisplay.cpp
blob: c8bd82ce42193ffcf7c3386a0a1848ae4df07dd3 (plain)
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
/*
 *  Quackle -- Crossword game artificial intelligence and analysis tool
 *  Copyright (C) 2005-2014 Jason Katz-Brown and John O'Laughlin.
 *
 *  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/>.
 */

#include <iostream>

#include <QtWidgets>

#include <game.h>
#include <quackleio/util.h>

#include "geometry.h"
#include "graphicalboard.h"
#include "rackdisplay.h"

QuickEntryRack::QuickEntryRack(QWidget *parent)
	: View(parent)
{
	QHBoxLayout *textLayout = new QHBoxLayout();
	Geometry::setupInnerLayout(textLayout);

	m_lineEdit = new QLineEdit;
	m_lineEdit->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	connect(m_lineEdit, SIGNAL(returnPressed()), this, SLOT(quickEditReturnPressed()));

	QPushButton *setButton = new QPushButton(tr("Set Rack"));
	connect(setButton, SIGNAL(clicked()), this, SLOT(quickEditReturnPressed()));

	QPushButton *shuffleButton = new QPushButton(tr("Shu&ffle"));
	connect(shuffleButton, SIGNAL(clicked()), this, SLOT(shuffle()));

	m_label = new QLabel(tr("&Rack"));
	m_label->setBuddy(m_lineEdit);
	textLayout->addWidget(m_label, 1);
	textLayout->addWidget(m_lineEdit, 4);
	textLayout->addWidget(setButton, 1);
	textLayout->addWidget(shuffleButton, 1);

	m_tiles = new GraphicalRack;

	QVBoxLayout *layout = new QVBoxLayout;
	Geometry::setupInnerLayout(layout);
	layout->addWidget(m_tiles);
	layout->addLayout(textLayout);
	setLayout(layout);
}

QuickEntryRack::~QuickEntryRack()
{
}

void QuickEntryRack::positionChanged(const Quackle::GamePosition &position)
{
	if (m_rackTiles == position.currentPlayer().rack().tiles())
		return;

	m_rackTiles = position.currentPlayer().rack().tiles();
	QString tiles = QuackleIO::Util::letterStringToQString(m_rackTiles);
	m_lineEdit->setText(tiles);
	m_label->setText(QString("%1's &rack:").arg(QuackleIO::Util::uvStringToQString(position.currentPlayer().name())));
	m_tiles->setText(m_rackTiles);
}

void QuickEntryRack::grabFocus()
{
	m_lineEdit->setFocus();
	m_lineEdit->selectAll();
}

void QuickEntryRack::quickEditReturnPressed()
{
	QString text(m_lineEdit->text());
	m_lineEdit->clear();
	processRack(text);
}

void QuickEntryRack::processRack(const QString &rack)
{
	if (rack.isEmpty())
	{
		emit statusMessage(tr("Useless rack."));
		return;
	}
	
	emit setRack(QuackleIO::Util::makeRack(rack));
}

void QuickEntryRack::shuffle()
{
	Quackle::Rack rack(QuackleIO::Util::makeRack(m_lineEdit->text()));
	rack.shuffle();

	emit setRack(rack);
}

GraphicalRack::GraphicalRack(QWidget * parent)
	: QFrame(parent)
{
	m_layout = new QHBoxLayout(this);
	Geometry::setupInnerLayout(m_layout);
	m_layout->addStretch();

	setAcceptDrops(true);
	setMinimumSize(50, 50);
}

void
GraphicalRack::setText(const Quackle::LetterString &text)
{
	// clear old labels
	while(m_layout->count()) {
		QLabel *label = qobject_cast<QLabel*>(m_layout->itemAt(0)->widget());
		if (!label) {
			break;
		}
		m_layout->removeWidget(label);
		label->close();
	}

	PixmapCacher::self()->invalidate();
	for (int i = text.size() - 1; i >= 0 ; --i) {
		QLabel *label = new QLabel;
        label->setAttribute (Qt::WA_DeleteOnClose);

		TileWidget tile;
		Quackle::Board::TileInformation info;
        info.isOnRack = true;
		info.letter = text[i];
		info.tileType = Quackle::Board::LetterTile;
		tile.setInformation(info);
		tile.setSideLength(50);
		tile.prepare();
	
		label->setPixmap(tile.tilePixmap());

		m_layout->insertWidget(0, label);
	}
}

const QString GraphicalRack::mime_type = "application/x-quackle-tile";

void
GraphicalRack::dragEnterEvent (QDragEnterEvent* event)
{
    if (event->mimeData()->hasFormat(mime_type))
    {
        if (event->source() == this) {
            event->setDropAction (Qt::MoveAction);
            event->accept();
        } else {
            event->acceptProposedAction();
        }
    } else {
        event->ignore();
    }
}

//---------------------------------------------------------------------------
//  dropEvent
//
//! The event handler that receives drop events.
//
//! @param event the drop event
//---------------------------------------------------------------------------
void
GraphicalRack::dropEvent (QDropEvent* event)
{
    if (event->mimeData()->hasFormat (mime_type)) {
        QByteArray itemData = event->mimeData()->data (mime_type);
        QDataStream dataStream(&itemData, QIODevice::ReadOnly);

        QPixmap pixmap;
        QPoint sourcePos;
        QPoint offset;
        dataStream >> pixmap >> sourcePos >> offset;

        QLabel* droppedTile = new QLabel;
        droppedTile->setPixmap(pixmap);
        droppedTile->setAttribute (Qt::WA_DeleteOnClose);

        QPoint dropPos = event->pos() - offset;

        // Move the tile an extra half tile width in the direction of the
        // move.  This allows the tile to assume a new spot if it is dragged
        // more than halfway onto the spot.
        int extraMove = (sourcePos.x() < dropPos.x() ? 50 / 2
                                                     : -50 / 2);

        dropPos.setX(dropPos.x() + extraMove);

		for(int i = 0; i < m_layout->count(); ++i) {
			QLabel *label = qobject_cast<QLabel*>(m_layout->itemAt(i)->widget());
			if (!label) { // hit the stretcher
				m_layout->insertWidget(i, droppedTile);
				break;
			}
			if (dropPos.x() > label->pos().x()) {
				continue;
			}
			m_layout->insertWidget(i, droppedTile);
			break;
		}

        if (event->source() == this) {
            event->setDropAction (Qt::MoveAction);
            event->accept();
        } else {
            event->acceptProposedAction();
        }
    }
    else {
        event->ignore();
    }
}

//---------------------------------------------------------------------------
//  mousePressEvent
//
//! The event handler that receives mouse press events.
//
//! @param event the mouse press event
//---------------------------------------------------------------------------
void
GraphicalRack::mousePressEvent (QMouseEvent* event)
{
    QLabel* child = qobject_cast<QLabel*>(childAt (event->pos()));
    if (!child)
        return;

    QPixmap pixmap = *(child->pixmap());

    QByteArray itemData;
    QDataStream dataStream (&itemData, QIODevice::WriteOnly);
    dataStream << pixmap << QPoint (event->pos())
               << QPoint (event->pos() - child->pos());

    QMimeData *mimeData = new QMimeData;
    mimeData->setData (mime_type, itemData);

    QDrag *drag = new QDrag (this);
    drag->setMimeData (mimeData);
    drag->setPixmap (pixmap);
    drag->setHotSpot (event->pos() - child->pos());

    QColor bgColor = palette().color(QPalette::Window);

    QPixmap tempPixmap = pixmap;
    QPainter painter;
    painter.begin (&tempPixmap);
    painter.fillRect(pixmap.rect(), QColor(bgColor.red(), bgColor.green(),
                                            bgColor.blue(), 127));
    painter.end();

    child->setPixmap (tempPixmap);

    if (drag->start (Qt::CopyAction | Qt::MoveAction) == Qt::MoveAction) {
        child->close();
    }
    else {
        child->show();
        child->setPixmap (pixmap);
    }
}