OSDN Git Service

Add check for length = 0 on recognition in highlight checking.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / xml / NoteFormatter.java
index 0ec67ef..d513322 100644 (file)
@@ -10,8 +10,13 @@ import com.trolltech.qt.core.QByteArray;
 import com.trolltech.qt.core.QDataStream;\r
 import com.trolltech.qt.core.QFile;\r
 import com.trolltech.qt.core.QIODevice;\r
+import com.trolltech.qt.core.QIODevice.OpenModeFlag;\r
 import com.trolltech.qt.core.QTemporaryFile;\r
 import com.trolltech.qt.core.QUrl;\r
+import com.trolltech.qt.core.Qt.BGMode;\r
+import com.trolltech.qt.gui.QColor;\r
+import com.trolltech.qt.gui.QPainter;\r
+import com.trolltech.qt.gui.QPixmap;\r
 import com.trolltech.qt.xml.QDomAttr;\r
 import com.trolltech.qt.xml.QDomDocument;\r
 import com.trolltech.qt.xml.QDomElement;\r
@@ -29,7 +34,8 @@ public class NoteFormatter {
        private final ApplicationLogger logger;\r
        private final DatabaseConnection conn;\r
        public boolean resourceError = false;\r
-       public boolean readOnly = false;\r
+       public boolean readOnly = false; \r
+       public boolean inkNote = false;\r
        public boolean addHighlight = true;\r
        private Note currentNote;\r
        private String currentNoteGuid;\r
@@ -48,8 +54,12 @@ public class NoteFormatter {
        public void setNote(Note note, boolean pdfPreview) {\r
                currentNote = note;\r
                this.pdfPreview = pdfPreview;\r
-               currentNoteGuid = note.getGuid();\r
                readOnly = false;\r
+               currentNoteGuid = null;\r
+               if (note != null) {\r
+                       currentNoteGuid = note.getGuid();\r
+                       readOnly = conn.getNotebookTable().isReadOnly(note.getNotebookGuid());\r
+               } \r
                resourceError = false;\r
        }\r
        \r
@@ -70,6 +80,8 @@ public class NoteFormatter {
        \r
        // Rebuild the note HTML to something usable\r
        public String rebuildNoteHTML() {\r
+               if (currentNote == null)\r
+                       return null;\r
                logger.log(logger.HIGH, "Entering NeverNote.rebuildNoteHTML");\r
                logger.log(logger.EXTREME, "Note guid: " +currentNoteGuid);\r
                logger.log(logger.EXTREME, "Note Text:" +currentNote);\r
@@ -114,6 +126,76 @@ public class NoteFormatter {
                return html.toString();\r
        }       \r
 \r
+       private void addImageHilight(String resGuid, QFile f) {\r
+               if (enSearch == null || enSearch.hilightWords == null || enSearch.hilightWords.size() == 0)\r
+                       return;\r
+               \r
+               // Get the recognition XML that tells where to hilight on the image\r
+               Resource recoResource = conn.getNoteTable().noteResourceTable.getNoteResourceRecognition(resGuid);\r
+               if (recoResource.getRecognition().getBody() == null || recoResource.getRecognition().getBody().length == 0)\r
+                       return;\r
+               QByteArray recoData = new QByteArray(recoResource.getRecognition().getBody());\r
+               String xml = recoData.toString();\r
+               \r
+               // Get a painter for the image.  This is the background (the initial image).\r
+       QPixmap pix = new QPixmap(f.fileName());\r
+       QPixmap hilightedPix = new QPixmap(pix.size());\r
+       QPainter p = new QPainter(hilightedPix);\r
+       p.drawPixmap(0,0, pix);\r
+\r
+       // Create a transparent pixmap.  The only non-transparent\r
+       // piece is the hilight that will be overlayed to hilight text no the background\r
+       QPixmap overlayPix = new QPixmap(pix.size());\r
+       QPainter p2 = new QPainter(overlayPix);\r
+       p2.setBackgroundMode(BGMode.TransparentMode);\r
+       p2.setBrush(QColor.yellow);\r
+       \r
+               // Get the recognition data from the note\r
+       QDomDocument doc = new QDomDocument();\r
+       doc.setContent(xml);\r
+       \r
+       // Go through all "item" nodes\r
+               QDomNodeList anchors = doc.elementsByTagName("item");\r
+               for (int i=0; i<anchors.length(); i++) {\r
+                       QDomElement element = anchors.at(i).toElement();\r
+                       int x = new Integer(element.attribute("x"));   // x coordinate\r
+                       int y = new Integer(element.attribute("y"));   // y coordinate\r
+                       int w = new Integer(element.attribute("w"));   // width\r
+                       int h = new Integer(element.attribute("h"));   // height\r
+                       QDomNodeList children = element.childNodes();  // all children ("t" nodes).\r
+                       \r
+                       // Go through the children ("t" nodes)\r
+                       for (int j=0; j<children.length(); j++) {\r
+                       QDomElement child = children.at(j).toElement();\r
+                       if (child.nodeName().equalsIgnoreCase("t")) {\r
+                               String text = child.text();   // recognition text\r
+                               int weight = new Integer(child.attribute("w"));  // recognition weight\r
+                               if (weight >= Global.getRecognitionWeight()) {   // Are we above the maximum?\r
+                                       \r
+                                       // Check to see if this word matches something we were searching for.\r
+                                       for (int k=0; k<enSearch.hilightWords.size(); k++) {\r
+                                               String searchWord = enSearch.hilightWords.get(k).toLowerCase();\r
+                                               if (searchWord.startsWith("*"))\r
+                                                       searchWord = searchWord.substring(1);\r
+                                               if (searchWord.endsWith("*"))\r
+                                                       searchWord = searchWord.substring(0,searchWord.length()-1);\r
+                                               if (text.toLowerCase().contains(searchWord))\r
+                                                       p2.drawRect(x,y,w,h);                           \r
+                                       }\r
+                               }\r
+                       }\r
+                       }\r
+               }\r
+       p2.end();\r
+       \r
+       // Paint the hilight onto the background.\r
+       p.setOpacity(0.40);\r
+       p.drawPixmap(0,0, overlayPix);\r
+       p.end();\r
+       \r
+       // Save over the initial pixmap.\r
+       hilightedPix.save(f.fileName());\r
+       }\r
        \r
     // Modify the en-media tag into an image tag so it can be displayed.\r
     private void modifyImageTags(QDomElement docElem, QDomElement enmedia, QDomAttr hash) {\r
@@ -126,24 +208,30 @@ public class NoteFormatter {
        \r
        String resGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(currentNoteGuid, hash.value());\r
        QFile tfile = new QFile(Global.getFileManager().getResDirPath(resGuid + type));\r
-       if (!tfile.exists()) {\r
+//     if (!tfile.exists()) {\r
                Resource r = null;\r
                if (resGuid != null)\r
                        r = conn.getNoteTable().noteResourceTable.getNoteResource(resGuid,true);\r
-                       if (r==null || r.getData() == null || r.getData().getBody().length == 0)\r
-                               resourceError = true;;\r
+                       if (r==null || r.getData() == null || r.getData().getBody().length == 0) {\r
+                               resourceError = true;\r
+                               readOnly = true;\r
+                       }\r
                        if (r!= null && r.getData() != null && r.getData().getBody().length > 0) {\r
                                tfile.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));\r
                                QByteArray binData = new QByteArray(r.getData().getBody());\r
                                tfile.write(binData);\r
                                tfile.close();\r
+                               \r
+                               // If we have recognition text, outline it\r
+                               addImageHilight(r.getGuid(), tfile);\r
+                               \r
                                enmedia.setAttribute("src", QUrl.fromLocalFile(tfile.fileName()).toString());\r
                                enmedia.setAttribute("en-tag", "en-media");\r
                                enmedia.setNodeValue("");\r
                        enmedia.setAttribute("guid", r.getGuid());\r
                        enmedia.setTagName("img");\r
                }\r
-       }\r
+//     }\r
        // Technically, we should do a file:// to have a proper url, but for some reason QWebPage hates\r
        // them and won't generate a thumbnail image properly if we use them.\r
 //             enmedia.setAttribute("src", QUrl.fromLocalFile(tfile.fileName()).toString());\r
@@ -237,12 +325,54 @@ public class NoteFormatter {
                return doc;\r
        }\r
        \r
+\r
+       // Get an ink note image.  If an image doesn't exist then we fall back \r
+       // to the old ugly icon\r
+    private boolean buildInkNote(QDomDocument doc, QDomElement docElem, QDomElement enmedia, QDomAttr hash, String appl) {\r
+       String resGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(currentNote.getGuid(), hash.value());\r
+       Resource r = conn.getNoteTable().noteResourceTable.getNoteResource(resGuid, false);\r
\r
+       // If we can't find the resource, then fall back to the old method.  We'll return & show\r
+       // an error later\r
+       if (r == null || r.getData() == null) \r
+               return false;\r
+       \r
+       // If there isn't some type of error, continue on.\r
+               if (!resourceError) {\r
+                       \r
+                       // Get a list of images in the database.  We'll use these to bulid the page.\r
+                       List<QByteArray> data = conn.getInkImagesTable().getImage(r.getGuid());\r
+                       \r
+                       // If no pictures are found, go back to & just show the icon\r
+                       if (data.size() == 0)\r
+                               return false;\r
+                       \r
+                       // We have pictures, so append them to the page.  This really isn't proper since\r
+                       // we leave the en-media tag in place, but since we can't edit the page it doesn't\r
+                       // hurt anything.\r
+                       for (int i=0; i<data.size(); i++) {\r
+                       QFile f = new QFile(Global.getFileManager().getResDirPath(resGuid + new Integer(i).toString()+".png"));\r
+                               f.open(OpenModeFlag.WriteOnly);\r
+                               f.write(data.get(i));\r
+                               f.close();\r
+                               QDomElement newImage = doc.createElement("img");\r
+                               newImage.setAttribute("src", QUrl.fromLocalFile(f.fileName()).toString());\r
+                               enmedia.appendChild(newImage);\r
+                       }\r
+                       return true;\r
+               }\r
+       return false;\r
+    }\r
+       \r
        \r
     // Modify the en-media tag into an attachment\r
     private void modifyApplicationTags(QDomDocument doc, QDomElement docElem, QDomElement enmedia, QDomAttr hash, String appl) {\r
        logger.log(logger.HIGH, "Entering NeverNote.modifyApplicationTags");\r
-       if (appl.equalsIgnoreCase("vnd.evernote.ink"))\r
-               readOnly = true;\r
+       if (appl.equalsIgnoreCase("vnd.evernote.ink")) {\r
+               inkNote = true;\r
+           if (buildInkNote(doc, docElem, enmedia, hash, appl))\r
+               return;\r
+       }\r
        String resGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(currentNote.getGuid(), hash.value());\r
        Resource r = conn.getNoteTable().noteResourceTable.getNoteResource(resGuid, false);\r
        if (r == null || r.getData() == null) \r
@@ -379,6 +509,7 @@ public class NoteFormatter {
                        todo.setAttribute("unchecked","");\r
                todo.setAttribute("value", checked);\r
                todo.setAttribute("onClick", "value=checked;window.jambi.contentChanged(); ");\r
+               todo.setAttribute("onMouseOver", "style.cursor='hand'");\r
                todo.setTagName("input");\r
                logger.log(logger.HIGH, "Leaving NeverNote.modifyTodoTags");\r
     }\r
@@ -431,5 +562,4 @@ public class NoteFormatter {
        return "attachment.png";\r
     }\r
 \r
-\r
 }\r