OSDN Git Service

Add the ability to assign custom icons to the "All Notebooks" & stack icons.
authorRandy Baumgarte <randy@fbn.cx>
Fri, 12 Nov 2010 13:53:26 +0000 (08:53 -0500)
committerRandy Baumgarte <randy@fbn.cx>
Wed, 15 Dec 2010 15:15:55 +0000 (10:15 -0500)
src/cx/fbn/nevernote/NeverNote.java
src/cx/fbn/nevernote/gui/NotebookTreeWidget.java
src/cx/fbn/nevernote/sql/DatabaseConnection.java
src/cx/fbn/nevernote/sql/SharedNotebookTable.java
src/cx/fbn/nevernote/sql/SystemIconTable.java [new file with mode: 0644]

index 5e6f845..ce346b5 100644 (file)
@@ -92,6 +92,7 @@ import com.trolltech.qt.gui.QCloseEvent;
 import com.trolltech.qt.gui.QColor;
 import com.trolltech.qt.gui.QComboBox;
 import com.trolltech.qt.gui.QComboBox.InsertPolicy;
+import com.trolltech.qt.gui.QCursor;
 import com.trolltech.qt.gui.QDesktopServices;
 import com.trolltech.qt.gui.QDialog;
 import com.trolltech.qt.gui.QFileDialog;
@@ -953,10 +954,14 @@ public class NeverNote extends QMainWindow{
        }
                
        private void waitCursor(boolean wait) {
-//             if (wait)
-//                     QApplication.setOverrideCursor(new QCursor(Qt.CursorShape.WaitCursor));
-//             else
-//                     QApplication.restoreOverrideCursor();
+               if (wait) {
+                       if (QApplication.overrideCursor() == null)
+                               QApplication.setOverrideCursor(new QCursor(Qt.CursorShape.WaitCursor));
+               }
+               else {
+                       while (QApplication.overrideCursor() != null)
+                               QApplication.restoreOverrideCursor();
+               }
        }
        
        private void setupIndexListeners() {
@@ -1217,7 +1222,7 @@ public class NeverNote extends QMainWindow{
                        menuBar.notebookIconAction.setEnabled(true);
                }
                else {
-                       menuBar.notebookIconAction.setEnabled(false);
+                       menuBar.notebookIconAction.setEnabled(true);
                                for (int j=0; j<listManager.getNotebookIndex().size(); j++) {
                                        Notebook book = listManager.getNotebookIndex().get(j);
                                        if (book.getStack() != null && book.getStack().equalsIgnoreCase(stackName))
@@ -1600,6 +1605,9 @@ public class NeverNote extends QMainWindow{
        }
        // Change the notebook's icon
        private void setNotebookIcon() {
+               boolean stackSelected = false;
+               boolean allNotebookSelected = false;
+               
                QTreeWidgetItem currentSelection;
                List<QTreeWidgetItem> selections = notebookTree.selectedItems();
                if (selections.size() == 0)
@@ -1608,21 +1616,56 @@ public class NeverNote extends QMainWindow{
                currentSelection = selections.get(0);   
                String guid = currentSelection.text(2);
                if (guid.equalsIgnoreCase(""))
-                       return;
+                       allNotebookSelected = true;
+               if (guid.equalsIgnoreCase("STACK"))
+                       stackSelected = true;
 
                QIcon currentIcon = currentSelection.icon(0);
-               QIcon icon = conn.getNotebookTable().getIcon(guid);
+               QIcon icon;
                SetIcon dialog;
-               if (icon == null) {
-                       dialog = new SetIcon(currentIcon);
-                       dialog.setUseDefaultIcon(true);
+               
+               if (!stackSelected && !allNotebookSelected) {
+                       icon = conn.getNotebookTable().getIcon(guid);
+                       if (icon == null) {
+                               dialog = new SetIcon(currentIcon);
+                               dialog.setUseDefaultIcon(true);
+                       } else {
+                               dialog = new SetIcon(icon);
+                               dialog.setUseDefaultIcon(false);
+                       }
                } else {
-                       dialog = new SetIcon(icon);
-                       dialog.setUseDefaultIcon(false);
+                       if (stackSelected) {
+                               icon = conn.getSystemIconTable().getIcon(currentSelection.text(0), "STACK");
+                       } else {
+                               icon = conn.getSystemIconTable().getIcon(currentSelection.text(0), "ALLNOTEBOOK");                              
+                       }
+                       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();
+                       if (stackSelected) {
+                               conn.getSystemIconTable().setIcon(currentSelection.text(0), "STACK", newIcon, dialog.getFileType());
+                               if (newIcon == null) {
+                                       newIcon = new QIcon(iconPath+"books2.png");
+                               }
+                               currentSelection.setIcon(0,newIcon);
+                               return;
+                       }
+                       if (allNotebookSelected) {
+                               conn.getSystemIconTable().setIcon(currentSelection.text(0), "ALLNOTEBOOK", newIcon, dialog.getFileType());
+                               if (newIcon == null) {
+                                       newIcon = new QIcon(iconPath+"notebook-green.png");
+                               }
+                               currentSelection.setIcon(0,newIcon);
+                               return;
+                       }
                        conn.getNotebookTable().setIcon(guid, newIcon, dialog.getFileType());
                        if (newIcon == null) {
                                boolean isPublished = false;;
index 5fcd3a1..80e8263 100644 (file)
@@ -203,10 +203,13 @@ public class NotebookTreeWidget extends QTreeWidget {
        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
+               QIcon allIcon = db.getSystemIconTable().getIcon("All Notebooks", "ALLNOTEBOOK");\r
+               \r
+               if (allIcon == null)\r
+                       allIcon = new QIcon(iconPath+"notebook-green.png");\r
                \r
                child = new QTreeWidgetItem();\r
-               child.setIcon(0, greenIcon);\r
+               child.setIcon(0, allIcon);\r
                child.setText(0, "All Notebooks");\r
                child.setText(2, "");\r
                child.setTextAlignment(1, ra.value());\r
@@ -441,7 +444,11 @@ public class NotebookTreeWidget extends QTreeWidget {
 \r
        private QTreeWidgetItem createStackIcon(String stackName, Qt.Alignment ra) {\r
                String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
-               QIcon stackIcon = new QIcon(iconPath+"books2.png");\r
+               QIcon stackIcon;\r
+               stackIcon = db.getSystemIconTable().getIcon(stackName, "STACK");\r
+               \r
+               if (stackIcon == null)\r
+                       stackIcon = new QIcon(iconPath+"books2.png");\r
                QTreeWidgetItem parent = new QTreeWidgetItem();\r
                stacks.put(stackName, parent);\r
                parent.setText(0, stackName);\r
index 47a6113..4e87e8b 100644 (file)
@@ -43,6 +43,7 @@ public class DatabaseConnection {
        private LinkedNotebookTable                     linkedNotebookTable;
        private SharedNotebookTable                     sharedNotebookTable;
        private SyncTable                                       syncTable;
+       private SystemIconTable                         systemIconTable;
        private final ApplicationLogger         logger;
        private Connection                                      conn;
        int id;
@@ -65,6 +66,7 @@ public class DatabaseConnection {
                syncTable = new SyncTable(logger, this);
                linkedNotebookTable = new LinkedNotebookTable(logger, this);
                sharedNotebookTable = new SharedNotebookTable(logger, this);
+               systemIconTable = new SystemIconTable(logger, this);
        }
        
        
@@ -161,6 +163,7 @@ public class DatabaseConnection {
                        
                        sharedNotebookTable.createTable();
                        linkedNotebookTable.createTable();
+                       systemIconTable.createTable();
                        
                        version = "0.95";
                        executeSql("Insert into Sync (key, value) values ('FullLinkedNotebookSync', 'true')");
@@ -246,4 +249,7 @@ public class DatabaseConnection {
        public SharedNotebookTable getSharedNotebookTable() {
                return sharedNotebookTable;
        }
+       public SystemIconTable getSystemIconTable() {
+               return systemIconTable;
+       }
 }
index e6aab91..e3d8393 100644 (file)
@@ -135,6 +135,8 @@ public class SharedNotebookTable {
                        deletedTable.addDeletedItem(new Long(id).toString(), "SharedNotebook");\r
                }\r
        }\r
+\r
+       \r
        // Update a notebook\r
        public void updateNotebook(SharedNotebook tempNotebook, boolean isDirty) {\r
                boolean check;\r
@@ -207,21 +209,6 @@ public class SharedNotebookTable {
                return index;\r
        }                       \r
 \r
-       // does a record exist?\r
-       public String findNotebookByName(String newname) {\r
-               \r
-               NSqlQuery query = new NSqlQuery(db.getConnection());\r
-               \r
-               query.prepare("Select guid from sharednotebook where name=:newname");\r
-               query.bindValue(":newname", newname);\r
-               if (!query.exec())\r
-                       logger.log(logger.EXTREME, "notebook SQL retrieve has failed.");\r
-               String val = null;\r
-               if (query.next())\r
-                       val = query.valueString(0);\r
-               return val;\r
-       }\r
-\r
 \r
 }\r
 \r
diff --git a/src/cx/fbn/nevernote/sql/SystemIconTable.java b/src/cx/fbn/nevernote/sql/SystemIconTable.java
new file mode 100644 (file)
index 0000000..e9ffc0d
--- /dev/null
@@ -0,0 +1,167 @@
+/*\r
+ * This file is part of NeverNote \r
+ * Copyright 2009 Randy Baumgarte\r
+ * \r
+ * This file may be licensed under the terms of of the\r
+ * GNU General Public License Version 2 (the ``GPL'').\r
+ *\r
+ * Software distributed under the License is distributed\r
+ * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
+ * express or implied. See the GPL for the specific language\r
+ * governing rights and limitations.\r
+ *\r
+ * You should have received a copy of the GPL along with this\r
+ * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
+ * or write to the Free Software Foundation, Inc.,\r
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
+ *\r
+*/\r
+\r
+\r
+package cx.fbn.nevernote.sql;\r
+\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
+\r
+public class SystemIconTable {\r
+       \r
+       private final ApplicationLogger                 logger;\r
+       DatabaseConnection                                                      db;\r
+       \r
+       // Constructor\r
+       public SystemIconTable(ApplicationLogger l, DatabaseConnection d) {\r
+               logger = l;\r
+               db = d;\r
+       }\r
+       // Create the table\r
+       public void createTable() {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+        logger.log(logger.HIGH, "Creating table SystemIcon...");\r
+        if (!query.exec("Create table SystemIcon (name varchar, " +\r
+                       "type varchar, " +\r
+                       "primary key(name, type), " +\r
+                       "icon blob)"))                          \r
+               logger.log(logger.HIGH, "Table SystemIcon creation FAILED!!!");   \r
+       }\r
+       // Drop the table\r
+       public void dropTable() {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               query.exec("Drop table SystemIcon");\r
+       }\r
+\r
+       \r
+       // Get the notebooks custom icon\r
+       public QIcon getIcon(String name, String type) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               \r
+               if (!query.prepare("Select icon from SystemIcon where name=:name and type=:type"))\r
+                       logger.log(logger.EXTREME, "Error preparing system icon select.");\r
+               query.bindValue(":name", name);\r
+               query.bindValue(":type", type);\r
+               if (!query.exec())\r
+                       logger.log(logger.EXTREME, "Error finding system 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
+       \r
+       \r
+       // Get the notebooks custom icon\r
+       public boolean exists(String name, String type) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               \r
+               if (!query.prepare("Select icon from SystemIcon where name=:name and type=:type"))\r
+                       logger.log(logger.EXTREME, "Error preparing system icon select.");\r
+               query.bindValue(":name", name);\r
+               query.bindValue(":type", type);\r
+               if (!query.exec())\r
+                       logger.log(logger.EXTREME, "Error finding system icon.");\r
+               if (!query.next() || query.getBlob(0) == null)\r
+                       return false;\r
+               return true;\r
+       }\r
+       \r
+       \r
+       // Set the notebooks custom icon\r
+       public void setIcon(String name, String rectype, QIcon icon, String filetype) {\r
+               if (exists(name, rectype))\r
+                       updateIcon(name, rectype, icon, filetype);\r
+               else\r
+                       addIcon(name, rectype, icon, filetype);\r
+       }\r
+       \r
+       \r
+       \r
+       // Set the notebooks custom icon\r
+       public void addIcon(String name, String rectype, QIcon icon, String filetype) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               if (icon == null) {\r
+                       return;\r
+               } else {\r
+                       if (!query.prepare("Insert into SystemIcon (icon, name, type) values (:icon, :name, :type)"))\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, filetype.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(":name", name);\r
+               query.bindValue(":type", rectype);\r
+               if (!query.exec()) \r
+                       logger.log(logger.LOW, "Error setting system icon. " +query.lastError());\r
+       }\r
+       \r
+       \r
+       \r
+       // Set the notebooks custom icon\r
+       public void updateIcon(String name, String rectype, QIcon icon, String filetype) {\r
+               NSqlQuery query = new NSqlQuery(db.getConnection());\r
+               if (icon == null) {\r
+                       if (!query.prepare("delete from SystemIcon where name=:name and type=:type"))\r
+                               logger.log(logger.EXTREME, "Error preparing notebook icon select.");\r
+               } else {\r
+                       if (!query.prepare("update SystemIcon set icon=:icon where name=:name and type=:type"))\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, filetype.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(":name", name);\r
+               query.bindValue(":type", rectype);\r
+               if (!query.exec()) \r
+                       logger.log(logger.LOW, "Error setting system icon. " +query.lastError());\r
+       }\r
+\r
+}\r
+\r