Skip to content

Commit

Permalink
Adding Window Layout features and control functions to Mudlet (Mudlet…
Browse files Browse the repository at this point in the history
…#996)

* Adds Window Layout features and control functions.

This commit adds two new lua functions `loadWindowLayout()` and
`saveWindowLayout()` as well as modifies `openUserWindow( name )` with
an optional second boolean argument to allow scripters manual control
over the layout features if they need it.
When a window is opened the layout information will automatically be
reloaded and applied if available, unless called like so:
`openUserWindow( 'my window', false )`

This code also changes/completes code in `Host.cpp` which is used to
mark a Host as closing or check its closing state.
The code is used in the `TConsole::closeEvent` and in
`TDockWidget::closeEvent` to aid in closing and removing the consoles
from Mudlet when a Host profile tab is closed.

* BugFix: correctly destroy dockable map widgets on profile close

It has been a long-standing fatal issue:
Mudlet#550
that dock-able mapper widgets were left orphaned with dangerously dangling
pointers that will crash Mudlet when a profile using them was closed whilst
multi-playing.  This commit makes the Host class now maintain a QPointer
that tracks the QDockWidget that this type of map has, and, as part of the
Host destructor ensures that the widget is given suicide instructions when
the Host itself is killed.

A QPointer was done so that should the map widget somehow get deleted by
other future code changes the pointer will automagically null itself so
that a test for it being non-null in the Host destructor is sufficient to
ensure that there is a widget that needs to be killed off at that time.

Signed-off-by: Stephen Lyons <slysven@virginmedia.com>
  • Loading branch information
itsTheFae authored and vadi2 committed May 16, 2017
1 parent e8b9377 commit 610cf95
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 51 deletions.
16 changes: 13 additions & 3 deletions src/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Host::Host(int port, const QString& hostname, const QString& login, const QStrin
, mCommandLineFgColor(Qt::darkGray)
, mCommandLineBgColor(Qt::black)
, mFORCE_MXP_NEGOTIATION_OFF(false)
, mpDockableMapWidget()
, mHaveMapperScript(false)
{
// mLogStatus = mudlet::self()->mAutolog;
Expand Down Expand Up @@ -190,7 +191,11 @@ Host::Host(int port, const QString& hostname, const QString& login, const QStrin

Host::~Host()
{
if (mpDockableMapWidget) {
mpDockableMapWidget->deleteLater();
}
mIsGoingDown = true;
mIsClosingDown = true;
mTelnet.disconnect();
mErrorLogStream.flush();
mErrorLogFile.close();
Expand Down Expand Up @@ -663,11 +668,16 @@ void Host::connectToServer()
mTelnet.connectIt(mUrl, mPort);
}

bool Host::closingDown()
void Host::closingDown()
{
QMutexLocker locker(&mLock);
mIsClosingDown = true;
}

bool Host::isClosingDown()
{
QMutexLocker locker(&mLock);
bool shutdown = mIsClosingDown;
return shutdown;
return mIsClosingDown;
}

bool Host::installPackage(const QString& fileName, int module)
Expand Down
5 changes: 4 additions & 1 deletion src/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "post_guard.h"

class QDialog;
class QDockWidget;
class QPushButton;
class QListWidget;

Expand Down Expand Up @@ -82,7 +83,8 @@ class Host : public QObject
int getTimeout() { QMutexLocker locker(& mLock); return mTimeout; }
void setTimeout( int seconds ) { QMutexLocker locker(& mLock); mTimeout=seconds; }

bool closingDown();
void closingDown();
bool isClosingDown();
const unsigned int assemblePath();
const bool checkForMappingScript();

Expand Down Expand Up @@ -302,6 +304,7 @@ class Host : public QObject
bool mMapperUseAntiAlias;
bool mFORCE_MXP_NEGOTIATION_OFF;
QSet<QChar> mDoubleClickIgnore;
QPointer<QDockWidget> mpDockableMapWidget;

private:
QScopedPointer<LuaInterface> mLuaInterface;
Expand Down
35 changes: 22 additions & 13 deletions src/TConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,33 +707,38 @@ void TConsole::closeEvent( QCloseEvent *event )
{
if( mIsDebugConsole )
{
if( ! mudlet::self()->isGoingDown() )
{
if( mudlet::self()->isGoingDown() || mpHost->isClosingDown() ) {
event->accept();
return;
} else {
hide();
mudlet::mpDebugArea->setVisible(false);
mudlet::debugMode = false;
event->ignore();
return;
}
else
{
event->accept();
return;
}
}

if( mUserConsole )
{
if( ! mudlet::self()->isGoingDown() )
if( mudlet::self()->isGoingDown() || mpHost->isClosingDown() )
{
std::string key = objectName().toLatin1().data();
TConsole * pC = mpHost->mpConsole;
if( pC->mSubConsoleMap.find(key) != pC->mSubConsoleMap.end() ) {
console->close();
console2->close();

pC->mSubConsoleMap.erase(key);
}

event->accept();
return;
} else {
hide();
event->ignore();
return;
}
else
{
event->accept();
return;
}
}

if( profile_name != "default_host" )
Expand Down Expand Up @@ -780,6 +785,8 @@ void TConsole::closeEvent( QCloseEvent *event )
return;
}
if (choice == QMessageBox::Yes) {
mudlet::self()->saveWindowLayout();

mpHost->modulesToWrite.clear();
std::tuple<bool, QString, QString> result = mpHost->saveProfile();

Expand All @@ -804,6 +811,8 @@ void TConsole::closeEvent( QCloseEvent *event )
return;

} else if (choice == QMessageBox::No) {
mudlet::self()->saveWindowLayout();

event->accept();
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/TConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class TConsole : public QWidget
int mCurrentSearchResult;
QList<int> mSearchResults;
QString mSearchQuery;
bool mSaveLayoutRequested;

signals:

Expand Down
25 changes: 22 additions & 3 deletions src/TDockWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,26 @@ TDockWidget::TDockWidget( Host * pH, const QString & consoleName ) : QDockWidget
widgetConsoleName = consoleName;
}

void TDockWidget::closeEvent(QCloseEvent * event) {
mudlet::self()->hideWindow(mpHost, widgetConsoleName);
event->ignore();
void TDockWidget::closeEvent(QCloseEvent * event)
{
if( ! mpHost->isClosingDown() ) {
mudlet::self()->hideWindow(mpHost, widgetConsoleName);
event->ignore();
return;
} else {
event->accept();
return;
}
}

void TDockWidget::resizeEvent(QResizeEvent *event) {
if( ! mudlet::self()->mIsLoadingLayout ) {
mudlet::self()->setDockLayoutUpdated(mpHost, widgetConsoleName);
}
}

void TDockWidget::moveEvent(QMoveEvent *event) {
if( ! mudlet::self()->mIsLoadingLayout ) {
mudlet::self()->setDockLayoutUpdated(mpHost, widgetConsoleName);
}
}
2 changes: 2 additions & 0 deletions src/TDockWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class TDockWidget : public QDockWidget {

protected:
void closeEvent(QCloseEvent *) override;
void resizeEvent(QResizeEvent *) override;
void moveEvent(QMoveEvent *) override;

private:
QPointer<Host> mpHost;
Expand Down
25 changes: 22 additions & 3 deletions src/TLuaInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2422,6 +2422,17 @@ int TLuaInterpreter::closeMudlet(lua_State* L)
return 0;
}

int TLuaInterpreter::loadWindowLayout(lua_State *L) {
lua_pushboolean( L, mudlet::self()->loadWindowLayout() );
return 1;
}

int TLuaInterpreter::saveWindowLayout(lua_State *L) {
mudlet::self()->mHasSavedLayout = false;
lua_pushboolean( L, mudlet::self()->saveWindowLayout() );
return 1;
}

int TLuaInterpreter::saveProfile(lua_State* L)
{
Host* pHost = TLuaInterpreter::luaInterpreterMap[L];
Expand Down Expand Up @@ -2462,10 +2473,16 @@ int TLuaInterpreter::openUserWindow( lua_State *L )
{
luaSendText = lua_tostring( L, 1 );
}

bool loadLayout = true;
if( ! lua_isnoneornil(L, 2) && lua_isboolean(L, 2) ) {
loadLayout = lua_toboolean(L, 2);
}

Host * pHost = TLuaInterpreter::luaInterpreterMap[L];
QString text(luaSendText.c_str());
mudlet::self()->openWindow( pHost, text );
return 0;
lua_pushboolean( L, mudlet::self()->openWindow( pHost, text, loadLayout ));
return 1;
}

int TLuaInterpreter::createMiniConsole( lua_State *L )
Expand Down Expand Up @@ -12985,7 +13002,9 @@ void TLuaInterpreter::initLuaGlobals()
lua_register( pGlobalLua, "tempTimer", TLuaInterpreter::tempTimer );
lua_register( pGlobalLua, "tempTrigger", TLuaInterpreter::tempTrigger );
lua_register( pGlobalLua, "tempRegexTrigger", TLuaInterpreter::tempRegexTrigger );
lua_register( pGlobalLua, "closeMudlet", TLuaInterpreter::closeMudlet);
lua_register( pGlobalLua, "closeMudlet", TLuaInterpreter::closeMudlet );
lua_register( pGlobalLua, "loadWindowLayout", TLuaInterpreter::loadWindowLayout );
lua_register( pGlobalLua, "saveWindowLayout", TLuaInterpreter::saveWindowLayout );
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 @@ -226,6 +226,8 @@ class TLuaInterpreter : public QThread
static int setBgColor(lua_State* L);
static int tempTimer(lua_State* L);
static int closeMudlet(lua_State* L);
static int loadWindowLayout(lua_State* L);
static int saveWindowLayout(lua_State* L);
static int saveProfile(lua_State* L);
static int openUserWindow(lua_State* L);
static int echoUserWindow(lua_State* L);
Expand Down
11 changes: 11 additions & 0 deletions src/TToolBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ TToolBar::TToolBar( TAction * pA, const QString& name, QWidget * pW )
{
setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
setWidget(mpWidget);
setObjectName(QString("dockToolBar_%1").arg(name));

if (!mpTAction->mUseCustomLayout) {
mpLayout = new QGridLayout(mpWidget);
Expand All @@ -61,8 +62,18 @@ TToolBar::TToolBar( TAction * pA, const QString& name, QWidget * pW )
mpWidget->setStyleSheet(mpTAction->css);
}

void TToolBar::resizeEvent(QResizeEvent *e) {
if( ! mudlet::self()->mIsLoadingLayout ) {
mudlet::self()->setToolbarLayoutUpdated(mpTAction->mpHost, this);
}
}

void TToolBar::moveEvent(QMoveEvent* e)
{
if( ! mudlet::self()->mIsLoadingLayout ) {
mudlet::self()->setToolbarLayoutUpdated(mpTAction->mpHost, this);
}

if (mRecordMove) {
if (!mpTAction) {
return;
Expand Down
2 changes: 2 additions & 0 deletions src/TToolBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ class TToolBar : public QDockWidget
public:
TToolBar(TAction*, const QString&, QWidget* pW = 0);
void addButton(TFlipButton* pW);
void resizeEvent(QResizeEvent* e) override;
void moveEvent(QMoveEvent* e) override;
void setVerticalOrientation() { mVerticalOrientation = true; }
void setHorizontalOrientation() { mVerticalOrientation = false; }
void clear();
void finalize();
TAction* mpTAction;
void recordMove() { mRecordMove = true; }
QString getName() { return mName; }

private:
bool mVerticalOrientation;
Expand Down
Loading

0 comments on commit 610cf95

Please sign in to comment.