OSDN Git Service

e3cac5b0645e78de93e37fd539f70392601ea042
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / ContentView.java
1 /*\r
2  * This file is part of NixNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 package cx.fbn.nevernote.gui;\r
20 \r
21 import com.trolltech.qt.core.QEvent;\r
22 import com.trolltech.qt.core.QMimeData;\r
23 import com.trolltech.qt.core.Qt;\r
24 import com.trolltech.qt.gui.QAction;\r
25 import com.trolltech.qt.gui.QContextMenuEvent;\r
26 import com.trolltech.qt.gui.QDropEvent;\r
27 import com.trolltech.qt.gui.QKeyEvent;\r
28 import com.trolltech.qt.gui.QKeySequence;\r
29 import com.trolltech.qt.gui.QKeySequence.StandardKey;\r
30 import com.trolltech.qt.gui.QMenu;\r
31 import com.trolltech.qt.gui.QShortcut;\r
32 import com.trolltech.qt.network.QNetworkRequest;\r
33 import com.trolltech.qt.webkit.QWebPage;\r
34 import com.trolltech.qt.webkit.QWebView;\r
35 \r
36 import cx.fbn.nevernote.Global;\r
37 \r
38 public class ContentView extends QWebView {\r
39 \r
40         BrowserWindow parent;\r
41         QMenu contextMenu = new QMenu(this);\r
42         QMenu tableMenu = new QMenu(this);\r
43         QAction cutAction;\r
44         QShortcut cutShortcut;\r
45         QAction copyAction;\r
46         QShortcut copyShortcut;\r
47         QAction pasteAction;\r
48         QShortcut pasteShortcut;\r
49         QAction pasteWithoutFormatAction;\r
50         QShortcut pasteWithoutFormatShortcut;\r
51         QAction todoAction;\r
52         QShortcut todoShortcut;\r
53         QAction encryptAction;\r
54         QShortcut encryptShortcut;\r
55         QAction downloadAttachment;\r
56         QShortcut downloadAttachmentShortcut;\r
57         QAction downloadImage;\r
58         QShortcut downloadImageShortcut;\r
59         QAction rotateImageRight;\r
60         QShortcut rotateImageRightShortcut;\r
61         QAction rotateImageLeft;\r
62         QShortcut rotateImageLeftShortcut;      \r
63         QAction insertLinkAction;\r
64         QShortcut insertLinkShortcut;\r
65         QAction insertLatexAction;\r
66         QShortcut insertLatexShortcut;\r
67         QAction insertTableAction;\r
68         QShortcut insertTableShortcut;\r
69         QAction insertTableRowAction;\r
70         QShortcut insertTableRowShortcut;\r
71         QAction insertTableColumnAction;\r
72         QShortcut insertTableColumnShortcut;\r
73         QAction deleteTableRowAction;\r
74         QShortcut deleteTableRowShortcut;\r
75         QAction deleteTableColumnAction;\r
76         QShortcut deleteTableColumnShortcut;\r
77         QAction openAction;\r
78         QAction insertQuickLinkAction;\r
79         QShortcut insertQuickLinkShortcut;\r
80         \r
81         QAction redBackgroundColor;\r
82         \r
83         QShortcut insertDateTimeShortcut;\r
84         \r
85         Signal1<QNetworkRequest> downloadAttachmentRequested;\r
86         Signal1<QNetworkRequest> downloadImageRequested;\r
87         \r
88         public ContentView(BrowserWindow p) {\r
89                 parent=p;\r
90                 contextMenu = new QMenu(this);\r
91                 \r
92                 openAction = new QAction(tr("Open"), this);\r
93                 openAction.setText(tr("Open"));\r
94                 contextMenu.addAction(openAction);\r
95                 openAction.triggered.connect(parent, "openFile()");\r
96                 \r
97                 cutAction = new QAction(tr("Cut"), this);\r
98                 cutAction.triggered.connect(parent, "cutClicked()");\r
99                 contextMenu.addAction(cutAction); \r
100                 contextMenu.insertSeparator(cutAction);\r
101                 setupShortcut(cutAction, "Edit_Cut");\r
102                 cutShortcut = new QShortcut(this);\r
103                 cutShortcut.activated.connect(parent, "cutClicked()");\r
104                 setupShortcut(cutShortcut, "Edit_Cut");\r
105                 \r
106                 copyAction = new QAction(tr("Copy"), this);\r
107                 copyAction.triggered.connect(parent, "copyClicked()");\r
108                 contextMenu.addAction(copyAction); \r
109                 setupShortcut(copyAction, "Edit_Copy");\r
110                 copyShortcut = new QShortcut(this);\r
111                 copyShortcut.activated.connect(parent, "copyClicked()");\r
112                 setupShortcut(copyShortcut, "Edit_Copy");\r
113                 \r
114                 pasteAction = pageAction(QWebPage.WebAction.Paste);\r
115                 pasteAction.disconnect();\r
116                 pasteAction.triggered.connect(parent, "pasteClicked()");\r
117                 \r
118                 contextMenu.addAction(pasteAction); \r
119                 setupShortcut(pasteAction, "Edit_Paste");\r
120                 pasteShortcut = new QShortcut(this);\r
121                 pasteShortcut.activated.connect(parent, "pasteClicked()");\r
122                 setupShortcut(pasteShortcut, "Edit_Paste");\r
123                 \r
124                 pasteWithoutFormatAction = new QAction(tr("Paste Without Formatting"), this);\r
125                 pasteWithoutFormatAction.triggered.connect(parent, "pasteWithoutFormattingClicked()");\r
126                 contextMenu.addAction(pasteWithoutFormatAction); \r
127                 setupShortcut(pasteWithoutFormatAction, "Edit_Paste_Without_Formatting");\r
128 //              pasteWithoutFormatShortcut = new QShortcut(this);\r
129 //              pasteWithoutFormatShortcut.activated.connect(parent, "pasteWithoutFormattingClicked()");\r
130 //              setupShortcut(pasteWithoutFormatShortcut, "Edit_Paste_Without_Formatting");\r
131                 \r
132                 contextMenu.addSeparator();\r
133                 QMenu colorMenu = new QMenu(tr("Background Color"));\r
134                 contextMenu.addMenu(colorMenu);\r
135                 \r
136                 colorMenu.addAction(setupColorMenuOption("White"));\r
137                 colorMenu.addAction(setupColorMenuOption("Red"));\r
138                 colorMenu.addAction(setupColorMenuOption("Blue"));\r
139                 colorMenu.addAction(setupColorMenuOption("Green"));\r
140                 colorMenu.addAction(setupColorMenuOption("Yellow"));\r
141                 colorMenu.addAction(setupColorMenuOption("Black"));\r
142                 colorMenu.addAction(setupColorMenuOption("Grey"));\r
143                 colorMenu.addAction(setupColorMenuOption("Purple"));\r
144                 colorMenu.addAction(setupColorMenuOption("Brown"));\r
145                 colorMenu.addAction(setupColorMenuOption("Orange"));\r
146                 colorMenu.addAction(setupColorMenuOption("Powder Blue"));\r
147                 \r
148                 \r
149                 contextMenu.addSeparator();\r
150                 todoAction = new QAction(tr("To-do"), this);\r
151                 todoAction.triggered.connect(parent, "todoClicked()");\r
152                 contextMenu.addAction(todoAction);\r
153                 setupShortcut(todoAction, "Edit_Insert_Todo");\r
154                 contextMenu.insertSeparator(todoAction);\r
155                 todoShortcut = new QShortcut(this);\r
156                 todoShortcut.activated.connect(parent, "todoClicked()");\r
157                 setupShortcut(todoShortcut, "Edit_Insert_Todo");\r
158                 \r
159                 encryptAction = new QAction(tr("Encrypt Selected Text"), this);\r
160                 encryptAction.triggered.connect(parent, "encryptText()");\r
161                 contextMenu.addAction(encryptAction);\r
162                 contextMenu.insertSeparator(encryptAction);\r
163                 setupShortcut(encryptAction, "Edit_Encrypt_Text");\r
164                 encryptAction.setEnabled(false);\r
165                 encryptShortcut = new QShortcut(this);\r
166                 encryptShortcut.activated.connect(parent, "encryptText()");\r
167                 setupShortcut(encryptShortcut, "Edit_Encrypt_Text");\r
168                 \r
169                 insertLinkAction = new QAction(tr("Insert Hyperlink"), this);\r
170                 insertLinkAction.triggered.connect(parent, "insertLink()");\r
171                 setupShortcut(insertLinkAction, "Edit_Insert_Hyperlink");\r
172                 contextMenu.addAction(insertLinkAction);\r
173                 insertLinkAction.setEnabled(false);\r
174                 insertLinkShortcut = new QShortcut(this);\r
175                 setupShortcut(insertLinkShortcut, "Edit_Insert_Hyperlink");\r
176                 insertLinkShortcut.activated.connect(parent, "insertLink()");\r
177                 \r
178                 insertQuickLinkAction = new QAction(tr("Quick Link"), this);\r
179                 insertQuickLinkAction.triggered.connect(parent, "insertQuickLink()");\r
180                 setupShortcut(insertQuickLinkAction, "Edit_Insert_QuickLink");\r
181                 contextMenu.addAction(insertQuickLinkAction);\r
182                 insertQuickLinkAction.setEnabled(false);\r
183                 insertQuickLinkShortcut = new QShortcut(this);\r
184                 setupShortcut(insertQuickLinkShortcut, "Edit_Insert_Quicklink");\r
185                 insertQuickLinkShortcut.activated.connect(parent, "insertQuickLink()");\r
186 \r
187                 insertLatexAction = new QAction(tr("Insert LaTeX Formula"), this);\r
188                 insertLatexAction.triggered.connect(parent, "insertLatex()");\r
189                 setupShortcut(insertLatexAction, "Edit_Insert_Latex");\r
190                 contextMenu.addAction(insertLatexAction);\r
191                 insertLatexShortcut = new QShortcut(this);\r
192                 setupShortcut(insertLatexShortcut, "Edit_Insert_Latex");\r
193                 insertLatexShortcut.activated.connect(parent, "insertLatex()");\r
194                 \r
195                 contextMenu.addMenu(tableMenu);\r
196                 tableMenu.setTitle("Table");\r
197                 insertTableAction = new QAction(tr("Insert Table"), this);\r
198                 insertTableAction.triggered.connect(parent, "insertTable()");\r
199                 setupShortcut(insertTableAction, "Edit_Insert_Table");\r
200                 tableMenu.addAction(insertTableAction);\r
201                 insertTableShortcut = new QShortcut(this);\r
202                 setupShortcut(insertTableShortcut, "Edit_Insert_Table");\r
203                 insertTableShortcut.activated.connect(parent, "insertTable()");\r
204 \r
205                 insertTableRowAction = new QAction(tr("Insert Row"), this);\r
206                 insertTableRowAction.triggered.connect(parent, "insertTableRow()");\r
207                 setupShortcut(insertTableRowAction, "Edit_Insert_Table_Row");\r
208                 tableMenu.addAction(insertTableRowAction);\r
209                 insertTableRowShortcut = new QShortcut(this);\r
210                 setupShortcut(insertTableRowShortcut, "Edit_Insert_Table_Row");\r
211                 insertTableRowShortcut.activated.connect(parent, "insertTableRow()");\r
212                 \r
213                 insertTableColumnAction = new QAction(tr("Insert Column"), this);\r
214                 insertTableColumnAction.triggered.connect(parent, "insertTableColumn()");\r
215                 setupShortcut(insertTableColumnAction, "Edit_Insert_Table_Column");\r
216                 tableMenu.addAction(insertTableColumnAction);\r
217                 insertTableColumnShortcut = new QShortcut(this);\r
218                 setupShortcut(insertTableColumnShortcut, "Edit_Insert_Table_Column");\r
219                 insertTableColumnShortcut.activated.connect(parent, "insertTableColumn()");\r
220                 \r
221                 deleteTableRowAction = new QAction(tr("Delete Row"), this);\r
222                 deleteTableRowAction.triggered.connect(parent, "deleteTableRow()");\r
223                 setupShortcut(deleteTableRowAction, "Edit_Delete_Table_Row");\r
224                 tableMenu.addAction(deleteTableRowAction);\r
225                 deleteTableRowShortcut = new QShortcut(this);\r
226                 setupShortcut(deleteTableRowShortcut, "Edit_Delete_Table_Row");\r
227                 deleteTableRowShortcut.activated.connect(parent, "deleteTableRow()");\r
228                 \r
229                 deleteTableColumnAction = new QAction(tr("Delete Column"), this);\r
230                 deleteTableColumnAction.triggered.connect(parent, "deleteTableColumn()");\r
231                 setupShortcut(deleteTableColumnAction, "Edit_Delete_Table_Column");\r
232                 tableMenu.addAction(deleteTableColumnAction);\r
233                 deleteTableColumnShortcut = new QShortcut(this);\r
234                 setupShortcut(deleteTableColumnShortcut, "Edit_Delete_Table_Column");\r
235                 deleteTableColumnShortcut.activated.connect(parent, "deleteTableColumn()");\r
236                 \r
237                 \r
238                 insertDateTimeShortcut = new QShortcut(this);\r
239                 insertDateTimeShortcut.activated.connect(parent, "insertDateTime()");\r
240                 setupShortcut(insertDateTimeShortcut, "Insert_DateTime");\r
241                         \r
242                 QMenu imageMenu = new QMenu();\r
243                 imageMenu.setTitle(tr("Image"));\r
244                 contextMenu.addMenu(imageMenu);\r
245                 downloadImage = pageAction(QWebPage.WebAction.DownloadImageToDisk);\r
246                 downloadImage.setText(tr("Save Image"));\r
247                 imageMenu.addAction(downloadImage);\r
248 //              contextMenu.insertSeparator(downloadImage);\r
249 //              page().downloadRequested.connect(this, "downloadImage(QNetworkRequest)");\r
250                 downloadImageRequested = new Signal1<QNetworkRequest>();\r
251                 \r
252                 rotateImageRight = new QAction(tr("Rotate Right"), this);\r
253                 imageMenu.addAction(rotateImageRight);\r
254                 rotateImageRight.triggered.connect(parent, "rotateImageRight()");\r
255                 rotateImageRightShortcut = new QShortcut(this);\r
256                 setupShortcut(rotateImageRightShortcut, "Edit_Image_Rotate_Right");\r
257                 rotateImageRightShortcut.activated.connect(parent, "rotateImageRight()");\r
258                 \r
259                 rotateImageLeft = new QAction(tr("Rotate Left"), this);\r
260                 imageMenu.addAction(rotateImageLeft);\r
261                 rotateImageLeft.triggered.connect(parent, "rotateImageLeft()");\r
262                 rotateImageLeftShortcut = new QShortcut(this);\r
263                 setupShortcut(rotateImageLeftShortcut, "Edit_Image_Rotate_Left");\r
264                 rotateImageLeftShortcut.activated.connect(parent, "rotateImageLeft()");\r
265                 \r
266                 downloadAttachment = pageAction(QWebPage.WebAction.DownloadLinkToDisk);\r
267                 downloadAttachment.setText(tr("Save Attachment"));\r
268                 contextMenu.addAction(downloadAttachment);\r
269                 page().downloadRequested.connect(this, "downloadAttachment(QNetworkRequest)");\r
270                 downloadAttachmentRequested = new Signal1<QNetworkRequest>();\r
271                 \r
272         }\r
273         \r
274         private void setupShortcut(QAction action, String text) {\r
275                 if (!Global.shortcutKeys.containsAction(text))\r
276                         return;\r
277                 action.setShortcut(Global.shortcutKeys.getShortcut(text));\r
278         }\r
279         \r
280         private void setupShortcut(QShortcut action, String text) {\r
281                 if (!Global.shortcutKeys.containsAction(text))\r
282                         return;\r
283                 action.setKey(new QKeySequence(Global.shortcutKeys.getShortcut(text)));\r
284         }\r
285         \r
286         private QAction setupColorMenuOption(String color) {\r
287                 QAction backgroundColor = new QAction(tr(color), this);\r
288                 color = color.replace(" ", "");\r
289                 backgroundColor.triggered.connect(this, "setBackground"+color+"()");\r
290                 return backgroundColor;\r
291         }\r
292         \r
293         @Override\r
294         public boolean event(QEvent event)\r
295     {\r
296         if (event.type().equals(QEvent.Type.KeyPress)) {\r
297             QKeyEvent ke = (QKeyEvent) event;\r
298             if (ke.key() == Qt.Key.Key_Tab.value()) {\r
299                         parent.tabPressed();\r
300                         ke.accept();\r
301                 return true;\r
302             }\r
303             if (ke.key() == Qt.Key.Key_Backtab.value()) {\r
304                 parent.backtabPressed();\r
305                 return true;\r
306             }\r
307         }\r
308         return super.event(event);\r
309     }\r
310         \r
311         \r
312         @Override\r
313         public void keyPressEvent(QKeyEvent e) {\r
314                         \r
315                 // This is done because if we set the content editable, the scroll keys are\r
316                 // ignored by webkit.\r
317                 if (e.key() == Qt.Key.Key_PageUp.value() || e.key() == Qt.Key.Key_PageDown.value()) {\r
318                         int bottom = page().mainFrame().geometry().bottom();\r
319                         int top = page().mainFrame().geometry().top();\r
320                         int scrollValue = top-bottom;\r
321                         if (e.key() == Qt.Key.Key_PageDown.value())\r
322                                 scrollValue = -1*scrollValue;\r
323                         page().mainFrame().scroll(0, scrollValue);\r
324                 }\r
325                                 \r
326                 // This is done to allow proper pasting of resources, otherwise it messes up multiple pastes\r
327                 if (e.matches(StandardKey.Paste)) {\r
328                         parent.pasteClicked();\r
329                         e.accept();\r
330                         return;\r
331                 }\r
332                 super.keyPressEvent(e);\r
333         }\r
334 \r
335 //      public void downloadImage(QNetworkRequest req) {\r
336 //              System.out.println(req.url().toString());\r
337 //              downloadImageRequested.emit(req);\r
338 //      }\r
339         public void downloadAttachment(QNetworkRequest req) {\r
340                 downloadAttachmentRequested.emit(req);\r
341         }\r
342 \r
343         \r
344         @Override\r
345         public void dropEvent(QDropEvent e) {\r
346                 setFocus();\r
347                 QMimeData mime = e.mimeData();\r
348                 parent.handleUrls(mime);\r
349                 parent.contentChanged();\r
350 //              triggerPageAction(WebAction.Reload);\r
351         }\r
352         \r
353         @Override\r
354         public void contextMenuEvent(QContextMenuEvent event) {\r
355                 if (event != null)\r
356                         contextMenu.exec(event.globalPos());\r
357         }\r
358 \r
359         \r
360         \r
361         @SuppressWarnings("unused")\r
362         private void setBackgroundWhite() {parent.setBackgroundColor("white");}\r
363         @SuppressWarnings("unused")\r
364         private void setBackgroundRed() {parent.setBackgroundColor("red");}\r
365         @SuppressWarnings("unused")\r
366         private void setBackgroundBlue() {parent.setBackgroundColor("blue");}\r
367         @SuppressWarnings("unused")\r
368         private void setBackgroundGreen() {parent.setBackgroundColor("green");}\r
369         @SuppressWarnings("unused")\r
370         private void setBackgroundYellow() {parent.setBackgroundColor("yellow");}\r
371         @SuppressWarnings("unused")\r
372         private void setBackgroundBlack() {parent.setBackgroundColor("black");}\r
373         @SuppressWarnings("unused")\r
374         private void setBackgroundPurple() {parent.setBackgroundColor("purple");}\r
375         @SuppressWarnings("unused")\r
376         private void setBackgroundBrown() {parent.setBackgroundColor("brown");}\r
377         @SuppressWarnings("unused")\r
378         private void setBackgroundGrey() {parent.setBackgroundColor("grey");}\r
379         @SuppressWarnings("unused")\r
380         private void setBackgroundOrange() {parent.setBackgroundColor("orange");}\r
381         @SuppressWarnings("unused")\r
382         private void setBackgroundPowderBlue() {parent.setBackgroundColor("powderblue");}\r
383 \r
384 \r
385 }\r