"Fossies" - the Fresh Open Source Software Archive

Member "labplot-2.8.2/src/backend/core/aspectcommands.h" (24 Feb 2021, 5417 Bytes) of package /linux/privat/labplot-2.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "aspectcommands.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.8.1_vs_2.8.2.

    1 /***************************************************************************
    2     File                 : aspectcommands.h
    3     Project              : LabPlot
    4     --------------------------------------------------------------------
    5     Copyright            : (C) 2007-2010 by Knut Franke (knut.franke@gmx.de)
    6     Copyright            : (C) 2007-2009 Tilman Benkert(thzs@gmx.net)
    7     Copyright            : (C) 2013-2017 by Alexander Semke (alexander.semke@web.de)
    8     Description          : Undo commands used by AbstractAspect.
    9                            Only meant to be used within AbstractAspect.cpp
   10 
   11  ***************************************************************************/
   12 
   13 /***************************************************************************
   14  *                                                                         *
   15  *  This program is free software; you can redistribute it and/or modify   *
   16  *  it under the terms of the GNU General Public License as published by   *
   17  *  the Free Software Foundation; either version 2 of the License, or      *
   18  *  (at your option) any later version.                                    *
   19  *                                                                         *
   20  *  This program is distributed in the hope that it will be useful,        *
   21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
   22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
   23  *  GNU General Public License for more details.                           *
   24  *                                                                         *
   25  *   You should have received a copy of the GNU General Public License     *
   26  *   along with this program; if not, write to the Free Software           *
   27  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
   28  *   Boston, MA  02110-1301  USA                                           *
   29  *                                                                         *
   30  ***************************************************************************/
   31 #ifndef ASPECTCOMMANDS_H
   32 #define ASPECTCOMMANDS_H
   33 
   34 #include "AspectPrivate.h"
   35 #include <QUndoCommand>
   36 #include <KLocalizedString>
   37 
   38 class AspectChildRemoveCmd : public QUndoCommand {
   39 public:
   40     AspectChildRemoveCmd(AbstractAspectPrivate* target, AbstractAspect* child)
   41         : m_target(target), m_child(child), m_index(-1) {
   42         setText(i18n("%1: remove %2", m_target->m_name, m_child->name()));
   43     }
   44 
   45     ~AspectChildRemoveCmd() override {
   46         //TODO: this makes labplot crashing on project close/save.
   47 //          if (m_removed)
   48 //              delete m_child;
   49     }
   50 
   51     // calling redo transfers ownership of m_child to the undo command
   52     void redo() override {
   53         AbstractAspect* nextSibling;
   54         if (m_child == m_target->m_children.last())
   55             nextSibling = nullptr;
   56         else
   57             nextSibling = m_target->m_children.at(m_target->indexOfChild(m_child) + 1);
   58 
   59         emit m_target->q->aspectAboutToBeRemoved(m_child);
   60 
   61         //emit the "about to be removed" signal also for all children of the aspect being currently removed
   62         for (auto* child : m_child->children<AbstractAspect>(AbstractAspect::ChildIndexFlag::Recursive | AbstractAspect::ChildIndexFlag::IncludeHidden))
   63             m_child->aspectAboutToBeRemoved(child);
   64 
   65         m_index = m_target->removeChild(m_child);
   66         emit m_target->q->aspectRemoved(m_target->q, nextSibling, m_child);
   67 //      m_removed = true;
   68     }
   69 
   70     // calling undo transfers ownership of m_child back to its parent aspect
   71     void undo() override {
   72         Q_ASSERT(m_index != -1); // m_child must be a child of m_target->q
   73 
   74         emit m_target->q->aspectAboutToBeAdded(m_target->q, nullptr, m_child);
   75         m_target->insertChild(m_index, m_child);
   76         emit m_target->q->aspectAdded(m_child);
   77 //      m_removed = false;
   78     }
   79 
   80 protected:
   81     AbstractAspectPrivate* m_target;
   82     AbstractAspect* m_child;
   83     int m_index;
   84 //  bool m_removed{false};
   85 };
   86 
   87 class AspectChildAddCmd : public AspectChildRemoveCmd {
   88 public:
   89     AspectChildAddCmd(AbstractAspectPrivate* target, AbstractAspect* child, int index)
   90         : AspectChildRemoveCmd(target, child) {
   91         setText(i18n("%1: add %2", m_target->m_name, m_child->name()));
   92         m_index = index;
   93 //      m_removed = true;
   94     }
   95 
   96     void redo() override {
   97         AspectChildRemoveCmd::undo();
   98     }
   99 
  100     void undo() override {
  101         AspectChildRemoveCmd::redo();
  102     }
  103 };
  104 
  105 class AspectChildReparentCmd : public QUndoCommand {
  106 public:
  107     AspectChildReparentCmd(AbstractAspectPrivate* target, AbstractAspectPrivate* new_parent,
  108                            AbstractAspect* child, int new_index)
  109         : m_target(target), m_new_parent(new_parent), m_child(child), m_new_index(new_index) {
  110         setText(i18n("%1: move %2 to %3.", m_target->m_name, m_child->name(), m_new_parent->m_name));
  111     }
  112 
  113     // calling redo transfers ownership of m_child to the new parent aspect
  114     void redo() override {
  115         emit m_child->aspectAboutToBeRemoved(m_child);
  116         m_index = m_target->removeChild(m_child);
  117         m_new_parent->insertChild(m_new_index, m_child);
  118         emit m_child->aspectAdded(m_child);
  119     }
  120 
  121     // calling undo transfers ownership of m_child back to its previous parent aspect
  122     void undo() override {
  123         Q_ASSERT(m_index != -1);
  124         emit m_child->aspectAboutToBeRemoved(m_child);
  125         m_new_parent->removeChild(m_child);
  126         m_target->insertChild(m_index, m_child);
  127         emit m_child->aspectAdded(m_child);
  128     }
  129 
  130 protected:
  131     AbstractAspectPrivate * m_target;
  132     AbstractAspectPrivate * m_new_parent;
  133     AbstractAspect* m_child;
  134     int m_index{-1};
  135     int m_new_index;
  136 };
  137 
  138 #endif