Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
// Copyright (C) 2011-2013, Gabriel Dos Reis.
2
// All rights reserved.
3
// Written by Gabriel Dos Reis.
4
//
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
7
// met:
8
//
9
// - Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
//
12
// - Redistributions in binary form must reproduce the above copyright
13
// notice, this list of conditions and the following disclaimer in
14
// the documentation and/or other materials provided with the
15
// distribution.
16
//
17
// - Neither the name of OpenAxiom. nor the names of its contributors
18
// may be used to endorse or promote products derived from this
19
// software without specific prior written permission.
20
//
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33
#ifndef OPENAXIOM_CONVERSATION_INCLUDED
34
#define OPENAXIOM_CONVERSATION_INCLUDED
35
36
#include <vector>
37
#include <QFrame>
38
#include <QLineEdit>
39
#include <QTextEdit>
40
#include <QFont>
41
#include <QEvent>
42
#include <QResizeEvent>
43
#include <QPaintEvent>
44
#include <QRegExp>
45
#include "server.h"
46
47
namespace OpenAxiom {
48
// A conversation is a set of exchanges. An exchange is a question
49
// followed by an answer. A conversation that takes place in a
50
// a certain frame is a debate.
51
class Debate;
52
class Conversation;
53
class Exchange;
54
class Question;
55
class Answer;
56
57
class MainWindow;
58
59
// --------------------
60
// -- OutputTextArea --
61
// --------------------
62
// An output text area is a widget where we output text.
63
// The texts are accumulated, as opposed to overwritten.
64
class OutputTextArea : public QTextEdit {
65
typedef QTextEdit Base;
66
public:
67
explicit OutputTextArea(QWidget*);
68
// the metrics of this output area
69
QSize sizeHint() const;
70
// Add a new paragraph to existing texts. Paragraghs are
71
// separated by the newline character.
72
void add_paragraph(const QString&);
73
// Add accumulate new text.
74
void add_text(const QString&);
75
// Current cursor
76
QTextCursor& get_cursor() { return cur; }
77
OutputTextArea& insert_block(const QString&);
78
protected:
79
QTextCursor cur;
80
};
81
82
// ---------------
83
// -- Question --
84
// ---------------
85
// A question is just a one-liner query area.
86
struct Question : QLineEdit {
87
explicit Question(Exchange*);
88
89
protected:
90
void enterEvent(QEvent*);
91
void focusInEvent(QFocusEvent*);
92
};
93
94
// ------------
95
// -- Answer --
96
// ------------
97
struct Answer : OutputTextArea {
98
explicit Answer(Exchange*);
99
};
100
101
// --------------
102
// -- Exchange --
103
// --------------
104
class Exchange : public QFrame {
105
Q_OBJECT;
106
public:
107
Exchange(Conversation*, int);
108
109
// The widget holding the query area
110
Question* question() { return &query; }
111
const Question* question() const { return &query; }
112
Server* server() const;
113
114
// The widget holding the reply area.
115
Answer* answer() { return &reply; }
116
const Answer* answer() const { return &reply; }
117
118
// Conversion number
119
int number() const { return no; }
120
121
// Reimplement position management.
122
QSize sizeHint() const;
123
124
protected:
125
void resizeEvent(QResizeEvent*);
126
127
private:
128
Conversation* const win;
129
const int no;
130
Question query;
131
Answer reply;
132
133
private slots:
134
void reply_to_query();
135
};
136
137
// Conversation banner, welcome greatings.
138
class Banner : public OutputTextArea {
139
typedef OutputTextArea Base;
140
public:
141
explicit Banner(Conversation*);
142
};
143
144
// -- Set of conversations that make up a session.
145
// -- We remember and number each topic so that we
146
// -- can go back in the conversation set and reevaluate
147
// -- queries.
148
class Conversation : public QWidget {
149
Q_OBJECT;
150
public:
151
explicit Conversation(Debate*);
152
~Conversation();
153
154
Debate* debate() const { return win; }
155
156
// Holds if this conversation just started.
157
bool fresh() const { return children.empty(); }
158
159
// Number of exchanges in this conversation
160
int length() const { return children.size(); }
161
162
// Return the `i'-th conversation in this set, if any.
163
Exchange* operator[](int) const;
164
165
// Return the bottom left corner of the rectangle enclosing the
166
// the set of exchanges in this conversation.
167
QPoint bottom_left() const;
168
169
// Start a new conversation topic.
170
Exchange* new_topic();
171
172
// Override QWidegt::sizeHint. Return the cumulative sizes
173
// of all conversations so far.
174
QSize sizeHint() const;
175
176
// Return a pointer to the current exchange, if any.
177
Exchange* exchange() { return cur_ex; }
178
179
public slots:
180
// Return the topic following a given topic in this set of conversations
181
Exchange* next(Exchange*);
182
183
protected:
184
void resizeEvent(QResizeEvent*);
185
void paintEvent(QPaintEvent*);
186
187
private slots:
188
void read_reply();
189
190
private:
191
typedef std::vector<Exchange*> Children;
192
Debate* const win;
193
Banner greatings;
194
Children children;
195
Exchange* cur_ex;
196
OutputTextArea* cur_out;
197
QRegExp rx;
198
QRegExp tx;
199
};
200
}
201
202
#endif // OPENAXIOM_CONVERSATION_INCLUDED
203
204
205
// Local Variables:
206
// mode: c++
207
// End:
208
209