* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
+ * \author Angus Leeming
* \author John Levon
* \author Edwin Leuven
* \author Herbert Voß
#include "GuiGraphics.h"
-#include "ControlGraphics.h"
-#include "debug.h"
#include "LengthCombo.h"
-#include "lengthcommon.h"
+#include "Length.h"
#include "LyXRC.h"
#include "qt_helpers.h"
#include "Validator.h"
+#include "FuncRequest.h"
+
+#include "graphics/GraphicsCache.h"
+#include "graphics/GraphicsCacheItem.h"
+#include "graphics/GraphicsImage.h"
+
#include "insets/InsetGraphicsParams.h"
#include "support/convert.h"
+#include "support/debug.h"
+#include "support/filetools.h"
+#include "support/gettext.h"
#include "support/lstrings.h"
-#include "support/lyxlib.h"
#include "support/os.h"
-
-#include <boost/bind.hpp>
+#include "support/Package.h"
+#include "support/types.h"
#include <QCheckBox>
-#include <QCloseEvent>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QValidator>
-#include <algorithm>
#include <cmath>
#include <utility>
-using lyx::support::float_equal;
-using lyx::support::token;
+using namespace std;
+using namespace lyx::support;
-using lyx::support::os::internal_path;
+namespace {
-#ifndef CXX_GLOBAL_CSTD
-using std::floor;
-#endif
+// These are the strings that are stored in the LyX file and which
+// correspond to the LaTeX identifiers shown in the comments at the
+// end of each line.
+char const * const rorigin_lyx_strs[] = {
+ // the LaTeX default is leftBaseline
+ "",
+ "leftTop", "leftBottom", "leftBaseline", // lt lb lB
+ "center", "centerTop", "centerBottom", "centerBaseline", // c ct cb cB
+ "rightTop", "rightBottom", "rightBaseline" }; // rt rb rB
-using std::vector;
-using std::string;
+// These are the strings, corresponding to the above, that the GUI should
+// use. Note that they can/should be translated.
+char const * const rorigin_gui_strs[] = {
+ N_("Default"),
+ N_("Top left"), N_("Bottom left"), N_("Baseline left"),
+ N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
+ N_("Top right"), N_("Bottom right"), N_("Baseline right") };
+
+size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
+
+} // namespace anon
namespace lyx {
namespace frontend {
-
//FIXME setAutoTextCB should really take an argument, as indicated, that
//determines what text is to be written for "auto". But making
//that work involves more extensive revisions than we now want
* checkbox is unchecked and clearing the line edit if it previously
* said "text".
*/
-void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
+static void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
LengthCombo * lengthCombo/*, string text = "auto"*/)
{
if (!checkBox->isChecked())
}
-
template<class Pair>
-std::vector<typename Pair::first_type> const
-getFirst(std::vector<Pair> const & pr)
+vector<typename Pair::first_type> getFirst(vector<Pair> const & pr)
{
- std::vector<typename Pair::first_type> tmp(pr.size());
- std::transform(pr.begin(), pr.end(), tmp.begin(),
- boost::bind(&Pair::first, _1));
+ size_t const n = pr.size();
+ vector<typename Pair::first_type> tmp(n);
+ for (size_t i = 0; i != n; ++i)
+ tmp[i] = pr[i].first;
return tmp;
}
+
///
template<class Pair>
-std::vector<typename Pair::second_type> const
-getSecond(std::vector<Pair> const & pr)
+vector<typename Pair::second_type> getSecond(vector<Pair> const & pr)
{
- std::vector<typename Pair::second_type> tmp(pr.size());
- std::transform(pr.begin(), pr.end(), tmp.begin(),
- boost::bind(&Pair::second, _1));
- return tmp;
+ size_t const n = pr.size();
+ vector<typename Pair::second_type> tmp(n);
+ for (size_t i = 0; i != n; ++i)
+ tmp[i] = pr[i].second;
+ return tmp;
}
-GuiGraphicsDialog::GuiGraphicsDialog(LyXView & lv)
- : GuiDialog(lv, "graphics")
+
+/// The (tranlated) GUI string and it's LaTeX equivalent.
+typedef pair<docstring, string> RotationOriginPair;
+///
+vector<RotationOriginPair> getRotationOriginData()
{
- setupUi(this);
- setViewTitle(_("Graphics"));
- setController(new ControlGraphics(*this));
+ static vector<RotationOriginPair> data;
+ if (!data.empty())
+ return data;
+
+ data.resize(rorigin_size);
+ for (size_type i = 0; i < rorigin_size; ++i) {
+ data[i] = make_pair(_(rorigin_gui_strs[i]),
+ rorigin_lyx_strs[i]);
+ }
+
+ return data;
+}
+
+
+GuiGraphics::GuiGraphics(GuiView & lv)
+ : GuiDialog(lv, "graphics", qt_("Graphics"))
+{
+ setupUi(this);
+
//main buttons
connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
// setChecked(). Note, too, that clicked() would get called whenever it
// is clicked, even right clicked (I think), not just whenever it is
// toggled.
- connect(subfigure, SIGNAL(toggled(bool)),
- this, SLOT(change_adaptor()));
- connect(subcaption, SIGNAL(textChanged(const QString&)),
- this, SLOT(change_adaptor()));
connect(displayGB, SIGNAL(toggled(bool)),
this, SLOT(change_adaptor()));
connect(showCB, SIGNAL(currentIndexChanged(int)),
bc().setCancel(closePB);
bc().addReadOnly(latexoptions);
- bc().addReadOnly(subfigure);
bc().addReadOnly(filenameL);
bc().addReadOnly(filename);
bc().addReadOnly(browsePB);
}
-ControlGraphics & GuiGraphicsDialog::controller()
-{
- return static_cast<ControlGraphics &>(GuiDialog::controller());
-}
-
-
-void GuiGraphicsDialog::change_adaptor()
+void GuiGraphics::change_adaptor()
{
changed();
}
-void GuiGraphicsDialog::change_bb()
+void GuiGraphics::change_bb()
{
- controller().bbChanged = true;
- LYXERR(Debug::GRAPHICS)
- << "[controller().bb_Changed set to true]\n";
+ bbChanged = true;
+ LYXERR(Debug::GRAPHICS, "[bb_Changed set to true]");
changed();
}
-void GuiGraphicsDialog::closeEvent(QCloseEvent * e)
-{
- slotClose();
- e->accept();
-}
-
-
-void GuiGraphicsDialog::on_browsePB_clicked()
+void GuiGraphics::on_browsePB_clicked()
{
- docstring const str =
- controller().browse(qstring_to_ucs4(filename->text()));
- if(!str.empty()){
- filename->setText(toqstr(str));
- embedCB->setCheckState(Qt::Unchecked);
+ QString const str = browse(filename->text());
+ if (!str.isEmpty()) {
+ filename->setText(str);
changed();
}
}
-void GuiGraphicsDialog::on_getPB_clicked()
+void GuiGraphics::on_getPB_clicked()
{
getBB();
}
-void GuiGraphicsDialog::on_editPB_clicked()
-{
- controller().editGraphics();
-}
-
-
-void GuiGraphicsDialog::on_filename_textChanged(const QString & filename)
-{
- editPB->setDisabled(filename.isEmpty());
-}
-
-
-void GuiGraphicsDialog::setAutoText()
+void GuiGraphics::setAutoText()
{
if (scaleCB->isChecked())
return;
}
-void GuiGraphicsDialog::on_scaleCB_toggled(bool setScale)
+void GuiGraphics::on_scaleCB_toggled(bool setScale)
{
Scale->setEnabled(setScale);
if (setScale) {
}
-void GuiGraphicsDialog::on_WidthCB_toggled(bool setWidth)
+void GuiGraphics::on_WidthCB_toggled(bool setWidth)
{
Width->setEnabled(setWidth);
widthUnit->setEnabled(setWidth);
}
-void GuiGraphicsDialog::on_HeightCB_toggled(bool setHeight)
+void GuiGraphics::on_HeightCB_toggled(bool setHeight)
{
Height->setEnabled(setHeight);
heightUnit->setEnabled(setHeight);
}
-void GuiGraphicsDialog::on_angle_textChanged(const QString & filename)
+void GuiGraphics::on_angle_textChanged(const QString & filename)
{
rotateOrderCB->setEnabled((WidthCB->isChecked() ||
HeightCB->isChecked() ||
}
// returns the number of the string s in the vector v
-static int getItemNo(const vector<string> & v, string const & s)
+static int itemNumber(const vector<string> & v, string const & s)
{
vector<string>::const_iterator cit =
find(v.begin(), v.end(), s);
}
-void GuiGraphicsDialog::updateContents()
+void GuiGraphics::updateContents()
{
- // clear and fill in the comboboxes
- vector<string> const bb_units = frontend::getBBUnits();
+ static char const * const bb_units[] = { "bp", "cm", "mm", "in" };
+ size_t const bb_size = sizeof(bb_units) / sizeof(bb_units[0]);
+
+ vector<string> const units = vector<string>(bb_units, bb_units + bb_size);
lbXunit->clear();
lbYunit->clear();
rtXunit->clear();
rtYunit->clear();
- for (vector<string>::const_iterator it = bb_units.begin();
- it != bb_units.end(); ++it) {
+ for (vector<string>::const_iterator it = units.begin();
+ it != units.end(); ++it) {
lbXunit->addItem(toqstr(*it));
lbYunit->addItem(toqstr(*it));
rtXunit->addItem(toqstr(*it));
rtYunit->addItem(toqstr(*it));
}
- InsetGraphicsParams & igp = controller().params();
+ InsetGraphicsParams & igp = params_;
// set the right default unit
Length::UNIT unitDefault = Length::CM;
break;
}
+ lyxerr << bufferFilepath();
string const name =
- igp.filename.outputFilename(controller().bufferFilepath());
+ igp.filename.outputFilename(fromqstr(bufferFilepath()));
filename->setText(toqstr(name));
- embedCB->setCheckState(igp.filename.embedded() ? Qt::Checked : Qt::Unchecked);
// set the bounding box values
if (igp.bb.empty()) {
- string const bb = controller().readBB(igp.filename.absFilename());
+ string const bb = readBoundingBox(igp.filename.absFilename());
// the values from the file always have the bigpoint-unit bp
lbX->setText(toqstr(token(bb, ' ', 0)));
lbY->setText(toqstr(token(bb, ' ', 1)));
lbYunit->setCurrentIndex(0);
rtXunit->setCurrentIndex(0);
rtYunit->setCurrentIndex(0);
- controller().bbChanged = false;
+ bbChanged = false;
} else {
// get the values from the inset
Length anyLength;
- string const xl(token(igp.bb, ' ', 0));
- string const yl(token(igp.bb, ' ', 1));
- string const xr(token(igp.bb, ' ', 2));
- string const yr(token(igp.bb, ' ', 3));
+ string const xl = token(igp.bb, ' ', 0);
+ string const yl = token(igp.bb, ' ', 1);
+ string const xr = token(igp.bb, ' ', 2);
+ string const yr = token(igp.bb, ' ', 3);
if (isValidLength(xl, &anyLength)) {
lbX->setText(toqstr(convert<string>(anyLength.value())));
- string const unit(unit_name[anyLength.unit()]);
- lbXunit->setCurrentIndex(getItemNo(bb_units, unit));
+ string const unit = unit_name[anyLength.unit()];
+ lbXunit->setCurrentIndex(itemNumber(units, unit));
} else {
lbX->setText(toqstr(xl));
}
if (isValidLength(yl, &anyLength)) {
lbY->setText(toqstr(convert<string>(anyLength.value())));
- string const unit(unit_name[anyLength.unit()]);
- lbYunit->setCurrentIndex(getItemNo(bb_units, unit));
+ string const unit = unit_name[anyLength.unit()];
+ lbYunit->setCurrentIndex(itemNumber(units, unit));
} else {
lbY->setText(toqstr(xl));
}
if (isValidLength(xr, &anyLength)) {
rtX->setText(toqstr(convert<string>(anyLength.value())));
- string const unit(unit_name[anyLength.unit()]);
- rtXunit->setCurrentIndex(getItemNo(bb_units, unit));
+ string const unit = unit_name[anyLength.unit()];
+ rtXunit->setCurrentIndex(itemNumber(units, unit));
} else {
rtX->setText(toqstr(xl));
}
if (isValidLength(yr, &anyLength)) {
rtY->setText(toqstr(convert<string>(anyLength.value())));
- string const unit(unit_name[anyLength.unit()]);
- rtYunit->setCurrentIndex(getItemNo(bb_units, unit));
+ string const unit = unit_name[anyLength.unit()];
+ rtYunit->setCurrentIndex(itemNumber(units, unit));
} else {
rtY->setText(toqstr(xl));
}
- controller().bbChanged = true;
+ bbChanged = true;
}
// Update the draft and clip mode
clip->setChecked(igp.clip);
unzipCB->setChecked(igp.noUnzip);
- // Update the subcaption check button and input field
- subfigure->setChecked(igp.subcaption);
- subcaption->setText(toqstr(igp.subcaptionText));
-
int item = 0;
switch (igp.display) {
case graphics::DefaultDisplay: item = 0; break;
origin->addItem(toqstr(*it));
if (!igp.rotateOrigin.empty())
- origin->setCurrentIndex(
- getItemNo(origin_ltx, igp.rotateOrigin));
+ origin->setCurrentIndex(itemNumber(origin_ltx, igp.rotateOrigin));
else
origin->setCurrentIndex(0);
- // disable edit button when no filename is present
- editPB->setDisabled(filename->text().isEmpty());
-
- //// latex section
+ // latex section
latexoptions->setText(toqstr(igp.special));
}
-void GuiGraphicsDialog::applyView()
+void GuiGraphics::applyView()
{
- InsetGraphicsParams & igp = controller().params();
+ InsetGraphicsParams & igp = params_;
- igp.filename.set(internal_path(fromqstr(filename->text())),
- controller().bufferFilepath());
- igp.filename.setEmbed(embedCB->checkState() == Qt::Checked);
+ igp.filename.set(fromqstr(filename->text()), fromqstr(bufferFilepath()));
// the bb section
igp.bb.erase();
- if (controller().bbChanged) {
+ if (bbChanged) {
string bb;
string lbXs = fromqstr(lbX->text());
string lbYs = fromqstr(lbY->text());
igp.draft = draftCB->isChecked();
igp.clip = clip->isChecked();
- igp.subcaption = subfigure->isChecked();
- igp.subcaptionText = fromqstr(subcaption->text());
switch (showCB->currentIndex()) {
case 0: igp.display = graphics::DefaultDisplay; break;
igp.rotateAngle = fromqstr(angle->text());
double rotAngle = convert<double>(igp.rotateAngle);
- if (std::abs(rotAngle) > 360.0) {
+ if (abs(rotAngle) > 360.0) {
rotAngle -= 360.0 * floor(rotAngle / 360.0);
igp.rotateAngle = convert<string>(rotAngle);
}
}
-void GuiGraphicsDialog::getBB()
+void GuiGraphics::getBB()
{
string const fn = fromqstr(filename->text());
- if (!fn.empty()) {
- string const bb = controller().readBB(fn);
- if (!bb.empty()) {
- lbX->setText(toqstr(token(bb, ' ', 0)));
- lbY->setText(toqstr(token(bb, ' ', 1)));
- rtX->setText(toqstr(token(bb, ' ', 2)));
- rtY->setText(toqstr(token(bb, ' ', 3)));
- // the default units for the bb values when reading
- // it from the file
- lbXunit->setCurrentIndex(0);
- lbYunit->setCurrentIndex(0);
- rtXunit->setCurrentIndex(0);
- rtYunit->setCurrentIndex(0);
+ if (fn.empty())
+ return;
+ string const bb = readBoundingBox(fn);
+ bbChanged = false;
+ if (bb.empty())
+ return;
+ lbX->setText(toqstr(token(bb, ' ', 0)));
+ lbY->setText(toqstr(token(bb, ' ', 1)));
+ rtX->setText(toqstr(token(bb, ' ', 2)));
+ rtY->setText(toqstr(token(bb, ' ', 3)));
+ // the default units for the bb values when reading
+ // it from the file
+ lbXunit->setCurrentIndex(0);
+ lbYunit->setCurrentIndex(0);
+ rtXunit->setCurrentIndex(0);
+ rtYunit->setCurrentIndex(0);
+}
+
+
+bool GuiGraphics::isValid()
+{
+ return !filename->text().isEmpty();
+}
+
+
+bool GuiGraphics::initialiseParams(string const & data)
+{
+ InsetGraphics::string2params(data, buffer(), params_);
+ return true;
+}
+
+
+void GuiGraphics::clearParams()
+{
+ params_ = InsetGraphicsParams();
+}
+
+
+void GuiGraphics::dispatchParams()
+{
+ InsetGraphicsParams tmp_params(params_);
+ string const lfun = InsetGraphics::params2string(tmp_params, buffer());
+ dispatch(FuncRequest(getLfun(), lfun));
+}
+
+
+QString GuiGraphics::browse(QString const & in_name) const
+{
+ QString const title = qt_("Select graphics file");
+
+ // Does user clipart directory exist?
+ string clipdir = addName(package().user_support().absFilename(), "clipart");
+ FileName clip(clipdir);
+
+ // bail out to system clipart directory
+ if (!clip.isDirectory())
+ clipdir = addName(package().system_support().absFilename(), "clipart");
+
+ return browseRelFile(in_name, bufferFilepath(),
+ title, fileFilters(QString()), false,
+ qt_("Clipart|#C#c"), toqstr(clipdir),
+ qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
+}
+
+
+string GuiGraphics::readBoundingBox(string const & file)
+{
+ FileName const abs_file = support::makeAbsPath(file, fromqstr(bufferFilepath()));
+
+ // try to get it from the file, if possible. Zipped files are
+ // unzipped in the readBB_from_PSFile-Function
+ string const bb = readBB_from_PSFile(abs_file);
+ if (!bb.empty())
+ return bb;
+
+ // we don't, so ask the Graphics Cache if it has loaded the file
+ int width = 0;
+ int height = 0;
+
+ graphics::Cache & gc = graphics::Cache::get();
+ if (gc.inCache(abs_file)) {
+ graphics::Image const * image = gc.item(abs_file)->image();
+
+ if (image) {
+ width = image->width();
+ height = image->height();
}
- controller().bbChanged = false;
}
+
+ return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
}
-bool GuiGraphicsDialog::isValid()
+bool GuiGraphics::isFileNameValid(string const & fname) const
{
- return !filename->text().isEmpty();
+ // It may be that the filename is relative.
+ return support::makeAbsPath(fname, fromqstr(bufferFilepath())).isReadableFile();
}
+
+Dialog * createGuiGraphics(GuiView & lv) { return new GuiGraphics(lv); }
+
+
} // namespace frontend
} // namespace lyx
-
#include "GuiGraphics_moc.cpp"
-