OSDN Git Service

6e0f33726b21a128de83e296f7a7d2a3de3e09d1
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / ContentView.java
1 /*\r
2  * This file is part of NixNote/NeighborNote \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         QMenu imageMenu;\r
81         \r
82         QAction redBackgroundColor;\r
83         \r
84         QShortcut insertDateTimeShortcut;\r
85         \r
86         Signal1<QNetworkRequest> downloadAttachmentRequested;\r
87         Signal1<QNetworkRequest> downloadImageRequested;\r
88         \r
89         public ContentView(BrowserWindow p) {\r
90                 parent=p;\r
91                 contextMenu = new QMenu(this);\r
92                 \r
93                 openAction = new QAction(tr("Open"), this);\r
94                 openAction.setText(tr("Open"));\r
95                 contextMenu.addAction(openAction);\r
96                 openAction.triggered.connect(parent, "openFile()");\r
97                 \r
98                 cutAction = new QAction(tr("Cut"), this);\r
99                 cutAction.triggered.connect(parent, "cutClicked()");\r
100                 contextMenu.addAction(cutAction); \r
101                 contextMenu.insertSeparator(cutAction);\r
102                 setupShortcut(cutAction, "Edit_Cut");\r
103                 cutShortcut = new QShortcut(this);\r
104                 cutShortcut.activated.connect(parent, "cutClicked()");\r
105                 setupShortcut(cutShortcut, "Edit_Cut");\r
106                 \r
107                 copyAction = new QAction(tr("Copy"), this);\r
108                 copyAction.triggered.connect(parent, "copyClicked()");\r
109                 contextMenu.addAction(copyAction); \r
110                 setupShortcut(copyAction, "Edit_Copy");\r
111                 copyShortcut = new QShortcut(this);\r
112                 copyShortcut.activated.connect(parent, "copyClicked()");\r
113                 setupShortcut(copyShortcut, "Edit_Copy");\r
114                 \r
115                 pasteAction = pageAction(QWebPage.WebAction.Paste);\r
116                 pasteAction.disconnect();\r
117                 pasteAction.triggered.connect(parent, "pasteClicked()");\r
118                 \r
119                 contextMenu.addAction(pasteAction); \r
120                 setupShortcut(pasteAction, "Edit_Paste");\r
121                 pasteShortcut = new QShortcut(this);\r
122                 pasteShortcut.activated.connect(parent, "pasteClicked()");\r
123                 setupShortcut(pasteShortcut, "Edit_Paste");\r
124                 \r
125                 pasteWithoutFormatAction = new QAction(tr("Paste Without Formatting"), this);\r
126                 pasteWithoutFormatAction.triggered.connect(parent, "pasteWithoutFormattingClicked()");\r
127                 contextMenu.addAction(pasteWithoutFormatAction); \r
128                 setupShortcut(pasteWithoutFormatAction, "Edit_Paste_Without_Formatting");\r
129 //              pasteWithoutFormatShortcut = new QShortcut(this);\r
130 //              pasteWithoutFormatShortcut.activated.connect(parent, "pasteWithoutFormattingClicked()");\r
131 //              setupShortcut(pasteWithoutFormatShortcut, "Edit_Paste_Without_Formatting");\r
132                 \r
133                 contextMenu.addSeparator();\r
134                 QMenu colorMenu = new QMenu(tr("Background Color"));\r
135                 contextMenu.addMenu(colorMenu);\r
136                 \r
137                 colorMenu.addAction(setupColorMenuOption("White"));\r
138                 colorMenu.addAction(setupColorMenuOption("Red"));\r
139                 colorMenu.addAction(setupColorMenuOption("Blue"));\r
140                 colorMenu.addAction(setupColorMenuOption("Green"));\r
141                 colorMenu.addAction(setupColorMenuOption("Yellow"));\r
142                 colorMenu.addAction(setupColorMenuOption("Black"));\r
143                 colorMenu.addAction(setupColorMenuOption("Grey"));\r
144                 colorMenu.addAction(setupColorMenuOption("Purple"));\r
145                 colorMenu.addAction(setupColorMenuOption("Brown"));\r
146                 colorMenu.addAction(setupColorMenuOption("Orange"));\r
147                 colorMenu.addAction(setupColorMenuOption("Powder Blue"));\r
148                 \r
149                 \r
150                 contextMenu.addSeparator();\r
151                 todoAction = new QAction(tr("To-do"), this);\r
152                 todoAction.triggered.connect(parent, "todoClicked()");\r
153                 contextMenu.addAction(todoAction);\r
154                 setupShortcut(todoAction, "Edit_Insert_Todo");\r
155                 contextMenu.insertSeparator(todoAction);\r
156                 todoShortcut = new QShortcut(this);\r
157                 todoShortcut.activated.connect(parent, "todoClicked()");\r
158                 setupShortcut(todoShortcut, "Edit_Insert_Todo");\r
159                 \r
160                 encryptAction = new QAction(tr("Encrypt Selected Text"), this);\r
161                 encryptAction.triggered.connect(parent, "encryptText()");\r
162                 contextMenu.addAction(encryptAction);\r
163                 contextMenu.insertSeparator(encryptAction);\r
164                 setupShortcut(encryptAction, "Edit_Encrypt_Text");\r
165                 encryptAction.setEnabled(false);\r
166                 encryptShortcut = new QShortcut(this);\r
167                 encryptShortcut.activated.connect(parent, "encryptText()");\r
168                 setupShortcut(encryptShortcut, "Edit_Encrypt_Text");\r
169                 \r
170                 insertLinkAction = new QAction(tr("Insert Hyperlink"), this);\r
171                 insertLinkAction.triggered.connect(parent, "insertLink()");\r
172                 setupShortcut(insertLinkAction, "Edit_Insert_Hyperlink");\r
173                 contextMenu.addAction(insertLinkAction);\r
174                 insertLinkAction.setEnabled(false);\r
175                 insertLinkShortcut = new QShortcut(this);\r
176                 setupShortcut(insertLinkShortcut, "Edit_Insert_Hyperlink");\r
177                 insertLinkShortcut.activated.connect(parent, "insertLink()");\r
178                 \r
179                 insertQuickLinkAction = new QAction(tr("Quick Link"), this);\r
180                 insertQuickLinkAction.triggered.connect(parent, "insertQuickLink()");\r
181                 setupShortcut(insertQuickLinkAction, "Edit_Insert_QuickLink");\r
182                 contextMenu.addAction(insertQuickLinkAction);\r
183                 insertQuickLinkAction.setEnabled(false);\r
184                 insertQuickLinkShortcut = new QShortcut(this);\r
185                 setupShortcut(insertQuickLinkShortcut, "Edit_Insert_Quicklink");\r
186                 insertQuickLinkShortcut.activated.connect(parent, "insertQuickLink()");\r
187 \r
188                 insertLatexAction = new QAction(tr("Insert LaTeX Formula"), this);\r
189                 insertLatexAction.triggered.connect(parent, "insertLatex()");\r
190                 setupShortcut(insertLatexAction, "Edit_Insert_Latex");\r
191                 contextMenu.addAction(insertLatexAction);\r
192                 insertLatexShortcut = new QShortcut(this);\r
193                 setupShortcut(insertLatexShortcut, "Edit_Insert_Latex");\r
194                 insertLatexShortcut.activated.connect(parent, "insertLatex()");\r
195                 \r
196                 contextMenu.addMenu(tableMenu);\r
197                 tableMenu.setTitle("Table");\r
198                 insertTableAction = new QAction(tr("Insert Table"), this);\r
199                 insertTableAction.triggered.connect(parent, "insertTable()");\r
200                 setupShortcut(insertTableAction, "Edit_Insert_Table");\r
201                 tableMenu.addAction(insertTableAction);\r
202                 insertTableShortcut = new QShortcut(this);\r
203                 setupShortcut(insertTableShortcut, "Edit_Insert_Table");\r
204                 insertTableShortcut.activated.connect(parent, "insertTable()");\r
205 \r
206                 insertTableRowAction = new QAction(tr("Insert Row"), this);\r
207                 insertTableRowAction.triggered.connect(parent, "insertTableRow()");\r
208                 setupShortcut(insertTableRowAction, "Edit_Insert_Table_Row");\r
209                 tableMenu.addAction(insertTableRowAction);\r
210                 insertTableRowShortcut = new QShortcut(this);\r
211                 setupShortcut(insertTableRowShortcut, "Edit_Insert_Table_Row");\r
212                 insertTableRowShortcut.activated.connect(parent, "insertTableRow()");\r
213                 \r
214                 insertTableColumnAction = new QAction(tr("Insert Column"), this);\r
215                 insertTableColumnAction.triggered.connect(parent, "insertTableColumn()");\r
216                 setupShortcut(insertTableColumnAction, "Edit_Insert_Table_Column");\r
217                 tableMenu.addAction(insertTableColumnAction);\r
218                 insertTableColumnShortcut = new QShortcut(this);\r
219                 setupShortcut(insertTableColumnShortcut, "Edit_Insert_Table_Column");\r
220                 insertTableColumnShortcut.activated.connect(parent, "insertTableColumn()");\r
221                 \r
222                 deleteTableRowAction = new QAction(tr("Delete Row"), this);\r
223                 deleteTableRowAction.triggered.connect(parent, "deleteTableRow()");\r
224                 setupShortcut(deleteTableRowAction, "Edit_Delete_Table_Row");\r
225                 tableMenu.addAction(deleteTableRowAction);\r
226                 deleteTableRowShortcut = new QShortcut(this);\r
227                 setupShortcut(deleteTableRowShortcut, "Edit_Delete_Table_Row");\r
228                 deleteTableRowShortcut.activated.connect(parent, "deleteTableRow()");\r
229                 \r
230                 deleteTableColumnAction = new QAction(tr("Delete Column"), this);\r
231                 deleteTableColumnAction.triggered.connect(parent, "deleteTableColumn()");\r
232                 setupShortcut(deleteTableColumnAction, "Edit_Delete_Table_Column");\r
233                 tableMenu.addAction(deleteTableColumnAction);\r
234                 deleteTableColumnShortcut = new QShortcut(this);\r
235                 setupShortcut(deleteTableColumnShortcut, "Edit_Delete_Table_Column");\r
236                 deleteTableColumnShortcut.activated.connect(parent, "deleteTableColumn()");\r
237                 \r
238                 \r
239                 insertDateTimeShortcut = new QShortcut(this);\r
240                 insertDateTimeShortcut.activated.connect(parent, "insertDateTime()");\r
241                 setupShortcut(insertDateTimeShortcut, "Insert_DateTime");\r
242                         \r
243                 imageMenu = new QMenu();\r
244                 imageMenu.setTitle(tr("Image"));\r
245                 contextMenu.addMenu(imageMenu);\r
246                 downloadImage = pageAction(QWebPage.WebAction.DownloadImageToDisk);\r
247                 downloadImage.setText(tr("Save Image"));\r
248                 imageMenu.addAction(downloadImage);\r
249 //              contextMenu.insertSeparator(downloadImage);\r
250 //              page().downloadRequested.connect(this, "downloadImage(QNetworkRequest)");\r
251                 downloadImageRequested = new Signal1<QNetworkRequest>();\r
252                 \r
253                 rotateImageRight = new QAction(tr("Rotate Right"), this);\r
254                 imageMenu.addAction(rotateImageRight);\r
255                 rotateImageRight.triggered.connect(parent, "rotateImageRight()");\r
256                 rotateImageRightShortcut = new QShortcut(this);\r
257                 setupShortcut(rotateImageRightShortcut, "Edit_Image_Rotate_Right");\r
258                 rotateImageRightShortcut.activated.connect(parent, "rotateImageRight()");\r
259                 \r
260                 rotateImageLeft = new QAction(tr("Rotate Left"), this);\r
261                 imageMenu.addAction(rotateImageLeft);\r
262                 rotateImageLeft.triggered.connect(parent, "rotateImageLeft()");\r
263                 rotateImageLeftShortcut = new QShortcut(this);\r
264                 setupShortcut(rotateImageLeftShortcut, "Edit_Image_Rotate_Left");\r
265                 rotateImageLeftShortcut.activated.connect(parent, "rotateImageLeft()");\r
266                 \r
267                 downloadAttachment = pageAction(QWebPage.WebAction.DownloadLinkToDisk);\r
268                 downloadAttachment.setText(tr("Save Attachment"));\r
269                 contextMenu.addAction(downloadAttachment);\r
270                 page().downloadRequested.connect(this, "downloadAttachment(QNetworkRequest)");\r
271                 downloadAttachmentRequested = new Signal1<QNetworkRequest>();\r
272                 \r
273         }\r
274         \r
275         private void setupShortcut(QAction action, String text) {\r
276                 if (!Global.shortcutKeys.containsAction(text))\r
277                         return;\r
278                 action.setShortcut(Global.shortcutKeys.getShortcut(text));\r
279         }\r
280         \r
281         private void setupShortcut(QShortcut action, String text) {\r
282                 if (!Global.shortcutKeys.containsAction(text))\r
283                         return;\r
284                 action.setKey(new QKeySequence(Global.shortcutKeys.getShortcut(text)));\r
285         }\r
286         \r
287         private QAction setupColorMenuOption(String color) {\r
288                 QAction backgroundColor = new QAction(tr(color), this);\r
289                 color = color.replace(" ", "");\r
290                 backgroundColor.triggered.connect(this, "setBackground"+color+"()");\r
291                 return backgroundColor;\r
292         }\r
293         \r
294         @Override\r
295         public boolean event(QEvent event)\r
296     {\r
297         if (event.type().equals(QEvent.Type.KeyPress)) {\r
298             QKeyEvent ke = (QKeyEvent) event;\r
299             if (ke.key() == Qt.Key.Key_Tab.value()) {\r
300                         parent.tabPressed();\r
301                         ke.accept();\r
302                 return true;\r
303             }\r
304             if (ke.key() == Qt.Key.Key_Backtab.value()) {\r
305                 parent.backtabPressed();\r
306                 return true;\r
307             }\r
308         }\r
309         return super.event(event);\r
310     }\r
311         \r
312         \r
313         @Override\r
314         public void keyPressEvent(QKeyEvent e) {\r
315                         \r
316                 // This is done because if we set the content editable, the scroll keys are\r
317                 // ignored by webkit.\r
318                 if (e.key() == Qt.Key.Key_PageUp.value() || e.key() == Qt.Key.Key_PageDown.value()) {\r
319                         int bottom = page().mainFrame().geometry().bottom();\r
320                         int top = page().mainFrame().geometry().top();\r
321                         int scrollValue = top-bottom;\r
322                         if (e.key() == Qt.Key.Key_PageDown.value())\r
323                                 scrollValue = -1*scrollValue;\r
324                         page().mainFrame().scroll(0, scrollValue);\r
325                 }\r
326                                 \r
327                 // This is done to allow proper pasting of resources, otherwise it messes up multiple pastes\r
328                 if (e.matches(StandardKey.Paste)) {\r
329                         parent.pasteClicked();\r
330                         e.accept();\r
331                         return;\r
332                 }\r
333                 super.keyPressEvent(e);\r
334         }\r
335 \r
336 //      public void downloadImage(QNetworkRequest req) {\r
337 //              System.out.println(req.url().toString());\r
338 //              downloadImageRequested.emit(req);\r
339 //      }\r
340         public void downloadAttachment(QNetworkRequest req) {\r
341                 downloadAttachmentRequested.emit(req);\r
342         }\r
343 \r
344         \r
345         @Override\r
346         public void dropEvent(QDropEvent e) {\r
347                 setFocus();\r
348                 QMimeData mime = e.mimeData();\r
349                 parent.handleUrls(mime);\r
350                 parent.contentChanged();\r
351 //              triggerPageAction(WebAction.Reload);\r
352         }\r
353         \r
354         @Override\r
355         public void contextMenuEvent(QContextMenuEvent event) {\r
356                 if (event != null)\r
357                         contextMenu.exec(event.globalPos());\r
358         }\r
359 \r
360         \r
361         \r
362         @SuppressWarnings("unused")\r
363         private void setBackgroundWhite() {parent.setBackgroundColor("white");}\r
364         @SuppressWarnings("unused")\r
365         private void setBackgroundRed() {parent.setBackgroundColor("red");}\r
366         @SuppressWarnings("unused")\r
367         private void setBackgroundBlue() {parent.setBackgroundColor("blue");}\r
368         @SuppressWarnings("unused")\r
369         private void setBackgroundGreen() {parent.setBackgroundColor("green");}\r
370         @SuppressWarnings("unused")\r
371         private void setBackgroundYellow() {parent.setBackgroundColor("yellow");}\r
372         @SuppressWarnings("unused")\r
373         private void setBackgroundBlack() {parent.setBackgroundColor("black");}\r
374         @SuppressWarnings("unused")\r
375         private void setBackgroundPurple() {parent.setBackgroundColor("purple");}\r
376         @SuppressWarnings("unused")\r
377         private void setBackgroundBrown() {parent.setBackgroundColor("brown");}\r
378         @SuppressWarnings("unused")\r
379         private void setBackgroundGrey() {parent.setBackgroundColor("grey");}\r
380         @SuppressWarnings("unused")\r
381         private void setBackgroundOrange() {parent.setBackgroundColor("orange");}\r
382         @SuppressWarnings("unused")\r
383         private void setBackgroundPowderBlue() {parent.setBackgroundColor("powderblue");}\r
384 \r
385 \r
386 }\r