XPlor/app/dirtystatemanager.h

93 lines
2.4 KiB
C
Raw Normal View History

#ifndef DIRTYSTATEMANAGER_H
#define DIRTYSTATEMANAGER_H
#include <QObject>
#include <QSet>
#include <QMap>
class QWidget;
/**
* @brief Tracks dirty (unsaved changes) state for editor tabs.
*
* The DirtyStateManager is a singleton that tracks which editor widgets
* have unsaved changes. It emits signals when dirty state changes, allowing
* the UI to update tab titles (add/remove asterisk) and prompt on close.
*/
class DirtyStateManager : public QObject {
Q_OBJECT
public:
static DirtyStateManager& instance();
/**
* @brief Mark a tab as having unsaved changes.
* @param tab The widget to mark as dirty
*/
void markDirty(QWidget* tab);
/**
* @brief Mark a tab as having no unsaved changes.
* @param tab The widget to mark as clean
*/
void markClean(QWidget* tab);
/**
* @brief Check if a tab has unsaved changes.
* @param tab The widget to check
* @return true if the tab has unsaved changes
*/
bool isDirty(QWidget* tab) const;
/**
* @brief Get list of all tabs with unsaved changes.
* @return List of dirty widgets
*/
QList<QWidget*> dirtyTabs() const;
/**
* @brief Check if any tabs have unsaved changes.
* @return true if at least one tab is dirty
*/
bool hasDirtyTabs() const;
/**
* @brief Store the original file path for a tab (for Save functionality).
* @param tab The widget
* @param path The file path
*/
void setFilePath(QWidget* tab, const QString& path);
/**
* @brief Get the original file path for a tab.
* @param tab The widget
* @return The file path, or empty string if not set
*/
QString filePath(QWidget* tab) const;
/**
* @brief Remove tracking for a tab (call when tab is closed).
* @param tab The widget being closed
*/
void removeTab(QWidget* tab);
signals:
/**
* @brief Emitted when a tab's dirty state changes.
* @param tab The affected widget
* @param isDirty The new dirty state
*/
void dirtyStateChanged(QWidget* tab, bool isDirty);
private:
DirtyStateManager() = default;
~DirtyStateManager() = default;
DirtyStateManager(const DirtyStateManager&) = delete;
DirtyStateManager& operator=(const DirtyStateManager&) = delete;
QSet<QWidget*> m_dirtyTabs;
QMap<QWidget*, QString> m_filePaths;
};
#endif // DIRTYSTATEMANAGER_H