work save

This commit is contained in:
Roy Qu 2024-04-02 14:32:52 +08:00
parent 775148eb43
commit bca4be08b2
2 changed files with 22 additions and 20 deletions

View File

@ -51,7 +51,7 @@ ToolsGeneralWidget::ToolsGeneralWidget(const QString &name, const QString &group
QItemSelectionModel *m=ui->lstTools->selectionModel(); QItemSelectionModel *m=ui->lstTools->selectionModel();
ui->lstTools->setModel(&mToolsModel); ui->lstTools->setModel(&mToolsModel);
delete m; delete m;
mEditType = EditType::None; mCurrentEditingRow = -1;
showEditPanel(false); showEditPanel(false);
connect(ui->lstTools->selectionModel(), &QItemSelectionModel::currentRowChanged, connect(ui->lstTools->selectionModel(), &QItemSelectionModel::currentRowChanged,
this,&ToolsGeneralWidget::onToolsCurrentChanged); this,&ToolsGeneralWidget::onToolsCurrentChanged);
@ -94,9 +94,11 @@ void ToolsGeneralWidget::onToolsCurrentChanged(const QModelIndex &current, const
void ToolsGeneralWidget::finishEditing(bool askSave, const QModelIndex& itemIndex) void ToolsGeneralWidget::finishEditing(bool askSave, const QModelIndex& itemIndex)
{ {
auto action = finally([this]{ auto action = finally([this]{
mEdited = false;
mCurrentEditingRow = -1;
showEditPanel(false); showEditPanel(false);
}); });
if (mEditType == EditType::None) if (mCurrentEditingRow == -1)
return; return;
if (!mEdited) if (!mEdited)
return; return;
@ -113,19 +115,13 @@ void ToolsGeneralWidget::finishEditing(bool askSave, const QModelIndex& itemInde
tr("Title shouldn't be empty!")); tr("Title shouldn't be empty!"));
return; return;
} }
mEditType = EditType::None; PToolItem item = mToolsModel.getTool(mCurrentEditingRow);
QModelIndex index=itemIndex.isValid()?itemIndex:ui->lstTools->currentIndex();
if (!index.isValid())
return;
PToolItem item = mToolsModel.getTool(index.row());
item->workingDirectory = ui->txtDirectory->text(); item->workingDirectory = ui->txtDirectory->text();
item->parameters = ui->txtParameters->text(); item->parameters = ui->txtParameters->text();
item->program = ui->txtProgram->text(); item->program = ui->txtProgram->text();
item->title = ui->txtTitle->text(); item->title = ui->txtTitle->text();
item->inputOrigin = static_cast<ToolItemInputOrigin>(ui->cbInput->currentIndex()); item->inputOrigin = static_cast<ToolItemInputOrigin>(ui->cbInput->currentIndex());
item->outputTarget = static_cast<ToolItemOutputTarget>(ui->cbOutput->currentIndex()); item->outputTarget = static_cast<ToolItemOutputTarget>(ui->cbOutput->currentIndex());
mEdited=false;
} }
void ToolsGeneralWidget::prepareEdit(const PToolItem& item) void ToolsGeneralWidget::prepareEdit(const PToolItem& item)
@ -186,14 +182,23 @@ void ToolsModel::addTool(PToolItem item)
endInsertRows(); endInsertRows();
} }
PToolItem ToolsModel::getTool(int index) PToolItem ToolsModel::getTool(int row)
{ {
return mTools[index]; return mTools[row];
} }
void ToolsModel::removeTool(int index) void ToolsModel::updateTool(int row, PToolItem item)
{ {
mTools.removeAt(index); mTools[row] = item;
QModelIndex index=createIndex(row, 0);
emit dataChanged(index, index);
}
void ToolsModel::removeTool(int row)
{
beginRemoveRows(QModelIndex(),row,row);
mTools.removeAt(row);
endRemoveRows();
} }
int ToolsModel::rowCount(const QModelIndex &) const int ToolsModel::rowCount(const QModelIndex &) const

View File

@ -34,8 +34,9 @@ public:
const QList<PToolItem> &tools() const; const QList<PToolItem> &tools() const;
void setTools(const QList<PToolItem> &newTools); void setTools(const QList<PToolItem> &newTools);
void addTool(PToolItem item); void addTool(PToolItem item);
PToolItem getTool(int index); PToolItem getTool(int row);
void removeTool(int index); void updateTool(int row, PToolItem item);
void removeTool(int row);
private: private:
QList<PToolItem> mTools; QList<PToolItem> mTools;
@ -51,10 +52,6 @@ class ToolsGeneralWidget : public SettingsWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
enum class EditType {
Edit,
None
};
explicit ToolsGeneralWidget(const QString& name, const QString& group, QWidget *parent = nullptr); explicit ToolsGeneralWidget(const QString& name, const QString& group, QWidget *parent = nullptr);
~ToolsGeneralWidget(); ~ToolsGeneralWidget();
private: private:
@ -82,8 +79,8 @@ private:
Ui::ToolsGeneralWidget *ui; Ui::ToolsGeneralWidget *ui;
MacroInfoModel mMacroInfoModel; MacroInfoModel mMacroInfoModel;
ToolsModel mToolsModel; ToolsModel mToolsModel;
EditType mEditType;
bool mEdited; bool mEdited;
int mCurrentEditingRow;
// SettingsWidget interface // SettingsWidget interface
protected: protected: