File: networkViewer.cc

package info (click to toggle)
gpsshogi 0.7.0-3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 111,280 kB
  • sloc: cpp: 80,962; perl: 12,610; ruby: 3,929; javascript: 1,631; makefile: 1,202; sh: 473; tcl: 166; ansic: 67
file content (158 lines) | stat: -rw-r--r-- 3,204 bytes parent folder | download | duplicates (3)
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
#include "networkViewer.h"
#include "moveList.h"

#include "gpsshogi/gui/util.h"
#include "gpsshogi/gui/board.h"

#include "osl/record/csaRecord.h"

#include <qmessagebox.h>
#include <qlayout.h>
#include <qstring.h>
#include <qlayout.h>
#include <iostream>

using gpsshogi::gui::Util;
/* ======================================================================
 * NetworkViewer
 *  the viewer, with the board on the left and chat screen on the right
 */
NetworkViewer::
NetworkViewer(QWidget *parent)
  : BoardTabChild(parent), index(0)
{
  board->setManualMovement(false);

  QHBoxLayout *mainLayout = new QHBoxLayout();
  moveList = new MoveList();
  mainLayout->addWidget(moveList);
  mainLayout->addWidget(board);

  QVBoxLayout *sideLayout = new QVBoxLayout();
  networkClient = new NetworkClient();
  QObject::connect(networkClient, SIGNAL(gotBoard(QString)),
		   SLOT(receive_board(QString)));
  QObject::connect(networkClient, SIGNAL(gotLastMove(QString)),
		   SLOT(receive_last_move(QString)));
  QObject::connect(networkClient, SIGNAL(chatReceived()),
		   this, SIGNAL(chatReceived()));
  sideLayout->addWidget(networkClient);

  mainLayout->addLayout(sideLayout);

  setLayout(mainLayout);
}

void
NetworkViewer::
setHostname(QString str)
{
  networkClient->setHostname(str);
}

void
NetworkViewer::
setUsername(QString str)
{
  networkClient->setUsername(str);
}

void
NetworkViewer::
setPassword(QString str)
{
  networkClient->setPassword(str);
}

void
NetworkViewer::
connect()
{
  networkClient->openConnection();
}

void
NetworkViewer::
receive_board(QString content)
{
  try {
    std::istringstream is(content.toStdString());
    osl::CsaFile csa(is);
    std::vector<osl::Move> moves;
    std::vector<int> consumed_time;
    csa.load().load(moves, consumed_time);

    initialState = csa.initialState();
    board->setState(csa.initialState());
    moveList->setState(initialState, moves, consumed_time, std::vector<QString>());
  } catch (osl::CsaIOError& e) {
    std::cerr << "receive_board " << e.what() << "\n" << content.toStdString() << "\n";
  }
}

void
NetworkViewer::
receive_last_move(QString content)
{
  osl::Move move = osl::csa::strToMove(content.toStdString(), board->getState());
  moveList->pushMove(move);
  toLastState();
}

void NetworkViewer::forward()
{
  if (index > (int)moveList->numMoves())
    index++;

  updateState();
}

void NetworkViewer::backward()
{
  if (index > 0)
    index--;

  updateState();
}

void NetworkViewer::updateState()
{
  osl::NumEffectState state(initialState);
  for (int i = 0; i < index; i++)
  {
    state.makeMove(moveList->getMove(i));
  }
  board->setState(state);
}

void NetworkViewer::toLastState()
{
  index = moveList->numMoves();
  updateState();
}

int NetworkViewer::moveCount() const
{
  return index;
}

osl::NumEffectState NetworkViewer::
getStateAndMovesToCurrent(std::vector<osl::Move> &moves)
{
  moves.reserve(index);
  for (int i = 0; i < index; i++)
    moves.push_back(moveList->getMove(i));

  return initialState;
}

void NetworkViewer::paintEvent(QPaintEvent *event)
{
  emit painted();
  BoardTabChild::paintEvent(event);
}

// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: