USGS

Isis 3.0 Object Programmers' Reference

Home

MosaicControlNetTool.cpp
1 #include "MosaicControlNetTool.h"
2 
3 #include <iostream>
4 
5 #include <QMenu>
6 
7 #include "Control.h"
8 #include "ControlList.h"
9 #include "ControlNet.h"
10 #include "ControlNetGraphicsItem.h"
11 #include "Directory.h"
12 #include "FileDialog.h"
13 #include "FileName.h"
14 #include "IException.h"
15 #include "Image.h"
16 #include "ImageDisplayProperties.h"
17 #include "ImageList.h"
18 #include "IString.h"
19 #include "MosaicControlNetToolMovementConfigDialog.h"
20 #include "MosaicSceneWidget.h"
21 #include "Project.h"
22 #include "PvlObject.h"
23 #include "SpecialPixel.h"
24 
25 namespace Isis {
26 
33  MosaicTool(scene) {
34  m_controlNet = NULL;
35  m_controlNetGraphics = NULL;
36  m_loadControlNetButton = NULL;
37  m_displayControlNetButton = NULL;
38  m_displayConnectivity = NULL;
39  m_closeNetwork = NULL;
40  m_controlNetFileLabel = NULL;
41  m_randomizeColors = NULL;
42 
44  m_measureCount = 10;
45  m_residualMagnitude = 5.0;
46 
47  // Create the action buttons
48 
49  m_displayControlNetButton = new QPushButton("Display");
50  m_displayControlNetButton->setCheckable(true);
51  m_displayControlNetButton->setEnabled(false);
52  m_displayControlNetButton->setToolTip("Toggle the visibility of the "
53  "control points and movement arrows in the network.");
54  connect(m_displayControlNetButton, SIGNAL(clicked()), this, SLOT(displayControlNet()));
55  connect(m_displayControlNetButton, SIGNAL(destroyed(QObject *)),
56  this, SLOT(objectDestroyed(QObject *)));
57 
58  m_displayConnectivity = new QPushButton("Color Islands");
59  m_displayConnectivity->setToolTip("Color cubes the same if the control "
60  "network has a connection between them");
61  connect(m_displayConnectivity, SIGNAL(clicked()), this, SLOT(displayConnectivity()));
62  connect(m_displayConnectivity, SIGNAL(destroyed(QObject *)),
63  this, SLOT(objectDestroyed(QObject *)));
64  m_displayConnectivity->setEnabled(false);
65 
66  m_randomizeColors = new QPushButton("Color Images");
67  m_randomizeColors->setToolTip("Color all cubes differently");
68  connect(m_randomizeColors, SIGNAL(clicked()), this, SLOT(randomizeColors()));
69  connect(m_randomizeColors, SIGNAL(destroyed(QObject *)),
70  this, SLOT(objectDestroyed(QObject *)));
71 
72  m_configMovement = new QPushButton(tr("Configure Movement Display"));
73  m_configMovement->setToolTip(tr("Show arrow from the apriori surface point to "
74  "the adjusted surface point for each control point with this "
75  "information"));
76  connect(m_configMovement, SIGNAL(clicked()), this, SLOT(configMovement()));
77  connect(m_configMovement, SIGNAL(destroyed(QObject *)),
78  this, SLOT(objectDestroyed(QObject *)));
79 
80  m_closeNetwork = new QPushButton("Close Network");
81  m_closeNetwork->setEnabled(false);
82  m_closeNetwork->setVisible(false);
83  m_closeNetwork->setToolTip("Close the currently open control network");
84  connect(m_closeNetwork, SIGNAL(clicked()), this, SLOT(closeNetwork()));
85  connect(m_closeNetwork, SIGNAL(destroyed(QObject *)),
86  this, SLOT(objectDestroyed(QObject *)));
87 
88  m_loadControlNetButton = new QPushButton("Open Network");
89  m_loadControlNetButton->setToolTip("Open and load a control network");
90  connect(m_loadControlNetButton, SIGNAL(clicked()),
91  this, SLOT(openControlNet()));
92  connect(m_loadControlNetButton, SIGNAL(destroyed(QObject *)),
93  this, SLOT(objectDestroyed(QObject *)));
94 
95  m_controlNetFileLabel = new QLabel;
96  m_controlNetFileLabel->setToolTip("The filename of the currently open "
97  "control network");
98  connect(m_controlNetFileLabel, SIGNAL(destroyed(QObject *)),
99  this, SLOT(objectDestroyed(QObject *)));
100  }
101 
102 
103  MosaicControlNetTool::~MosaicControlNetTool() {
104  m_controlNetGraphics = NULL; // the scene cleans/cleaned this up
105 
106  delete m_loadControlNetButton;
107  delete m_displayControlNetButton;
108  delete m_displayConnectivity;
109  delete m_configMovement;
110  delete m_closeNetwork;
111  delete m_controlNetFileLabel;
112  delete m_randomizeColors;
113 
114  closeNetwork();
115  }
116 
117 
118  Image *MosaicControlNetTool::takeImage(
119  QString sn, ImageList &images) {
120  if (m_controlNet && m_controlNetGraphics) {
121  QString filename = m_controlNetGraphics->snToFileName(sn);
122 
123  for(int i = 0; i < images.size(); i++) {
124  Image *image = images[i];
125 
126  if (image->fileName() == filename) {
127  return images.takeAt(i);
128  }
129  }
130  }
131 
132  return NULL;
133  }
134 
135 
136  PvlObject MosaicControlNetTool::toPvl() const {
137  PvlObject obj(projectPvlObjectName());
138 
139  obj += PvlKeyword("FileName", m_controlNetFile);
140  obj += PvlKeyword("Visible",
141  Isis::toString((int)(m_controlNetGraphics && m_controlNetGraphics->isVisible())));
142  obj += PvlKeyword("Movement", toString(m_movementArrowColorSource));
143 
144  if (maxMovementColorMeasureCount() != -1) {
145  obj += PvlKeyword("MovementColorMaxMeasureCount", Isis::toString(m_measureCount));
146  }
147 
149  obj += PvlKeyword("MovementColorMaxResidualMagnitude",
151  }
152 
153  return obj;
154  }
155 
156 
157  void MosaicControlNetTool::fromPvl(const PvlObject &obj) {
158  m_controlNetFile = obj["FileName"][0];
159  if (m_controlNetFile == "Null")
160  m_controlNetFile = "";
161 
162  if (obj.hasKeyword("Movement")) {
164  }
165 
166  if (obj.hasKeyword("MovementColorMaxMeasureCount")) {
167  m_measureCount = toInt(obj["MovementColorMaxMeasureCount"][0]);
168  }
169 
170  if (obj.hasKeyword("MovementColorMaxResidualMagnitude")) {
171  m_residualMagnitude = toDouble(obj["MovementColorMaxResidualMagnitude"][0]);
172  }
173 
174  loadNetwork();
175 
176  if (m_controlNetGraphics && m_displayControlNetButton) {
177  m_displayControlNetButton->setChecked( toBool(obj["Visible"][0]) );
179  }
180  }
181 
182 
183  QString MosaicControlNetTool::projectPvlObjectName() const {
184  return "MosaicControlNetTool";
185  }
186 
187 
201  int maxMeasureCount, double maxResidualMagnitude) {
202  m_movementArrowColorSource = colorSource;
203  m_measureCount = maxMeasureCount;
204  m_residualMagnitude = maxResidualMagnitude;
205 
206  if (m_controlNetGraphics) {
207  m_controlNetGraphics->setArrowsVisible(m_movementArrowColorSource != NoMovement,
210  }
211  }
212 
213 
219  }
220 
221 
226  int result = -1;
227 
228  if (m_measureCount > 0)
229  result = m_measureCount;
230 
231  return result;
232  }
233 
234 
240  double result = Null;
241 
243  result = m_residualMagnitude;
244 
245  return result;
246  }
247 
248 
253  QString result;
254 
255  switch (source) {
256  case NoMovement:
257  result = "No movement arrows";
258  break;
259 
260  case NoColor:
261  result = "Black movement arrows";
262  break;
263 
264  case MeasureCount:
265  result = "Movement arrows colored by measure count";
266  break;
267 
268  case ResidualMagnitude:
269  result = "Movement arrows colored by residual magnitude";
270  break;
271  }
272 
273  return result;
274  }
275 
276 
281  QString string) {
283 
284  for (int i = 0; i < NUM_MOVEMENT_COLOR_SOURCE_VALUES; i++) {
285  if (string.toLower() == toString((MovementColorSource)i).toLower()) {
286  result = (MovementColorSource)i;
287  }
288  }
289 
290  return result;
291  }
292 
293 
302  QAction *action = new QAction(this);
303  action->setIcon(getIcon("HILLBLU_molecola.png"));
304  action->setToolTip("Control Net (c)");
305  action->setShortcut(Qt::Key_C);
306  QString text =
307  "<b>Function:</b> Display and analyze a control network<br><br>"
308  "This tool shows you all of the control points in your network for "
309  "which a latitude/longitude can be calculated. The control points are "
310  "shown as color-coded '+' marks. The control points have a right-click "
311  "menu and information about them can be seen just by hovering over them."
312  "<p><b>Shortcut:</b> c</p> ";
313  action->setWhatsThis(text);
314  return action;
315  }
316 
317 
319  // Put the buttons in a horizontal orientation
320  QHBoxLayout *actionLayout = new QHBoxLayout();
321 
322  if (m_displayControlNetButton)
323  actionLayout->addWidget(m_displayControlNetButton);
324 
325  if (m_displayConnectivity)
326  actionLayout->addWidget(m_displayConnectivity);
327 
328  if (m_randomizeColors)
329  actionLayout->addWidget(m_randomizeColors);
330 
331  if (m_configMovement)
332  actionLayout->addWidget(m_configMovement);
333 
334  if (m_closeNetwork)
335  actionLayout->addWidget(m_closeNetwork);
336 
337  if (m_loadControlNetButton)
338  actionLayout->addWidget(m_loadControlNetButton);
339 
340  if (m_controlNetFileLabel)
341  actionLayout->addWidget(m_controlNetFileLabel);
342 
343  actionLayout->setMargin(0);
344 
345  QWidget *toolBarWidget = new QWidget;
346  toolBarWidget->setLayout(actionLayout);
347 
348  return toolBarWidget;
349  }
350 
351 
357  new MosaicControlNetToolMovementConfigDialog(this, qobject_cast<QWidget *>(parent()));
358  configDialog->setAttribute(Qt::WA_DeleteOnClose);
359  configDialog->show();
360  }
361 
362 
367  if (isActive() && m_controlNetFile == "") {
368  openControlNet();
369  }
370  }
371 
372 
378  if (m_controlNet) {
379  ImageList images = getWidget()->images();
380 
381  QList<QColor> colorsUsed;
382 
383  QList< QList<QString> > serialConns =
384  m_controlNet->GetSerialConnections();
385 
386  QList<QString> island;
387  foreach(island, serialConns) {
388  QColor color;
389 
390  QString cubeSn;
391  foreach(cubeSn, island) {
392  Image *image = takeImage(cubeSn, images);
393 
394  if (image) {
395  while(!color.isValid()) {
396  QColor displayColor = image->displayProperties()->getValue(
397  ImageDisplayProperties::Color).value<QColor>();
398 
399  if (colorsUsed.indexOf(displayColor) == -1) {
400  colorsUsed.append(displayColor);
401  color = displayColor;
402  }
403  else {
404  QColor ranColor = ImageDisplayProperties::randomColor();
405 
406  if (colorsUsed.indexOf(ranColor) == -1) {
407  colorsUsed.append(ranColor);
408  color = ranColor;
409  }
410  }
411  }
412 
413  image->displayProperties()->setColor(color);
414  }
415  }
416  }
417  }
418  }
419 
420 
426  if (m_controlNetGraphics) {
427  getWidget()->getScene()->removeItem(m_controlNetGraphics);
428 
429  delete m_controlNetGraphics;
430  }
431 
432  if (m_controlNet) {
433  delete m_controlNet;
434  m_controlNet = NULL;
435  }
436 
437  if (m_displayControlNetButton)
438  m_displayControlNetButton->setChecked(false);
439 
440  if (m_displayControlNetButton)
441  m_displayControlNetButton->setEnabled(false);
442 
443  if (m_displayConnectivity)
444  m_displayConnectivity->setEnabled(false);
445 
446  if (m_closeNetwork) {
447  m_closeNetwork->setEnabled(false);
448  m_closeNetwork->setVisible(false);
449  }
450 
451  if (m_loadControlNetButton) {
452  m_loadControlNetButton->setEnabled(true);
453  m_loadControlNetButton->setVisible(true);
454  }
455 
456  if (m_controlNetFileLabel)
457  m_controlNetFileLabel->setText("");
458 
459  m_controlNetFile = "";
460  }
461 
462 
467  if (obj == m_loadControlNetButton)
468  m_loadControlNetButton = NULL;
469  else if (obj == m_displayControlNetButton)
470  m_displayControlNetButton = NULL;
471  else if (obj == m_displayConnectivity)
472  m_displayConnectivity = NULL;
473  else if (obj == m_closeNetwork)
474  m_closeNetwork = NULL;
475  else if (obj == m_controlNetGraphics)
476  m_controlNetGraphics = NULL;
477  else if (obj == m_configMovement)
478  m_configMovement = NULL;
479  else if (obj == m_controlNetFileLabel)
480  m_controlNetFileLabel = NULL;
481  else if (obj == m_randomizeColors)
482  m_randomizeColors = NULL;
483  }
484 
485 
491  if (!getWidget()->directory()) {
492  // Bring up a file dialog for user to select their cnet file.
493  QString netFile = FileDialog::getOpenFileName(getWidget(),
494  "Select Control Net. File",
495  QDir::current().dirName() + "/",
496  "Control Networks (*.net);;All Files (*.*)");
497 
498  //--------------------------------------------------------------
499  // if the file is not empty attempt to load in the control points
500  // for each mosaic item
501  //---------------------------------------------------------------
502  if (!netFile.isEmpty()) {
503  FileName controlNetFile(netFile);
504  m_controlNetFile = controlNetFile.expanded();
505  loadNetwork();
506 
507  if (m_displayControlNetButton)
508  m_displayControlNetButton->setChecked(true);
509  }
510  }
511  else {
512  QList<ControlList *> controls = getWidget()->directory()->project()->controls();
513  bool ok = false;
514  if (controls.count() > 0) {
515  QMap<Control *, QString> cnetChoices;
516  foreach (ControlList *list, controls) {
517  foreach (Control *control, *list) {
518  cnetChoices[control] = tr("%1/%2").arg(list->name())
519  .arg(control->displayProperties()->displayName());
520  }
521  }
522 
523  QStringList cnetNames = cnetChoices.values();
524  qSort(cnetNames);
525 
526  QString selected = QInputDialog::getItem(NULL, tr("Select control network"),
527  tr("Select control network"), cnetNames, 0, false,
528  &ok);
529  if (ok && !selected.isEmpty()) {
530  m_controlNetFile = cnetChoices.key(selected)->fileName();
531  loadNetwork();
532  if (m_displayControlNetButton)
533  m_displayControlNetButton->setChecked(true);
534  }
535  }
536  else if (controls.count() == 1 && controls.at(0)->count() == 1) {
537  m_controlNetFile = controls.first()->first()->fileName();
538  loadNetwork();
539  if (m_displayControlNetButton)
540  m_displayControlNetButton->setChecked(true);
541  }
542  }
543  }
544 
545 
551  QString netFile = m_controlNetFile;
552  closeNetwork();
553  m_controlNetFile = netFile;
554  m_controlNetFileLabel->setText( QFileInfo(netFile).fileName() );
555 
556  if (m_controlNetFile.size() > 0) {
557  try {
558  m_controlNet = new ControlNet(
559  m_controlNetFile);
560  m_controlNetGraphics = new ControlNetGraphicsItem(m_controlNet,
561  getWidget());
562 
565 
566  connect(m_controlNetGraphics, SIGNAL(destroyed(QObject *)),
567  this, SLOT(objectDestroyed(QObject *)));
568  }
569  catch(IException &e) {
570  QString message = "Invalid control network.\n";
571  message += e.toString();
572  QMessageBox::information(getWidget(), "Error", message);
573  return;
574  }
575 
576  if (m_displayControlNetButton)
577  m_displayControlNetButton->setEnabled(true);
578 
579  if (m_displayConnectivity)
580  m_displayConnectivity->setEnabled(true);
581 
582  if (m_closeNetwork) {
583  m_closeNetwork->setEnabled(true);
584  m_closeNetwork->setVisible(true);
585  }
586 
587  if (m_loadControlNetButton) {
588  m_loadControlNetButton->setEnabled(false);
589  m_loadControlNetButton->setVisible(false);
590  }
591  }
592  }
593 
594 
595  void MosaicControlNetTool::randomizeColors() {
596  foreach (Image *image, getWidget()->images()) {
598  }
599  }
600 
601 
607  if (m_controlNetGraphics && m_displayControlNetButton)
608  m_controlNetGraphics->setVisible(m_displayControlNetButton->isChecked());
609  }
610 
611 
612  void MosaicControlNetTool::mouseButtonRelease(QPointF point, Qt::MouseButton s) {
613  if (!isActive() || !m_controlNet) return;
614 
615  // Find closes point
616 
617  }
618 }