OSDN Git Service

Cleanup the spell checker dialog.
authorRandy Baumgarte <randy@fbn.cx>
Wed, 6 Oct 2010 14:37:00 +0000 (10:37 -0400)
committerRandy Baumgarte <randy@fbn.cx>
Sun, 10 Oct 2010 14:03:46 +0000 (10:03 -0400)
src/cx/fbn/nevernote/NeverNote.java
src/cx/fbn/nevernote/config/FileManager.java
src/cx/fbn/nevernote/dialog/SpellCheck.java
src/cx/fbn/nevernote/gui/BrowserWindow.java
src/cx/fbn/nevernote/gui/EditorButtonBar.java
src/cx/fbn/nevernote/gui/MainMenuBar.java
src/cx/fbn/nevernote/icons/spellCheck.gif [new file with mode: 0644]

index 57d894c..f1f4b86 100644 (file)
@@ -3533,6 +3533,7 @@ public class NeverNote extends QMainWindow{
                browserWindow.leftAlignAction.setVisible(Global.isEditorButtonVisible("alignLeft"));
                browserWindow.centerAlignAction.setVisible(Global.isEditorButtonVisible("alignCenter"));
                browserWindow.rightAlignAction.setVisible(Global.isEditorButtonVisible("alignRight"));
+               browserWindow.spellCheckAction.setVisible(Global.isEditorButtonVisible("spellCheck"));
     }
     private void duplicateNote(String guid) {
                
index 73c1054..ec3bee3 100644 (file)
@@ -30,6 +30,9 @@ public class FileManager {
     private final String spellDirPath;
     private final File spellDir;
     
+    private final String spellDirPathUser;
+    private final File spellDirUser;
+    
     private final String qssDirPath;
     private final File qssDir;
 
@@ -74,6 +77,11 @@ public class FileManager {
         checkExistingReadableDir(spellDir);
         spellDirPath = slashTerminatePath(spellDir.getPath());
         
+
+        spellDirUser = new File(homeDir, "spell");
+        createDirOrCheckWriteable(spellDirUser);
+        spellDirPathUser = slashTerminatePath(spellDirUser.getPath());
+        
         xmlDir = new File(programDir, "xml");
         checkExistingReadableDir(xmlDir);
 
@@ -132,7 +140,7 @@ public class FileManager {
     }
 
     /**
-     * Get a path below the 'db' directory, using native {@link File#separator}.
+     * Get a path below the 'spell' directory, using native {@link File#separator}.
      * This will contain backslashes on Windows.
      */
     public String getSpellDirPath(String relativePath) {
@@ -143,7 +151,7 @@ public class FileManager {
      * Get a file below the 'spell' directory.
      */
     public File getSpellDirFile(String relativePath) {
-        return new File(dbDir, toPlatformPathSeparator(relativePath));
+        return new File(spellDir, toPlatformPathSeparator(relativePath));
     }
     
     /** 
@@ -154,9 +162,23 @@ public class FileManager {
     }
 
     /**
-     * Get a path below the 'spell' directory, using native {@link File#separator}.
-     * This will contain backslashes on Windows.
+     * Get a file below the 'spell' directory for user dictionaries.
      */
+    public File getSpellDirFileUser(String relativePath) {
+        return new File(spellDirUser, toPlatformPathSeparator(relativePath));
+    }
+    
+    /** 
+     * Get the spell directory for the jazzy word list (user dictionary).
+     */
+    public String getSpellDirPathUser() {
+       return spellDirPathUser;
+    }
+    
+    /**
+     * Get a path below the 'db' directory, using native {@link File#separator}.
+     * This will contain backslashes on Windows.
+     */    
     public String getDbDirPath(String relativePath) {
         return dbDirPath + toPlatformPathSeparator(relativePath);
     }
index c835290..e400bba 100644 (file)
 \r
 package cx.fbn.nevernote.dialog;\r
 \r
+import java.util.List;\r
+\r
+import com.swabunga.spell.engine.Word;\r
+import com.swabunga.spell.event.SpellChecker;\r
+import com.trolltech.qt.core.Qt.AlignmentFlag;\r
 import com.trolltech.qt.gui.QDialog;\r
 import com.trolltech.qt.gui.QGridLayout;\r
 import com.trolltech.qt.gui.QLabel;\r
@@ -35,13 +40,17 @@ public class SpellCheck extends QDialog {
        private String misspelledWord;\r
        private final QPushButton replace;\r
        private final QPushButton ignore;\r
+       private final QPushButton ignoreAll;\r
+       private final QPushButton addToDictionary;\r
        private final QListWidget suggestions;\r
+       private final SpellChecker checker;\r
        \r
        \r
        // Constructor\r
-       public SpellCheck() {\r
+       public SpellCheck(SpellChecker checker) {\r
                replacePressed = false;\r
                cancelPressed = false;\r
+               this.checker = checker;\r
                setWindowTitle(tr("Spell Check"));\r
                QGridLayout grid = new QGridLayout();\r
                setLayout(grid);\r
@@ -64,12 +73,19 @@ public class SpellCheck extends QDialog {
                \r
                replace = new QPushButton(tr("Replace"));\r
                ignore = new QPushButton(tr("Ignore"));\r
+               ignoreAll = new QPushButton(tr("Ignore All"));\r
+               addToDictionary = new QPushButton(tr("Add To Dictionary"));\r
                replace.clicked.connect(this, "replaceButtonPressed()");\r
                ignore.clicked.connect(this, "ignoreButtonPressed()");\r
+               ignoreAll.clicked.connect(this, "ignoreAllButtonPressed()");\r
+               addToDictionary.clicked.connect(this, "addToDictionaryButtonPressed()");\r
                QPushButton cancel = new QPushButton(tr("Cancel"));\r
                cancel.clicked.connect(this, "cancelButtonPressed()");\r
                suggestionGrid.addWidget(replace, 1, 2);\r
                suggestionGrid.addWidget(ignore, 2, 2);\r
+               suggestionGrid.addWidget(ignoreAll,3,2);\r
+               suggestionGrid.addWidget(addToDictionary,4,2);\r
+               suggestionGrid.setAlignment(addToDictionary, AlignmentFlag.AlignTop);\r
                buttonGrid.addWidget(new QLabel(), 1,1);\r
                buttonGrid.addWidget(cancel, 1,2);\r
                buttonGrid.addWidget(new QLabel(), 1,3);\r
@@ -102,6 +118,13 @@ public class SpellCheck extends QDialog {
                close();\r
        }\r
        \r
+       // The ignore button was pressed\r
+       @SuppressWarnings("unused")\r
+       private void ignoreAllButtonPressed() {\r
+               checker.ignoreAll(misspelledWord);\r
+               close();\r
+       }\r
+       \r
        // Get the userid from the field\r
        public String getReplacementWord() {\r
                return replacementWord.text();\r
@@ -126,10 +149,16 @@ public class SpellCheck extends QDialog {
        // Validate user input\r
        public void validateInput() {\r
                replace.setEnabled(true);\r
+               suggestions.clear();\r
                if (replacementWord.text().trim().equals("")) {\r
                        replace.setEnabled(false);\r
                        return;\r
                }\r
+               \r
+               List<Word> values = checker.getSuggestions(replacementWord.text(), 10);\r
+               for (int i=0; i<values.size(); i++) {\r
+                       suggestions.addItem(values.get(i).toString());\r
+               }\r
        }\r
        \r
        private void replacementChosen() {\r
@@ -137,6 +166,11 @@ public class SpellCheck extends QDialog {
                replacementWord.setText(sel);\r
        }\r
        \r
+       //Add an item to the dictionary\r
+       private void addToDictionaryButtonPressed() {\r
+               checker.addToDictionary(misspelledWord);\r
+               this.close();\r
+       }\r
        \r
        // Add a suggestion\r
        public void addSuggestion(String word) {\r
index 68e881e..c49febb 100644 (file)
@@ -57,11 +57,15 @@ import com.trolltech.qt.core.QByteArray;
 import com.trolltech.qt.core.QDataStream;\r
 import com.trolltech.qt.core.QDateTime;\r
 import com.trolltech.qt.core.QEvent;\r
+import com.trolltech.qt.core.QEvent.Type;\r
 import com.trolltech.qt.core.QFile;\r
 import com.trolltech.qt.core.QFileSystemWatcher;\r
 import com.trolltech.qt.core.QIODevice;\r
 import com.trolltech.qt.core.QMimeData;\r
 import com.trolltech.qt.core.QUrl;\r
+import com.trolltech.qt.core.Qt.Key;\r
+import com.trolltech.qt.core.Qt.KeyboardModifier;\r
+import com.trolltech.qt.core.Qt.KeyboardModifiers;\r
 import com.trolltech.qt.gui.QAction;\r
 import com.trolltech.qt.gui.QApplication;\r
 import com.trolltech.qt.gui.QCalendarWidget;\r
@@ -79,6 +83,7 @@ import com.trolltech.qt.gui.QGridLayout;
 import com.trolltech.qt.gui.QHBoxLayout;\r
 import com.trolltech.qt.gui.QIcon;\r
 import com.trolltech.qt.gui.QImage;\r
+import com.trolltech.qt.gui.QKeyEvent;\r
 import com.trolltech.qt.gui.QKeySequence;\r
 import com.trolltech.qt.gui.QLabel;\r
 import com.trolltech.qt.gui.QLineEdit;\r
@@ -192,6 +197,8 @@ public class BrowserWindow extends QWidget {
        public final QAction    bulletListAction;\r
        public final QPushButton numberListButton;\r
        public final QAction    numberListAction;\r
+       public final QPushButton spellCheckButton;\r
+       public final QAction    spellCheckAction;\r
 \r
        public final QShortcut focusTitleShortcut;\r
        public final QShortcut focusTagShortcut;\r
@@ -215,41 +222,48 @@ public class BrowserWindow extends QWidget {
        private String currentHyperlink;\r
        public boolean keepPDFNavigationHidden;\r
        private final ApplicationLogger logger;\r
-       \r
+       SpellDictionary dictionary;\r
+    SpellDictionary userDictionary;\r
+    SpellChecker spellChecker;\r
+    SuggestionListener spellListener;\r
        private final HashMap<String,Integer> previewPageList; \r
        \r
        \r
        public static class SuggestionListener implements SpellCheckListener {\r
                public boolean abortSpellCheck = false;\r
                public boolean errorsFound = false;\r
+               private final SpellCheck                spellCheckDialog;\r
+               private final SpellChecker checker;\r
+               \r
                \r
                private final BrowserWindow parent;\r
-               public SuggestionListener(BrowserWindow parent) {\r
+               public SuggestionListener(BrowserWindow parent, SpellChecker checker) {\r
                        this.parent = parent;\r
+                       spellCheckDialog = new SpellCheck(checker);\r
+                       this.checker = checker;\r
                }\r
                public void spellingError(SpellCheckEvent event) {\r
                        errorsFound = true;\r
-                       SpellCheck dialog = new SpellCheck();\r
-                       dialog.setWord(event.getInvalidWord());\r
+                       spellCheckDialog.setWord(event.getInvalidWord());\r
 \r
                    List<Word> suggestions = event.getSuggestions();\r
                    if (suggestions.isEmpty()) {\r
-                      dialog.setNoSuggestions(true);\r
+                      spellCheckDialog.setNoSuggestions(true);\r
                    } else {\r
-                      dialog.setCurrentSuggestion(suggestions.get(0).getWord());\r
+                      spellCheckDialog.setCurrentSuggestion(suggestions.get(0).getWord());\r
                       for (int i=0; i<suggestions.size(); i++) {\r
-                         dialog.addSuggestion(suggestions.get(i).getWord());\r
+                         spellCheckDialog.addSuggestion(suggestions.get(i).getWord());\r
                       }\r
-                      dialog.setSelectedSuggestion(0);\r
+                      spellCheckDialog.setSelectedSuggestion(0);\r
                    }\r
-                   dialog.exec();\r
-                   if (dialog.cancelPressed()) {\r
+                   spellCheckDialog.exec();\r
+                   if (spellCheckDialog.cancelPressed()) {\r
                        abortSpellCheck = true;\r
                        return;\r
                    }\r
-                   if (dialog.replacePressed()) {\r
+                   if (spellCheckDialog.replacePressed()) {\r
                        QClipboard clipboard = QApplication.clipboard();\r
-                       clipboard.setText(dialog.getReplacementWord()); \r
+                       clipboard.setText(spellCheckDialog.getReplacementWord()); \r
                        parent.pasteClicked();\r
                    }\r
                 }\r
@@ -413,10 +427,10 @@ public class BrowserWindow extends QWidget {
                outdentButton = newEditorButton("outdent", tr("Shift Left"));\r
                bulletListButton = newEditorButton("bulletList", tr("Bullet List"));\r
                numberListButton = newEditorButton("numberList", tr("Number List"));\r
+               spellCheckButton = newEditorButton("spellCheck", tr("Spell Check"));\r
 \r
                \r
                buttonLayout = new EditorButtonBar();\r
-//             buttonLayout.setSpacing(0);\r
                v.addWidget(buttonLayout);\r
                \r
                undoAction = buttonLayout.addWidget(undoButton);\r
@@ -504,6 +518,11 @@ public class BrowserWindow extends QWidget {
                fontHilightAction = buttonLayout.addWidget(fontHilight);\r
                fontHilightColorMenu.setDefault(QColor.yellow);\r
                buttonLayout.toggleFontHilight.triggered.connect(this, "toggleFontHilightVisible(Boolean)");\r
+               \r
+               spellCheckAction = buttonLayout.addWidget(spellCheckButton);\r
+               buttonLayout.toggleNumberListVisible.triggered.connect(this, "spellCheckClicked()");\r
+               buttonLayout.toggleSpellCheck.triggered.connect(this, "toggleSpellCheckVisible(Boolean)");\r
+\r
 \r
 //             buttonLayout.addWidget(new QLabel(), 1);\r
                v.addWidget(browser, 1);\r
@@ -2630,59 +2649,37 @@ public class BrowserWindow extends QWidget {
                fontHilightAction.setVisible(toggle);\r
                Global.saveEditorButtonsVisible("fontHilight", toggle);\r
        }\r
+       private void toggleSpellCheckVisible(Boolean toggle) {\r
+               spellCheckAction.setVisible(toggle);\r
+               Global.saveEditorButtonsVisible("spellCheck", toggle);\r
+       }\r
 \r
 \r
-       // Invoke spell checker dialog\r
-       private void doSpellCheck() {\r
-\r
+       private void setupDictionary() {\r
                File wordList = new File(Global.getFileManager().getSpellDirPath()+Locale.getDefault()+".dic");\r
-           SpellDictionary dictionary;\r
                try {\r
                        dictionary = new SpellDictionaryHashMap(wordList);\r
-                       SpellChecker spellChecker = new SpellChecker(dictionary);\r
-                       SuggestionListener spellListener = new SuggestionListener(this);\r
-                       spellChecker.addSpellCheckListener(spellListener);\r
-\r
-                       String content = getBrowser().page().mainFrame().toPlainText();\r
-                       StringWordTokenizer tokenizer = new StringWordTokenizer(content);\r
-                       if (!tokenizer.hasMoreWords())\r
-                               return;\r
-                       String word = tokenizer.nextWord();\r
-                       getBrowser().page().action(WebAction.MoveToStartOfDocument);\r
-                       QWebPage.FindFlags flags = new QWebPage.FindFlags();\r
-                       flags.set(QWebPage.FindFlag.FindBackward);\r
-\r
-                       getBrowser().setFocus();\r
-                       boolean found = getBrowser().page().findText(word);\r
-                       if (!found) {\r
-                               QMessageBox.critical(this, tr("Spell Check Error"), \r
-                                               tr("An error has occurred while launching the spell check.  The most probable" +\r
-                                                               " cause is that the cursor was not at the beginning of the document.\n\n" +\r
-                                                               "Please place the cursor at the beginning & try again"));\r
-                               return;\r
-                       }\r
-                       while (found) {\r
-                               found = getBrowser().page().findText(word);\r
+                       spellChecker = new SpellChecker(dictionary);\r
+                       File userWordList;\r
+                       userWordList = new File(Global.getFileManager().getSpellDirPathUser()+"user.dic");\r
+                       \r
+                       // Get the local user spell dictionary\r
+                       try {\r
+                               userDictionary = new SpellDictionaryHashMap(userWordList);\r
+                       } catch (FileNotFoundException e) {\r
+                               userWordList.createNewFile();\r
+                               userDictionary = new SpellDictionaryHashMap(userWordList);\r
+                       } catch (IOException e) {\r
+                               userWordList.createNewFile();\r
+                               userDictionary = new SpellDictionaryHashMap(userWordList);\r
                        }\r
-               \r
-                       spellChecker.checkSpelling(new StringWordTokenizer(word));\r
-                       getBrowser().setFocus();\r
                        \r
-                       flags = new QWebPage.FindFlags();\r
-                       tokenizer = new StringWordTokenizer(content);\r
+                       spellListener = new SuggestionListener(this, spellChecker);\r
                        \r
-                       while(tokenizer.hasMoreWords()) {\r
-                               word = tokenizer.nextWord();\r
-                               found = getBrowser().page().findText(word);\r
-                               if (found && !spellListener.abortSpellCheck) {\r
-                                       spellChecker.checkSpelling(new StringWordTokenizer(word));\r
-                                       getBrowser().setFocus();\r
-                               }\r
-                       }\r
-                       spellChecker.removeSpellCheckListener(spellListener);\r
-                       if (!spellListener.errorsFound)\r
-                               QMessageBox.information(this, tr("Spell Check Complete"), \r
-                                               tr("No spelling errors found"));\r
+                       // Add the user dictionary\r
+                       spellChecker.addSpellCheckListener(spellListener);\r
+                       spellChecker.setUserDictionary(userDictionary);\r
+\r
                } catch (FileNotFoundException e) {\r
                        QMessageBox.critical(this, tr("Spell Check Error"), \r
                                        tr("Dictionary "+ Global.getFileManager().getSpellDirPath()+Locale.getDefault()+\r
@@ -2693,6 +2690,50 @@ public class BrowserWindow extends QWidget {
                                                ".dic is invalid."));\r
                }\r
 \r
+       }\r
+       \r
+       // Invoke spell checker dialog\r
+       private void spellCheckClicked() {\r
+\r
+               if (spellChecker == null) {\r
+                       setupDictionary();      \r
+               }\r
+\r
+               spellListener.abortSpellCheck = false;\r
+               String content = getBrowser().page().mainFrame().toPlainText();\r
+               StringWordTokenizer tokenizer = new StringWordTokenizer(content);\r
+               if (!tokenizer.hasMoreWords())\r
+                       return;\r
+               getBrowser().page().action(WebAction.MoveToStartOfDocument);\r
+\r
+               getBrowser().setFocus();\r
+               boolean found;\r
+                       \r
+               // Move to the start of page\r
+               KeyboardModifiers ctrl = new KeyboardModifiers(KeyboardModifier.ControlModifier.value());\r
+               QKeyEvent home = new QKeyEvent(Type.KeyPress, Key.Key_Home.value(), ctrl);  \r
+               browser.keyPressEvent(home);\r
+               getBrowser().setFocus();\r
+                       \r
+               tokenizer = new StringWordTokenizer(content);\r
+               String word;\r
+                       \r
+               while(tokenizer.hasMoreWords()) {\r
+                       word = tokenizer.nextWord();\r
+                       found = getBrowser().page().findText(word);\r
+                       if (found && !spellListener.abortSpellCheck) {\r
+                               spellChecker.checkSpelling(new StringWordTokenizer(word));\r
+                               getBrowser().setFocus();\r
+                       }\r
+               }\r
+\r
+               // Go to the end of the document & finish up.\r
+               home = new QKeyEvent(Type.KeyPress, Key.Key_End.value(), ctrl);  \r
+               browser.keyPressEvent(home);\r
+               if (!spellListener.errorsFound)\r
+                       QMessageBox.information(this, tr("Spell Check Complete"), \r
+                                       tr("No spelling errors found"));\r
+\r
     }\r
 \r
 }\r
index 1d701af..c210bc5 100644 (file)
@@ -33,6 +33,7 @@ public class EditorButtonBar extends QToolBar {
        QAction toggleFontSizeVisible;\r
        QAction toggleFontColorVisible;\r
        QAction toggleFontHilight;\r
+       QAction toggleSpellCheck;\r
        \r
 \r
 \r
@@ -59,10 +60,11 @@ public class EditorButtonBar extends QToolBar {
                toggleBulletListVisible = addAction("bulletList", tr("Bullet List"));\r
                toggleNumberListVisible = addAction("numberList", tr("Number List"));\r
 \r
-               toggleFontVisible = addAction("font", "Font");\r
-               toggleFontSizeVisible = addAction("fontSize", "Font Size");\r
-               toggleFontColorVisible = addAction("fontColor", "Font Color");\r
-               toggleFontHilight = addAction("fontHilight", "Font Hilight");\r
+               toggleFontVisible = addAction("font", tr("Font"));\r
+               toggleFontSizeVisible = addAction("fontSize", tr("Font Size"));\r
+               toggleFontColorVisible = addAction("fontColor", tr("Font Color"));\r
+               toggleFontHilight = addAction("fontHilight", tr("Font Hilight"));\r
+               toggleSpellCheck = addAction("spellCheck", tr("Spell Check"));\r
        }\r
        \r
        \r
index 97cbaaf..f3bbbb9 100644 (file)
@@ -523,7 +523,7 @@ public class MainMenuBar extends QMenuBar {
                \r
                spellCheckAction = new QAction(tr("Spell Check"), this);\r
                spellCheckAction.setToolTip("Check for spelling errors");\r
-               spellCheckAction.triggered.connect(parent.browserWindow, "doSpellCheck()");\r
+               spellCheckAction.triggered.connect(parent.browserWindow, "spellCheckClicked()");\r
                setupShortcut(spellCheckAction, "Tools_Spell_Check");\r
 \r
                encryptDatabaseAction = new QAction(tr("Encrypt Database"), this);\r
diff --git a/src/cx/fbn/nevernote/icons/spellCheck.gif b/src/cx/fbn/nevernote/icons/spellCheck.gif
new file mode 100644 (file)
index 0000000..deb6735
Binary files /dev/null and b/src/cx/fbn/nevernote/icons/spellCheck.gif differ