git-svn-id: svn://db.shs.com.ru/libs@159 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
196
qcd_utils/qcd_core.cpp
Normal file
196
qcd_utils/qcd_core.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
#include "qcd_core.h"
|
||||
#include "cdutils_core.h"
|
||||
#include "piqt.h"
|
||||
#include <QWidget>
|
||||
#include <QCheckBox>
|
||||
#include <QGroupBox>
|
||||
#include <QSpinBox>
|
||||
#include <QSlider>
|
||||
#include <QScrollBar>
|
||||
#include <QDoubleSpinBox>
|
||||
#include <QLineEdit>
|
||||
#include <spinslider.h>
|
||||
#include <clineedit.h>
|
||||
|
||||
using namespace CDUtils;
|
||||
|
||||
|
||||
int __QCore_Initializer__::count_(0);
|
||||
QCDCore * __QCore_Initializer__::__instance__(0);
|
||||
|
||||
|
||||
__QCore_Initializer__::__QCore_Initializer__() {
|
||||
count_++;
|
||||
if (count_ > 1) return;
|
||||
__instance__ = new QCDCore();
|
||||
}
|
||||
|
||||
|
||||
__QCore_Initializer__::~__QCore_Initializer__() {
|
||||
count_--;
|
||||
if (count_ > 0) return;
|
||||
if (__instance__ != 0) {
|
||||
delete __instance__;
|
||||
__instance__ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
QCDCore::QCDCore() {
|
||||
setObjectName("QCDCore");
|
||||
setName("QCDCore");
|
||||
CONNECTU(CDCore::instance(), K_ChangedGlobal, this, K_ChangedGlobal);
|
||||
updating = false;
|
||||
}
|
||||
|
||||
|
||||
QCDCore::~QCDCore() {
|
||||
}
|
||||
|
||||
|
||||
void QCDCore::K_ChangedGlobal() {
|
||||
QMetaObject::invokeMethod(this, "updateBindedWidgets", Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
|
||||
void QCDCore::slotBool(bool v) {
|
||||
QWidget * w = (QWidget*)sender();
|
||||
if (!w || updating) return;
|
||||
PIDeque<int> path = binded_widgets.value(w);
|
||||
CDCore::instance()->k()[path].setValue(PIString::fromBool(v));
|
||||
}
|
||||
|
||||
|
||||
void QCDCore::slotInt(int v) {
|
||||
QWidget * w = (QWidget*)sender();
|
||||
if (!w || updating) return;
|
||||
PIDeque<int> path = binded_widgets.value(w);
|
||||
CDCore::instance()->k()[path].setValue(PIString::fromNumber(v));
|
||||
}
|
||||
|
||||
|
||||
void QCDCore::slotDouble(double v) {
|
||||
QWidget * w = (QWidget*)sender();
|
||||
if (!w || updating) return;
|
||||
PIDeque<int> path = binded_widgets.value(w);
|
||||
CDCore::instance()->k()[path].setValue(PIString::fromNumber(v));
|
||||
}
|
||||
|
||||
|
||||
void QCDCore::slotText(QString v) {
|
||||
QWidget * w = (QWidget*)sender();
|
||||
if (!w || updating) return;
|
||||
PIDeque<int> path = binded_widgets.value(w);
|
||||
CDCore::instance()->k()[path].setValue(Q2PIString(v));
|
||||
}
|
||||
|
||||
|
||||
int QCDCore::bindWindow(QWidget * wnd) {
|
||||
if (!wnd) return 0;
|
||||
CDCore::instance()->k().makePath();
|
||||
return bindWidgets(wnd->findChildren<QWidget * >());
|
||||
}
|
||||
|
||||
|
||||
int QCDCore::bindWidgets(QList<QWidget * > wl) {
|
||||
int ret = 0;
|
||||
foreach (QWidget * w, wl)
|
||||
if (bindWidget(w)) ++ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
bool QCDCore::bindWidget(QWidget * w) {
|
||||
if (!w) return false;
|
||||
QString on = w->objectName();
|
||||
PIVector<CDType * > ak = CDCore::instance()->k().children();
|
||||
piForeachC (CDType * k, ak) {
|
||||
if (!on.endsWith(PI2QString(k->name()))) continue;
|
||||
QString cn = w->metaObject()->className();
|
||||
bool ok = false;
|
||||
if (cn == "QCheckBox" || cn == "QGroupBox") {
|
||||
connect(w, SIGNAL(toggled(bool)), this, SLOT(slotBool(bool)), Qt::UniqueConnection);
|
||||
ok = true;
|
||||
}
|
||||
if (cn == "QSpinBox" || cn == "QSlider" || cn == "QScrollBar") {
|
||||
connect(w, SIGNAL(valueChanged(int)), this, SLOT(slotInt(int)), Qt::UniqueConnection);
|
||||
ok = true;
|
||||
}
|
||||
if (cn == "QDoubleSpinBox" || cn == "SpinSlider") {
|
||||
connect(w, SIGNAL(valueChanged(double)), this, SLOT(slotDouble(double)), Qt::UniqueConnection);
|
||||
ok = true;
|
||||
}
|
||||
if (cn == "QLineEdit" || cn == "CLineEdit") {
|
||||
connect(w, SIGNAL(textChanged(QString)), this, SLOT(slotText(QString)), Qt::UniqueConnection);
|
||||
ok = true;
|
||||
}
|
||||
if (!ok) continue;
|
||||
binded_widgets[w] = k->path();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void QCDCore::updateBindedWidgets() {
|
||||
QMapIterator<QWidget * , PIDeque<int> > it(binded_widgets);
|
||||
updating = true;
|
||||
while (it.hasNext()) {
|
||||
QWidget * w = it.next().key();
|
||||
QString cn = w->metaObject()->className();
|
||||
const CDType k(CDCore::instance()->k()[it.value()]);
|
||||
if (cn == "QCheckBox") qobject_cast<QCheckBox*>(w)->setChecked(k.toBool());
|
||||
if (cn == "QGroupBox") qobject_cast<QGroupBox*>(w)->setChecked(k.toBool());
|
||||
if (cn == "QSpinBox") qobject_cast<QSpinBox*>(w)->setValue(k.toInt());
|
||||
if (cn == "QSlider") qobject_cast<QSlider*>(w)->setValue(k.toInt());
|
||||
if (cn == "QScrollBar") qobject_cast<QScrollBar*>(w)->setValue(k.toInt());
|
||||
if (cn == "QDoubleSpinBox") qobject_cast<QDoubleSpinBox*>(w)->setValue(k.toDouble());
|
||||
if (cn == "SpinSlider") qobject_cast<SpinSlider*>(w)->setValue(k.toDouble());
|
||||
if (cn == "QLineEdit") qobject_cast<QLineEdit*>(w)->setText(PI2QString(k.value()));
|
||||
if (cn == "CLineEdit") qobject_cast<CLineEdit*>(w)->setText(PI2QString(k.value()));
|
||||
}
|
||||
updating = false;
|
||||
}
|
||||
|
||||
|
||||
int QCDCore::unbindWindow(QWidget * wnd) {
|
||||
if (!wnd) return 0;
|
||||
return unbindWidgets(wnd->findChildren<QWidget * >());
|
||||
}
|
||||
|
||||
|
||||
int QCDCore::unbindWidgets(QList<QWidget * > wl) {
|
||||
int ret = 0;
|
||||
foreach (QWidget * w, wl)
|
||||
if (unbindWidget(w)) ++ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
bool QCDCore::unbindWidget(QWidget * w) {
|
||||
if (!w) return false;
|
||||
if (!binded_widgets.contains(w)) return false;
|
||||
QString cn = w->metaObject()->className();
|
||||
if (cn == "QCheckBox" || cn == "QGroupBox")
|
||||
disconnect(w, SIGNAL(toggled(bool)), this, SLOT(slotBool(bool)));
|
||||
if (cn == "QSpinBox" || cn == "QSlider" || cn == "QScrollBar")
|
||||
disconnect(w, SIGNAL(valueChanged(int)), this, SLOT(slotInt(int)));
|
||||
if (cn == "QDoubleSpinBox" || cn == "SpinSlider")
|
||||
disconnect(w, SIGNAL(valueChanged(double)), this, SLOT(slotDouble(double)));
|
||||
if (cn == "QLineEdit" || cn == "CLineEdit")
|
||||
disconnect(w, SIGNAL(textChanged(QString)), this, SLOT(slotText(QString)));
|
||||
binded_widgets.remove(w);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void QCDCore::unbindAllWidgets() {
|
||||
QMapIterator<QWidget * , PIDeque<int> > it(binded_widgets);
|
||||
while (it.hasNext()) {
|
||||
QWidget * w = it.next().key();
|
||||
unbindWidget(w);
|
||||
}
|
||||
binded_widgets.clear();
|
||||
}
|
||||
Reference in New Issue
Block a user