From 7c6af64d5516d3fe91da59eeaf1c6db1747d8bbc Mon Sep 17 00:00:00 2001 From: Rainer Keller Date: Fri, 13 Sep 2019 14:11:06 +0200 Subject: Use UI file in opcuaviewer example Change-Id: Ifbf9aae2476eb09840928700d7b37ecc059cefd5 Reviewed-by: Friedemann Kleint --- examples/opcua/opcuaviewer/mainwindow.cpp | 161 +++++++++---------------- examples/opcua/opcuaviewer/mainwindow.h | 20 +--- examples/opcua/opcuaviewer/mainwindow.ui | 181 +++++++++++++++++++++++++++++ examples/opcua/opcuaviewer/opcuaviewer.pro | 3 +- 4 files changed, 246 insertions(+), 119 deletions(-) create mode 100644 examples/opcua/opcuaviewer/mainwindow.ui diff --git a/examples/opcua/opcuaviewer/mainwindow.cpp b/examples/opcua/opcuaviewer/mainwindow.cpp index 7c7c989..488dda1 100644 --- a/examples/opcua/opcuaviewer/mainwindow.cpp +++ b/examples/opcua/opcuaviewer/mainwindow.cpp @@ -51,24 +51,13 @@ #include "mainwindow.h" #include "opcuamodel.h" #include "certificatedialog.h" +#include "ui_mainwindow.h" -#include #include #include -#include -#include #include -#include -#include -#include -#include -#include -#include #include -#include #include -#include -#include #include #include #include @@ -124,75 +113,34 @@ static void messageHandler(QtMsgType type, const QMessageLogContext &context, co } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) - , mOpcUaPlugin(new QComboBox(this)) - , mHost(new QLineEdit(this)) - , mServers(new QComboBox(this)) - , mEndpoints(new QComboBox(this)) - , mFindServersButton(new QPushButton(tr("Find Servers"), this)) - , mGetEndpointsButton(new QPushButton(tr("Get Endpoints"), this)) - , mConnectButton(new QPushButton(tr("Connect"), this)) - , mLog(new QPlainTextEdit(this)) - , mTreeView(new QTreeView(this)) + , ui(new Ui::MainWindow) , mOpcUaModel(new OpcUaModel(this)) , mOpcUaProvider(new QOpcUaProvider(this)) { + ui->setupUi(this); mainWindowGlobal = this; - auto menubar = menuBar(); - auto fileMenu = menubar->addMenu(tr("File")); - auto quitAction = fileMenu->addAction(tr("Quit"), this, &QWidget::close); - quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); - auto helpMenu = menubar->addMenu(tr("Help")); - auto aboutAction = helpMenu->addAction(tr("About Qt"), this, &QApplication::aboutQt); - aboutAction->setShortcut(QKeySequence(QKeySequence::HelpContents)); - - auto centralWidget = new QWidget; - auto vbox = new QVBoxLayout(centralWidget); - setCentralWidget(centralWidget); - - int row = 0; - auto grid = new QGridLayout; - grid->addWidget(new QLabel(tr("Select OPC UA Backend:")), row, 0); - grid->addWidget(mOpcUaPlugin, row, 1); - grid->addWidget(new QLabel(tr("Select host to discover:")), ++row, 0); - grid->addWidget(mHost, row, 1); - grid->addWidget(mFindServersButton, row, 2); - grid->addWidget(new QLabel(tr("Select OPC UA Server:")), ++row, 0); - grid->addWidget(mServers, row, 1); - grid->addWidget(mGetEndpointsButton, row, 2); - grid->addWidget(new QLabel(tr("Select OPC UA Endpoint:")), ++row, 0); - grid->addWidget(mEndpoints, row, 1); - grid->addWidget(mConnectButton, row, 2); - - vbox->addLayout(grid); - vbox->addWidget(mTreeView); - vbox->addWidget(new QLabel(tr("Log:"))); - vbox->addWidget(mLog); - - mHost->setText("opc.tcp://localhost:48010"); - mEndpoints->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + connect(ui->quitAction, &QAction::triggered, this, &QWidget::close); + ui->quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); + + connect(ui->aboutAction, &QAction::triggered, this, &QApplication::aboutQt); + ui->aboutAction->setShortcut(QKeySequence(QKeySequence::HelpContents)); updateUiState(); - mOpcUaPlugin->addItems(mOpcUaProvider->availableBackends()); - mLog->setReadOnly(true); - mLog->setLineWrapMode(QPlainTextEdit::NoWrap); - setMinimumWidth(800); - mTreeView->setModel(mOpcUaModel); - mTreeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents); - mTreeView->setTextElideMode(Qt::ElideRight); - mTreeView->setAlternatingRowColors(true); - mTreeView->setSelectionBehavior(QAbstractItemView::SelectItems); - - if (mOpcUaPlugin->count() == 0) { - mOpcUaPlugin->setDisabled(true); - mConnectButton->setDisabled(true); + ui->opcUaPlugin->addItems(mOpcUaProvider->availableBackends()); + ui->treeView->setModel(mOpcUaModel); + ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents); + + if (ui->opcUaPlugin->count() == 0) { + ui->opcUaPlugin->setDisabled(true); + ui->connectButton->setDisabled(true); QMessageBox::critical(this, tr("No OPCUA plugins available"), tr("The list of available OPCUA plugins is empty. No connection possible.")); } - connect(mFindServersButton, &QPushButton::clicked, this, &MainWindow::findServers); - connect(mGetEndpointsButton, &QPushButton::clicked, this, &MainWindow::getEndpoints); - connect(mConnectButton, &QPushButton::clicked, this, &MainWindow::connectToServer); + connect(ui->findServersButton, &QPushButton::clicked, this, &MainWindow::findServers); + connect(ui->getEndpointsButton, &QPushButton::clicked, this, &MainWindow::getEndpoints); + connect(ui->connectButton, &QPushButton::clicked, this, &MainWindow::connectToServer); oldMessageHandler = qInstallMessageHandler(&messageHandler); setupPkiConfiguration(); @@ -202,6 +150,11 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) //! [Application Identity] } +MainWindow::~MainWindow() +{ + delete ui; +} + //! [PKI Configuration] void MainWindow::setupPkiConfiguration() { @@ -225,7 +178,7 @@ void MainWindow::setupPkiConfiguration() void MainWindow::createClient() { if (mOpcUaClient == nullptr) { - mOpcUaClient = mOpcUaProvider->createClient(mOpcUaPlugin->currentText()); + mOpcUaClient = mOpcUaProvider->createClient(ui->opcUaPlugin->currentText()); if (!mOpcUaClient) { const QString message(tr("Connecting to the given sever failed. See the log for details.")); log(message, QString(), Qt::red); @@ -256,7 +209,7 @@ void MainWindow::findServers() { QStringList localeIds; QStringList serverUris; - QUrl url(mHost->text()); + QUrl url(ui->host->text()); updateUiState(); @@ -275,11 +228,11 @@ void MainWindow::findServersComplete(const QVector QOpcUaApplicationDescription server; if (isSuccessStatus(statusCode)) { - mServers->clear(); + ui->servers->clear(); for (const auto &server : servers) { QVector urls = server.discoveryUrls(); for (const auto &url : qAsConst(urls)) - mServers->addItem(url); + ui->servers->addItem(url); } } @@ -288,11 +241,11 @@ void MainWindow::findServersComplete(const QVector void MainWindow::getEndpoints() { - mEndpoints->clear(); + ui->endpoints->clear(); updateUiState(); - if (mServers->currentIndex() >= 0) { - const QString serverUrl = mServers->currentText(); + if (ui->servers->currentIndex() >= 0) { + const QString serverUrl = ui->servers->currentText(); createClient(); mOpcUaClient->requestEndpoints(serverUrl); } @@ -319,7 +272,7 @@ void MainWindow::getEndpointsComplete(const QVector & const QString EndpointName = QString("%1 (%2)") .arg(endpoint.securityPolicy()) .arg(modes[endpoint.securityMode()]); - mEndpoints->addItem(EndpointName, index++); + ui->endpoints->addItem(EndpointName, index++); } } @@ -333,8 +286,8 @@ void MainWindow::connectToServer() return; } - if (mEndpoints->currentIndex() >= 0) { - m_endpoint = mEndpointList[mEndpoints->currentIndex()]; + if (ui->endpoints->currentIndex() >= 0) { + m_endpoint = mEndpointList[ui->endpoints->currentIndex()]; createClient(); mOpcUaClient->connectToEndpoint(m_endpoint); } @@ -367,7 +320,7 @@ void MainWindow::namespacesArrayUpdated(const QStringList &namespaceArray) disconnect(mOpcUaClient, &QOpcUaClient::namespaceArrayUpdated, this, &MainWindow::namespacesArrayUpdated); mOpcUaModel->setOpcUaClient(mOpcUaClient); - mTreeView->header()->setSectionResizeMode(1 /* Value column*/, QHeaderView::Interactive); + ui->treeView->header()->setSectionResizeMode(1 /* Value column*/, QHeaderView::Interactive); } void MainWindow::clientError(QOpcUaClient::ClientError error) @@ -383,44 +336,44 @@ void MainWindow::clientState(QOpcUaClient::ClientState state) void MainWindow::updateUiState() { // allow changing the backend only if it was not already created - mOpcUaPlugin->setEnabled(mOpcUaClient == nullptr); - mConnectButton->setText(mClientConnected ? tr("Disconnect") : tr("Connect")); + ui->opcUaPlugin->setEnabled(mOpcUaClient == nullptr); + ui->connectButton->setText(mClientConnected ? tr("Disconnect") : tr("Connect")); if (mClientConnected) { - mHost->setEnabled(false); - mServers->setEnabled(false); - mEndpoints->setEnabled(false); - mFindServersButton->setEnabled(false); - mGetEndpointsButton->setEnabled(false); - mConnectButton->setEnabled(true); + ui->host->setEnabled(false); + ui->servers->setEnabled(false); + ui->endpoints->setEnabled(false); + ui->findServersButton->setEnabled(false); + ui->getEndpointsButton->setEnabled(false); + ui->connectButton->setEnabled(true); } else { - mHost->setEnabled(true); - mServers->setEnabled(mServers->count() > 0); - mEndpoints->setEnabled(mEndpoints->count() > 0); + ui->host->setEnabled(true); + ui->servers->setEnabled(ui->servers->count() > 0); + ui->endpoints->setEnabled(ui->endpoints->count() > 0); - mFindServersButton->setDisabled(mHost->text().isEmpty()); - mGetEndpointsButton->setEnabled(mServers->currentIndex() != -1); - mConnectButton->setEnabled(mEndpoints->currentIndex() != -1); + ui->findServersButton->setDisabled(ui->host->text().isEmpty()); + ui->getEndpointsButton->setEnabled(ui->servers->currentIndex() != -1); + ui->connectButton->setEnabled(ui->endpoints->currentIndex() != -1); } if (!mOpcUaClient) { - mServers->setEnabled(false); - mEndpoints->setEnabled(false); - mGetEndpointsButton->setEnabled(false); - mConnectButton->setEnabled(false); + ui->servers->setEnabled(false); + ui->endpoints->setEnabled(false); + ui->getEndpointsButton->setEnabled(false); + ui->connectButton->setEnabled(false); } } void MainWindow::log(const QString &text, const QString &context, QColor color) { - auto cf = mLog->currentCharFormat(); + auto cf = ui->log->currentCharFormat(); cf.setForeground(color); - mLog->setCurrentCharFormat(cf); - mLog->appendPlainText(text); + ui->log->setCurrentCharFormat(cf); + ui->log->appendPlainText(text); if (!context.isEmpty()) { cf.setForeground(Qt::gray); - mLog->setCurrentCharFormat(cf); - mLog->insertPlainText(context); + ui->log->setCurrentCharFormat(cf); + ui->log->insertPlainText(context); } } diff --git a/examples/opcua/opcuaviewer/mainwindow.h b/examples/opcua/opcuaviewer/mainwindow.h index e19059a..c506df6 100644 --- a/examples/opcua/opcuaviewer/mainwindow.h +++ b/examples/opcua/opcuaviewer/mainwindow.h @@ -56,19 +56,19 @@ QT_BEGIN_NAMESPACE -class QPlainTextEdit; -class QLineEdit; -class QComboBox; -class QPushButton; class QOpcUaProvider; -class QTreeView; class OpcUaModel; +namespace Ui { +class MainWindow; +} + class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); Q_INVOKABLE void log(const QString &text, const QString &context, QColor color); void log(const QString &text, QColor color = Qt::black); @@ -93,15 +93,7 @@ private: bool createPkiPath(const QString &path); private: - QComboBox *mOpcUaPlugin; - QLineEdit *mHost; - QComboBox *mServers; - QComboBox *mEndpoints; - QPushButton *mFindServersButton; - QPushButton *mGetEndpointsButton; - QPushButton *mConnectButton; - QPlainTextEdit *mLog; - QTreeView *mTreeView; + Ui::MainWindow *ui; OpcUaModel *mOpcUaModel; QOpcUaProvider *mOpcUaProvider; QOpcUaClient *mOpcUaClient = nullptr; diff --git a/examples/opcua/opcuaviewer/mainwindow.ui b/examples/opcua/opcuaviewer/mainwindow.ui new file mode 100644 index 0000000..6fe0420 --- /dev/null +++ b/examples/opcua/opcuaviewer/mainwindow.ui @@ -0,0 +1,181 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + + 800 + 0 + + + + MainWindow + + + + + + + true + + + QAbstractItemView::SelectItems + + + + + + + + + + + Select OPC UA Backend: + + + + + + + Find Servers + + + + + + + Get Endpoints + + + + + + + + + + Select OPC UA Server: + + + + + + + opc.tcp://localhost:48010 + + + + + + + + + + Select host to discover: + + + + + + + Select OPC UA Endpoint: + + + + + + + + 0 + 0 + + + + + + + + Connect + + + + + + + + + + + Log: + + + + + + + QPlainTextEdit::NoWrap + + + true + + + + + + + + + 0 + 0 + 800 + 30 + + + + + File + + + + + + Help + + + + + + + + + + Quit + + + + + Help + + + + + About Qt + + + + + About Qt + + + + + + diff --git a/examples/opcua/opcuaviewer/opcuaviewer.pro b/examples/opcua/opcuaviewer/opcuaviewer.pro index bc641d0..cbe8057 100644 --- a/examples/opcua/opcuaviewer/opcuaviewer.pro +++ b/examples/opcua/opcuaviewer/opcuaviewer.pro @@ -19,4 +19,5 @@ HEADERS += \ certificatedialog.h FORMS += \ - certificatedialog.ui + certificatedialog.ui \ + mainwindow.ui \ -- cgit v1.2.3