Skip to content

Commit

Permalink
added new scripting command inputDialogGetItem
Browse files Browse the repository at this point in the history
  • Loading branch information
pbek committed Jul 29, 2017
1 parent 7d4c1a1 commit 1bd01f8
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Expand Up @@ -3,11 +3,15 @@
## 17.07.9
- fixed a possible crash when using scripts with custom actions in the note
list context menu when the script engine is reloaded and the context menu
is opened
is opened (for [Issue #490](https://github.com/pbek/QOwnNotes/issues/490))
- there now is a new scripting command `script.inputDialogGetItem` to open a
input dialog with a select box (for [Issue #490](https://github.com/pbek/QOwnNotes/issues/490))
- for more information please take a look at
[Opening an input dialog with a select box](http://docs.qownnotes.org/en/develop/scripting/README.html#opening-an-input-dialog-with-a-select-box)

## 17.07.8
- there now is a new scripting command to return a list of the paths of all
selected notes `script.selectedNotesPaths()`
- there now is a new scripting command `script.selectedNotesPaths()` to return a
list of the paths of all selected notes
(for [Issue #490](https://github.com/pbek/QOwnNotes/issues/490))
- for more information please take a look at
[Getting a list of the paths of all selected notes](http://docs.qownnotes.org/en/develop/scripting/README.html#getting-a-list-of-the-paths-of-all-selected-notes)
Expand Down
36 changes: 36 additions & 0 deletions doc/scripting/README.rst
Expand Up @@ -894,6 +894,42 @@ You may want to take a look at the example
`external-note-diff.qml <https://github.com/pbek/QOwnNotes/blob/develop/doc/scripting/external-note-diff.qml>`__.


Opening an input dialog with a select box
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Properties
^^^^^^^^^^

.. code:: cpp
/**
* Opens an input dialog with a select box
*
* @param title {QString} title of the dialog
* @param label {QString} label text of the dialog
* @param items {QStringList} list of items to select
* @param current {int} index of the item that should be selected (default: 0)
* @param editable {bool} if true the text in the dialog can be edited (default: false)
* @return {QString} text of the selected item
*/
QString ScriptingService::inputDialogGetItem(
const QString &title, const QString &label, const QStringList &items,
int current, bool editable);
Usage in QML
^^^^^^^^^^^^

.. code:: javascript
var result = script.inputDialogGetItem(
"combo box", "Please select an item", ["Item 1", "Item 2", "Item 3"]);
script.log(result);
You may want to take a look at the example
`input-dialogs.qml <https://github.com/pbek/QOwnNotes/blob/develop/doc/scripting/input-dialogs.qml>`__.



Hooks
-----
Expand Down
33 changes: 33 additions & 0 deletions doc/scripting/input-dialogs.qml
@@ -0,0 +1,33 @@
import QtQml 2.0
import QOwnNotesTypes 1.0

/**
* This example script with input dialogs as custom actions
*/
Script {
/**
* Initializes the custom actions
*/
function init() {
script.registerCustomAction("inputDialogGetItem", "Combo box");
}

/**
* This function is invoked when a custom action is triggered
* in the menu or via button
*
* @param identifier string the identifier defined in registerCustomAction
*/
function customActionInvoked(identifier) {
script.log("customActionInvoked - " + identifier);

switch (identifier) {
// open an input dialog with a select box
case "inputDialogGetItem":
var result = script.inputDialogGetItem(
"combo box", "Please select an item", ["Item 1", "Item 2", "Item 3"]);
script.log(result);
break;
}
}
}
32 changes: 32 additions & 0 deletions src/services/scriptingservice.cpp
Expand Up @@ -22,6 +22,8 @@
#ifndef INTEGRATION_TESTS
#include <mainwindow.h>
#include <QMessageBox>
#include <QInputDialog>

#endif

ScriptingService::ScriptingService(QObject *parent) : QObject(parent) {
Expand Down Expand Up @@ -1180,6 +1182,8 @@ QString ScriptingService::dirSeparator() {
*/
QStringList ScriptingService::selectedNotesPaths() {
QStringList selectedNotePaths;
MetricsService::instance()->sendVisitIfEnabled(
"scripting/" + QString(__func__));

#ifndef INTEGRATION_TESTS
MainWindow *mainWindow = MainWindow::instance();
Expand All @@ -1194,3 +1198,31 @@ QStringList ScriptingService::selectedNotesPaths() {

return selectedNotePaths;
}

/**
* Opens an input dialog with a select box
*
* @param title {QString} title of the dialog
* @param label {QString} label text of the dialog
* @param items {QStringList} list of items to select
* @param current {int} index of the item that should be selected (default: 0)
* @param editable {bool} if true the text in the dialog can be edited (default: false)
* @return {QString} text of the selected item
*/
QString ScriptingService::inputDialogGetItem(
const QString &title, const QString &label, const QStringList &items,
int current, bool editable) {
MetricsService::instance()->sendVisitIfEnabled(
"scripting/" + QString(__func__));

#ifndef INTEGRATION_TESTS
return QInputDialog::getItem(
Q_NULLPTR, title, label, items, current, editable);
#else
Q_UNUSED(title);
Q_UNUSED(label);
Q_UNUSED(items);
Q_UNUSED(current);
Q_UNUSED(editable);
#endif
}
4 changes: 4 additions & 0 deletions src/services/scriptingservice.h
Expand Up @@ -98,6 +98,10 @@ class ScriptingService : public QObject
Q_INVOKABLE QString dirSeparator();
Q_INVOKABLE QStringList selectedNotesPaths();

Q_INVOKABLE QString inputDialogGetItem(
const QString &title, const QString &label,
const QStringList &items, int current = 0, bool editable = false);

private:
QQmlEngine *_engine;
NoteApi *_currentNoteApi;
Expand Down

0 comments on commit 1bd01f8

Please sign in to comment.