open-axiom repository from github
// Copyright (C) 2011-2013, Gabriel Dos Reis.1// All rights reserved.2// Written by Gabriel Dos Reis.3//4// Redistribution and use in source and binary forms, with or without5// modification, are permitted provided that the following conditions are6// met:7//8// - Redistributions of source code must retain the above copyright9// notice, this list of conditions and the following disclaimer.10//11// - Redistributions in binary form must reproduce the above copyright12// notice, this list of conditions and the following disclaimer in13// the documentation and/or other materials provided with the14// distribution.15//16// - Neither the name of OpenAxiom. nor the names of its contributors17// may be used to endorse or promote products derived from this18// software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS21// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED22// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A23// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER24// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,25// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,26// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR27// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF28// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING29// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.3132#ifndef OPENAXIOM_CONVERSATION_INCLUDED33#define OPENAXIOM_CONVERSATION_INCLUDED3435#include <vector>36#include <QFrame>37#include <QLineEdit>38#include <QTextEdit>39#include <QFont>40#include <QEvent>41#include <QResizeEvent>42#include <QPaintEvent>43#include <QRegExp>44#include "server.h"4546namespace OpenAxiom {47// A conversation is a set of exchanges. An exchange is a question48// followed by an answer. A conversation that takes place in a49// a certain frame is a debate.50class Debate;51class Conversation;52class Exchange;53class Question;54class Answer;5556class MainWindow;5758// --------------------59// -- OutputTextArea --60// --------------------61// An output text area is a widget where we output text.62// The texts are accumulated, as opposed to overwritten.63class OutputTextArea : public QTextEdit {64typedef QTextEdit Base;65public:66explicit OutputTextArea(QWidget*);67// the metrics of this output area68QSize sizeHint() const;69// Add a new paragraph to existing texts. Paragraghs are70// separated by the newline character.71void add_paragraph(const QString&);72// Add accumulate new text.73void add_text(const QString&);74// Current cursor75QTextCursor& get_cursor() { return cur; }76OutputTextArea& insert_block(const QString&);77protected:78QTextCursor cur;79};8081// ---------------82// -- Question --83// ---------------84// A question is just a one-liner query area.85struct Question : QLineEdit {86explicit Question(Exchange*);8788protected:89void enterEvent(QEvent*);90void focusInEvent(QFocusEvent*);91};9293// ------------94// -- Answer --95// ------------96struct Answer : OutputTextArea {97explicit Answer(Exchange*);98};99100// --------------101// -- Exchange --102// --------------103class Exchange : public QFrame {104Q_OBJECT;105public:106Exchange(Conversation*, int);107108// The widget holding the query area109Question* question() { return &query; }110const Question* question() const { return &query; }111Server* server() const;112113// The widget holding the reply area.114Answer* answer() { return &reply; }115const Answer* answer() const { return &reply; }116117// Conversion number118int number() const { return no; }119120// Reimplement position management.121QSize sizeHint() const;122123protected:124void resizeEvent(QResizeEvent*);125126private:127Conversation* const win;128const int no;129Question query;130Answer reply;131132private slots:133void reply_to_query();134};135136// Conversation banner, welcome greatings.137class Banner : public OutputTextArea {138typedef OutputTextArea Base;139public:140explicit Banner(Conversation*);141};142143// -- Set of conversations that make up a session.144// -- We remember and number each topic so that we145// -- can go back in the conversation set and reevaluate146// -- queries.147class Conversation : public QWidget {148Q_OBJECT;149public:150explicit Conversation(Debate*);151~Conversation();152153Debate* debate() const { return win; }154155// Holds if this conversation just started.156bool fresh() const { return children.empty(); }157158// Number of exchanges in this conversation159int length() const { return children.size(); }160161// Return the `i'-th conversation in this set, if any.162Exchange* operator[](int) const;163164// Return the bottom left corner of the rectangle enclosing the165// the set of exchanges in this conversation.166QPoint bottom_left() const;167168// Start a new conversation topic.169Exchange* new_topic();170171// Override QWidegt::sizeHint. Return the cumulative sizes172// of all conversations so far.173QSize sizeHint() const;174175// Return a pointer to the current exchange, if any.176Exchange* exchange() { return cur_ex; }177178public slots:179// Return the topic following a given topic in this set of conversations180Exchange* next(Exchange*);181182protected:183void resizeEvent(QResizeEvent*);184void paintEvent(QPaintEvent*);185186private slots:187void read_reply();188189private:190typedef std::vector<Exchange*> Children;191Debate* const win;192Banner greatings;193Children children;194Exchange* cur_ex;195OutputTextArea* cur_out;196QRegExp rx;197QRegExp tx;198};199}200201#endif // OPENAXIOM_CONVERSATION_INCLUDED202203204// Local Variables:205// mode: c++206// End:207208209