diff --git a/src/edt/edt/edt.pro b/src/edt/edt/edt.pro index 477e14ff4..97ce17c08 100644 --- a/src/edt/edt/edt.pro +++ b/src/edt/edt/edt.pro @@ -36,6 +36,7 @@ DEFINES += MAKE_EDT_LIBRARY HEADERS = \ edtDialogs.h \ + edtEditorHooks.h \ edtEditorOptionsPages.h \ edtInstPropertiesPage.h \ edtPCellParametersPage.h \ @@ -45,6 +46,7 @@ HEADERS = \ SOURCES = \ edtDialogs.cc \ + edtEditorHooks.cc \ edtEditorOptionsPages.cc \ edtInstPropertiesPage.cc \ edtPCellParametersPage.cc \ diff --git a/src/edt/edt/edtEditorHooks.cc b/src/edt/edt/edtEditorHooks.cc new file mode 100644 index 000000000..b369c5950 --- /dev/null +++ b/src/edt/edt/edtEditorHooks.cc @@ -0,0 +1,133 @@ + +/* + + KLayout Layout Viewer + Copyright (C) 2006-2024 Matthias Koefferlein + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + + +#include "edtEditorHooks.h" +#include "tlObjectCollection.h" + +namespace edt +{ + +// --------------------------------------------------------------- +// EditorHooksManager definition and implementation + +class EditorHooksManager +{ +public: + EditorHooksManager () { } + + static EditorHooksManager *instance () + { + static EditorHooksManager *sp_instance = 0; + if (! sp_instance) { + sp_instance = new EditorHooksManager (); + } + return sp_instance; + } + + void register_editor_hook (EditorHooks *hook) + { + m_hooks.push_back (hook); + } + + std::vector get_editor_hooks (const std::string &for_technology) + { + std::vector res; + for (auto h = m_hooks.begin (); h != m_hooks.end (); ++h) { + if (! h->for_technologies () || h->is_for_technology (for_technology)) { + res.push_back (h.operator-> ()); + } + } + + return res; + } + +private: + tl::shared_collection m_hooks; +}; + +// --------------------------------------------------------------- +// EditorHooks implementation + +EditorHooks::EditorHooks () +{ + // .. nothing yet .. +} + +EditorHooks::~EditorHooks () +{ + // .. nothing yet .. +} + +bool +EditorHooks::is_for_technology (const std::string &name) const +{ + return m_technologies.find (name) != m_technologies.end (); +} + +bool +EditorHooks::for_technologies () const +{ + return ! m_technologies.empty (); +} + +void +EditorHooks::set_technology (const std::string &t) +{ + m_technologies.clear (); + if (! t.empty ()) { + m_technologies.insert (t); + } +} + +void +EditorHooks::clear_technologies () +{ + m_technologies.clear (); +} + +void +EditorHooks::add_technology (const std::string &tech) +{ + m_technologies.insert (tech); +} + +void +EditorHooks::register_editor_hook (EditorHooks *hook) +{ + if (EditorHooksManager::instance ()) { + hook->keep (); + EditorHooksManager::instance ()->register_editor_hook (hook); + } +} + +std::vector +EditorHooks::get_editor_hooks (const std::string &for_technology) +{ + if (EditorHooksManager::instance ()) { + return EditorHooksManager::instance ()->get_editor_hooks (for_technology); + } else { + return std::vector (); + } +} + +} diff --git a/src/edt/edt/edtEditorHooks.h b/src/edt/edt/edtEditorHooks.h new file mode 100644 index 000000000..4f93986cc --- /dev/null +++ b/src/edt/edt/edtEditorHooks.h @@ -0,0 +1,167 @@ + +/* + + KLayout Layout Viewer + Copyright (C) 2006-2024 Matthias Koefferlein + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + + + +#ifndef HDR_edtEditorHooks +#define HDR_edtEditorHooks + +#include "edtCommon.h" +#include "dbTrans.h" +#include "gsiObject.h" + +#include +#include + +namespace lay +{ + class LayoutView; + class ObjectInstPath; +} + +namespace edt +{ + +/** + * @ brief The editor hooks handler object + * + * Editor hooks are a way to hook into the editor feature - for example + * to implement dynamic DRC or display hints. + * + * The protocols are: + * + * 1. Object Creation + * + * begin_create { begin_new_objects { create } end_new_objects } [ commit_create ] end_create + * + * 2. Modification (i.e. partial edit) + * + * begin_modify { begin_modifications { modified } end_modifications } [ commit_modify ] end_modify + * + * 3. Interactive edit (move, transform, interactive clone) + * + * begin_edit { begin_edits { transformed } end_edits } [ commit_edit ] end_edit + * + * Notation: { ... } means the sequence can be repeated, [ ... ] means the call is optional. + */ + +class EDT_PUBLIC EditorHooks + : public gsi::ObjectBase, public tl::Object +{ +public: + /** + * @brief Constructor + */ + EditorHooks (); + + /** + * @brief Destructor + */ + virtual ~EditorHooks (); + + // creation protocol + virtual void begin_create (lay::LayoutView * /*view*/) { } + virtual void begin_new_objects () { } + virtual void create (const lay::ObjectInstPath & /*object*/, double /*dbu*/) { } + virtual void end_new_objects () { } + virtual void commit_create () { } + virtual void end_create () { } + + // modification protocol + virtual void begin_modify (lay::LayoutView * /*view*/) { } + virtual void begin_modifications () { } + virtual void modified (const lay::ObjectInstPath & /*object*/, double /*dbu*/) { } + virtual void end_modifications () { } + virtual void commit_modfiy () { } + virtual void end_modify () { } + + // editing protocol + virtual void begin_edit (lay::LayoutView * /*view*/) { } + virtual void begin_edits () { } + virtual void transformed (const lay::ObjectInstPath & /*object*/, const db::DCplxTrans & /*trans*/, double /*dbu*/) { } + virtual void end_edits () { } + virtual void commit_edit () { } + virtual void end_edit () { } + + /** + * @brief Gets the technology name this hook is associated with + * + * If this attribute is non-empty, the hook is selected only when the given technology is + * used for the layout. + */ + const std::set &get_technologies () const + { + return m_technologies; + } + + /** + * @brief Gets a value indicating whether this hook is associated with the given technology + */ + bool is_for_technology (const std::string &name) const; + + /** + * @brief Gets a value indicating whether the hook is associated with any technology + */ + bool for_technologies () const; + + /** + * @brief Sets the technology name this hook is associated with + * + * This will reset the list of technologies to this one. + * If the given technology string is empty, the list of technologies will be cleared. + */ + void set_technology (const std::string &t); + + /** + * @brief Clears the list of technologies this hook is associated with + */ + void clear_technologies (); + + /** + * @brief Additionally associate the hook with the given technology + */ + void add_technology (const std::string &tech); + + /** + * @brief Registers the editor hook + */ + static void register_editor_hook (EditorHooks *hook); + + /** + * @brief Gets the editor hooks for a given technology + * + * The order of the hooks is determined by the registration order. + */ + static std::vector get_editor_hooks (const std::string &for_technology); + +private: + std::set m_technologies; + + // no copying. + EditorHooks &operator= (const EditorHooks &); + EditorHooks (const EditorHooks &); +}; + +} + +#endif +