OSDN Git Service

Added LaTex image insert & edit
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / BrowserWindow.java
index a3de546..0aab4b9 100644 (file)
@@ -104,6 +104,9 @@ import com.trolltech.qt.gui.QToolButton;
 import com.trolltech.qt.gui.QToolButton.ToolButtonPopupMode;\r
 import com.trolltech.qt.gui.QVBoxLayout;\r
 import com.trolltech.qt.gui.QWidget;\r
+import com.trolltech.qt.network.QNetworkAccessManager;\r
+import com.trolltech.qt.network.QNetworkReply;\r
+import com.trolltech.qt.network.QNetworkReply.NetworkError;\r
 import com.trolltech.qt.network.QNetworkRequest;\r
 import com.trolltech.qt.webkit.QWebPage;\r
 import com.trolltech.qt.webkit.QWebPage.WebAction;\r
@@ -114,6 +117,7 @@ import cx.fbn.nevernote.Global;
 import cx.fbn.nevernote.dialog.EnCryptDialog;\r
 import cx.fbn.nevernote.dialog.EnDecryptDialog;\r
 import cx.fbn.nevernote.dialog.GeoDialog;\r
+import cx.fbn.nevernote.dialog.InsertLatexImage;\r
 import cx.fbn.nevernote.dialog.InsertLinkDialog;\r
 import cx.fbn.nevernote.dialog.SpellCheck;\r
 import cx.fbn.nevernote.dialog.TableDialog;\r
@@ -162,7 +166,6 @@ public class BrowserWindow extends QWidget {
        private String saveNoteTitle;\r
        private String saveTagList;\r
        private boolean insideList;\r
-//     private String selectedText;\r
        private final DatabaseConnection conn;\r
        private final QCalendarWidget createdCalendarWidget;\r
        private final QCalendarWidget alteredCalendarWidget;\r
@@ -241,6 +244,9 @@ public class BrowserWindow extends QWidget {
        boolean insertHyperlink = true;\r
        boolean insideTable = false;\r
        boolean insideEncryption = false;\r
+       public Signal1<Long> blockApplication;\r
+       public Signal0 unblockApplication;\r
+       String latexGuid;  // This is set if we are editing an existing LaTeX formula.  Useful to track guid.\r
 \r
        \r
        public static class SuggestionListener implements SpellCheckListener {\r
@@ -595,6 +601,9 @@ public class BrowserWindow extends QWidget {
                tagEdit.setPalette(pal);\r
                notebookBox.setPalette(pal);\r
                \r
+               blockApplication = new Signal1<Long>();\r
+               unblockApplication = new Signal0();\r
+               \r
                logger.log(logger.HIGH, "Browser setup complete");\r
        }\r
 \r
@@ -855,7 +864,15 @@ public class BrowserWindow extends QWidget {
        @SuppressWarnings("unused")\r
        private void linkClicked(QUrl url) {\r
                logger.log(logger.EXTREME, "URL Clicked: " +url.toString());\r
-               if (url.toString().substring(0,8).equals("nnres://")) {\r
+               if (url.toString().startsWith("latex://")) {\r
+                       int position = url.toString().lastIndexOf(".");\r
+                       String guid = url.toString().substring(0,position);\r
+                       position = guid.lastIndexOf("/");\r
+                       guid = guid.substring(position+1);\r
+                       editLatex(guid);\r
+                       return;\r
+               }\r
+               if (url.toString().startsWith("nnres://")) {\r
                        logger.log(logger.EXTREME, "URL is NN resource");\r
                        if (url.toString().endsWith("/vnd.evernote.ink")) {\r
                                logger.log(logger.EXTREME, "Unable to open ink note");\r
@@ -1392,6 +1409,109 @@ public class BrowserWindow extends QWidget {
        }\r
        \r
        \r
+       // Insert a hyperlink\r
+       public void insertLatex() {\r
+               editLatex(null);\r
+       }\r
+       public void editLatex(String guid) {\r
+               logger.log(logger.EXTREME, "Inserting latex");\r
+               String text = browser.selectedText();\r
+               if (text.trim().equalsIgnoreCase("")) {\r
+                       InsertLatexImage dialog = new InsertLatexImage();\r
+                       if (guid != null) {\r
+                               String formula = conn.getNoteTable().noteResourceTable.getNoteSourceUrl(guid).replace("http://latex.codecogs.com/gif.latex?", "");\r
+                               dialog.setFormula(formula);\r
+                       }\r
+                       dialog.exec();\r
+                       if (!dialog.okPressed()) {\r
+                               logger.log(logger.EXTREME, "Edit LaTex canceled");\r
+                               return;\r
+                       }\r
+                       text = dialog.getFormula().trim();\r
+               }\r
+               blockApplication.emit(new Long(5000));\r
+               logger.log(logger.EXTREME, "Inserting LaTeX formula:" +text);\r
+               latexGuid = guid;\r
+               text = StringUtils.replace(text, "'", "\\'");\r
+               String url = "http://latex.codecogs.com/gif.latex?" +text;\r
+               QNetworkAccessManager manager = new QNetworkAccessManager(this);\r
+               manager.finished.connect(this, "insertLatexImageReady(QNetworkReply)");\r
+               manager.get(new QNetworkRequest(new QUrl(url)));\r
+       }\r
+       \r
+       public void insertLatexImageReady(QNetworkReply reply) {\r
+               if (reply.error() != NetworkError.NoError) \r
+                       return;\r
+               \r
+               QByteArray image = reply.readAll();\r
+\r
+\r
+               Resource newRes = null;\r
+               QFile tfile;\r
+               String path;\r
+               if (latexGuid == null) {\r
+                       path = Global.getFileManager().getResDirPath("latex-temp.gif");\r
+                       tfile = new QFile(path);\r
+                       tfile.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));\r
+                       tfile.write(image);\r
+                       tfile.close();\r
+                       newRes = createResource(path,0,"image/gif", false);\r
+                       path = Global.getFileManager().getResDirPath(newRes.getGuid()+".gif");\r
+                       tfile.rename(path);\r
+               } else {\r
+                       newRes = conn.getNoteTable().noteResourceTable.getNoteResource(latexGuid, false);\r
+                       path = Global.getFileManager().getResDirPath(newRes.getGuid()+".gif");\r
+                       tfile = new QFile(path);\r
+                       tfile.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));\r
+                       tfile.write(image);\r
+                       tfile.close();\r
+               }\r
+\r
+\r
+               newRes.getAttributes().setSourceURL(reply.url().toString());\r
+//             newRes.getData().setBody(image.toByteArray());\r
+//             conn.getNoteTable().noteResourceTable.updateNoteResource(newRes, true);\r
+               conn.getNoteTable().noteResourceTable.updateNoteSourceUrl(newRes.getGuid(), reply.url().toString(), true);\r
+               \r
+               for(int i=0; i<currentNote.getResourcesSize(); i++) {\r
+                       if (currentNote.getResources().get(i).getGuid().equals(newRes.getGuid())) {\r
+                               currentNote.getResources().remove(i);\r
+                               i=currentNote.getResourcesSize();\r
+                       }\r
+               }\r
+               currentNote.getResources().add(newRes);\r
+               \r
+\r
+               // do the actual insert into the note.  We only do this on new formulas.  Existing ones we\r
+               // just write out the file (which is aleady done) and reload.\r
+               if (latexGuid == null) {\r
+                       StringBuffer buffer = new StringBuffer(100);\r
+                       String formula = reply.url().toString().toLowerCase().replace("http://latex.codecogs.com/gif.latex?", "");\r
+                       buffer.append("<a href=\"latex://"+path.replace("\\", "/")+"\" title=\""+formula+"\"><img src=\"");\r
+                       buffer.append(path.replace("\\", "/"));\r
+                       buffer.append("\" en-tag=en-media type=\"image/gif\""\r
+                               +" hash=\""+Global.byteArrayToHexString(newRes.getData().getBodyHash()) +"\""\r
+                               +" guid=\"" +newRes.getGuid() +"\""\r
+                               + " /></a>");\r
+               \r
+                       String script_start = new String("document.execCommand('insertHTML', false, '");\r
+                       String script_end = new String("');");\r
+                       browser.page().mainFrame().evaluateJavaScript(\r
+                                       script_start + buffer + script_end);\r
+               }\r
+               QWebSettings.setMaximumPagesInCache(0);\r
+               QWebSettings.setObjectCacheCapacities(0, 0, 0);\r
+               browser.setHtml(browser.page().mainFrame().toHtml());\r
+               browser.reload();\r
+               contentChanged();\r
+               resourceSignal.contentChanged.emit(path);\r
+       unblockApplication.emit();\r
+               return;\r
+               \r
+       }\r
+\r
+       \r
+       \r
        // Insert a table\r
        public void insertTable() {\r
                TableDialog dialog = new TableDialog();\r
@@ -1932,7 +2052,6 @@ public class BrowserWindow extends QWidget {
                buffer.append("\" en-tag=en-media type=\"image/jpeg\""\r
                                +" hash=\""+Global.byteArrayToHexString(newRes.getData().getBodyHash()) +"\""\r
                                +" guid=\"" +newRes.getGuid() +"\""\r
-//                             +" onContextMenu=\"window.jambi.imageContextMenu('" +tfile.fileName() +"');\""\r
                                +" onContextMenu=\"window.jambi.imageContextMenu(&amp." +tfile.fileName() +"&amp.);\""\r
                                + " />");\r
                \r
@@ -2704,8 +2823,14 @@ public class BrowserWindow extends QWidget {
                browser.openAction.setEnabled(true);\r
                selectedFile = f;\r
        }\r
-       \r
-       \r
+       public void latexContextMenu(String f) {\r
+               browser.downloadImage.setEnabled(true);\r
+               browser.rotateImageRight.setEnabled(true);\r
+               browser.rotateImageLeft.setEnabled(true);\r
+               browser.openAction.setEnabled(true);\r
+               selectedFile = f;\r
+       }\r
+\r
        //****************************************************************\r
        //* Apply CSS style to specified word\r
        //****************************************************************\r