Commit 0b624809 authored by Tao Guo's avatar Tao Guo Committed by Tao Guo
Browse files

Add new plugin QuickCommands (2nd)



1. add valid for Title and Command
2. add Tooltip field
3. other small fix
Signed-off-by: default avatarTao Guo <tao.guo@amlogic.com>
parent 365a5298
Pipeline #134002 passed with stage
in 3 minutes and 28 seconds
......@@ -30,21 +30,26 @@
<item row="0" column="1">
<widget class="QLineEdit" name="name"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Caption</string>
<item row="2" column="1">
<widget class="QComboBox" name="group">
<property name="editable">
<bool>true</bool>
</property>
<item>
<property name="text">
<string>Default</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="labelGroup">
<property name="text">
<string>Group</string>
</property>
</widget>
</item>
<item row="2" column="0">
<item row="4" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Command</string>
......@@ -54,25 +59,30 @@
</property>
</widget>
</item>
<item row="2" column="1">
<item row="4" column="1">
<widget class="QPlainTextEdit" name="command">
<property name="plainText">
<string notr="true"/>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="group">
<property name="editable">
<bool>true</bool>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Title</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Tooltip</string>
</property>
<item>
<property name="text">
<string>Default</string>
</property>
</item>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="tooltip"/>
</item>
</layout>
</widget>
</item>
......
......@@ -11,6 +11,7 @@ class QuickCommandData
{
public:
QString name;
QString tooltip;
QString command;
};
......
......@@ -29,6 +29,7 @@ void QuickCommandsModel::load()
QuickCommandData data;
KConfigGroup element = group.group(commandGroup);
data.name = element.readEntry("name");
data.tooltip = element.readEntry("tooltip");
data.command = element.readEntry("command");
addChildItem(data, groupName);
}
......@@ -48,8 +49,9 @@ void QuickCommandsModel::save()
for (int j = 0, end2 = groupItem->rowCount(); j < end2; j++) {
QStandardItem *item = groupItem->child(j);
const auto data = item->data(QuickCommandRole).value<QuickCommandData>();
KConfigGroup element = baseGroup.group(data.name.trimmed());
KConfigGroup element = baseGroup.group(data.name);
element.writeEntry("name", data.name);
element.writeEntry("tooltip", data.tooltip);
element.writeEntry("command", data.command);
element.sync();
}
......@@ -99,11 +101,9 @@ bool QuickCommandsModel::addChildItem(const QuickCommandData &data, const QStrin
return false;
}
auto newChild = new QStandardItem();
newChild->setData(QVariant::fromValue(data), QuickCommandRole);
newChild->setText(data.name);
newChild->setToolTip(data.command);
parentItem->appendRow(newChild);
auto item = new QStandardItem();
updateItem(item, data);
parentItem->appendRow(item);
parentItem->sortChildren(0);
return true;
}
......@@ -121,11 +121,19 @@ bool QuickCommandsModel::editChildItem(const QuickCommandData &data, const QMode
return false;
parentItem->removeRow(item->row());
} else {
item->setData(QVariant::fromValue(data), QuickCommandRole);
item->setText(data.name);
item->setToolTip(data.command);
updateItem(item, data);
item->parent()->sortChildren(0);
}
return true;
}
void QuickCommandsModel::updateItem(QStandardItem *item, const QuickCommandData &data)
{
item->setData(QVariant::fromValue(data), QuickCommandRole);
item->setText(data.name);
if (data.tooltip.trimmed().isEmpty())
item->setToolTip(data.command);
else
item->setToolTip(data.tooltip);
}
......@@ -24,6 +24,7 @@ public:
private:
void load();
void save();
void updateItem(QStandardItem *item, const QuickCommandData &data);
QStandardItem *addTopLevelItem(const QString &groupName);
};
......
......@@ -88,6 +88,7 @@ void QuickCommandsWidget::addMode()
{
ui->command->setPlainText({});
ui->name->setText({});
ui->tooltip->setText({});
ui->btnAdd->hide();
ui->btnSave->show();
......@@ -107,19 +108,23 @@ void QuickCommandsWidget::editMode()
void QuickCommandsWidget::saveCommand()
{
if (!valid())
return;
if (priv->model->addChildItem(data(), ui->group->currentText()))
viewMode();
else
KMessageBox::messageBox(this, KMessageBox::DialogType::Error, i18n("A duplicate name exists"));
KMessageBox::messageBox(this, KMessageBox::DialogType::Error, i18n("A duplicate item exists"));
}
void QuickCommandsWidget::updateCommand()
{
const auto sourceIdx = priv->filterModel->mapToSource(ui->commandsTreeView->currentIndex());
if (!valid())
return;
if (priv->model->editChildItem(data(), sourceIdx, ui->group->currentText()))
viewMode();
else
KMessageBox::messageBox(this, KMessageBox::DialogType::Error, i18n("A duplicate name exists"));
KMessageBox::messageBox(this, KMessageBox::DialogType::Error, i18n("A duplicate item exists"));
}
void QuickCommandsWidget::invokeCommand(const QModelIndex &idx)
......@@ -146,6 +151,7 @@ void QuickCommandsWidget::triggerEdit()
const auto item = priv->model->itemFromIndex(sourceIdx);
const auto data = item->data(QuickCommandsModel::QuickCommandRole).value<QuickCommandData>();
ui->name->setText(data.name);
ui->tooltip->setText(data.tooltip);
ui->command->setPlainText(data.command);
ui->group->setCurrentText(item->parent()->text());
editMode();
......@@ -182,6 +188,7 @@ QuickCommandData QuickCommandsWidget::data() const
{
QuickCommandData data;
data.name = ui->name->text().trimmed();
data.tooltip = ui->tooltip->text();
data.command = ui->command->toPlainText();
return data;
}
......@@ -195,3 +202,16 @@ void QuickCommandsWidget::setCurrentController(Konsole::SessionController *contr
{
priv->controller = controller;
}
bool QuickCommandsWidget::valid()
{
if (ui->name->text().isEmpty() || ui->name->text().trimmed().isEmpty()) {
KMessageBox::messageBox(this, KMessageBox::DialogType::Error, i18n("Title can not be empty or blank"));
return false;
}
if (ui->command->toPlainText().isEmpty()) {
KMessageBox::messageBox(this, KMessageBox::DialogType::Error, i18n("Command can not be empty"));
return false;
}
return true;
}
......@@ -49,6 +49,7 @@ public:
private:
QuickCommandData data() const;
void prepareEdit();
bool valid();
struct Private;
std::unique_ptr<Ui::QuickCommandsWidget> ui;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment