]> git.lyx.org Git - features.git/blobdiff - src/frontends/qt4/GuiGraphics.cpp
reduce line noise
[features.git] / src / frontends / qt4 / GuiGraphics.cpp
index 4ce719a1a3b6d4e26061c759a1db26df0c978a99..5eb0fba11f66182d00e08e82001766cd5f81e0d9 100644 (file)
@@ -3,6 +3,7 @@
  * 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 "controllers/frontend_helpers.h"
+#include "FuncRequest.h"
+#include "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 <algorithm>
 #include <cmath>
-
-using lyx::support::float_equal;
-using lyx::support::token;
-
-using lyx::support::os::internal_path;
+#include <utility>
 
 #ifndef CXX_GLOBAL_CSTD
 using std::floor;
 #endif
-
+using std::find;
 using std::vector;
 using std::string;
-
+using std::transform;
+using std::make_pair;
+using std::pair;
+using std::vector;
 
 namespace lyx {
 namespace frontend {
 
+using support::addName;
+using support::FileFilterList;
+using support::FileName;
+using support::float_equal;
+using support::makeAbsPath;
+using support::os::internal_path;
+using support::package;
+using support::readBB_from_PSFile;
+using support::token;
+
+
+//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
+//to make, since "auto" also appears in updateContents() (see
+//GuiGraphics.cpp).
+//The right way to do this, I think, would be to define a class
+//checkedLengthSet (and a partnering labeledLengthSete) that encapsulated
+//the checkbox, line edit, and length combo together, and then made e.g.
+//lengthToWidgets, widgetsToLength, etc, all public methods of that class.
+//Perhaps even the validator could be exposed through it.
+/**
+ * sets a checkbox-line edit-length combo group, using "text" if the
+ * checkbox is unchecked and clearing the line edit if it previously
+ * said "text".
+*/
+static void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
+       LengthCombo * lengthCombo/*, string text = "auto"*/)
+{
+       if (!checkBox->isChecked())
+               lengthToWidgets(lineEdit, lengthCombo,
+                               "auto", lengthCombo->currentLengthItem());
+       else if (lineEdit->text() == "auto")
+               lengthToWidgets(lineEdit, lengthCombo, string(),
+                               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;
 }
 
-GuiGraphicsDialog::GuiGraphicsDialog(LyXView & lv)
+///
+template<class Pair>
+vector<typename Pair::second_type> const
+getSecond(vector<Pair> const & pr)
+{
+        vector<typename Pair::second_type> tmp(pr.size());
+        transform(pr.begin(), pr.end(), tmp.begin(),
+                  boost::bind(&Pair::second, _1));
+        return tmp;
+}
+
+/// The (tranlated) GUI std::string and it's LaTeX equivalent.
+typedef std::pair<docstring, std::string> RotationOriginPair;
+///
+std::vector<RotationOriginPair> getRotationOriginData();
+
+
+GuiGraphics::GuiGraphics(LyXView & lv)
        : GuiDialog(lv, "graphics")
 {
        setupUi(this);
        setViewTitle(_("Graphics"));
-       setController(new ControlGraphics(*this));
 
        //main buttons
        connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
@@ -211,64 +273,57 @@ GuiGraphicsDialog::GuiGraphicsDialog(LyXView & lv)
 }
 
 
-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)
+void GuiGraphics::closeEvent(QCloseEvent * e)
 {
        slotClose();
-       e->accept();
+       GuiDialog::closeEvent(e);
 }
 
 
-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));
+               embedCB->setCheckState(Qt::Unchecked);
                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());
 }
 
 
-void GuiGraphicsDialog::setAutoText()
+void GuiGraphics::setAutoText()
 {
        if (scaleCB->isChecked())
                return;
@@ -280,7 +335,7 @@ void GuiGraphicsDialog::setAutoText()
 }
 
 
-void GuiGraphicsDialog::on_scaleCB_toggled(bool setScale)
+void GuiGraphics::on_scaleCB_toggled(bool setScale)
 {
        Scale->setEnabled(setScale);
        if (setScale) {
@@ -314,7 +369,7 @@ void GuiGraphicsDialog::on_scaleCB_toggled(bool setScale)
 }
 
 
-void GuiGraphicsDialog::on_WidthCB_toggled(bool setWidth)
+void GuiGraphics::on_WidthCB_toggled(bool setWidth)
 {
        Width->setEnabled(setWidth);
        widthUnit->setEnabled(setWidth);
@@ -339,7 +394,7 @@ void GuiGraphicsDialog::on_WidthCB_toggled(bool setWidth)
 }
 
 
-void GuiGraphicsDialog::on_HeightCB_toggled(bool setHeight)
+void GuiGraphics::on_HeightCB_toggled(bool setHeight)
 {
        Height->setEnabled(setHeight);
        heightUnit->setEnabled(setHeight);
@@ -364,7 +419,7 @@ void GuiGraphicsDialog::on_HeightCB_toggled(bool setHeight)
 }
 
 
-void GuiGraphicsDialog::on_angle_textChanged(const QString & filename)
+void GuiGraphics::on_angle_textChanged(const QString & filename)
 {
        rotateOrderCB->setEnabled((WidthCB->isChecked() ||
                                 HeightCB->isChecked() ||
@@ -381,7 +436,7 @@ static int getItemNo(const vector<string> & v, string const & s)
 }
 
 
-void GuiGraphicsDialog::updateContents()
+void GuiGraphics::updateContents()
 {
        // clear and fill in the comboboxes
        vector<string> const bb_units = frontend::getBBUnits();
@@ -397,7 +452,7 @@ void GuiGraphicsDialog::updateContents()
                rtYunit->addItem(toqstr(*it));
        }
 
-       InsetGraphicsParams & igp = controller().params();
+       InsetGraphicsParams & igp = params_;
 
        // set the right default unit
        Length::UNIT unitDefault = Length::CM;
@@ -412,12 +467,13 @@ void GuiGraphicsDialog::updateContents()
        }
 
        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)));
@@ -427,14 +483,14 @@ void GuiGraphicsDialog::updateContents()
                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()]);
@@ -463,7 +519,7 @@ void GuiGraphicsDialog::updateContents()
                } else {
                        rtY->setText(toqstr(xl));
                }
-               controller().bbChanged = true;
+               bbChanged = true;
        }
 
        // Update the draft and clip mode
@@ -554,16 +610,17 @@ void GuiGraphicsDialog::updateContents()
 }
 
 
-void GuiGraphicsDialog::applyView()
+void GuiGraphics::applyView()
 {
-       InsetGraphicsParams & igp = controller().params();
+       InsetGraphicsParams & igp = params_;
 
        igp.filename.set(internal_path(fromqstr(filename->text())),
-                        controller().bufferFilepath());
+                        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());
@@ -650,11 +707,11 @@ void GuiGraphicsDialog::applyView()
 }
 
 
-void GuiGraphicsDialog::getBB()
+void GuiGraphics::getBB()
 {
        string const fn = fromqstr(filename->text());
        if (!fn.empty()) {
-               string const bb = controller().readBB(fn);
+               string const bb = readBB(fn);
                if (!bb.empty()) {
                        lbX->setText(toqstr(token(bb, ' ', 0)));
                        lbY->setText(toqstr(token(bb, ' ', 1)));
@@ -667,19 +724,156 @@ void GuiGraphicsDialog::getBB()
                        rtXunit->setCurrentIndex(0);
                        rtYunit->setCurrentIndex(0);
                }
-               controller().bbChanged = false;
+               bbChanged = false;
        }
 }
 
 
-bool GuiGraphicsDialog::isValid()
+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->getWidth();
+                       height = image->getHeight();
+               }
+       }
+
+       return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
+}
+
+
+bool GuiGraphics::isFilenameValid(string const & fname) const
+{
+       // It may be that the filename is relative.
+       return makeAbsPath(fname, bufferFilepath()).isFileReadable();
+}
+
+
+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(LyXView & lv) { return new GuiGraphics(lv); }
+
+
 } // namespace frontend
 } // namespace lyx
 
-
 #include "GuiGraphics_moc.cpp"
-