Skip to content

Instantly share code, notes, and snippets.

@SpeedSX
Created February 9, 2025 20:11
Show Gist options
  • Save SpeedSX/6b614a1d432debf2ce346f06c6424484 to your computer and use it in GitHub Desktop.
Save SpeedSX/6b614a1d432debf2ce346f06c6424484 to your computer and use it in GitHub Desktop.
#include <QApplication>
#include <QMainWindow>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QString>
#include <QScrollBar>
class ElisaDialog : public QMainWindow {
Q_OBJECT
public:
ElisaDialog(QWidget *parent = nullptr) : QMainWindow(parent) {
setWindowTitle("ELISA Dialog");
setMinimumSize(600, 800);
// Create central widget and layout
QWidget *centralWidget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
layout->setSpacing(10);
layout->setContentsMargins(20, 20, 20, 20);
// Chat display area
chatDisplay = new QTextEdit(this);
chatDisplay->setReadOnly(true);
chatDisplay->setStyleSheet(
"QTextEdit {"
" background-color: white;"
" border: 1px solid #e0e0e0;"
" border-radius: 5px;"
" padding: 10px;"
" font-family: 'Segoe UI';"
" font-size: 14px;"
"}"
);
layout->addWidget(chatDisplay);
// Input area container
QHBoxLayout *inputLayout = new QHBoxLayout();
// Input field
inputField = new QLineEdit(this);
inputField->setPlaceholderText("Type your message here...");
inputField->setStyleSheet(
"QLineEdit {"
" border: 1px solid #e0e0e0;"
" border-radius: 5px;"
" padding: 10px;"
" font-family: 'Segoe UI';"
" font-size: 14px;"
" background-color: white;"
"}"
);
// Send button
sendButton = new QPushButton("Send", this);
sendButton->setStyleSheet(
"QPushButton {"
" background-color: #0084ff;"
" color: white;"
" border: none;"
" border-radius: 5px;"
" padding: 10px 20px;"
" font-family: 'Segoe UI';"
" font-size: 14px;"
"}"
"QPushButton:hover {"
" background-color: #0073e6;"
"}"
"QPushButton:pressed {"
" background-color: #0066cc;"
"}"
);
inputLayout->addWidget(inputField);
inputLayout->addWidget(sendButton);
layout->addLayout(inputLayout);
setCentralWidget(centralWidget);
// Connect signals and slots
connect(sendButton, &QPushButton::clicked, this, &ElisaDialog::handleInput);
connect(inputField, &QLineEdit::returnPressed, this, &ElisaDialog::handleInput);
// Initial greeting
chatDisplay->append("ELISA: Hello! How can I help you today?");
}
private slots:
void handleInput() {
QString input = inputField->text().trimmed();
if (!input.isEmpty()) {
// Display user input
chatDisplay->append("You: " + input);
// Generate and display response
QString response = generateResponse(input);
chatDisplay->append("ELISA: " + response);
chatDisplay->append(""); // Empty line for spacing
// Clear input field
inputField->clear();
// Scroll to bottom
chatDisplay->verticalScrollBar()->setValue(
chatDisplay->verticalScrollBar()->maximum()
);
}
}
private:
QTextEdit *chatDisplay;
QLineEdit *inputField;
QPushButton *sendButton;
QString generateResponse(const QString &input) {
QString lowercaseInput = input.toLower();
// Response patterns
struct Pattern {
QString keyword;
QString response;
};
static const Pattern patterns[] = {
{"hello", "Hello! How are you feeling today?"},
{"sad", "I'm sorry to hear that you're feeling sad. Would you like to talk about it?"},
{"happy", "That's wonderful! What made you feel happy?"},
{"bye", "Goodbye! Take care of yourself."},
{"thank", "You're welcome! Is there anything else on your mind?"},
{"help", "I'm here to listen and chat. What's troubling you?"},
{"love", "Love is a complex emotion. Tell me more about these feelings."},
{"angry", "I understand you're feeling angry. What happened?"},
{"why", "That's an interesting question. What makes you ask that?"},
{"because", "I see. And how does that make you feel?"}
};
for (const auto &pattern : patterns) {
if (lowercaseInput.contains(pattern.keyword)) {
return pattern.response;
}
}
return "Please tell me more about that.";
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// Set application-wide style
app.setStyle("Fusion");
ElisaDialog dialog;
dialog.show();
return app.exec();
}
#include "main.moc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment