add QAD valuetreeeditor util
This commit is contained in:
90
utils/valuetreeeditor/mainwindow.cpp
Normal file
90
utils/valuetreeeditor/mainwindow.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include "piqt.h"
|
||||
#include "qad_locations.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <pifile.h>
|
||||
#include <pijson.h>
|
||||
#include <pivaluetree.h>
|
||||
#include <pivaluetree_conversions.h>
|
||||
|
||||
|
||||
MainWindow::MainWindow(QWidget * parent): EMainWindow(parent), Ui::MainWindow() {
|
||||
setupUi(this);
|
||||
session.setFile(QAD::userPath(QAD::ltConfig, "session_valuetreeeditor"));
|
||||
session.addEntry(this);
|
||||
session.load();
|
||||
widget->setGroupingEnabled(actionGrouping->isChecked());
|
||||
widget->setFullEditMode(actionFull_edit_mode->isChecked());
|
||||
}
|
||||
|
||||
|
||||
MainWindow::~MainWindow() {
|
||||
session.save();
|
||||
session.clear(true);
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::reset(bool) {
|
||||
widget->setValue(PIValueTree());
|
||||
setWindowTitle(tr("PIValueTree Editor"));
|
||||
}
|
||||
|
||||
|
||||
bool MainWindow::load(const QString & path) {
|
||||
PIFile f(Q2PIString(path), PIIODevice::ReadOnly);
|
||||
if (!f.isOpened()) return false;
|
||||
PIString ext = PIFile::FileInfo(f.path()).extension().toLowerCase();
|
||||
PIValueTree v;
|
||||
if (ext == "conf" || ext == "ini")
|
||||
v = PIValueTreeConversions::fromText(&f);
|
||||
else if (ext == "json")
|
||||
v = PIValueTreeConversions::fromJSON(PIJSON::fromJSON(PIString::fromUTF8(f.readAll())));
|
||||
else if (ext == "bin")
|
||||
v = piDeserialize<PIValueTree>(f.readAll());
|
||||
widget->setValue(v);
|
||||
setWindowTitle(tr("PIValueTree Editor - %1").arg(QFileInfo(path).fileName()));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool MainWindow::save(const QString & path) {
|
||||
PIFile f(Q2PIString(path), PIIODevice::ReadWrite);
|
||||
if (!f.isOpened()) return false;
|
||||
f.clear();
|
||||
PIString ext = PIFile::FileInfo(f.path()).extension().toLowerCase();
|
||||
auto v = widget->value();
|
||||
if (ext == "conf" || ext == "ini")
|
||||
f.write(PIValueTreeConversions::toText(v).toUTF8());
|
||||
else if (ext == "json")
|
||||
f.write(PIValueTreeConversions::toJSON(v).toJSON(PIJSON::Tree, false).toUTF8());
|
||||
else if (ext == "bin")
|
||||
f.write(piSerialize(v));
|
||||
setWindowTitle(tr("PIValueTree Editor - %1").arg(QFileInfo(path).fileName()));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::changeEvent(QEvent * e) {
|
||||
QMainWindow::changeEvent(e);
|
||||
switch (e->type()) {
|
||||
case QEvent::LanguageChange: retranslateUi(this); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
QString MainWindow::loadFilter() {
|
||||
return "All types(*.conf *.ini *.json *.bin);;INI format(*.conf *.ini);;JSON(*.json);;Binary(*.bin)";
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::on_actionGrouping_toggled(bool on) {
|
||||
widget->setGroupingEnabled(on);
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::on_actionFull_edit_mode_toggled(bool on) {
|
||||
widget->setFullEditMode(on);
|
||||
}
|
||||
Reference in New Issue
Block a user