* 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 "support/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 "support/gettext.h"
+
+#include "graphics/GraphicsCache.h"
+#include "graphics/GraphicsCacheItem.h"
+#include "graphics/GraphicsImage.h"
+
#include "insets/InsetGraphicsParams.h"
#include "support/convert.h"
+#include "support/FileFilterList.h"
+#include "support/filetools.h"
#include "support/lstrings.h"
-#include "support/lyxlib.h"
#include "support/os.h"
+#include "support/Package.h"
+#include "support/types.h"
#include <boost/bind.hpp>
#include <QCheckBox>
-#include <QCloseEvent>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <cmath>
#include <utility>
-using lyx::support::float_equal;
-using lyx::support::token;
-
-using lyx::support::os::internal_path;
-
-#ifndef CXX_GLOBAL_CSTD
-using std::floor;
-#endif
-
-using std::vector;
-using std::string;
-
+using namespace std;
+using namespace lyx::support;
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())
lengthCombo->currentLengthItem());
}
-
-
template<class Pair>
-std::vector<typename Pair::first_type> const
-getFirst(std::vector<Pair> const & pr)
+vector<typename Pair::first_type> const
+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));
+ vector<typename Pair::first_type> tmp(pr.size());
+ transform(pr.begin(), pr.end(), tmp.begin(),
+ boost::bind(&Pair::first, _1));
return tmp;
}
///
template<class Pair>
-std::vector<typename Pair::second_type> const
-getSecond(std::vector<Pair> const & pr)
+vector<typename Pair::second_type> const
+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));
+ vector<typename Pair::second_type> tmp(pr.size());
+ transform(pr.begin(), pr.end(), tmp.begin(),
+ boost::bind(&Pair::second, _1));
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();
+
+
+GuiGraphics::GuiGraphics(GuiView & lv)
+ : GuiDialog(lv, "graphics", qt_("Graphics"))
{
setupUi(this);
- setViewTitle(_("Graphics"));
- setController(new ControlGraphics(*this));
-
+
//main buttons
connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
//graphics pane
connect(filename, SIGNAL(textChanged(const QString &)),
this, SLOT(change_adaptor()));
+ connect(embedCB, SIGNAL(toggled(bool)),
+ this, SLOT(change_adaptor()));
connect(WidthCB, SIGNAL( clicked()),
this, SLOT(change_adaptor()));
connect(HeightCB, SIGNAL( clicked()),
}
-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()){
+ docstring const str = browse(qstring_to_ucs4(filename->text()));
+ if (!str.empty()) {
filename->setText(toqstr(str));
changed();
}
}
-void GuiGraphicsDialog::on_getPB_clicked()
+void GuiGraphics::on_getPB_clicked()
{
getBB();
}
-void GuiGraphicsDialog::on_editPB_clicked()
+void GuiGraphics::on_editPB_clicked()
{
- controller().editGraphics();
+ editGraphics();
}
-void GuiGraphicsDialog::on_filename_textChanged(const QString & filename)
+void GuiGraphics::on_filename_textChanged(const QString & filename)
{
editPB->setDisabled(filename.isEmpty());
+ EmbeddedFile file = EmbeddedFile(fromqstr(filename), bufferFilepath());
}
-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() ||
}
-void GuiGraphicsDialog::updateContents()
+void GuiGraphics::updateContents()
{
// clear and fill in the comboboxes
vector<string> const bb_units = frontend::getBBUnits();
rtYunit->addItem(toqstr(*it));
}
- InsetGraphicsParams & igp = controller().params();
+ InsetGraphicsParams & igp = params_;
// set the right default unit
Length::UNIT unitDefault = Length::CM;
}
string const name =
- igp.filename.outputFilename(controller().bufferFilepath());
+ igp.filename.outputFilename(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 = readBB(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()]);
} else {
rtY->setText(toqstr(xl));
}
- controller().bbChanged = true;
+ bbChanged = true;
}
// Update the draft and clip mode
}
-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.set(fromqstr(filename->text()), bufferFilepath());
+ igp.filename.setEmbed(embedCB->checkState() == Qt::Checked);
// the bb section
igp.bb.erase();
- if (controller().bbChanged) {
+ if (bbChanged) {
string bb;
string lbXs = fromqstr(lbX->text());
string lbYs = fromqstr(lbY->text());
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 = readBB(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)
+{
+ InsetGraphicsMailer::string2params(data, buffer(), params_);
+ return true;
+}
+
+
+void GuiGraphics::clearParams()
+{
+ params_ = InsetGraphicsParams();
+}
+
+
+void GuiGraphics::dispatchParams()
+{
+ InsetGraphicsParams tmp_params(params_);
+ string const lfun =
+ InsetGraphicsMailer::params2string(tmp_params, buffer());
+ dispatch(FuncRequest(getLfun(), lfun));
+}
+
+
+docstring const GuiGraphics::browse(docstring const & in_name) const
+{
+ docstring const title = _("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.exists() && clip.isDirectory()))
+ clipdir = addName(package().system_support().absFilename(), "clipart");
+
+ return browseRelFile(in_name, from_utf8(bufferFilepath()),
+ title, FileFilterList(), false,
+ _("Clipart|#C#c"), from_utf8(clipdir),
+ _("Documents|#o#O"), from_utf8(lyxrc.document_path));
+}
+
+
+string const GuiGraphics::readBB(string const & file)
+{
+ FileName const abs_file = makeAbsPath(file, 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 makeAbsPath(fname, bufferFilepath()).isReadableFile();
+}
+
+
+void GuiGraphics::editGraphics()
+{
+ applyView();
+ string const lfun =
+ InsetGraphicsMailer::params2string(params_, buffer());
+ dispatch(FuncRequest(LFUN_GRAPHICS_EDIT, lfun));
+}
+
+
+namespace {
+
+char const * const bb_units[] = { "bp", "cm", "mm", "in" };
+size_t const bb_size = sizeof(bb_units) / sizeof(char *);
+
+// 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
+
+// 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
+
+
+vector<string> const getBBUnits()
+{
+ return vector<string>(bb_units, bb_units + bb_size);
}
+
+vector<RotationOriginPair> getRotationOriginData()
+{
+ 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;
+}
+
+
+Dialog * createGuiGraphics(GuiView & lv) { return new GuiGraphics(lv); }
+
+
} // namespace frontend
} // namespace lyx
-
#include "GuiGraphics_moc.cpp"
-