QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCFileWatcher.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <QtCore/QFileSystemWatcher>
7#include <QtCore/QObject>
8#include <QtCore/QSet>
9#include <QtCore/QString>
10#include <QtCore/QTimer>
11
12#include <functional>
13
26class QGCFileWatcher : public QObject
27{
28 Q_OBJECT
29
30public:
33 using ChangeCallback = std::function<void(const QString &path)>;
34
37 explicit QGCFileWatcher(QObject *parent = nullptr);
38
39 ~QGCFileWatcher() override;
40
44 void setDebounceDelay(int milliseconds);
45
48 int debounceDelay() const { return _debounceDelay; }
49
50 // ========================================================================
51 // File Watching
52 // ========================================================================
53
58 bool watchFile(const QString &filePath, ChangeCallback callback);
59
63 bool unwatchFile(const QString &filePath);
64
68 bool isWatchingFile(const QString &filePath) const;
69
72 QStringList watchedFiles() const;
73
74 // ========================================================================
75 // Directory Watching
76 // ========================================================================
77
82 bool watchDirectory(const QString &directoryPath, ChangeCallback callback);
83
87 bool unwatchDirectory(const QString &directoryPath);
88
92 bool isWatchingDirectory(const QString &directoryPath) const;
93
96 QStringList watchedDirectories() const;
97
98 // ========================================================================
99 // Bulk Operations
100 // ========================================================================
101
106 int watchFiles(const QStringList &filePaths, ChangeCallback callback);
107
112 int watchDirectories(const QStringList &directoryPaths, ChangeCallback callback);
113
115 void clear();
116
117 // ========================================================================
118 // Convenience Methods
119 // ========================================================================
120
126 bool watchFilePersistent(const QString &filePath, ChangeCallback callback);
127
128signals:
131 void fileChanged(const QString &path);
132
135 void directoryChanged(const QString &path);
136
137private slots:
138 void _onFileChanged(const QString &path);
139 void _onDirectoryChanged(const QString &path);
140 void _processPendingChanges();
141
142private:
143 void _scheduleCallback(const QString &path, bool isDirectory);
144
145 QFileSystemWatcher *_watcher = nullptr;
146
147 // Callbacks per path
148 QHash<QString, ChangeCallback> _fileCallbacks;
149 QHash<QString, ChangeCallback> _directoryCallbacks;
150
151 // Persistent watch tracking
152 QSet<QString> _persistentFiles;
153
154 // Debouncing
155 int _debounceDelay = 100; // milliseconds
156 QTimer *_debounceTimer = nullptr;
157 QSet<QString> _pendingFileChanges;
158 QSet<QString> _pendingDirectoryChanges;
159 bool _processingPendingChanges = false;
160};
Callback-based file/directory watcher with debouncing support.
bool unwatchDirectory(const QString &directoryPath)
bool isWatchingDirectory(const QString &directoryPath) const
void clear()
Stop watching all files and directories.
int watchFiles(const QStringList &filePaths, ChangeCallback callback)
int watchDirectories(const QStringList &directoryPaths, ChangeCallback callback)
~QGCFileWatcher() override
QStringList watchedFiles() const
void setDebounceDelay(int milliseconds)
bool isWatchingFile(const QString &filePath) const
bool watchFilePersistent(const QString &filePath, ChangeCallback callback)
void directoryChanged(const QString &path)
void fileChanged(const QString &path)
int debounceDelay() const
std::function< void(const QString &path)> ChangeCallback
QStringList watchedDirectories() const
bool unwatchFile(const QString &filePath)
bool watchFile(const QString &filePath, ChangeCallback callback)
bool watchDirectory(const QString &directoryPath, ChangeCallback callback)