Diff of /trunk/EnlargerDialog.cpp [r44] .. [r45] Maximize Restore

  Switch to side-by-side view

--- a/trunk/EnlargerDialog.cpp
+++ b/trunk/EnlargerDialog.cpp
@@ -42,12 +42,17 @@
     : QMainWindow(parent), ui(new Ui::EnlargerDialog),
       calcThread( new EnlargerThread ), previewThread( new EnlargerThread )
 {
+    setUpdatesEnabled( false );
     ui->setupUi(this);
     MenuBarSetup();
     setCentralWidget( ui->centralwidget );
     zoomFact = 1.0;
+    aspectX = aspectY = 1.0;
+    freeAspectRatio = false;
     zoomInt = 100;
     zoomUpdateMod = useZoom;
+    ui->zoomRButton->setChecked( true );
+
     // avoid indirect self-calls of setZoom via diverse value-changes
     // recursionBlocker set true at begin of SetZoom, false at end
     setZoom_recursionBlocker = false;
@@ -90,10 +95,7 @@
              this,  SLOT(stopPreview() ) );
     connect( ui->selectField,   SIGNAL(clippingChanged( float, float, float, float )),
              this,  SLOT(slot_setClipping( float, float, float, float ) ) );
-    connect( ui->calcStopButton,SIGNAL(clicked()), this,  SLOT(stopEnlarge() ) );
     connect( ui->clipResetButton, SIGNAL(clicked()), this, SLOT(slot_resetClipping()) );
-
-    // connect( ui->quitButton, SIGNAL(clicked()), this, SLOT(close()));
 
     //--------- ToolTips ---------------------------------
     QString tipStr;
@@ -111,10 +113,9 @@
     SetToolTip( ui->newWidthBox,     QString(" explicitly set Width of result "                   ) );
     SetToolTip( ui->newHeightBox,    QString(" explicitly set Height of result "                  ) );
     SetToolTip( ui->clipResetButton, QString(" Undo the last cropping "                           ) );
-    SetToolTip( ui->zoomSlider, ui->zoomSpinBox, QString(" Set the Scale-factor ( in percent ) "  ) );
+    SetToolTip( ui->zoomSlider, ui->zoomSpinBox, QString(" Set the scale factor ( in percent ) "  ) );
     SetToolTip( ui->previewButton,   QString(" calculate the preview "                            ) );
     SetToolTip( ui->calcButton,      QString(" calculate the enlarged image "                     ) );
-    SetToolTip( ui->calcStopButton,  QString(" stop current calculation "                         ) );
 
     SetToolTip( ui->deNoiseSlider,    ui->deNoiseSpinBox,   QString(" remove some noise/artifacts in the source image "  ) );
     SetToolTip( ui->preSharpSlider,   ui->preSharpSpinBox,  QString(" simple sharpening of the source image "            ) );
@@ -161,6 +162,9 @@
     connect( ui->zoomSpinBox, SIGNAL(valueChanged(int)), this,  SLOT(SetZoomInt(int)) );
     connect( ui->newWidthBox, SIGNAL(valueChanged(int)), this,  SLOT(SetNewWidth(int)) );
     connect( ui->newHeightBox, SIGNAL(valueChanged(int)), this,  SLOT(SetNewHeight(int)) );
+    connect( ui->zoomRButton,   SIGNAL(pressed()), this, SLOT(slot_ZoomRadioButton()) );
+    connect( ui->widthRButton,  SIGNAL(pressed()), this, SLOT(slot_WidthRadioButton()) );
+    connect( ui->heightRButton, SIGNAL(pressed()), this, SLOT(slot_HeightRadioButton()) );
     ui->zoomSpinBox->setValue( 1100 );
 
     ui->sharpSpinBox->setRange( sharpMin, sharpMax );
@@ -242,7 +246,10 @@
     SetZoom( 5.0 );
     DoPreview();
     IncDestName();   // if dstName exists already: add or inc number at end of name
- }
+
+    resize( minimumSize() );
+    setUpdatesEnabled( true );
+}
 
 EnlargerDialog::~EnlargerDialog()
 {
@@ -261,6 +268,7 @@
 void EnlargerDialog::MenuBarSetup( void ) {
    QMenu *fileMenu, *helpMenu;
    QAction *aboutA, *quitA, *loadA, *saveA, *pasteA;
+   QAction *smallA;
    QAction *helpStartA, *helpClipA, *helpSliderA, *helpDropA, *helpLoadA, *helpMoreA;
 
    fileMenu = menuBar()->addMenu(tr("File"));
@@ -269,6 +277,9 @@
    loadA  = fileMenu->addAction(tr("&Open..."));
    saveA  = fileMenu->addAction(tr("Enlarge && Save"));
    pasteA = fileMenu->addAction(tr("Paste"));
+   smallA = fileMenu->addAction(tr("small mode"));
+   smallA->setCheckable( true );
+   smallA->setChecked( false );
    quitA  = fileMenu->addAction(tr("Quit"));
 
    aboutA      = helpMenu->addAction(tr("About"));
@@ -287,6 +298,7 @@
    connect( loadA,  SIGNAL(triggered()), this, SLOT( slot_load1())  );
    connect( saveA,  SIGNAL(triggered()), this, SLOT( slot_calc())   );
    connect( pasteA, SIGNAL(triggered()), this, SLOT( slot_paste())   );
+   connect( smallA, SIGNAL(toggled(bool)), this, SLOT( slot_SmallMode( bool ))   );
    connect( quitA,  SIGNAL(triggered()), this, SLOT( close())        );
 
    connect( helpStartA,  SIGNAL(triggered()), this, SLOT( HelpMessageStart()) );
@@ -355,7 +367,7 @@
 }
 
 void EnlargerDialog::DoPreview( void ) {
-    if( zoomFact < 1.0 )
+    if( ZoomX() < 1.0 && ZoomY() < 1.0 )
         return;
 
     QRect dstRect;
@@ -369,59 +381,13 @@
 
     format.srcWidth  = srcImage.width();
     format.srcHeight = srcImage.height();
-    format.SetScaleFact( zoomFact );
+    format.SetScaleFact( ZoomX(), ZoomY() );
     format.SetDstClip( dstRect.x(), dstRect.y(), dstRect.x() + dstRect.width(), dstRect.y() + dstRect.height() );
 
     ReadParameters( enlargeParam );
     previewThread->Enlarge( srcImage, format, enlargeParam.FloatParam() );
     previewThread->setPriority( QThread::NormalPriority );
 
-}
-
-void EnlargerDialog::slot_calc( void ) {
-    QRect dstRect;
-    float zoomF;
-    QImage srcImage;
-    QImage dstImage;
-
-    //IncDestName();
-    if( dstDir.exists( ui->destFileEdit->text() ) )  {
-       if( !FileExistsMessage( ui->destFileEdit->text(), dstDir.dirName() ) )
-          return;
-    }
-
-    ui->calcStopButton->setEnabled( true  );
-    ui->calcButton->setEnabled    ( false );
-    ui->calcButton->setText       ( "Calculating...");
-
-    ui->calcStopButton->setFocus();
-    calcResultName = dstDir.absoluteFilePath( ui->destFileEdit->text() );
-
-    calcSrc = ui->comboBox->currentText();
-    calcDst = ui->destFileEdit->text();
-    QString statusText;
-    statusText =  "Calculating '" + ui->destFileEdit->text() + "' . ";
-    statusText += " Source was '" + ui->comboBox->currentText() + "' . ";
-    statusText += " Destination directory is '" + ui->destDirLabel->text() + "' . ";
-    PrintStatusText( statusText );
-    SetToolTip( ui->progressBar,   statusText );
-    SetToolTip( ui->calcButton,    statusText );
-    SetToolTip( ui->calcStopButton,  " Stop calculating '" + ui->destFileEdit->text() + "' . " );
-
-    zoomF = ui->previewField->Zoom();
-    srcImage = ui->previewField->theImage();
-
-    EnlargeFormat   format;
-    EnlargeParamInt enlargeParam;
-
-    format.srcWidth  = srcImage.width();
-    format.srcHeight = srcImage.height();
-    format.SetScaleFact( zoomF );
-    format.SetSrcClip( baseClipX0, baseClipY0, baseClipX1, baseClipY1 );
-    ReadParameters( enlargeParam );
-
-    calcThread->EnlargeAndSave( srcImage, format, enlargeParam.FloatParam(), calcResultName );
-    currentDialogMode = fullEnlarge;
 }
 
 void EnlargerDialog::slot_queueCalc( void ) {
@@ -442,7 +408,7 @@
    newJob->dstPath = dstDir.absoluteFilePath( ui->destFileEdit->text() );
    newJob->format.srcWidth  = ui->previewField->theImage().width();
    newJob->format.srcHeight = ui->previewField->theImage().height();
-   newJob->format.SetScaleFact( zoomFact );
+   newJob->format.SetScaleFact( ZoomX(), ZoomY() );
    newJob->format.SetSrcClip( baseClipX0, baseClipY0, baseClipX1, baseClipY1 );
    ReadParameters( newJob->param );
 
@@ -491,23 +457,77 @@
 
 void EnlargerDialog::SetZoomInt( int v ) {
     if( zoomInt != v ) {
+        if( ui->zoomSlider->hasFocus() || ui->zoomSpinBox->hasFocus() ) {
+           ZoomUseZoom();
+        }
         SetZoom( float( v )*0.01 );
         stopPreview();
     }
 }
 
 void EnlargerDialog::SetNewWidth( int v ) {
-    if( v != newWidth ) {
-        SetZoom( float( v ) / (baseClipX1-baseClipX0) );
-        stopPreview();
-    }
+   if( setZoom_recursionBlocker )  // called from within SetZoom -> nothing to do
+      return;
+   if( v != newWidth ) {
+      if( zoomUpdateMod == useWidthAndHeight ){
+         ChangeAspectRatio( v, newHeight );
+      }
+      else {
+         if( ui->newWidthBox->hasFocus() ) {
+            ZoomUseWidth();
+         }
+         SetZoom( float( v ) / (baseClipX1-baseClipX0) );
+      }
+      stopPreview();
+   }
 }
 
 void EnlargerDialog::SetNewHeight( int v ) {
-    if( v != newHeight ) {
-        SetZoom( float( v ) / (baseClipY1-baseClipY0) );
-        stopPreview();
-    }
+   if( setZoom_recursionBlocker )  // called from within SetZoom -> nothing to do
+      return;
+   if( v != newHeight ) {
+      if( zoomUpdateMod == useWidthAndHeight ){
+         ChangeAspectRatio( newWidth, v );
+      }
+      else {
+         if( ui->newHeightBox->hasFocus() ) {
+            ZoomUseHeight();
+         }
+         SetZoom( float( v ) / (baseClipY1-baseClipY0) );
+      }
+      stopPreview();
+   }
+}
+
+void EnlargerDialog::slot_ZoomRadioButton( void ) {
+   ZoomUseZoom();
+   ui->zoomRButton->setChecked( false ); // will be toggled after releasing the button
+}
+
+void EnlargerDialog::slot_WidthRadioButton( void ) {
+   if( ui->widthRButton->isChecked() && ui->heightRButton->isChecked()) {
+      ZoomUseWidth();
+   }
+   else if( !ui->widthRButton->isChecked() && !ui->heightRButton->isChecked() ) {
+      ZoomUseWidth();
+   }
+   else if( !ui->widthRButton->isChecked() && ui->heightRButton->isChecked() ) {
+      ZoomUseWidthAndHeight();
+   }
+   ui->widthRButton->setChecked( false ); // will be toggled after releasing the button
+}
+
+void EnlargerDialog::slot_HeightRadioButton( void ) {
+   if( ui->widthRButton->isChecked() && ui->heightRButton->isChecked()) {
+      ZoomUseHeight();
+   }
+   else if( !ui->widthRButton->isChecked() && !ui->heightRButton->isChecked() ) {
+      ZoomUseHeight();
+   }
+   else if( ui->widthRButton->isChecked() && !ui->heightRButton->isChecked() ) {
+      ZoomUseWidthAndHeight();
+   }
+   ui->heightRButton->setChecked( false ); // will be toggled after releasing the button
 }
 
 void EnlargerDialog::slot_applyClipping( void ) {
@@ -565,13 +585,46 @@
 }
 
 void EnlargerDialog::slot_ShowJobCount( int endedJobs, int totalJobs ) {
-   ui->tabWidgetRight->setTabText( 0, "Jobs: ( " + QString::number( endedJobs ) + "/" + QString::number( totalJobs ) + " )" );
+   int tabIndex;
+   tabIndex = ui->tabWidgetRight->indexOf( ui->jobTab );
+   if( tabIndex != -1 ) {
+      ui->tabWidgetRight->setTabText( tabIndex, "Jobs: ( " + QString::number( endedJobs ) + "/" + QString::number( totalJobs ) + " )" );
+   }
 }
 
 void EnlargerDialog::slot_MessageToLog( const QString & message ) {
    ui->logDisplay->append( message );
    ui->logDisplay->moveCursor ( QTextCursor::End );
    ui->logDisplay->ensureCursorVisible();
+}
+
+void EnlargerDialog::slot_SmallMode( bool switchToSmall ) {
+//   hide();
+   setUpdatesEnabled( false );
+   if( switchToSmall ) {
+      ui->srcGroupBox->hide();
+      ui->previewGroupBox->hide();
+      ui->tabWidgetLeft->hide();
+      ui->tabWidgetRight->setParent( ui->centralwidget );
+      ui->tabWidgetLeft->removeTab( 0 );
+      ui->tabWidgetRight->insertTab( 0, ui->enlargeTab, "Enlarge" );
+
+      // workaround ?!?: force correct resize ?!?
+      setUpdatesEnabled( true );
+      ui->centralwidget->setParent( 0 );
+      setCentralWidget( ui->centralwidget );
+      ui->centralwidget->show();
+   }
+   else {
+      ui->tabWidgetRight->removeTab( 0 );
+      ui->tabWidgetLeft->insertTab( 0, ui->enlargeTab, "Enlarge"  );
+      ui->tabWidgetLeft->show();
+      ui->tabWidgetRight->show();
+      ui->srcGroupBox->show();
+      ui->previewGroupBox->show();
+   }
+   resize( minimumSize() );
+   setUpdatesEnabled( true );
 }
 
 void EnlargerDialog::closeEvent(QCloseEvent *event)
@@ -750,28 +803,46 @@
 }
 
 void EnlargerDialog::ZoomUseWidth ( void ) {
-   ui->newWidthBox->setFocus();
    zoomUpdateMod  = useWidth;
+   ui->zoomRButton->setChecked( false );
+   ui->widthRButton->setChecked( true );
+   ui->heightRButton->setChecked( false );
+   aspectX = aspectY = 1.0;
+   UpdateZoom();
 }
 
 void EnlargerDialog::ZoomUseHeight( void ) {
-   ui->newHeightBox->setFocus();
    zoomUpdateMod = useHeight;
+   ui->zoomRButton->setChecked( false );
+   ui->widthRButton->setChecked( false );
+   ui->heightRButton->setChecked( true );
+   aspectX = aspectY = 1.0;
+   UpdateZoom();
+}
+
+void EnlargerDialog::ZoomUseWidthAndHeight( void ) {
+   zoomUpdateMod = useWidthAndHeight;
+   ui->zoomRButton->setChecked( false );
+   ui->widthRButton->setChecked( true );
+   ui->heightRButton->setChecked( true );
 }
 
 void EnlargerDialog::ZoomUseZoom  ( void ) {
-   ui->zoomSpinBox->setFocus();
    zoomUpdateMod  = useZoom;
+   ui->zoomRButton->setChecked( true );
+   ui->widthRButton->setChecked( false );
+   ui->heightRButton->setChecked( false );
+   aspectX = aspectY = 1.0;
+   UpdateZoom();
 }
 
 void EnlargerDialog::UpdateZoom( void ) {
    float newZoom;
-   if( ui->newWidthBox->hasFocus() )
-      zoomUpdateMod = useWidth;
-   else if( ui->newHeightBox->hasFocus() )
-      zoomUpdateMod = useHeight;
-   else if( ui->zoomSlider->hasFocus() || ui->zoomSpinBox->hasFocus())
-      zoomUpdateMod = useZoom;
+
+   if( zoomUpdateMod == useWidthAndHeight ) {  //  width and height const
+      ChangeAspectRatio( ui->newWidthBox->value(), ui->newHeightBox->value() );
+      return;
+   }
 
    if( zoomUpdateMod == useWidth ) {  // newWidth spinbox has focus -> width const
       newZoom = float( ui->newWidthBox->value() ) / (baseClipX1-baseClipX0) ;
@@ -787,21 +858,13 @@
 
 void EnlargerDialog::SetZoom( float zoomF ) {
     if( !setZoom_recursionBlocker ) {
-        // by the way: check if zoomUpdateMod has changed
-        if( ui->newWidthBox->hasFocus() )
-           zoomUpdateMod = useWidth;
-        else if( ui->newHeightBox->hasFocus() )
-           zoomUpdateMod = useHeight;
-        else if( ui->zoomSlider->hasFocus() || ui->zoomSpinBox->hasFocus())
-           zoomUpdateMod = useZoom;
-
         setZoom_recursionBlocker = true;
         zoomFact = zoomF;
-        newWidth  = int( (baseClipX1-baseClipX0)*zoomFact + 0.5);
-        newHeight = int( (baseClipY1-baseClipY0)*zoomFact + 0.5);
+        newWidth  = int( (baseClipX1-baseClipX0)*ZoomX() + 0.5);
+        newHeight = int( (baseClipY1-baseClipY0)*ZoomY() + 0.5);
         zoomInt   = int(zoomF*100.0 + 0.5);
-        ui->previewField->setZoom( zoomF );
-        ui->selectField->setSelectSize( preWidth/zoomF, preHeight/zoomF );
+        ui->previewField->setZoom( ZoomX(), ZoomY() );
+        ui->selectField->setSelectSize( preWidth/ZoomX(), preHeight/ZoomY() );
         ui->newWidthBox->setValue(newWidth);
         ui->newHeightBox->setValue(newHeight);
         ui->zoomSpinBox->setValue(zoomInt);
@@ -810,17 +873,18 @@
     }
 }
 
+void EnlargerDialog::ChangeAspectRatio( int dstWidth, int dstHeight ) {
+   float zx, zy , zoomF;
+   zx = float( dstWidth ) / (baseClipX1-baseClipX0);  // zoom in X
+   zy = float( dstHeight) / (baseClipY1-baseClipY0);  // zoom in Y
+   zoomF = sqrt( zx*zy );   // zoomFact
+   aspectX = zx/zoomF;
+   aspectY = zy/zoomF;
+   SetZoom( zoomF );
+}
+
 void EnlargerDialog::ResetDialog( void ) {
-    currentDialogMode = noEnlarge;
-    ui->calcStopButton->setEnabled( false );
-    ui->calcButton->setEnabled    ( true  );
-    ui->calcButton->setText       ( "Enlarge && Save" );
-    ui->previewButton->setEnabled ( true  );
     ui->previewButton->setFocus();
-
-    SetToolTip( ui->progressBar,     QString(""                                ) );
-    SetToolTip( ui->calcButton,      QString(" calculate the enlarged image "  ) );
-    SetToolTip( ui->calcStopButton,  QString(" stop current calculation "      ) );
 }
 
 bool EnlargerDialog::TryOpenSource( QString fileName ) {
@@ -934,7 +998,7 @@
     baseClipX1 = float( srcWidth ); baseClipY1 = float( srcHeight );
     ClippingHistoryClear();
 
-    UpdateZoom();
+//    UpdateZoom();
 
     ui->selectField->setTheImage( src );
     ui->selectField->setClipRect( baseClipX0, baseClipY0, baseClipX1, baseClipY1 );
@@ -965,8 +1029,8 @@
     SetZoom( newZoom );
 */
 
-    float nX =  baseClipX0 + 0.5*( baseClipX1 - baseClipX0 ) - 0.5*preWidth/zoomFact;
-    float nY =  baseClipY0 + 0.5*( baseClipY1 - baseClipY0 ) - 0.5*preHeight/zoomFact;
+    float nX =  baseClipX0 + 0.5*( baseClipX1 - baseClipX0 ) - 0.5*preWidth/ZoomX();
+    float nY =  baseClipY0 + 0.5*( baseClipY1 - baseClipY0 ) - 0.5*preHeight/ZoomY();
     ui->selectField->moveSelection( nX, nY );
 }