refs #911, support for cut and paste menu and support in views

This commit is contained in:
Klaus Basan
2017-03-23 03:13:23 +01:00
committed by Mathew Sutcliffe
parent b785e32256
commit 5e69be6208
3 changed files with 95 additions and 5 deletions

View File

@@ -176,6 +176,9 @@ namespace BlackGui
//! View load/save //! View load/save
static const QString &pathViewLoadSave() { static const QString p("View.18.LoadSave"); return p; } static const QString &pathViewLoadSave() { static const QString p("View.18.LoadSave"); return p; }
//! View cut and paste
static const QString &pathViewCutPaste() { static const QString p("View.18.CutPaste"); return p; }
// ---- nested dock widgets ---- // ---- nested dock widgets ----
//! Nested dock widget //! Nested dock widget

View File

@@ -63,7 +63,9 @@
#include "blackmisc/weather/windlayerlist.h" #include "blackmisc/weather/windlayerlist.h"
#include "blackmisc/worker.h" #include "blackmisc/worker.h"
#include <QApplication>
#include <QAction> #include <QAction>
#include <QClipboard>
#include <QDialog> #include <QDialog>
#include <QDragEnterEvent> #include <QDragEnterEvent>
#include <QDragLeaveEvent> #include <QDragLeaveEvent>
@@ -266,13 +268,35 @@ namespace BlackGui
case MenuClear: { ma.addAction(BlackMisc::CIcons::delete16(), "Clear", CMenuAction::pathViewAddRemove(), { this, &CViewBaseNonTemplate::ps_clear }); break; } case MenuClear: { ma.addAction(BlackMisc::CIcons::delete16(), "Clear", CMenuAction::pathViewAddRemove(), { this, &CViewBaseNonTemplate::ps_clear }); break; }
case MenuFilter: case MenuFilter:
{ {
ma.addAction(CIcons::filter16(), "Filter", CMenuAction::pathViewFilter(), { this, &CViewBaseNonTemplate::ps_displayFilterDialog }, CShortcut::keyDisplayFilter()); if (m_filterWidget)
ma.addAction(CIcons::filter16(), "Remove Filter", CMenuAction::pathViewFilter(), { this, &CViewBaseNonTemplate::ps_removeFilter }); {
const bool dialog = qobject_cast<QDialog *>(m_filterWidget);
if (dialog) ma.addAction(CIcons::filter16(), "Show filter", CMenuAction::pathViewFilter(), { this, &CViewBaseNonTemplate::ps_displayFilterDialog }, CShortcut::keyDisplayFilter());
ma.addAction(CIcons::filter16(), "Remove Filter", CMenuAction::pathViewFilter(), { this, &CViewBaseNonTemplate::ps_removeFilter });
}
break; break;
} }
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::ps_loadJsonAction }); break; } case MenuLoad: { ma.addAction(CIcons::disk16(), "Load from file", CMenuAction::pathViewLoadSave(), { this, &CViewBaseNonTemplate::ps_loadJsonAction }); break; }
case MenuSave: { ma.addAction(CIcons::disk16(), "Save data in file", CMenuAction::pathViewLoadSave(), { this, &CViewBaseNonTemplate::ps_saveJsonAction }, CShortcut::keySaveViews()); break; } case MenuSave: { ma.addAction(CIcons::disk16(), "Save data in file", CMenuAction::pathViewLoadSave(), { this, &CViewBaseNonTemplate::ps_saveJsonAction }, CShortcut::keySaveViews()); break; }
case MenuCut:
{
if (!QApplication::clipboard()) break;
ma.addAction(CIcons::cut16(), "Cut", CMenuAction::pathViewCutPaste(), { this, &CViewBaseNonTemplate::ps_cut }, QKeySequence(QKeySequence::Paste));
break;
}
case MenuPaste:
{
if (!QApplication::clipboard()) break;
ma.addAction(CIcons::paste16(), "Paste", CMenuAction::pathViewCutPaste(), { this, &CViewBaseNonTemplate::ps_paste }, QKeySequence(QKeySequence::Paste));
break;
}
case MenuCopy:
{
if (!QApplication::clipboard()) break;
ma.addAction(CIcons::copy16(), "Copy", CMenuAction::pathViewCutPaste(), { this, &CViewBaseNonTemplate::ps_copy }, QKeySequence(QKeySequence::Copy));
break;
}
default: default:
break; break;
} }
@@ -321,7 +345,12 @@ namespace BlackGui
menuActions.addActions(this->initMenuActions(MenuRemoveSelectedRows)); menuActions.addActions(this->initMenuActions(MenuRemoveSelectedRows));
} }
} }
if (this->m_menus.testFlag(MenuFilter))
if (this->m_menus.testFlag(MenuCopy)) { menuActions.addActions(this->initMenuActions(MenuCopy)); }
if (this->m_menus.testFlag(MenuCut)) { menuActions.addActions(this->initMenuActions(MenuCut)); }
if (this->m_menus.testFlag(MenuPaste)) { menuActions.addActions(this->initMenuActions(MenuPaste)); }
if (this->m_menus.testFlag(MenuFilter) && m_filterWidget)
{ {
menuActions.addActions(this->initMenuActions(MenuFilter)); menuActions.addActions(this->initMenuActions(MenuFilter));
if (this->m_menus.testFlag(MenuMaterializeFilter)) if (this->m_menus.testFlag(MenuMaterializeFilter))
@@ -1417,6 +1446,49 @@ namespace BlackGui
} }
} }
template<class ModelClass, class ContainerType, class ObjectType>
void CViewBase<ModelClass, ContainerType, ObjectType>::ps_copy()
{
QClipboard *clipboard = QApplication::clipboard();
if (!clipboard) { return; }
if (!this->hasSelection()) { return; }
const ContainerType selection = this->selectedObjects();
if (selection.isEmpty()) { return; }
const QString json = selection.toJsonString();
clipboard->setText(json);
}
template<class ModelClass, class ContainerType, class ObjectType>
void CViewBase<ModelClass, ContainerType, ObjectType>::ps_cut()
{
if (!QApplication::clipboard()) { return; }
this->ps_copy();
this->removeSelectedRows();
}
template<class ModelClass, class ContainerType, class ObjectType>
void CViewBase<ModelClass, ContainerType, ObjectType>::ps_paste()
{
const QClipboard *clipboard = QApplication::clipboard();
if (!clipboard) { return; }
const QString json = clipboard->text();
if (json.isEmpty()) { return; }
if (!Json::looksLikeSwiftJson(json)) { return; } // no JSON
try
{
ContainerType objects;
objects.convertFromJson(json);
if (!objects.isEmpty())
{
this->insert(objects);
}
}
catch (const CJsonException &ex)
{
Q_UNUSED(ex);
}
}
template <class ModelClass, class ContainerType, class ObjectType> template <class ModelClass, class ContainerType, class ObjectType>
bool CViewBase<ModelClass, ContainerType, ObjectType>::ps_filterDialogFinished(int status) bool CViewBase<ModelClass, ContainerType, ObjectType>::ps_filterDialogFinished(int status)
{ {

View File

@@ -112,6 +112,9 @@ namespace BlackGui
MenuLoad = 1 << 8, //!< load from JSON MenuLoad = 1 << 8, //!< load from JSON
MenuToggleSelectionMode = 1 << 9, //!< allow to toggle selection mode MenuToggleSelectionMode = 1 << 9, //!< allow to toggle selection mode
MenuOrderable = 1 << 10, //!< items can be ordered (if container is BlackMisc::IOrderableList MenuOrderable = 1 << 10, //!< items can be ordered (if container is BlackMisc::IOrderableList
MenuCopy = 1 << 11, //!< copy (for copy/paste)
MenuPaste = 1 << 12, //!< paste (for copy/paste)
MenuCut = 1 << 13, //!< cut (for copy/paste)
MenuStandard = MenuClear | MenuRemoveSelectedRows | MenuRefresh | MenuBackend | MenuStandard = MenuClear | MenuRemoveSelectedRows | MenuRefresh | MenuBackend |
MenuDisplayAutomatically | MenuFilter | MenuSave | MenuLoad | MenuToggleSelectionMode, MenuDisplayAutomatically | MenuFilter | MenuSave | MenuLoad | MenuToggleSelectionMode,
MenuLoadAndSave = MenuLoad | MenuSave, MenuLoadAndSave = MenuLoad | MenuSave,
@@ -120,8 +123,8 @@ namespace BlackGui
MenuDefaultDbViews = MenuToggleSelectionMode | MenuBackend, MenuDefaultDbViews = MenuToggleSelectionMode | MenuBackend,
// special menus, should be in derived classes, but enums cannot be inherited // special menus, should be in derived classes, but enums cannot be inherited
// maybe shifted in the future to elsewhere // maybe shifted in the future to elsewhere
MenuHighlightStashed = 1 << 11, //!< highlight stashed models MenuHighlightStashed = 1 << 14, //!< highlight stashed models
MenuCanStashModels = 1 << 12, //!< stash models MenuCanStashModels = 1 << 15, //!< stash models
MenuStashing = MenuHighlightStashed | MenuCanStashModels, MenuStashing = MenuHighlightStashed | MenuCanStashModels,
}; };
Q_DECLARE_FLAGS(Menu, MenuFlag) Q_DECLARE_FLAGS(Menu, MenuFlag)
@@ -469,6 +472,15 @@ namespace BlackGui
//! Hide load indicator (no parameters) //! Hide load indicator (no parameters)
void ps_hideLoadIndicator(); void ps_hideLoadIndicator();
//! Copy
virtual void ps_copy() = 0;
//! Cut
virtual void ps_cut() = 0;
//! Paste
virtual void ps_paste() = 0;
// ------------ slots of CViewDbObjects ---------------- // ------------ slots of CViewDbObjects ----------------
// need to be declared here and overridden, as this is the only part with valid Q_OBJECT // need to be declared here and overridden, as this is the only part with valid Q_OBJECT
@@ -685,6 +697,9 @@ namespace BlackGui
virtual void ps_rowSelected(const QModelIndex &index) override; virtual void ps_rowSelected(const QModelIndex &index) override;
virtual BlackMisc::CStatusMessage ps_loadJson() override; virtual BlackMisc::CStatusMessage ps_loadJson() override;
virtual BlackMisc::CStatusMessage ps_saveJson() const override; virtual BlackMisc::CStatusMessage ps_saveJson() const override;
virtual void ps_copy() override;
virtual void ps_cut() override;
virtual void ps_paste() override;
//! @} //! @}
}; };
} // namespace } // namespace