2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\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
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
19 package cx.fbn.nevernote.gui;
\r
21 import java.util.List;
\r
22 import java.util.SortedMap;
\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.Orientation;
\r
29 import com.trolltech.qt.core.Qt.SortOrder;
\r
30 import com.trolltech.qt.gui.QAbstractItemView;
\r
31 import com.trolltech.qt.gui.QAction;
\r
32 import com.trolltech.qt.gui.QApplication;
\r
33 import com.trolltech.qt.gui.QColor;
\r
34 import com.trolltech.qt.gui.QContextMenuEvent;
\r
35 import com.trolltech.qt.gui.QDragEnterEvent;
\r
36 import com.trolltech.qt.gui.QDropEvent;
\r
37 import com.trolltech.qt.gui.QFontMetrics;
\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
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
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
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
73 public TableViewHeader header;
\r
75 public Signal1<String> rowChanged;
\r
76 public Signal0 resetViewport;
\r
77 public NoteSignal noteSignal;
\r
79 public TableView(ApplicationLogger l, ListManager m) {
\r
81 header = new TableViewHeader(Orientation.Horizontal,this);
\r
82 setHorizontalHeader(header);
\r
83 header.setMovable(true);
\r
84 header.subjectDateAction.toggled.connect(this, "toggleSubjectDate(Boolean)");
\r
85 header.createdDateAction.toggled.connect(this, "toggleCreationDate(Boolean)");
\r
86 header.changedDateAction.toggled.connect(this, "toggleChangedDate(Boolean)");
\r
87 header.authorAction.toggled.connect(this, "toggleAuthor(Boolean)");
\r
88 header.urlAction.toggled.connect(this, "toggleSourceUrl(Boolean)");
\r
89 header.tagsAction.toggled.connect(this, "toggleTags(Boolean)");
\r
90 header.notebookAction.toggled.connect(this, "toggleNotebook(Boolean)");
\r
91 header.synchronizedAction.toggled.connect(this, "toggleSynchronized(Boolean)");
\r
93 noteSignal = new NoteSignal();
\r
94 setAcceptDrops(true);
\r
95 setDragEnabled(true);
\r
96 setDragDropMode(QAbstractItemView.DragDropMode.DragDrop);
\r
97 setDropIndicatorShown(false);
\r
101 runner.getNoteTableModel().setHeaderData(Global.noteTableCreationPosition, Qt.Orientation.Horizontal, tr("Date Created"), Qt.ItemDataRole.DisplayRole);
\r
102 runner.getNoteTableModel().setHeaderData(Global.noteTableTagPosition, Qt.Orientation.Horizontal, tr("Tags"), Qt.ItemDataRole.DisplayRole);
\r
103 runner.getNoteTableModel().setHeaderData(Global.noteTableGuidPosition, Qt.Orientation.Horizontal, tr("Guid"), Qt.ItemDataRole.DisplayRole);
\r
104 runner.getNoteTableModel().setHeaderData(Global.noteTableNotebookPosition, Qt.Orientation.Horizontal, tr("Notebook"), Qt.ItemDataRole.DisplayRole);
\r
105 runner.getNoteTableModel().setHeaderData(Global.noteTableTitlePosition, Qt.Orientation.Horizontal, tr("Title"), Qt.ItemDataRole.DisplayRole);
\r
106 runner.getNoteTableModel().setHeaderData(Global.noteTableChangedPosition, Qt.Orientation.Horizontal, tr("Date Changed"), Qt.ItemDataRole.DisplayRole);
\r
107 runner.getNoteTableModel().setHeaderData(Global.noteTableAuthorPosition, Qt.Orientation.Horizontal, tr("Author"), Qt.ItemDataRole.DisplayRole);
\r
108 runner.getNoteTableModel().setHeaderData(Global.noteTableSourceUrlPosition, Qt.Orientation.Horizontal, tr("Source Url"), Qt.ItemDataRole.DisplayRole);
\r
109 runner.getNoteTableModel().setHeaderData(Global.noteTableSubjectDatePosition, Qt.Orientation.Horizontal, tr("Subject Date"), Qt.ItemDataRole.DisplayRole);
\r
110 runner.getNoteTableModel().setHeaderData(Global.noteTableSynchronizedPosition, Qt.Orientation.Horizontal, tr("Synchronized"), Qt.ItemDataRole.DisplayRole);
\r
111 header.sortIndicatorChanged.connect(this, "resetViewport()");
\r
113 proxyModel = new NoteSortFilterProxyModel(this);
\r
114 proxyModel.setSourceModel(runner.getNoteTableModel());
\r
115 setAlternatingRowColors(false);
\r
116 setModel(proxyModel);
\r
117 // setModel(runner.getNoteTableModel());
\r
118 runner.getNoteTableModel().setSortProxyModel(proxyModel);
\r
120 setSortingEnabled(true);
\r
121 int sortCol = proxyModel.sortColumn();
\r
122 SortOrder sortOrder = proxyModel.sortOrder();
\r
123 sortByColumn(sortCol, sortOrder);
\r
125 setSelectionBehavior(SelectionBehavior.SelectRows);
\r
126 setSelectionMode(SelectionMode.SingleSelection);
\r
127 verticalHeader().setVisible(false);
\r
128 hideColumn(Global.noteTableGuidPosition); // Hide the guid column
\r
129 setShowGrid(false);
\r
130 setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers);
\r
132 QFontMetrics f = QApplication.fontMetrics();
\r
133 fontHeight = f.height();
\r
134 rowChanged = new Signal1<String>();
\r
135 resetViewport = new Signal0();
\r
137 NoteTableDateDelegate dateDelegate = new NoteTableDateDelegate();
\r
138 setItemDelegateForColumn(Global.noteTableCreationPosition, dateDelegate);
\r
139 setItemDelegateForColumn(Global.noteTableChangedPosition, dateDelegate);
\r
140 setItemDelegateForColumn(Global.noteTableSubjectDatePosition, dateDelegate);
\r
144 // This should rescroll to the current item in the list when a column is
\r
145 // sorted. Somehow I can't get this to work, but this part is correct.
\r
146 @SuppressWarnings("unused")
\r
147 private void resetViewport() {
\r
148 // if (currentIndex() == null)
\r
151 // resetViewport.emit();
\r
155 public void load(boolean reload) {
\r
156 proxyModel.clear();
\r
157 setSortingEnabled(false);
\r
158 QFontMetrics f = QApplication.fontMetrics();
\r
159 verticalHeader().setDefaultSectionSize(f.height());
\r
160 for (int i=0; i<runner.getNoteIndex().size(); i++) {
\r
161 if (Global.showDeleted == true && !runner.getNoteIndex().get(i).isActive())
\r
162 proxyModel.addGuid(runner.getNoteIndex().get(i).getGuid());
\r
163 if (!Global.showDeleted == true && runner.getNoteIndex().get(i).isActive())
\r
164 proxyModel.addGuid(runner.getNoteIndex().get(i).getGuid());
\r
168 logger.log(logger.EXTREME, "TableView.load() reload starting.");
\r
169 proxyModel.filter();
\r
170 setSortingEnabled(true);
\r
171 logger.log(logger.EXTREME, "TableView.load() leaving reload.");
\r
174 logger.log(logger.EXTREME, "TableView.load() Filling table data from scratch");
\r
176 for (int i=0; i<runner.getMasterNoteIndex().size(); i++) {
\r
177 if (runner.getMasterNoteIndex().get(i) != null) {
\r
178 insertRow(runner.getMasterNoteIndex().get(i), false, i);
\r
181 proxyModel.invalidate();
\r
184 width = Global.getColumnWidth("noteTableCreationPosition");
\r
185 if (width>0) setColumnWidth(Global.noteTableCreationPosition, width);
\r
186 width = Global.getColumnWidth("noteTableChangedPosition");
\r
187 if (width>0) setColumnWidth(Global.noteTableChangedPosition, width);
\r
188 width = Global.getColumnWidth("noteTableTitlePosition");
\r
189 if (width>0) setColumnWidth(Global.noteTableTitlePosition, width);
\r
190 width = Global.getColumnWidth("noteTableTagPosition");
\r
191 if (width>0) setColumnWidth(Global.noteTableTagPosition, width);
\r
192 width = Global.getColumnWidth("noteTableGuidPosition");
\r
193 if (width>0) setColumnWidth(Global.noteTableGuidPosition, width);
\r
194 width = Global.getColumnWidth("noteTableNotebookPosition");
\r
195 if (width>0) setColumnWidth(Global.noteTableNotebookPosition, width);
\r
196 width = Global.getColumnWidth("noteTableSourceUrlPosition");
\r
197 if (width>0) setColumnWidth(Global.noteTableSourceUrlPosition, width);
\r
198 width = Global.getColumnWidth("noteTableAuthorPosition");
\r
199 if (width>0) setColumnWidth(Global.noteTableAuthorPosition, width);
\r
200 width = Global.getColumnWidth("noteTableSubjectDatePosition");
\r
201 if (width>0) setColumnWidth(Global.noteTableSubjectDatePosition, width);
\r
202 width = Global.getColumnWidth("noteTableSynchronizedPosition");
\r
203 if (width>0) setColumnWidth(Global.noteTableSynchronizedPosition, width);
\r
205 int from = header.visualIndex(Global.noteTableCreationPosition);
\r
206 int to = Global.getColumnPosition("noteTableCreationPosition");
\r
207 if (to>=0) header.moveSection(from, to);
\r
209 from = header.visualIndex(Global.noteTableTitlePosition);
\r
210 to = Global.getColumnPosition("noteTableTitlePosition");
\r
211 if (to>=0) header.moveSection(from, to);
\r
213 from = header.visualIndex(Global.noteTableTagPosition);
\r
214 to = Global.getColumnPosition("noteTableTagPosition");
\r
215 if (to>=0) header.moveSection(from, to);
\r
217 from = header.visualIndex(Global.noteTableNotebookPosition);
\r
218 to = Global.getColumnPosition("noteTableNotebookPosition");
\r
219 if (to>=0) header.moveSection(from, to);
\r
221 from = header.visualIndex(Global.noteTableChangedPosition);
\r
222 to = Global.getColumnPosition("noteTableChangedPosition");
\r
223 if (to>=0) header.moveSection(from, to);
\r
225 from = header.visualIndex(Global.noteTableSourceUrlPosition);
\r
226 to = Global.getColumnPosition("noteTableSourceUrlPosition");
\r
227 if (to>=0) header.moveSection(from, to);
\r
229 from = header.visualIndex(Global.noteTableAuthorPosition);
\r
230 to = Global.getColumnPosition("noteTableAuthorPosition");
\r
231 if (to>=0) header.moveSection(from, to);
\r
233 from = header.visualIndex(Global.noteTableSubjectDatePosition);
\r
234 to = Global.getColumnPosition("noteTableSubjectDatePosition");
\r
235 if (to>=0) header.moveSection(from, to);
\r
237 from = header.visualIndex(Global.noteTableSynchronizedPosition);
\r
238 to = Global.getColumnPosition("noteTableSynchronizedPosition");
\r
239 if (to>=0) header.moveSection(from, to);
\r
241 proxyModel.filter();
\r
243 setSortingEnabled(true);
\r
244 resetViewport.emit();
\r
247 public void insertRow(Note tempNote, boolean newNote, int row) {
\r
249 proxyModel.addGuid(tempNote.getGuid());
\r
251 if (row > runner.getNoteTableModel().rowCount())
\r
252 runner.getNoteTableModel().insertRow(0);
\r
255 row = runner.getNoteTableModel().rowCount();
\r
256 runner.getNoteTableModel().insertRow(row);
\r
260 QFontMetrics f = QApplication.fontMetrics();
\r
261 fontHeight = f.height();
\r
262 for (int i=0; i<runner.getNoteTableModel().rowCount(); i++)
\r
263 setRowHeight(i, fontHeight);
\r
266 protected boolean filterAcceptsRow(int sourceRow, QModelIndex sourceParent) {
\r
270 public void setAddAction(QAction a) {
\r
274 public void setMergeNotesAction(QAction a) {
\r
275 mergeNotesAction = a;
\r
278 public void setNoteHistoryAction(QAction a) {
\r
279 noteHistoryAction = a;
\r
282 public void setDeleteAction(QAction d) {
\r
286 public void setRestoreAction(QAction r) {
\r
289 public void setNoteDuplicateAction(QAction d) {
\r
290 duplicateAction = d;
\r
294 public void keyPressEvent(QKeyEvent e) {
\r
295 if (e.matches(StandardKey.MoveToStartOfDocument)) {
\r
296 if (runner.getNoteTableModel().rowCount() > 0) {
\r
301 if (e.matches(StandardKey.MoveToEndOfDocument)) {
\r
302 if (runner.getNoteTableModel().rowCount() > 0) {
\r
304 selectRow(model().rowCount()-1);
\r
307 super.keyPressEvent(e);
\r
311 public void contextMenuEvent(QContextMenuEvent event) {
\r
312 QMenu menu = new QMenu(this);
\r
313 if (Global.showDeleted) {
\r
314 menu.addAction(restoreAction);
\r
316 menu.addAction(addAction);
\r
318 menu.addAction(deleteAction);
\r
319 menu.addSeparator();
\r
320 menu.addAction(duplicateAction);
\r
321 menu.addAction(noteHistoryAction);
\r
322 menu.addAction(mergeNotesAction);
\r
324 QMenu titleColorMenu = new QMenu();
\r
325 titleColorMenu.setTitle(tr("Title Color"));
\r
326 menu.addMenu(titleColorMenu);
\r
327 noteTitleColorWhite = new QAction(titleColorMenu);
\r
328 noteTitleColorRed = new QAction(titleColorMenu);
\r
329 noteTitleColorBlue = new QAction(titleColorMenu);
\r
330 noteTitleColorGreen = new QAction(titleColorMenu);
\r
331 noteTitleColorYellow = new QAction(titleColorMenu);
\r
332 noteTitleColorBlack = new QAction(titleColorMenu);
\r
333 noteTitleColorGray = new QAction(titleColorMenu);
\r
334 noteTitleColorCyan = new QAction(titleColorMenu);
\r
335 noteTitleColorMagenta = new QAction(titleColorMenu);
\r
337 noteTitleColorWhite.setText(tr("White"));
\r
338 noteTitleColorRed.setText(tr("Red"));
\r
339 noteTitleColorBlue.setText(tr("Blue"));
\r
340 noteTitleColorGreen.setText(tr("Green"));
\r
341 noteTitleColorYellow.setText(tr("Yellow"));
\r
342 noteTitleColorBlack.setText(tr("Black"));
\r
343 noteTitleColorGray.setText(tr("Gray"));
\r
344 noteTitleColorCyan.setText(tr("Cyan"));
\r
345 noteTitleColorMagenta.setText(tr("Magenta"));
\r
347 titleColorMenu.addAction(noteTitleColorWhite);
\r
348 titleColorMenu.addAction(noteTitleColorRed);
\r
349 titleColorMenu.addAction(noteTitleColorBlue);
\r
350 titleColorMenu.addAction(noteTitleColorGreen);
\r
351 titleColorMenu.addAction(noteTitleColorYellow);
\r
352 titleColorMenu.addAction(noteTitleColorBlack);
\r
353 titleColorMenu.addAction(noteTitleColorGray);
\r
354 titleColorMenu.addAction(noteTitleColorCyan);
\r
355 titleColorMenu.addAction(noteTitleColorMagenta);
\r
357 noteTitleColorWhite.triggered.connect(this, "titleColorWhite()");
\r
359 noteTitleColorWhite.triggered.connect(this, "titleColorWhite()");
\r
360 noteTitleColorRed.triggered.connect(this, "titleColorRed()");
\r
361 noteTitleColorBlue.triggered.connect(this, "titleColorBlue()");
\r
362 noteTitleColorGreen.triggered.connect(this, "titleColorGreen()");
\r
363 noteTitleColorYellow.triggered.connect(this, "titleColorYellow()");
\r
364 noteTitleColorBlack.triggered.connect(this, "titleColorBlack()");
\r
365 noteTitleColorGray.triggered.connect(this, "titleColorGray()");
\r
366 noteTitleColorCyan.triggered.connect(this, "titleColorCyan()");
\r
367 noteTitleColorMagenta.triggered.connect(this, "titleColorMagenta()");
\r
369 menu.exec(event.globalPos());
\r
373 @SuppressWarnings("unused")
\r
374 private void titleColorWhite() {noteSignal.titleColorChanged.emit(QColor.white.rgb());}
\r
375 @SuppressWarnings("unused")
\r
376 private void titleColorRed() {noteSignal.titleColorChanged.emit(QColor.red.rgb());}
\r
377 @SuppressWarnings("unused")
\r
378 private void titleColorBlue() {noteSignal.titleColorChanged.emit(QColor.blue.rgb());}
\r
379 @SuppressWarnings("unused")
\r
380 private void titleColorGreen() {noteSignal.titleColorChanged.emit(QColor.green.rgb());}
\r
381 @SuppressWarnings("unused")
\r
382 private void titleColorYellow(){noteSignal.titleColorChanged.emit(QColor.yellow.rgb());}
\r
383 @SuppressWarnings("unused")
\r
384 private void titleColorBlack() {noteSignal.titleColorChanged.emit(QColor.black.rgb());}
\r
385 @SuppressWarnings("unused")
\r
386 private void titleColorGray() {noteSignal.titleColorChanged.emit(QColor.gray.rgb());}
\r
387 @SuppressWarnings("unused")
\r
388 private void titleColorCyan() {noteSignal.titleColorChanged.emit(QColor.cyan.rgb());}
\r
389 @SuppressWarnings("unused")
\r
390 private void titleColorMagenta() {noteSignal.titleColorChanged.emit(QColor.magenta.rgb());}
\r
395 public void dragEnterEvent(QDragEnterEvent event) {
\r
396 StringBuffer guid = new StringBuffer(1000);
\r
398 showColumn(Global.noteTableGuidPosition);
\r
399 List<QModelIndex> selections = selectionModel().selectedRows();
\r
400 hideColumn(Global.noteTableGuidPosition);
\r
402 if (selections.size() > 0) {
\r
404 for (int i=0; i<selections.size(); i++) {
\r
405 int row = selections.get(i).row();
\r
406 index = proxyModel.index(row, Global.noteTableGuidPosition);
\r
407 SortedMap<Integer, Object> ix = proxyModel.itemData(index);
\r
408 guid.append((String)ix.values().toArray()[0]);
\r
412 event.mimeData().setData("application/x-nevernote-note", new QByteArray(guid.toString()));
\r
418 public void dropEvent(QDropEvent event) {
\r
419 if (event.source() == this)
\r
423 // Return a column width
\r
424 public int getColumnWidth(int col) {
\r
425 return columnWidth(col);
\r
430 public void scrollTo(final QModelIndex index, ScrollHint hint) {
\r
431 QRect area = viewport().rect();
\r
432 QRect rect = visualRect(index);
\r
434 if (rect.top() < area.top())
\r
435 verticalScrollBar().setValue(
\r
436 verticalScrollBar().value() + rect.top() - area.top());
\r
437 else if (rect.bottom() > area.bottom())
\r
438 verticalScrollBar().setValue(
\r
439 verticalScrollBar().value() + Math.min(
\r
440 rect.bottom() - area.bottom(), rect.top() - area.top()));
\r
445 protected void updateGeometries() {
\r
446 verticalScrollBar().setPageStep(viewport().height());
\r
447 verticalScrollBar().setRange(0, Math.max(0, viewport().height()));
\r
450 protected int verticalOffset() {
\r
451 return verticalScrollBar().value();
\r
455 public void toggleSubjectDate(Boolean toggle) {
\r
456 Global.saveColumnVisible("dateSubject", toggle);
\r
457 setColumnHidden(Global.noteTableSubjectDatePosition, !toggle);
\r
460 public void toggleChangedDate(Boolean toggle) {
\r
461 Global.saveColumnVisible("dateChanged", toggle);
\r
462 setColumnHidden(Global.noteTableChangedPosition, !toggle);
\r
466 public void toggleCreationDate(Boolean toggle) {
\r
467 Global.saveColumnVisible("dateCreated", toggle);
\r
468 setColumnHidden(Global.noteTableCreationPosition, !toggle);
\r
471 public void toggleSourceUrl(Boolean toggle) {
\r
472 Global.saveColumnVisible("sourceUrl", toggle);
\r
473 setColumnHidden(Global.noteTableSourceUrlPosition, !toggle);
\r
476 public void toggleAuthor(Boolean toggle) {
\r
477 Global.saveColumnVisible("author", toggle);
\r
478 setColumnHidden(Global.noteTableAuthorPosition, !toggle);
\r
481 public void toggleNotebook(Boolean toggle) {
\r
482 Global.saveColumnVisible("notebook", toggle);
\r
483 setColumnHidden(Global.noteTableNotebookPosition, !toggle);
\r
486 public void toggleTags(Boolean toggle) {
\r
487 Global.saveColumnVisible("tags", toggle);
\r
488 setColumnHidden(Global.noteTableTagPosition, !toggle);
\r
491 public void toggleSynchronized(Boolean toggle) {
\r
492 Global.saveColumnVisible("synchronized", toggle);
\r
493 setColumnHidden(Global.noteTableSynchronizedPosition, !toggle);
\r