OSDN Git Service

f95a8447c147579abf906def561b12388e7a49b7
[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                         setSortingEnabled(true);\r
156                         logger.log(logger.EXTREME, "TableView.load() leaving reload.");\r
157                         return;\r
158                 }\r
159                 logger.log(logger.EXTREME, "TableView.load() Filling table data from scratch");\r
160         \r
161                 for (int i=0; i<runner.getMasterNoteIndex().size(); i++) {\r
162                         if (runner.getMasterNoteIndex().get(i) != null) {       \r
163                                 insertRow(runner.getMasterNoteIndex().get(i), false, i);                                                        \r
164                         }\r
165                 } \r
166                 proxyModel.invalidate();\r
167                 \r
168                 int width;\r
169                 width = Global.getColumnWidth("noteTableCreationPosition");\r
170                 if (width>0) setColumnWidth(Global.noteTableCreationPosition, width);\r
171                 width = Global.getColumnWidth("noteTableChangedPosition");\r
172                 if (width>0) setColumnWidth(Global.noteTableChangedPosition, width);\r
173                 width = Global.getColumnWidth("noteTableTitlePosition");\r
174                 if (width>0) setColumnWidth(Global.noteTableTitlePosition, width);\r
175                 width = Global.getColumnWidth("noteTableTagPosition");\r
176                 if (width>0) setColumnWidth(Global.noteTableTagPosition, width);\r
177                 width = Global.getColumnWidth("noteTableGuidPosition");\r
178                 if (width>0) setColumnWidth(Global.noteTableGuidPosition, width);\r
179                 width = Global.getColumnWidth("noteTableNotebookPosition");\r
180                 if (width>0) setColumnWidth(Global.noteTableNotebookPosition, width);\r
181                 width = Global.getColumnWidth("noteTableSourceUrlPosition");\r
182                 if (width>0) setColumnWidth(Global.noteTableSourceUrlPosition, width);\r
183                 width = Global.getColumnWidth("noteTableAuthorPosition");\r
184                 if (width>0) setColumnWidth(Global.noteTableAuthorPosition, width);\r
185                 width = Global.getColumnWidth("noteTableSubjectDatePosition");\r
186                 if (width>0) setColumnWidth(Global.noteTableSubjectDatePosition, width);\r
187                 width = Global.getColumnWidth("noteTableSynchronizedPosition");\r
188                 if (width>0) setColumnWidth(Global.noteTableSynchronizedPosition, width);\r
189                 \r
190                 int from = header.visualIndex(Global.noteTableCreationPosition);\r
191                 int to = Global.getColumnPosition("noteTableCreationPosition");\r
192                 if (to>=0) header.moveSection(from, to);\r
193 \r
194                 from = header.visualIndex(Global.noteTableTitlePosition);\r
195                 to = Global.getColumnPosition("noteTableTitlePosition");\r
196                 if (to>=0) header.moveSection(from, to);\r
197                 \r
198                 from = header.visualIndex(Global.noteTableTagPosition);\r
199                 to = Global.getColumnPosition("noteTableTagPosition");\r
200                 if (to>=0) header.moveSection(from, to);\r
201                 \r
202                 from = header.visualIndex(Global.noteTableNotebookPosition);\r
203                 to = Global.getColumnPosition("noteTableNotebookPosition");\r
204                 if (to>=0) header.moveSection(from, to);\r
205                 \r
206                 from = header.visualIndex(Global.noteTableChangedPosition);\r
207                 to = Global.getColumnPosition("noteTableChangedPosition");\r
208                 if (to>=0) header.moveSection(from, to);\r
209                 \r
210                 from = header.visualIndex(Global.noteTableSourceUrlPosition);\r
211                 to = Global.getColumnPosition("noteTableSourceUrlPosition");\r
212                 if (to>=0) header.moveSection(from, to);\r
213                 \r
214                 from = header.visualIndex(Global.noteTableAuthorPosition);\r
215                 to = Global.getColumnPosition("noteTableAuthorPosition");\r
216                 if (to>=0) header.moveSection(from, to);\r
217                 \r
218                 from = header.visualIndex(Global.noteTableSubjectDatePosition);\r
219                 to = Global.getColumnPosition("noteTableSubjectDatePosition");\r
220                 if (to>=0) header.moveSection(from, to);\r
221                 \r
222                 from = header.visualIndex(Global.noteTableSynchronizedPosition);\r
223                 to = Global.getColumnPosition("noteTableSynchronizedPosition");\r
224                 if (to>=0) header.moveSection(from, to);\r
225 \r
226                 proxyModel.filter();\r
227                 \r
228                 setSortingEnabled(true);\r
229                 resetViewport.emit();\r
230         }\r
231 \r
232         public void insertRow(Note tempNote, boolean newNote, int row) {\r
233                 if (newNote)\r
234                         proxyModel.addGuid(tempNote.getGuid());\r
235                 \r
236                 if (row > runner.getNoteTableModel().rowCount())\r
237                         runner.getNoteTableModel().insertRow(0);\r
238                 \r
239                 if (row < 0) {\r
240                         row  = runner.getNoteTableModel().rowCount();\r
241                         runner.getNoteTableModel().insertRow(row);\r
242                 }\r
243                 \r
244                 if (newNote) {\r
245                         QFontMetrics f = QApplication.fontMetrics();\r
246                         fontHeight = f.height();\r
247                         for (int i=0; i<runner.getNoteTableModel().rowCount(); i++)\r
248                                 setRowHeight(i, fontHeight);\r
249                 }\r
250         }\r
251         protected boolean filterAcceptsRow(int sourceRow, QModelIndex sourceParent) {\r
252                 return true;\r
253         }\r
254 \r
255         public void setAddAction(QAction a) {\r
256                 addAction = a;\r
257         }\r
258         \r
259         public void setMergeNotesAction(QAction a) {\r
260                 mergeNotesAction = a;\r
261         }\r
262         \r
263         public void setNoteHistoryAction(QAction a) {\r
264                 noteHistoryAction = a;\r
265         }\r
266         \r
267         public void setDeleteAction(QAction d) {\r
268                 deleteAction = d;\r
269         }\r
270          \r
271         public void setRestoreAction(QAction r) {\r
272                 restoreAction = r;\r
273         }\r
274         public void setNoteDuplicateAction(QAction d) {\r
275                 duplicateAction = d;\r
276         }\r
277         \r
278         @Override\r
279         public void keyPressEvent(QKeyEvent e) {\r
280                 if (e.matches(StandardKey.MoveToStartOfDocument)) {\r
281                         if (runner.getNoteTableModel().rowCount() > 0) {\r
282                                 clearSelection();\r
283                                 selectRow(0);\r
284                         }\r
285                 }\r
286                 if (e.matches(StandardKey.MoveToEndOfDocument)) {\r
287                         if (runner.getNoteTableModel().rowCount() > 0) {\r
288                                 clearSelection();\r
289                                 selectRow(model().rowCount()-1);\r
290                         }\r
291                 }\r
292                 super.keyPressEvent(e);\r
293         }\r
294         \r
295         @Override\r
296         public void contextMenuEvent(QContextMenuEvent event) {\r
297                 QMenu menu = new QMenu(this);\r
298                 if (Global.showDeleted) {\r
299                         menu.addAction(restoreAction);\r
300                 } else {\r
301                         menu.addAction(addAction);\r
302                 }\r
303                 menu.addAction(deleteAction);\r
304                 menu.addSeparator();\r
305                 menu.addAction(duplicateAction);\r
306                 menu.addAction(noteHistoryAction);\r
307                 menu.addAction(mergeNotesAction);\r
308                 \r
309                 QMenu titleColorMenu = new QMenu();\r
310                 titleColorMenu.setTitle("Title Color");\r
311                 menu.addMenu(titleColorMenu);\r
312                 noteTitleColorWhite = new QAction(titleColorMenu);\r
313             noteTitleColorRed = new QAction(titleColorMenu);\r
314             noteTitleColorBlue = new QAction(titleColorMenu);\r
315             noteTitleColorGreen = new QAction(titleColorMenu);\r
316             noteTitleColorYellow = new QAction(titleColorMenu);\r
317             noteTitleColorBlack = new QAction(titleColorMenu);\r
318             noteTitleColorGray = new QAction(titleColorMenu);\r
319             noteTitleColorCyan = new QAction(titleColorMenu);\r
320             noteTitleColorMagenta = new QAction(titleColorMenu);\r
321     \r
322             noteTitleColorWhite.setText(tr("White"));\r
323             noteTitleColorRed.setText(tr("Red"));\r
324             noteTitleColorBlue.setText(tr("Blue"));\r
325             noteTitleColorGreen.setText(tr("Green"));\r
326             noteTitleColorYellow.setText(tr("Yellow"));\r
327             noteTitleColorBlack.setText(tr("Black"));\r
328             noteTitleColorGray.setText(tr("Gray"));\r
329             noteTitleColorCyan.setText(tr("Cyan"));\r
330             noteTitleColorMagenta.setText(tr("Magenta"));\r
331             \r
332             titleColorMenu.addAction(noteTitleColorWhite);\r
333             titleColorMenu.addAction(noteTitleColorRed);\r
334             titleColorMenu.addAction(noteTitleColorBlue);\r
335             titleColorMenu.addAction(noteTitleColorGreen);\r
336             titleColorMenu.addAction(noteTitleColorYellow);\r
337             titleColorMenu.addAction(noteTitleColorBlack);\r
338             titleColorMenu.addAction(noteTitleColorGray);\r
339             titleColorMenu.addAction(noteTitleColorCyan);\r
340             titleColorMenu.addAction(noteTitleColorMagenta);\r
341             \r
342             noteTitleColorWhite.triggered.connect(this, "titleColorWhite()");\r
343             \r
344             noteTitleColorWhite.triggered.connect(this, "titleColorWhite()");\r
345             noteTitleColorRed.triggered.connect(this, "titleColorRed()");\r
346             noteTitleColorBlue.triggered.connect(this, "titleColorBlue()");\r
347             noteTitleColorGreen.triggered.connect(this, "titleColorGreen()");\r
348             noteTitleColorYellow.triggered.connect(this, "titleColorYellow()");\r
349             noteTitleColorBlack.triggered.connect(this, "titleColorBlack()");\r
350             noteTitleColorGray.triggered.connect(this, "titleColorGray()");\r
351             noteTitleColorCyan.triggered.connect(this, "titleColorCyan()");\r
352             noteTitleColorMagenta.triggered.connect(this, "titleColorMagenta()");\r
353             \r
354                 menu.exec(event.globalPos());\r
355         }\r
356         \r
357         \r
358     @SuppressWarnings("unused")\r
359         private void titleColorWhite() {noteSignal.titleColorChanged.emit(QColor.white.rgb());}\r
360     @SuppressWarnings("unused")\r
361         private void titleColorRed() {noteSignal.titleColorChanged.emit(QColor.red.rgb());}\r
362     @SuppressWarnings("unused")\r
363         private void titleColorBlue() {noteSignal.titleColorChanged.emit(QColor.blue.rgb());}\r
364     @SuppressWarnings("unused")\r
365         private void titleColorGreen() {noteSignal.titleColorChanged.emit(QColor.green.rgb());}\r
366     @SuppressWarnings("unused")\r
367         private void titleColorYellow(){noteSignal.titleColorChanged.emit(QColor.yellow.rgb());}\r
368     @SuppressWarnings("unused")\r
369         private void titleColorBlack() {noteSignal.titleColorChanged.emit(QColor.black.rgb());}\r
370     @SuppressWarnings("unused")\r
371         private void titleColorGray() {noteSignal.titleColorChanged.emit(QColor.gray.rgb());}\r
372     @SuppressWarnings("unused")\r
373         private void titleColorCyan() {noteSignal.titleColorChanged.emit(QColor.cyan.rgb());}\r
374     @SuppressWarnings("unused")\r
375         private void titleColorMagenta() {noteSignal.titleColorChanged.emit(QColor.magenta.rgb());}\r
376         \r
377         \r
378 \r
379         @Override\r
380         public void dragEnterEvent(QDragEnterEvent event) {\r
381                 StringBuffer guid = new StringBuffer(1000);\r
382                 \r
383                 showColumn(Global.noteTableGuidPosition);\r
384                 List<QModelIndex> selections = selectionModel().selectedRows();\r
385                 hideColumn(Global.noteTableGuidPosition);\r
386                 \r
387         if (selections.size() > 0) {\r
388                 QModelIndex index;\r
389                 for (int i=0; i<selections.size(); i++) {\r
390                         int row = selections.get(i).row();\r
391                         index = proxyModel.index(row, Global.noteTableGuidPosition);\r
392                         SortedMap<Integer, Object> ix = proxyModel.itemData(index);\r
393                         guid.append((String)ix.values().toArray()[0]);\r
394                         guid.append(" ");\r
395                 }\r
396         }\r
397                 event.mimeData().setData("application/x-nevernote-note", new QByteArray(guid.toString()));\r
398                 event.accept();\r
399                                 \r
400         }\r
401         \r
402         @Override\r
403         public void dropEvent(QDropEvent event) {\r
404                 if (event.source() == this)\r
405                         event.ignore();\r
406         }\r
407         \r
408         // Return a column width\r
409         public int getColumnWidth(int col) {\r
410                 return columnWidth(col);\r
411         }\r
412 \r
413 /*\r
414     @Override\r
415         public void scrollTo(final QModelIndex index, ScrollHint hint) {\r
416         QRect area = viewport().rect();\r
417         QRect rect = visualRect(index);\r
418 \r
419         if (rect.top() < area.top())\r
420             verticalScrollBar().setValue(\r
421                 verticalScrollBar().value() + rect.top() - area.top());\r
422         else if (rect.bottom() > area.bottom())\r
423             verticalScrollBar().setValue(\r
424                 verticalScrollBar().value() + Math.min(\r
425                     rect.bottom() - area.bottom(), rect.top() - area.top()));\r
426         update();\r
427     }\r
428     \r
429     @Override\r
430         protected void updateGeometries() {\r
431         verticalScrollBar().setPageStep(viewport().height());\r
432         verticalScrollBar().setRange(0, Math.max(0, viewport().height()));\r
433     }\r
434     @Override\r
435     protected int verticalOffset() {\r
436         return verticalScrollBar().value();\r
437     }\r
438 */\r
439 }\r