1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/*
* Cantata
*
* Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "customactionssettings.h"
#include "customactions.h"
#include "support/messagebox.h"
#include "widgets/basicitemdelegate.h"
#include "widgets/icons.h"
#include "widgets/notelabel.h"
#include <QFormLayout>
#include <QGridLayout>
#include <QHeaderView>
#include <QLabel>
#include <QPushButton>
#include <QSpacerItem>
CustomActionDialog::CustomActionDialog(QWidget* p)
: Dialog(p)
{
QWidget* widget = new QWidget(this);
QFormLayout* layout = new QFormLayout(widget);
nameEntry = new LineEdit(widget);
commandEntry = new LineEdit(widget);
nameEntry->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
commandEntry->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
layout->addRow(new QLabel(tr("Name:"), widget), nameEntry);
layout->addRow(new QLabel(tr("Command:"), widget), commandEntry);
layout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
NoteLabel* note = new NoteLabel(widget);
note->setText(tr("In the command line above, %f will be replaced with the file list and %d with the folder list. If neither are supplied, the the list of files will be appended to the command."));
layout->setWidget(2, QFormLayout::SpanningRole, note);
layout->setContentsMargins(0, 0, 0, 0);
setButtons(Dialog::Ok | Dialog::Cancel);
setMainWidget(widget);
setMinimumWidth(400);
ensurePolished();
adjustSize();
}
bool CustomActionDialog::create()
{
setCaption(tr("Add New Command"));
nameEntry->setText(QString());
commandEntry->setText(QString());
return QDialog::Accepted == exec();
}
bool CustomActionDialog::edit(const QString& name, const QString& cmd)
{
setCaption(tr("Edit Command"));
nameEntry->setText(name);
commandEntry->setText(cmd);
return QDialog::Accepted == exec();
}
static inline void setResizeMode(QHeaderView* hdr, int idx, QHeaderView::ResizeMode mode)
{
hdr->setSectionResizeMode(idx, mode);
}
CustomActionsSettings::CustomActionsSettings(QWidget* parent)
: QWidget(parent), dlg(nullptr)
{
QGridLayout* layout = new QGridLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
QLabel* label = new QLabel(tr("To have Cantata call external commands (e.g. to edit tags with another application), add an entry for the command below. When at least one command "
"command is defined, a 'Custom Actions' entry will be added to the context menus in the Library, Folders, and Playlists views."),
this);
QFont f(Utils::smallFont(label->font()));
f.setItalic(true);
label->setFont(f);
label->setWordWrap(true);
layout->addWidget(label, 0, 0, 1, 2);
tree = new QTreeWidget(this);
add = new QPushButton(this);
edit = new QPushButton(this);
del = new QPushButton(this);
layout->addWidget(tree, 1, 0, 4, 1);
layout->addWidget(add, 1, 1, 1, 1);
layout->addWidget(edit, 2, 1, 1, 1);
layout->addWidget(del, 3, 1, 1, 1);
layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Fixed, QSizePolicy::MinimumExpanding), 4, 1);
add->setText(tr("Add"));
edit->setText(tr("Edit"));
del->setText(tr("Remove"));
edit->setEnabled(false);
del->setEnabled(false);
tree->setHeaderLabels(QStringList() << tr("Name") << tr("Command"));
tree->setAllColumnsShowFocus(true);
tree->setSelectionMode(QAbstractItemView::ExtendedSelection);
tree->setRootIsDecorated(false);
tree->setSortingEnabled(true);
setResizeMode(tree->header(), 0, QHeaderView::ResizeToContents);
tree->header()->setStretchLastSection(true);
tree->setAlternatingRowColors(false);
tree->setItemDelegate(new BasicItemDelegate(this));
connect(tree, SIGNAL(itemSelectionChanged()), this, SLOT(controlButtons()));
connect(add, SIGNAL(clicked()), SLOT(addCommand()));
connect(edit, SIGNAL(clicked()), SLOT(editCommand()));
connect(del, SIGNAL(clicked()), SLOT(delCommand()));
}
CustomActionsSettings::~CustomActionsSettings()
{
}
void CustomActionsSettings::controlButtons()
{
int selCount = tree->selectedItems().count();
edit->setEnabled(1 == selCount);
del->setEnabled(selCount > 0);
}
void CustomActionsSettings::load()
{
for (const CustomActions::Command& cmd : CustomActions::self()->commandList()) {
new QTreeWidgetItem(tree, QStringList() << cmd.name << cmd.cmd);
}
}
void CustomActionsSettings::save()
{
QList<CustomActions::Command> commands;
for (int i = 0; i < tree->topLevelItemCount(); ++i) {
commands.append((CustomActions::Command(tree->topLevelItem(i)->text(0), tree->topLevelItem(i)->text(1))));
}
CustomActions::self()->set(commands);
}
void CustomActionsSettings::addCommand()
{
if (!dlg) {
dlg = new CustomActionDialog(this);
}
if (dlg->create() && !dlg->nameText().isEmpty() && !dlg->commandText().isEmpty()) {
new QTreeWidgetItem(tree, QStringList() << dlg->nameText() << dlg->commandText());
}
}
void CustomActionsSettings::editCommand()
{
QList<QTreeWidgetItem*> items = tree->selectedItems();
if (1 != items.count()) {
return;
}
if (!dlg) {
dlg = new CustomActionDialog(this);
}
QTreeWidgetItem* item = items.at(0);
if (dlg->edit(item->text(0), item->text(1)) && !dlg->nameText().isEmpty() && !dlg->commandText().isEmpty()) {
item->setText(0, dlg->nameText());
item->setText(1, dlg->commandText());
}
}
void CustomActionsSettings::delCommand()
{
if (MessageBox::Yes == MessageBox::warningYesNo(this, tr("Remove the selected commands?"), QString(), GuiItem(tr("Remove")), StdGuiItem::cancel())) {
for (QTreeWidgetItem* i : tree->selectedItems()) {
delete i;
}
}
}
#include "moc_customactionssettings.cpp"
|