QuickSnapshot plugin added to xournal++.

Add a 'saveAs' dialog function for the Lua api

Fixed the QuickScreenshot plugin to work with scrot (gnome-screenshot polluted the xournalpp window)

Fixed the comments in QuickScreenshot/main.lua to reflect current changes.

Fixed formatting in QuickScreenshot files and luapi_application.h
presentation
John Doe 6 years ago
parent a320e1c544
commit 50a69c59f9
  1. 112
      plugins/QuickScreenshot/main.lua
  2. 15
      plugins/QuickScreenshot/plugin.ini
  3. 39
      src/plugin/luapi_application.h

@ -0,0 +1,112 @@
-- The QuickScreenshot plugin:
--
-- Allows the user to save a region to disk as png image.
-- To be used so that users can save various diagrams, etc
-- to their directory of choice; useful when creating a lot
-- of diagrams for presentation.
--
-- TODO:
-- o Support for Windows and macOS is missing. To add support
-- you must edit the functions 'existsUtility' and 'go' to
-- use the right system programs.
--
-- Returns 'true' if the system utility exists;
-- returns 'false' otherwise. This is a dangerous function,
-- since the argument to popen is not sanitized.
-- The programmer must ensure that the argument to
-- existsUtility() is sanitized (not malicious).
function existsUtility(utility)
if operatingSystem == "Windows" then
-- Windows not supported yet.
elseif operatingSystem == "Darwin" then
-- macOS not supported yet.
else
local f = assert(io.popen("command -v " .. utility))
local s = assert(f:read('*a'))
f:close()
return s ~= ""
end
end
-- Return one of "Windows", "Linux", "Darwin", "FreeBSD",
-- "OpenBSD" or "NetBSD", etc.
function findOS()
if package.config:sub(1,1) == '\\' then
return "Windows"
else
local f = assert(io.popen("uname -s", 'r'))
local s = assert(f:read('*a'))
f:close()
return s
end
end
-- Register all Toolbar actions and intialize all UI stuff
function initUi()
ref = app.registerUi({["menu"] = "QuickScreenshot", ["callback"] = "go", ["accelerator"] = "<Shift><Alt>t"});
operatingSystem = findOS() -- What's the operating system?
end
-- This is the callback that gets executed when the user
-- activates the plugin via the menu or hotkey.
--
-- This function calls 'app.saveAs' to create
-- a "Save As" dialog for the user to choose the
-- filename where the image should reside. Then
-- it executes a screenshot utility to capture the
-- region the user selects.
function go()
local windowsUtilities = {} -- list of windows programs here
local macUtilities = {} -- list of macOS programs here
local elseUtilities = -- list of programs for other systems
{ "scrot --overwrite --select --silent "
-- gnome-screenshot behaves oddly (pollutes the window)
-- the line is added here to be indicative of what
-- adding another utility looks like.
--
-- , "gnome-screenshot --area --file="
}
-- Different operating systems have different
-- screenshot utilities, so we check the OS here
if operatingSystem == "Windows" then
app.msgbox("Windows not supported yet.", {[1] = "OK"})
elseif operatingSystem == "Darwin" then
app.msgbox("macOS not supported yet.", {[1] = "OK"})
else
-- This becomes true if at least one screenshot
-- utility is available on the system
local foundUtility = false
-- Check utility availability and use it
for i,command in ipairs(elseUtilities) do
utilityName = command:match("%S+")
if existsUtility(utilityName) then
local tmpFilename = os.tmpname()
local runCommand = assert(io.popen(command .. tmpFilename .. " &"))
runCommand:read('*all')
runCommand:close()
-- Launch the "Save As" dialog
local filename = app.saveAs()
if not filename then
os.remove(tmpFilename)
return
end
os.rename(tmpFilename, filename)
foundUtility = true
break
end
end
-- No utility available on the system, inform the user
if not foundUtility then
print("Missing screenshot utility.")
print("Please install one of:")
for i,command in ipairs(elseUtilities) do
print(command:match("%S+"))
end
return
end
end
end

@ -0,0 +1,15 @@
[about]
## Author / Copyright notice
author=Debbie Reynolds
description=Capture a region of the screen and save to PNG, or other.
## If the plugin is packed with Xournal++, use
## <xournalpp> then it gets the same version number
version=<xournalpp>
[default]
enabled=false
[plugin]
mainfile=main.lua

@ -9,6 +9,7 @@
* @license GNU GPLv2 or later
*/
#include <cstring>
#include <map>
#include "control/Control.h"
@ -19,6 +20,43 @@
#include "XojMsgBox.h"
using std::map;
/**
* Create a 'Save As' native dialog and return as a string
* the filepath of the location the user chose to save.
*/
static int applib_saveAs(lua_State* L) {
GtkFileChooserNative* native;
gint res;
int args_returned = 0; // change to 1 if user chooses file
// Create a 'Save As' native dialog
native = gtk_file_chooser_native_new(_("Save file"), nullptr, GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr);
// If user tries to overwrite a file, ask if it's OK
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(native), TRUE);
// Offer a suggestion for the filename
gchar* default_filename = g_strconcat(_("Untitled"), ".png", nullptr);
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(native), default_filename);
// Wait until user responds to dialog
res = gtk_native_dialog_run(GTK_NATIVE_DIALOG(native));
// Return the filename chosen to lua
if (res == GTK_RESPONSE_ACCEPT) {
char* filename = static_cast<char*>(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(native)));
lua_pushlstring(L, filename, strlen(filename));
g_free(static_cast<gchar*>(filename));
args_returned = 1;
}
// Destroy the dialog and free memory
g_object_unref(native);
g_free(default_filename);
return args_returned;
}
/**
* Example:
* app.msgbox("Test123", {[1] = "Yes", [2] = "No"})
@ -201,6 +239,7 @@ static const luaL_Reg applib[] = {{"msgbox", applib_msgbox},
{"uiAction", applib_uiAction},
{"uiActionSelected", applib_uiActionSelected},
{"changeCurrentPageBackground", applib_changeCurrentPageBackground},
{"saveAs", applib_saveAs},
// Placeholder
// {"MSG_BT_OK", nullptr},

Loading…
Cancel
Save