FigureEditor.cxx

Go to the documentation of this file.
00001 
00015 #ifdef _MSC_VER
00016 #include "msdevstudio/MSconfig.h"
00017 #endif
00018 
00019 // for MEMFUN1_DEFECT
00020 #ifdef HAVE_CONFIG_H
00021 #include "config.h"
00022 #endif
00023 
00024 #include "FigureEditor.h"
00025 
00026 #include "QtGroupView.h"
00027 #include "CanvasWindow.h"
00028 #include "PickTable.h"
00029 #include "plotters/PlotterBase.h"
00030 
00031 #include <qapplication.h>
00032 #include <qclipboard.h>
00033 #include <qcursor.h>
00034 #include <qimage.h>
00035 
00036 #if QT_VERSION < 0x040000
00037 #include <qdragobject.h>
00038 #include <qwmatrix.h>
00039 #include <qpaintdevicemetrics.h>
00040 #else
00041 // Added by the Qt porting tool:
00042 #include <q3dragobject.h>
00043 #include <QtGui/QMouseEvent>
00044 #include <QtGui/QPixmap>
00045 #include <QtGui/QResizeEvent>
00046 #include <QtGui/QPrintDialog>
00047 #include <qmatrix.h>
00048 #include <q3paintdevicemetrics.h>
00049 #endif
00050 
00051 #include <qpainter.h>
00052 #include <qprinter.h>
00053 #include <qsettings.h>
00054 #include <qstatusbar.h>
00055 
00056 #include <algorithm>
00057 #include <functional>
00058 
00059 #include <cmath>
00060 #include <cctype>
00061 
00062 #include <cassert>
00063 
00064 using std::abs;
00065 using std::bind2nd;
00066 using std::for_each;
00067 using std::mem_fun;
00068 using std::string;
00069 using std::transform;
00070 using std::vector;
00071 
00072 
00078 static QPrinter::PageSize indexToPageSize[] = 
00079   { QPrinter::A4, QPrinter::B5, QPrinter::Letter, 
00080     QPrinter::Legal, QPrinter::Executive,
00081     QPrinter::A0, QPrinter::A1, QPrinter::A2, QPrinter::A3, 
00082     QPrinter::A5, QPrinter::A6, QPrinter::A7, QPrinter::A8, 
00083     QPrinter::A9, QPrinter::B0, QPrinter::B1,
00084     QPrinter::B10, QPrinter::B2, QPrinter::B3, QPrinter::B4, 
00085     QPrinter::B6, QPrinter::B7, QPrinter::B8, QPrinter::B9, 
00086     QPrinter::C5E, QPrinter::Comm10E,
00087     QPrinter::DLE, QPrinter::Folio, QPrinter::Ledger, 
00088     QPrinter::Tabloid, QPrinter::Custom, QPrinter::NPageSize };
00089 
00095 static  QPrinter::Orientation indexToOrientation[] = { QPrinter::Portrait,
00096                                                  QPrinter::Landscape };
00097 
00103 static  QPrinter::ColorMode indexToColorMode[] = { QPrinter::GrayScale,
00104                                                    QPrinter::Color };
00105 
00106 using namespace hippodraw;
00107 
00108 QPrinter * FigureEditor::s_printer = 0;
00109 QString    FigureEditor::s_app_key ( "/HippoDraw/" );
00110 QString    FigureEditor::s_registry ( "/Trolltech" );
00111 
00112 FigureEditor::
00113 #if QT_VERSION < 0x040000
00114 FigureEditor ( QCanvas * c, QWidget * parent,
00115                const char * name, Qt::WFlags f)
00116   : QCanvasView ( c, parent, name, f ),
00117 #else
00118 FigureEditor ( Q3Canvas * c, QWidget * parent,
00119                const char * name, Qt::WFlags f)
00120   : Q3CanvasView ( c, parent, name, f ),
00121 #endif
00122     m_scale_factor ( 1.0 ),
00123     m_printer_bounds ( false ),
00124     m_ignore_margin ( false ),
00125     m_rightItem ( 0 )
00126 {
00127   m_zoom_mode = false ;
00128 
00129   m_canvas = canvas ();
00130   m_canvas->setUpdatePeriod ( 30 );
00131 
00132   if ( s_printer == 0 )
00133     {
00134       initPrinter ();
00135     }
00136   
00137   calcPrinterMetrics ( s_printer );
00138   
00139   m_canvas->resize ( m_apage_w, m_apage_h );
00140 
00141   //Printer Page margin is not visible by default
00142   QSettings settings;
00143   settings.insertSearchPath ( QSettings::Windows, s_registry );
00144   int pb = settings.readNumEntry ( s_app_key + "Canvas/Margin", 0 );
00145   bool yes = ( pb == 1 ) ? true : false;
00146   showPrinterMargins ( yes );
00147  }
00148 
00149 
00150 void
00151 FigureEditor::
00152 initPrinter ()
00153 {
00154   s_printer = new QPrinter ( );
00155   
00156   QSettings settings;
00157   settings.insertSearchPath ( QSettings::Windows, s_registry );
00158 
00159   const int pagesize 
00160     = settings.readNumEntry ( s_app_key + "Printer/PageSize", 
00161                               QPrinter::Letter );
00162   assert( pagesize < QPrinter::NPageSize );
00163   QPrinter::PageSize size = indexToPageSize [ pagesize ];
00164   
00165   const int orientation
00166     = settings.readNumEntry(  s_app_key + "Printer/Orientation",
00167                               QPrinter::Portrait );
00168   assert(  orientation == 0 || orientation == 1 );
00169   QPrinter::Orientation orient = indexToOrientation [ orientation ];
00170 
00171   const int color
00172     = settings.readNumEntry( s_app_key + "Printer/ColorMode",
00173                              QPrinter::Color );
00174   assert( color == 0 || color == 1 );
00175   QPrinter::ColorMode colorMode = indexToColorMode[color];
00176 
00177   s_printer->setOrientation ( orient );
00178   s_printer->setPageSize ( size );
00179   s_printer->setFullPage ( true );
00180   s_printer->setColorMode ( colorMode );
00181 }
00182 
00183 void
00184 FigureEditor::
00185 setAppKey ( const std::string & appkey )
00186 {
00187   s_app_key = appkey.c_str();
00188 }
00189 
00190 void
00191 FigureEditor::
00192 showPrinterMargins ( bool on )
00193 {
00194    
00195   if ( m_printer_bounds == false && on == true  ) // No margin exists. Draw 'em
00196     {
00197       m_printer_bounds = true;
00198       
00199       int height = m_canvas -> height();
00200       int y = 0;
00201       while ( y < height )
00202         {
00203           addPageMargin ( 0, y );
00204           y += m_apage_h;
00205         }
00206       
00207     }
00208   else if( m_printer_bounds == true && on == false ) // Margins exist delete it
00209     {
00210       m_printer_bounds = false;
00211       
00212 #if QT_VERSION < 0x040000
00213       QCanvasItemList itemList = m_canvas -> allItems ();
00214       
00215       QValueListIterator< QCanvasItem * > iter;
00216       iter = itemList.begin();
00217             
00218       while( iter != itemList.end() )
00219         {
00220           QCanvasItem * item = *iter++;
00221           QtView * view = dynamic_cast < QtView * > ( item );
00222           
00223           if ( view == 0 )  // it is a printer page border
00224             {
00225               QCanvasRectangle * rect
00226                 = dynamic_cast< QCanvasRectangle * >( item );
00227               assert( rect );
00228               delete rect;
00229             }
00230         }
00231 #else
00232       Q3CanvasItemList itemList = m_canvas -> allItems ();
00233       
00234       Q3ValueListIterator< Q3CanvasItem * > iter;
00235       iter = itemList.begin();
00236             
00237       while( iter != itemList.end() )
00238         {
00239           Q3CanvasItem * item = *iter++;
00240           QtView * view = dynamic_cast < QtView * > ( item );
00241           
00242           if ( view == 0 )  // it is a printer page border
00243             {
00244               Q3CanvasRectangle * rect
00245                 = dynamic_cast< Q3CanvasRectangle * >( item );
00246               assert( rect );
00247               delete rect;
00248             }
00249         }
00250 #endif
00251     }
00252 }
00253 
00254 void
00255 FigureEditor::
00256 addPageMargin ( int x, int y )
00257 {
00258     
00259   if ( m_printer_bounds == true )
00260     {
00261 #if QT_VERSION < 0x040000
00262       QCanvasRectangle * rect 
00263         = new QCanvasRectangle ( x, y, m_upage_w, m_upage_h, m_canvas );
00264 #else
00265       Q3CanvasRectangle * rect 
00266         = new Q3CanvasRectangle ( x, y, m_upage_w, m_upage_h, m_canvas );
00267 #endif
00268       
00269       int dx = ( m_apage_w - m_upage_w ) / 2;
00270       int dy = ( m_apage_h - m_upage_h ) / 2;
00271       rect ->moveBy ( dx, dy );
00272       
00273       QColor color ( "lightGray" );
00274       QPen pen ( color, 1, Qt::DashLine );
00275       rect -> setPen ( pen );
00276       
00277       rect -> show ();
00278     }
00279 }
00280 
00284 void 
00285 FigureEditor::
00286 calcPrinterMetrics ( QPaintDevice * device )
00287 {
00288   QString name = s_printer->printerName();
00289   if ( name.isNull() )
00290     {
00291       if( s_printer -> orientation() == QPrinter::Portrait )
00292         {
00293           m_upage_w = 720; // dimensions of letter size
00294           m_upage_h = 990; // HP printer in portrait mode
00295           m_apage_w = 816;
00296           m_apage_h = 1054;
00297         }
00298       else
00299         {
00300           m_upage_h = 720; // dimensions of letter size
00301           m_upage_w = 990; // HP printer in landscape mode
00302           m_apage_h = 816;
00303           m_apage_w = 1054;
00304         }
00305     }
00306   else
00307     {
00308 #if QT_VERSION < 0x040000
00309       QPaintDeviceMetrics metrics ( device );
00310 #else
00311       Q3PaintDeviceMetrics metrics ( device );
00312 #endif
00313       int dpix = metrics.logicalDpiX ();
00314       int dpiy = metrics.logicalDpiY ();
00315 
00316       m_apage_w = metrics.widthMM ();
00317       m_apage_h = metrics.heightMM ();
00318       
00319       m_apage_w = static_cast < int > ( m_apage_w * dpix / 25.4 ); 
00320       m_apage_h = static_cast < int > ( m_apage_h * dpiy / 25.4 ); 
00321 
00322       if( s_printer -> orientation() == QPrinter::Landscape )
00323         std::swap( m_apage_w, m_apage_h );
00324       
00325       QSize margins = s_printer -> margins ();
00326       m_upage_w = m_apage_w - 2 * margins.width ();
00327       m_upage_h = m_apage_h - 2 * margins.height ();
00328   }
00329 }
00330 
00331 bool FigureEditor::isSingleItemSelected () const
00332 {
00333   return ( m_selected_list.size() == 1 );
00334 }
00335 
00336 #if QT_VERSION < 0x040000
00337 QCanvasItem * 
00338 #else
00339 Q3CanvasItem * 
00340 #endif
00341 FigureEditor::
00342 selectedItem () const
00343 {  
00344   if ( isSingleItemSelected() )
00345     {
00346       return m_selected_list[0];
00347     }
00348   else
00349     {
00350       return 0;
00351     }
00352 }
00353 
00354 #if QT_VERSION < 0x040000
00355 std::vector < QCanvasItem * > & 
00356 #else
00357 std::vector < Q3CanvasItem * > & 
00358 #endif
00359 FigureEditor::
00360 getSelectedItems ()
00361 {
00362   return m_selected_list;
00363 }
00364 
00365 void
00366 FigureEditor::
00367 #if QT_VERSION < 0x040000
00368 addSelectedItem ( QCanvasItem * item )
00369 #else
00370 addSelectedItem ( Q3CanvasItem * item )
00371 #endif
00372 {
00373 
00374   if ( ! item->isSelected() )
00375     {
00376       m_selected_list.push_back ( item );
00377       setSelectedFlags();
00378     }
00379 
00380 }
00381  
00382 void
00383 FigureEditor::
00384 #if QT_VERSION < 0x040000
00385 removeSelectedItem ( QCanvasItem * item )
00386 {
00387   if ( item->isSelected() ) {
00388     vector < QCanvasItem * > ::iterator last
00389 #else
00390 removeSelectedItem ( Q3CanvasItem * item )
00391 {
00392   if ( item->isSelected() ) {
00393     vector < Q3CanvasItem * > ::iterator last
00394 #endif
00395       = std::remove ( m_selected_list.begin(), 
00396                       m_selected_list.end(), item );
00397     m_selected_list.erase ( last, m_selected_list.end() );
00398     setSelectedFlags();
00399   }
00400 }
00401  
00402 QRect FigureEditor::getSelectedBounds () const
00403 {
00404 #ifdef ITERATOR_MEMBER_DEFECT
00405   std:: 
00406 #endif
00407 #if QT_VERSION < 0x040000
00408   vector < QCanvasItem * > ::const_iterator first 
00409     = m_selected_list.begin();
00410   QCanvasItem * item = *first++;
00411 #else
00412   vector < Q3CanvasItem * > ::const_iterator first 
00413     = m_selected_list.begin();
00414   Q3CanvasItem * item = *first++;
00415 #endif
00416   QRect rect = item->boundingRect();
00417 
00418   while ( first != m_selected_list.end() ) {
00419     item = *first++;
00420     QRect r = item->boundingRect ();
00421     rect |= r;
00422   }
00423 
00424   return rect;
00425 }
00426 
00427 double
00428 FigureEditor::
00429 maximumZ () const
00430 {
00431   /* This is only used to determine if the view is a text view */
00432   QtView* view = NULL;
00433 
00434 #if QT_VERSION < 0x040000
00435   vector < QCanvasItem * > ::const_iterator first = m_items.begin();
00436   double max_z = (*first) -> z ();
00437   
00438   /* Get z()-100 for text plotter */
00439   view = dynamic_cast<QtView*> (*first);
00440   if (view && view->isTextView())
00441     max_z -= 100;
00442   
00443   first++;
00444   
00445   
00446   while ( first != m_items.end () ) {
00447     QCanvasItem * item = *first;
00448 #else
00449   vector < Q3CanvasItem * > ::const_iterator first = m_items.begin();
00450   double max_z = (*first) -> z ();
00451 
00452   /* z()-100 for text */
00453   view = dynamic_cast<QtView*> (*first);
00454   if (view && view->isTextView())
00455     max_z -= 100;
00456   
00457   first++;
00458 
00459   while ( first != m_items.end () ) {
00460     Q3CanvasItem * item = *first;
00461 #endif
00462     double z = item -> z ();
00463 
00464     /* z()-100 for text */
00465     view = dynamic_cast<QtView*> (item);
00466     if (view && view->isTextView())
00467       z -= 100;
00468 
00469     
00470     first++;
00471     
00472     max_z = std::max ( max_z, z );
00473   }
00474 
00475   return max_z;
00476 }
00477 
00478 void
00479 FigureEditor::
00480 setUnselectedVisible ( bool yes )
00481 {
00482 #if QT_VERSION < 0x040000
00483   vector < QCanvasItem * > ::iterator first = m_items.begin();
00484   while ( first != m_items.end() ) {
00485     QCanvasItem * item = *first++;
00486 #else
00487   vector < Q3CanvasItem * > ::iterator first = m_items.begin();
00488   while ( first != m_items.end() ) {
00489     Q3CanvasItem * item = *first++;
00490 #endif
00491     if ( item->isSelected() ) continue;
00492     item->setVisible ( yes );
00493   }
00494 }
00495 
00496 void
00497 FigureEditor::
00498 #if QT_VERSION < 0x040000
00499 setCollidingSelected ( const QCanvasItem * target )
00500 {
00501   vector < QCanvasItem * > ::iterator first = m_items.begin ();
00502   while ( first != m_items.end () ) {
00503     QCanvasItem * item = *first++;
00504 #else
00505 setCollidingSelected ( const Q3CanvasItem * target )
00506 {
00507   vector < Q3CanvasItem * > ::iterator first = m_items.begin ();
00508   while ( first != m_items.end () ) {
00509     Q3CanvasItem * item = *first++;
00510 #endif
00511     if ( item != target ) {
00512       if ( item -> collidesWith ( target ) ) {
00513         if ( item -> selected () == false ) {
00514           item -> setSelected ( true );
00515           m_selected_list.push_back ( item );
00516         }
00517       }
00518     }
00519   }
00520 }
00521 
00522 void 
00523 FigureEditor::
00524 setSelectedItems ( bool state )
00525 {
00526 #ifdef ITERATOR_MEMBER_DEFECT
00527   std::
00528 #endif
00529 #if QT_VERSION < 0x040000
00530   vector < QCanvasItem * > ::iterator first = m_selected_list.begin();
00531   while ( first != m_selected_list.end() ) {
00532     QCanvasItem * item = *first++;
00533 #else
00534   vector < Q3CanvasItem * > ::iterator first = m_selected_list.begin();
00535   while ( first != m_selected_list.end() ) {
00536     Q3CanvasItem * item = *first++;
00537 #endif
00538     item->setSelected ( state );
00539   }
00540 }
00541 
00542 void FigureEditor::clearSelectedList ()
00543 {
00544   if ( m_selected_list.empty () == true ) return;
00545 
00546   setSelectedItems ( false );
00547 
00548   m_selected_list.clear();
00549 }
00550 
00551 void
00552 FigureEditor::
00553 setAllSelected ( bool yes )
00554 {
00555 #if QT_VERSION < 0x040000
00556 #ifdef MEMFUN1_DEFECT
00557   for_each ( m_items.begin(), m_items.end(),
00558              bind2nd ( mem_fun1 ( &QCanvasItem::setSelected ), yes ) );
00559 #else
00560   for_each ( m_items.begin(), m_items.end(),
00561              bind2nd ( mem_fun ( &QCanvasItem::setSelected ), yes ) );
00562 #endif
00563 #else
00564 #ifdef MEMFUN1_DEFECT
00565   for_each ( m_items.begin(), m_items.end(),
00566              bind2nd ( mem_fun1 ( &Q3CanvasItem::setSelected ), yes ) );
00567 #else
00568   for_each ( m_items.begin(), m_items.end(),
00569              bind2nd ( mem_fun ( &Q3CanvasItem::setSelected ), yes ) );
00570 #endif
00571 #endif
00572   if (yes) {
00573      m_selected_list = m_items;
00574   }
00575 }
00576 
00577 void FigureEditor::setSelectedFlags ()
00578 {
00579 
00580   // Set all selected flags to false.
00581   setAllSelected ( false );
00582 
00583   // Set all selected flags to true from m_selected_list.
00584 #if QT_VERSION < 0x040000
00585   vector < QCanvasItem * > :: iterator first = m_selected_list.begin();
00586   while ( first != m_selected_list.end() ) {
00587     QCanvasItem * item = *first++;
00588 #else
00589   vector < Q3CanvasItem * > :: iterator first = m_selected_list.begin();
00590   while ( first != m_selected_list.end() ) {
00591     Q3CanvasItem * item = *first++;
00592 #endif
00593     item->setSelected ( true );
00594   }
00595 }
00596 
00597 void
00598 FigureEditor::
00599 customEvent ( QCustomEvent * event )
00600 {
00601     CanvasEvent * ce = dynamic_cast < CanvasEvent * > ( event );
00602     if (  ce != 0 ) {
00603 #if QT_VERSION < 0x040000
00604         const QCanvasItem * item = ce -> item ();
00605 #else
00606         const Q3CanvasItem * item = ce -> item ();
00607 #endif
00608         makeVisible ( item );
00609     }
00610 }
00611 void
00612   FigureEditor::
00613 #if QT_VERSION < 0x040000
00614   makeVisible ( const QCanvasItem * item ) 
00615 #else
00616   makeVisible ( const Q3CanvasItem * item )
00617 #endif
00618 {
00619   if ( item != 0 ) {
00620     QRect rect = item->boundingRect ();
00621     int x = 0, y = 0, w = 0, h = 0;
00622     rect.rect( & x, & y, & w, & h );
00623 #if QT_VERSION < 0x040000
00624     QCanvasView::ensureVisible ( x, y, w, h );
00625 #else
00626     Q3CanvasView::ensureVisible ( x, y, w, h );
00627 #endif
00628   }
00629 }
00630 
00631 void
00632 FigureEditor::
00633 #if QT_VERSION < 0x040000
00634 setSelectedItem ( QCanvasItem * item )
00635 #else
00636 setSelectedItem ( Q3CanvasItem * item )
00637 #endif
00638 {
00639 
00640   m_selected_list.clear ();
00641   setSelectedFlags ();
00642   addSelectedItem ( item );
00643 
00644   double z = maximumZ ();
00645 
00646   /* SetZ for text plotter */
00647   const QtView* view = dynamic_cast<const QtView*> (item);
00648   if (view && view->isTextView())
00649     item->setZ( z + 101.0);   // ensures it is on top of other text plotters
00650   else
00651     item -> setZ ( z + 1.0 ); // ensures it is on top
00652 
00653   notifyObservers ();
00654 }
00655 
00656 void
00657 FigureEditor::
00658 #if QT_VERSION < 0x040000
00659 ensureVisible ( const QCanvasItem * item )
00660 #else
00661 ensureVisible ( const Q3CanvasItem * item )
00662 #endif
00663 {
00664 #ifdef _MSC_VER
00665 CanvasEvent * event = new CanvasEvent ( item );
00666 qApp -> postEvent ( this, event );
00667 #else
00668  makeVisible ( item );
00669 #endif
00670 }
00671 
00672 void 
00673 FigureEditor::
00674 setLocked ( bool yes )
00675 {
00676 #if QT_VERSION < 0x040000
00677   // here
00678   const vector < QCanvasItem * > & items = getSelectedItems ();
00679   if ( items.empty () ) return; // shouldn't be called
00680 
00681 #ifdef MEMFUN_DEFECT
00682   vector < QCanvasItem * >:: const_iterator first = items.begin ();
00683   while ( first != items.end () ) {
00684     QCanvasItem * item = *first++;
00685     item->setActive ( yes );
00686   }
00687 #else
00688 #ifdef MEMFUN1_DEFECT
00689     for_each ( items.begin(), items.end(),
00690                bind2nd ( mem_fun1 ( &QCanvasItem::setActive ), yes ) );
00691 #else
00692     for_each ( items.begin(), items.end(),
00693                bind2nd ( mem_fun ( &QCanvasItem::setActive ), yes ) );
00694 #endif
00695 #endif
00696     // here
00697 #else
00698   // here
00699   const vector < Q3CanvasItem * > & items = getSelectedItems ();
00700   if ( items.empty () ) return; // shouldn't be called
00701 
00702 #ifdef MEMFUN_DEFECT
00703   vector < Q3CanvasItem * >:: const_iterator first = items.begin ();
00704   while ( first != items.end () ) {
00705     Q3CanvasItem * item = *first++;
00706     item->setActive ( yes );
00707   }
00708 #else
00709 #ifdef MEMFUN1_DEFECT
00710     for_each ( items.begin(), items.end(),
00711                bind2nd ( mem_fun1 ( &Q3CanvasItem::setActive ), yes ) );
00712 #else
00713     for_each ( items.begin(), items.end(),
00714                bind2nd ( mem_fun ( &Q3CanvasItem::setActive ), yes ) );
00715 #endif
00716 #endif
00717     // here
00718 #endif
00719 }
00720 
00724 void
00725 FigureEditor::
00726 #if QT_VERSION < 0x040000
00727 placeGraphOnSelected ( QCanvasRectangle * view )
00728 #else
00729 placeGraphOnSelected ( Q3CanvasRectangle * view )
00730 #endif
00731 {
00732 
00733   if ( !isSingleItemSelected() ) return;
00734 
00735 #if QT_VERSION < 0x040000
00736   QCanvasItem * sel_item = selectedItem ();
00737 #else
00738   Q3CanvasItem * sel_item = selectedItem ();
00739 #endif
00740   QRect rect = sel_item->boundingRect ();
00741 
00742   view->setX ( rect.x() );
00743   view->setY ( rect.y() );
00744   view->setSize ( rect.width(), rect.height () );
00745 
00746   view->setCanvas ( m_canvas );
00747   m_items.push_back ( view );
00748   view->setSelected ( true );
00749 
00750   view->show ();
00751 }
00752 
00756 void FigureEditor::contentsMousePressEvent ( QMouseEvent* e )
00757 {
00758   // If both Shift and Control buttons are down, then error, do nothing.
00759   
00760 #if QT_VERSION < 0x040000
00761   if ( e->state() == Qt::ShiftButton && 
00762        e->state() == Qt::ControlButton ) return;
00763 #else
00764   if ( ( e -> modifiers () & Qt::ShiftModifier ) &&
00765        ( e -> modifiers() & Qt::ControlModifier ) ) return;
00766 #endif
00767 
00768   QPoint p = inverseWorldMatrix().map(e->pos());
00769 #if QT_VERSION < 0x040000
00770   QCanvasItemList l = m_canvas->collisions ( p );
00771   QCanvasItemList::Iterator it = l.begin(); // for MS VC++
00772   QCanvasItem * selItem = 0;  
00773 #else
00774   Q3CanvasItemList l = m_canvas->collisions ( p );
00775   Q3CanvasItemList::Iterator it = l.begin(); // for MS VC++
00776   Q3CanvasItem * selItem = 0;  
00777 #endif
00778   for ( ; it!=l.end(); ++it) {
00779     selItem = *it;
00780 #if QT_VERSION < 0x040000
00781     vector < QCanvasItem * > ::const_iterator first 
00782 #else
00783     vector < Q3CanvasItem * > ::const_iterator first 
00784 #endif
00785       = std::find ( m_items.begin (), m_items.end (), selItem );
00786     if ( first != m_items.end () ) { // only accept views
00787       m_moving_start = p;
00788       m_zoom_start = p;
00789       break;
00790     }
00791     else {
00792       selItem = 0;
00793     }
00794   }
00795 
00796   if ( selItem == 0 )  {
00797       // There was no display where the click occurred. Ignore the click if
00798       // its control click, else clear the selected list.
00799 #if QT_VERSION < 0x040000
00800     if ( e->state() != Qt::ControlButton ) {
00801 #else
00802       if ( ( e -> modifiers() & Qt::ControlModifier ) == 0 ) {
00803 #endif
00804         m_selected_list.clear();
00805         setSelectedFlags();
00806         whereClicked();
00807         m_rightItem = 0;
00808         return;
00809       }
00810       else {
00811         return;
00812       }
00813     }
00814     
00815   // If control key is down, then add the current item to the vector, else
00816   // clear the vector and then add the current item to the vector.
00817 
00818   if (e->button() == Qt::LeftButton)
00819     {
00820 
00821       // Shift click is used to select multiple items when not in zoom mode.
00822       
00823 #if QT_VERSION < 0x040000
00824       if (( e->state() == Qt::ShiftButton ) && (!getZoomMode()))
00825 #else
00826       if (( e -> modifiers () & Qt::ShiftModifier ) &&( !getZoomMode()))
00827 #endif
00828     {
00829       if ( selItem -> isSelected () ) {
00830         removeSelectedItem ( selItem );
00831       }
00832       else {
00833         addSelectedItem ( selItem );      
00834       }
00835     }
00836 #if QT_VERSION < 0x040000
00837   else if ( e->state() == Qt::ControlButton )
00838 #else
00839   else if ( e -> modifiers() & Qt::ControlModifier )
00840 #endif
00841     {
00842       m_selected_list.clear();
00843       setSelectedFlags();
00844       addSelectedItem ( selItem );
00845 
00846       controlMousePressEvent (); // handled by derived class
00847 
00848       whereClicked ();
00849     }
00850 
00851   else
00852     { // neither shift nor control
00853 #if QT_VERSION < 0x040000
00854       vector < QCanvasItem * > ::const_iterator first 
00855 #else
00856       vector < Q3CanvasItem * > ::const_iterator first 
00857 #endif
00858         = std::find ( m_selected_list.begin (), m_selected_list.end (),
00859                       selItem );
00860       // Selected plot. Drag to move or click to select signal plot.
00861       if ( first != m_selected_list.end () )  
00862         {
00863           m_mouse_down = p;
00864           m_isMouseDown = true;
00865           m_preSelected = selItem;
00866           whereClicked();
00867           return;
00868         }
00869 
00870       // Not selected. Select the plot when mouse down.
00871       else 
00872         {  
00873           setSelectedItem ( selItem );
00874           whereClicked();
00875         }
00876     }
00877     }
00878   
00879 
00884   if ( e->button() == Qt::RightButton ||
00885        e->state() == Qt::RightButton )
00886   {
00887     double z = maximumZ ();
00888 
00889     /* SetZ for text plotter */
00890     const QtView* view = dynamic_cast<const QtView*> (selItem);
00891     if (view && view->isTextView())
00892       selItem->setZ( z + 101.0);   // ensures it is on top of other text plotters
00893     else
00894       selItem -> setZ ( z + 1.0 ); // ensures it is on top
00895     
00896     m_rightItem = selItem; 
00897   }
00898 }
00899 
00900 
00901 
00902 void FigureEditor::whereClicked ( )
00903 {
00904 
00905   m_whereClicked = invalid;
00906   
00907   if ( !isSingleItemSelected() ) return;
00908   
00909   m_whereClicked = none;
00910 
00911 #if QT_VERSION < 0x040000
00912   QCanvasRectangle * rect 
00913     = dynamic_cast <QCanvasRectangle *> ( selectedItem() );
00914 #else
00915   Q3CanvasRectangle * rect 
00916     = dynamic_cast <Q3CanvasRectangle *> ( selectedItem() );
00917 #endif
00918   
00919   // The margin around the point where a click will be registered.
00920   int clickMargin = 10;
00921 
00922   QPoint p = m_moving_start;
00923 
00924   if ( ( p.x() < rect->x() + clickMargin ) &&
00925        ( p.y() < rect->y() + clickMargin ) )
00926     {
00927       m_whereClicked = upper_left;
00928     }
00929 
00930   if ( ( p.x() < ( rect->x() + (rect->width() / 2) + clickMargin ) ) &&
00931        ( p.x() > ( rect->x() + (rect->width() / 2) - clickMargin ) ) &&
00932        ( p.y() < rect->y() + clickMargin ) )
00933     {
00934       m_whereClicked = upper_middle;
00935     }
00936 
00937   if ( ( p.x() > ( rect->x() + rect->width() - clickMargin ) ) &&
00938        ( p.y() < rect->y() + clickMargin ) )
00939     {
00940       m_whereClicked = upper_right;
00941     }
00942 
00943   if ( ( p.x() < rect->x() + clickMargin ) &&
00944        ( p.y() < ( rect->y() + (rect->height() / 2) + clickMargin ) ) &&
00945        ( p.y() > ( rect->y() + (rect->height() / 2) - clickMargin ) ) )
00946     {
00947       m_whereClicked = middle_left;
00948     }
00949 
00950   if ( ( p.x() > ( rect->x() + rect->width() - clickMargin ) ) &&
00951        ( p.y() < ( rect->y() + (rect->height() / 2) + clickMargin ) ) &&
00952        ( p.y() > ( rect->y() + (rect->height() / 2) - clickMargin ) ) )
00953     {
00954       m_whereClicked = middle_right;
00955     }
00956 
00957   if ( ( p.x() < rect->x() + clickMargin ) &&
00958        ( p.y() > rect->y() + rect->height() - clickMargin ) )
00959     {
00960       m_whereClicked = lower_left;
00961     }
00962 
00963   if ( ( p.x() < ( rect->x() + (rect->width() / 2) + clickMargin ) ) &&
00964        ( p.x() > ( rect->x() + (rect->width() / 2) - clickMargin ) ) &&
00965        ( p.y() > rect->y() + rect->height() - clickMargin ) )
00966     {
00967       m_whereClicked = lower_middle;
00968     }
00969   
00970   if ( ( p.x() > ( rect->x() + rect->width()  - clickMargin ) ) &&
00971        ( p.y() > ( rect->y() + rect->height() - clickMargin ) ) )
00972     {
00973       m_whereClicked = lower_right;
00974     }
00975 
00976 }
00977 
00978 void
00979 FigureEditor::
00980 #if QT_VERSION < 0x040000
00981 removeFromItemList ( QCanvasItem * item )
00982 {
00983   vector < QCanvasItem * > ::iterator last 
00984 #else
00985 removeFromItemList ( Q3CanvasItem * item )
00986 {
00987   vector < Q3CanvasItem * > ::iterator last 
00988 #endif
00989     = std::remove ( m_items.begin (), m_items.end (), item );
00990   m_items.erase ( last, m_items.end () );
00991 }
00992 
00993 void
00994 FigureEditor::
00995 #if QT_VERSION < 0x040000
00996 addToItemList ( QCanvasItem * item )
00997 #else
00998 addToItemList ( Q3CanvasItem * item )
00999 #endif
01000 {
01001   item->setCanvas ( m_canvas );
01002   m_items.push_back ( item );
01003 }
01004 
01005 void
01006 FigureEditor::
01007 #if QT_VERSION < 0x040000
01008 remove ( QCanvasItem * item )
01009 #else
01010 remove ( Q3CanvasItem * item )
01011 #endif
01012 {
01013   removeSelectedItem ( item );
01014   //m_selected_list.clear();
01015   removeFromItemList ( item );
01016 
01017   //item->hide();
01018   //m_canvas->update();
01019   delete item;
01020 }
01021 
01022 void
01023 FigureEditor::
01024 #if QT_VERSION < 0x040000
01025 hide ( QCanvasItem * item )
01026 #else
01027 hide ( Q3CanvasItem * item )
01028 #endif
01029 {
01030   m_selected_list.clear();
01031   removeFromItemList ( item );
01032 
01033   item->hide();
01034   //m_canvas->update();
01035   //delete item;
01036 }
01037 
01038 void FigureEditor::clear()
01039 {
01040 #if QT_VERSION < 0x040000
01041   vector < QCanvasItem * >:: iterator it = m_items.begin();
01042 #else
01043   vector < Q3CanvasItem * >:: iterator it = m_items.begin();
01044 #endif
01045   for ( ; it != m_items.end(); ++it ) {
01046     if ( *it ) delete *it;
01047   }
01048 
01049   m_items.clear();
01050   m_selected_list.clear();
01051 }
01052 
01060 void
01061 FigureEditor::
01062 contentsMouseMoveEvent ( QMouseEvent * e, double aspect )
01063 {
01064   // Zoom mode is processed in CanvasView class.
01065   if ( getZoomMode() ) return;
01066 
01067   // Right button movement is processed by CanvasView class.
01068 
01069   QPoint p = inverseWorldMatrix().map(e->pos());
01070 
01071   if ( m_whereClicked == invalid ) return;
01072   if ( !isSingleItemSelected() ) return;
01073 
01074 #if QT_VERSION < 0x040000
01075   QCanvasItem * selItem = selectedItem();
01076   QCanvasRectangle * rect 
01077     = dynamic_cast <QCanvasRectangle *> ( selItem );
01078 #else
01079   Q3CanvasItem * selItem = selectedItem();
01080   Q3CanvasRectangle * rect 
01081     = dynamic_cast <Q3CanvasRectangle *> ( selItem );
01082 #endif
01083   
01084   QtView * view = dynamic_cast < QtView * > ( selItem );
01085   QtGroupView * gv = dynamic_cast < QtGroupView * > (selItem);
01086 
01087   float p_x = p.x ();
01088   float p_y = p.y ();
01089 
01090   float d_x = m_moving_start.x() - p_x;
01091   float d_y = m_moving_start.y() - p_y;
01092 
01093   float new_x = rect -> x();
01094   float new_y = rect -> y();
01095   float new_w = rect -> width ();
01096   float new_h = rect -> height ();
01097 
01098   switch ( m_whereClicked )
01099     {
01100     
01101           
01102     case none:  // Move the selected view and all target text plotters
01103       if ( ! (getZoomMode() ) ) {
01104         if (view) movePlotterWithText(selItem, p.x()-m_moving_start.x(), p.y()-m_moving_start.y());
01105         if (gv) selItem->moveBy(p.x()-m_moving_start.x(), p.y()-m_moving_start.y());
01106       }
01107       break;
01108 
01109     case upper_left:
01110 
01111       new_x = p_x;
01112       new_y = p_y;
01113       new_w += d_x;
01114       new_h += d_y;
01115       if ( aspect > 0 ) {
01116         new_h += d_x;
01117         new_w = new_h * aspect;
01118       }
01119       new_w = std::max ( new_w, 20.0f );
01120       new_h = std::max ( new_h, 20.0f );
01121 
01122       if (view) view -> setDrawRect ( new_x, new_y, new_w, new_h );
01123       if (gv)  gv -> setDrawRect ( new_x, new_y, new_w, new_h );
01124 
01125       rect->setEnabled ( true );
01126       break;
01127         
01128     case upper_middle:
01129 
01130       // new_x unchanged      
01131       new_y = p_y;
01132       // new_w = unchanged
01133       new_h += d_y;
01134 
01135       if ( aspect > 0 ) {
01136           new_w += aspect * d_y;          
01137           new_x -= 0.5 * aspect * d_y;
01138       }
01139 
01140       new_w = std::max ( new_w, 20.0f );
01141       new_h = std::max ( new_h, 20.0f );
01142 
01143       if (view) view -> setDrawRect ( new_x, p_y, new_w, new_h );
01144       if (gv)  gv -> setDrawRect ( new_x, p_y, new_w, new_h );
01145 
01146       rect->setEnabled ( true );
01147       break;
01148         
01149     case upper_right:
01150       
01151       // new_x unchanged
01152       new_y = p_y;
01153       new_w -= d_x;
01154       new_h += d_y;
01155       if ( aspect > 0 ) {
01156           new_h -= d_x;
01157           new_w = aspect * new_h;
01158       }
01159 
01160       new_w = std::max ( new_w, 20.0f );
01161       new_h = std::max ( new_h, 20.0f );
01162 
01163       if (view) view -> setDrawRect ( new_x, new_y, new_w, new_h );
01164       if (gv) gv->setDrawRect ( new_x, new_y, new_w, new_h );
01165           
01166       rect->setEnabled ( true );
01167       break;
01168         
01169     case middle_left:
01170 
01171       new_x = p_x;
01172       // new_y unchanged
01173       new_w += d_x;
01174       // new_h unchanged
01175 
01176       if ( aspect > 0 ) {
01177           new_h += d_x / aspect;
01178           new_w = new_h * aspect;
01179       }
01180 
01181       new_w = std::max ( new_w, 20.0f );
01182       new_h = std::max ( new_h, 20.0f );
01183 
01184       if (view) view -> setDrawRect ( new_x, new_y, new_w, new_h );
01185       if (gv) gv -> setDrawRect ( new_x, new_y, new_w, new_h );
01186           
01187       rect->setEnabled ( true );
01188       break;
01189         
01190     case middle_right:
01191 
01192       // new_x unchanged
01193       // new_y unchanged
01194       new_w -= d_x;
01195       // new_h unchanged
01196 
01197       if ( aspect > 0 ) {
01198         new_h -= d_x / aspect;
01199         new_w = new_h * aspect;
01200       }
01201 
01202       new_w = std::max ( new_w, 20.0f );
01203       new_h = std::max ( new_h, 20.0f );
01204 
01205       if (view) view -> setDrawRect ( new_x, new_y, new_w, new_h );
01206       if (gv) gv -> setDrawRect ( new_x, new_y, new_w, new_h );
01207 
01208       rect->setEnabled ( true );
01209       break;
01210         
01211     case lower_left:
01212 
01213       new_x = p_x;
01214       // new_y unchanged
01215       new_w += d_x;
01216       new_h-= d_y;
01217       
01218       if ( aspect > 0 ) {
01219           new_h += d_x;
01220           new_y -= d_x;
01221           new_w = new_h * aspect;
01222       }
01223          
01224       new_w = std::max ( new_w, 20.0f );
01225       new_h = std::max ( new_h, 20.0f );
01226 
01227       if (view) view -> setDrawRect ( new_x, new_y, new_w, new_h );
01228       if (gv) gv -> setDrawRect ( new_x, new_y, new_w, new_h );
01229 
01230       rect->setEnabled ( true );
01231       break;
01232         
01233     case lower_middle:
01234      
01235       // new_x unchanged
01236       // new_y unchanged
01237       // new_w unchanged
01238       new_h -= d_y;
01239 
01240       if ( aspect > 0 ) {
01241           new_w = new_h * aspect;
01242           new_x += 0.5 * aspect * d_y;
01243       }
01244       new_w = std::max ( new_w, 20.0f );
01245       new_h = std::max ( new_h, 20.0f );
01246 
01247       if (view) view -> setDrawRect ( new_x, new_y, new_w, new_h );
01248       if (gv) gv -> setDrawRect ( new_x, new_y, new_w, new_h );
01249 
01250       rect->setEnabled ( true );
01251       break;
01252         
01253     case lower_right:
01254 
01255       // new_x unchanged
01256       // new_y unchanged
01257       new_w -= d_x;
01258       new_h -= d_y;      
01259 
01260       if ( aspect > 0 ) {
01261         new_h -= d_x;
01262         new_w = aspect * new_h;
01263         new_y += d_x;
01264       }
01265       new_w = std::max ( new_w, 20.0f );
01266       new_h = std::max ( new_h, 20.0f );
01267 
01268       if (view) view -> setDrawRect ( new_x, new_y, new_w, new_h );
01269       if (gv) gv -> setDrawRect ( new_x, new_y, new_w, new_h );
01270 
01271       rect->setEnabled ( true );
01272       break;
01273           
01274     default:
01275 
01276       break;
01277         
01278     }
01279   
01280   m_moving_start = p;
01281   
01282   m_canvas->update();
01283 
01284 }
01285 
01286 
01287 
01290 void
01291 FigureEditor::
01292 contentsMouseReleaseEvent ( QMouseEvent * e )
01293 {
01294   if (!m_isMouseDown) return;
01295 
01296   QPoint p = inverseWorldMatrix().map(e->pos());
01297 
01298   // Mouse has not moved, process as a click.
01299   if ( p==m_mouse_down ) {
01300     setSelectedItem(m_preSelected);
01301     m_isMouseDown = false;
01302   }
01303 
01304 }
01305 
01306 
01307 void
01308 FigureEditor::
01309 #if QT_VERSION < 0x040000
01310 placeGraph ( QCanvasRectangle * item )
01311 #else
01312 placeGraph ( Q3CanvasRectangle * item )
01313 #endif
01314 {
01315   item -> setX ( 0 );
01316   item -> setY ( 0 );
01317 
01318   int margin_x = 0;
01319   int margin_y = 0;
01320   int max_width = 0;
01321 
01322   if ( m_ignore_margin == false ) {
01323     margin_x = ( m_apage_w - m_upage_w ) / 2;
01324     margin_y = ( m_apage_h - m_upage_h ) / 2;
01325     max_width = m_apage_w - margin_x;
01326   }
01327   else {
01328     max_width = m_canvas -> width ();
01329   }
01330 
01331   if ( item -> x () < margin_x ) item -> setX ( margin_x + 1 );
01332   if ( item -> y () < margin_y ) item -> setY ( margin_y + 1 );
01333 
01334   if ( m_items.empty () ) return;
01335 
01336   double deltax = 1.05 * item -> width ();
01337   double deltay = 1.05 * item -> height ();
01338 
01339   while ( true ) {
01340     if ( m_ignore_margin == false ) {
01341       QRect brect = item->boundingRect ();
01342       int top = brect.top () / m_apage_h;
01343       int bot = brect.bottom () / m_apage_h;
01344       if ( top != bot  ) { // across page boundary
01345         int new_y = bot * m_apage_h;
01346         new_y += ( m_apage_h - m_upage_h ) / 2;
01347         item->setY ( new_y ); // beginning next actual page
01348       }
01349     }
01350 
01351 #if QT_VERSION < 0x040000
01352     vector < QCanvasItem * >:: iterator it = m_items.begin();
01353 #else
01354     vector < Q3CanvasItem * >:: iterator it = m_items.begin();
01355 #endif
01356     bool collides = false;
01357 
01358     for ( ; it != m_items.end(); ++it ) {
01359       if ( (*it)->collidesWith ( item ) ) {
01360         collides = true;
01361         break;
01362       }
01363     }
01364 
01365     if ( !collides ) break;
01366     if ( item -> x() + deltax + item -> width () < max_width ) {
01367       item -> moveBy ( deltax, 0 ); // move across
01368     }
01369     else {
01370       item -> setX ( margin_x + 1 );
01371       item -> moveBy ( 0, deltay ); // move down
01372     }
01373 
01374   } // while true
01375 
01376 }
01377 
01378 void
01379 FigureEditor::
01380 #if QT_VERSION < 0x040000
01381 add ( QCanvasItem * item ) 
01382 #else
01383 add ( Q3CanvasItem * item ) 
01384 #endif
01385 { 
01386   resizeCanvasToFit ( item );
01387   
01388   item->setCanvas ( m_canvas );
01389   m_items.push_back ( item );
01390   item->setSelected ( false );
01391 
01392   item->show ();
01393   QRect area = item -> boundingRect ();
01394   m_canvas -> setChanged ( area );
01395   m_canvas -> update();
01396 }
01397 
01400 void
01401 FigureEditor::
01402 #if QT_VERSION < 0x040000
01403 paste ( QCanvasItem * item )
01404 #else
01405 paste ( Q3CanvasItem * item )
01406 #endif
01407 {
01408   add ( item );
01409 #if QT_VERSION < 0x040000
01410   QCanvasItemList items = item->collisions ( true );
01411 #else
01412   Q3CanvasItemList items = item->collisions ( true );
01413 #endif
01414   unsigned int count = items.count ();
01415   if ( count > 1 ) {
01416 
01417     item->moveBy ( 10., 10. );
01418   }
01419 }
01420 
01421 void
01422 FigureEditor::
01423 #if QT_VERSION < 0x040000
01424 resizeCanvasToFit ( QCanvasItem * item )
01425 #else
01426 resizeCanvasToFit ( Q3CanvasItem * item )
01427 #endif
01428 {
01429   QRect brect = item->boundingRect ();
01430   QPoint p = brect.bottomRight ();
01431   if ( ! m_canvas->onCanvas ( p ) )
01432     {
01433       int new_w = m_canvas->width ();
01434       int old_h = m_canvas->height();
01435       int new_h = old_h + m_apage_h;
01436       m_canvas->resize ( new_w, new_h );
01437       addPageMargin ( 0, old_h );
01438   }
01439 }
01440 
01441 void
01442 FigureEditor::
01443 scaleBy ( double factor )
01444 {
01445   m_apage_w = static_cast < int > ( factor * m_apage_w );
01446   m_apage_h = static_cast < int > ( factor * m_apage_h );
01447   m_upage_w = static_cast < int > ( factor * m_upage_w );
01448   m_upage_h = static_cast < int > ( factor * m_upage_h );
01449   
01450   m_canvas -> resize ( m_apage_w, m_apage_h );
01451   resizeContents ( m_apage_w, m_apage_h );
01452   
01453   m_scale_factor = factor;
01454   
01455   if( m_printer_bounds == true )
01456     {
01457       showPrinterMargins ( false ); // Delete old ones ( if any    )
01458       showPrinterMargins ( true );  // Redraw new ones ( if needed )
01459     }
01460 }
01461 
01462 void
01463 FigureEditor::
01464 viewZoomIn ()
01465 {
01466 #if QT_VERSION < 0x040000
01467     QWMatrix matrix = worldMatrix ();
01468 #else
01469     QMatrix matrix = worldMatrix ();
01470 #endif
01471     matrix.scale( 4./3., 4./3. );
01472 
01473     setWorldMatrix ( matrix );
01474 }
01475 
01479 void
01480 FigureEditor::
01481 viewZoomOut ()
01482 {
01483 #if QT_VERSION < 0x040000
01484     QWMatrix matrix = worldMatrix ();
01485 #else
01486     QMatrix matrix = worldMatrix ();
01487 #endif
01488     matrix.scale( 3./4., 3./4. );
01489 
01490     setWorldMatrix ( matrix );
01491 }
01492 
01493 void
01494 FigureEditor::
01495 viewZoomReset ()
01496 {
01497 #if QT_VERSION < 0x040000
01498   QWMatrix matrix = worldMatrix ();
01499 #else
01500   QMatrix matrix = worldMatrix ();
01501 #endif
01502   matrix.reset();
01503 
01504   setWorldMatrix ( matrix );
01505 }
01506 
01507 void
01508 FigureEditor::
01509 savePrinterSettings ()
01510 {
01511   QSettings settings;
01512   settings.insertSearchPath ( QSettings::Windows, s_registry );
01513 
01514   QPrinter::PageSize page_size = s_printer -> pageSize ();
01515   settings.writeEntry ( s_app_key + "Printer/PageSize", page_size );
01516 
01517 }
01518 
01519 void
01520 FigureEditor::
01521 setPrinterSettings ()
01522 {
01523  #if QT_VERSION < 0x040000
01524  bool yes = s_printer -> setup ( this );
01525 //  if ( yes == false ) return;
01526 #else
01527  QPrintDialog dialog ( s_printer, this );
01528  bool yes = ( dialog.exec () == QDialog::Accepted );
01529 #endif
01530 
01531  if ( yes ) {
01532    savePrinterSettings ();
01533  }
01534 
01535 }
01536 
01539 void
01540 FigureEditor::
01541 print ()
01542 {
01543   bool yes = s_printer->setup ( this );
01544   if ( yes == false ) return;
01545 
01546   print ( s_printer );
01547 
01548 }
01549 
01550 void
01551 FigureEditor::
01552 print ( const std::string & filename )
01553 {
01554   const QString name = filename.c_str();
01555   s_printer -> setOutputFileName ( name );
01556   s_printer -> setOutputToFile ( true );
01557 
01558   print ( s_printer );
01559 }
01560 
01561 void
01562 FigureEditor::
01563 print ( QPrinter * printer )
01564 {
01565   setSelectedItems ( false ); // hide border
01566   QPainter painter;
01567   bool ok = painter.begin ( printer );
01568   assert ( ok );
01569 
01570   calcPrinterMetrics ( painter.device () );
01571   double scale = 1.0 / m_scale_factor;
01572   painter.scale ( scale, scale );
01573   int page_y = 0;
01574   int trans_y = static_cast < int > ( m_apage_h * m_scale_factor );
01575 
01576   while ( page_y < m_canvas->height() ) {
01577     const QRect clip ( 0, page_y, m_apage_w, m_apage_h );
01578 #if QT_VERSION < 0x040000
01579     QCanvasItemList items = m_canvas->collisions ( clip );
01580 #else
01581     Q3CanvasItemList items = m_canvas->collisions ( clip );
01582 #endif
01583 
01584     if ( items.empty () ) {
01585       page_y += m_apage_h;
01586       painter.translate ( 0, - trans_y );
01587       continue;
01588     }
01589 
01590     if ( page_y != 0 ) printer->newPage();
01591     page_y += m_apage_h;
01592 
01593     bool dbuf = false;
01594 
01595     m_canvas->drawArea ( clip, & painter, dbuf );
01596     painter.translate ( 0, - trans_y );
01597   }
01598 
01599   painter.end();
01600   setSelectedItems ( true ); // hide border
01601 }
01602 
01603 void
01604 FigureEditor::
01605 resizeEvent ( QResizeEvent * e )
01606 {
01607   const QSize & w_size = e->size();
01608 
01609   // need to specify type for vs.net compiler
01610   int new_w = std::max<int> ( w_size.width (),  m_canvas->width () );
01611   int new_h = std::max<int> ( w_size.height (), m_canvas->height () );
01612 
01613   m_canvas->resize ( new_w, new_h );
01614 
01615   updateScrollBars ();
01616 }
01617 
01618 void
01619 FigureEditor::
01620 addPage ()
01621 {
01622   int old_y = m_canvas->height ();
01623   m_canvas->resize ( m_canvas->width(), old_y + m_apage_h );
01624   addPageMargin ( 0, old_y );
01625 
01626   updateScrollBars ();
01627 }
01628 
01629 void 
01630 FigureEditor::
01631 saveSelectedAsPixmap ( const std::string & filename )
01632 {
01633   QRect rect = getSelectedBounds ();
01634   setSelectedItems ( false ); // hide for pixmap
01635   saveAreaAsPixmap ( rect, filename );
01636   setSelectedItems ( true ); // restore
01637 }
01638 
01639 QPixmap *
01640 FigureEditor::
01641 createPixmap ( const QRect & rect ) const
01642 {
01643   QPixmap * pixmap = new QPixmap ( rect.width(), rect.height() );
01644   QPainter painter;
01645 
01646   painter.begin ( pixmap );
01647   painter.translate ( -rect.x(), -rect.y() );
01648   m_canvas->drawArea ( rect, & painter );
01649   painter.end ();
01650 
01651   return pixmap;
01652 }
01653 
01654 void
01655 FigureEditor::
01656 saveAreaAsPixmap ( const QRect & rect, const std::string & filename )
01657 {
01658   QPixmap * pixmap = createPixmap ( rect );
01659 
01660   QString fn ( filename.c_str() );
01661   string::size_type i = filename.find_last_of ( '.' );
01662   string suffix ( filename.substr ( i + 1 ) );
01663   transform ( suffix.begin(), suffix.end(),
01664               suffix.begin(), toupper );
01665   if ( suffix == "JPG" ) suffix = "JPEG";
01666 
01667   pixmap -> save ( fn, suffix.c_str() );
01668 
01669   delete pixmap;
01670 }
01671 
01672 QImage
01673 FigureEditor::
01674 createImage ( const QRect & rect ) const
01675 {
01676   QPixmap * pixmap = createPixmap ( rect );
01677   QImage image = pixmap -> convertToImage ();
01678 
01679   return image;
01680 }
01681 
01682 void
01683 FigureEditor::
01684 copySelectedToClipboard ()
01685 {
01686   QRect rect = getSelectedBounds ();
01687   QImage image = createImage ( rect );
01688 #if QT_VERSION < 0x040000
01689   QImageDrag * image_drag = new QImageDrag ( image );
01690 #else
01691   Q3ImageDrag * image_drag = new Q3ImageDrag ( image );
01692 #endif
01693   QClipboard *  cb =  QApplication::clipboard ();
01694   cb -> setData ( image_drag );
01695 }
01696 
01699 void 
01700 FigureEditor::
01701 setZoomMode ( bool flag )
01702 {
01703   m_zoom_mode = flag;
01704   if ( flag ) {
01705     QApplication::setOverrideCursor( QCursor ( Qt::CrossCursor ) );
01706     QWidget * view_port = viewport ();
01707     view_port -> setMouseTracking ( TRUE );
01708   }
01709   else {
01710     QApplication::restoreOverrideCursor();
01711     QWidget * view_port = viewport ();
01712     view_port -> setMouseTracking ( FALSE );
01713   }
01714 }
01715 
01716 void
01717 FigureEditor::
01718 leaveEvent ( QEvent * )
01719 {
01720   QApplication::restoreOverrideCursor ();
01721 }
01722 
01723 void
01724 FigureEditor::
01725 enterEvent ( QEvent * )
01726 {
01727   if ( m_zoom_mode == true ) {
01728     QApplication::setOverrideCursor ( QCursor ( Qt::CrossCursor ) );
01729   }
01730 }
01731 
01732 bool 
01733 FigureEditor::
01734 getZoomMode ( ) const
01735 {
01736   return m_zoom_mode;
01737 }
01738 
01739 const QString &
01740 FigureEditor::
01741 getAppKey () const
01742 {
01743   return s_app_key;
01744 }
01745 
01746 const QString &
01747 FigureEditor::
01748 getRegistry () const
01749 {
01750   return s_registry;
01751 }
01752 
01753 #if QT_VERSION < 0x040000
01754 QCanvasItem * 
01755 #else
01756 Q3CanvasItem *
01757 #endif
01758 FigureEditor::getRightItem ()
01759 {
01760   return m_rightItem;
01761 }
01762 
01763 void
01764 FigureEditor::
01765 mouseMoveMultiItem( QMouseEvent * e )
01766 {
01767   if (getZoomMode()) return;
01768 
01769   QPoint p = inverseWorldMatrix().map(e->pos());
01770 
01771 #if QT_VERSION < 0x040000
01772   vector < QCanvasItem * >::iterator it = m_selected_list.begin();
01773 #else 
01774   vector < Q3CanvasItem * >::iterator it = m_selected_list.begin();
01775 #endif
01776 
01777   while ( it != m_selected_list.end() )
01778     {
01779       movePlotterWithText ( *it, p.x()-m_moving_start.x(), p.y()-m_moving_start.y());
01780       it++;
01781     }
01782   m_moving_start = p;  
01783 
01784   m_canvas->update();
01785 }
01786 
01787 void
01788 FigureEditor::
01789 #if QT_VERSION < 0X040000
01790 movePlotterWithText( QCanvasItem * item, float dx, float dy )
01791 #else 
01792 movePlotterWithText ( Q3CanvasItem * item, float dx, float dy )
01793 #endif  
01794 {
01795     
01796 #if QT_VERSION < 0x040000
01797   QCanvasItemList item_list = m_canvas->allItems();
01798   QCanvasItemList::Iterator it = item_list.begin();
01799 #else
01800   Q3CanvasItemList item_list = m_canvas->allItems();
01801   Q3CanvasItemList::Iterator it = item_list.begin();
01802 #endif
01803   
01804   QtView * xyview = dynamic_cast < QtView * > ( item );
01805   PlotterBase * xyplotter = xyview->getPlotter();
01806   
01807   /* Move the selected plotter */
01808   item->moveBy(dx,dy);
01809   
01810   /* Move all child text plotters */
01811   for ( ; it !=item_list.end(); ++it ) {
01812     const QtView * view = dynamic_cast < const QtView * > ( *it);
01813     if (view!=0){
01814       PlotterBase * plotter = view->getPlotter();
01815       if ( (plotter->isTextPlotter()) && (plotter->getParentPlotter()==xyplotter) ){
01816         (*it)->moveBy(dx,dy);
01817       }      
01818     }
01819   }     
01820 }
01821 
01822   

Generated for HippoDraw Class Library by doxygen