OSDN Git Service

Allow customizing of notebook icons.
authorRandy Baumgarte <randy@fbn.cx>
Fri, 5 Nov 2010 18:52:41 +0000 (14:52 -0400)
committerRandy Baumgarte <randy@fbn.cx>
Wed, 15 Dec 2010 15:15:31 +0000 (10:15 -0500)
src/cx/fbn/nevernote/NeverNote.java
src/cx/fbn/nevernote/dialog/SetIcon.java [new file with mode: 0644]
src/cx/fbn/nevernote/gui/MainMenuBar.java
src/cx/fbn/nevernote/gui/NotebookTreeWidget.java
src/cx/fbn/nevernote/sql/NotebookTable.java

index edd2766..952de30 100644 (file)
@@ -139,6 +139,7 @@ import cx.fbn.nevernote.dialog.NotebookArchive;
 import cx.fbn.nevernote.dialog.NotebookEdit;
 import cx.fbn.nevernote.dialog.OnlineNoteHistory;
 import cx.fbn.nevernote.dialog.SavedSearchEdit;
+import cx.fbn.nevernote.dialog.SetIcon;
 import cx.fbn.nevernote.dialog.TagEdit;
 import cx.fbn.nevernote.dialog.ThumbnailViewer;
 import cx.fbn.nevernote.dialog.WatchFolder;
@@ -512,6 +513,7 @@ public class NeverNote extends QMainWindow{
                notebookTree.setDeleteAction(menuBar.notebookDeleteAction);
                notebookTree.setEditAction(menuBar.notebookEditAction);
                notebookTree.setAddAction(menuBar.notebookAddAction);
+               notebookTree.setIconAction(menuBar.notebookIconAction);
                notebookTree.setVisible(Global.isWindowVisible("notebookTree"));
                notebookTree.noteSignal.notebookChanged.connect(this, "updateNoteNotebook(String, String)");
                menuBar.hideNotebooks.setChecked(Global.isWindowVisible("notebookTree"));
@@ -1158,6 +1160,7 @@ public class NeverNote extends QMainWindow{
                menuBar.noteRestoreAction.setVisible(false);            
        menuBar.notebookEditAction.setEnabled(true);
        menuBar.notebookDeleteAction.setEnabled(true);
+       menuBar.notebookIconAction.setEnabled(true);
        List<QTreeWidgetItem> selections = notebookTree.selectedItems();
        QTreeWidgetItem currentSelection;
        selectedNotebookGUIDs.clear();
@@ -1176,6 +1179,7 @@ public class NeverNote extends QMainWindow{
                        selectedNotebookGUIDs.clear();
                        menuBar.notebookEditAction.setEnabled(false);
                        menuBar.notebookDeleteAction.setEnabled(false);
+                       menuBar.notebookIconAction.setEnabled(false);
                }
                if (selectedNotebookGUIDs.size() == 1 && selectedNotebookGUIDs.get(0).equals(previousSelectedNotebook)) {
                        previousSelectedNotebook = selectedNotebookGUIDs.get(0);
@@ -1192,8 +1196,13 @@ public class NeverNote extends QMainWindow{
                String guid = "";
                if (selections.size() > 0)
                        guid = (selections.get(0).text(2));
-               if (!guid.equals(""))
+               if (!guid.equals("")) {
                        selectedNotebookGUIDs.add(guid);
+                       menuBar.notebookIconAction.setEnabled(true);
+               }
+               else {
+                       menuBar.notebookIconAction.setEnabled(false);
+               }
        }
        listManager.setSelectedNotebooks(selectedNotebookGUIDs);
        listManager.loadNotesIndex();
@@ -1214,7 +1223,12 @@ public class NeverNote extends QMainWindow{
        // Triggered when the notebook DB has been updated
        private void notebookIndexUpdated() {
                logger.log(logger.HIGH, "Entering NeverNote.notebookIndexUpdated");
-               if (selectedNotebookGUIDs == null)
+       
+               // Get the possible icons
+               HashMap<String, QIcon> icons = conn.getNotebookTable().getAllIcons();
+       notebookTree.setIcons(icons);
+       
+       if (selectedNotebookGUIDs == null)
                        selectedNotebookGUIDs = new ArrayList<String>();
                List<Notebook> books = conn.getNotebookTable().getAll();
                for (int i=books.size()-1; i>=0; i--) {
@@ -1483,9 +1497,47 @@ public class NeverNote extends QMainWindow{
                waitCursor(false);
                browserWindow.setNotebookList(nbooks);
        }
+       // Change the notebook's icon
+       private void setNotebookIcon() {
+               QTreeWidgetItem currentSelection;
+               List<QTreeWidgetItem> selections = notebookTree.selectedItems();
+               if (selections.size() == 0)
+                       return;
+               
+               currentSelection = selections.get(0);   
+               String guid = currentSelection.text(2);
+               if (guid.equalsIgnoreCase(""))
+                       return;
 
+               QIcon currentIcon = currentSelection.icon(0);
+               QIcon icon = conn.getNotebookTable().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.getNotebookTable().setIcon(guid, newIcon, dialog.getFileType());
+                       if (newIcon == null) {
+                               boolean isPublished = false;;
+                               boolean found = false;
+                               for (int i=0; i<listManager.getNotebookIndex().size() && !found; i++) {
+                                       if (listManager.getNotebookIndex().get(i).getGuid().equals(guid)) {
+                                               isPublished = listManager.getNotebookIndex().get(i).isPublished();
+                                               found = true;
+                                       }
+                               }
+                               newIcon = notebookTree.findDefaultIcon(guid, currentSelection.text(1), listManager.getLocalNotebooks(), isPublished);
+                       }
+                       currentSelection.setIcon(0, newIcon);
+               }
        
-       
+       }
        
        
     //***************************************************************
diff --git a/src/cx/fbn/nevernote/dialog/SetIcon.java b/src/cx/fbn/nevernote/dialog/SetIcon.java
new file mode 100644 (file)
index 0000000..daf30bd
--- /dev/null
@@ -0,0 +1,137 @@
+/*
+ * This file is part of NeverNote 
+ * Copyright 2009 Randy Baumgarte
+ * 
+ * This file may be licensed under the terms of of the
+ * GNU General Public License Version 2 (the ``GPL'').
+ *
+ * Software distributed under the License is distributed
+ * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
+ * express or implied. See the GPL for the specific language
+ * governing rights and limitations.
+ *
+ * You should have received a copy of the GPL along with this
+ * program. If not, go to http://www.gnu.org/licenses/gpl.html
+ * or write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+*/
+
+package cx.fbn.nevernote.dialog;
+
+import com.trolltech.qt.gui.QCheckBox;
+import com.trolltech.qt.gui.QDialog;
+import com.trolltech.qt.gui.QFileDialog;
+import com.trolltech.qt.gui.QFileDialog.AcceptMode;
+import com.trolltech.qt.gui.QFileDialog.FileMode;
+import com.trolltech.qt.gui.QGridLayout;
+import com.trolltech.qt.gui.QIcon;
+import com.trolltech.qt.gui.QPushButton;
+import com.trolltech.qt.gui.QSizePolicy.Policy;
+
+public class SetIcon extends QDialog {
+       private boolean okPressed;
+       QPushButton     ok;
+       QPushButton             iconButton;
+       QCheckBox               useDefault;
+       QIcon                   defaultIcon;
+       boolean                 startUseDefault;
+       private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
+       
+       // Constructor
+       public SetIcon(QIcon i) {
+               okPressed = false;
+               setWindowTitle(tr("Set Icon"));
+               QGridLayout grid = new QGridLayout();
+               setWindowIcon(new QIcon(iconPath+"nevernote.png"));
+               setLayout(grid);
+               
+               QGridLayout textGrid = new QGridLayout();
+               textGrid.setContentsMargins(10, 10,-10, -10);
+               useDefault = new QCheckBox();
+               iconButton = new QPushButton();
+               iconButton.setSizePolicy(Policy.Fixed, Policy.Fixed);
+               iconButton.clicked.connect(this, "iconButtonPressed()");
+               useDefault.setText(tr("Use Default"));
+               iconButton.setIcon(i);
+               
+               textGrid.addWidget(iconButton,1,1);
+               textGrid.addWidget(useDefault,2,1);
+               useDefault.clicked.connect(this, "useDefaultIconChecked(Boolean)");
+               grid.addLayout(textGrid,1,1);
+               
+               QGridLayout buttonGrid = new QGridLayout();
+               ok = new QPushButton(tr("OK"));
+               ok.clicked.connect(this, "okButtonPressed()");
+               ok.setEnabled(false);
+               QPushButton cancel = new QPushButton(tr("Cancel"));
+               cancel.clicked.connect(this, "cancelButtonPressed()");
+               buttonGrid.addWidget(ok, 3, 1);
+               buttonGrid.addWidget(cancel, 3,2);
+               grid.addLayout(buttonGrid,2,1);
+       }
+       
+       // The OK button was pressed
+       @SuppressWarnings("unused")
+       private void okButtonPressed() {
+               okPressed = true;
+               close();
+       }
+       
+       // The CANCEL button was pressed
+       @SuppressWarnings("unused")
+       private void cancelButtonPressed() {
+               okPressed = false;
+               close();
+       }
+       
+       // Check if the OK button was pressed
+       public boolean okPressed() {
+               return okPressed;
+       }
+       
+       // Icon Button pressed
+       private void iconButtonPressed() {
+               QFileDialog fd = new QFileDialog(this);
+               fd.setFileMode(FileMode.ExistingFile);
+               fd.setConfirmOverwrite(true);
+               fd.setWindowTitle(tr("Icon"));
+               fd.setFilter(tr("PNG (*.png);;All Files (*.*)"));
+               fd.setAcceptMode(AcceptMode.AcceptOpen);
+               fd.setDirectory(System.getProperty("user.home"));
+               if (fd.exec() == 0 || fd.selectedFiles().size() == 0) {
+                       return;
+               }
+               
+               ok.setEnabled(true);
+               String path = fd.selectedFiles().get(0);
+               QIcon icon = new QIcon(path);
+               iconButton.setIcon(new QIcon(path));
+               iconButton.setSizePolicy(Policy.Fixed, Policy.Fixed);
+       }
+       
+       public void setUseDefaultIcon(boolean val) {
+               useDefault.setChecked(val);
+               iconButton.setEnabled(!val);
+               startUseDefault = val;
+       }
+       
+       public QIcon getIcon() {
+               if (useDefault.isChecked())
+                       return null;
+               return iconButton.icon();
+       }
+       
+       public void useDefaultIconChecked(Boolean value) {
+               iconButton.setEnabled(!value);
+               
+               if (value != startUseDefault) 
+                       ok.setEnabled(true);
+               else
+                       ok.setEnabled(false);
+       }
+       
+       public String getFileType() {
+               return "PNG";
+       }
+}
index 00cb2bf..08605e4 100644 (file)
@@ -102,6 +102,7 @@ public class MainMenuBar extends QMenuBar {
        public QAction                  notebookAddAction;                      // Add a new notebook\r
        public QAction                  notebookDeleteAction;           // Delete a notebook\r
        public QAction                  notebookCloseAction;            // Close notebooks\r
+       public QAction                  notebookIconAction;                     // Change the icon\r
        \r
        public QAction                  savedSearchAddAction;           // Add a saved search\r
        public QAction                  savedSearchEditAction;          // Edit a saved search\r
@@ -447,13 +448,14 @@ public class MainMenuBar extends QMenuBar {
                setupShortcut(notebookDeleteAction, "File_Notebook_Delete");\r
                \r
                notebookCloseAction = new QAction(tr("Open/Close Notebooks"), this);\r
-//             if (!Global.mimicEvernoteInterface) {\r
-                       notebookCloseAction.setEnabled(true);\r
-                       notebookCloseAction.triggered.connect(parent, "closeNotebooks()");\r
-                       setupShortcut(notebookCloseAction, "File_Notebook_Close");\r
-//             } else {\r
-//                     notebookCloseAction.setEnabled(false); \r
-//             }\r
+               notebookCloseAction.setEnabled(true);\r
+               notebookCloseAction.triggered.connect(parent, "closeNotebooks()");\r
+               setupShortcut(notebookCloseAction, "File_Notebook_Close");\r
+\r
+               notebookIconAction = new QAction(tr("Change Icon"), this);\r
+               notebookIconAction.setEnabled(false);\r
+               notebookIconAction.triggered.connect(parent, "setNotebookIcon()");\r
+               setupShortcut(notebookIconAction, "File_Notebook_Icon");\r
                \r
                tagAddAction = new QAction(tr("Add"),this);\r
                tagAddAction.triggered.connect(parent, "addTag()");\r
@@ -654,10 +656,10 @@ public class MainMenuBar extends QMenuBar {
                notebookMenu.addAction(notebookAddAction);\r
                notebookMenu.addAction(notebookEditAction);\r
                notebookMenu.addAction(notebookDeleteAction);\r
-//             if (!Global.mimicEvernoteInterface) {\r
-                       notebookMenu.addSeparator();\r
-                       notebookMenu.addAction(notebookCloseAction);\r
-//             }\r
+               notebookMenu.addSeparator();\r
+               notebookMenu.addAction(notebookCloseAction);\r
+               notebookMenu.addSeparator();\r
+               notebookMenu.addAction(notebookIconAction);\r
                \r
                tagMenu.addAction(tagAddAction);\r
                tagMenu.addAction(tagEditAction);\r
index 896b1a8..c0a172f 100644 (file)
@@ -20,6 +20,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.Notebook;\r
@@ -48,7 +49,9 @@ public class NotebookTreeWidget extends QTreeWidget {
        private QAction                                 deleteAction;\r
        private QAction                                 addAction;\r
        private QAction                                 editAction;\r
+       private QAction                                 iconAction;\r
        public NoteSignal                               noteSignal;\r
+       private HashMap<String, QIcon>  icons;\r
 //     private final QTreeWidgetItem                   previousMouseOver;\r
 //     private boolean                                 previousMouseOverWasSelected;\r
        \r
@@ -64,6 +67,10 @@ public class NotebookTreeWidget extends QTreeWidget {
                editAction = e;\r
        }\r
        \r
+       public void setIconAction(QAction e) {\r
+               iconAction = e;\r
+       }\r
+       \r
        public NotebookTreeWidget() {\r
                noteSignal = new NoteSignal();\r
                setProperty("hideTree", true);\r
@@ -118,33 +125,49 @@ public class NotebookTreeWidget extends QTreeWidget {
                return false;\r
        }\r
        \r
+       public void setIcons(HashMap<String, QIcon> i) {\r
+               icons = i;\r
+       }\r
        \r
-       public void load(List<Notebook> books, List<String> localBooks) {\r
-       Notebook book;\r
-       QTreeWidgetItem child;\r
-       clear();\r
+       public QIcon findDefaultIcon(String guid, String name, List<String> localBooks, boolean isPublished) {\r
        String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
        QIcon blueIcon = new QIcon(iconPath+"notebook-blue.png");\r
        QIcon greenIcon = new QIcon(iconPath+"notebook-green.png");\r
        QIcon redIcon = new QIcon(iconPath+"notebook-red.png");\r
        QIcon yellowIcon = new QIcon(iconPath+"notebook-yellow.png");\r
+\r
+               if (localBooks.contains(guid)) {\r
+                       return yellowIcon;\r
+               }\r
+               \r
+               if (localBooks.contains(guid) && \r
+                       (name.equalsIgnoreCase("Conflicting Changes") ||\r
+                        name.equalsIgnoreCase("Conflicting Changes (Local)")))\r
+                               return redIcon;\r
+               if (isPublished)\r
+                       return blueIcon;\r
+\r
+               return greenIcon;\r
+       }\r
+       \r
+       public void load(List<Notebook> books, List<String> localBooks) {\r
+       Notebook book;\r
+       QTreeWidgetItem child;\r
+       clear();\r
        \r
        if (books == null)\r
                return;\r
        Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
        for (int i=0; i<books.size(); i++) {\r
-               book = books.get(i);\r
-               child = new QTreeWidgetItem();\r
-               child.setText(0, book.getName());\r
-               child.setIcon(0, greenIcon);\r
-               if (localBooks.contains(book.getGuid()))\r
-                       child.setIcon(0, yellowIcon);\r
-               if (localBooks.contains(book.getGuid()) && \r
-                               (book.getName().equalsIgnoreCase("Conflicting Changes") ||\r
-                                book.getName().equalsIgnoreCase("Conflicting Changes (Local)")))\r
-                                       child.setIcon(0, redIcon);\r
-               if (book.isPublished())\r
-                       child.setIcon(0, blueIcon);\r
+                       book = books.get(i);\r
+                       child = new QTreeWidgetItem();\r
+                       child.setText(0, book.getName());\r
+               if (icons != null && !icons.containsKey(book.getGuid())) {\r
+                       QIcon icon = findDefaultIcon(book.getGuid(), book.getName(), localBooks, book.isPublished());\r
+                       child.setIcon(0, icon);\r
+               } else {\r
+                       child.setIcon(0, icons.get(book.getGuid()));\r
+               }\r
                child.setTextAlignment(1, ra.value());\r
                child.setText(2, book.getGuid());\r
                addTopLevelItem(child);\r
@@ -152,6 +175,9 @@ public class NotebookTreeWidget extends QTreeWidget {
 \r
        sortItems(0, SortOrder.AscendingOrder); \r
        if (Global.mimicEvernoteInterface) {\r
+               String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
+               QIcon greenIcon = new QIcon(iconPath+"notebook-green.png");\r
+               \r
                child = new QTreeWidgetItem();\r
                child.setIcon(0, greenIcon);\r
                child.setText(0, "All Notebooks");\r
@@ -236,6 +262,8 @@ public class NotebookTreeWidget 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
index fafd2eb..7cd6e0e 100644 (file)
@@ -24,9 +24,16 @@ import java.text.DateFormat;
 import java.text.ParseException;\r
 import java.text.SimpleDateFormat;\r
 import java.util.ArrayList;\r
+import java.util.HashMap;\r
 import java.util.List;\r
 \r
 import com.evernote.edam.type.Notebook;\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
@@ -403,7 +410,68 @@ public class NotebookTable {
                        logger.log(logger.EXTREME, "Error setting default notebook.");\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 notebook where ARCHIVED  != true"))\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
+       // Get the notebooks custom icon\r
+       public QIcon getIcon(String guid) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               \r
+               if (!query.prepare("Select icon from notebook where guid=:guid"))\r
+                       logger.log(logger.EXTREME, "Error preparing notebook icon select.");\r
+               query.bindValue(":guid", guid);\r
+               if (!query.exec())\r
+                       logger.log(logger.EXTREME, "Error finding notebook 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 notebooks 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 notebook set icon=null where guid=:guid"))\r
+                               logger.log(logger.EXTREME, "Error preparing notebook icon select.");\r
+               } else {\r
+                       if (!query.prepare("update notebook set icon=:icon where guid=:guid"))\r
+                               logger.log(logger.EXTREME, "Error preparing notebook 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 notebook icon. " +query.lastError());\r
+       }\r
 \r
        // does a record exist?\r
        public String findNotebookByName(String newname) {\r