Commit c39242c1 authored by Jochen Becher's avatar Jochen Becher
Browse files

ModelEditor: Support editor's navigation history



For now only the id of the currently opened diagram is stored in the
navigation state. The position of scrollers is not saved.

Change-Id: Ic9b2237a0223596d1111c635f04f11e3951fd397
Reviewed-by: default avatarTobias Hunger <tobias.hunger@theqtcompany.com>
parent 963e3b4f
......@@ -83,8 +83,21 @@ inline bool operator!=(const Uid &lhs, const Uid &rhs)
return !operator==(lhs, rhs);
}
inline QDataStream &operator<<(QDataStream &stream, const Uid &uid)
{
return stream << uid.get();
}
inline QDataStream &operator>>(QDataStream &stream, Uid &uid)
{
QUuid uuid;
stream >> uuid;
uid.setUuid(uuid);
return stream;
}
}
Q_DECLARE_METATYPE(qmt::Uid);
Q_DECLARE_METATYPE(qmt::Uid)
#endif // QMT_UID_H
......@@ -136,7 +136,7 @@ ModelEditor::ModelEditor(UiController *uiController, ActionHandler *actionHandle
ModelEditor::~ModelEditor()
{
closeCurrentDiagram();
closeCurrentDiagram(false);
delete d->toolbar;
delete d;
}
......@@ -151,6 +151,30 @@ QWidget *ModelEditor::toolBar()
return d->toolbar;
}
QByteArray ModelEditor::saveState() const
{
return saveState(currentDiagram());
}
bool ModelEditor::restoreState(const QByteArray &state)
{
QDataStream stream(state);
int version = 0;
stream >> version;
if (version == 1) {
qmt::Uid uid;
stream >> uid;
if (uid.isValid()) {
qmt::MDiagram *diagram = d->document->documentController()->getModelController()->findObject<qmt::MDiagram>(uid);
if (diagram) {
openDiagram(diagram, false);
return true;
}
}
}
return false;
}
void ModelEditor::init(QWidget *parent)
{
// create and configure properties view
......@@ -361,14 +385,7 @@ qmt::MDiagram *ModelEditor::currentDiagram() const
void ModelEditor::showDiagram(qmt::MDiagram *diagram)
{
closeCurrentDiagram();
if (diagram) {
qmt::DiagramSceneModel *diagramSceneModel = d->document->documentController()->getDiagramsManager()->bindDiagramSceneModel(diagram);
d->diagramView->setDiagramSceneModel(diagramSceneModel);
d->diagramStack->setCurrentWidget(d->diagramView);
updateSelectedArea(SelectedArea::Nothing);
addDiagramToSelector(diagram);
}
openDiagram(diagram, true);
}
void ModelEditor::undo()
......@@ -945,7 +962,19 @@ void ModelEditor::initToolbars()
d->leftToolBox->setCurrentIndex(0);
}
void ModelEditor::closeCurrentDiagram()
void ModelEditor::openDiagram(qmt::MDiagram *diagram, bool addToHistory)
{
closeCurrentDiagram(addToHistory);
if (diagram) {
qmt::DiagramSceneModel *diagramSceneModel = d->document->documentController()->getDiagramsManager()->bindDiagramSceneModel(diagram);
d->diagramView->setDiagramSceneModel(diagramSceneModel);
d->diagramStack->setCurrentWidget(d->diagramView);
updateSelectedArea(SelectedArea::Nothing);
addDiagramToSelector(diagram);
}
}
void ModelEditor::closeCurrentDiagram(bool addToHistory)
{
ExtDocumentController *documentController = d->document->documentController();
qmt::DiagramsManager *diagramsManager = documentController->getDiagramsManager();
......@@ -953,6 +982,8 @@ void ModelEditor::closeCurrentDiagram()
if (sceneModel) {
qmt::MDiagram *diagram = sceneModel->getDiagram();
if (diagram) {
if (addToHistory)
addToNavigationHistory(diagram);
d->diagramStack->setCurrentWidget(d->noDiagramLabel);
d->diagramView->setDiagramSceneModel(0);
diagramsManager->unbindDiagramSceneModel(diagram);
......@@ -966,6 +997,7 @@ void ModelEditor::closeDiagram(const qmt::MDiagram *diagram)
qmt::DiagramsManager *diagramsManager = documentController->getDiagramsManager();
qmt::DiagramSceneModel *sceneModel = d->diagramView->getDiagramSceneModel();
if (sceneModel && diagram == sceneModel->getDiagram()) {
addToNavigationHistory(diagram);
d->diagramStack->setCurrentWidget(d->noDiagramLabel);
d->diagramView->setDiagramSceneModel(0);
diagramsManager->unbindDiagramSceneModel(diagram);
......@@ -974,7 +1006,7 @@ void ModelEditor::closeDiagram(const qmt::MDiagram *diagram)
void ModelEditor::closeAllDiagrams()
{
closeCurrentDiagram();
closeCurrentDiagram(true);
}
void ModelEditor::onContentSet()
......@@ -1059,6 +1091,25 @@ QString ModelEditor::buildDiagramLabel(const qmt::MDiagram *diagram)
return label;
}
void ModelEditor::addToNavigationHistory(const qmt::MDiagram *diagram)
{
if (Core::EditorManager::currentEditor() == this)
Core::EditorManager::cutForwardNavigationHistory();
Core::EditorManager::addCurrentPositionToNavigationHistory(saveState(diagram));
}
QByteArray ModelEditor::saveState(const qmt::MDiagram *diagram) const
{
QByteArray state;
QDataStream stream(&state, QIODevice::WriteOnly);
stream << 1; // version number
if (diagram)
stream << diagram->getUid();
else
stream << qmt::Uid::getInvalidUid();
return state;
}
void ModelEditor::onEditSelectedElement()
{
// TODO introduce similar method for selected elements in model tree
......
......@@ -75,6 +75,8 @@ public:
Core::IDocument *document() override;
QWidget *toolBar() override;
QByteArray saveState() const override;
bool restoreState(const QByteArray &state) override;
qmt::MDiagram *currentDiagram() const;
void showDiagram(qmt::MDiagram *diagram);
......@@ -127,7 +129,8 @@ private:
void onRightHorizSplitterChanged(const QByteArray &state);
void initToolbars();
void closeCurrentDiagram();
void openDiagram(qmt::MDiagram *diagram, bool addToHistory);
void closeCurrentDiagram(bool addToHistory);
void closeDiagram(const qmt::MDiagram *diagram);
void closeAllDiagrams();
......@@ -138,6 +141,9 @@ private:
void onDiagramSelectorSelected(int index);
QString buildDiagramLabel(const qmt::MDiagram *diagram);
void addToNavigationHistory(const qmt::MDiagram *diagram);
QByteArray saveState(const qmt::MDiagram *diagram) const;
private slots:
void onEditSelectedElement();
......
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