| 1 |
/**************************************************************************** |
| 2 |
* Copyright (c) 2023 Zheng Lei (realthunder) <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 7 |
* under the terms of the GNU Lesser General Public License as * |
| 8 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 9 |
* License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with FreeCAD. If not, see * |
| 18 |
* <https://www.gnu.org/licenses/>. * |
| 19 |
* * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
|
| 23 |
#include "ParamHandler.h" |
| 24 |
|
| 25 |
using namespace Gui; |
| 26 |
|
| 27 |
ParamHandlers::ParamHandlers() |
| 28 |
{} |
| 29 |
|
| 30 |
ParamHandlers::~ParamHandlers() |
| 31 |
{} |
| 32 |
|
| 33 |
void ParamHandlers::addHandler(const ParamKey& key, const std::shared_ptr<ParamHandler>& handler) |
| 34 |
{ |
| 35 |
if (handlers.empty()) { |
| 36 |
conn = App::GetApplication().GetUserParameter().signalParamChanged.connect( |
| 37 |
[this](ParameterGrp* Param, ParameterGrp::ParamType, const char* Name, const char*) { |
| 38 |
if (!Param || !Name) { |
| 39 |
return; |
| 40 |
} |
| 41 |
auto it = handlers.find(ParamKey(Param, Name)); |
| 42 |
if (it != handlers.end() && it->second->onChange(&it->first)) { |
| 43 |
pendings.insert(it->second); |
| 44 |
timer.start(100); |
| 45 |
} |
| 46 |
} |
| 47 |
); |
| 48 |
|
| 49 |
timer.setSingleShot(true); |
| 50 |
QObject::connect(&timer, &QTimer::timeout, [this]() { |
| 51 |
for (const auto& v : pendings) { |
| 52 |
v->onTimer(); |
| 53 |
} |
| 54 |
pendings.clear(); |
| 55 |
}); |
| 56 |
} |
| 57 |
handlers[key] = handler; |
| 58 |
} |