OSDN Git Service

2a88ad090ee6e229ef894851f6c4b77b46d50c0d
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / TableView.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 java.util.List;\r
22 import java.util.SortedMap;\r
23 \r
24 import com.evernote.edam.type.Note;\r
25 import com.trolltech.qt.core.QByteArray;\r
26 import com.trolltech.qt.core.QModelIndex;\r
27 import com.trolltech.qt.core.Qt;\r
28 import com.trolltech.qt.core.Qt.SortOrder;\r
29 import com.trolltech.qt.gui.QAbstractItemView;\r
30 import com.trolltech.qt.gui.QAction;\r
31 import com.trolltech.qt.gui.QApplication;\r
32 import com.trolltech.qt.gui.QColor;\r
33 import com.trolltech.qt.gui.QContextMenuEvent;\r
34 import com.trolltech.qt.gui.QDragEnterEvent;\r
35 import com.trolltech.qt.gui.QDropEvent;\r
36 import com.trolltech.qt.gui.QFontMetrics;\r
37 import com.trolltech.qt.gui.QHeaderView;\r
38 import com.trolltech.qt.gui.QKeyEvent;\r
39 import com.trolltech.qt.gui.QKeySequence.StandardKey;\r
40 import com.trolltech.qt.gui.QMenu;\r
41 import com.trolltech.qt.gui.QTableView;\r
42 \r
43 import cx.fbn.nevernote.Global;\r
44 import cx.fbn.nevernote.filters.NoteSortFilterProxyModel;\r
45 import cx.fbn.nevernote.signals.NoteSignal;\r
46 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
47 import cx.fbn.nevernote.utilities.ListManager;\r
48 \r
49 public class TableView extends QTableView {\r
50         private final ListManager                               runner;\r
51         private final ApplicationLogger         logger;\r
52     public NoteSortFilterProxyModel     proxyModel;             // note sort model\r
53     private QAction deleteAction;\r
54     private QAction addAction;\r
55     private QAction restoreAction;\r
56     private QAction noteHistoryAction;\r
57     private QAction duplicateAction;\r
58     private QAction     mergeNotesAction;\r
59     \r
60     // Note title colors\r
61     private QAction     noteTitleColorWhite;\r
62     private QAction     noteTitleColorRed;\r
63     private QAction     noteTitleColorBlue;\r
64     private QAction     noteTitleColorGreen;\r
65     private QAction     noteTitleColorYellow;\r
66     private QAction     noteTitleColorBlack;\r
67     private QAction     noteTitleColorGray;\r
68     private QAction     noteTitleColorCyan;\r
69     private QAction     noteTitleColorMagenta;\r
70 \r
71 \r
72     \r
73     public QHeaderView header;\r
74     int fontHeight;\r
75     public Signal1<String> rowChanged;\r
76     public Signal0      resetViewport;\r
77     public NoteSignal noteSignal;\r
78         \r
79         public TableView(ApplicationLogger l, ListManager m) {\r
80                 logger = l;\r
81                 header = horizontalHeader();\r
82                 header.setMovable(true);\r
83                 \r
84                 noteSignal = new NoteSignal();\r
85                 setAcceptDrops(true);\r
86                 setDragEnabled(true);\r
87                 setDragDropMode(QAbstractItemView.DragDropMode.DragDrop);\r
88                 setDropIndicatorShown(false);\r
89                 \r
90                 runner = m;     \r
91                 \r
92         runner.getNoteTableModel().setHeaderData(Global.noteTableCreationPosition, Qt.Orientation.Horizontal, tr("Date Created"), Qt.ItemDataRole.DisplayRole);\r
93         runner.getNoteTableModel().setHeaderData(Global.noteTableTagPosition, Qt.Orientation.Horizontal, tr("Tags"), Qt.ItemDataRole.DisplayRole);\r
94         runner.getNoteTableModel().setHeaderData(Global.noteTableGuidPosition, Qt.Orientation.Horizontal, tr("Guid"), Qt.ItemDataRole.DisplayRole);\r
95         runner.getNoteTableModel().setHeaderData(Global.noteTableNotebookPosition, Qt.Orientation.Horizontal, tr("Notebook"), Qt.ItemDataRole.DisplayRole);\r
96         runner.getNoteTableModel().setHeaderData(Global.noteTableTitlePosition, Qt.Orientation.Horizontal, tr("Title"), Qt.ItemDataRole.DisplayRole);\r
97         runner.getNoteTableModel().setHeaderData(Global.noteTableChangedPosition, Qt.Orientation.Horizontal, tr("Date Changed"), Qt.ItemDataRole.DisplayRole);\r
98         runner.getNoteTableModel().setHeaderData(Global.noteTableAuthorPosition, Qt.Orientation.Horizontal, tr("Author"), Qt.ItemDataRole.DisplayRole);\r
99         runner.getNoteTableModel().setHeaderData(Global.noteTableSourceUrlPosition, Qt.Orientation.Horizontal, tr("Source Url"), Qt.ItemDataRole.DisplayRole);\r
100         runner.getNoteTableModel().setHeaderData(Global.noteTableSubjectDatePosition, Qt.Orientation.Horizontal, tr("Subject Date"), Qt.ItemDataRole.DisplayRole);\r
101         runner.getNoteTableModel().setHeaderData(Global.noteTableSynchronizedPosition, Qt.Orientation.Horizontal, tr("Synchronized"), Qt.ItemDataRole.DisplayRole);\r
102         header.sortIndicatorChanged.connect(this, "resetViewport()");\r
103        \r
104         proxyModel = new NoteSortFilterProxyModel(this);\r
105         proxyModel.setSourceModel(runner.getNoteTableModel());\r
106         setAlternatingRowColors(false);\r
107         setModel(proxyModel);\r
108 //        setModel(runner.getNoteTableModel());\r
109         runner.getNoteTableModel().setSortProxyModel(proxyModel);\r
110                \r
111         setSortingEnabled(true);\r
112         int sortCol = proxyModel.sortColumn();\r
113                 SortOrder sortOrder = proxyModel.sortOrder();\r
114                 sortByColumn(sortCol, sortOrder);\r
115 \r
116                 setSelectionBehavior(SelectionBehavior.SelectRows);\r
117                 setSelectionMode(SelectionMode.SingleSelection);\r
118                 verticalHeader().setVisible(false);\r
119                 hideColumn(Global.noteTableGuidPosition);  // Hide the guid column\r
120                 setShowGrid(false);\r
121                 setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers);\r
122                 \r
123                 QFontMetrics f = QApplication.fontMetrics();\r
124                 fontHeight = f.height();\r
125                 rowChanged = new Signal1<String>();\r
126                 resetViewport = new Signal0();\r
127         }\r
128         \r
129         // This should rescroll to the current item in the list when a column is \r
130         // sorted.  Somehow I can't get this to work, but this part is correct.\r
131         @SuppressWarnings("unused")\r
132         private void resetViewport() {\r
133 //              if (currentIndex() == null) \r
134 //                      return;\r
135                 \r
136 //              resetViewport.emit();\r
137         }\r
138         \r
139                 \r
140         public void load(boolean reload) {\r
141                 proxyModel.clear();\r
142                 setSortingEnabled(false);\r
143                 QFontMetrics f = QApplication.fontMetrics();\r
144                 verticalHeader().setDefaultSectionSize(f.height());\r
145                 for (int i=0; i<runner.getNoteIndex().size(); i++) {\r
146                         if (Global.showDeleted == true && !runner.getNoteIndex().get(i).isActive())\r
147                                 proxyModel.addGuid(runner.getNoteIndex().get(i).getGuid());\r
148                         if (!Global.showDeleted == true && runner.getNoteIndex().get(i).isActive())                     \r
149                                 proxyModel.addGuid(runner.getNoteIndex().get(i).getGuid());\r
150                 }\r
151 \r
152                 if (!reload) {\r
153                         logger.log(logger.EXTREME, "TableView.load() reload starting.");\r
154                         proxyModel.filter();\r
155                         logger.log(logger.EXTREME, "TableView.load() leaving reload.");\r
156                         return;\r
157                 }\r
158                 logger.log(logger.EXTREME, "TableView.load() Filling table data from scratch");\r
159         \r
160                 for (int i=0; i<runner.getMasterNoteIndex().size(); i++) {\r
161                         if (runner.getMasterNoteIndex().get(i) != null) {       \r
162                                 insertRow(runner.getMasterNoteIndex().get(i), false, i);                                                        \r
163                         }\r
164                 } \r
165                 proxyModel.invalidate();\r
166                 \r
167                 int width;\r
168                 width = Global.getColumnWidth("noteTableCreationPosition");\r
169                 if (width>0) setColumnWidth(Global.noteTableCreationPosition, width);\r
170                 width = Global.getColumnWidth("noteTableChangedPosition");\r
171                 if (width>0) setColumnWidth(Global.noteTableChangedPosition, width);\r
172                 width = Global.getColumnWidth("noteTableTitlePosition");\r
173                 if (width>0) setColumnWidth(Global.noteTableTitlePosition, width);\r
174                 width = Global.getColumnWidth("noteTableTagPosition");\r
175                 if (width>0) setColumnWidth(Global.noteTableTagPosition, width);\r
176                 width = Global.getColumnWidth("noteTableGuidPosition");\r
177                 if (width>0) setColumnWidth(Global.noteTableGuidPosition, width);\r
178                 width = Global.getColumnWidth("noteTableNotebookPosition");\r
179                 if (width>0) setColumnWidth(Global.noteTableNotebookPosition, width);\r
180                 width = Global.getColumnWidth("noteTableSourceUrlPosition");\r
181                 if (width>0) setColumnWidth(Global.noteTableSourceUrlPosition, width);\r
182                 width = Global.getColumnWidth("noteTableAuthorPosition");\r
183                 if (width>0) setColumnWidth(Global.noteTableAuthorPosition, width);\r
184                 width = Global.getColumnWidth("noteTableSubjectDatePosition");\r
185                 if (width>0) setColumnWidth(Global.noteTableSubjectDatePosition, width);\r
186                 width = Global.getColumnWidth("noteTableSynchronizedPosition");\r
187                 if (width>0) setColumnWidth(Global.noteTableSynchronizedPosition, width);\r
188                 \r
189                 int from = header.visualIndex(Global.noteTableCreationPosition);\r
190                 int to = Global.getColumnPosition("noteTableCreationPosition");\r
191                 if (to>=0) header.moveSection(from, to);\r
192 \r
193                 from = header.visualIndex(Global.noteTableTitlePosition);\r
194                 to = Global.getColumnPosition("noteTableTitlePosition");\r
195                 if (to>=0) header.moveSection(from, to);\r
196                 \r
197                 from = header.visualIndex(Global.noteTableTagPosition);\r
198                 to = Global.getColumnPosition("noteTableTagPosition");\r
199                 if (to>=0) header.moveSection(from, to);\r
200                 \r
201                 from = header.visualIndex(Global.noteTableNotebookPosition);\r
202                 to = Global.getColumnPosition("noteTableNotebookPosition");\r
203                 if (to>=0) header.moveSection(from, to);\r
204                 \r
205                 from = header.visualIndex(Global.noteTableChangedPosition);\r
206                 to = Global.getColumnPosition("noteTableChangedPosition");\r
207                 if (to>=0) header.moveSection(from, to);\r
208                 \r
209                 from = header.visualIndex(Global.noteTableSourceUrlPosition);\r
210                 to = Global.getColumnPosition("noteTableSourceUrlPosition");\r
211                 if (to>=0) header.moveSection(from, to);\r
212                 \r
213                 from = header.visualIndex(Global.noteTableAuthorPosition);\r
214                 to = Global.getColumnPosition("noteTableAuthorPosition");\r
215                 if (to>=0) header.moveSection(from, to);\r
216                 \r
217                 from = header.visualIndex(Global.noteTableSubjectDatePosition);\r
218                 to = Global.getColumnPosition("noteTableSubjectDatePosition");\r
219                 if (to>=0) header.moveSection(from, to);\r
220                 \r
221                 from = header.visualIndex(Global.noteTableSynchronizedPosition);\r
222                 to = Global.getColumnPosition("noteTableSynchronizedPosition");\r
223                 if (to>=0) header.moveSection(from, to);\r
224 \r
225                 proxyModel.filter();\r
226                 \r
227                 setSortingEnabled(true);\r
228                 resetViewport.emit();\r
229         }\r
230 \r
231         public void insertRow(Note tempNote, boolean newNote, int row) {\r
232                 if (newNote)\r
233                         proxyModel.addGuid(tempNote.getGuid());\r
234                 \r
235                 if (row > runner.getNoteTableModel().rowCount())\r
236                         runner.getNoteTableModel().insertRow(0);\r
237                 \r
238                 if (row < 0) {\r
239                         row  = runner.getNoteTableModel().rowCount();\r
240                         runner.getNoteTableModel().insertRow(row);\r
241                 }\r
242                 \r
243                 if (newNote) {\r
244                         QFontMetrics f = QApplication.fontMetrics();\r
245                         fontHeight = f.height();\r
246                         for (int i=0; i<runner.getNoteTableModel().rowCount(); i++)\r
247                                 setRowHeight(i, fontHeight);\r
248                 }\r
249         }\r
250         protected boolean filterAcceptsRow(int sourceRow, QModelIndex sourceParent) {\r
251                 return true;\r
252         }\r
253 \r
254         public void setAddAction(QAction a) {\r
255                 addAction = a;\r
256         }\r
257         \r
258         public void setMergeNotesAction(QAction a) {\r
259                 mergeNotesAction = a;\r
260         }\r
261         \r
262         public void setNoteHistoryAction(QAction a) {\r
263                 noteHistoryAction = a;\r
264         }\r
265         \r
266         public void setDeleteAction(QAction d) {\r
267                 deleteAction = d;\r
268         }\r
269          \r
270         public void setRestoreAction(QAction r) {\r
271                 restoreAction = r;\r
272         }\r
273         public void setNoteDuplicateAction(QAction d) {\r
274                 duplicateAction = d;\r
275         }\r
276         \r
277         @Override\r
278         public void keyPressEvent(QKeyEvent e) {\r
279                 if (e.matches(StandardKey.MoveToStartOfDocument)) {\r
280                         if (runner.getNoteTableModel().rowCount() > 0) {\r
281                                 clearSelection();\r
282                                 selectRow(0);\r
283                         }\r
284                 }\r
285                 if (e.matches(StandardKey.MoveToEndOfDocument)) {\r
286                         if (runner.getNoteTableModel().rowCount() > 0) {\r
287                                 clearSelection();\r
288                                 selectRow(model().rowCount()-1);\r
289                         }\r
290                 }\r
291                 super.keyPressEvent(e);\r
292         }\r
293         \r
294         @Override\r
295         public void contextMenuEvent(QContextMenuEvent event) {\r
296                 QMenu menu = new QMenu(this);\r
297                 if (Global.showDeleted) {\r
298                         menu.addAction(restoreAction);\r
299                 } else {\r
300                         menu.addAction(addAction);\r
301                 }\r
302                 menu.addAction(deleteAction);\r
303                 menu.addSeparator();\r
304                 menu.addAction(duplicateAction);\r
305                 menu.addAction(noteHistoryAction);\r
306                 menu.addAction(mergeNotesAction);\r
307                 \r
308                 QMenu titleColorMenu = new QMenu();\r
309                 titleColorMenu.setTitle("Title Color");\r
310                 menu.addMenu(titleColorMenu);\r
311                 noteTitleColorWhite = new QAction(titleColorMenu);\r
312             noteTitleColorRed = new QAction(titleColorMenu);\r
313             noteTitleColorBlue = new QAction(titleColorMenu);\r
314             noteTitleColorGreen = new QAction(titleColorMenu);\r
315             noteTitleColorYellow = new QAction(titleColorMenu);\r
316             noteTitleColorBlack = new QAction(titleColorMenu);\r
317             noteTitleColorGray = new QAction(titleColorMenu);\r
318             noteTitleColorCyan = new QAction(titleColorMenu);\r
319             noteTitleColorMagenta = new QAction(titleColorMenu);\r
320     \r
321             noteTitleColorWhite.setText(tr("White"));\r
322             noteTitleColorRed.setText(tr("Red"));\r
323             noteTitleColorBlue.setText(tr("Blue"));\r
324             noteTitleColorGreen.setText(tr("Green"));\r
325             noteTitleColorYellow.setText(tr("Yellow"));\r
326             noteTitleColorBlack.setText(tr("Black"));\r
327             noteTitleColorGray.setText(tr("Gray"));\r
328             noteTitleColorCyan.setText(tr("Cyan"));\r
329             noteTitleColorMagenta.setText(tr("Magenta"));\r
330             \r
331             titleColorMenu.addAction(noteTitleColorWhite);\r
332             titleColorMenu.addAction(noteTitleColorRed);\r
333             titleColorMenu.addAction(noteTitleColorBlue);\r
334             titleColorMenu.addAction(noteTitleColorGreen);\r
335             titleColorMenu.addAction(noteTitleColorYellow);\r
336             titleColorMenu.addAction(noteTitleColorBlack);\r
337             titleColorMenu.addAction(noteTitleColorGray);\r
338             titleColorMenu.addAction(noteTitleColorCyan);\r
339             titleColorMenu.addAction(noteTitleColorMagenta);\r
340             \r
341             noteTitleColorWhite.triggered.connect(this, "titleColorWhite()");\r
342             \r
343             noteTitleColorWhite.triggered.connect(this, "titleColorWhite()");\r
344             noteTitleColorRed.triggered.connect(this, "titleColorRed()");\r
345             noteTitleColorBlue.triggered.connect(this, "titleColorBlue()");\r
346             noteTitleColorGreen.triggered.connect(this, "titleColorGreen()");\r
347             noteTitleColorYellow.triggered.connect(this, "titleColorYellow()");\r
348             noteTitleColorBlack.triggered.connect(this, "titleColorBlack()");\r
349             noteTitleColorGray.triggered.connect(this, "titleColorGray()");\r
350             noteTitleColorCyan.triggered.connect(this, "titleColorCyan()");\r
351             noteTitleColorMagenta.triggered.connect(this, "titleColorMagenta()");\r
352             \r
353                 menu.exec(event.globalPos());\r
354         }\r
355         \r
356         \r
357     @SuppressWarnings("unused")\r
358         private void titleColorWhite() {noteSignal.titleColorChanged.emit(QColor.white.rgb());}\r
359     @SuppressWarnings("unused")\r
360         private void titleColorRed() {noteSignal.titleColorChanged.emit(QColor.red.rgb());}\r
361     @SuppressWarnings("unused")\r
362         private void titleColorBlue() {noteSignal.titleColorChanged.emit(QColor.blue.rgb());}\r
363     @SuppressWarnings("unused")\r
364         private void titleColorGreen() {noteSignal.titleColorChanged.emit(QColor.green.rgb());}\r
365     @SuppressWarnings("unused")\r
366         private void titleColorYellow(){noteSignal.titleColorChanged.emit(QColor.yellow.rgb());}\r
367     @SuppressWarnings("unused")\r
368         private void titleColorBlack() {noteSignal.titleColorChanged.emit(QColor.black.rgb());}\r
369     @SuppressWarnings("unused")\r
370         private void titleColorGray() {noteSignal.titleColorChanged.emit(QColor.gray.rgb());}\r
371     @SuppressWarnings("unused")\r
372         private void titleColorCyan() {noteSignal.titleColorChanged.emit(QColor.cyan.rgb());}\r
373     @SuppressWarnings("unused")\r
374         private void titleColorMagenta() {noteSignal.titleColorChanged.emit(QColor.magenta.rgb());}\r
375         \r
376         \r
377 \r
378         @Override\r
379         public void dragEnterEvent(QDragEnterEvent event) {\r
380                 StringBuffer guid = new StringBuffer(1000);\r
381                 \r
382                 showColumn(Global.noteTableGuidPosition);\r
383                 List<QModelIndex> selections = selectionModel().selectedRows();\r
384                 hideColumn(Global.noteTableGuidPosition);\r
385                 \r
386         if (selections.size() > 0) {\r
387                 QModelIndex index;\r
388                 for (int i=0; i<selections.size(); i++) {\r
389                         int row = selections.get(i).row();\r
390                         index = proxyModel.index(row, Global.noteTableGuidPosition);\r
391                         SortedMap<Integer, Object> ix = proxyModel.itemData(index);\r
392                         guid.append((String)ix.values().toArray()[0]);\r
393                         guid.append(" ");\r
394                 }\r
395         }\r
396                 event.mimeData().setData("application/x-nevernote-note", new QByteArray(guid.toString()));\r
397                 event.accept();\r
398                                 \r
399         }\r
400         \r
401         @Override\r
402         public void dropEvent(QDropEvent event) {\r
403                 if (event.source() == this)\r
404                         event.ignore();\r
405         }\r
406         \r
407         // Return a column width\r
408         public int getColumnWidth(int col) {\r
409                 return columnWidth(col);\r
410         }\r
411 \r
412 /*\r
413     @Override\r
414         public void scrollTo(final QModelIndex index, ScrollHint hint) {\r
415         QRect area = viewport().rect();\r
416         QRect rect = visualRect(index);\r
417 \r
418         if (rect.top() < area.top())\r
419             verticalScrollBar().setValue(\r
420                 verticalScrollBar().value() + rect.top() - area.top());\r
421         else if (rect.bottom() > area.bottom())\r
422             verticalScrollBar().setValue(\r
423                 verticalScrollBar().value() + Math.min(\r
424                     rect.bottom() - area.bottom(), rect.top() - area.top()));\r
425         update();\r
426     }\r
427     \r
428     @Override\r
429         protected void updateGeometries() {\r
430         verticalScrollBar().setPageStep(viewport().height());\r
431         verticalScrollBar().setRange(0, Math.max(0, viewport().height()));\r
432     }\r
433     @Override\r
434     protected int verticalOffset() {\r
435         return verticalScrollBar().value();\r
436     }\r
437 */\r
438 }\r