/*========================================================================= Program: ParaView Module: $RCSfile$ Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc. All rights reserved. ParaView is a free software; you can redistribute it and/or modify it under the terms of the ParaView license version 1.2. See License_v1.2.txt for the full ParaView license. A copy of this license can be obtained by contacting Kitware Inc. 28 Corporate Drive Clifton Park, NY 12065 USA THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ========================================================================*/ #include "pqServerLauncher.h" #include "ui_pqServerLauncherDialog.h" #include "pqApplicationCore.h" #include "pqCoreUtilities.h" #include "pqFileChooserWidget.h" #include "pqObjectBuilder.h" #include "pqServerConfiguration.h" #include "pqServer.h" #include "pqServerResource.h" #include "pqSettings.h" #include "vtkMath.h" #include "vtkProcessModule.h" #include "vtkPVConfig.h" #include "vtkPVOptions.h" #include "vtkPVXMLElement.h" #include "vtkTimerLog.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //---------------------------------------------------------------------------- const QMetaObject* pqServerLauncher::DefaultServerLauncherType = NULL; const QMetaObject* pqServerLauncher::setServerDefaultLauncherType(const QMetaObject* other) { const QMetaObject* old = pqServerLauncher::DefaultServerLauncherType; pqServerLauncher::DefaultServerLauncherType = other; return old; } const QMetaObject* pqServerLauncher::defaultServerLauncherType() { return pqServerLauncher::DefaultServerLauncherType; } pqServerLauncher* pqServerLauncher::newInstance( const pqServerConfiguration& configuration, QObject* parentObject) { if (pqServerLauncher::DefaultServerLauncherType) { QObject* aObject = pqServerLauncher::DefaultServerLauncherType->newInstance( Q_ARG(const pqServerConfiguration&, configuration), Q_ARG(QObject*, parentObject)); if (pqServerLauncher* aLauncher = qobject_cast(aObject)) { return aLauncher; } delete aObject; } return new pqServerLauncher(configuration, parentObject); } //---------------------------------------------------------------------------- namespace { /// pqWidget is used to make it easier to get and set values from different /// types of widgets. class pqWidget : public QObject { QString PropertyName; public: QWidget* Widget; bool ToSave; pqWidget() : Widget(NULL), ToSave(false) { } pqWidget(QWidget* wdg, const QString& pname) : PropertyName(pname), Widget(wdg), ToSave(false) { } virtual ~pqWidget() { } virtual QVariant get() const { return this->Widget->property(this->PropertyName.toLatin1().data()); } virtual void set(const QVariant& value) { this->Widget->setProperty(this->PropertyName.toLatin1().data(), value); } private: Q_DISABLE_COPY(pqWidget); }; class pqWidgetForComboBox : public pqWidget { public: pqWidgetForComboBox(QComboBox* widget): pqWidget(widget, QString()) { } virtual QVariant get() const { QComboBox* combobox = qobject_cast(this->Widget); return combobox->itemData(combobox->currentIndex()); } virtual void set(const QVariant& value) { QComboBox* combobox = qobject_cast(this->Widget); combobox->setCurrentIndex(combobox->findData(value)); } private: Q_DISABLE_COPY(pqWidgetForComboBox); }; class pqWidgetForCheckbox : public pqWidget { QString TrueValue; QString FalseValue; public: pqWidgetForCheckbox(QCheckBox* widget, const char* tval, const char* fval): pqWidget(widget, QString()), TrueValue(tval), FalseValue(fval) { } virtual QVariant get() const { QCheckBox* checkbox= qobject_cast(this->Widget); return checkbox->isChecked()? this->TrueValue : this->FalseValue; } virtual void set(const QVariant& value) { QCheckBox* checkbox= qobject_cast(this->Widget); checkbox->setChecked(value.toString() == this->TrueValue); } private: Q_DISABLE_COPY(pqWidgetForCheckbox); }; /// Returns pre-defined run-time environment. This includes the environement /// of this application itself as well as some predefined values. QProcessEnvironment getDefaultEnvironment( const pqServerConfiguration& configuration) { pqServerResource resource = configuration.resource(); // Get the process environment. QProcessEnvironment options = QProcessEnvironment::systemEnvironment(); // Now append the pre-defined runtime environment to this. options.insert("PV_CLIENT_HOST", QHostInfo::localHostName()); options.insert("PV_CONNECTION_URI", resource.toURI()); options.insert("PV_CONNECTION_SCHEME", resource.scheme()); options.insert("PV_VERSION_MAJOR", QString::number(PARAVIEW_VERSION_MAJOR)); options.insert("PV_VERSION_MINOR", QString::number(PARAVIEW_VERSION_MINOR)); options.insert("PV_VERSION_PATCH", QString::number(PARAVIEW_VERSION_PATCH)); options.insert("PV_VERSION",PARAVIEW_VERSION); options.insert("PV_VERSION_FULL", PARAVIEW_VERSION_FULL); options.insert("PV_SERVER_HOST", resource.host()); options.insert("PV_SERVER_PORT", QString::number(resource.port(11111))); options.insert("PV_DATA_SERVER_HOST", resource.dataServerHost()); options.insert("PV_DATA_SERVER_PORT", QString::number(resource.dataServerPort(11111))); options.insert("PV_RENDER_SERVER_HOST", resource.renderServerHost()); options.insert("PV_RENDER_SERVER_PORT", QString::number(resource.renderServerPort(22221))); return options; } /// Processes the XML defined in the server configuration to /// update the dialog with widgets of right type with correct default values. /// It uses application settings to obtain the default values, whenever /// possible. bool createWidgets(QMap& widgets, QDialog& dialog, const pqServerConfiguration& configuration, QProcessEnvironment& options) { vtkPVXMLElement* optionsXML = configuration.optionsXML(); Q_ASSERT(optionsXML != NULL); QFormLayout *formLayout = new QFormLayout(); dialog.setLayout(formLayout); dialog.setWindowTitle( QString("Connection Options for \"%1\"").arg(configuration.name())); pqSettings* settings = pqApplicationCore::instance()->settings(); // Process the to create dialog with widgets. for (unsigned int cc=0; cc < optionsXML->GetNumberOfNestedElements(); cc++) { vtkPVXMLElement* node = optionsXML->GetNestedElement(cc); if (node->GetName() == NULL) { continue; } if (strcmp(node->GetName(), "Set") == 0) { options.insert(node->GetAttribute("name"), node->GetAttribute("value")); } else if (strcmp(node->GetName(), "Option") == 0) { vtkPVXMLElement* typeNode = node->GetNestedElement(0); if (typeNode == NULL || typeNode->GetName() == NULL) { continue; } const char* name = node->GetAttribute("name"); const char* label = node->GetAttributeOrDefault("label", name); bool readonly = strcmp(node->GetAttributeOrDefault("readonly", "false"), "true") == 0; bool save = strcmp(node->GetAttributeOrDefault("save", "true"), "true") == 0; QString settingsKey = QString("SERVER_STARTUP/%1.%2").arg(configuration.name()).arg(name); bool default_is_random = false; QVariant default_value; if (typeNode->GetAttribute("default")) { default_is_random = (strcmp(typeNode->GetAttribute("default"), "random") == 0); default_value = QString(typeNode->GetAttribute("default")); // if default_is_random, save cannot be true. if (default_is_random) { save = false; } } // noise is a in the range [0, 1]. double noise = 0.0; if (default_is_random) { // We need a seed that changes every execution. Get the // universal time as double and then add all the bytes // together to get a nice seed without causing any overflow. long rseed = 0; double atime = vtkTimerLog::GetUniversalTime()*1000; char* tc = (char*)&atime; for (unsigned int ic=0; iccontains(settingsKey)) { default_value = settings->value(settingsKey); } if (strcmp(typeNode->GetName(), "Range") == 0) { QString min = typeNode->GetAttributeOrDefault("min","0"); QString max = typeNode->GetAttributeOrDefault("max", "99999999999"); QString step = typeNode->GetAttributeOrDefault("step", "1"); QWidget* widget = NULL; if (strcmp(typeNode->GetAttributeOrDefault("type", "int"), "int") == 0) { widget = new QSpinBox(&dialog); if (default_is_random) { default_value = min.toInt() + (max.toInt() - min.toInt()) * noise; } } else // assume double. { widget = new QDoubleSpinBox(&dialog); if (default_is_random) { default_value = min.toDouble() + (max.toDouble() - min.toDouble()) * noise; } } widgets[name] = new pqWidget(widget, "value"); widget->setProperty("minimum", QVariant(min)); widget->setProperty("maximum", QVariant(max)); widget->setProperty("singleStep", QVariant(step)); } else if (strcmp(typeNode->GetName(), "String") == 0) { QLineEdit* widget = new QLineEdit(QString(), &dialog); widgets[name] = new pqWidget(widget, "text"); } else if (strcmp(typeNode->GetName(), "File") == 0) { pqFileChooserWidget* widget = new pqFileChooserWidget(&dialog); widget->setForceSingleFile(true); widgets[name] = new pqWidget(widget, "singleFilename"); } else if (strcmp(typeNode->GetName(), "Boolean") == 0) { QCheckBox * checkbox = new QCheckBox(&dialog); const char* true_value = typeNode->GetAttributeOrDefault("true", "1"); const char* false_value = typeNode->GetAttributeOrDefault("false", "0"); widgets[name] = new pqWidgetForCheckbox(checkbox, true_value, false_value); } else if (strcmp(typeNode->GetName(), "Enumeration") == 0) { QComboBox* widget = new QComboBox(&dialog); for (unsigned int kk=0; kk < typeNode->GetNumberOfNestedElements(); kk++) { vtkPVXMLElement* child = typeNode->GetNestedElement(kk); if (QString(child->GetName()) == "Entry") { QString xml_value = child->GetAttribute("value"); QString xml_label = child->GetAttributeOrDefault( "label", xml_value.toLatin1().data()); widget->addItem(xml_label, xml_value); } } widgets[name] = new pqWidgetForComboBox(widget); } else { qWarning() << "Ignoring unknown element '<" << typeNode->GetName() << "/>' discovered under