mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-15 09:15:34 +08:00
Allow to save "selected objects" only as JSON
This commit is contained in:
@@ -740,16 +740,22 @@ namespace BlackGui
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename ObjectType, typename ContainerType, bool UseCompare>
|
template <typename ObjectType, typename ContainerType, bool UseCompare>
|
||||||
QJsonObject CListModelBase<ObjectType, ContainerType, UseCompare>::toJson() const
|
QJsonObject CListModelBase<ObjectType, ContainerType, UseCompare>::toJson(bool selectedOnly) const
|
||||||
{
|
{
|
||||||
const CVariant variant = CVariant::fromValue(container());
|
const CVariant variant = CVariant::fromValue(
|
||||||
|
selectedOnly && m_selectionModel ?
|
||||||
|
m_selectionModel->selectedObjects() :
|
||||||
|
container());
|
||||||
return variant.toJson();
|
return variant.toJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ObjectType, typename ContainerType, bool UseCompare>
|
template <typename ObjectType, typename ContainerType, bool UseCompare>
|
||||||
QString CListModelBase<ObjectType, ContainerType, UseCompare>::toJsonString(QJsonDocument::JsonFormat format) const
|
QString CListModelBase<ObjectType, ContainerType, UseCompare>::toJsonString(QJsonDocument::JsonFormat format, bool selectedOnly) const
|
||||||
{
|
{
|
||||||
const CVariant variant = CVariant::fromValue(container());
|
const CVariant variant = CVariant::fromValue(
|
||||||
|
selectedOnly && m_selectionModel ?
|
||||||
|
m_selectionModel->selectedObjects() :
|
||||||
|
container());
|
||||||
return variant.toJsonString(format);
|
return variant.toJsonString(format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ namespace BlackGui
|
|||||||
static constexpr int asyncThreshold = 50;
|
static constexpr int asyncThreshold = 50;
|
||||||
|
|
||||||
//! Destructor
|
//! Destructor
|
||||||
virtual ~CListModelBaseNonTemplate() {}
|
virtual ~CListModelBaseNonTemplate() override {}
|
||||||
|
|
||||||
//! \name Functions from QStandardItemModel
|
//! \name Functions from QStandardItemModel
|
||||||
//! @{
|
//! @{
|
||||||
@@ -125,10 +125,10 @@ namespace BlackGui
|
|||||||
void emitDataChanged(int startRowIndex, int endRowIndex);
|
void emitDataChanged(int startRowIndex, int endRowIndex);
|
||||||
|
|
||||||
//! Convert to JSON
|
//! Convert to JSON
|
||||||
virtual QJsonObject toJson() const = 0;
|
virtual QJsonObject toJson(bool selectedOnly = false) const = 0;
|
||||||
|
|
||||||
//! Convert to JSON string
|
//! Convert to JSON string
|
||||||
virtual QString toJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Indented) const = 0;
|
virtual QString toJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Indented, bool selectedOnly = false) const = 0;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
//! Asynchronous update finished
|
//! Asynchronous update finished
|
||||||
@@ -194,8 +194,8 @@ namespace BlackGui
|
|||||||
|
|
||||||
//! \name Functions from CListModelBaseNonTemplate
|
//! \name Functions from CListModelBaseNonTemplate
|
||||||
//! @{
|
//! @{
|
||||||
virtual QJsonObject toJson() const override;
|
virtual QJsonObject toJson(bool selectedOnly = false) const override;
|
||||||
virtual QString toJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Indented) const override;
|
virtual QString toJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Indented, bool selectedOnly = false) const override;
|
||||||
virtual bool isOrderable() const override;
|
virtual bool isOrderable() const override;
|
||||||
//! @}
|
//! @}
|
||||||
|
|
||||||
|
|||||||
@@ -211,9 +211,9 @@ namespace BlackGui
|
|||||||
return this->ps_loadJson(directory);
|
return this->ps_loadJson(directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
CStatusMessage CViewBaseNonTemplate::showFileSaveDialog(const QString &directory)
|
CStatusMessage CViewBaseNonTemplate::showFileSaveDialog(bool selectedOnly, const QString &directory)
|
||||||
{
|
{
|
||||||
return this->ps_saveJson(directory);
|
return this->ps_saveJson(selectedOnly, directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
IMenuDelegate *CViewBaseNonTemplate::setCustomMenu(IMenuDelegate *menu, bool nestPreviousMenu)
|
IMenuDelegate *CViewBaseNonTemplate::setCustomMenu(IMenuDelegate *menu, bool nestPreviousMenu)
|
||||||
@@ -277,7 +277,15 @@ namespace BlackGui
|
|||||||
}
|
}
|
||||||
case MenuMaterializeFilter: { ma.addAction(CIcons::tableRelationship16(), "Materialize filtered data", CMenuAction::pathViewFilter(), { this, &CViewBaseNonTemplate::materializeFilter }); break; }
|
case MenuMaterializeFilter: { ma.addAction(CIcons::tableRelationship16(), "Materialize filtered data", CMenuAction::pathViewFilter(), { this, &CViewBaseNonTemplate::materializeFilter }); break; }
|
||||||
case MenuLoad: { ma.addAction(CIcons::disk16(), "Load from file ", CMenuAction::pathViewLoadSave(), { this, &CViewBaseNonTemplate::loadJsonAction }); break; }
|
case MenuLoad: { ma.addAction(CIcons::disk16(), "Load from file ", CMenuAction::pathViewLoadSave(), { this, &CViewBaseNonTemplate::loadJsonAction }); break; }
|
||||||
case MenuSave: { ma.addAction(CIcons::disk16(), "Save data in file " + CShortcut::toParenthesisString(CShortcut::keySaveViews()), CMenuAction::pathViewLoadSave(), { this, &CViewBaseNonTemplate::saveJsonAction }, CShortcut::keySaveViews()); break; }
|
case MenuSave:
|
||||||
|
{
|
||||||
|
ma.addAction(CIcons::disk16(), "Save data in file " + CShortcut::toParenthesisString(CShortcut::keySaveViews()), CMenuAction::pathViewLoadSave(), { this, &CViewBaseNonTemplate::saveJsonAction }, CShortcut::keySaveViews());
|
||||||
|
if (this->hasSelection())
|
||||||
|
{
|
||||||
|
ma.addAction(CIcons::disk16(), "Save selected data in file", CMenuAction::pathViewLoadSave(), { this, &CViewBaseNonTemplate::saveSelectedJsonAction }); break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
case MenuCut:
|
case MenuCut:
|
||||||
{
|
{
|
||||||
if (!QApplication::clipboard()) break;
|
if (!QApplication::clipboard()) break;
|
||||||
@@ -620,7 +628,18 @@ namespace BlackGui
|
|||||||
{
|
{
|
||||||
if (this->isEmpty()) { return; }
|
if (this->isEmpty()) { return; }
|
||||||
if (!m_menus.testFlag(MenuSave)) { return; }
|
if (!m_menus.testFlag(MenuSave)) { return; }
|
||||||
const CStatusMessage m = this->ps_saveJson();
|
const CStatusMessage m = this->ps_saveJson(false);
|
||||||
|
if (!m.isEmpty())
|
||||||
|
{
|
||||||
|
CLogMessage::preformatted(m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CViewBaseNonTemplate::saveSelectedJsonAction()
|
||||||
|
{
|
||||||
|
if (this->isEmpty()) { return; }
|
||||||
|
if (!m_menus.testFlag(MenuSave)) { return; }
|
||||||
|
const CStatusMessage m = this->ps_saveJson(true);
|
||||||
if (!m.isEmpty())
|
if (!m.isEmpty())
|
||||||
{
|
{
|
||||||
CLogMessage::preformatted(m);
|
CLogMessage::preformatted(m);
|
||||||
@@ -1315,17 +1334,17 @@ namespace BlackGui
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class ModelClass, class ContainerType, class ObjectType>
|
template <class ModelClass, class ContainerType, class ObjectType>
|
||||||
QJsonObject CViewBase<ModelClass, ContainerType, ObjectType>::toJson() const
|
QJsonObject CViewBase<ModelClass, ContainerType, ObjectType>::toJson(bool selectedOnly) const
|
||||||
{
|
{
|
||||||
Q_ASSERT(m_model);
|
Q_ASSERT(m_model);
|
||||||
return m_model->toJson();
|
return m_model->toJson(selectedOnly);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class ModelClass, class ContainerType, class ObjectType>
|
template <class ModelClass, class ContainerType, class ObjectType>
|
||||||
QString CViewBase<ModelClass, ContainerType, ObjectType>::toJsonString(QJsonDocument::JsonFormat format) const
|
QString CViewBase<ModelClass, ContainerType, ObjectType>::toJsonString(QJsonDocument::JsonFormat format, bool selectedOnly) const
|
||||||
{
|
{
|
||||||
Q_ASSERT(m_model);
|
Q_ASSERT(m_model);
|
||||||
return m_model->toJsonString(format);
|
return m_model->toJsonString(format, selectedOnly);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class ModelClass, class ContainerType, class ObjectType>
|
template <class ModelClass, class ContainerType, class ObjectType>
|
||||||
@@ -1609,14 +1628,14 @@ namespace BlackGui
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class ModelClass, class ContainerType, class ObjectType>
|
template <class ModelClass, class ContainerType, class ObjectType>
|
||||||
CStatusMessage CViewBase<ModelClass, ContainerType, ObjectType>::ps_saveJson(const QString &directory)
|
CStatusMessage CViewBase<ModelClass, ContainerType, ObjectType>::ps_saveJson(bool selectedOnly, const QString &directory)
|
||||||
{
|
{
|
||||||
const QString fileName = QFileDialog::getSaveFileName(nullptr,
|
const QString fileName = QFileDialog::getSaveFileName(nullptr,
|
||||||
tr("Save data file"),
|
tr("Save data file"),
|
||||||
directory.isEmpty() ? this->getFileDialogFileName(false) : directory,
|
directory.isEmpty() ? this->getFileDialogFileName(false) : directory,
|
||||||
tr("swift (*.json *.txt)"));
|
tr("swift (*.json *.txt)"));
|
||||||
if (fileName.isEmpty()) { return CStatusMessage(this, CStatusMessage::SeverityDebug, "Save canceled", true); }
|
if (fileName.isEmpty()) { return CStatusMessage(this, CStatusMessage::SeverityDebug, "Save canceled", true); }
|
||||||
const QString json(this->toJsonString()); // save as CVariant JSON
|
const QString json(this->toJsonString(QJsonDocument::Indented, selectedOnly)); // save as CVariant JSON
|
||||||
|
|
||||||
// save file
|
// save file
|
||||||
const bool ok = CFileUtils::writeStringToFileInBackground(json, fileName);
|
const bool ok = CFileUtils::writeStringToFileInBackground(json, fileName);
|
||||||
|
|||||||
@@ -276,7 +276,7 @@ namespace BlackGui
|
|||||||
BlackMisc::CStatusMessage showFileLoadDialog(const QString &directory = {});
|
BlackMisc::CStatusMessage showFileLoadDialog(const QString &directory = {});
|
||||||
|
|
||||||
//! Show file save dialog
|
//! Show file save dialog
|
||||||
BlackMisc::CStatusMessage showFileSaveDialog(const QString &directory = {});
|
BlackMisc::CStatusMessage showFileSaveDialog(bool selectedOnly, const QString &directory = {});
|
||||||
|
|
||||||
//! Save file name (optional)
|
//! Save file name (optional)
|
||||||
void setSaveFileName(const QString &saveName) { m_saveFileName = saveName; }
|
void setSaveFileName(const QString &saveName) { m_saveFileName = saveName; }
|
||||||
@@ -448,6 +448,9 @@ namespace BlackGui
|
|||||||
//! Save JSON for action/menu, void return signatur
|
//! Save JSON for action/menu, void return signatur
|
||||||
void saveJsonAction();
|
void saveJsonAction();
|
||||||
|
|
||||||
|
//! Save JSON for action/menu, void return signatur
|
||||||
|
void saveSelectedJsonAction();
|
||||||
|
|
||||||
//! Load JSON for action/menu, void return signatur
|
//! Load JSON for action/menu, void return signatur
|
||||||
void loadJsonAction();
|
void loadJsonAction();
|
||||||
|
|
||||||
@@ -532,7 +535,7 @@ namespace BlackGui
|
|||||||
virtual BlackMisc::CStatusMessage ps_loadJson(const QString &directory = {}) = 0;
|
virtual BlackMisc::CStatusMessage ps_loadJson(const QString &directory = {}) = 0;
|
||||||
|
|
||||||
//! Save JSON
|
//! Save JSON
|
||||||
virtual BlackMisc::CStatusMessage ps_saveJson(const QString &directory = {}) = 0;
|
virtual BlackMisc::CStatusMessage ps_saveJson(bool selectedOnly = false, const QString &directory = {}) = 0;
|
||||||
|
|
||||||
//! Trigger reload from backend by signal requestUpdate();
|
//! Trigger reload from backend by signal requestUpdate();
|
||||||
void ps_triggerReload();
|
void ps_triggerReload();
|
||||||
@@ -683,10 +686,10 @@ namespace BlackGui
|
|||||||
int columnCount() const;
|
int columnCount() const;
|
||||||
|
|
||||||
//! Convert to JSON
|
//! Convert to JSON
|
||||||
QJsonObject toJson() const;
|
QJsonObject toJson(bool selectedOnly = false) const;
|
||||||
|
|
||||||
//! Convert to JSON string
|
//! Convert to JSON string
|
||||||
QString toJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Indented) const;
|
QString toJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Indented, bool selectedOnly = false) const;
|
||||||
|
|
||||||
//! Set own name and the model's name
|
//! Set own name and the model's name
|
||||||
virtual void setObjectName(const QString &name);
|
virtual void setObjectName(const QString &name);
|
||||||
@@ -764,7 +767,7 @@ namespace BlackGui
|
|||||||
virtual void ps_doubleClicked(const QModelIndex &index) override;
|
virtual void ps_doubleClicked(const QModelIndex &index) override;
|
||||||
virtual void ps_rowSelected(const QModelIndex &index) override;
|
virtual void ps_rowSelected(const QModelIndex &index) override;
|
||||||
virtual BlackMisc::CStatusMessage ps_loadJson(const QString &directory = {}) override;
|
virtual BlackMisc::CStatusMessage ps_loadJson(const QString &directory = {}) override;
|
||||||
virtual BlackMisc::CStatusMessage ps_saveJson(const QString &directory = {}) override;
|
virtual BlackMisc::CStatusMessage ps_saveJson(bool selectedOnly = false, const QString &directory = {}) override;
|
||||||
//! @}
|
//! @}
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
Reference in New Issue
Block a user