Skip to content

Commit

Permalink
Add ui_options for changing default color palette
Browse files Browse the repository at this point in the history
  • Loading branch information
falbru committed May 20, 2024
1 parent 3cb5843 commit 09864cc
Show file tree
Hide file tree
Showing 15 changed files with 205 additions and 28 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ qt_add_executable(kak-qt
src/rpc/color.cpp
src/rpc/attribute.hpp
src/rpc/rpc.cpp
src/colorpalette.cpp
src/keybindings.cpp
src/drawoptions.cpp
src/kakouneserver.cpp
Expand Down
74 changes: 74 additions & 0 deletions src/colorpalette.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "colorpalette.hpp"
#include <qnamespace.h>

ColorPalette::ColorPalette()
: m_gray(Qt::gray), m_red(Qt::red), m_green(Qt::green), m_yellow(Qt::yellow), m_blue(Qt::blue),
m_magenta(Qt::magenta), m_cyan(Qt::cyan), m_white(Qt::white)
{
}

void ColorPalette::setGray(QColor color)
{
m_gray = color;
}
void ColorPalette::setRed(QColor color)
{
m_red = color;
}
void ColorPalette::setGreen(QColor color)
{
m_green = color;
}
void ColorPalette::setYellow(QColor color)
{
m_yellow = color;
}
void ColorPalette::setBlue(QColor color)
{
m_blue = color;
}
void ColorPalette::setMagenta(QColor color)
{
m_magenta = color;
}
void ColorPalette::setCyan(QColor color)
{
m_cyan = color;
}
void ColorPalette::setWhite(QColor color)
{
m_white = color;
}

QColor ColorPalette::getGray() const
{
return m_gray;
}
QColor ColorPalette::getRed() const
{
return m_red;
}
QColor ColorPalette::getGreen() const
{
return m_green;
}
QColor ColorPalette::getYellow() const
{
return m_yellow;
}
QColor ColorPalette::getBlue() const
{
return m_blue;
}
QColor ColorPalette::getMagenta() const
{
return m_magenta;
}
QColor ColorPalette::getCyan() const
{
return m_cyan;
}
QColor ColorPalette::getWhite() const
{
return m_white;
}
41 changes: 41 additions & 0 deletions src/colorpalette.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef COLORPALETTE_HPP
#define COLORPALETTE_HPP

#include <QColor>
#include <QString>

class ColorPalette
{
public:
ColorPalette();

void setGray(QColor color);
void setRed(QColor color);
void setGreen(QColor color);
void setYellow(QColor color);
void setBlue(QColor color);
void setMagenta(QColor color);
void setCyan(QColor color);
void setWhite(QColor color);

QColor getGray() const;
QColor getRed() const;
QColor getGreen() const;
QColor getYellow() const;
QColor getBlue() const;
QColor getMagenta() const;
QColor getCyan() const;
QColor getWhite() const;

private:
QColor m_gray;
QColor m_red;
QColor m_green;
QColor m_yellow;
QColor m_blue;
QColor m_magenta;
QColor m_cyan;
QColor m_white;
};

#endif
2 changes: 2 additions & 0 deletions src/drawcontext.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#ifndef DRAWCONTEXT_HPP
#define DRAWCONTEXT_HPP

#include "colorpalette.hpp"
#include <QPainter>
#include <QSize>

struct DrawContext
{
QPainter &painter;
const ColorPalette color_palette;
const QSize &cell_size;
};

Expand Down
10 changes: 10 additions & 0 deletions src/drawoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ QSize DrawOptions::getCellSize()
return m_cell_size;
}

ColorPalette DrawOptions::getColorPalette()
{
return m_color_palette;
}

void DrawOptions::setFont(const QString &font_name, int font_size)
{
m_font = QFont(font_name, font_size);
Expand All @@ -29,3 +34,8 @@ void DrawOptions::setFont(const QString &font_name, int font_size)

emit updated();
}

void DrawOptions::setColorPalette(ColorPalette color_palette)
{
m_color_palette = color_palette;
}
4 changes: 4 additions & 0 deletions src/drawoptions.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef DRAWOPTIONS_HPP
#define DRAWOPTIONS_HPP

#include "colorpalette.hpp"
#include <QObject>
#include <QPainter>
#include <QSize>
Expand All @@ -15,15 +16,18 @@ class DrawOptions : public QObject

QFont getFont();
QSize getCellSize();
ColorPalette getColorPalette();

void setFont(const QString &font_name, int font_size);
void setColorPalette(ColorPalette color_palette);

signals:
void updated();

private:
QFont m_font;
QSize m_cell_size;
ColorPalette m_color_palette;
};

#endif
4 changes: 1 addition & 3 deletions src/kakouneclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ KakouneClient::KakouneClient(const QString &session_id, QString arguments,
m_process.setProcessEnvironment(env);

QStringList process_arguments;
process_arguments << "-ui"
<< "json"
<< "-c" << session_id;
process_arguments << "-ui" << "json" << "-c" << session_id;
if (!arguments.isEmpty())
{
process_arguments << "-e" << arguments;
Expand Down
4 changes: 2 additions & 2 deletions src/kakouneinfobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ void KakouneInfoBox::paintEvent(QPaintEvent *ev)
QPainter painter(this);
painter.setFont(m_draw_options->getFont());

DrawContext context{painter, m_draw_options->getCellSize()};
DrawContext context{painter, m_draw_options->getColorPalette(), m_draw_options->getCellSize()};

painter.fillRect(0, 0, width(), height(), m_client->getInfoFace().getBg().toQColor());
painter.fillRect(0, 0, width(), height(), m_client->getInfoFace().getBg().toQColor(context.color_palette));

RPC::Line title = m_client->getInfoTitle();
if (title.contentSize() > 0)
Expand Down
8 changes: 4 additions & 4 deletions src/kakounemenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int KakouneMenu::getItemWidth()
max_item_contentsize = qMax(max_item_contentsize, items[i].contentSize());
}

return max_item_contentsize * m_draw_options->getCellSize().width();
return qMin(max_item_contentsize * m_draw_options->getCellSize().width(), parentWidget()->width());
}

void KakouneMenu::applyInlineStyle()
Expand Down Expand Up @@ -103,9 +103,9 @@ void KakouneMenu::paintEvent(QPaintEvent *ev)
QPainter painter(this);
painter.setFont(m_draw_options->getFont());

DrawContext context{painter, m_draw_options->getCellSize()};
DrawContext context{painter, m_draw_options->getColorPalette(), m_draw_options->getCellSize()};

painter.fillRect(0, 0, width(), height(), m_client->getMenuFace().getBg().toQColor());
painter.fillRect(0, 0, width(), height(), m_client->getMenuFace().getBg().toQColor(context.color_palette));

QList<RPC::Line> items = m_client->getMenuItems();

Expand All @@ -124,7 +124,7 @@ void KakouneMenu::paintEvent(QPaintEvent *ev)
if (m_selected_item == index)
{
painter.fillRect(position.x(), position.y(), item_width, item_height,
m_client->getSelectedMenuItemFace().getBg().toQColor());
m_client->getSelectedMenuItemFace().getBg().toQColor(context.color_palette));
items[index].draw(context, position, m_client->getSelectedMenuItemFace());
}
else
Expand Down
7 changes: 4 additions & 3 deletions src/kakounetextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ void KakouneTextEdit::paintEvent(QPaintEvent *)
qDebug("Rerender kakounewidget");

QPainter painter(this);
painter.setFont(m_draw_options->getFont());
painter.fillRect(rect(), m_client->getDefaultFace().getBg().toQColor(m_client->getDefaultFace().getBg()));
DrawContext context{painter, m_draw_options->getColorPalette(), m_draw_options->getCellSize()};

DrawContext context{painter, m_draw_options->getCellSize()};
painter.setFont(m_draw_options->getFont());
painter.fillRect(
rect(), m_client->getDefaultFace().getBg().toQColor(m_client->getDefaultFace().getBg(), context.color_palette));

QList<RPC::Line> lines = m_client->getLines();
for (int i = 0; i < lines.size(); ++i)
Expand Down
44 changes: 44 additions & 0 deletions src/kakounewidget.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "kakounewidget.hpp"
#include "colorpalette.hpp"
#include "rpc/color.hpp"
#include "statusbar.hpp"

KakouneWidget::KakouneWidget(const QString &session_id, DrawOptions *draw_options, QWidget *parent)
Expand Down Expand Up @@ -89,6 +91,48 @@ void KakouneWidget::setUIOptions(QMap<QString, QString> options)

m_draw_options->setFont(font_name, font_size);
}
else if (option.key().length() > 13 && option.key().mid(0, 13) == "gui_set_color")
{
auto setColor = [&](auto setColorFunc) {
ColorPalette new_color_palette = m_draw_options->getColorPalette();
(new_color_palette.*
setColorFunc)(RPC::Color(option.value()).toQColor(m_draw_options->getColorPalette()));
m_draw_options->setColorPalette(new_color_palette);
};

if (option.key() == "gui_set_color_gray")
{
setColor(&ColorPalette::setGray);
}
else if (option.key() == "gui_set_color_red")
{
setColor(&ColorPalette::setRed);
}
else if (option.key() == "gui_set_color_green")
{
setColor(&ColorPalette::setGreen);
}
else if (option.key() == "gui_set_color_yellow")
{
setColor(&ColorPalette::setYellow);
}
else if (option.key() == "gui_set_color_blue")
{
setColor(&ColorPalette::setBlue);
}
else if (option.key() == "gui_set_color_magenta")
{
setColor(&ColorPalette::setMagenta);
}
else if (option.key() == "gui_set_color_cyan")
{
setColor(&ColorPalette::setCyan);
}
else if (option.key() == "gui_set_color_white")
{
setColor(&ColorPalette::setWhite);
}
}
else
{
qDebug() << "Unknown ui option: " << option.key();
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/atom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Face Atom::getFace()

void Atom::draw(const DrawContext &context, const QPoint &position, const Face &default_face)
{
QColor fg = m_face.getFg().toQColor(default_face.getFg());
QColor bg = m_face.getBg().toQColor(default_face.getBg());
QColor fg = m_face.getFg().toQColor(default_face.getFg(), context.color_palette);
QColor bg = m_face.getBg().toQColor(default_face.getBg(), context.color_palette);

int width = context.cell_size.width() * m_contents.size();
int height = context.cell_size.height();
Expand Down
20 changes: 10 additions & 10 deletions src/rpc/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ QString Color::getValue() const
return m_value;
}

QColor Color::toQColor()
QColor Color::toQColor(const ColorPalette &color_palette)
{
return toQColor(Color("black"));
return toQColor(Color("black"), color_palette);
}

QColor Color::toQColor(const Color &default_color)
QColor Color::toQColor(const Color &default_color, const ColorPalette &color_palette)
{
QString color = m_value;

Expand All @@ -26,31 +26,31 @@ QColor Color::toQColor(const Color &default_color)
}
else if (color == "black" || color == "bright-black")
{
return Qt::gray;
return color_palette.getGray();
}
else if (color == "red" || color == "bright-red")
{
return Qt::red;
return color_palette.getRed();
}
else if (color == "green" || color == "bright-green")
{
return Qt::green;
return color_palette.getGreen();
}
else if (color == "yellow" || color == "bright-yellow")
{
return Qt::yellow;
return color_palette.getYellow();
}
else if (color == "blue" || color == "bright-blue")
{
return Qt::blue;
return color_palette.getBlue();
}
else if (color == "magenta" || color == "bright-magenta")
{
return Qt::magenta;
return color_palette.getMagenta();
}
else if (color == "cyan" || color == "bright-cyan")
{
return Qt::cyan;
return color_palette.getCyan();
}
else if (color == "white" || color == "bright-white")
{
Expand Down
5 changes: 3 additions & 2 deletions src/rpc/color.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RPCCOLOR_HPP
#define RPCCOLOR_HPP

#include "../colorpalette.hpp"
#include <QColor>
#include <QString>

Expand All @@ -12,8 +13,8 @@ class Color
Color(QString value);

QString getValue() const;
QColor toQColor();
QColor toQColor(const Color &default_color);
QColor toQColor(const ColorPalette &color_palette);
QColor toQColor(const Color &default_color, const ColorPalette &color_palette);

private:
QString m_value;
Expand Down
Loading

0 comments on commit 09864cc

Please sign in to comment.