示例#1
0
/** \brief Open a new scripted or core application window.

    This method opens a new window on the display. It can be defined
    in its .ui as an XWidget, XDialog, or XMainWindow.
    If there is a record in the %uiform table, this .ui is instantiated;
    otherwise openWindow opens a core application window with the
    given name if one exists.

    Windows that inherit from QDialog are handled specially.
    If the modality passed is Qt::NonModal, the modality is changed to
    Qt::WindowModal. The flags parameter gets Qt::Dialog set
    even if the caller did not set it.
    Because of the way the new windows are created,
    a special \c mydialog object gets created to allow access to the
    QDialog properties and methods, such as QDialog::exec.

    \param pname  The name of the %uiform or core application class of the
                  window to open
    \param parent The widget to set as the parent of the newly-created window
    \param modality This can be any of the defined Qt::WindowModality values
    \param flags    This can be any Qt::WindowFlags value

    \return The newly-opened window; 0 if there was an error
  */
QWidget *ScriptToolbox::openWindow(QString pname, QWidget *parent, Qt::WindowModality modality, Qt::WindowFlags flags)
{
  QWidget *returnVal = xtGetScreen(pname, 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)"
                  "   AND (uiform_enabled))"
                  " ORDER BY uiform_order DESC"
                  " LIMIT 1;");
  screenq.bindValue(":uiform_name", pname);
  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);
    QSize size = ui->size();
    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);
    window->resize(size);

    if (ui->inherits("QDialog"))
    {
      QDialog *innerdlg = qobject_cast<QDialog*>(ui);

      connect(innerdlg, SIGNAL(finished(int)), 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;
}