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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
// Copyright (C) 2011, Gabriel Dos Reis.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// - Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the
// distribution.
//
// - Neither the name of OpenAxiom. nor the names of its contributors
// may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
#include <QScrollBar>
#include <QApplication>
#include "conversation.h"
#include "debate.h"
namespace OpenAxiom {
// Measurement in pixel of the em unit in the given font `f'.
static QSize em_metrics(const QWidget* w) {
const QFontMetrics fm = w->fontMetrics();
return QSize(fm.width(QLatin1Char('m')), fm.height());
}
// Return true if the QString `s' is morally an empty string.
// QT makes a difference between a null string and an empty string.
// That distinction is largely pedantic and without difference
// for most of our practical purposes.
static bool
empty_string(const QString& s) {
return s.isNull() or s.isEmpty();
}
// Return a resonable margin for this frame.
static int our_margin(const QFrame* f) {
return 2 + f->frameWidth();
}
// --------------------
// -- OutputTextArea --
// --------------------
OutputTextArea::OutputTextArea(QWidget* p)
: Base(p) {
setReadOnly(true); // this is a output only area.
setLineWrapMode(NoWrap); // for the time being, mess with nothing.
setFont(p->font());
setViewportMargins(0, 0, 0, 0);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
// We do not want to see scroll bars. Usually disallowing vertical
// scroll bars and allocating enough horizontal space is sufficient
// to ensure that we don't see any horizontal scrollbar.
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setLineWidth(1);
}
// This overriding implementation is so that we can control the
// amount of vertical space in the read-only editor viewport allocated
// for the display of output text. In particular we do not want
// scrollbars.
QSize
OutputTextArea::sizeHint() const {
return QSize(width(), document()->lineCount() * fontMetrics().height());
}
// Concatenate two paragraphs.
static QString
accumulate_paragaphs(const QString& before, const QString& after) {
if (empty_string(before))
return after;
return before + "\n" + after;
}
void OutputTextArea::add_paragraph(const QString& s) {
setPlainText(accumulate_paragaphs(toPlainText(), s));
QSize sz = sizeHint();
sz.setWidth(parentWidget()->width() - our_margin(this));
resize(sz);
show();
updateGeometry();
}
void OutputTextArea::add_text(const QString& s) {
setPlainText(toPlainText() + s);
QSize sz = sizeHint();
const int w = parentWidget()->width() - 2 * frameWidth();
if (w > sz.width())
sz.setWidth(w);
resize(sz);
show();
updateGeometry();
}
// --------------
// -- Question --
// --------------
Question::Question(Exchange& e) : Base(&e), parent(&e) {
setBackgroundRole(QPalette::AlternateBase);
}
void Question::focusInEvent(QFocusEvent* e) {
setFrame(true);
update();
Base::focusInEvent(e);
}
void Question::focusOutEvent(QFocusEvent* e) {
setFrame(false);
update();
Base::focusOutEvent(e);
}
void Question::enterEvent(QEvent* e) {
setFrame(true);
update();
Base::enterEvent(e);
}
void Question::leaveEvent(QEvent* e) {
if (not hasFocus())
setFrame(false);
update();
Base::leaveEvent(e);
}
// ------------
// -- Answer --
// ------------
Answer::Answer(Exchange& e) : Base(&e), parent(&e) {
setFrameStyle(StyledPanel | Sunken);
}
// --------------
// -- Exchange --
// --------------
// Amount of pixel spacing between the query and reply areas.
const int spacing = 2;
// Return a monospace font
static QFont monospace_font() {
QFont f("Courier");
f.setStyleHint(QFont::TypeWriter);
return f;
}
// The layout within an exchange is as follows:
// -- input area (an editor) with its own decoation accounted for.
// -- an optional spacing
// -- an output area with its own decoration accounted for.
QSize Exchange::sizeHint() const {
const int m = our_margin(this);
QSize sz = question()->size() + QSize(2 * m, 2 * m);
if (not answer()->isHidden())
sz.rheight() += answer()->height() + spacing;
return sz;
}
// Dress the query area with initial properties.
static void
prepare_query_widget(Conversation& conv, Exchange* e) {
Question* q = e->question();
q->setFrame(false);
q->setFont(conv.font());
const int m = our_margin(e);
q->setGeometry(m, m, conv.width() - 2 * m, q->height());
}
// Dress the reply aread with initial properties.
// Place the reply widget right below the frame containing
// the query widget; make both of the same width, of course.
static void
prepare_reply_widget(Conversation& conv, Exchange* e) {
Answer* a = e->answer();
Question* q = e->question();
const QPoint pt = e->question()->geometry().bottomLeft();
const int m = our_margin(a);
a->setGeometry(pt.x(), pt.y() + spacing,
conv.width() - 2 * m, q->height());
a->setBackgroundRole(q->backgroundRole());
a->hide(); // nothing to show yet
}
static void
finish_exchange_make_up(Conversation& conv, Exchange* e) {
e->setAutoFillBackground(true);
e->move(conv.bottom_left());
}
Exchange::Exchange(Conversation& conv, int n)
: QFrame(&conv), parent(&conv), no(n),
query(*this), reply(*this) {
setLineWidth(1);
setFont(conv.font());
prepare_query_widget(conv, this);
prepare_reply_widget(conv, this);
finish_exchange_make_up(conv, this);
connect(question(), SIGNAL(returnPressed()),
this, SLOT(reply_to_query()));
}
// Transfter focus to this input area.
static void
give_focus_to(Question* q) {
q->setFocus(Qt::OtherFocusReason);
}
static void ensure_visibility(Debate* debate, Exchange* e) {
const int y = e->y() + e->height();
QScrollBar* vbar = debate->verticalScrollBar();
const int value = vbar->value();
int new_value = y - vbar->pageStep();
if (y < value)
vbar->setValue(std::max(new_value, 0));
else if (new_value > value)
vbar->setValue(std::min(new_value, vbar->maximum()));
give_focus_to(e->question());
}
void
Exchange::reply_to_query() {
QString input = question()->text().trimmed();
if (empty_string(input))
return;
topic()->submit_query(input);
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
}
void Exchange::resizeEvent(QResizeEvent* e) {
QFrame::resizeEvent(e);
const int w = width() - 2 * our_margin(this);
if (w > question()->width()) {
question()->resize(w, question()->height());
answer()->resize(w, answer()->height());
}
}
// ------------
// -- Banner --
// ------------
Banner::Banner(Conversation* conv) : Base(conv) {
setFrameStyle(StyledPanel | Raised);
setBackgroundRole(QPalette::Base);
}
// ------------------
// -- Conversation --
// -------------------
// Default number of characters per question line.
const int columns = 80;
const int lines = 25;
static QSize
minimum_preferred_size(const Conversation* conv) {
const QSize em = em_metrics(conv);
return QSize(columns * em.width(), lines * em.height());
}
// Set a minimum preferred widget size, so no layout manager
// messes with it. Indicate we can make use of more space.
Conversation::Conversation(Debate& parent)
: group(parent), greatings(this), cur_ex(), cur_out(&greatings) {
setFont(monospace_font());
setBackgroundRole(QPalette::Base);
greatings.setFont(font());
oracle()->setProcessChannelMode(QProcess::MergedChannels);
connect(oracle(), SIGNAL(readyReadStandardOutput()),
this, SLOT(read_reply()));
// connect(oracle(), SIGNAL(readyReadStandardError()),
// this, SLOT(read_reply()));
}
Conversation::~Conversation() {
for (int i = children.size() -1 ; i >= 0; --i)
delete children[i];
if (oracle()->state() == QProcess::Running)
oracle()->terminate();
}
QPoint Conversation::bottom_left() const {
if (length() == 0)
return greatings.geometry().bottomLeft();
return children.back()->geometry().bottomLeft();
}
static QSize
round_up_height(const QSize& sz, int height) {
if (height < 1)
height = 1;
const int n = (sz.height() + height) / height;
return QSize(sz.width(), n * height);
}
QSize Conversation::sizeHint() const {
const int view_height = debate()->viewport()->height();
const int n = length();
if (n == 0)
return round_up_height(minimum_preferred_size(this), view_height);
QSize sz = greatings.size();
for (int i = 0; i < n; ++i)
sz.rheight() += children[i]->height();
return round_up_height(sz, view_height);
}
void Conversation::resizeEvent(QResizeEvent* e) {
Base::resizeEvent(e);
setMinimumSize(size());
const QSize sz = size();
if (e->oldSize() == sz)
return;
greatings.resize(sz.width(), greatings.height());
for (int i = 0; i < length(); ++i) {
Exchange* e = children[i];
e->resize(sz.width(), e->height());
}
}
void Conversation::paintEvent(QPaintEvent* e) {
QWidget::paintEvent(e);
if (length() == 0)
greatings.update();
}
Exchange*
Conversation::new_topic() {
Exchange* w = new Exchange(*this, length() + 1);
w->show();
children.push_back(w);
adjustSize();
updateGeometry();
cur_out = w->answer();
return cur_ex = w;
}
Exchange*
Conversation::next(Exchange* w) {
if (w == 0 or w->number() == length())
return new_topic();
return cur_ex = children[w->number()];
}
struct OracleOutput {
QString result;
QString prompt;
};
static OracleOutput
read_output(QProcess& proc) {
OracleOutput output;
QStringList strs = QString::fromLocal8Bit(proc.readAll()).split('\n');
QStringList new_list;
QRegExp rx("\\(\\d+\\)\\s->");
while (not strs.isEmpty()) {
QString s = strs.takeFirst();
if (empty_string(s))
continue;
if (rx.indexIn(s) != -1) {
output.prompt = s;
break;
}
new_list.append(s);
}
output.result =new_list.join("\n");
return output;
}
static bool
empty(const OracleOutput& out) {
return empty_string(out.result) and empty_string(out.prompt);
}
void
Conversation::read_reply() {
OracleOutput output = read_output(proc);
if (empty(output))
return;
if (not empty_string(output.result)) {
cur_out->add_paragraph(output.result);
adjustSize();
}
if (length() == 0) {
if (not empty_string(output.prompt))
ensure_visibility(debate(), new_topic());
}
else {
exchange()->adjustSize();
exchange()->update();
exchange()->updateGeometry();
if (empty_string(output.prompt))
ensure_visibility(debate(), exchange());
else {
ensure_visibility(debate(), next(exchange()));
QApplication::restoreOverrideCursor();
}
}
}
void
Conversation::submit_query(const QString& s) {
oracle()->write(s.toAscii());
oracle()->write("\n");
}
}
|