Fraxinus  16.5.0-fx-rc6
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxNetworkConnectionWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 
35 #include <QPushButton>
36 #include "cxNetworkConnection.h"
37 #include "cxHelperWidgets.h"
38 #include "cxProfile.h"
39 #include "cxLogger.h"
40 #include "boost/bind.hpp"
42 
43 
44 namespace cx {
45 
47  BaseWidget(parent, "OpenIGTLinkConnectionWidget", "OpenIGTLink Connection"),
48  mClient(client)
49 {
50  StringPropertyBasePtr ip = mClient->getIpOption();
51  DoublePropertyBasePtr port = mClient->getPortOption();
52  StringPropertyBasePtr dialects = mClient->getDialectOption();
53  StringPropertyBasePtr role = mClient->getRoleOption();
54 
55  mConnectButton = new QPushButton("Connect", this);
56  mConnectButton->setCheckable(true);
57  connect(mConnectButton, &QPushButton::clicked, this, &NetworkConnectionWidget::connectButtonClicked);
58 
59  connect(mClient->getNetworkConnection(), &NetworkConnection::stateChanged, this, &NetworkConnectionWidget::onStateChanged);
60 
61  QVBoxLayout* topLayout = new QVBoxLayout(this);
62 
63  mOptionsWidget = new QWidget(this);
64  QVBoxLayout* optionsLayout = new QVBoxLayout(mOptionsWidget);
65  optionsLayout->setMargin(0);
66  topLayout->addWidget(mOptionsWidget);
67  optionsLayout->addWidget(sscCreateDataWidget(this, role));
68  QHBoxLayout* hostipLayout = new QHBoxLayout;
69  optionsLayout->addLayout(hostipLayout);
70  hostipLayout->addWidget(sscCreateDataWidget(this, ip));
71  hostipLayout->addWidget(sscCreateDataWidget(this, port));
72  optionsLayout->addWidget(sscCreateDataWidget(this, dialects));
73 
74  topLayout->addWidget(mConnectButton);
75  topLayout->addStretch();
76 
77  this->onStateChanged(mClient->getNetworkConnection()->getState());
78 
79 }
80 
82 {
83 
84 }
85 
87 {
88  return "<html>"
89  "<h3>Connect to an OpenIGTLink server</h3>"
90  "<p>"
91  "Specify the ip address and port of the server you want to connect to. "
92  "</p>"
93  "<p>"
94  "</p>"
95  "<p><i></i></p>"
96  "</html>";
97 }
98 
99 void NetworkConnectionWidget::onStateChanged(CX_SOCKETCONNECTION_STATE state)
100 {
101  QString status = qstring_cast(state);
102  QString action = (state==scsINACTIVE) ? "Connect" : "Disconnect";
103 
104  mConnectButton->blockSignals(true);
105  mConnectButton->setEnabled(state != scsCONNECTING);
106  mConnectButton->setChecked(state != scsINACTIVE);
107  mConnectButton->setText(QString("%1 (%2)").arg(action).arg(status));
108  mConnectButton->blockSignals(false);
109 
110  mOptionsWidget->setEnabled(state == scsINACTIVE);
111 }
112 
113 void NetworkConnectionWidget::connectButtonClicked(bool checked)
114 {
115  if(checked)
116  {
117  boost::function<void()> connect = boost::bind(&NetworkConnection::requestConnect, mClient->getNetworkConnection());
118  mClient->getNetworkConnection()->invoke(connect);
119  }
120  else
121  {
122  boost::function<void()> disconnect = boost::bind(&NetworkConnection::requestDisconnect, mClient->getNetworkConnection());
123  mClient->getNetworkConnection()->invoke(disconnect);
124  }
125 }
126 
127 
128 
129 
130 
131 } //namespace cx
QString qstring_cast(const T &val)
CX_SOCKETCONNECTION_STATE
void stateChanged(CX_SOCKETCONNECTION_STATE status)
virtual void requestDisconnect()
not thread-safe: use invoke
virtual void requestConnect()
not thread-safe: use invoke
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr
boost::shared_ptr< class DoublePropertyBase > DoublePropertyBasePtr
boost::shared_ptr< class NetworkConnectionHandle > NetworkConnectionHandlePtr
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
QWidget * sscCreateDataWidget(QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
virtual QString defaultWhatsThis() const
NetworkConnectionWidget(NetworkConnectionHandlePtr client, QWidget *parent=NULL)