use qt resources for icons
[mussa.git] / qui / MussaWindow.cpp
1 #include <QAction>
2 #include <QDir>
3 #include <QFileDialog>
4 #include <QHBoxLayout>
5 #include <QIcon>
6 #include <QMenuBar>
7 #include <QMessageBox>
8 #include <QScrollBar>
9 #include <QStatusBar>
10 #include <QString>
11 #include <QWhatsThis>
12
13 #include "qui/MussaWindow.hpp"
14 #include "qui/mussa_setup_dialog/MussaSetupDialog.hpp"
15 #include "mussa_exceptions.hpp"
16
17 #include <iostream>
18
19 using namespace std;
20
21 MussaWindow::MussaWindow(Mussa *analysis_, QWidget *parent) :
22   QMainWindow(parent),
23   analysis(analysis_),
24   motif_editor(0),
25   browser(this),
26   mussaViewTB("Path Views"),
27   zoomBox(),
28   threshold(),
29   zoomLabel(),
30   closeAction(0) // initialize one of the pointers to null as a saftey flag
31 {
32   setupActions();
33   setupMainMenu();
34
35   //This next setWhatsThis function prevents
36   // a segfault when using WhatsThis feature with 
37   // opengl widget.
38   //scene->setWhatsThis(tr("Mussa in OpenGL!"));
39   setCentralWidget(&browser);
40   // well updatePosition isn't quite right as we really just need
41   // to call update()
42   connect(this, SIGNAL(changedAnnotations()), &browser, SLOT(update()));
43
44   mussaViewTB.addAction(toggleMotifsAction);
45   
46   zoomLabel.setPixmap(QIcon(":/icons/viewmag.png").pixmap(16, 16));
47   zoomLabel.setToolTip(tr("Zoom"));
48   zoomLabel.setWhatsThis(tr("Zoom magnification factor"));
49   mussaViewTB.addWidget(&zoomLabel);
50
51   zoomBox.setToolTip(tr("Zoom"));
52   zoomBox.setWhatsThis(tr("Zoom magnification factor"));
53   zoomBox.setRange(1,10000000);
54   mussaViewTB.addWidget(&zoomBox);
55   connect(&zoomBox, SIGNAL(valueChanged(int)), 
56           &browser, SLOT(setZoom(int)));
57  
58   threshold.setRange(10,30);
59   threshold.setThreshold(20);
60   //scene->setClipPlane(20);
61   // FIXME: for when we get the paths drawn at the appropriate depth
62   //connect(&threshold, SIGNAL(thresholdChanged(int)),
63   //        this, SLOT(setClipPlane(int)));
64   connect(&threshold, SIGNAL(thresholdChanged(int)),
65           this, SLOT(setSoftThreshold(int)));
66   mussaViewTB.addWidget(&threshold);
67
68   addToolBar(&mussaViewTB);
69
70   statusBar()->showMessage("Welcome to mussa", 2000);
71   updateAnalysis();
72 }
73
74 void MussaWindow::setAnalysis(Mussa *new_analysis)
75 {
76   if (new_analysis != 0) {
77     // only switch mussas if we loaded without error
78     delete analysis;
79     analysis = new_analysis;
80     updateAnalysis();
81   }
82 }
83
84 void MussaWindow::setupActions()
85 {
86   // we really don't want to run this more than once.
87   assert (closeAction == 0);
88
89   // the ever popular about box
90   aboutAction = new QAction(tr("&About"), this);
91   connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
92   aboutAction->setIcon(QIcon(":/icons/info.png"));
93
94   // add exit
95   closeAction = new QAction(tr("&Close"), this);
96   closeAction->setStatusTip(tr("Close this window"));
97   connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
98   closeAction->setIcon(QIcon(":/icons/exit.png"));
99   
100   createNewAnalysisAction = new QAction(tr("Define Analysis"), this);
101   connect(createNewAnalysisAction, SIGNAL(triggered()), 
102           this, SLOT(createNewAnalysis()));
103   createNewAnalysisAction->setIcon(QIcon(":/icons/filenew.png"));
104   
105   createSubAnalysisAction = new QAction(tr("Define SubAnalysis"), this);
106   connect(createSubAnalysisAction, SIGNAL(triggered()), 
107           this, SLOT(createSubAnalysis()));
108
109   editMotifsAction = new QAction(tr("Edit Motifs"), this);;
110   connect(editMotifsAction, SIGNAL(triggered()), this, SLOT(editMotifs()));
111   
112   loadMotifListAction = new QAction(tr("Load Motif List"), this);
113   connect(loadMotifListAction, SIGNAL(triggered()), 
114           this, SLOT(loadMotifList()));
115   loadMotifListAction->setIcon(QIcon(":/icons/fileopen.png"));
116   
117   loadMupaAction = new QAction(tr("Load Mussa Parameters"), this);
118   connect(loadMupaAction, SIGNAL(triggered()), 
119           this, SLOT(loadMupa()));
120   loadMupaAction->setIcon(QIcon(":/icons/fileopen.png"));
121
122   loadSavedAnalysisAction = new QAction(tr("Load &Analysis"), this);
123   connect(loadSavedAnalysisAction, SIGNAL(triggered()), 
124           this, SLOT(loadSavedAnalysis()));
125   loadSavedAnalysisAction->setIcon(QIcon(":/icons/fileopen.png"));
126
127   saveMotifListAction = new QAction(tr("Save Motifs"), this);
128   connect(saveMotifListAction, SIGNAL(triggered()), 
129           this, SLOT(saveMotifList()));
130   saveMotifListAction->setIcon(QIcon(":/icons/filesave.png"));
131
132   showMussaViewToolbarAction = new QAction(tr("Show Toolbar"), this);
133   connect(showMussaViewToolbarAction, SIGNAL(triggered()), 
134           this, SLOT(showMussaToolbar()));
135   showMussaViewToolbarAction->setCheckable(true);
136   showMussaViewToolbarAction->setChecked(true);
137
138   toggleMotifsAction = new QAction(tr("Toggle Motifs"), this);
139   connect(toggleMotifsAction, SIGNAL(triggered()), 
140           this, SLOT(toggleMotifs()));
141   toggleMotifsAction->setCheckable(true);
142   toggleMotifsAction->setIcon(QIcon(":/icons/motif_icon.png"));
143   toggleMotifsAction->setWhatsThis(tr("Toggle motif annotations on/off\n\n"
144                                    "You can load motif annotations via "
145                                    "'File->Load Motif List' menu option."));
146
147   //Save pixel map action
148   saveBrowserPixmapAction = new QAction(tr("Save to image..."), this);
149   connect(saveBrowserPixmapAction, (SIGNAL(triggered())),
150           &browser, SLOT(promptSaveBrowserPixmap()));
151   saveBrowserPixmapAction->setIcon(QIcon(":/icons/image2.png"));
152
153   viewMussaAlignmentAction = new QAction(tr("View mussa alignment"), this);
154   connect(viewMussaAlignmentAction, SIGNAL(triggered()),
155           this, SLOT(viewMussaAlignment() ));
156   viewMussaAlignmentAction->setWhatsThis(tr("Create a zoomed in window "
157                                             "showing alignment of the seqcomp "
158                                             "defined paths"));
159
160   whatsThisAction = QWhatsThis::createAction(this);
161   whatsThisAction->setIcon(QIcon(":/icons/help.png"));
162
163 }
164
165 void MussaWindow::setupMainMenu()
166 {
167   // we need to run setupActions first
168   assert (closeAction != 0);
169   
170   QMenu *newMenu;
171   newMenu = menuBar()->addMenu(tr("&File"));
172   newMenu->addAction(createNewAnalysisAction);
173   newMenu->addAction(loadMupaAction);
174   newMenu->addAction(loadSavedAnalysisAction);
175   //newMenu->addAction(createSubAnalysisAction);
176   newMenu->addSeparator();
177   newMenu->addAction(loadMotifListAction);
178   newMenu->addAction(saveMotifListAction);
179   newMenu->addSeparator();
180   newMenu->addAction(saveBrowserPixmapAction);
181   newMenu->addSeparator();
182   newMenu->addAction(closeAction);
183
184   newMenu = menuBar()->addMenu(tr("&View"));
185   newMenu->addAction(editMotifsAction);
186   newMenu->addAction(viewMussaAlignmentAction);
187   newMenu->addAction(showMussaViewToolbarAction);
188
189   newMenu = menuBar()->addMenu(tr("&Help"));
190   newMenu->addAction(whatsThisAction);
191   newMenu->addSeparator();
192   newMenu->addAction(aboutAction);
193 }
194   
195 void MussaWindow::about()
196 {
197   QString msg("Welcome to Multiple Species Sequence Analysis\n"
198               "(c) 2005-2006 California Institute of Technology\n"
199               "Tristan De Buysscher, Diane Trout\n");
200    msg += "\n\r";
201    msg += "Path: ";
202    msg += QDir::currentPath();
203    msg += "\n";
204    msg += "OpenGL: ";
205    msg += (char *)glGetString(GL_VERSION);
206    msg += "\n";
207    QMessageBox::about(this, tr("About mussa"), msg);
208 }
209
210 void MussaWindow::createNewAnalysis()
211 {
212   try {
213     if (setup_analysis_dialog.exec()) {
214       Mussa *m = 0;
215       m = setup_analysis_dialog.getMussa();
216       setAnalysis(m);
217     } else {
218       std::cout << "New mussa exp. aborted!\n";
219     }
220   } catch(mussa_error e) {
221     QString msg(e.what());
222     QMessageBox::warning(this, tr("Create New Analysis"), msg);
223   }
224 }
225
226 void MussaWindow::createSubAnalysis()
227 {
228   NotImplementedBox();
229 }
230
231 void MussaWindow::editMotifs()
232 {
233   if (motif_editor != 0) {
234     motif_editor->hide();
235     delete motif_editor;
236   }
237   motif_editor = new MotifEditor(analysis);
238   connect(motif_editor, SIGNAL(changedMotifs()), 
239           this, SLOT(updateAnnotations()));
240   motif_editor->show();
241 }
242
243 void MussaWindow::loadMotifList()
244 {
245   QString caption("Load a motif list");
246   QString filter("Motif list(*.txt *.mtl)");
247   QString path = QFileDialog::getOpenFileName(this,
248                                                    caption, 
249                                                    QDir::currentPath(),
250                                                    filter);
251   // user hit cancel?
252   if (path.isNull()) 
253     return;
254   // try to load safely
255   try {
256     analysis->load_motifs(path.toStdString());
257   } catch (runtime_error e) {
258     QString msg("Unable to load ");
259     msg += path;
260     msg += "\n";
261     msg += e.what();
262     QMessageBox::warning(this, "Load Motifs", msg);
263   }
264   assert (analysis != 0);
265 }
266
267 void MussaWindow::saveMotifList()
268 {
269   NotImplementedBox();
270 }
271
272 void MussaWindow::loadMupa()
273 {
274   QString caption("Load a mussa parameter file");
275   QString filter("Mussa Parameters (*.mupa)");
276   QString mupa_path = QFileDialog::getOpenFileName(this,
277                                                    caption, 
278                                                    QDir::currentPath(),
279                                                    filter);
280   // user hit cancel?
281   if (mupa_path.isNull()) 
282     return;
283   // try to load safely
284   try {
285     Mussa *m = new Mussa;
286     m->load_mupa_file(mupa_path.toStdString());
287     m->analyze(0, 0, Mussa::TransitiveNway, 0.0);
288     setAnalysis(m);
289   } catch (mussa_load_error e) {
290     QString msg("Unable to load ");
291     msg += mupa_path;
292     msg += "\n";
293     msg += e.what();
294     QMessageBox::warning(this, "Load Parameter", msg);
295   }
296   assert (analysis != 0);
297 }
298
299 void MussaWindow::loadSavedAnalysis()
300 {
301   QString caption("Load a previously run analysis");
302   QString muway_dir = QFileDialog::getExistingDirectory(this,
303                                                         caption, 
304                                                         QDir::currentPath());
305   // user hit cancel?
306   if (muway_dir.isNull()) 
307     return;
308   // try to safely load
309   try {
310     Mussa *m = new Mussa;
311     m->load(muway_dir.toStdString());
312     // only switch mussas if we loaded without error
313     setAnalysis(m);
314   } catch (mussa_load_error e) {
315     QString msg("Unable to load ");
316     msg += muway_dir;
317     msg += "\n";
318     msg += e.what();
319     QMessageBox::warning(this, "Load Parameter", msg);
320   }
321   assert (analysis != 0);
322 }
323
324 void MussaWindow::setSoftThreshold(int threshold)
325 {
326   if (analysis->get_soft_thres() != threshold) {
327     analysis->set_soft_thres(threshold);
328     analysis->nway();
329     updateLinks();
330     update();
331   }
332 }
333
334 void MussaWindow::showMussaToolbar()
335 {
336   if (mussaViewTB.isVisible())
337     mussaViewTB.hide();
338   else
339     mussaViewTB.show();
340 }
341
342 void MussaWindow::toggleMotifs()
343 {
344   NotImplementedBox();
345 }
346
347 void MussaWindow::NotImplementedBox()
348 {
349   QMessageBox::warning(this, QObject::tr("mussa"), QObject::tr("Not implemented yet"));
350 }      
351
352 void MussaWindow::viewMussaAlignment()
353 {
354   const set<int>& selected_paths = browser.selectedPaths();
355   if (selected_paths.size() == 0 ) {
356     QMessageBox::warning(this, 
357                          QObject::tr("mussa"),
358                          QObject::tr("you should probably select some paths "
359                                      "first"));
360   } else {
361     MussaAlignedWindow *ma_win = new MussaAlignedWindow(*analysis, 
362                                                         selected_paths);
363     connect(this, SIGNAL(changedAnnotations()), 
364             ma_win, SLOT(update()));
365     aligned_windows.push_back(ma_win);
366     ma_win->show();
367   }
368 }
369                         
370 void MussaWindow::updateAnalysis()
371 {
372   cout << "analysis updated" << endl;
373   for (list<MussaAlignedWindow *>::iterator maw_i = aligned_windows.begin();
374        maw_i != aligned_windows.end();
375        ++maw_i)
376   {
377     (*maw_i)->hide();
378     delete *maw_i;
379   }
380
381   browser.clear();
382   const vector<Sequence>& seqs = analysis->sequences();
383   browser.setSequences(seqs, analysis->colorMapper());
384   updateLinks();
385   browser.zoomOut();
386   cout << "browser zoom " <<  browser.zoom() << endl;
387   zoomBox.setValue(browser.zoom());
388 }
389
390 void MussaWindow::updateAnnotations()
391 {
392   // motifs were changed in the sequences by 
393   // Mussa::update_sequences_motifs
394   emit changedAnnotations();
395   browser.update();
396 }
397
398 void MussaWindow::updateLinks()
399 {
400   browser.clear_links();
401   bool reversed = false;
402   const NwayPaths& nway = analysis->paths();
403
404   typedef list<ExtendedConservedPath> conserved_paths;
405   typedef conserved_paths::const_iterator const_conserved_paths_itor;
406   for(const_conserved_paths_itor path_itor = nway.refined_pathz.begin();
407       path_itor != nway.refined_pathz.end();
408       ++path_itor)
409   {
410     // since we were drawing to the start of a window, and opengl lines
411     // are centered around the two connecting points our lines were slightly
412     // offset. the idea of window_offset is to adjust them to the
413     // right for forward compliment or left for reverse compliment
414     // FIXME: figure out how to unit test these computations
415     //GLfloat window_offset = (path_itor->window_size)/2.0;
416
417     size_t track_len = path_itor->track_indexes.size();
418     vector<int> normalized_path;
419     normalized_path.reserve(track_len);
420     vector<bool> rc_flags(false, track_len);
421     for (size_t track_i=0; track_i != track_len; ++track_i)
422     {
423       int x = path_itor->track_indexes[track_i];
424       // at some point when we modify the pathz data structure to keep
425       // track of the score we can put grab the depth here.
426       //
427       // are we reverse complimented?
428       if ( x>=0) {
429         reversed = false;
430       } else {
431         reversed = true;
432         x = -x; // make positive
433       }
434       normalized_path.push_back(x);
435       rc_flags.push_back(reversed);
436     }
437     browser.link(normalized_path, rc_flags, path_itor->window_size);
438   }
439   browser.update();
440 }
441