I wrote:
In my opinion the best solution would be to calculate the aspectration
from the image bounding box and calculating the width and height
accordingly. We already have the infrastructure for this. I'll try to
implement this until tomorrow.
Attached is the patch, now the width and height are calculated according to the
aspect ratio.
Opinions?
My solution works when you open the graphics dialog of images that are already in your LyX document.
When you want to insert a new image it doesn't work as the bounding box is not read out but this
should be easy to implement - I just don't have the time yet to investigate how this could be done:
After the filename has been specified, the bounding box is read out automatically - the same is done
when you press the OK button but we need this to be done before pressing OK.
Could anybody implement this or help me here? Richard?
regards Uwe
Index: lib/doc/EmbeddedObjects.lyx
===================================================================
--- lib/doc/EmbeddedObjects.lyx (revision 17689)
+++ lib/doc/EmbeddedObjects.lyx (working copy)
@@ -405,6 +405,15 @@
button.
The program can be set for every image format in the file format settings
in LyX's preferences.
+\newline
+When you use the option
+\family sans
+Maintain aspect ratio
+\family default
+ the width and heigth is calculated corresponding to the aspect ratio of
+ the image.
+ Note that this calculation only works after you have already inserted the
+ image!
\end_layout
\begin_layout Description
Index: src/frontends/qt4/QGraphics.C
===================================================================
--- src/frontends/qt4/QGraphics.C (revision 17689)
+++ src/frontends/qt4/QGraphics.C (working copy)
@@ -79,7 +79,6 @@
bcview().addReadOnly(dialog_->filename);
bcview().addReadOnly(dialog_->browsePB);
bcview().addReadOnly(dialog_->unzipCB);
- bcview().addReadOnly(dialog_->filename);
bcview().addReadOnly(dialog_->bbFrame);
bcview().addReadOnly(dialog_->draftCB);
bcview().addReadOnly(dialog_->clip);
@@ -91,8 +90,8 @@
bcview().addReadOnly(dialog_->getPB);
// initialize the length validator
- addCheckedLineEdit(bcview(), dialog_->Width, dialog_->widthL);
- addCheckedLineEdit(bcview(), dialog_->Height, dialog_->heightL);
+ addCheckedLineEdit(bcview(), dialog_->widthED, dialog_->widthL);
+ addCheckedLineEdit(bcview(), dialog_->heightED, dialog_->heightL);
addCheckedLineEdit(bcview(), dialog_->displayscale, dialog_->scaleLA);
addCheckedLineEdit(bcview(), dialog_->angle, dialog_->angleL);
addCheckedLineEdit(bcview(), dialog_->lbX, dialog_->xL);
@@ -228,13 +227,13 @@
// the output section (width/height)
dialog_->Scale->setText(toqstr(igp.scale));
- lengthToWidgets(dialog_->Width, dialog_->widthUnit,
+ lengthToWidgets(dialog_->widthED, dialog_->widthUnit,
igp.width.asString(), unitDefault);
- lengthToWidgets(dialog_->Height, dialog_->heightUnit,
+ lengthToWidgets(dialog_->heightED, dialog_->heightUnit,
igp.height.asString(), unitDefault);
- dialog_->aspectratio->setChecked(igp.keepAspectRatio);
+ dialog_->aspectratioCB->setChecked(igp.keepAspectRatio);
dialog_->scaleCB->setChecked(!igp.scale.empty() || igp.width.empty());
@@ -328,11 +327,11 @@
igp.scale = string();
}
- igp.width = LyXLength(widgetsToLength(dialog_->Width, dialog_->widthUnit));
+ igp.width = LyXLength(widgetsToLength(dialog_->widthED, dialog_->widthUnit));
- igp.height = LyXLength(widgetsToLength(dialog_->Height, dialog_->heightUnit));
+ igp.height = LyXLength(widgetsToLength(dialog_->heightED, dialog_->heightUnit));
- igp.keepAspectRatio = dialog_->aspectratio->isChecked();
+ igp.keepAspectRatio = dialog_->aspectratioCB->isChecked();
igp.noUnzip = dialog_->unzipCB->isChecked();
Index: src/frontends/qt4/QGraphicsDialog.C
===================================================================
--- src/frontends/qt4/QGraphicsDialog.C (revision 17689)
+++ src/frontends/qt4/QGraphicsDialog.C (working copy)
@@ -69,15 +69,15 @@
this, SLOT( change_adaptor() ) );
connect(displayscale, SIGNAL( textChanged(const QString&) ),
this, SLOT( change_adaptor() ) );
- connect(Width, SIGNAL( textChanged(const QString&) ),
+ connect(widthED, SIGNAL( textChanged(const QString&) ),
this, SLOT( change_adaptor() ) );
- connect(aspectratio, SIGNAL( stateChanged(int) ),
+ connect(aspectratioCB, SIGNAL( stateChanged(int) ),
this, SLOT( change_adaptor() ) );
connect(draftCB, SIGNAL( stateChanged(int) ),
this, SLOT( change_adaptor() ) );
connect(unzipCB, SIGNAL( stateChanged(int) ),
this, SLOT( change_adaptor() ) );
- connect(Height, SIGNAL( textChanged(const QString&) ),
+ connect(heightED, SIGNAL( textChanged(const QString&) ),
this, SLOT( change_adaptor() ) );
connect(heightUnit, SIGNAL( selectionChanged(lyx::LyXLength::UNIT) ),
this, SLOT( change_adaptor() ) );
@@ -120,9 +120,12 @@
rtY->setValidator(new QDoubleValidator(rtY));
displayscale->setValidator(new QIntValidator(displayscale));
- Height->setValidator(unsignedLengthValidator(Height));
- Width->setValidator(unsignedLengthValidator(Width));
+ //FIXME We should have a validator for Scale, too.
+ //It would have to inherit from QDoubleValidator
+ heightED->setValidator(unsignedLengthValidator(heightED));
+ widthED->setValidator(unsignedLengthValidator(widthED));
+
filename->setValidator(new PathValidator(true, filename));
setFocusProxy(filename);
}
@@ -182,18 +185,84 @@
editPB->setDisabled(filename.isEmpty());
}
+void QGraphicsDialog::on_heightED_textChanged(const QString &)
+{
+ //when aspectratioCB is checked, the aspect ratio of the image is calculated from
+ //the bounding box borders rtX and rtY
+ //the width is then set as aspect ratio * height (when widthED was changed by the user)
+ if (aspectratioCB->isChecked()) {
+ // assure that width and height have the same unit
+ if (widthUnit->currentIndex() != heightUnit->currentIndex())
+ widthUnit->setCurrentIndex(heightUnit->currentIndex());
+ double AspectValue = (rtX->text().toDouble()) / (rtY->text().toDouble());
+ double Hvalue = heightED->text().toDouble();
+ double Wvalue = AspectValue * Hvalue;
+ QString Wstring;
+ Wstring.sprintf("%.2f", Wvalue);
+ if (heightED->isModified())
+ widthED->setText(Wstring);
+ }
+}
+void QGraphicsDialog::on_widthED_textChanged(const QString &)
+{
+ //when aspectratioCB is checked, the aspect ratio of the image is calculated from
+ //the bounding box borders rtX and rtY
+ //the height is then set as width / aspect ratio (when widthED was changed by the user)
+ if (aspectratioCB->isChecked()) {
+ // assure that width and height have the same unit
+ if (widthUnit->currentIndex() != heightUnit->currentIndex())
+ heightUnit->setCurrentIndex(widthUnit->currentIndex());
+ double AspectValue = (rtX->text().toDouble()) / (rtY->text().toDouble());
+ double Wvalue = widthED->text().toDouble();
+ double Hvalue = Wvalue / AspectValue;
+ QString Hstring;
+ Hstring.sprintf("%.2f", Hvalue);
+ if (widthED->isModified())
+ heightED->setText(Hstring);
+ }
+}
+
+void QGraphicsDialog::on_aspectratioCB_toggled()
+{
+ //when aspectratioCB is checked, the aspect ratio of the image is calculated from
+ //the bounding box borders rtX and rtY
+ //when the width is > 0 the height is then set as width / aspect ratio
+ //otherwise the width is set as aspect ratio * height
+ if (aspectratioCB->isChecked()) {
+ double AspectValue = (rtX->text().toDouble()) / (rtY->text().toDouble());
+ double Hvalue = heightED->text().toDouble();
+ double Wvalue = widthED->text().toDouble();
+ if (Wvalue != 0) {
+ double Hnewvalue = Wvalue / AspectValue;
+ QString Hstring;
+ Hstring.sprintf("%.2f", Hnewvalue);
+ heightED->setText(Hstring);
+ }
+ if (Hvalue != 0 && Wvalue == 0) {
+ double Wnewvalue = AspectValue * Hvalue;;
+ QString Wstring;
+ Wstring.sprintf("%.2f", Wnewvalue);
+ widthED->setText(Wstring);
+ }
+ // assure that width and height have the same unit
+ if (widthUnit->currentIndex() != heightUnit->currentIndex())
+ heightUnit->setCurrentIndex(widthUnit->currentIndex());
+ }
+}
+
void QGraphicsDialog::on_scaleCB_toggled(bool setscale)
{
+ if (scaleCB->isChecked() && Scale->text() == "")
+ Scale->setText("100");
Scale->setEnabled(setscale);
widthL->setDisabled(setscale);
- Width->setDisabled(setscale);
+ widthED->setDisabled(setscale);
widthUnit->setDisabled(setscale);
- aspectratio->setDisabled(setscale);
- bool noheight = setscale || aspectratio->checkState()==Qt::Checked;
- heightL->setDisabled(noheight);
- Height->setDisabled(noheight);
- heightUnit->setDisabled(noheight);
+ aspectratioCB->setDisabled(setscale);
+ heightL->setDisabled(setscale);
+ heightED->setDisabled(setscale);
+ heightUnit->setDisabled(setscale);
}
} // namespace frontend
Index: src/frontends/qt4/QGraphicsDialog.h
===================================================================
--- src/frontends/qt4/QGraphicsDialog.h (revision 17689)
+++ src/frontends/qt4/QGraphicsDialog.h (working copy)
@@ -37,6 +37,9 @@
virtual void on_getPB_clicked();
virtual void on_editPB_clicked();
virtual void on_filename_textChanged(const QString &);
+ virtual void on_heightED_textChanged(const QString &);
+ virtual void on_widthED_textChanged(const QString &);
+ virtual void on_aspectratioCB_toggled();
virtual void on_scaleCB_toggled(bool);
protected:
virtual void closeEvent(QCloseEvent * e);
Index: src/frontends/qt4/ui/QGraphicsUi.ui
===================================================================
--- src/frontends/qt4/ui/QGraphicsUi.ui (revision 17689)
+++ src/frontends/qt4/ui/QGraphicsUi.ui (working copy)
@@ -232,19 +232,6 @@
</item>
</layout>
</widget>
- <widget class="QLineEdit" name="filename" >
- <property name="geometry" >
- <rect>
- <x>35</x>
- <y>12</y>
- <width>235</width>
- <height>18</height>
- </rect>
- </property>
- <property name="toolTip" >
- <string>File name of image</string>
- </property>
- </widget>
<widget class="QGroupBox" name="sizeGB" >
<property name="geometry" >
<rect>
@@ -274,7 +261,7 @@
<widget class="LengthCombo" name="heightUnit" />
</item>
<item row="1" column="1" >
- <widget class="QLineEdit" name="Width" >
+ <widget class="QLineEdit" name="widthED" >
<property name="enabled" >
<bool>true</bool>
</property>
@@ -308,7 +295,7 @@
<widget class="QLineEdit" name="Scale" />
</item>
<item row="2" column="1" >
- <widget class="QLineEdit" name="Height" >
+ <widget class="QLineEdit" name="heightED" >
<property name="enabled" >
<bool>true</bool>
</property>
@@ -334,7 +321,7 @@
<string>&Height:</string>
</property>
<property name="buddy" >
- <cstring>Height</cstring>
+ <cstring>heightED</cstring>
</property>
</widget>
</item>
@@ -354,12 +341,12 @@
<string>&Width:</string>
</property>
<property name="buddy" >
- <cstring>Width</cstring>
+ <cstring>widthED</cstring>
</property>
</widget>
</item>
<item row="3" column="1" colspan="2" >
- <widget class="QCheckBox" name="aspectratio" >
+ <widget class="QCheckBox" name="aspectratioCB" >
<property name="enabled" >
<bool>true</bool>
</property>
@@ -408,6 +395,19 @@
<bool>false</bool>
</property>
</widget>
+ <widget class="QLineEdit" name="filename" >
+ <property name="geometry" >
+ <rect>
+ <x>35</x>
+ <y>12</y>
+ <width>235</width>
+ <height>18</height>
+ </rect>
+ </property>
+ <property name="toolTip" >
+ <string>File name of image</string>
+ </property>
+ </widget>
</widget>
<widget class="QWidget" name="Clipping" >
<attribute name="title" >
@@ -800,11 +800,11 @@
<tabstop>editPB</tabstop>
<tabstop>scaleCB</tabstop>
<tabstop>Scale</tabstop>
- <tabstop>Width</tabstop>
+ <tabstop>widthED</tabstop>
<tabstop>widthUnit</tabstop>
- <tabstop>Height</tabstop>
+ <tabstop>heightED</tabstop>
<tabstop>heightUnit</tabstop>
- <tabstop>aspectratio</tabstop>
+ <tabstop>aspectratioCB</tabstop>
<tabstop>angle</tabstop>
<tabstop>origin</tabstop>
<tabstop>restorePB</tabstop>
@@ -834,54 +834,5 @@
<include location="local" >qt_helpers.h</include>
</includes>
<resources/>
- <connections>
- <connection>
- <sender>aspectratio</sender>
- <signal>toggled(bool)</signal>
- <receiver>heightL</receiver>
- <slot>setDisabled(bool)</slot>
- <hints>
- <hint type="sourcelabel" >
- <x>201</x>
- <y>193</y>
- </hint>
- <hint type="destinationlabel" >
- <x>81</x>
- <y>158</y>
- </hint>
- </hints>
- </connection>
- <connection>
- <sender>aspectratio</sender>
- <signal>toggled(bool)</signal>
- <receiver>Height</receiver>
- <slot>setDisabled(bool)</slot>
- <hints>
- <hint type="sourcelabel" >
- <x>315</x>
- <y>193</y>
- </hint>
- <hint type="destinationlabel" >
- <x>275</x>
- <y>169</y>
- </hint>
- </hints>
- </connection>
- <connection>
- <sender>aspectratio</sender>
- <signal>toggled(bool)</signal>
- <receiver>heightUnit</receiver>
- <slot>setDisabled(bool)</slot>
- <hints>
- <hint type="sourcelabel" >
- <x>337</x>
- <y>193</y>
- </hint>
- <hint type="destinationlabel" >
- <x>337</x>
- <y>167</y>
- </hint>
- </hints>
- </connection>
- </connections>
+ <connections/>
</ui>