File: DatasetServerDialog.cpp

package info (click to toggle)
imagevis3d 2.0.1-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,872 kB
  • sloc: cpp: 53,586; sh: 788; ansic: 292; xml: 258; python: 35; makefile: 16
file content (310 lines) | stat: -rw-r--r-- 9,998 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
   For more information, please see: http://software.sci.utah.edu

   The MIT License

   Copyright (c) 2008 Scientific Computing and Imaging Institute,
   University of Utah.


   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/


//!    File   : DatasetServerDialog.cpp
//!    Author : Jens Krueger
//!             DFKI & MMCI, Saarbruecken
//!             SCI Institute, University of Utah
//!    Date   : March 2010
//
//!    Copyright (C) 2010 DFKI, MMCI, SCI Institute

#include <cstdlib>
#include <fstream>
#if defined(__GNUC__) && defined(DETECTED_OS_LINUX)
# pragma GCC visibility push(default)
#endif
#include <QtGui/QtGui>
#include <QtNetwork/QtNetwork>
#if defined(__GNUC__) && defined(DETECTED_OS_LINUX)
# pragma GCC visibility pop
#endif

#include "DatasetServerDialog.h"
#include "../Tuvok/Basics/SysTools.h"


using namespace std;

DatasetServerDialog::DatasetServerDialog(std::string strTempDir, QWidget *parent) : 
  QDialog(parent),
  m_strTempDir(strTempDir),
  m_bRunning(false),
  server(strTempDir)
{
  QSettings settings;
  settings.beginGroup("DatasetServer");

  aDatasets.resize( settings.value("DatasetCount", 0).toUInt() );

  datasetList = new QListWidget(this);

  for (size_t b = 0;b< aDatasets.size();b++) {
    QString volume = tr("Volume%1").arg(b);
    QString transferfunction = tr("TransferFunction%1").arg(b);

    std::string strFilename(settings.value(volume, "").toString().toAscii());
    std::string strTFFilename(settings.value(transferfunction, "").toString().toAscii());
    aDatasets[b] = DatasetTFPair(strFilename, strTFFilename);

  }


  labelPort = new QLabel(this);
  labelPort->setText("TCP/IP Port");
  labelVolFile = new QLabel(this);
  labelVolFile->setText("Dataset");
  label1DTFile = new QLabel(this);
  label1DTFile->setText("Transfer Function");
  editPort = new QLineEdit(this);
  editPort->setText(settings.value("StartPort",22).toString());
  editVolFile = new QLineEdit(this);
  editVolFile->setReadOnly(true);
  editVolFile->setText("");
  edit1DTFile = new QLineEdit(this);
  edit1DTFile->setReadOnly(true);
  edit1DTFile->setText("");

  pickVolFile = new QToolButton(this);
  pickVolFile->setText("...");
  pick1DTFile = new QToolButton(this);
  pick1DTFile->setText("...");
  connect(pickVolFile, SIGNAL(clicked()), this, SLOT(selectVolFile()));
  connect(pick1DTFile, SIGNAL(clicked()), this, SLOT(select1DTFile()));

  addButton = new QPushButton(tr("Add To List"),this);
  removeButton = new QPushButton(tr("Remove From List"),this);
  connect(addButton, SIGNAL(clicked()), this, SLOT(addDataset()));
  connect(removeButton, SIGNAL(clicked()), this, SLOT(removeDataset()));

  statusLabel = new QLabel("",this);

  startButton = new QPushButton(tr("Start"),this);
  startButton->setAutoDefault(true);

  quitButton = new QPushButton(tr("Close"),this);
  quitButton->setAutoDefault(false);

  connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
  connect(startButton, SIGNAL(clicked()), this, SLOT(toggleServers()));

  QHBoxLayout *layoutPort = new QHBoxLayout();
  layoutPort->setMargin(0);
  layoutPort->addWidget(labelPort);
  layoutPort->addWidget(editPort);

  QHBoxLayout *layoutI3MFile = new QHBoxLayout();
  layoutI3MFile->setMargin(0);
  layoutI3MFile->addWidget(labelVolFile);
  layoutI3MFile->addWidget(editVolFile);
  layoutI3MFile->addWidget(pickVolFile);

  QHBoxLayout *layoutAddRemove = new QHBoxLayout();
  layoutAddRemove->setMargin(0);
  layoutAddRemove->addWidget(addButton);
  layoutAddRemove->addWidget(removeButton);

  QHBoxLayout *layout1DTFile = new QHBoxLayout();
  layout1DTFile->setMargin(0);
  layout1DTFile->addWidget(label1DTFile);
  layout1DTFile->addWidget(edit1DTFile);
  layout1DTFile->addWidget(pick1DTFile);

  QHBoxLayout *buttonLayout = new QHBoxLayout();
  buttonLayout->addStretch(1);
  buttonLayout->addWidget(startButton);
  buttonLayout->addWidget(quitButton);
  buttonLayout->addStretch(1);

  QVBoxLayout *mainLayout = new QVBoxLayout();
  mainLayout->addLayout(layoutI3MFile);
  mainLayout->addLayout(layout1DTFile);
  mainLayout->addLayout(layoutAddRemove);
  mainLayout->addWidget(datasetList);
  mainLayout->addLayout(layoutPort);
  mainLayout->addWidget(statusLabel);
  mainLayout->addLayout(buttonLayout);
  setLayout(mainLayout);

  setWindowTitle(tr("ImageVis3D Mobile Dataset Server"));
  settings.endGroup();

  updateListWidgetAndButtons();
}

DatasetServerDialog::~DatasetServerDialog() {
  QSettings settings;
  settings.beginGroup("DatasetServer");
  settings.setValue("StartPort", editPort->text().toInt());
  settings.setValue("DatasetCount", static_cast<unsigned>(aDatasets.size()));

  for (size_t b = 0;b< aDatasets.size();b++) {
    QString volume = tr("Volume%1").arg(b);
    QString transferfunction = tr("TransferFunction%1").arg(b);

    settings.setValue(volume, aDatasets[b].dataset.c_str());
    settings.setValue(transferfunction, aDatasets[b].transferfunction.c_str());
  }

  
  settings.endGroup();
}


void DatasetServerDialog::startServers() {
  // no servers ... nothing todo
  if (aDatasets.size() == 0)  return;

  int port = editPort->text().toInt();

  QString errorMsg = server.startServers(aDatasets, port);

  if (errorMsg != "") {
    QMessageBox::critical(this, this->windowTitle(),errorMsg);
    return;
  }

  if (aDatasets.size() > 1) 
    statusLabel->setText(tr("Sharing %1 datasets on port %2.\n"
                            "Run the iPhone App now.")
						                .arg(aDatasets.size()).arg(port)  );
  else
    statusLabel->setText(tr("Sharing %1 dataset on port %2.\n"
                            "Run the iPhone App now.")
						                .arg(aDatasets.size()).arg(port) );
  startButton->setText(tr("Stop"));
  m_bRunning = true;
}

void DatasetServerDialog::stopServers() {
  if (!m_bRunning) return;

  server.stopServers();
  startButton->setText(tr("Restart"));
  statusLabel->setText(tr(""));

  m_bRunning = false;
}

void DatasetServerDialog::toggleServers() {
  if (m_bRunning) {
    stopServers();
  } else {
    startServers();
  }

}

void DatasetServerDialog::addDataset() {
  if (editVolFile->text() == "") return;

	std::string strFilename(editVolFile->text().toAscii());
	std::string strTFFilename(edit1DTFile->text().toAscii());
  aDatasets.push_back(DatasetTFPair(strFilename, strTFFilename));
  updateListWidgetAndButtons();

  stopServers();
}

void DatasetServerDialog::removeDataset() {
  int index = datasetList->currentRow();
  if (index >= 0 && index < int(aDatasets.size())) 
    aDatasets.erase(aDatasets.begin()+index);
  updateListWidgetAndButtons();

  stopServers();
}

void DatasetServerDialog::updateListWidgetAndButtons() {
  int index = datasetList->currentRow();
  datasetList->clear();
  QString qstr;
  for (size_t i = 0;i<aDatasets.size();i++) {
    if (aDatasets[i].transferfunction != "")
      qstr = tr("%1 / %2").arg(SysTools::GetFilename(aDatasets[i].dataset).c_str())
                                  .arg(SysTools::GetFilename(aDatasets[i].transferfunction).c_str());
    else 
      qstr = tr("%1 / none").arg(SysTools::GetFilename(aDatasets[i].dataset).c_str());
    datasetList->addItem(qstr);
  }
  if ( aDatasets.size() > 0 ) {
    datasetList->setCurrentRow(std::min<int>(index, int(aDatasets.size()-1)));
    removeButton->setEnabled(true);
    startButton->setEnabled(true);
  } else {
    removeButton->setEnabled(false);
    startButton->setEnabled(false);
  }
}

void DatasetServerDialog::select1DTFile() {
  QFileDialog::Options options;
#ifdef DETECTED_OS_APPLE
  options |= QFileDialog::DontUseNativeDialog;
#endif
  QString selectedFilter;

  QSettings settings;
  QString strLastDir = settings.value("Folders/Server1DTFilename", ".").toString();

  QString fileName = QFileDialog::getOpenFileName(this, "Select 1D Transfer function",
             strLastDir, "1D Transfer Functions (*.1dt)", &selectedFilter, options);

  if (!fileName.isEmpty()) {
    // add png as the default filetype if the user forgot to enter one
    if (SysTools::GetExt(fileName.toStdString()) == "")
      fileName = fileName + ".png";

    settings.setValue("Folders/Server1DTFilename", QFileInfo(fileName).absoluteDir().path());
    edit1DTFile->setText(fileName);
  }
}

void DatasetServerDialog::selectVolFile() {
  QFileDialog::Options options;
#ifdef DETECTED_OS_APPLE
  options |= QFileDialog::DontUseNativeDialog;
#endif
  QString selectedFilter;

  QSettings settings;
  QString strLastDir = settings.value("Folders/ServerVolFilename", ".").toString();

  QString fileName = QFileDialog::getOpenFileName(this, "Select Volume",
             strLastDir, "All Files (*.*)", &selectedFilter, options);

  if (!fileName.isEmpty()) {
    // add png as the default filetype if the user forgot to enter one
    if (SysTools::GetExt(fileName.toStdString()) == "")
      fileName = fileName + ".png";

    settings.setValue("Folders/ServerVolFilename", QFileInfo(fileName).absoluteDir().path());
    editVolFile->setText(fileName);
  }
}