qnetdice

view 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 source
1 #include <stdio.h>
2 #include "rollwidget.h"
3 #include <QHBoxLayout>
4 #include <QComboBox>
5 #include <QLineEdit>
6 #include <QPushButton>
8 static struct {
9 const char *name;
10 int sides;
11 } dice_types[] = {
12 { "d4", 4 },
13 { "d6", 6 },
14 { "d8", 8 },
15 { "d10", 10 },
16 { "d12", 12 },
17 { "d20", 20 },
18 { "d100", 100 },
19 { 0, 0 }
20 };
22 RollWidget::RollWidget(QWidget *parent) :
23 QWidget(parent)
24 {
25 dice = 0;
27 QHBoxLayout *hbox = new QHBoxLayout(this);
29 cmb_type = new QComboBox;
30 for(int i=0; dice_types[i].name; i++) {
31 char buf[64];
32 sprintf(buf, ":/data/dice/%s.png", dice_types[i].name);
33 cmb_type->addItem(QIcon(buf), dice_types[i].name);
34 }
35 hbox->addWidget(cmb_type);
37 QPushButton *bn_roll = new QPushButton("roll");
38 hbox->addWidget(bn_roll);
39 connect(bn_roll, SIGNAL(clicked()), this, SLOT(roll()));
41 tx_res = new QLineEdit;
42 tx_res->setReadOnly(true);
43 hbox->addWidget(tx_res);
45 QPushButton *bn_rm = new QPushButton("remove");
46 hbox->addWidget(bn_rm);
47 }
49 void RollWidget::set_roll(Roll *roll)
50 {
51 dice = roll;
52 }
54 Roll *RollWidget::get_roll() const
55 {
56 return dice;
57 }
60 void RollWidget::roll()
61 {
62 if(!dice) return;
64 int sel = cmb_type->currentIndex();
65 dice->set_sides(dice_types[sel].sides);
66 int value = dice->roll();
68 QString text;
69 text.setNum(value);
70 tx_res->setText(text);
72 emit value_changed(value);
73 }