QWidget *ScriptToolbox::openWindow(QString name, QWidget *parent, Qt::WindowModality modality, Qt::WindowFlags flags) { QWidget *returnVal = xtGetScreen(name, parent, flags, 0); if(returnVal) { if(!returnVal->inherits("QDialog")) omfgThis->handleNewWindow(returnVal); _lastWindow = returnVal; return returnVal; } XSqlQuery screenq; screenq.prepare("SELECT * " "FROM uiform " "WHERE (uiform_name=:uiform_name);"); screenq.bindValue(":uiform_name", name); screenq.exec(); if (screenq.first()) { XUiLoader loader; QByteArray ba = screenq.value("uiform_source").toByteArray(); QBuffer uiFile(&ba); if (!uiFile.open(QIODevice::ReadOnly)) { QMessageBox::critical(0, tr("Could not load UI"), tr("<p>There was an error loading the UI Form " "from the database.")); return 0; } QWidget *ui = loader.load(&uiFile); uiFile.close(); if (ui->inherits("QDialog")) { flags |= Qt::Dialog; if (modality == Qt::NonModal) modality = Qt::WindowModal; } XMainWindow *window = new XMainWindow(parent, screenq.value("uiform_name").toString(), flags); window->setCentralWidget(ui); window->setWindowTitle(ui->windowTitle()); window->setWindowModality(modality); if (ui->inherits("QDialog")) { QDialog *innerdlg = qobject_cast<QDialog*>(ui); connect(innerdlg, SIGNAL(finished(int)), window, SLOT(close())); connect(innerdlg, SIGNAL(accepted()), window, SLOT(close())); connect(innerdlg, SIGNAL(rejected()), window, SLOT(close())); // alternative to creating mydialog object: // for each property of mydialog (including functions) // add a property to _engine's mywindow property if (engine(window)) { QScriptValue mydialog = engine(window)->newQObject(innerdlg); engine(window)->globalObject().setProperty("mydialog", mydialog); } else qWarning("Could not find the script engine to embed a dialog inside " "a placeholder window"); omfgThis->handleNewWindow(window); returnVal = ui; } else { omfgThis->handleNewWindow(window); returnVal = window; } _lastWindow = window; } else if (screenq.lastError().type() != QSqlError::NoError) { systemError(0, screenq.lastError().databaseText(), __FILE__, __LINE__); return 0; } return returnVal; }
void setup::setCurrentIndex(XTreeWidgetItem* item) { QString uiName = item->data(0, Xt::RawRole).toString(); QString label = "<span style=\" font-size:14pt; font-weight:600;\">%1</span></p></body></html>"; if (_itemMap.contains(uiName) && _itemMap.value(uiName).index >= 0) { _stack->setCurrentIndex(_itemMap.value(uiName).index); _stackLit->setText(label.arg(item->text(0))); return; } else if (_itemMap.contains(uiName) && !item->isDisabled()) { // First look for a class... QWidget *w = xtGetScreen(uiName, this); if (w) _itemMap[uiName].implementation = w; else { // No class, so look for an extension XSqlQuery screenq; screenq.prepare("SELECT * " " FROM uiform " " WHERE((uiform_name=:uiform_name)" " AND (uiform_enabled))" " ORDER BY uiform_order DESC" " LIMIT 1;"); screenq.bindValue(":uiform_name", uiName); screenq.exec(); if (screenq.first()) { QUiLoader loader; QByteArray ba = screenq.value("uiform_source").toByteArray(); QBuffer uiFile(&ba); if (!uiFile.open(QIODevice::ReadOnly)) QMessageBox::critical(0, tr("Could not load UI"), tr("<p>There was an error loading the UI Form " "from the database.")); w = loader.load(&uiFile); w->setObjectName(uiName); uiFile.close(); // Load scripts if applicable XSqlQuery scriptq; scriptq.prepare("SELECT script_source, script_order" " FROM script" " WHERE((script_name=:script_name)" " AND (script_enabled))" " ORDER BY script_order;"); scriptq.bindValue(":script_name", uiName); scriptq.exec(); QScriptEngine* engine = new QScriptEngine(); if (_preferences->boolean("EnableScriptDebug")) { QScriptEngineDebugger* debugger = new QScriptEngineDebugger(this); debugger->attachTo(engine); } omfgThis->loadScriptGlobals(engine); QScriptValue mywindow = engine->newQObject(w); engine->globalObject().setProperty("mywindow", mywindow); while(scriptq.next()) { QString script = scriptHandleIncludes(scriptq.value("script_source").toString()); QScriptValue result = engine->evaluate(script, uiName); if (engine->hasUncaughtException()) { int line = engine->uncaughtExceptionLineNumber(); qDebug() << "uncaught exception at line" << line << ":" << result.toString(); } } _itemMap[uiName].implementation = engine; } } if (w) { // Hide buttons out of context here QWidget* close = w->findChild<QWidget*>("_close"); if (close) close->hide(); QWidget* buttons = w->findChild<QDialogButtonBox*>(); if (buttons) buttons->hide(); //Set mode if applicable int mode = _itemMap.value(uiName).mode; if (mode && w->inherits("XDialog")) { XWidget* x = dynamic_cast<XWidget*>(w); ParameterList params; if (mode == cEdit) params.append("mode", "edit"); else if (mode == cView) params.append("mode", "view"); if (x) x->set(params); } int idx = _stack->count(); _itemMap[uiName].index = idx; _stack->addWidget(w); _stack->setCurrentIndex(idx); _stackLit->setText(label.arg(item->text(0))); return; } } // Nothing here so try the next one XTreeWidgetItem* next = dynamic_cast<XTreeWidgetItem*>(_tree->itemBelow(item)); if (next) setCurrentIndex(next); }