Skip to content

Commit

Permalink
Improved: Add optional folder location to invokeFileDialog (Mudlet#7472)
Browse files Browse the repository at this point in the history
<!-- Keep the title short & concise so anyone non-technical can
understand it,
     the title appears in PTB changelogs -->
#### Brief overview of PR changes/additions
Allow `invokeFileDialog()` to open at an optional specified directory.
If not a valid dir or dir not specified, simply continue as per default
operation.

#### Motivation for adding to Mudlet
Allow scripters to open file dialogs at a specified place (e.g. their
scripts data location)

#### Other info (issues closed, discussion etc)

---------

Co-authored-by: Stephen Lyons <[email protected]>
  • Loading branch information
ZookaOnGit and SlySven authored Oct 14, 2024
1 parent 695e391 commit dbd3fcf
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/TLuaInterpreterMudletObjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,15 +848,27 @@ int TLuaInterpreter::getScript(lua_State* L)
// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#invokeFileDialog
int TLuaInterpreter::invokeFileDialog(lua_State* L)
{
const int n = lua_gettop(L);
Host& host = getHostFromLua(L);
QString location = mudlet::getMudletPath(mudlet::profileHomePath, host.getName());
const bool luaDir = getVerifiedBool(L, __func__, 1, "fileOrFolder");
const QString title = getVerifiedString(L, __func__, 2, "dialogTitle");

if (n > 2) {
QString target = getVerifiedString(L, __func__, 3, "dialogLocation");
QDir dir(target);

if (dir.exists()) {
location = target;
}
}

if (!luaDir) {
const QString fileName = QFileDialog::getExistingDirectory(nullptr, title, QDir::currentPath());
const QString fileName = QFileDialog::getExistingDirectory(nullptr, title, location);
lua_pushstring(L, fileName.toUtf8().constData());
return 1;
} else {
const QString fileName = QFileDialog::getOpenFileName(nullptr, title, QDir::currentPath());
const QString fileName = QFileDialog::getOpenFileName(nullptr, title, location);
lua_pushstring(L, fileName.toUtf8().constData());
return 1;
}
Expand Down

0 comments on commit dbd3fcf

Please sign in to comment.