From lyx-devel Fri Jan 28 04:34:35 2005 From: Johnathan Burchill Date: Fri, 28 Jan 2005 04:34:35 +0000 To: lyx-devel Subject: Drag-n-drop image files (QT) Message-Id: <200501272134.42178.jkerrb () users ! sourceforge ! net> X-MARC-Message: https://marc.info/?l=lyx-devel&m=110688726102596 MIME-Version: 1 Content-Type: multipart/mixed; boundary="--nextPart1611559.oAL8W5lptu" --nextPart1611559.oAL8W5lptu Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Howdy, A colleague asked me if there was anything that MS word could do well, that= =20 LyX couldn't, and all I could think of was drag-n-dropping image files=20 into the document. That got me to thinking what it would take to implement= =20 it in LyX. Here's one solution, albeit somewhat naive perhaps, as I've not= =20 been hacking LyX for quite some time, so there may be unaccounted=20 side-effects. The strategy was to build on the current drag-n-drop support, which allows= =20 you to open a lyx file by dropping it in the main window. Now=20 QWorkArea::dropEvent() checks each entry in the QStringList::Iterator, and= =20 if it represents an image file, dispatch sends an LFUN_INSET_INSERT=20 graphics request. If it isn't a graphics file, then dispatch requests an=20 LFUN_FILE_OPEN. Then in factory.C, createInset() handles the LFUN_INSET_INSERT "graphics"=20 by adding the filename obtained from the dropEvent to the=20 InsetGraphicsParams structure before InsetGraphics::setParams() is called. I've patched 1.3.5-qt (3.3.3) for drag-n-drop of graphics files, and it=20 works great. Also have a patch for 1.4.0 cvs, but it's buggy, and I'm not=20 sure whether it's my doing or just a result of the current state of cvs. In 1.3.5, the image is placed at the cursor, already showing, and clipped.= =20 But in 1.4.0, the image is always inserted at the top of the document, and= =20 there are bizarre redraws with multiple images (e.g. some are missing=20 until you resize the window a certain way). Is anyone working on dnd image support? The 1.4.0 current cvs patch follows. Let me know if you want the patch for= =20 1.3.5. JB =2D-=20 Johnathan K. Burchill, Ph.D. jkerrb@users.sourceforge.net Patch for 1.4.0 current cvs: Index: src/frontends/qt2/QWorkArea.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvs/lyx/lyx-devel/src/frontends/qt2/QWorkArea.h,v retrieving revision 1.15 diff -u -3 -p -u -r1.15 QWorkArea.h =2D-- src/frontends/qt2/QWorkArea.h 2004/04/28 17:22:04 1.15 +++ src/frontends/qt2/QWorkArea.h 2005/01/28 04:29:20 @@ -64,6 +64,8 @@ public: /// get the content pane widget QWidget * getContent() const { return content_; } private: + /// Test whether str is a loadable image + bool decodeGraphics( QString const & str ); /// scroll bar QScrollBar * scrollbar_; /// content Index: src/frontends/qt2/QWorkArea.C =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvs/lyx/lyx-devel/src/frontends/qt2/QWorkArea.C,v retrieving revision 1.28 diff -u -3 -p -u -r1.28 QWorkArea.C =2D-- src/frontends/qt2/QWorkArea.C 2004/05/20 09:36:28 1.28 +++ src/frontends/qt2/QWorkArea.C 2005/01/28 04:29:20 @@ -225,7 +225,24 @@ void QWorkArea::dropEvent(QDropEvent* ev lyxerr[Debug::GUI] << "QWorkArea::dropEvent: got URIs!" << endl; for (QStringList::Iterator i =3D files.begin(); =2D i!=3Dfiles.end(); ++i) =2D dispatch(FuncRequest(LFUN_FILE_OPEN,=20 fromqstr(*i))); + i!=3Dfiles.end(); ++i) { + //First see if a graphic was dropped + if ( decodeGraphics(*i) ) { + dispatch(FuncRequest(LFUN_INSET_INSERT, + "graphics \"" + fromqstr(*i) + "\"")); + } else { + dispatch(FuncRequest(LFUN_FILE_OPEN,=20 fromqstr(*i))); + } + } } } + +bool QWorkArea::decodeGraphics( QString const & str ) +{ + //Allow ps, eps, and allowable QT formats + QString name(str); + return (name.endsWith(".ps", false) || + name.endsWith(".eps", false) || + (QImageIO::imageFormat(name) !=3D 0)); +} + Index: src/factory.C =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvs/lyx/lyx-devel/src/factory.C,v retrieving revision 1.95 diff -u -3 -p -u -r1.95 factory.C =2D-- src/factory.C 2004/08/14 15:55:17 1.95 +++ src/factory.C 2005/01/28 04:29:20 @@ -252,6 +252,12 @@ InsetBase * createInset(BufferView * bv, InsetGraphicsMailer::string2params(cmd.argument, buffer, igp); auto_ptr inset(new InsetGraphics); + string const & filename =3D cmd.getArg( 1 ); + if (!filename.empty()) { + igp.filename =3D filename; + //automatically clip the image + igp.clip =3D true; + } inset->setParams(igp); return inset.release(); --nextPart1611559.oAL8W5lptu Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.7 (GNU/Linux) iD8DBQBB+cDirXMSOtiWrO4RAkDeAKDBr7qJvN+T8nOS4YlU7yoKT62fTACdF6fr pWqqg7fIJ6+WJLJB46kS3Vk= =7WuY -----END PGP SIGNATURE----- --nextPart1611559.oAL8W5lptu--