OSDN Git Service

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