Skip to content

Commit

Permalink
Transitioning to single message pathway between controller and kernel…
Browse files Browse the repository at this point in the history
…. Added C_CONSOLE_SET_TEXT_COLOR.
  • Loading branch information
jasonsikes committed Jun 12, 2018
1 parent e896b9b commit 91b0ad9
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 23 deletions.
22 changes: 10 additions & 12 deletions console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ void Console::setTextSize(double pointSize)
textFormat.setFont(f);
}

void Console::setTextColor(QVector<QColor> colors)
{
textFormat.setForeground(QBrush(colors.first()));
textFormat.setBackground(QBrush(colors.last()));
QPalette p = palette();
p.setBrush(QPalette::Base, QBrush(colors.last()));
setPalette(p);

}

// TODO: the control functions should be broken out.
void Console::printString(const QString &text) {
QTextCursor tc = textCursor();
Expand All @@ -88,18 +98,6 @@ void Console::printString(const QString &text) {
textFormat.setForeground(bg);
break;
}
case C_SET_TEXT_COLOR: {
QStringRef rowcolStringref = i->rightRef(i->size() - 1);
QVector<QStringRef> ary = rowcolStringref.split(C_DELIM);
if (ary.size() == 2) {
textFormat.setForeground(QBrush(QColor(ary[0].toString())));
textFormat.setBackground(QBrush(QColor(ary[1].toString())));
QPalette p = palette();
p.setBrush(QPalette::Base, QBrush(QColor(ary[1].toString())));
setPalette(p);
}
break;
}
case C_CLEAR_TEXT: {
QTextEdit::clear();
break;
Expand Down
1 change: 1 addition & 0 deletions console.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Console : public QTextEdit {
void getCursorPos(int &row, int &col);
void setTextSize(double pointSize);
void setCursorPosition(QVector<int> position);
void setTextColor(QVector<QColor> colors);
};

#endif // CONSOLE_H
2 changes: 2 additions & 0 deletions kernel_communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,9 @@ DatumP Kernel::excCursor(DatumP node) {
DatumP Kernel::excSettextcolor(DatumP node) {
ProcedureHelper h(this, node);
QColor foreground;
// TODO: current background color might not be white
QColor background = QColor("white");
// TODO: if foregroundP and backgroundP are unused, consider removing
DatumP foregroundP =
h.validatedDatumAtIndex(0, [&foreground, this](DatumP candidate) {
return colorFromDatumP(foreground, candidate);
Expand Down
9 changes: 7 additions & 2 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,22 @@ void MainWindow::takeMesage(const QByteArray &message)
const QString text = consolePrintStringFromMessage(message);
ui->mainConsole->printString(text);
break;
}
}
case C_CONSOLE_SET_TEXT_SIZE: {
double size = consoleSetTextSizeFromMessage(message);
ui->mainConsole->setTextSize(size);
break;
}
}
case C_CONSOLE_SET_CURSOR_POS: {
QVector<int> position = consoleSetCursorPosFromMessage(message);
ui->mainConsole->setCursorPosition(position);
break;
}
case C_CONSOLE_SET_TEXT_COLOR: {
QVector<QColor> colors = consoleSetTextColorFromMessage(message);
ui->mainConsole->setTextColor(colors);
break;
}
default:
break;
}
Expand Down
40 changes: 35 additions & 5 deletions message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
const QString consolePrintStringFromMessage(const QByteArray message)
{
const char *data = message.constData();
int *length = ((int*)&data[1]);
const QChar *str = ((const QChar *)&data[1 + sizeof(int)]);
int *length = (int*)&data[1];
const QChar *str = (const QChar *)&data[1 + sizeof(int)];
return QString::fromRawData(str, *length);
}

Expand All @@ -29,7 +29,7 @@ double consoleSetTextSizeFromMessage(const QByteArray message)
{
const char *data = message.constData();

double *val = ((double*)&data[1]);
double *val = (double*)&data[1];
return *val;
}

Expand All @@ -47,8 +47,9 @@ QVector<int> consoleSetCursorPosFromMessage(const QByteArray message)
{
const char *data = message.constData();

int *row = ((int*)&data[1]);
int *column = ((int*)&data[1 + sizeof(int)]);
int *row = (int*)&data[1];
int *column = (int*)&data[1 + sizeof(int)];

QVector<int> retval;
retval << *row;
retval << *column;
Expand All @@ -62,3 +63,32 @@ const QByteArray messageFromConsoleSetCursorPos(QVector<int> position)
message.append((char*)&position.last(), sizeof(int));
return message;
}

//
// C_CONSOLE_SET_TEXT_COLOR
//

QVector<QColor> consoleSetTextColorFromMessage(const QByteArray message)
{
const char *data = message.constData();

QRgba64 *foreground = (QRgba64 *) &data[1];
QRgba64 *background = (QRgba64 *) &data[1 + sizeof(QRgba64)];


QVector<QColor> retval;
retval << QColor(*foreground);
retval << QColor(*background);
return retval;
}

const QByteArray messageFromConsoleSetTextColor(QVector<QColor> colors)
{
QRgba64 foreground = colors.first().rgba64();
QRgba64 background = colors.last().rgba64();

QByteArray message(1, C_CONSOLE_SET_TEXT_COLOR);
message.append((char*)&foreground, sizeof(QRgba64));
message.append((char*)&background, sizeof(QRgba64));
return message;
}
13 changes: 12 additions & 1 deletion message.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
//===----------------------------------------------------------------------===//
#include <QString>
#include <QVector>
#include <QColor>

enum MessageCommandChar : char {
C_CONSOLE_PRINT_STRING,
Expand Down Expand Up @@ -59,14 +60,21 @@ const QByteArray messageFromConsolePrintString(const QString str);
/// * double size
const QByteArray messageFromConsoleSetTextSize(double size);


/// Create a C_CONSOLE_SET_CURSOR_POS message from two integers.
///
/// The format of the message is:
/// * int row
/// * int column
const QByteArray messageFromConsoleSetCursorPos(QVector<int> position);


/// Create a C_CONSOLE_SET_TEXT_COLOR message from two QColors.
///
/// The format of the message is:
/// * QRgba foreground
/// * QRgba background
const QByteArray messageFromConsoleSetTextColor(QVector<QColor> colors);

//
// MESSAGE DECOMPOSITION
//
Expand All @@ -80,4 +88,7 @@ double consoleSetTextSizeFromMessage(const QByteArray message);
/// Retrieve two integers from a C_CONSOLE_SET_CURSOR_POS message
QVector<int> consoleSetCursorPosFromMessage(const QByteArray message);

/// Retrieve two colors from a C_CONSOLE_SET_TEXT_COLOR message
QVector<QColor> consoleSetTextColorFromMessage(const QByteArray message);

#endif // MESSAGE_H
9 changes: 6 additions & 3 deletions qlogo_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,12 @@ void Controller::getTextCursorPos(int &row, int &col) {

void Controller::setTextColor(const QColor &foreground,
const QColor &background) {
// printToConsole(
// escapeChar + C_SET_TEXT_COLOR + foreground.name(QColor::HexArgb) +
// C_DELIM + background.name(QColor::HexArgb) + escapeChar);

QVector<QColor> colors;
colors << foreground;
colors << background;
const QByteArray message = messageFromConsoleSetTextColor(colors);
sendMessage(message);
}

void Controller::getTextCursorPosSlot(int &row, int &col) {
Expand Down

0 comments on commit 91b0ad9

Please sign in to comment.