OSDN Git Service

Add SpellCheck.java
authorRandy Baumgarte <randy@fbn.cx>
Sat, 25 Sep 2010 19:06:53 +0000 (15:06 -0400)
committerRandy Baumgarte <randy@fbn.cx>
Sat, 25 Sep 2010 19:06:53 +0000 (15:06 -0400)
src/cx/fbn/nevernote/dialog/SpellCheck.java [new file with mode: 0644]

diff --git a/src/cx/fbn/nevernote/dialog/SpellCheck.java b/src/cx/fbn/nevernote/dialog/SpellCheck.java
new file mode 100644 (file)
index 0000000..c835290
--- /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
+package cx.fbn.nevernote.dialog;\r
+\r
+import com.trolltech.qt.gui.QDialog;\r
+import com.trolltech.qt.gui.QGridLayout;\r
+import com.trolltech.qt.gui.QLabel;\r
+import com.trolltech.qt.gui.QLineEdit;\r
+import com.trolltech.qt.gui.QListWidget;\r
+import com.trolltech.qt.gui.QPushButton;\r
+\r
+public class SpellCheck extends QDialog {\r
+\r
+       private boolean         replacePressed;\r
+       private boolean         cancelPressed;\r
+       private final QLabel    currentWord;\r
+       private final QLineEdit replacementWord;\r
+       private String misspelledWord;\r
+       private final QPushButton replace;\r
+       private final QPushButton ignore;\r
+       private final QListWidget suggestions;\r
+       \r
+       \r
+       // Constructor\r
+       public SpellCheck() {\r
+               replacePressed = false;\r
+               cancelPressed = false;\r
+               setWindowTitle(tr("Spell Check"));\r
+               QGridLayout grid = new QGridLayout();\r
+               setLayout(grid);\r
+               QGridLayout suggestionGrid = new QGridLayout();\r
+               QGridLayout buttonGrid = new QGridLayout();\r
+               \r
+               currentWord = new QLabel(misspelledWord);\r
+               replacementWord = new QLineEdit();\r
+               suggestions = new QListWidget();\r
+               \r
+               replacementWord.textChanged.connect(this, "validateInput()");\r
+               suggestions.itemSelectionChanged.connect(this, "replacementChosen()");\r
+               \r
+               suggestionGrid.addWidget(currentWord, 1, 1);\r
+               suggestionGrid.addWidget(new QLabel(tr("Suggestion")), 2,1);\r
+               suggestionGrid.addWidget(replacementWord, 3, 1);\r
+               suggestionGrid.addWidget(suggestions,4,1);\r
+               suggestionGrid.setContentsMargins(10, 10,  -10, -10);\r
+               grid.addLayout(suggestionGrid,1,1);\r
+               \r
+               replace = new QPushButton(tr("Replace"));\r
+               ignore = new QPushButton(tr("Ignore"));\r
+               replace.clicked.connect(this, "replaceButtonPressed()");\r
+               ignore.clicked.connect(this, "ignoreButtonPressed()");\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
+               buttonGrid.addWidget(new QLabel(), 1,1);\r
+               buttonGrid.addWidget(cancel, 1,2);\r
+               buttonGrid.addWidget(new QLabel(), 1,3);\r
+               buttonGrid.setColumnStretch(1, 10);\r
+               buttonGrid.setColumnStretch(3, 10);\r
+               grid.addLayout(buttonGrid,2,1);\r
+       }\r
+       \r
+       // The OK button was pressed\r
+       @SuppressWarnings("unused")\r
+       private void replaceButtonPressed() {\r
+               replacePressed = true;\r
+               cancelPressed = false;\r
+               close();\r
+       }\r
+       \r
+       // The CANCEL button was pressed\r
+       @SuppressWarnings("unused")\r
+       private void cancelButtonPressed() {\r
+               replacePressed = false;\r
+               cancelPressed = true;\r
+               close();\r
+       }\r
+       \r
+       // The ignore button was pressed\r
+       @SuppressWarnings("unused")\r
+       private void ignoreButtonPressed() {\r
+               replacePressed = false;\r
+               cancelPressed = false;\r
+               close();\r
+       }\r
+       \r
+       // Get the userid from the field\r
+       public String getReplacementWord() {\r
+               return replacementWord.text();\r
+       }\r
+       \r
+       // Set the current misspelled word\r
+       public void setWord(String w) {\r
+               misspelledWord = w;\r
+               currentWord.setText(tr("Word: ") +misspelledWord);\r
+       }\r
+       \r
+       // Check if the OK button was pressed\r
+       public boolean replacePressed() {\r
+               return replacePressed;\r
+       }\r
+       \r
+       // Check if the OK button was pressed\r
+       public boolean cancelPressed() {\r
+               return cancelPressed;\r
+       }\r
+       \r
+       // Validate user input\r
+       public void validateInput() {\r
+               replace.setEnabled(true);\r
+               if (replacementWord.text().trim().equals("")) {\r
+                       replace.setEnabled(false);\r
+                       return;\r
+               }\r
+       }\r
+       \r
+       private void replacementChosen() {\r
+               String sel = suggestions.currentItem().text();\r
+               replacementWord.setText(sel);\r
+       }\r
+       \r
+       \r
+       // Add a suggestion\r
+       public void addSuggestion(String word) {\r
+               suggestions.addItem(word);\r
+       }\r
+       \r
+       // Set the current suggestion\r
+       public void setCurrentSuggestion(String word) {\r
+               replacementWord.setText(word);\r
+       }\r
+       \r
+       public void setNoSuggestions(boolean enable) {\r
+               if (enable) {\r
+                       replacementWord.setEnabled(true);\r
+                       replace.setEnabled(true);\r
+                       suggestions.setEnabled(true);\r
+               } else {\r
+                       replacementWord.setEnabled(false);\r
+                       replace.setEnabled(false);\r
+                       suggestions.setEnabled(false);\r
+               }\r
+       }\r
+       \r
+       public void setSelectedSuggestion(int index) {\r
+               if (index < suggestions.count())\r
+                       suggestions.setCurrentRow(index);\r
+       }\r
+}\r