Some checks failed
Build and Test / build (push) Has been cancelled
- Add originForDocument(), setDocumentOrigin(), clearDocumentOrigin() methods to OriginManager - Add signalDocumentOriginChanged signal for UI updates - Add document-to-origin tracking map in OriginManager - Update MDIView::buildWindowTitle() to append origin suffix for non-local origins (e.g., 'Part001 [Silo]') This implements Issue #16: Document origin tracking and display
185 lines
6.0 KiB
C++
185 lines
6.0 KiB
C++
/***************************************************************************
|
|
* Copyright (c) 2025 Kindred Systems *
|
|
* *
|
|
* This file is part of the FreeCAD CAx development system. *
|
|
* *
|
|
* This library is free software; you can redistribute it and/or *
|
|
* modify it under the terms of the GNU Library General Public *
|
|
* License as published by the Free Software Foundation; either *
|
|
* version 2 of the License, or (at your option) any later version. *
|
|
* *
|
|
* This library 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 Library General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU Library General Public *
|
|
* License along with this library; see the file COPYING.LIB. If not, *
|
|
* write to the Free Software Foundation, Inc., 59 Temple Place, *
|
|
* Suite 330, Boston, MA 02111-1307, USA *
|
|
* *
|
|
***************************************************************************/
|
|
|
|
#ifndef GUI_ORIGINMANAGER_H
|
|
#define GUI_ORIGINMANAGER_H
|
|
|
|
#include <map>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <FCGlobal.h>
|
|
#include <fastsignals/signal.h>
|
|
|
|
namespace App {
|
|
class Document;
|
|
}
|
|
|
|
namespace Gui {
|
|
|
|
class FileOrigin;
|
|
|
|
/**
|
|
* @brief Singleton manager for document origins
|
|
*
|
|
* OriginManager tracks all registered FileOrigin instances and maintains
|
|
* the current origin selection. It provides lookup methods and signals
|
|
* for UI updates.
|
|
*
|
|
* The manager always has at least one origin: the local filesystem origin,
|
|
* which is created automatically on initialization.
|
|
*/
|
|
class GuiExport OriginManager
|
|
{
|
|
public:
|
|
/** Get the singleton instance */
|
|
static OriginManager* instance();
|
|
/** Destroy the singleton instance */
|
|
static void destruct();
|
|
|
|
///@name Origin Registration
|
|
//@{
|
|
/**
|
|
* Register an origin with the manager.
|
|
* The manager takes ownership of the origin.
|
|
* @param origin The origin to register
|
|
* @return true if successfully registered (ID not already in use)
|
|
*/
|
|
bool registerOrigin(FileOrigin* origin);
|
|
|
|
/**
|
|
* Unregister and delete an origin.
|
|
* Cannot unregister the built-in "local" origin.
|
|
* @param id The origin ID to unregister
|
|
* @return true if successfully unregistered
|
|
*/
|
|
bool unregisterOrigin(const std::string& id);
|
|
|
|
/**
|
|
* Get all registered origin IDs.
|
|
* @return Vector of origin ID strings
|
|
*/
|
|
std::vector<std::string> originIds() const;
|
|
|
|
/**
|
|
* Get origin by ID.
|
|
* @param id The origin ID
|
|
* @return The origin or nullptr if not found
|
|
*/
|
|
FileOrigin* getOrigin(const std::string& id) const;
|
|
//@}
|
|
|
|
///@name Current Origin Selection
|
|
//@{
|
|
/**
|
|
* Get the currently selected origin.
|
|
* @return The current origin (never nullptr)
|
|
*/
|
|
FileOrigin* currentOrigin() const;
|
|
|
|
/**
|
|
* Get the current origin ID.
|
|
* @return The current origin's ID
|
|
*/
|
|
std::string currentOriginId() const;
|
|
|
|
/**
|
|
* Set the current origin by ID.
|
|
* @param id The origin ID to select
|
|
* @return true if origin was found and selected
|
|
*/
|
|
bool setCurrentOrigin(const std::string& id);
|
|
//@}
|
|
|
|
///@name Document Origin Resolution
|
|
//@{
|
|
/**
|
|
* Find which origin owns a document.
|
|
* Iterates through all origins to find one that claims ownership
|
|
* based on document properties.
|
|
* @param doc The document to check
|
|
* @return The owning origin or nullptr if unowned
|
|
*/
|
|
FileOrigin* findOwningOrigin(App::Document* doc) const;
|
|
|
|
/**
|
|
* Get the origin associated with a document.
|
|
* First checks explicit association, then uses findOwningOrigin().
|
|
* @param doc The document to check
|
|
* @return The document's origin or nullptr if unknown
|
|
*/
|
|
FileOrigin* originForDocument(App::Document* doc) const;
|
|
|
|
/**
|
|
* Associate a document with an origin.
|
|
* @param doc The document
|
|
* @param origin The origin to associate (nullptr to clear)
|
|
*/
|
|
void setDocumentOrigin(App::Document* doc, FileOrigin* origin);
|
|
|
|
/**
|
|
* Clear document origin association (called when document closes).
|
|
* @param doc The document being closed
|
|
*/
|
|
void clearDocumentOrigin(App::Document* doc);
|
|
|
|
/**
|
|
* Get the appropriate origin for a new document.
|
|
* Returns the current origin.
|
|
* @return The origin to use for new documents
|
|
*/
|
|
FileOrigin* originForNewDocument() const;
|
|
//@}
|
|
|
|
///@name Signals
|
|
//@{
|
|
/** Emitted when an origin is registered */
|
|
fastsignals::signal<void(const std::string&)> signalOriginRegistered;
|
|
/** Emitted when an origin is unregistered */
|
|
fastsignals::signal<void(const std::string&)> signalOriginUnregistered;
|
|
/** Emitted when current origin changes */
|
|
fastsignals::signal<void(const std::string&)> signalCurrentOriginChanged;
|
|
/** Emitted when a document's origin association changes */
|
|
fastsignals::signal<void(App::Document*, const std::string&)> signalDocumentOriginChanged;
|
|
//@}
|
|
|
|
protected:
|
|
OriginManager();
|
|
~OriginManager();
|
|
|
|
private:
|
|
void loadPreferences();
|
|
void savePreferences();
|
|
void ensureLocalOrigin();
|
|
|
|
static OriginManager* _instance;
|
|
std::map<std::string, std::unique_ptr<FileOrigin>> _origins;
|
|
std::string _currentOriginId;
|
|
|
|
// Document-to-origin associations (doc -> origin ID)
|
|
mutable std::map<App::Document*, std::string> _documentOrigins;
|
|
};
|
|
|
|
} // namespace Gui
|
|
|
|
#endif // GUI_ORIGINMANAGER_H
|