mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-17 10:55:32 +08:00
Ref T264, Ref T263 simple approach to remember last directory (load/save) in view
* utility function "unwrapCache" to unwrap cache data * fixed loading from file, also supporting memoized formats as well * remember last directory
This commit is contained in:
@@ -309,6 +309,29 @@ namespace BlackGui
|
||||
}
|
||||
}
|
||||
|
||||
void CViewBaseNonTemplate::rememberLastJsonDirectory(const QString &selectedFileOrDir)
|
||||
{
|
||||
if (selectedFileOrDir.isEmpty()) { return; }
|
||||
const QFileInfo fi(selectedFileOrDir);
|
||||
if ((fi.isDir() && fi.exists()) || (fi.isFile() && fi.exists()))
|
||||
{
|
||||
// existing dir
|
||||
m_lastJsonDirectory = fi.absolutePath();
|
||||
}
|
||||
}
|
||||
|
||||
QString CViewBaseNonTemplate::getRememberedLastJsonDirectory() const
|
||||
{
|
||||
if (!m_lastJsonDirectory.isEmpty()) { return m_lastJsonDirectory; }
|
||||
QString dirCandidate = m_dirSettings.get();
|
||||
const QFileInfo fi(dirCandidate);
|
||||
if ((fi.isDir() && fi.exists()) || (fi.isFile() && fi.exists()))
|
||||
{
|
||||
return fi.absolutePath();
|
||||
}
|
||||
return QStringLiteral("");
|
||||
}
|
||||
|
||||
void CViewBaseNonTemplate::customMenu(CMenuActions &menuActions)
|
||||
{
|
||||
// delegate?
|
||||
@@ -530,16 +553,16 @@ namespace BlackGui
|
||||
});
|
||||
}
|
||||
|
||||
QString CViewBaseNonTemplate::getSettingsFileName(bool load) const
|
||||
QString CViewBaseNonTemplate::getFileDialogFileName(bool load) const
|
||||
{
|
||||
// some logic to find a useful default name
|
||||
const QString dir = m_dirSettings.get();
|
||||
if (load)
|
||||
{
|
||||
return CFileUtils::appendFilePaths(dir, CFileUtils::jsonWildcardAppendix());
|
||||
return CFileUtils::appendFilePaths(this->getRememberedLastJsonDirectory(), CFileUtils::jsonWildcardAppendix());
|
||||
}
|
||||
|
||||
// Save file path
|
||||
const QString dir = m_dirSettings.get();
|
||||
QString name(m_saveFileName);
|
||||
if (name.isEmpty())
|
||||
{
|
||||
@@ -818,7 +841,7 @@ namespace BlackGui
|
||||
void CViewBaseNonTemplate::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
if (!event || !this->acceptDrop(event->mimeData())) { return; }
|
||||
setBackgroundRole(QPalette::Highlight);
|
||||
this->setBackgroundRole(QPalette::Highlight);
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
|
||||
@@ -1478,7 +1501,7 @@ namespace BlackGui
|
||||
do
|
||||
{
|
||||
const QString fileName = QFileDialog::getOpenFileName(nullptr,
|
||||
tr("Load data file"), getSettingsFileName(true),
|
||||
tr("Load data file"), this->getFileDialogFileName(true),
|
||||
tr("swift (*.json *.txt)"));
|
||||
if (fileName.isEmpty())
|
||||
{
|
||||
@@ -1497,16 +1520,26 @@ namespace BlackGui
|
||||
m = CStatusMessage(this).warning("No swift JSON '%1'") << fileName;
|
||||
break;
|
||||
}
|
||||
|
||||
this->rememberLastJsonDirectory(fileName);
|
||||
|
||||
try
|
||||
{
|
||||
CVariant containerVariant;
|
||||
containerVariant.convertFromJson(Json::jsonObjectFromString(json));
|
||||
if (!containerVariant.canConvert<ContainerType>())
|
||||
const bool allowCacheFormat = this->allowCacheFileFormatJson();
|
||||
const QJsonObject jsonObject = Json::jsonObjectFromString(json, allowCacheFormat);
|
||||
if (jsonObject.isEmpty())
|
||||
{
|
||||
m = CStatusMessage(this).warning("No valid swift JSON '%1'") << fileName;
|
||||
break;
|
||||
}
|
||||
|
||||
CVariant containerVariant;
|
||||
containerVariant.convertFromJson(jsonObject);
|
||||
if (!containerVariant.canConvert<ContainerType>())
|
||||
{
|
||||
m = CStatusMessage(this).warning("No valid swift JSON '%1'") << fileName;
|
||||
break;
|
||||
}
|
||||
ContainerType container = containerVariant.value<ContainerType>();
|
||||
const int countBefore = container.size();
|
||||
m = this->modifyLoadedJsonData(container);
|
||||
@@ -1534,7 +1567,7 @@ namespace BlackGui
|
||||
CStatusMessage CViewBase<ModelClass, ContainerType, ObjectType>::ps_saveJson()
|
||||
{
|
||||
const QString fileName = QFileDialog::getSaveFileName(nullptr,
|
||||
tr("Save data file"), getSettingsFileName(false),
|
||||
tr("Save data file"), getFileDialogFileName(false),
|
||||
tr("swift (*.json *.txt)"));
|
||||
if (fileName.isEmpty()) { return CStatusMessage(this, CStatusMessage::SeverityDebug, "Save canceled", true); }
|
||||
const QString json(this->toJsonString()); // save as CVariant JSON
|
||||
|
||||
@@ -277,6 +277,12 @@ namespace BlackGui
|
||||
//! Save file name (optional)
|
||||
void setSaveFileName(const QString &saveName) { m_saveFileName = saveName; }
|
||||
|
||||
//! Allow cache file JSON to be loaded
|
||||
bool allowCacheFileFormatJson() const { return m_allowCacheFileJson; }
|
||||
|
||||
//! Enable/disable cache file format to be loaded as JSON
|
||||
void setAllowCacheFileFormatJson(bool allow) { m_allowCacheFileJson = allow; }
|
||||
|
||||
//! Force that columns are extended to full viewport width.
|
||||
//! Workaround as of https://stackoverflow.com/q/3433664/356726
|
||||
void setForceColumnsToMaxSize(bool force) { m_forceColumnsToMaxSize = force; }
|
||||
@@ -415,7 +421,7 @@ namespace BlackGui
|
||||
void init();
|
||||
|
||||
//! Default file for load/save operations
|
||||
QString getSettingsFileName(bool load) const;
|
||||
QString getFileDialogFileName(bool load) const;
|
||||
|
||||
//! Init menu actions
|
||||
Menus::CMenuActions initMenuActions(MenuFlag menu);
|
||||
@@ -438,7 +444,14 @@ namespace BlackGui
|
||||
//! Settings have been changed
|
||||
void settingsChanged();
|
||||
|
||||
QString m_saveFileName; //!< save file name (JSON)
|
||||
//! JSON directory
|
||||
//! \remark Default implementation, can be overridden with specifi implementation
|
||||
virtual void rememberLastJsonDirectory(const QString &selectedFileOrDir);
|
||||
|
||||
//! JSON directory
|
||||
//! \remark Default implementation, can be overridden with specifi implementation
|
||||
virtual QString getRememberedLastJsonDirectory() const;
|
||||
|
||||
ResizeMode m_resizeMode = PresizeSubset; //!< mode
|
||||
RowsResizeMode m_rowResizeMode = Interactive; //!< row resize mode for row height
|
||||
SelectionMode m_originalSelectionMode = this->selectionMode(); //!< Selection mode set
|
||||
@@ -456,14 +469,17 @@ namespace BlackGui
|
||||
bool m_enableDeleteSelectedRows = false; //!< selected rows can be deleted
|
||||
bool m_dropIndicator = false; //!< draw indicator
|
||||
bool m_forceColumnsToMaxSize = true; //!< force that columns are extended to full viewport width
|
||||
bool m_allowCacheFileJson = true; //!< allow Cache format JSON to be loaded
|
||||
QWidget *m_filterWidget = nullptr; //!< filter widget or dialog
|
||||
Menu m_menus = MenuDefault; //!< Default menu settings
|
||||
Menus::IMenuDelegate *m_menu = nullptr; //!< custom menu if any
|
||||
Menus::CFontMenu *m_fontMenu = nullptr; //!< font menu if applicable
|
||||
CLoadIndicator *m_loadIndicator = nullptr; //!< load indicator if needed
|
||||
QMap<MenuFlag, Menus::CMenuActions> m_menuFlagActions; //!< initialized actions
|
||||
BlackMisc::CSettingReadOnly<Settings::TGeneralGui> m_guiSettings { this, &CViewBaseNonTemplate::settingsChanged }; //!< general GUI settings
|
||||
QString m_saveFileName; //!< save file name (JSON)
|
||||
QString m_lastJsonDirectory; //!< remember last JSON directory
|
||||
BlackMisc::CSetting<Settings::TViewDirectorySettings> m_dirSettings { this }; //!< directory for load/save
|
||||
BlackMisc::CSettingReadOnly<Settings::TGeneralGui> m_guiSettings { this, &CViewBaseNonTemplate::settingsChanged }; //!< general GUI settings
|
||||
|
||||
protected slots:
|
||||
//! Helper method with template free signature serving as callback from threaded worker
|
||||
|
||||
Reference in New Issue
Block a user