Viewer
display a stack of images
viewer.cpp
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00047 #include <iostream>
00048 #include <cassert>
00049 
00050 #include <qgl.h>
00051 
00052 #include <jwsc/image/image.h>
00053 #include <jwsc/imgblock/imgblock.h>
00054 #include <jwsc/imgblock/imgblock_io.h>
00055 
00056 #include <jwsc++/base/option.h>
00057 #include <jwsc++/base/exception.h>
00058 
00059 #include "viewer.h"
00060 
00061 using namespace jwsc;
00062 
00063 
00065 jwscxx::base::Options* opts = 0;
00066 
00067 
00077 Viewer::Viewer(QWidget* parent, const char* name)
00078     : QWidget(parent, name)
00079 {
00080     fnames = 0;
00081 
00082     QAction* new_viewer_action = new QAction("&New Viewer", CTRL+Key_N, this);
00083     QObject::connect(new_viewer_action, SIGNAL(activated()), this, 
00084             SLOT(new_viewer()));
00085 
00086     QAction* file_open_action = new QAction("&Open", CTRL+Key_O, this);
00087     QObject::connect(file_open_action, SIGNAL(activated()), this, 
00088             SLOT(file_open()));
00089 
00090     QAction* file_close_action = new QAction("&Close", CTRL+Key_L, this);
00091     QObject::connect(file_close_action, SIGNAL(activated()), this, 
00092             SLOT(file_close()));
00093 
00094     QAction* set_xz_view_action = new QAction("&XZ View", CTRL+Key_X, this);
00095     set_xz_view_action->setToggleAction(true);
00096     QObject::connect(set_xz_view_action, SIGNAL(toggled(bool)), this, 
00097             SLOT(set_xz_view(bool)));
00098 
00099     QAction* set_yz_view_action = new QAction("&YZ View", CTRL+Key_Y, this);
00100     set_yz_view_action->setToggleAction(true);
00101     QObject::connect(set_yz_view_action, SIGNAL(toggled(bool)), this, 
00102             SLOT(set_yz_view(bool)));
00103 
00104     QAction* set_draw_lines_action = new QAction("&Lines", CTRL+Key_I, this);
00105     set_draw_lines_action->setToggleAction(true);
00106 
00107     QPopupMenu* file = new QPopupMenu(this);
00108     new_viewer_action->addTo(file);
00109     file_open_action->addTo(file);
00110     file_close_action->addTo(file);
00111 
00112     QPopupMenu* view = new QPopupMenu(this);
00113     set_xz_view_action->addTo(view);
00114     set_yz_view_action->addTo(view);
00115     set_draw_lines_action->addTo(view);
00116 
00117     QMenuBar* m = new QMenuBar(this);
00118     m->insertItem("&File", file);
00119     m->insertItem("&View", view);
00120 
00121     QSplitter* xy_yz_splitter = new QSplitter(Qt::Horizontal, this);
00122     QSplitter* xy_xz_splitter = new QSplitter(Qt::Vertical, xy_yz_splitter);
00123 
00124     QWidget* xy_panel = new QWidget(xy_xz_splitter);
00125     QWidget* xz_panel = new QWidget(xy_xz_splitter);
00126     QWidget* yz_panel = new QWidget(xy_yz_splitter);
00127 
00128     /* XY Panel. */
00129     gl_xy_scroll_view = new QScrollView(xy_panel);
00130     gl_xy_scroll_view->setVScrollBarMode(QScrollView::AlwaysOn);
00131 
00132     xy_view = new GLImagePanelXY(gl_xy_scroll_view->viewport(), "xy_view");
00133     xy_view->updateGL();
00134     QObject::connect(xy_view, 
00135             SIGNAL(mouse_position_changed(int, int, int, const jwsc::Pixel_f*)), 
00136             this, SLOT(update_xy_line_edit(int, int, int, const jwsc::Pixel_f*)));
00137     QObject::connect(set_draw_lines_action, SIGNAL(toggled(bool)), xy_view, 
00138             SLOT(set_draw_line(bool)));
00139 
00140     gl_xy_scroll_view->addChild(xy_view);
00141 
00142     xy_line_edit = new QLineEdit(xy_panel);
00143     xy_line_edit->setReadOnly(TRUE);
00144 
00145     /* XZ Panel. */
00146     gl_xz_scroll_view = new QScrollView(xz_panel);
00147     gl_xz_scroll_view->setVScrollBarMode(QScrollView::AlwaysOn);
00148 
00149     xz_view = new GLImagePanelXZ(gl_xz_scroll_view->viewport(), "xz_view");
00150     xz_view->updateGL();
00151     QObject::connect(set_draw_lines_action, SIGNAL(toggled(bool)), xz_view, 
00152             SLOT(set_draw_line(bool)));
00153 
00154     gl_xz_scroll_view->addChild(xz_view);
00155 
00156     xz_line_edit = new QLineEdit(xz_panel);
00157     xz_line_edit->setReadOnly(TRUE);
00158 
00159     xy_fname_line_edit = new QLineEdit(xy_panel);
00160     xy_fname_line_edit->setReadOnly(TRUE);
00161 
00162     /* YZ Panel. */
00163     gl_yz_scroll_view = new QScrollView(yz_panel);
00164     gl_yz_scroll_view->setVScrollBarMode(QScrollView::AlwaysOn);
00165 
00166     yz_view = new GLImagePanelYZ(gl_yz_scroll_view->viewport(), "yz_view");
00167     yz_view->updateGL();
00168     QObject::connect(set_draw_lines_action, SIGNAL(toggled(bool)), yz_view, 
00169             SLOT(set_draw_line(bool)));
00170 
00171     gl_yz_scroll_view->addChild(yz_view);
00172 
00173     yz_line_edit = new QLineEdit(yz_panel);
00174     yz_line_edit->setReadOnly(TRUE);
00175 
00176     /* X-axis Selector. */
00177     x_axis_slider = new QSlider(xy_panel);
00178     x_axis_slider->setRange(0, 0);
00179     x_axis_slider->setOrientation(Qt::Horizontal);
00180 
00181     x_axis_spin_box = new QSpinBox(xy_panel);
00182     x_axis_spin_box->setRange(0, 0);
00183 
00184     /* Y-axis Selector. */
00185     y_axis_slider = new QSlider(xy_panel);
00186     y_axis_slider->setRange(0, 0);
00187 
00188     y_axis_spin_box = new QSpinBox(xy_panel);
00189     y_axis_spin_box->setRange(0, 0);
00190 
00191     /* Z-axis Selector. */
00192     xz_z_axis_slider = new QSlider(xz_panel);
00193     xz_z_axis_slider->setRange(0, 0);
00194     yz_z_axis_slider = new QSlider(yz_panel);
00195     yz_z_axis_slider->setRange(0, 0);
00196     yz_z_axis_slider->setOrientation(Qt::Horizontal);
00197 
00198     xz_z_axis_spin_box = new QSpinBox(xz_panel);
00199     xz_z_axis_spin_box->setRange(0, 0);
00200     yz_z_axis_spin_box = new QSpinBox(yz_panel);
00201     yz_z_axis_spin_box->setRange(0, 0);
00202 
00203     QObject::connect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00204             this, SLOT(update_xy_fname_line_edit(int)));
00205 
00206     /* XY Layout. */
00207     QHBoxLayout* xy_slider_spin_box_layout = new QHBoxLayout();
00208     xy_slider_spin_box_layout->addWidget(x_axis_slider);
00209     xy_slider_spin_box_layout->addWidget(x_axis_spin_box);
00210 
00211     QHBoxLayout* xy_spin_box_line_edit_layout = new QHBoxLayout();
00212     xy_spin_box_line_edit_layout->addWidget(y_axis_spin_box);
00213     xy_spin_box_line_edit_layout->addWidget(xy_line_edit);
00214 
00215     QHBoxLayout* xy_fname_line_edit_layout = new QHBoxLayout();
00216     xy_fname_line_edit_layout->addWidget(xy_fname_line_edit);
00217 
00218     QGridLayout* xy_panel_layout = new QGridLayout(xy_panel, 4, 2, 10);
00219     xy_panel_layout->addLayout(xy_slider_spin_box_layout, 0, 1);
00220     xy_panel_layout->addWidget(y_axis_slider, 1, 0);
00221     xy_panel_layout->addWidget(gl_xy_scroll_view, 1, 1);
00222     xy_panel_layout->addMultiCellLayout(xy_spin_box_line_edit_layout, 
00223             2, 2, 0, 1);
00224     xy_panel_layout->addMultiCellLayout(xy_fname_line_edit_layout, 3, 3, 0, 1);
00225 
00226     /* XZ Layout. */
00227     QHBoxLayout* xz_spin_box_line_edit_layout = new QHBoxLayout();
00228     xz_spin_box_line_edit_layout->addWidget(xz_z_axis_spin_box);
00229     xz_spin_box_line_edit_layout->addWidget(xz_line_edit);
00230 
00231     QGridLayout* xz_panel_layout = new QGridLayout(xz_panel, 2, 2, 10);
00232     xz_panel_layout->addWidget(xz_z_axis_slider, 0, 0);
00233     xz_panel_layout->addWidget(gl_xz_scroll_view, 0, 1);
00234     xz_panel_layout->addMultiCellLayout(xz_spin_box_line_edit_layout, 
00235             1, 1, 0, 1);
00236 
00237 
00238     /* YZ Layout. */
00239     QHBoxLayout* yz_slider_spin_box_layout = new QHBoxLayout();
00240     yz_slider_spin_box_layout->addWidget(yz_z_axis_slider);
00241     yz_slider_spin_box_layout->addWidget(yz_z_axis_spin_box);
00242 
00243     QGridLayout* yz_panel_layout = new QGridLayout(yz_panel, 3, 1, 10);
00244     yz_panel_layout->addLayout(yz_slider_spin_box_layout, 0, 0);
00245     yz_panel_layout->addWidget(gl_yz_scroll_view, 1, 0);
00246     yz_panel_layout->addWidget(yz_line_edit, 2, 0);
00247 
00248     /* Application Layout. */
00249     QHBoxLayout* top_level_layout = new QHBoxLayout(this, 10);
00250     top_level_layout->setMenuBar(m);
00251     top_level_layout->addWidget(xy_yz_splitter);
00252 
00253     gl_xy_scroll_view->viewport()->setMaximumSize(xy_view->size());
00254     gl_xz_scroll_view->viewport()->setMaximumSize(xz_view->size());
00255     gl_yz_scroll_view->viewport()->setMaximumSize(yz_view->size());
00256 
00257     updating_gl_scroll_view = 0;
00258     mutex = new QMutex(TRUE);
00259 
00260     img_blk = NULL;
00261 
00262     /* By default, turn on the XZ and YZ views and lines. */
00263     set_xz_view_action->toggle();
00264     set_yz_view_action->toggle();
00265     set_draw_lines_action->toggle();
00266 }
00267 
00268 void Viewer::set_xz_view(bool on)
00269 {
00270     int num_imgs, num_rows, num_cols;
00271 
00272     show_xz_view = on;
00273 
00274     if (img_blk == NULL)
00275     {
00276         num_imgs = num_rows = num_cols = 1;
00277     }
00278     else
00279     {
00280         num_imgs = img_blk->num_imgs;
00281         num_rows = img_blk->num_rows;
00282         num_cols = img_blk->num_cols;
00283     }
00284 
00285     xy_view->set_draw_x_line(on);
00286 
00287     if (show_xz_view)
00288     {
00289         xz_view->set_imgblock(img_blk);
00290         y_axis_slider->setRange(0, num_rows - 1);
00291         y_axis_slider->update();
00292         y_axis_spin_box->setRange(0, num_rows - 1);
00293         y_axis_spin_box->update();
00294         xz_z_axis_slider->setRange(0, num_imgs - 1);
00295         xz_z_axis_slider->update();
00296         xz_z_axis_spin_box->setRange(0, num_imgs - 1);
00297         xz_z_axis_spin_box->update();
00298         xz_view->updateGL();
00299         xy_view->updateGL();
00300         gl_xz_scroll_view->viewport()->setMaximumSize(xz_view->size());
00301 
00302         QObject::connect(gl_xy_scroll_view, SIGNAL(contentsMoving(int, int)),
00303                 this, SLOT(update_gl_xz_scroll_view(int, int)));
00304         QObject::connect(gl_xz_scroll_view, SIGNAL(contentsMoving(int, int)),
00305                 this, SLOT(update_gl_xy_scroll_view(int, int)));
00306         QObject::connect(xz_view, 
00307                 SIGNAL(mouse_position_changed(int, int, int, const jwsc::Pixel_f*)),
00308                 this, SLOT(update_xz_line_edit(int, int, int, const jwsc::Pixel_f*)));
00309 
00310         QObject::connect(y_axis_slider, SIGNAL(valueChanged(int)), 
00311                 xy_view, SLOT(set_current_y_index(int)));
00312         QObject::connect(y_axis_slider, SIGNAL(valueChanged(int)), 
00313                 xz_view, SLOT(set_current_y_index(int)));
00314         QObject::connect(y_axis_slider, SIGNAL(valueChanged(int)), 
00315                 yz_view, SLOT(set_current_y_index(int)));
00316         QObject::connect(y_axis_slider, SIGNAL(valueChanged(int)), 
00317                 y_axis_spin_box, SLOT(setValue(int)));
00318         QObject::connect(y_axis_spin_box, SIGNAL(valueChanged(int)), 
00319                 y_axis_slider, SLOT(setValue(int)));
00320 
00321         QObject::connect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00322                 xy_view, SLOT(set_current_z_index(int)));
00323         QObject::connect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00324                 xz_view, SLOT(set_current_z_index(int)));
00325         QObject::connect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00326                 yz_view, SLOT(set_current_z_index(int)));
00327 
00328         QObject::connect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00329                 xz_z_axis_spin_box, SLOT(setValue(int)));
00330         QObject::connect(xz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00331                 xz_z_axis_slider, SLOT(setValue(int)));
00332 
00333         if (show_yz_view)
00334         {
00335             QObject::connect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00336                     xz_z_axis_slider, SLOT(setValue(int)));
00337             QObject::connect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00338                     xz_z_axis_spin_box, SLOT(setValue(int)));
00339             QObject::connect(yz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00340                     xz_z_axis_slider, SLOT(setValue(int)));
00341             QObject::connect(yz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00342                     xz_z_axis_spin_box, SLOT(setValue(int)));
00343             QObject::connect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00344                     yz_z_axis_slider, SLOT(setValue(int)));
00345             QObject::connect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00346                     yz_z_axis_spin_box, SLOT(setValue(int)));
00347             QObject::connect(xz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00348                     yz_z_axis_slider, SLOT(setValue(int)));
00349             QObject::connect(xz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00350                     yz_z_axis_spin_box, SLOT(setValue(int)));
00351         }
00352     }
00353     else
00354     {
00355         xz_view->set_imgblock(NULL);
00356         xz_view->updateGL();
00357         xy_view->updateGL();
00358         gl_xz_scroll_view->viewport()->setMaximumSize(xz_view->size());
00359         y_axis_slider->setRange(0, 0);
00360         y_axis_spin_box->setRange(0, 0);
00361         xz_z_axis_slider->setRange(0, 0);
00362         xz_z_axis_spin_box->setRange(0, 0);
00363         y_axis_slider->update();
00364         y_axis_spin_box->update();
00365         xz_z_axis_slider->update();
00366         xz_z_axis_spin_box->update();
00367 
00368         QObject::disconnect(gl_xy_scroll_view, SIGNAL(contentsMoving(int, int)),
00369                 this, SLOT(update_gl_xz_scroll_view(int, int)));
00370         QObject::disconnect(gl_xz_scroll_view, SIGNAL(contentsMoving(int, int)),
00371                 this, SLOT(update_gl_xy_scroll_view(int, int)));
00372         QObject::disconnect(xz_view, 
00373                 SIGNAL(mouse_position_changed(int, int, int, const jwsc::Pixel_f*)),
00374                 this, SLOT(update_xz_line_edit(int, int, int, const jwsc::Pixel_f*)));
00375 
00376         QObject::disconnect(y_axis_slider, SIGNAL(valueChanged(int)), 
00377                 xy_view, SLOT(set_current_y_index(int)));
00378         QObject::disconnect(y_axis_slider, SIGNAL(valueChanged(int)), 
00379                 xz_view, SLOT(set_current_y_index(int)));
00380         QObject::disconnect(y_axis_slider, SIGNAL(valueChanged(int)), 
00381                 yz_view, SLOT(set_current_y_index(int)));
00382         QObject::disconnect(y_axis_slider, SIGNAL(valueChanged(int)), 
00383                 y_axis_spin_box, SLOT(setValue(int)));
00384         QObject::disconnect(y_axis_spin_box, SIGNAL(valueChanged(int)), 
00385                 y_axis_slider, SLOT(setValue(int)));
00386 
00387         QObject::disconnect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00388                 xy_view, SLOT(set_current_z_index(int)));
00389         QObject::disconnect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00390                 xz_view, SLOT(set_current_z_index(int)));
00391         QObject::disconnect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00392                 yz_view, SLOT(set_current_z_index(int)));
00393 
00394         QObject::disconnect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00395                 xz_z_axis_spin_box, SLOT(setValue(int)));
00396         QObject::disconnect(xz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00397                 xz_z_axis_slider, SLOT(setValue(int)));
00398 
00399         if (show_yz_view)
00400         {
00401             QObject::disconnect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00402                     xz_z_axis_slider, SLOT(setValue(int)));
00403             QObject::disconnect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00404                     xz_z_axis_spin_box, SLOT(setValue(int)));
00405             QObject::disconnect(yz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00406                     xz_z_axis_slider, SLOT(setValue(int)));
00407             QObject::disconnect(yz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00408                     xz_z_axis_spin_box, SLOT(setValue(int)));
00409             QObject::disconnect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00410                     yz_z_axis_slider, SLOT(setValue(int)));
00411             QObject::disconnect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00412                     yz_z_axis_spin_box, SLOT(setValue(int)));
00413             QObject::disconnect(xz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00414                     yz_z_axis_slider, SLOT(setValue(int)));
00415             QObject::disconnect(xz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00416                     yz_z_axis_spin_box, SLOT(setValue(int)));
00417         }
00418     }
00419 }
00420 
00421 void Viewer::set_yz_view(bool on)
00422 {
00423     int num_imgs, num_rows, num_cols;
00424 
00425     show_yz_view = on;
00426 
00427     if (img_blk == NULL)
00428     {
00429         num_imgs = num_rows = num_cols = 1;
00430     }
00431     else
00432     {
00433         num_imgs = img_blk->num_imgs;
00434         num_rows = img_blk->num_rows;
00435         num_cols = img_blk->num_cols;
00436     }
00437 
00438     xy_view->set_draw_y_line(on);
00439 
00440     if (show_yz_view)
00441     {
00442         yz_view->set_imgblock(img_blk);
00443         x_axis_slider->setRange(0, num_cols - 1);
00444         x_axis_slider->update();
00445         x_axis_spin_box->setRange(0, num_cols - 1);
00446         x_axis_spin_box->update();
00447         yz_z_axis_slider->setRange(0, num_imgs - 1);
00448         yz_z_axis_slider->update();
00449         yz_z_axis_spin_box->setRange(0, num_imgs - 1);
00450         yz_z_axis_spin_box->update();
00451         yz_view->updateGL();
00452         xy_view->updateGL();
00453         gl_yz_scroll_view->viewport()->setMaximumSize(yz_view->size());
00454 
00455         QObject::connect(gl_yz_scroll_view, SIGNAL(contentsMoving(int, int)),
00456                 this, SLOT(update_gl_xy_scroll_view(int, int)));
00457         QObject::connect(yz_view, 
00458                 SIGNAL(mouse_position_changed(int, int, int, const jwsc::Pixel_f*)),
00459                 this, SLOT(update_yz_line_edit(int, int, int, const jwsc::Pixel_f*)));
00460 
00461         QObject::connect(x_axis_slider, SIGNAL(valueChanged(int)), 
00462                 xy_view, SLOT(set_current_x_index(int)));
00463         QObject::connect(x_axis_slider, SIGNAL(valueChanged(int)), 
00464                 xz_view, SLOT(set_current_x_index(int)));
00465         QObject::connect(x_axis_slider, SIGNAL(valueChanged(int)), 
00466                 yz_view, SLOT(set_current_x_index(int)));
00467         QObject::connect(x_axis_slider, SIGNAL(valueChanged(int)), 
00468                 x_axis_spin_box, SLOT(setValue(int)));
00469         QObject::connect(x_axis_spin_box, SIGNAL(valueChanged(int)), 
00470                 x_axis_slider, SLOT(setValue(int)));
00471 
00472         QObject::connect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00473                 xy_view, SLOT(set_current_z_index(int)));
00474         QObject::connect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00475                 xz_view, SLOT(set_current_z_index(int)));
00476         QObject::connect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00477                 yz_view, SLOT(set_current_z_index(int)));
00478 
00479         QObject::connect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00480                 yz_z_axis_spin_box, SLOT(setValue(int)));
00481         QObject::connect(yz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00482                 yz_z_axis_slider, SLOT(setValue(int)));
00483 
00484         if (show_xz_view)
00485         {
00486             QObject::connect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00487                     xz_z_axis_slider, SLOT(setValue(int)));
00488             QObject::connect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00489                     xz_z_axis_spin_box, SLOT(setValue(int)));
00490             QObject::connect(yz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00491                     xz_z_axis_slider, SLOT(setValue(int)));
00492             QObject::connect(yz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00493                     xz_z_axis_spin_box, SLOT(setValue(int)));
00494             QObject::connect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00495                     yz_z_axis_slider, SLOT(setValue(int)));
00496             QObject::connect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00497                     yz_z_axis_spin_box, SLOT(setValue(int)));
00498             QObject::connect(xz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00499                     yz_z_axis_slider, SLOT(setValue(int)));
00500             QObject::connect(xz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00501                     yz_z_axis_spin_box, SLOT(setValue(int)));
00502         }
00503     }
00504     else
00505     {
00506         yz_view->set_imgblock(NULL);
00507         yz_view->updateGL();
00508         xy_view->updateGL();
00509         gl_yz_scroll_view->viewport()->setMaximumSize(yz_view->size());
00510         x_axis_slider->setRange(0, 0);
00511         x_axis_spin_box->setRange(0, 0);
00512         yz_z_axis_slider->setRange(0, 0);
00513         yz_z_axis_spin_box->setRange(0, 0);
00514         x_axis_slider->update();
00515         x_axis_spin_box->update();
00516         yz_z_axis_slider->update();
00517         yz_z_axis_spin_box->update();
00518 
00519         QObject::disconnect(gl_yz_scroll_view, SIGNAL(contentsMoving(int, int)),
00520                 this, SLOT(update_gl_xy_scroll_view(int, int)));
00521         QObject::disconnect(yz_view, 
00522                 SIGNAL(mouse_position_changed(int, int, int, const jwsc::Pixel_f*)),
00523                 this, SLOT(update_yz_line_edit(int, int, int, const jwsc::Pixel_f*)));
00524 
00525         QObject::disconnect(x_axis_slider, SIGNAL(valueChanged(int)), 
00526                 xy_view, SLOT(set_current_x_index(int)));
00527         QObject::disconnect(x_axis_slider, SIGNAL(valueChanged(int)), 
00528                 xz_view, SLOT(set_current_x_index(int)));
00529         QObject::disconnect(x_axis_slider, SIGNAL(valueChanged(int)), 
00530                 yz_view, SLOT(set_current_x_index(int)));
00531         QObject::disconnect(x_axis_slider, SIGNAL(valueChanged(int)), 
00532                 x_axis_spin_box, SLOT(setValue(int)));
00533         QObject::disconnect(x_axis_spin_box, SIGNAL(valueChanged(int)), 
00534                 x_axis_slider, SLOT(setValue(int)));
00535 
00536         QObject::disconnect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00537                 xy_view, SLOT(set_current_z_index(int)));
00538         QObject::disconnect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00539                 xz_view, SLOT(set_current_z_index(int)));
00540         QObject::disconnect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00541                 yz_view, SLOT(set_current_z_index(int)));
00542 
00543         QObject::disconnect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00544                 yz_z_axis_spin_box, SLOT(setValue(int)));
00545         QObject::disconnect(yz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00546                 yz_z_axis_slider, SLOT(setValue(int)));
00547 
00548         if (show_xz_view)
00549         {
00550             QObject::disconnect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00551                     xz_z_axis_slider, SLOT(setValue(int)));
00552             QObject::disconnect(yz_z_axis_slider, SIGNAL(valueChanged(int)), 
00553                     xz_z_axis_spin_box, SLOT(setValue(int)));
00554             QObject::disconnect(yz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00555                     xz_z_axis_slider, SLOT(setValue(int)));
00556             QObject::disconnect(yz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00557                     xz_z_axis_spin_box, SLOT(setValue(int)));
00558             QObject::disconnect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00559                     yz_z_axis_slider, SLOT(setValue(int)));
00560             QObject::disconnect(xz_z_axis_slider, SIGNAL(valueChanged(int)), 
00561                     yz_z_axis_spin_box, SLOT(setValue(int)));
00562             QObject::disconnect(xz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00563                     yz_z_axis_slider, SLOT(setValue(int)));
00564             QObject::disconnect(xz_z_axis_spin_box, SIGNAL(valueChanged(int)), 
00565                     yz_z_axis_spin_box, SLOT(setValue(int)));
00566         }
00567     }
00568 }
00569 
00570 void Viewer::file_close()
00571 {
00572     if (img_blk != NULL)
00573     {
00574         int num_imgs = img_blk->num_imgs;
00575 
00576         for (int i = 0; i < num_imgs; i++)
00577         {
00578             free(fnames[ i ]);
00579         }
00580         free(fnames);
00581         fnames = 0;
00582 
00583         free_imgblock_f(img_blk);
00584         img_blk = NULL;
00585     }
00586 
00587     xy_view->set_imgblock(NULL);
00588     xz_view->set_imgblock(NULL);
00589     yz_view->set_imgblock(NULL);
00590 
00591     xy_view->updateGL();
00592     xz_view->updateGL();
00593     yz_view->updateGL();
00594 
00595     gl_xy_scroll_view->viewport()->setMaximumSize(xy_view->size());
00596     gl_xz_scroll_view->viewport()->setMaximumSize(xz_view->size());
00597     gl_yz_scroll_view->viewport()->setMaximumSize(yz_view->size());
00598 
00599     x_axis_slider->setRange(0, 0);
00600     x_axis_spin_box->setRange(0, 0);
00601     y_axis_slider->setRange(0, 0);
00602     y_axis_spin_box->setRange(0, 0);
00603     xz_z_axis_slider->setRange(0, 0);
00604     xz_z_axis_spin_box->setRange(0, 0);
00605     yz_z_axis_slider->setRange(0, 0);
00606     yz_z_axis_spin_box->setRange(0, 0);
00607 
00608     x_axis_slider->update();
00609     x_axis_spin_box->update();
00610     y_axis_slider->update();
00611     y_axis_spin_box->update();
00612     xz_z_axis_slider->update();
00613     xz_z_axis_spin_box->update();
00614     yz_z_axis_slider->update();
00615     yz_z_axis_spin_box->update();
00616 }
00617 
00618 void Viewer::file_open()
00619 {
00620     int num_rows, num_cols, num_imgs;
00621     int img;
00622     Error* err;
00623 
00624     QStringList image_names = QFileDialog::getOpenFileNames(
00625             "Images (*.tif *.tiff *.jpeg *.jpg *.png *.jiff)", 
00626             QDir::currentDirPath(), this);
00627 
00628     if (image_names.empty())
00629     {
00630         return;
00631     }
00632 
00633     file_close();
00634 
00635     image_names.sort();
00636     num_imgs = image_names.count();
00637 
00638     assert(fnames = (char**)malloc(num_imgs*sizeof(char*)));
00639     for (img = 0; img < num_imgs; img++)
00640     {
00641         int len = image_names[ img ].length() + 1;
00642         assert(fnames[ img ] = (char*)malloc(len*sizeof(char)));
00643         strcpy(fnames[ img ], image_names[ img ].ascii());
00644     }
00645 
00646     if ((err = read_imgblock_named_f(&img_blk, (const char**)fnames, num_imgs)) 
00647             != NULL)
00648     {
00649         print_error_msg_exit("viewer", err->msg);
00650     }
00651 
00652     //free(fnames);
00653 
00654     num_rows = img_blk->num_rows;
00655     num_cols = img_blk->num_cols;
00656 
00657     xy_view->set_imgblock(img_blk);
00658     xz_view->set_imgblock(img_blk);
00659     yz_view->set_imgblock(img_blk);
00660 
00661     x_axis_slider->setRange(0, num_cols - 1);
00662     x_axis_slider->update();
00663     x_axis_spin_box->setRange(0, num_cols - 1);
00664     x_axis_spin_box->update();
00665     y_axis_slider->setRange(0, num_rows - 1);
00666     y_axis_slider->update();
00667     y_axis_spin_box->setRange(0, num_rows - 1);
00668     y_axis_spin_box->update();
00669     xz_z_axis_slider->setRange(0, num_imgs - 1);
00670     xz_z_axis_slider->update();
00671     yz_z_axis_slider->setRange(0, num_imgs - 1);
00672     yz_z_axis_slider->update();
00673     xz_z_axis_spin_box->setRange(0, num_imgs - 1);
00674     xz_z_axis_spin_box->update();
00675     yz_z_axis_spin_box->setRange(0, num_imgs - 1);
00676     yz_z_axis_spin_box->update();
00677 
00678     xy_view->updateGL();
00679     xz_view->updateGL();
00680     yz_view->updateGL();
00681 
00682     gl_xy_scroll_view->viewport()->setMaximumSize(xy_view->size());
00683     gl_xz_scroll_view->viewport()->setMaximumSize(xz_view->size());
00684     gl_yz_scroll_view->viewport()->setMaximumSize(yz_view->size());
00685 
00686     update_xy_fname_line_edit(0);
00687 }
00688 
00689 void Viewer::new_viewer()
00690 {
00691     Viewer* theViewer = new Viewer();
00692     theViewer->show();
00693 }
00694 
00695 void Viewer::update_gl_xy_scroll_view(int x, int y)
00696 {
00697     mutex->lock();
00698 
00699     if (!updating_gl_scroll_view )
00700     {
00701         updating_gl_scroll_view = 1;
00702         gl_xy_scroll_view->setContentsPos(x, gl_xy_scroll_view->contentsY());
00703     }
00704     else
00705     {
00706         updating_gl_scroll_view = 0;
00707     }
00708 
00709     mutex->unlock();
00710 }
00711 
00712 void Viewer::update_gl_xz_scroll_view(int x, int y)
00713 {
00714     mutex->lock();
00715 
00716     if (!updating_gl_scroll_view )
00717     {
00718         updating_gl_scroll_view = 1;
00719         gl_xz_scroll_view->setContentsPos(x, gl_xz_scroll_view->contentsY());
00720     }
00721     else
00722     {
00723         updating_gl_scroll_view = 0;
00724     }
00725 
00726     mutex->unlock();
00727 }
00728 
00729 void Viewer::update_gl_yz_scroll_view(int x, int y)
00730 {
00731     mutex->lock();
00732 
00733     if (!updating_gl_scroll_view )
00734     {
00735         updating_gl_scroll_view = 1;
00736         gl_yz_scroll_view->setContentsPos(x, gl_yz_scroll_view->contentsY());
00737     }
00738     else
00739     {
00740         updating_gl_scroll_view = 0;
00741     }
00742 
00743     mutex->unlock();
00744 }
00745 
00746 void Viewer::update_xy_line_edit(int x, int y, int z, const Pixel_f* px)
00747 {
00748     QString string;
00749     
00750     string.sprintf("%3d  %3d  %3d", (int)(255*px->r), (int)(255*px->g), 
00751             (int)(255*px->b));
00752     xy_line_edit->setText(string);
00753 }
00754 
00755 void Viewer::update_xz_line_edit(int x, int y, int z, const Pixel_f* px)
00756 {
00757     QString string;
00758     
00759     string.sprintf("%3d  %3d  %3d", (int)(255*px->r), (int)(255*px->g), 
00760             (int)(255*px->b));
00761     xz_line_edit->setText(string);
00762 }
00763 
00764 void Viewer::update_yz_line_edit(int x, int y, int z, const Pixel_f* px)
00765 {
00766     QString string;
00767     
00768     string.sprintf("%3d  %3d  %3d", (int)(255*px->r), (int)(255*px->g), 
00769             (int)(255*px->b));
00770     yz_line_edit->setText(string);
00771 }
00772 
00773 void Viewer::update_xy_fname_line_edit(int i)
00774 {
00775     if (!fnames)
00776     {
00777         xy_fname_line_edit->setText("");
00778         return;
00779     }
00780 
00781     const char* str = fnames[ i ];
00782     int len = strlen(str);
00783     const char* ptr = &(str[ len-1 ]);
00784 
00785     while (ptr != str && *ptr != '/')
00786         ptr--;
00787 
00788     if (*ptr == '/')
00789         ptr++;
00790 
00791     //std::cerr << ptr << '\n';
00792     //QString string(ptr);
00793     xy_fname_line_edit->setText(ptr);
00794 }
00795 
00796 
00798 void clean_up()
00799 {
00800     delete opts;
00801 }
00802 
00803 
00805 void print_usage(void)
00806 {
00807     cerr << "usage: viewer OPTIONS\n";
00808     cerr << "where OPTIONS is one or more of the following:\n";
00809     opts->print();
00810 }
00811 
00812 
00814 void process_help_opt(void)
00815 {   
00816     print_usage();
00817     clean_up();
00818     exit(EXIT_SUCCESS);
00819 }
00820 
00821 
00823 void process_version_opt(void)
00824 {
00825     std::cout << VIEWER_PACKAGE_STRING << "\n";
00826     clean_up();
00827     exit(EXIT_SUCCESS);
00828 }
00829 
00835 int main(int argc, char **argv)
00836 {
00837     using jwscxx::base::Option_no_arg;
00838     using jwscxx::base::Option_with_arg;
00839     using jwscxx::base::Arg_error;
00840     using jwscxx::base::Options;
00841 
00842     int rstatus = EXIT_SUCCESS;
00843 
00844     const char* l_name;
00845     const char* desc;
00846 
00847     opts = new Options();
00848 
00849     l_name = "help";
00850     desc   = "Program usage";
00851     opts->add(new Option_no_arg('h', l_name, desc, process_help_opt));
00852 
00853     l_name = "version";
00854     desc   = "Program version.";
00855     opts->add(new Option_no_arg('v', l_name, desc, process_version_opt));
00856 
00857     try
00858     {
00859         opts->process(argc, argv);
00860 
00861         QApplication app(argc, argv);
00862 
00863         if (! QGLFormat::hasOpenGL())
00864         {
00865             throw Arg_error("This system has no OpenGL support");
00866         }
00867 
00868         Viewer theViewer;
00869         theViewer.show();
00870 
00871         QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
00872 
00873         rstatus = app.exec();
00874     }
00875     catch (Arg_error e)
00876     {
00877         cerr << "viewer: ";
00878         e.print();
00879         rstatus = EXIT_FAILURE;
00880     }
00881 
00882     clean_up();
00883 
00884     return rstatus;
00885 }