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.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
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 QHeaderView 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 = horizontalHeader();
\r
82 header.setMovable(true);
\r
84 noteSignal = new NoteSignal();
\r
85 setAcceptDrops(true);
\r
86 setDragEnabled(true);
\r
87 setDragDropMode(QAbstractItemView.DragDropMode.DragDrop);
\r
88 setDropIndicatorShown(false);
\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
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
111 setSortingEnabled(true);
\r
112 int sortCol = proxyModel.sortColumn();
\r
113 SortOrder sortOrder = proxyModel.sortOrder();
\r
114 sortByColumn(sortCol, sortOrder);
\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
123 QFontMetrics f = QApplication.fontMetrics();
\r
124 fontHeight = f.height();
\r
125 rowChanged = new Signal1<String>();
\r
126 resetViewport = new Signal0();
\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
136 // resetViewport.emit();
\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
153 logger.log(logger.EXTREME, "TableView.load() reload starting.");
\r
154 proxyModel.filter();
\r
155 logger.log(logger.EXTREME, "TableView.load() leaving reload.");
\r
158 logger.log(logger.EXTREME, "TableView.load() Filling table data from scratch");
\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
165 proxyModel.invalidate();
\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
189 int from = header.visualIndex(Global.noteTableCreationPosition);
\r
190 int to = Global.getColumnPosition("noteTableCreationPosition");
\r
191 if (to>=0) header.moveSection(from, to);
\r
193 from = header.visualIndex(Global.noteTableTitlePosition);
\r
194 to = Global.getColumnPosition("noteTableTitlePosition");
\r
195 if (to>=0) header.moveSection(from, to);
\r
197 from = header.visualIndex(Global.noteTableTagPosition);
\r
198 to = Global.getColumnPosition("noteTableTagPosition");
\r
199 if (to>=0) header.moveSection(from, to);
\r
201 from = header.visualIndex(Global.noteTableNotebookPosition);
\r
202 to = Global.getColumnPosition("noteTableNotebookPosition");
\r
203 if (to>=0) header.moveSection(from, to);
\r
205 from = header.visualIndex(Global.noteTableChangedPosition);
\r
206 to = Global.getColumnPosition("noteTableChangedPosition");
\r
207 if (to>=0) header.moveSection(from, to);
\r
209 from = header.visualIndex(Global.noteTableSourceUrlPosition);
\r
210 to = Global.getColumnPosition("noteTableSourceUrlPosition");
\r
211 if (to>=0) header.moveSection(from, to);
\r
213 from = header.visualIndex(Global.noteTableAuthorPosition);
\r
214 to = Global.getColumnPosition("noteTableAuthorPosition");
\r
215 if (to>=0) header.moveSection(from, to);
\r
217 from = header.visualIndex(Global.noteTableSubjectDatePosition);
\r
218 to = Global.getColumnPosition("noteTableSubjectDatePosition");
\r
219 if (to>=0) header.moveSection(from, to);
\r
221 from = header.visualIndex(Global.noteTableSynchronizedPosition);
\r
222 to = Global.getColumnPosition("noteTableSynchronizedPosition");
\r
223 if (to>=0) header.moveSection(from, to);
\r
225 proxyModel.filter();
\r
227 setSortingEnabled(true);
\r
228 resetViewport.emit();
\r
231 public void insertRow(Note tempNote, boolean newNote, int row) {
\r
233 proxyModel.addGuid(tempNote.getGuid());
\r
235 if (row > runner.getNoteTableModel().rowCount())
\r
236 runner.getNoteTableModel().insertRow(0);
\r
239 row = runner.getNoteTableModel().rowCount();
\r
240 runner.getNoteTableModel().insertRow(row);
\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
250 protected boolean filterAcceptsRow(int sourceRow, QModelIndex sourceParent) {
\r
254 public void setAddAction(QAction a) {
\r
258 public void setMergeNotesAction(QAction a) {
\r
259 mergeNotesAction = a;
\r
262 public void setNoteHistoryAction(QAction a) {
\r
263 noteHistoryAction = a;
\r
266 public void setDeleteAction(QAction d) {
\r
270 public void setRestoreAction(QAction r) {
\r
273 public void setNoteDuplicateAction(QAction d) {
\r
274 duplicateAction = d;
\r
278 public void keyPressEvent(QKeyEvent e) {
\r
279 if (e.matches(StandardKey.MoveToStartOfDocument)) {
\r
280 if (runner.getNoteTableModel().rowCount() > 0) {
\r
285 if (e.matches(StandardKey.MoveToEndOfDocument)) {
\r
286 if (runner.getNoteTableModel().rowCount() > 0) {
\r
288 selectRow(model().rowCount()-1);
\r
291 super.keyPressEvent(e);
\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
300 menu.addAction(addAction);
\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
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
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
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
341 noteTitleColorWhite.triggered.connect(this, "titleColorWhite()");
\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
353 menu.exec(event.globalPos());
\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
379 public void dragEnterEvent(QDragEnterEvent event) {
\r
380 StringBuffer guid = new StringBuffer(1000);
\r
382 showColumn(Global.noteTableGuidPosition);
\r
383 List<QModelIndex> selections = selectionModel().selectedRows();
\r
384 hideColumn(Global.noteTableGuidPosition);
\r
386 if (selections.size() > 0) {
\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
396 event.mimeData().setData("application/x-nevernote-note", new QByteArray(guid.toString()));
\r
402 public void dropEvent(QDropEvent event) {
\r
403 if (event.source() == this)
\r
407 // Return a column width
\r
408 public int getColumnWidth(int col) {
\r
409 return columnWidth(col);
\r
414 public void scrollTo(final QModelIndex index, ScrollHint hint) {
\r
415 QRect area = viewport().rect();
\r
416 QRect rect = visualRect(index);
\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
429 protected void updateGeometries() {
\r
430 verticalScrollBar().setPageStep(viewport().height());
\r
431 verticalScrollBar().setRange(0, Math.max(0, viewport().height()));
\r
434 protected int verticalOffset() {
\r
435 return verticalScrollBar().value();
\r