# HG changeset patch # User John Tsiombikas # Date 1387678130 -7200 # Node ID 92377189a5c61c07dbf100145db497f4ce499d6c # Parent 0e9c16d77e47a5fd7f17a126e7c725d61166b6fc moving along diff -r 0e9c16d77e47 -r 92377189a5c6 data/dice/d10.png Binary file data/dice/d10.png has changed diff -r 0e9c16d77e47 -r 92377189a5c6 data/dice/d100.png Binary file data/dice/d100.png has changed diff -r 0e9c16d77e47 -r 92377189a5c6 data/dice/d12.png Binary file data/dice/d12.png has changed diff -r 0e9c16d77e47 -r 92377189a5c6 data/dice/d20.png Binary file data/dice/d20.png has changed diff -r 0e9c16d77e47 -r 92377189a5c6 data/dice/d4.png Binary file data/dice/d4.png has changed diff -r 0e9c16d77e47 -r 92377189a5c6 data/dice/d6.png Binary file data/dice/d6.png has changed diff -r 0e9c16d77e47 -r 92377189a5c6 data/dice/d8.png Binary file data/dice/d8.png has changed diff -r 0e9c16d77e47 -r 92377189a5c6 data/icons/edit-add-2.png Binary file data/icons/edit-add-2.png has changed diff -r 0e9c16d77e47 -r 92377189a5c6 data/icons/edit-clear-2.png Binary file data/icons/edit-clear-2.png has changed diff -r 0e9c16d77e47 -r 92377189a5c6 qnetdice.pro --- a/qnetdice.pro Thu Dec 19 13:34:40 2013 +0200 +++ b/qnetdice.pro Sun Dec 22 04:08:50 2013 +0200 @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core gui +QT += core gui network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -15,10 +15,18 @@ SOURCES +=\ src/mainwin.cc \ src/main.cc \ - src/dicedialog.cc + src/rollwidget.cc \ + src/roll.cc \ + src/logger.cc HEADERS += src/mainwin.h \ - src/dicedialog.h + src/rollwidget.h \ + src/roll.h \ + src/main.h \ + src/logger.h FORMS += ui/mainwin.ui \ - ui/dicedialog.ui + ui/server.ui + +RESOURCES += \ + qnetdice.qrc diff -r 0e9c16d77e47 -r 92377189a5c6 qnetdice.qrc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/qnetdice.qrc Sun Dec 22 04:08:50 2013 +0200 @@ -0,0 +1,13 @@ + + + data/dice/d4.png + data/dice/d6.png + data/dice/d8.png + data/dice/d10.png + data/dice/d12.png + data/dice/d20.png + data/dice/d100.png + data/icons/edit-add-2.png + data/icons/edit-clear-2.png + + diff -r 0e9c16d77e47 -r 92377189a5c6 src/logger.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/logger.cc Sun Dec 22 04:08:50 2013 +0200 @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include "logger.h" + +struct LogTarget { + enum LogTargetType { LOG_TYPE_STREAM, LOG_TYPE_CALLBACK } type; + FILE *fp; + void (*func)(const char*, void*); + void *func_cls; +}; + +static void logmsg(LogTarget &out, const char *msg); + +static LogTarget def_log_target = { LogTarget::LOG_TYPE_STREAM, stderr, 0, 0 }; +static std::list targets; + +void set_log_file(FILE *fp) +{ + LogTarget targ; + memset(&targ, 0, sizeof targ); + targ.type = LogTarget::LOG_TYPE_STREAM; + targ.fp = fp; + targets.push_back(targ); +} + +void set_log_callback(void (*cb)(const char*, void*), void *cls) +{ + LogTarget targ; + memset(&targ, 0, sizeof targ); + targ.type = LogTarget::LOG_TYPE_CALLBACK; + targ.func = cb; + targ.func_cls = cls; + targets.push_back(targ); +} + +void log_message(const char *fmt, ...) +{ + static char buf[512]; + + va_list ap; + va_start(ap, fmt); + vsnprintf(buf, sizeof buf, fmt, ap); + va_end(ap); + + if(targets.empty()) { + logmsg(def_log_target, buf); + } else { + std::list::iterator it = targets.begin(); + while(it != targets.end()) { + logmsg(*it++, buf); + } + } +} + + +static void logmsg(LogTarget &out, const char *msg) +{ + switch(out.type) { + case LogTarget::LOG_TYPE_STREAM: + fputs(msg, out.fp); + fflush(out.fp); + break; + + case LogTarget::LOG_TYPE_CALLBACK: + out.func(msg, out.func_cls); + break; + + default: + break; + } +} diff -r 0e9c16d77e47 -r 92377189a5c6 src/logger.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/logger.h Sun Dec 22 04:08:50 2013 +0200 @@ -0,0 +1,9 @@ +#ifndef LOGGER_H_ +#define LOGGER_H_ + +void set_log_file(FILE *fp); +void set_log_callback(void (*cb)(const char*, void*), void *cls); + +void log_message(const char *msg, ...); + +#endif // LOGGER_H_ diff -r 0e9c16d77e47 -r 92377189a5c6 src/main.cc --- a/src/main.cc Thu Dec 19 13:34:40 2013 +0200 +++ b/src/main.cc Sun Dec 22 04:08:50 2013 +0200 @@ -1,11 +1,72 @@ +#include +#include +#include +#include +#include +#include "main.h" #include "mainwin.h" -#include +#include "roll.h" +#include "logger.h" + +static void new_client(); +static void status_log(const char *msg, void *cls); + +std::vector dice; +QTcpServer *server; +std::vector clients; + +QTcpSocket *socket; + +static std::string srv_passwd; +static MainWindow win_main; int main(int argc, char *argv[]) { - QApplication a(argc, argv); - MainWindow w; - w.show(); + srand(time(0)); - return a.exec(); + QApplication app(argc, argv); + win_main.show(); + + set_log_file(stdout); + set_log_callback(status_log, win_main.statusBar()); + + return app.exec(); } + + +void start_server(int port, const char *passwd) +{ + if(server) return; + + log_message("starting server on port: %d\n", port); + + server = new QTcpServer; + server->listen(QHostAddress::Any, port); + srv_passwd = passwd; + + QObject::connect(server, &QTcpServer::newConnection, new_client); +} + + +static void new_client() +{ + QTcpSocket *sock = server->nextPendingConnection(); + if(!sock) return; + + QHostAddress client_addr = sock->peerAddress(); + clients.push_back(sock); + + log_message("received connection request from: %s\n", client_addr.toString().toUtf8().data()); +} + +void connect(const char *host, int port, const char *passwd) +{ + log_message("connecting to host: %s port: %d\n", host, port); +} + + +static void status_log(const char *msg, void *cls) +{ + QStatusBar *sbar = (QStatusBar*)cls; + sbar->showMessage(msg); +} diff -r 0e9c16d77e47 -r 92377189a5c6 src/main.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main.h Sun Dec 22 04:08:50 2013 +0200 @@ -0,0 +1,17 @@ +#ifndef MAIN_H_ +#define MAIN_H_ + +#include +#include +#include +#include "roll.h" + +extern std::vector dice; + +extern QTcpServer *server; +extern QTcpSocket *sock; + +void start_server(int port, const char *passwd); +void start_client(const char *host, int port, const char *passwd); + +#endif // MAIN_H_ diff -r 0e9c16d77e47 -r 92377189a5c6 src/mainwin.cc --- a/src/mainwin.cc Thu Dec 19 13:34:40 2013 +0200 +++ b/src/mainwin.cc Sun Dec 22 04:08:50 2013 +0200 @@ -1,14 +1,73 @@ #include "mainwin.h" #include "ui_mainwin.h" +#include "ui_server.h" +#include "rollwidget.h" +#include "main.h" -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::MainWindow) +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent), + ui(new Ui::MainWindow) { - ui->setupUi(this); + ui->setupUi(this); + + dicebox = new QVBoxLayout; + ui->sarea_main_cont->setLayout(dicebox); + + connect(ui->bn_add, SIGNAL(clicked()), this, SLOT(add_dice())); + connect(ui->bn_clear, SIGNAL(clicked()), this, SLOT(clear_dice())); + connect(ui->bn_roll_all, SIGNAL(clicked()), this, SLOT(roll_all())); + + //connect(ui->action_connect, SIGNAL(triggered()), this, SLOT(start_client())); + //connect(ui->action_start_server, SIGNAL(triggered()), this, SLOT(start_server())); } MainWindow::~MainWindow() { - delete ui; + delete ui; } + + +void MainWindow::roll_all() +{ + for(int i=0; icount() - 1; i++) { + RollWidget *roll = (RollWidget*)dicebox->itemAt(i)->widget(); + roll->roll(); + } +} + + +void MainWindow::add_dice() +{ + RollWidget *w = new RollWidget; + dicebox->takeAt(dicebox->count() - 1); + dicebox->addWidget(w); + dicebox->addStretch(); + + Roll *roll = new Roll; + roll->set_sides(4); + dice.push_back(roll); + w->set_roll(roll); +} + +void MainWindow::clear_dice() +{ + while(!dicebox->isEmpty()) { + RollWidget *roll = (RollWidget*)dicebox->takeAt(0)->widget(); + delete roll; + } + + for(size_t i=0; isetupUi(0); +} + +void MainWindow::start_server() +{ +} diff -r 0e9c16d77e47 -r 92377189a5c6 src/mainwin.h --- a/src/mainwin.h Thu Dec 19 13:34:40 2013 +0200 +++ b/src/mainwin.h Sun Dec 22 04:08:50 2013 +0200 @@ -1,22 +1,31 @@ -#ifndef MAINWIN_H -#define MAINWIN_H +#ifndef MAINWIN_H_ +#define MAINWIN_H_ #include +#include +#include "rollwidget.h" namespace Ui { class MainWindow; } -class MainWindow : public QMainWindow -{ - Q_OBJECT +class MainWindow : public QMainWindow { + Q_OBJECT +private: + Ui::MainWindow *ui; + QVBoxLayout *dicebox; + +private slots: + void roll_all(); + void add_dice(); + void clear_dice(); + + void start_client(); + void start_server(); public: - explicit MainWindow(QWidget *parent = 0); - ~MainWindow(); - -private: - Ui::MainWindow *ui; + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); }; -#endif // MAINWIN_H +#endif // MAINWIN_H_ diff -r 0e9c16d77e47 -r 92377189a5c6 src/roll.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/roll.cc Sun Dec 22 04:08:50 2013 +0200 @@ -0,0 +1,45 @@ +#include +#include +#include "roll.h" + +Roll::Roll(int sides) +{ + this->sides = sides; + value = 0; +} + +void Roll::set_name(const char *name) +{ + this->name = std::string(name); +} + +void Roll::set_sides(int sides) +{ + this->sides = sides; +} + +void Roll::set_value(int value) +{ + this->value = value; +} + +const char *Roll::get_name() const +{ + return name.c_str(); +} + +int Roll::get_sides() const +{ + return sides; +} + +int Roll::get_value() const +{ + return value; +} + +int Roll::roll() +{ + value = (int)floor((double)sides * (double)rand() / (double)RAND_MAX); + return value; +} diff -r 0e9c16d77e47 -r 92377189a5c6 src/roll.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/roll.h Sun Dec 22 04:08:50 2013 +0200 @@ -0,0 +1,26 @@ +#ifndef ROLL_H_ +#define ROLL_H_ + +#include + +class Roll { +private: + int sides; + int value; + std::string name; + +public: + explicit Roll(int sides = 20); + + void set_name(const char *name); + void set_sides(int sides); + void set_value(int value); + + const char *get_name() const; + int get_sides() const; + int get_value() const; + + int roll(); +}; + +#endif // ROLL_H_ diff -r 0e9c16d77e47 -r 92377189a5c6 src/rollwidget.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/rollwidget.cc Sun Dec 22 04:08:50 2013 +0200 @@ -0,0 +1,73 @@ +#include +#include "rollwidget.h" +#include +#include +#include +#include + +static struct { + const char *name; + int sides; +} dice_types[] = { + { "d4", 4 }, + { "d6", 6 }, + { "d8", 8 }, + { "d10", 10 }, + { "d12", 12 }, + { "d20", 20 }, + { "d100", 100 }, + { 0, 0 } +}; + +RollWidget::RollWidget(QWidget *parent) : + QWidget(parent) +{ + dice = 0; + + QHBoxLayout *hbox = new QHBoxLayout(this); + + cmb_type = new QComboBox; + for(int i=0; dice_types[i].name; i++) { + char buf[64]; + sprintf(buf, ":/data/dice/%s.png", dice_types[i].name); + cmb_type->addItem(QIcon(buf), dice_types[i].name); + } + hbox->addWidget(cmb_type); + + QPushButton *bn_roll = new QPushButton("roll"); + hbox->addWidget(bn_roll); + connect(bn_roll, SIGNAL(clicked()), this, SLOT(roll())); + + tx_res = new QLineEdit; + tx_res->setReadOnly(true); + hbox->addWidget(tx_res); + + QPushButton *bn_rm = new QPushButton("remove"); + hbox->addWidget(bn_rm); +} + +void RollWidget::set_roll(Roll *roll) +{ + dice = roll; +} + +Roll *RollWidget::get_roll() const +{ + return dice; +} + + +void RollWidget::roll() +{ + if(!dice) return; + + int sel = cmb_type->currentIndex(); + dice->set_sides(dice_types[sel].sides); + int value = dice->roll(); + + QString text; + text.setNum(value); + tx_res->setText(text); + + emit value_changed(value); +} diff -r 0e9c16d77e47 -r 92377189a5c6 src/rollwidget.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/rollwidget.h Sun Dec 22 04:08:50 2013 +0200 @@ -0,0 +1,29 @@ +#ifndef ROLLWIDGET_H +#define ROLLWIDGET_H + +#include +#include +#include "roll.h" + +class RollWidget : public QWidget +{ + Q_OBJECT +private: + Roll *dice; + QComboBox *cmb_type; + QLineEdit *tx_res; + +public: + explicit RollWidget(QWidget *parent = 0); + + void set_roll(Roll *roll); + Roll *get_roll() const; + +signals: + void value_changed(int val); + +public slots: + void roll(); +}; + +#endif // ROLLWIDGET_H diff -r 0e9c16d77e47 -r 92377189a5c6 ui/mainwin.ui --- a/ui/mainwin.ui Thu Dec 19 13:34:40 2013 +0200 +++ b/ui/mainwin.ui Sun Dec 22 04:08:50 2013 +0200 @@ -6,8 +6,8 @@ 0 0 - 938 - 727 + 697 + 530 @@ -16,6 +16,52 @@ + + + + + Add + + + + :/data/icons/edit-add-2.png:/data/icons/edit-add-2.png + + + + + + + Clear + + + + :/data/icons/edit-clear-2.png:/data/icons/edit-clear-2.png + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Roll all + + + + + + true @@ -25,8 +71,8 @@ 0 0 - 918 - 594 + 677 + 383 @@ -39,8 +85,8 @@ 0 0 - 938 - 38 + 697 + 23 @@ -51,9 +97,9 @@ + - @@ -74,7 +120,6 @@ - @@ -98,6 +143,10 @@ + + + :/data/icons/edit-add-2.png:/data/icons/edit-add-2.png + &Add die @@ -109,6 +158,8 @@ - + + + diff -r 0e9c16d77e47 -r 92377189a5c6 ui/server.ui --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/server.ui Sun Dec 22 04:08:50 2013 +0200 @@ -0,0 +1,132 @@ + + + ServerDialog + + + + 0 + 0 + 378 + 233 + + + + Start server + + + + + + + + Port + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + Password + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + QLineEdit::PasswordEchoOnEdit + + + + + + + + + Qt::Vertical + + + + 20 + 113 + + + + + + + + + + &Cancel + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + &Start Server + + + + + + + + + + + pushButton + clicked() + ServerDialog + accept() + + + 315 + 207 + + + 276 + 139 + + + + + pushButton_2 + clicked() + ServerDialog + reject() + + + 70 + 204 + + + 23 + 137 + + + + +