Skip to content

Commit

Permalink
Allow UserWindow font size control with setFontSize(name, size) (Mu…
Browse files Browse the repository at this point in the history
  • Loading branch information
itsTheFae authored and vadi2 committed Aug 2, 2017
1 parent b151f92 commit 7808867
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 8 deletions.
96 changes: 95 additions & 1 deletion src/TLuaInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,99 @@ int TLuaInterpreter::saveProfile(lua_State* L)
}
}

// openUserWindow( session, string window_name )
int TLuaInterpreter::setFontSize(lua_State* L)
{
Host* pHost = &getHostFromLua(L);

QString windowName;
int s = 0;
if (lua_gettop(L) > 1) { // Have more than one argument so first must be a console name
if (!lua_isstring(L, ++s)) {
lua_pushfstring(L,
"setFontSize: bad argument #%d type (more than one argument supplied and first,\n"
"window name, as string expected (omission selects \"main\" window), got %s!",
s,
luaL_typename(L, s));
return lua_error(L);
} else {
windowName = QString::fromUtf8(lua_tostring(L, s));
}
}

int size;
if (!lua_isnumber(L, ++s)) {
lua_pushfstring(L, "setFontSize: bad argument #%d type (size as number expected, got %s!)", s, luaL_typename(L, s));
return lua_error(L);
} else {
size = lua_tointeger(L, s);
if (size <= 0) {
// just throw an error, no default needed.
lua_pushnil(L);
lua_pushstring(L, "size cannot be 0 or negative");
return 2;
}
}

if (windowName.isEmpty() || windowName.compare(QStringLiteral("main"), Qt::CaseSensitive) == 0) {
if (mudlet::self()->mConsoleMap.contains(pHost)) {
// get host profile display font and alter it, since that is how it's done in Settings.
QFont font = pHost->mDisplayFont;
font.setPointSize(size);
pHost->mDisplayFont = font;
// apply changes to main console and its while-scrolling component too.
mudlet::self()->mConsoleMap[pHost]->console->updateScreenView();
mudlet::self()->mConsoleMap[pHost]->console->forceUpdate();
mudlet::self()->mConsoleMap[pHost]->console2->updateScreenView();
mudlet::self()->mConsoleMap[pHost]->console2->forceUpdate();
mudlet::self()->mConsoleMap[pHost]->refresh();
lua_pushboolean(L, true);
} else {
lua_pushnil(L);
lua_pushstring(L, "could not find the main window");
return 2;
}
} else {
if (mudlet::self()->setFontSize(pHost, windowName, size)) {
lua_pushboolean(L, true);
} else {
lua_pushnil(L);
lua_pushfstring(L, R"(window "%s" not found)", windowName.toUtf8().constData());
}
}
return 1;
}

int TLuaInterpreter::getFontSize(lua_State* L)
{
Host* pHost = &getHostFromLua(L);

QString windowName = QStringLiteral("main");
int rval = -1;
if (lua_gettop(L) == 1) {
if (!lua_isstring(L, 1)) {
lua_pushfstring(L, "getFontSize: bad argument #1 type (window name as string expected, got %s!)", luaL_typename(L, 1));
return lua_error(L);
} else {
windowName = QString::fromUtf8(lua_tostring(L, 1));

if (windowName.isEmpty() || windowName.compare(QStringLiteral("main"), Qt::CaseSensitive) == 0) {
rval = pHost->mDisplayFont.pointSize();
} else {
rval = mudlet::self()->getFontSize(pHost, windowName);
}
}
} else {
rval = pHost->mDisplayFont.pointSize();
}

if (rval <= -1) {
lua_pushnil(L);
} else {
lua_pushnumber(L, rval);
}
return 1;
}

int TLuaInterpreter::openUserWindow(lua_State* L)
{
string luaSendText = "";
Expand Down Expand Up @@ -11659,6 +11751,8 @@ void TLuaInterpreter::initLuaGlobals()
lua_register(pGlobalLua, "closeMudlet", TLuaInterpreter::closeMudlet);
lua_register(pGlobalLua, "loadWindowLayout", TLuaInterpreter::loadWindowLayout);
lua_register(pGlobalLua, "saveWindowLayout", TLuaInterpreter::saveWindowLayout);
lua_register(pGlobalLua, "setFontSize", TLuaInterpreter::setFontSize);
lua_register(pGlobalLua, "getFontSize", TLuaInterpreter::getFontSize);
lua_register(pGlobalLua, "openUserWindow", TLuaInterpreter::openUserWindow);
lua_register(pGlobalLua, "echoUserWindow", TLuaInterpreter::echoUserWindow);
lua_register(pGlobalLua, "enableTimer", TLuaInterpreter::enableTimer);
Expand Down
2 changes: 2 additions & 0 deletions src/TLuaInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ class TLuaInterpreter : public QThread
static int loadWindowLayout(lua_State* L);
static int saveWindowLayout(lua_State* L);
static int saveProfile(lua_State* L);
static int setFontSize(lua_State* L);
static int getFontSize(lua_State* L);
static int openUserWindow(lua_State* L);
static int echoUserWindow(lua_State* L);
static int clearUserWindow(lua_State* L);
Expand Down
30 changes: 23 additions & 7 deletions src/dlgProfilePreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,16 @@ dlgProfilePreferences::dlgProfilePreferences(QWidget* pF, Host* pH) : QDialog(pF
if (mFontSize < 0) {
mFontSize = 10;
}
if (mFontSize <= 40) {
fontSize->setCurrentIndex(mFontSize);
if (mFontSize < 40 && mFontSize > 0) {
fontSize->setCurrentIndex( (mFontSize - 1) );
} else {
// if the font size set for the main console is outside the pre-set range
// this will unfortunately reset the font to default size.
// without this the first entry (font-size 1) is selected and on-save
// will make the console font far too tiny to read.
// Maybe our font-size range should be generated differently if the console
// has a font size larger than the preset range offers?
fontSize->setCurrentIndex(9); // default font is size 10, index 9.
}

setColors();
Expand Down Expand Up @@ -615,8 +623,9 @@ void dlgProfilePreferences::setCommandBgColor()

void dlgProfilePreferences::setFontSize()
{
mFontSize = fontSize->currentIndex();
setDisplayFont();
mFontSize = fontSize->currentIndex() + 1;
// delay setting pHost->mDisplayFont until save is clicked by the user.
//setDisplayFont();
}

void dlgProfilePreferences::setDisplayFont()
Expand All @@ -631,6 +640,13 @@ void dlgProfilePreferences::setDisplayFont()
pHost->mDisplayFont = font;
if (mudlet::self()->mConsoleMap.contains(pHost)) {
mudlet::self()->mConsoleMap[pHost]->changeColors();

// update the display properly when font or size selections change.
mudlet::self()->mConsoleMap[pHost]->console->updateScreenView();
mudlet::self()->mConsoleMap[pHost]->console->forceUpdate();
mudlet::self()->mConsoleMap[pHost]->console2->updateScreenView();
mudlet::self()->mConsoleMap[pHost]->console2->forceUpdate();
mudlet::self()->mConsoleMap[pHost]->refresh();
}
auto config = edbeePreviewWidget->config();
config->beginChanges();
Expand Down Expand Up @@ -1392,10 +1408,10 @@ void dlgProfilePreferences::slot_save_and_exit()
QFile file_use_smallscreen(QDir::homePath() + "/.config/mudlet/mudlet_option_use_smallscreen");
file_use_smallscreen.remove();
}

setDisplayFont();

if (mudlet::self()->mConsoleMap.contains(pHost)) {
mudlet::self()->mConsoleMap[pHost]->console->updateScreenView();
mudlet::self()->mConsoleMap[pHost]->console->forceUpdate();
mudlet::self()->mConsoleMap[pHost]->refresh();
int x = mudlet::self()->mConsoleMap[pHost]->width();
int y = mudlet::self()->mConsoleMap[pHost]->height();
QSize s = QSize(x, y);
Expand Down
44 changes: 44 additions & 0 deletions src/mudlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,46 @@ void mudlet::commitLayoutUpdates()
}
}

bool mudlet::setFontSize(Host* pHost, const QString& name, int size)
{
if (!pHost) {
return false;
}

QMap<QString, TConsole*>& dockWindowConsoleMap = mHostConsoleMap[pHost];
QMap<QString, TDockWidget*>& dockWindowMap = mHostDockConsoleMap[pHost];

if (dockWindowMap.contains(name) && dockWindowConsoleMap.contains(name)) {
TConsole* pC = dockWindowConsoleMap.value(name);
pC->console->mDisplayFont = QFont("Bitstream Vera Sans Mono", size, QFont::Normal);
pC->console->updateScreenView();
pC->console->forceUpdate();
pC->console2->mDisplayFont = QFont("Bitstream Vera Sans Mono", size, QFont::Normal);
pC->console2->updateScreenView();
pC->console2->forceUpdate();

return true;
} else {
return false;
}
}

int mudlet::getFontSize(Host* pHost, const QString& name)
{
if (!pHost) {
return -1;
}

QMap<QString, TConsole*>& dockWindowConsoleMap = mHostConsoleMap[pHost];
QMap<QString, TDockWidget*>& dockWindowMap = mHostDockConsoleMap[pHost];

if (dockWindowMap.contains(name) && dockWindowConsoleMap.contains(name)) {
return dockWindowConsoleMap.value(name)->console->mDisplayFont.pointSize();
} else {
return -1;
}
}

bool mudlet::openWindow(Host* pHost, const QString& name, bool loadLayout)
{
if (!pHost) {
Expand All @@ -1233,9 +1273,13 @@ bool mudlet::openWindow(Host* pHost, const QString& name, bool loadLayout)
pC->layerCommandLine->hide();
pC->mpScrollBar->hide();
pC->setUserWindow();
pC->console->setIsMiniConsole();
pC->console2->setIsMiniConsole();
dockWindowConsoleMap[name] = pC;
addDockWidget(Qt::RightDockWidgetArea, pD);

setFontSize(pHost, name, 10);

if (loadLayout) {
loadWindowLayout();
}
Expand Down
2 changes: 2 additions & 0 deletions src/mudlet.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class mudlet : public QMainWindow, public Ui::MainWindow
void setDockLayoutUpdated(Host*, const QString&);
void setToolbarLayoutUpdated(Host*, TToolBar*);
void commitLayoutUpdates();
bool setFontSize(Host*, const QString&, int);
int getFontSize(Host*, const QString&);
bool openWindow(Host*, const QString&, bool loadLayout = true);
bool createMiniConsole(Host*, const QString&, int, int, int, int);
bool createLabel(Host*, const QString&, int, int, int, int, bool);
Expand Down

0 comments on commit 7808867

Please sign in to comment.