qnetdice

diff src/rollwidget.cc @ 1:92377189a5c6

moving along
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Dec 2013 04:08:50 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/rollwidget.cc	Sun Dec 22 04:08:50 2013 +0200
     1.3 @@ -0,0 +1,73 @@
     1.4 +#include <stdio.h>
     1.5 +#include "rollwidget.h"
     1.6 +#include <QHBoxLayout>
     1.7 +#include <QComboBox>
     1.8 +#include <QLineEdit>
     1.9 +#include <QPushButton>
    1.10 +
    1.11 +static struct {
    1.12 +	const char *name;
    1.13 +	int sides;
    1.14 +} dice_types[] = {
    1.15 +	{ "d4", 4 },
    1.16 +	{ "d6", 6 },
    1.17 +	{ "d8", 8 },
    1.18 +	{ "d10", 10 },
    1.19 +	{ "d12", 12 },
    1.20 +	{ "d20", 20 },
    1.21 +	{ "d100", 100 },
    1.22 +	{ 0, 0 }
    1.23 +};
    1.24 +
    1.25 +RollWidget::RollWidget(QWidget *parent) :
    1.26 +	QWidget(parent)
    1.27 +{
    1.28 +	dice = 0;
    1.29 +
    1.30 +	QHBoxLayout *hbox = new QHBoxLayout(this);
    1.31 +
    1.32 +	cmb_type = new QComboBox;
    1.33 +	for(int i=0; dice_types[i].name; i++) {
    1.34 +		char buf[64];
    1.35 +		sprintf(buf, ":/data/dice/%s.png", dice_types[i].name);
    1.36 +		cmb_type->addItem(QIcon(buf), dice_types[i].name);
    1.37 +	}
    1.38 +	hbox->addWidget(cmb_type);
    1.39 +
    1.40 +	QPushButton *bn_roll = new QPushButton("roll");
    1.41 +	hbox->addWidget(bn_roll);
    1.42 +	connect(bn_roll, SIGNAL(clicked()), this, SLOT(roll()));
    1.43 +
    1.44 +	tx_res = new QLineEdit;
    1.45 +	tx_res->setReadOnly(true);
    1.46 +	hbox->addWidget(tx_res);
    1.47 +
    1.48 +	QPushButton *bn_rm = new QPushButton("remove");
    1.49 +	hbox->addWidget(bn_rm);
    1.50 +}
    1.51 +
    1.52 +void RollWidget::set_roll(Roll *roll)
    1.53 +{
    1.54 +	dice = roll;
    1.55 +}
    1.56 +
    1.57 +Roll *RollWidget::get_roll() const
    1.58 +{
    1.59 +	return dice;
    1.60 +}
    1.61 +
    1.62 +
    1.63 +void RollWidget::roll()
    1.64 +{
    1.65 +	if(!dice) return;
    1.66 +
    1.67 +	int sel = cmb_type->currentIndex();
    1.68 +	dice->set_sides(dice_types[sel].sides);
    1.69 +	int value = dice->roll();
    1.70 +
    1.71 +	QString text;
    1.72 +	text.setNum(value);
    1.73 +	tx_res->setText(text);
    1.74 +
    1.75 +	emit value_changed(value);
    1.76 +}