Isis 3 Programmer Reference
BlinkTool.cpp
1 #include "BlinkTool.h"
2 
3 #include "FileName.h"
4 #include "IString.h"
5 #include "MainWindow.h"
6 #include "MdiCubeViewport.h"
7 
8 #include <QDialog>
9 #include <QDoubleSpinBox>
10 #include <QEvent>
11 #include <QHBoxLayout>
12 #include <QListWidgetItem>
13 #include <QMenu>
14 #include <QPainter>
15 #include <QPushButton>
16 #include <QSettings>
17 #include <QSplitter>
18 #include <QTimer>
19 #include <QToolBar>
20 #include <QToolButton>
21 #include <QVBoxLayout>
22 
23 namespace Isis {
24 
31  BlinkTool::BlinkTool(QWidget *parent) : Tool(parent) {
32  // Create the blink window
33  p_dialog = new QDialog(parent);
34  p_dialog->setWindowTitle("Blink Comparator");
35  p_dialog->setSizeGripEnabled(true);
37  p_blinkWindow->setMinimumSize(492, 492);
38  p_blinkWindow->setBackgroundRole(QPalette::Shadow);
39  //p_blinkWindow->installEventFilter(this);
40 
41  p_listWidget = new QListWidget(p_dialog);
42  p_listWidget->setMinimumHeight(100);
43  connect(p_listWidget, SIGNAL(itemActivated(QListWidgetItem *)),
44  this, SLOT(toggleLink(QListWidgetItem *)));
45  connect(p_listWidget, SIGNAL(currentRowChanged(int)),
46  this, SLOT(updateWindow()));
47 
48  p_splitter = new QSplitter(Qt::Vertical, p_dialog);
49  p_splitter->addWidget(p_blinkWindow);
50  p_splitter->addWidget(p_listWidget);
51  p_splitter->setCollapsible(0, false);
52  p_splitter->setCollapsible(1, false);
53  p_splitter->setStretchFactor(0, 0);
54  p_splitter->setStretchFactor(1, 1);
55 
56  QWidget *buttons = new QWidget(p_dialog);
57 
58  QVBoxLayout *layout = new QVBoxLayout();
59 // layout->addWidget(p_blinkWindow,2);
60 // layout->addWidget(p_listWidget,1);
61  layout->addWidget(p_splitter, 1);
62  layout->addWidget(buttons, 0);
63  p_dialog->setLayout(layout);
64 
65  QToolButton *reverse = new QToolButton(buttons);
66  reverse->setIcon(QPixmap(toolIconDir() + "/blinkReverse.png"));
67  reverse->setIconSize(QSize(22, 22));
68  reverse->setShortcut(Qt::CTRL + Qt::Key_Delete);
69  reverse->setToolTip("Previous");
70  QString text = "<b>Function:</b> Show previous linked viewport and \
71  stop automatic timed blinking \
72  <p><b>Shortcut:</b> Ctrl+Delete</p>";
73  reverse->setWhatsThis(text);
74  connect(reverse, SIGNAL(released()), this, SLOT(reverse()));
75 
76  QToolButton *stop = new QToolButton(buttons);
77  stop->setIcon(QPixmap(toolIconDir() + "/blinkStop.png"));
78  stop->setIconSize(QSize(22, 22));
79  stop->setToolTip("Stop");
80  text = "<b>Function:</b> Stop automatic timed blinking";
81  stop->setWhatsThis(text);
82  connect(stop, SIGNAL(released()), this, SLOT(stop()));
83 
84  QToolButton *start = new QToolButton(buttons);
85  start->setIcon(QPixmap(toolIconDir() + "/blinkStart.png"));
86  start->setIconSize(QSize(22, 22));
87  start->setToolTip("Start");
88  text = "<b>Function:</b> Start automatic timed blinking. Cycles \
89  through linked viewports at variable rate";
90  start->setWhatsThis(text);
91  connect(start, SIGNAL(released()), this, SLOT(start()));
92 
93  QToolButton *forward = new QToolButton(buttons);
94  forward->setIcon(QPixmap(toolIconDir() + "/blinkAdvance.png"));
95  forward->setIconSize(QSize(22, 22));
96  forward->setToolTip("Next");
97  forward->setShortcut(Qt::Key_Delete);
98  text = "<b>Function:</b> Show next linked viewport and stop \
99  automatic timed blinking \
100  <p><b>Shortcut:</b> Delete</p>";
101  forward->setWhatsThis(text);
102  connect(forward, SIGNAL(released()), this, SLOT(advance()));
103 
104  p_timeBox = new QDoubleSpinBox(buttons);
105  p_timeBox->setMinimum(0.1);
106  p_timeBox->setMaximum(5.0);
107  p_timeBox->setDecimals(1);
108  p_timeBox->setSingleStep(0.1);
109  p_timeBox->setValue(0.5);
110  p_timeBox->setToolTip("iTime Delay");
111  text = "<b>Function:</b> Change automatic blink rate between " +
112  QString::number(p_timeBox->minimum()) + " and " +
113  QString::number(p_timeBox->maximum()) + " seconds";
114  p_timeBox->setWhatsThis(text);
115 
116  QPushButton *close = new QPushButton("Close", buttons);
117  close->setDefault(false);
118  close->setAutoDefault(false);
119  connect(close, SIGNAL(released()), p_dialog, SLOT(hide()));
120  connect(close, SIGNAL(released()), this, SLOT(stop()));
121 
122  QHBoxLayout *hlayout = new QHBoxLayout();
123  hlayout->addWidget(reverse);
124  hlayout->addWidget(stop);
125  hlayout->addWidget(start);
126  hlayout->addWidget(forward);
127  hlayout->addWidget(p_timeBox);
128  hlayout->addStretch(1);
129  hlayout->addWidget(close);
130  buttons->setLayout(hlayout);
131 
132  // Create the action to bring up the blink window
133  p_action = new QAction(parent);
134  p_action->setShortcut(Qt::Key_K);
135  p_action->setText("&Blink ...");
136  p_action->setIcon(QPixmap(toolIconDir() + "/blink.png"));
137  p_action->setToolTip("Blink");
138  text =
139  "<b>Function:</b> Opens a blink comparator for linked viewports. \
140  <p><b>Shortcut:</b>K</p>";
141  p_action->setWhatsThis(text);
142  p_action->setEnabled(false);
143  connect(p_action, SIGNAL(triggered()), p_dialog, SLOT(show()));
144 
145  p_timerOn = false;
146  readSettings();
147  p_blinkWindow->installEventFilter(this);
148  }
149 
150 
157  void BlinkTool::addTo(QMenu *menu) {
158  menu->addAction(p_action);
159  }
160 
161 
169  perm->addAction(p_action);
170  }
171 
172 
178  QString unlinkedIcon = FileName("$base/icons/unlinked.png").expanded();
179  static QIcon unlinked(unlinkedIcon);
180  QString linkedIcon = FileName("$base/icons/linked.png").expanded();
181  static QIcon linked(linkedIcon);
182  p_listWidget->clear();
183  for(int i = 0; i < (int)cubeViewportList()->size(); i++) {
184  MdiCubeViewport *d = (*(cubeViewportList()))[i];
185  disconnect(d, SIGNAL(linkChanging(bool)), this, 0);
186  disconnect(d, SIGNAL(windowTitleChanged()), this, 0);
187  QListWidgetItem *item = new QListWidgetItem(p_listWidget);
188  item->setText(d->parentWidget()->windowTitle());
189  if(d->isLinked()) {
190  item->setIcon(linked);
191  }
192  else {
193  item->setIcon(unlinked);
194  }
195  connect(d, SIGNAL(linkChanging(bool)), this, SLOT(updateTool()));
196  connect(d, SIGNAL(windowTitleChanged()), this, SLOT(updateTool()));
197  }
198 
199  if(cubeViewport() == NULL) {
200  p_action->setEnabled(false);
201  }
202  else if(cubeViewportList()->size() <= 1) {
203  p_action->setEnabled(false);
204  }
205  else {
206  p_action->setEnabled(true);
207  }
208  }
209 
210 
217  void BlinkTool::toggleLink(QListWidgetItem *item) {
218  for(int i = 0; i < p_listWidget->count(); i++) {
219  if(p_listWidget->item(i) == item) {
220  MdiCubeViewport *d = (*(cubeViewportList()))[i];
221  d->setLinked(!d->isLinked());
222  return;
223  }
224  }
225  }
226 
227 
233  p_timerOn = false;
234 
235  int row = p_listWidget->currentRow() - 1;
236  for(int i = 0; i < (int)cubeViewportList()->size(); i++) {
237  if(row < 0) row = (int)cubeViewportList()->size() - 1;
238  MdiCubeViewport *d = (*(cubeViewportList()))[row];
239  if(d->isLinked()) {
240  p_listWidget->setCurrentRow(row);
241  p_listWidget->scrollToItem(p_listWidget->currentItem());
242  break;
243  }
244  row--;
245  }
246  }
247 
248 
254  p_timerOn = false;
255  }
256 
257 
263  if(p_timerOn) return;
264  p_timerOn = true;
265  int msec = (int)(p_timeBox->value() * 1000.0);
266  QTimer::singleShot(msec, this, SLOT(timeout()));
267  }
268 
274  p_timerOn = false;
275 
276  int row = p_listWidget->currentRow() + 1;
277  for(int i = 0; i < (int)cubeViewportList()->size(); i++) {
278  if(row >= (int) cubeViewportList()->size()) row = 0;
279  MdiCubeViewport *d = (*(cubeViewportList()))[row];
280  if(d->isLinked()) {
281  p_listWidget->setCurrentRow(row);
282  p_listWidget->scrollToItem(p_listWidget->currentItem());
283  break;
284  }
285  row++;
286  }
287  }
288 
289 
295  if(p_timerOn) {
296  advance();
297  start();
298  }
299  }
300 
301 
307  p_blinkWindow->repaint();
308  }
309 
310 
317  QSettings settings(MainWindow::settingsFileName("BlinkTool"), QSettings::NativeFormat);
318  settings.setValue("rate", p_timeBox->value());
319  settings.setValue("size", p_dialog->size());
320  settings.setValue("pos", p_dialog->pos());
321  settings.setValue("geom", p_dialog->saveGeometry());
322  settings.setValue("splitterState", p_splitter->saveState());
323  }
324 
325 
332  QSettings settings(MainWindow::settingsFileName("BlinkTool"), QSettings::NativeFormat);
333  double rate = settings.value("rate", 0.5).toDouble();
334 
335  p_dialog->restoreGeometry(settings.value("geom").toByteArray());
336  p_splitter->restoreState(settings.value("splitterState").toByteArray());
337 
338  if (!settings.value("pos").toPoint().isNull())
339  p_dialog->move(settings.value("pos").toPoint());
340 
341  p_dialog->resize(settings.value("size", QSize(492, 492)).toSize());
342 
343  p_timeBox->setValue(rate);
344  }
345 
346 
357  bool BlinkTool::eventFilter(QObject *o, QEvent *e) {
358  if(o != p_blinkWindow) return false;
359  if(e->type() == QEvent::Hide) writeSettings();
360  if(e->type() != QEvent::Paint) return false;
361 
362  static QPixmap lastPixmap;
363  int row = p_listWidget->currentRow();
364  if(row != -1) {
365  MdiCubeViewport *d = (*(cubeViewportList()))[row];
366  lastPixmap = d->pixmap();
367  }
368 
369  int dx = lastPixmap.width() - p_blinkWindow->width();
370  int x = 0;
371  int sx = dx / 2;
372  if(dx < 0) {
373  x = -dx / 2;
374  sx = 0;
375  }
376  int dy = lastPixmap.height() - p_blinkWindow->height();
377  int y = 0;
378  int sy = dy / 2;
379  if(dy < 0) {
380  y = -dy / 2;
381  sy = 0;
382  }
383 
384  QPainter p(p_blinkWindow);
385  p.drawPixmap(x, y, lastPixmap, sx, sy, -1, -1);
386 
387  return true;
388  }
389 }
Cube display widget for certain Isis MDI applications.
QString toolIconDir() const
returns the path to the icon directory.
Definition: Tool.h:127
File name manipulation and expansion.
Definition: FileName.h:116
CubeViewportList * cubeViewportList() const
Return the list of cubeviewports.
Definition: Tool.cpp:390
QPixmap pixmap()
Returns the pixmap.
Definition: CubeViewport.h:411
void setLinked(bool b)
Change the linked state of the viewport.
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:212
bool isLinked() const
Is the viewport linked with other viewports.
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
Base class for the Qisis tools.
Definition: Tool.h:81
MdiCubeViewport * cubeViewport() const
Return the current cubeviewport.
Definition: Tool.h:211