OSDN Git Service

Add ability to customize tag icons
authorRandy Baumgarte <randy@fbn.cx>
Sun, 7 Nov 2010 15:58:54 +0000 (10:58 -0500)
committerRandy Baumgarte <randy@fbn.cx>
Wed, 15 Dec 2010 15:15:43 +0000 (10:15 -0500)
shortcuts_sample.txt
src/cx/fbn/nevernote/NeverNote.java
src/cx/fbn/nevernote/gui/MainMenuBar.java
src/cx/fbn/nevernote/gui/TagTreeWidget.java
src/cx/fbn/nevernote/sql/TagTable.java

index d26794d..51f1bb2 100644 (file)
@@ -9,9 +9,11 @@ File_Notebook_Add                              // Add a notebook
 File_Notebook_Edit                             // Edit an existing notebook
 File_Notebook_Delete                           // Delete the existing notebook
 File_Notebook_Close                            // Open/Close (i.e. archive) a notebook
+File_Notebook_Icon                             // Modify the selected notebook icon
 File_Tag_Add                   Ctrl+Shift+T    // Add a notebook
 File_Tag_Edit                                  // Edit an existing notebook
 File_Tag_Delete                                        // Delete the existing notebook
+File_Tag_Icon                                  // Modify the selected tag(s) icon.
 File_SavedSearch_Add                           // Add a notebook
 File_SavedSearch_Edit                          // Edit an existing notebook
 File_SavedSearch_Delete                                // Delete the existing notebook
index fe8b947..743873f 100644 (file)
@@ -505,6 +505,7 @@ public class NeverNote extends QMainWindow{
                tagTree.setDeleteAction(menuBar.tagDeleteAction);
                tagTree.setEditAction(menuBar.tagEditAction);
                tagTree.setAddAction(menuBar.tagAddAction);
+               tagTree.setIconAction(menuBar.tagIconAction);
                tagTree.setVisible(Global.isWindowVisible("tagTree"));
                tagTree.noteSignal.tagsAdded.connect(this, "tagsAdded(String, String)");
                menuBar.hideTags.setChecked(Global.isWindowVisible("tagTree"));
@@ -1706,10 +1707,12 @@ public class NeverNote extends QMainWindow{
        if (selections.size() > 0) {
                menuBar.tagEditAction.setEnabled(true);
                menuBar.tagDeleteAction.setEnabled(true);
+               menuBar.tagIconAction.setEnabled(true);
        }
        else {
                menuBar.tagEditAction.setEnabled(false);
                menuBar.tagDeleteAction.setEnabled(false);
+               menuBar.tagIconAction.setEnabled(true);
        }
        if (selectedTagGUIDs.size() == 1 && selectedTagGUIDs.get(0).equals(previousSelectedTag)) {
                previousSelectedTag = selectedTagGUIDs.get(0);
@@ -1739,8 +1742,10 @@ public class NeverNote extends QMainWindow{
 //             selectedTagGUIDs.clear();  // clear out old entries
 
                tagTree.blockSignals(true);
-               if (reload)
+               if (reload) {
+                       tagTree.setIcons(conn.getTagTable().getAllIcons());
                        tagTree.load(listManager.getTagIndex());
+               }
        for (int i=selectedTagGUIDs.size()-1; i>=0; i--) {
                boolean found = tagTree.selectGuid(selectedTagGUIDs.get(i));
                if (!found)
@@ -1861,11 +1866,43 @@ public class NeverNote extends QMainWindow{
                menuBar.noteRestoreAction.setVisible(false);
                menuBar.tagEditAction.setEnabled(false);
                menuBar.tagDeleteAction.setEnabled(false);
+               menuBar.tagIconAction.setEnabled(false);
                selectedTagGUIDs.clear();
        listManager.setSelectedTags(selectedTagGUIDs);
        tagTree.blockSignals(false);
        }
+       // Change the icon for a tag
+       // Change the notebook's icon
+       private void setTagIcon() {
+               QTreeWidgetItem currentSelection;
+               List<QTreeWidgetItem> selections = tagTree.selectedItems();
+               if (selections.size() == 0)
+                       return;
+               
+               currentSelection = selections.get(0);   
+               String guid = currentSelection.text(2);
+
+               QIcon currentIcon = currentSelection.icon(0);
+               QIcon icon = conn.getTagTable().getIcon(guid);
+               SetIcon dialog;
+               if (icon == null) {
+                       dialog = new SetIcon(currentIcon);
+                       dialog.setUseDefaultIcon(true);
+               } else {
+                       dialog = new SetIcon(icon);
+                       dialog.setUseDefaultIcon(false);
+               }
+               dialog.exec();
+               if (dialog.okPressed()) {
+                       QIcon newIcon = dialog.getIcon();
+                       conn.getTagTable().setIcon(guid, newIcon, dialog.getFileType());
+                       if (newIcon == null) 
+                               newIcon = new QIcon(iconPath+"tag.png");
+                       currentSelection.setIcon(0, newIcon);
+               }
        
+       }
+
        
     //***************************************************************
     //***************************************************************
index 08605e4..eed7874 100644 (file)
@@ -111,6 +111,7 @@ public class MainMenuBar extends QMenuBar {
        public QAction                  tagEditAction;                          // Edit a tag\r
        public QAction                  tagAddAction;                           // Add a tag\r
        public QAction                  tagDeleteAction;                        // Delete a tag\r
+       public QAction                  tagIconAction;                          // Change the icon\r
        \r
        //**************************************************************************\r
        //* Menu Bar Titles\r
@@ -472,6 +473,11 @@ public class MainMenuBar extends QMenuBar {
                tagDeleteAction.setEnabled(false);              \r
                setupShortcut(tagDeleteAction, "File_Tag_Delete");\r
                                \r
+               tagIconAction = new QAction(tr("Change Icon"), this);\r
+               tagIconAction.triggered.connect(parent, "setTagIcon()");\r
+               tagIconAction.setEnabled(false);                \r
+               setupShortcut(tagIconAction, "File_Tag_Icon");\r
+                               \r
                savedSearchAddAction = new QAction(tr("Add"),this);\r
                savedSearchAddAction.triggered.connect(parent, "addSavedSearch()");\r
                setupShortcut(savedSearchAddAction, "File_SavedSearch_Add");\r
@@ -664,6 +670,8 @@ public class MainMenuBar extends QMenuBar {
                tagMenu.addAction(tagAddAction);\r
                tagMenu.addAction(tagEditAction);\r
                tagMenu.addAction(tagDeleteAction);\r
+               tagMenu.addSeparator();\r
+               tagMenu.addAction(tagIconAction);\r
                \r
                savedSearchMenu.addAction(savedSearchAddAction);\r
                savedSearchMenu.addAction(savedSearchEditAction);\r
index 66d31c2..7859383 100644 (file)
@@ -21,6 +21,7 @@
 package cx.fbn.nevernote.gui;\r
 \r
 import java.util.ArrayList;\r
+import java.util.HashMap;\r
 import java.util.List;\r
 \r
 import com.evernote.edam.type.Tag;\r
@@ -53,10 +54,12 @@ public class TagTreeWidget extends QTreeWidget {
        private QAction editAction;\r
        private QAction deleteAction;\r
        private QAction addAction;\r
+       private QAction iconAction;\r
        public TagSignal tagSignal;\r
        public NoteSignal noteSignal;\r
        private boolean showAllTags;\r
        private final DatabaseConnection db;\r
+       private HashMap<String, QIcon>  icons;\r
        \r
        \r
        public TagTreeWidget(DatabaseConnection d) {\r
@@ -93,6 +96,9 @@ public class TagTreeWidget extends QTreeWidget {
        public void setAddAction(QAction a) {\r
                addAction = a;\r
        }\r
+       public void setIconAction(QAction i) {\r
+               iconAction = i;\r
+       }\r
        \r
        // Insert a new tag into the tree.  This is used when we dynamically add a \r
        // new tag after the full tag tree has been built.  It only adds to the\r
@@ -141,7 +147,12 @@ public class TagTreeWidget extends QTreeWidget {
                        if (tag.getParentGuid()==null || tag.getParentGuid().equals("")) {\r
                                child = new QTreeWidgetItem();\r
                                child.setText(0, tag.getName());\r
-                               child.setIcon(0,icon);\r
+                               if (icons != null && !icons.containsKey(tag.getGuid())) {\r
+                                       child.setIcon(0, icon);\r
+                               } else {\r
+                                       child.setIcon(0, icons.get(tag.getGuid()));\r
+                               }\r
+\r
                                child.setText(2, tag.getGuid());\r
                                child.setTextAlignment(1, ra.value());\r
                                index.add(child);\r
@@ -321,9 +332,15 @@ public class TagTreeWidget extends QTreeWidget {
                menu.addAction(addAction);\r
                menu.addAction(editAction);\r
                menu.addAction(deleteAction);\r
+               menu.addSeparator();\r
+               menu.addAction(iconAction);\r
                menu.exec(event.globalPos());\r
        }\r
        \r
+       public void setIcons(HashMap<String, QIcon> i) {\r
+               icons = i;\r
+       }\r
+       \r
        // Copy an individual item within the tree.  I need to do this because\r
        // Qt doesn't call the dropMimeData on a move, just a copy.\r
        private void copyTreeItem(QTreeWidgetItem source, QTreeWidgetItem target) {\r
index 23d4a19..6457544 100644 (file)
 package cx.fbn.nevernote.sql;\r
 \r
 import java.util.ArrayList;\r
+import java.util.HashMap;\r
 import java.util.List;\r
 \r
 import com.evernote.edam.type.Tag;\r
+import com.trolltech.qt.core.QBuffer;\r
+import com.trolltech.qt.core.QByteArray;\r
+import com.trolltech.qt.core.QIODevice;\r
+import com.trolltech.qt.gui.QIcon;\r
+import com.trolltech.qt.gui.QImage;\r
+import com.trolltech.qt.gui.QPixmap;\r
 \r
 import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
@@ -31,6 +38,7 @@ import cx.fbn.nevernote.utilities.ApplicationLogger;
 public class TagTable {\r
        private final ApplicationLogger logger;\r
        DatabaseConnection db;\r
+       private HashMap<String, QIcon>  icons;\r
 \r
        public TagTable (ApplicationLogger l, DatabaseConnection d) {\r
                logger = l;\r
@@ -335,4 +343,69 @@ public class TagTable {
                if (!query.exec())\r
                        logger.log(logger.EXTREME, "Error resetting tag dirty field.");\r
        }\r
+       \r
+       \r
+       // Get the custom icon\r
+       public QIcon getIcon(String guid) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               \r
+               if (!query.prepare("Select icon from tag where guid=:guid"))\r
+                       logger.log(logger.EXTREME, "Error preparing tag icon select.");\r
+               query.bindValue(":guid", guid);\r
+               if (!query.exec())\r
+                       logger.log(logger.EXTREME, "Error finding tag icon.");\r
+               if (!query.next() || query.getBlob(0) == null)\r
+                       return null;\r
+               \r
+               QByteArray blob = new QByteArray(query.getBlob(0));\r
+               QIcon icon = new QIcon(QPixmap.fromImage(QImage.fromData(blob)));\r
+               return icon;\r
+       }\r
+       // Set the custom icon\r
+       public void setIcon(String guid, QIcon icon, String type) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               if (icon == null) {\r
+                       if (!query.prepare("update tag set icon=null where guid=:guid"))\r
+                               logger.log(logger.EXTREME, "Error preparing tag icon select.");\r
+               } else {\r
+                       if (!query.prepare("update tag set icon=:icon where guid=:guid"))\r
+                               logger.log(logger.EXTREME, "Error preparing tag icon select.");\r
+                       QBuffer buffer = new QBuffer();\r
+               if (!buffer.open(QIODevice.OpenModeFlag.ReadWrite)) {\r
+                       logger.log(logger.EXTREME, "Failure to open buffer.  Aborting.");\r
+                       return;\r
+               }\r
+               QPixmap p = icon.pixmap(32, 32);\r
+               QImage i = p.toImage();\r
+               i.save(buffer, type.toUpperCase());\r
+               buffer.close();\r
+               QByteArray b = new QByteArray(buffer.buffer());\r
+               if (!b.isNull() && !b.isEmpty())\r
+                       query.bindValue(":icon", b.toByteArray());\r
+               else\r
+                       return;\r
+               }\r
+               query.bindValue(":guid", guid);\r
+               if (!query.exec()) \r
+                       logger.log(logger.LOW, "Error setting tag icon. " +query.lastError());\r
+       }\r
+\r
+       // Get a list of all icons\r
+       public HashMap<String, QIcon> getAllIcons() {\r
+               HashMap<String, QIcon> values = new HashMap<String, QIcon>();\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+       \r
+               if (!query.exec("SELECT guid, icon from tag"))\r
+                       logger.log(logger.EXTREME, "Error executing notebook getAllIcons select.");\r
+               while (query.next()) {\r
+                       if (query.getBlob(1) != null) {\r
+                               String guid = query.valueString(0);\r
+                               QByteArray blob = new QByteArray(query.getBlob(1));\r
+                               QIcon icon = new QIcon(QPixmap.fromImage(QImage.fromData(blob)));\r
+                               values.put(guid, icon);\r
+                       }\r
+               }\r
+               return values;\r
+       }\r
+\r
 }\r