Skip to content

Commit

Permalink
resetBgImage BgImageModes and code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Edru2 committed Sep 11, 2020
1 parent 3055c4f commit 58ea923
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 26 deletions.
62 changes: 54 additions & 8 deletions src/TConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ TConsole::TConsole(Host* pH, ConsoleType type, QWidget* parent)
, emergencyStop(new QToolButton)
, layerCommandLine(nullptr)
, mBgColor(QColor(Qt::black))
, mBgImageMode(0)
, mBgImagePath()
, mClipboard(mpHost)
, mCommandBgColor(Qt::black)
, mCommandFgColor(QColor(213, 195, 0))
Expand Down Expand Up @@ -293,8 +295,8 @@ TConsole::TConsole(Host* pH, ConsoleType type, QWidget* parent)
splitter->setSizePolicy(sizePolicy);
splitter->setOrientation(Qt::Vertical);
splitter->setHandleWidth(3);
auto style_sheet = QStringLiteral("background-color: rgba(0,0,0,0)");
splitter->setStyleSheet(style_sheet);
auto styleSheet = QStringLiteral("background-color: rgba(0,0,0,0)");
splitter->setStyleSheet(styleSheet);
splitter->setParent(layer);

mUpperPane = new TTextEdit(this, splitter, &buffer, mpHost, false);
Expand Down Expand Up @@ -1139,6 +1141,11 @@ void TConsole::slot_toggleReplayRecording()
}
}

QString getColorCode(QColor color)
{
return QStringLiteral("%1,%2,%3,%4").arg(color.red()).arg(color.green()).arg(color.blue()).arg(color.alpha());
}

void TConsole::changeColors()
{
mDisplayFont.setFixedPitch(true);
Expand Down Expand Up @@ -1166,8 +1173,12 @@ void TConsole::changeColors()
layer->setPalette(palette);
mUpperPane->setPalette(palette);
mLowerPane->setPalette(palette);
auto style_sheet = QStringLiteral("QLabel{background-color: #%1;}").arg(palette.color(QPalette::Base).rgba(), 0, 16);
mpBackground->setStyleSheet(style_sheet);
if (!mBgImageMode) {
auto styleSheet = QStringLiteral("QLabel {background-color: rgba(%1);}").arg(getColorCode(palette.color(QPalette::Base)));
mpBackground->setStyleSheet(styleSheet);
} else {
setConsoleBackgroundImage(mBgImagePath, mBgImageMode);
}
} else if (mType == MainConsole) {
if (mpCommandLine) {
QPalette pal;
Expand Down Expand Up @@ -1195,8 +1206,12 @@ void TConsole::changeColors()
layer->setPalette(palette);
mUpperPane->setPalette(palette);
mLowerPane->setPalette(palette);
auto style_sheet = QStringLiteral("QLabel{background-color: #%1;}").arg(palette.color(QPalette::Base).rgba(), 0, 16);
mpBackground->setStyleSheet(style_sheet);
if (!mBgImageMode) {
auto styleSheet = QStringLiteral("QLabel {background-color: rgba(%1);}").arg(getColorCode(palette.color(QPalette::Base)));
mpBackground->setStyleSheet(styleSheet);
} else {
setConsoleBackgroundImage(mBgImagePath, mBgImageMode);
}
mCommandFgColor = mpHost->mCommandFgColor;
mCommandBgColor = mpHost->mCommandBgColor;
if (mpCommandLine) {
Expand Down Expand Up @@ -1922,9 +1937,40 @@ bool TConsole::setMiniConsoleFontSize(int size)
return true;
}

bool TConsole::setConsoleBackgroundImage(const QString& imgPath)
bool TConsole::setConsoleBackgroundImage(const QString& imgPath, int mode)
{
QColor bgColor;
QString styleSheet;

if (mType == MainConsole) {
bgColor = mpHost->mBgColor;
} else {
bgColor = mBgColor;
}

if (mode == 1) {
styleSheet = QStringLiteral("QLabel {background-color: rgba(%1); border-image: url(%2);}").arg(getColorCode(bgColor)).arg(imgPath);
} else if (mode == 2) {
styleSheet = QStringLiteral("QLabel {background-color: rgba(%1); background-image: url(%2); background-repeat: no-repeat; background-position: center; background-origin: margin;}")
.arg(getColorCode(bgColor))
.arg(imgPath);
} else if (mode == 3) {
styleSheet = QStringLiteral("QLabel {background-color: rgba(%1); background-image: url(%2);}").arg(getColorCode(bgColor)).arg(imgPath);
} else if (mode == 4) {
styleSheet = QStringLiteral("QLabel {background-color: rgba(%1); %2}").arg(getColorCode(bgColor)).arg(imgPath);
} else {
return false;
}
mpBackground->setStyleSheet(styleSheet);
mBgImageMode = mode;
mBgImagePath = imgPath;
return true;
}

bool TConsole::resetConsoleBackgroundImage()
{
mpBackground->setStyleSheet(QStringLiteral("QLabel {border-image: url(%1);}").arg(imgPath));
mBgImageMode = 0;
changeColors();
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion src/TConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ class TConsole : public QWidget
void selectCurrentLine(std::string&);
bool setMiniConsoleFontSize(int);
bool setMiniConsoleFont(const QString& font);
bool setConsoleBackgroundImage(const QString&);
bool setConsoleBackgroundImage(const QString&, int);
bool resetConsoleBackgroundImage();
void setLink(const QStringList& linkFunction, const QStringList& linkHint);
// Cannot be called setAttributes as that would mask an inherited method
void setDisplayAttributes(const TChar::AttributeFlags, const bool);
Expand Down Expand Up @@ -316,6 +317,8 @@ class TConsole : public QWidget
QList<int> mSearchResults;
QString mSearchQuery;
QWidget* mpButtonMainLayer;
int mBgImageMode;
QString mBgImagePath;

signals:
// Raised when new data is incoming to trigger Alert handling in mudlet
Expand Down
66 changes: 55 additions & 11 deletions src/TLuaInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1339,28 +1339,71 @@ int TLuaInterpreter::setMiniConsoleFontSize(lua_State* L)
int TLuaInterpreter::setConsoleBackgroundImage(lua_State* L)
{
QString windowName = "main";
QString imgPath;
int mode = 1;
int counter = 1;
int n = lua_gettop(L);
if (n > 1) {
if (n > 1 && lua_type(L, 2) == LUA_TSTRING) {
if (!lua_isstring(L, 1)) {
lua_pushfstring(L, "setConsoleBackgroundImage: bad argument #1 type (console name as string expected, got %s!)", luaL_typename(L, 1));
return lua_error(L);
} else {
windowName = QString::fromUtf8(lua_tostring(L, 1));
counter++;
}
}
QString imgPath;
if (!lua_isstring(L, n)) {
lua_pushfstring(L, "setConsoleBackgroundImage: bad argument #%d type (image path as string expected, got %s!)", n, luaL_typename(L, n));

if (!lua_isstring(L, counter)) {
lua_pushfstring(L, "setConsoleBackgroundImage: bad argument #%d type (image path as string expected, got %s!)", counter, luaL_typename(L, counter));
return lua_error(L);
} else {
imgPath = QString::fromUtf8(lua_tostring(L, n));
imgPath = QString::fromUtf8(lua_tostring(L, counter));
counter++;
}
if (n > 2) {
lua_pushfstring(L, "setConsoleBackgroundImage: too many arguments. only 2 arguments expected, console name and image path ");
return lua_error(L);

if (n > 2 || (counter == 2 && n > 1)) {
if (!lua_isnumber(L, counter)) {
lua_pushfstring(L, "setConsoleBackgroundImage: bad argument #%d type (mode as number expected, got %s!)", counter, luaL_typename(L, counter));
return lua_error(L);
} else {
mode = lua_tonumber(L, counter);
}
}

if (mode < 1 || mode > 4) {
lua_pushnil(L);
lua_pushfstring(L, "setConsoleBackgroundImage: %d is not a valid mode!)", mode);
return 2;
}

Host* host = &getHostFromLua(L);
if (mudlet::self()->setWindowBackgroundImage(host, windowName, imgPath, mode)) {
lua_pushboolean(L, true);
return 1;
} else {
lua_pushnil(L);
lua_pushfstring(L, R"(console "%s" not found)", windowName.toUtf8().constData());
return 2;
}
return 0;
}

// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#resetConsoleBackgroundImage
int TLuaInterpreter::resetConsoleBackgroundImage(lua_State* L)
{
QString windowName = "main";
int n = lua_gettop(L);
if (n > 1) {
if (!lua_isstring(L, 1)) {
lua_pushfstring(L, "resetConsoleBackgroundImage: bad argument #1 type (console name as string expected, got %s!)", luaL_typename(L, 1));
return lua_error(L);
} else {
windowName = QString::fromUtf8(lua_tostring(L, 1));
}
}

Host* host = &getHostFromLua(L);
if (mudlet::self()->setWindowBackgroundImage(host, windowName, imgPath)) {
if (mudlet::self()->resetWindowBackgroundImage(host, windowName)) {
lua_pushboolean(L, true);
return 1;
} else {
Expand Down Expand Up @@ -4670,7 +4713,7 @@ int TLuaInterpreter::setBackgroundColor(lua_State* L)
int r, g, b, alpha;

auto validRange = [](int number) {
return number >= 0 and number <= 255;
return number >= 0 && number <= 255;
};

int s = 1;
Expand Down Expand Up @@ -12031,7 +12074,7 @@ int TLuaInterpreter::setBgColor(lua_State* L)
QString windowName;
int r, g, b, alpha;

auto validRange = [](int number) { return number >= 0 and number <= 255; };
auto validRange = [](int number) { return number >= 0 && number <= 255; };

int s = 1;
if (lua_isstring(L, s) && !lua_isnumber(L, s)) {
Expand Down Expand Up @@ -16878,6 +16921,7 @@ void TLuaInterpreter::initLuaGlobals()
lua_register(pGlobalLua, "getCurrentLine", TLuaInterpreter::getCurrentLine);
lua_register(pGlobalLua, "setMiniConsoleFontSize", TLuaInterpreter::setMiniConsoleFontSize);
lua_register(pGlobalLua, "setConsoleBackgroundImage", TLuaInterpreter::setConsoleBackgroundImage);
lua_register(pGlobalLua, "resetConsoleBackgroundImage", TLuaInterpreter::resetConsoleBackgroundImage);
lua_register(pGlobalLua, "selectCurrentLine", TLuaInterpreter::selectCurrentLine);
lua_register(pGlobalLua, "spawn", TLuaInterpreter::spawn);
lua_register(pGlobalLua, "getButtonState", TLuaInterpreter::getButtonState);
Expand Down
1 change: 1 addition & 0 deletions src/TLuaInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ class TLuaInterpreter : public QThread
static int getMousePosition(lua_State*);
static int setMiniConsoleFontSize(lua_State*);
static int setConsoleBackgroundImage(lua_State*);
static int resetConsoleBackgroundImage(lua_State*);
static int setProfileIcon(lua_State*);
static int resetProfileIcon(lua_State*);
static int getCurrentLine(lua_State*);
Expand Down
2 changes: 1 addition & 1 deletion src/TTextEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ TTextEdit::TTextEdit(TConsole* pC, QWidget* pW, TBuffer* pB, Host* pH, bool isLo
QCursor cursor;
cursor.setShape(Qt::IBeamCursor);
setCursor(cursor);
setAttribute(Qt::WA_OpaquePaintEvent, false); //was disabled
setAttribute(Qt::WA_OpaquePaintEvent, false);
setAttribute(Qt::WA_DeleteOnClose);

QPalette palette;
Expand Down
2 changes: 1 addition & 1 deletion src/dlgProfilePreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,6 @@ void dlgProfilePreferences::resetColors()
if (!pHost) {
return;
}

pHost->mCommandLineFgColor = Qt::darkGray;
pHost->mCommandLineBgColor = Qt::black;
pHost->mCommandFgColor = QColor(113, 113, 0);
Expand All @@ -1477,6 +1476,7 @@ void dlgProfilePreferences::resetColors()

setColors();
if (mudlet::self()->mConsoleMap.contains(pHost)) {
pHost->mpConsole->resetConsoleBackgroundImage();
mudlet::self()->mConsoleMap[pHost]->changeColors();
}

Expand Down
17 changes: 17 additions & 0 deletions src/mudlet-lua/lua/GUIUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2160,6 +2160,23 @@ function setLabelCursor(labelname, cursorShape)
return setLabelCursorLayer(labelname, cursorShape)
end

mudlet.BgImageMode ={
["border"] = 1,
["center"] = 2,
["tile"] = 3,
["style"] = 4,
}

local setConsoleBackgroundImageLayer = setConsoleBackgroundImage
function setConsoleBackgroundImage(...)
local mode = arg[arg.n]
if type(mode) == "string" then
mode = mudlet.BgImageMode[mode] or mode
end
arg[arg.n] = mode
return setConsoleBackgroundImageLayer(unpack(arg))
end


--These functions ensure backward compatibility for the setLabelCallback functions
--unpack function which also returns the nil values
Expand Down
26 changes: 23 additions & 3 deletions src/mudlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1974,20 +1974,40 @@ void mudlet::commitLayoutUpdates()
}
}

bool mudlet::setWindowBackgroundImage(Host *pHost, const QString &name, const QString &imgPath)
bool mudlet::setWindowBackgroundImage(Host* pHost, const QString& name, const QString& imgPath, int mode)
{
if (!pHost || !pHost->mpConsole) {
return false;
}

if (name.isEmpty() || name.compare(QStringLiteral("main"), Qt::CaseSensitive) == 0) {
pHost->mpConsole->setConsoleBackgroundImage(imgPath);
pHost->mpConsole->setConsoleBackgroundImage(imgPath, mode);
return true;
}

auto pC = pHost->mpConsole->mSubConsoleMap.value(name);
if (pC) {
pC->setConsoleBackgroundImage(imgPath);
pC->setConsoleBackgroundImage(imgPath, mode);
return true;
} else {
return false;
}
}

bool mudlet::resetWindowBackgroundImage(Host *pHost, const QString &name)
{
if (!pHost || !pHost->mpConsole) {
return false;
}

if (name.isEmpty() || name.compare(QStringLiteral("main"), Qt::CaseSensitive) == 0) {
pHost->mpConsole->resetConsoleBackgroundImage();
return true;
}

auto pC = pHost->mpConsole->mSubConsoleMap.value(name);
if (pC) {
pC->resetConsoleBackgroundImage();
return true;
} else {
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/mudlet.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ class mudlet : public QMainWindow, public Ui::main_window
bool setWindowFont(Host*, const QString&, const QString&);
QString getWindowFont(Host*, const QString&);
bool setWindowFontSize(Host *, const QString &, int);
bool setWindowBackgroundImage(Host *, const QString &, const QString &);
bool setWindowBackgroundImage(Host *, const QString&, const QString&, int);
bool resetWindowBackgroundImage(Host *, const QString&);
int getFontSize(Host*, const QString&);
QSize calcFontSize(Host* pHost, const QString& windowName);
std::pair<bool, QString> openWindow(Host*, const QString&, bool loadLayout, bool autoDock, const QString &area);
Expand Down

0 comments on commit 58ea923

Please sign in to comment.