Skip to content

Commit

Permalink
Allow modifying menu item properties in filter
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Holecek <[email protected]>
  • Loading branch information
hluk committed May 17, 2020
1 parent 90a8c65 commit a68123a
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 53 deletions.
1 change: 1 addition & 0 deletions src/common/mimetypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ const char mimeHidden[] = COPYQ_MIME_PREFIX "hidden";
const char mimeShortcut[] = COPYQ_MIME_PREFIX "shortcut";
const char mimeColor[] = COPYQ_MIME_PREFIX "color";
const char mimeOutputTab[] = COPYQ_MIME_PREFIX "output-tab";
const char mimeMenuItem[] = COPYQ_MIME_PREFIX "menu-item";
1 change: 1 addition & 0 deletions src/common/mimetypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ extern const char mimeHidden[];
extern const char mimeShortcut[];
extern const char mimeColor[];
extern const char mimeOutputTab[];
extern const char mimeMenuItem[];

#endif // MIMETYPES_H
4 changes: 2 additions & 2 deletions src/gui/commandwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Command CommandWidget::command() const
c.name = ui->lineEditName->text();
c.re = QRegularExpression( ui->lineEditMatch->text() );
c.wndre = QRegularExpression( ui->lineEditWindow->text() );
c.matchCmd = ui->lineEditMatchCmd->text();
c.matchCmd = ui->lineEditMatchCmd->command();
c.cmd = ui->commandEdit->command();
c.sep = ui->lineEditSeparator->text();
c.input = ui->comboBoxInputFormat->currentText();
Expand Down Expand Up @@ -163,7 +163,7 @@ void CommandWidget::setCommand(const Command &c)
ui->lineEditName->setText(c.name);
ui->lineEditMatch->setText( c.re.pattern() );
ui->lineEditWindow->setText( c.wndre.pattern() );
ui->lineEditMatchCmd->setText(c.matchCmd);
ui->lineEditMatchCmd->setCommand(c.matchCmd);
ui->commandEdit->setCommand(c.cmd);
ui->lineEditSeparator->setText(c.sep);
ui->comboBoxInputFormat->setEditText(c.input);
Expand Down
66 changes: 43 additions & 23 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,7 @@ void MainWindow::onSaveCommand(const Command &command)

void MainWindow::onItemCommandActionTriggered(CommandAction *commandAction, const QString &triggeredShortcut)
{
COPYQ_LOG( QString("Trigger: %1").arg(commandAction->text()) );
auto c = getPlaceholder()->createBrowser();
if (!c)
return;
Expand Down Expand Up @@ -1605,28 +1606,31 @@ void MainWindow::updateToolBar()
if ( action->isSeparator() ) {
m_toolBar->addSeparator();
} else if ( !action->icon().isNull() ) {
const QIcon icon = action->icon();
const QString text = action->text().remove("&");
const QString shortcut = action->shortcut().toString(QKeySequence::NativeText);
const QString label = text + (shortcut.isEmpty() ? QString() : "\n[" + shortcut + "]");
const QString tooltip = "<center>" + escapeHtml(text)
+ (shortcut.isEmpty() ? QString() : "<br /><b>" + escapeHtml(shortcut) + "</b>") + "</center>";
act = m_toolBar->addAction(icon, label);
act = m_toolBar->addAction(QString());

const auto update = [=]() {
const QIcon icon = action->icon();
act->setIcon(icon);

const QString text = action->text().remove("&");
const QString shortcut = action->shortcut().toString(QKeySequence::NativeText);
const QString label = text + (shortcut.isEmpty() ? QString() : "\n[" + shortcut + "]");
act->setText(label);

const QString tooltip = "<center>" + escapeHtml(text)
+ (shortcut.isEmpty() ? QString() : "<br /><b>" + escapeHtml(shortcut) + "</b>") + "</center>";
act->setToolTip(tooltip);
act->setEnabled(action->isEnabled());

if ( action->isCheckable() ) {
act->setCheckable(true);
act->setChecked(action->isChecked());
}
};

connect(act, &QAction::triggered, action, &QAction::triggered);
act->setToolTip(tooltip);

act->setEnabled(action->isEnabled());
connect( action, &QAction::changed,
act, [=]() { act->setEnabled(action->isEnabled()); } );

if ( action->isCheckable() ) {
act->setCheckable(true);
act->setChecked(action->isChecked());
connect( act, &QAction::triggered,
action, &QAction::setChecked );
connect( action, &QAction::toggled,
act, &QAction::setChecked );
}
connect(action, &QAction::changed, act, update);
update();
}
}

Expand Down Expand Up @@ -2915,7 +2919,7 @@ void MainWindow::setTrayTooltip(const QString &tooltip)
m_tray->setToolTip(tooltip);
}

bool MainWindow::setMenuItemEnabled(int actionId, int currentRun, int menuItemMatchCommandIndex, bool enabled)
bool MainWindow::setMenuItemEnabled(int actionId, int currentRun, int menuItemMatchCommandIndex, const QVariantMap &menuItem)
{
if (actionId != m_trayMenuMatchCommands.actionId && actionId != m_itemMenuMatchCommands.actionId)
return false;
Expand All @@ -2934,7 +2938,23 @@ bool MainWindow::setMenuItemEnabled(int actionId, int currentRun, int menuItemMa
if (!action)
return true;

action->setEnabled(enabled);
for (auto it = menuItem.constBegin(); it != menuItem.constEnd(); ++it) {
const auto key = it.key().toUtf8();
if (key == "color" || key == "tag")
continue;

const auto value = it.value();
if (key == "icon") {
const QString icon = value.toString();
const QString colorName = menuItem.value("color").toString();
const QColor color = colorName.isEmpty() ? getDefaultIconColor(*this) : deserializeColor(colorName);
const QString tag = menuItem.value("tag").toString();
action->setIcon( iconFromFile(icon, tag, color) );
} else {
action->setProperty(key, value);
}
}
const bool enabled = action->isEnabled();
action->setProperty(propertyActionFilterCommandFailed, !enabled);

const auto shortcuts = action->shortcuts();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class MainWindow final : public QMainWindow

void setTrayTooltip(const QString &tooltip);

bool setMenuItemEnabled(int actionId, int currentRun, int menuItemMatchCommandIndex, bool enabled);
bool setMenuItemEnabled(int actionId, int currentRun, int menuItemMatchCommandIndex, const QVariantMap &menuItem);

QVariantMap setDisplayData(int actionId, const QVariantMap &data);

Expand Down
5 changes: 4 additions & 1 deletion src/scriptable/scriptable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2563,8 +2563,11 @@ void Scriptable::runMenuCommandFilters()
PerformanceLogger logger( QLatin1String("Menu item filters") );

for (int i = 0; i < matchCommands.length(); ++i) {
m_engine->globalObject().setProperty( "menuItem", m_engine->newObject() );
const bool enabled = canExecuteCommandFilter(matchCommands[i]);
if ( !m_proxy->enableMenuItem(actionId, currentRun, i, enabled) )
QVariantMap menuItem = toDataMap(m_engine->globalObject().property("menuItem"));
menuItem["enabled"] = enabled && menuItem.value("enabled", true).toBool();
if ( !m_proxy->enableMenuItem(actionId, currentRun, i, menuItem) )
break;
}
});
Expand Down
2 changes: 2 additions & 0 deletions src/scriptable/scriptable.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Scriptable final : public QObject, protected QScriptable
Q_PROPERTY(QScriptValue mimeShortcut READ getMimeShortcut CONSTANT)
Q_PROPERTY(QScriptValue mimeColor READ getMimeColor CONSTANT)
Q_PROPERTY(QScriptValue mimeOutputTab READ getMimeOutputTab CONSTANT)
Q_PROPERTY(QScriptValue mimeMenuItem READ getMimeMenuItem CONSTANT)

Q_PROPERTY(QScriptValue global READ getGlobal CONSTANT)
Q_PROPERTY(QScriptValue plugins READ getPlugins CONSTANT)
Expand Down Expand Up @@ -143,6 +144,7 @@ class Scriptable final : public QObject, protected QScriptable
QScriptValue getMimeShortcut() const { return mimeShortcut; }
QScriptValue getMimeColor() const { return mimeColor; }
QScriptValue getMimeOutputTab() const { return mimeOutputTab; }
QScriptValue getMimeMenuItem() const { return mimeMenuItem; }

QScriptValue getGlobal();
QScriptValue getPlugins();
Expand Down
6 changes: 3 additions & 3 deletions src/scriptable/scriptableproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2137,10 +2137,10 @@ void ScriptableProxy::showDataNotification(const QVariantMap &data)
}
}

bool ScriptableProxy::enableMenuItem(int actionId, int currentRun, int menuItemMatchCommandIndex, bool enabled)
bool ScriptableProxy::enableMenuItem(int actionId, int currentRun, int menuItemMatchCommandIndex, const QVariantMap &menuItem)
{
INVOKE_NO_SNIP(enableMenuItem, (actionId, currentRun, menuItemMatchCommandIndex, enabled));
return m_wnd->setMenuItemEnabled(actionId, currentRun, menuItemMatchCommandIndex, enabled);
INVOKE_NO_SNIP(enableMenuItem, (actionId, currentRun, menuItemMatchCommandIndex, menuItem));
return m_wnd->setMenuItemEnabled(actionId, currentRun, menuItemMatchCommandIndex, menuItem);
}

QVariantMap ScriptableProxy::setDisplayData(int actionId, const QVariantMap &displayData)
Expand Down
2 changes: 1 addition & 1 deletion src/scriptable/scriptableproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public slots:
void saveData(const QString &tab, const QVariantMap &data, ClipboardMode mode);
void showDataNotification(const QVariantMap &data);

bool enableMenuItem(int actionId, int currentRun, int menuItemMatchCommandIndex, bool enabled);
bool enableMenuItem(int actionId, int currentRun, int menuItemMatchCommandIndex, const QVariantMap &menuItem);

QVariantMap setDisplayData(int actionId, const QVariantMap &displayData);

Expand Down
53 changes: 31 additions & 22 deletions src/ui/commandwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
<x>0</x>
<y>0</y>
<width>277</width>
<height>556</height>
<height>564</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
Expand Down Expand Up @@ -346,46 +346,55 @@
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_9">
<widget class="QLabel" name="label_7">
<property name="text">
<string>&amp;Filter:</string>
<string>For&amp;mat:</string>
</property>
<property name="buddy">
<cstring>lineEditMatchCmd</cstring>
<cstring>comboBoxInputFormat</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="lineEditMatchCmd">
<widget class="QComboBox" name="comboBoxInputFormat">
<property name="toolTip">
<string>&lt;p&gt;Use commands only if filter command succeeds.&lt;/p&gt;

&lt;p&gt;Item text is passed to &lt;b&gt;standard input&lt;/b&gt; of the filter command. The item is &lt;b&gt;matched only if the filter command exit code is 0&lt;/b&gt;.&lt;/p&gt;

&lt;p&gt;Use &lt;b&gt;%1&lt;/b&gt; for item text passed as argument and &lt;b&gt;%2&lt;/b&gt; to &lt;b&gt;%9&lt;/b&gt; for arguments captured by regular expression (parts enclosed in parentheses).&lt;/p&gt;

&lt;p&gt;Use &lt;b&gt;|&lt;/b&gt; to chain commands (pass standard output to next command).&lt;/p&gt;</string>
<string>Data of this MIME type will be sent to standard input of command.
Leave empty to disable this.</string>
</property>
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_7">
<widget class="QLabel" name="label_9">
<property name="text">
<string>For&amp;mat:</string>
<string>&amp;Filter:</string>
</property>
<property name="buddy">
<cstring>comboBoxInputFormat</cstring>
<cstring>lineEditMatchCmd</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="comboBoxInputFormat">
<property name="toolTip">
<string>Data of this MIME type will be sent to standard input of command.
Leave empty to disable this.</string>
<widget class="CommandEdit" name="lineEditMatchCmd" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="editable">
<bool>true</bool>
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="toolTip">
<string>&lt;p&gt;Use commands only if filter command succeeds.&lt;/p&gt;

&lt;p&gt;Item text is passed to &lt;b&gt;standard input&lt;/b&gt; of the filter command. The item is &lt;b&gt;matched only if the filter command exit code is 0&lt;/b&gt;.&lt;/p&gt;

&lt;p&gt;Use &lt;b&gt;%1&lt;/b&gt; for item text passed as argument and &lt;b&gt;%2&lt;/b&gt; to &lt;b&gt;%9&lt;/b&gt; for arguments captured by regular expression (parts enclosed in parentheses).&lt;/p&gt;

&lt;p&gt;Use &lt;b&gt;|&lt;/b&gt; to chain commands (pass standard output to next command).&lt;/p&gt;</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -716,8 +725,8 @@ Note: If this is applied automatically, no other automatic commands are executed
<tabstop>checkBoxDisplay</tabstop>
<tabstop>lineEditMatch</tabstop>
<tabstop>lineEditWindow</tabstop>
<tabstop>lineEditMatchCmd</tabstop>
<tabstop>comboBoxInputFormat</tabstop>
<tabstop>lineEditMatchCmd</tabstop>
<tabstop>comboBoxCopyToTab</tabstop>
<tabstop>checkBoxIgnore</tabstop>
<tabstop>checkBoxHideWindow</tabstop>
Expand Down

0 comments on commit a68123a

Please sign in to comment.