Skip to content

Commit

Permalink
Removed old console, use new one
Browse files Browse the repository at this point in the history
  • Loading branch information
etet100 committed Mar 24, 2024
1 parent 0f1742c commit eb0f46a
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 226 deletions.
2 changes: 1 addition & 1 deletion src/candle/communicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ SendCommandResult Communicator::sendCommand(QString command, int tableIndex, boo
if (showInConsole) {
// @todo ui
// writeConsole(command);
ca.consoleIndex = ui->txtConsole->blockCount() - 1;
ca.consoleIndex = -1; //ui->txtConsole->blockCount() - 1;
} else {
ca.consoleIndex = -1;
}
Expand Down
71 changes: 69 additions & 2 deletions src/candle/form_partial/main/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,77 @@

#include "console.h"
#include "ui_console.h"
#include <QScrollBar>
#include <QTextBlock>

partMainConsole::partMainConsole(QWidget *parent)
partMainConsole::partMainConsole(QWidget *parent, const ConfigurationConsole &configurationConsole)
: QWidget(parent)
, ui(new Ui::partMainConsole)
, m_configurationConsole(configurationConsole)
{
ui->setupUi(this);

if (m_configurationConsole.darkBackgroundMode()) {
applyDarkBackgroundMode();
}

#ifndef UNIX
ui->cboCommand->setStyleSheet("QComboBox {padding: 2;} QComboBox::drop-down {width: 0; border-style: none;} QComboBox::down-arrow {image: url(noimg); border-width: 0;}");
#endif

ui->cboCommand->setMinimumHeight(ui->cboCommand->height());
ui->cmdClearConsole->setFixedHeight(ui->cboCommand->height());
ui->cmdCommandSend->setFixedHeight(ui->cboCommand->height());

ui->cboCommand->setAutoCompletion(m_configurationConsole.commandAutoCompletion());

// restore command history
ui->cboCommand->addItems(m_configurationConsole.commandHistory());
ui->cboCommand->setCurrentIndex(-1);
}

partMainConsole::~partMainConsole()
{
delete ui;
}

void partMainConsole::append(QString text)
void partMainConsole::applyDarkBackgroundMode()
{
ui->txtConsole->setStyleSheet("QPlainTextEdit { background-color: #000000; color: #FFFFFF; }");
}

int partMainConsole::append(QString text)
{
ui->txtConsole->appendPlainText(text);

return ui->txtConsole->document()->blockCount() - 1;
}

void partMainConsole::appendResponse(int consoleIndex, QString command, QString response)
{
bool scrolledDown = this->isScrolledToEnd();

QTextBlock block = ui->txtConsole->document()->findBlockByNumber(consoleIndex);
if (!block.isValid() || block.text() != command) return;

QTextCursor cursor(block);

// Update text block numbers
// @TODO response is added as multiple lines, do we have to do this?
// int blocksAdded = response.count("; ");

// if (blocksAdded > 0) for (int i = 0; i < m_communicator->m_commands.count(); i++) {
// if (m_communicator->m_commands[i].consoleIndex != -1) m_communicator->m_commands[i].consoleIndex += blocksAdded;
// }

cursor.beginEditBlock();
cursor.movePosition(QTextCursor::EndOfBlock);
// @TODO response was added as multiple lines, do we have to do this?
// cursor.insertText(" < " + QString(response).replace("; ", "\r\n"));
cursor.insertText(" < " + QString(response));
cursor.endEditBlock();

if (scrolledDown) this->scrollToEnd();
}

void partMainConsole::clear()
Expand All @@ -37,6 +92,18 @@ void partMainConsole::send()
emit newCommand(command);
}

bool partMainConsole::isScrolledToEnd()
{
return ui->txtConsole->verticalScrollBar()->value()
== ui->txtConsole->verticalScrollBar()->maximum();
}

void partMainConsole::scrollToEnd()
{
ui->txtConsole->verticalScrollBar()->setValue(
ui->txtConsole->verticalScrollBar()->maximum());
}

void partMainConsole::onClearClicked()
{
clear();
Expand Down
18 changes: 15 additions & 3 deletions src/candle/form_partial/main/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define CONSOLE_H

#include <QWidget>
#include "../../config/module/configurationconsole.h"

namespace Ui {
class partMainConsole;
Expand All @@ -16,9 +17,10 @@ class partMainConsole : public QWidget
Q_OBJECT

public:
explicit partMainConsole(QWidget *parent = nullptr);
explicit partMainConsole(QWidget *parent, const ConfigurationConsole &configurationConsole);
~partMainConsole();
void append(QString text);
int append(QString text);
void appendResponse(int consoleIndex, QString command, QString response);
void clear();

signals:
Expand All @@ -27,9 +29,19 @@ class partMainConsole : public QWidget

private:
Ui::partMainConsole *ui;
const ConfigurationConsole &m_configurationConsole;
void send();
bool isScrolledToEnd();
void scrollToEnd();

private slots:
int m_commandIndex = 0; // won't be cleared even if the console is cleared
// int m_minIndex = 0;
// int m_maxIndex = 0;


void applyDarkBackgroundMode();

private slots:
void onClearClicked();
void onSendClicked();
};
Expand Down
Loading

0 comments on commit eb0f46a

Please sign in to comment.