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 for (int i=0; i<runner.getNoteIndex().size(); i++) {
\r
144 if (Global.showDeleted == true && !runner.getNoteIndex().get(i).isActive())
\r
145 proxyModel.addGuid(runner.getNoteIndex().get(i).getGuid());
\r
146 if (!Global.showDeleted == true && runner.getNoteIndex().get(i).isActive())
\r
147 proxyModel.addGuid(runner.getNoteIndex().get(i).getGuid());
\r
151 logger.log(logger.EXTREME, "TableView.load() reload starting.");
\r
152 proxyModel.filter();
\r
153 logger.log(logger.EXTREME, "TableView.load() leaving reload.");
\r
154 QFontMetrics f = QApplication.fontMetrics();
\r
155 fontHeight = f.height();
\r
156 for (int i=0; i<runner.getNoteTableModel().rowCount(); i++)
\r
157 setRowHeight(i, fontHeight);
\r
158 proxyModel.invalidate();
\r
159 setSortingEnabled(true);
\r
160 resetViewport.emit();
\r
163 logger.log(logger.EXTREME, "TableView.load() Filling table data from scratch");
\r
165 for (int i=0; i<runner.getMasterNoteIndex().size(); i++) {
\r
166 if (runner.getMasterNoteIndex().get(i) != null) {
\r
167 insertRow(runner.getMasterNoteIndex().get(i), false, i);
\r
170 proxyModel.invalidate();
\r
173 width = Global.getColumnWidth("noteTableCreationPosition");
\r
174 if (width>0) setColumnWidth(Global.noteTableCreationPosition, width);
\r
175 width = Global.getColumnWidth("noteTableChangedPosition");
\r
176 if (width>0) setColumnWidth(Global.noteTableChangedPosition, width);
\r
177 width = Global.getColumnWidth("noteTableTitlePosition");
\r
178 if (width>0) setColumnWidth(Global.noteTableTitlePosition, width);
\r
179 width = Global.getColumnWidth("noteTableTagPosition");
\r
180 if (width>0) setColumnWidth(Global.noteTableTagPosition, width);
\r
181 width = Global.getColumnWidth("noteTableGuidPosition");
\r
182 if (width>0) setColumnWidth(Global.noteTableGuidPosition, width);
\r
183 width = Global.getColumnWidth("noteTableNotebookPosition");
\r
184 if (width>0) setColumnWidth(Global.noteTableNotebookPosition, width);
\r
185 width = Global.getColumnWidth("noteTableSourceUrlPosition");
\r
186 if (width>0) setColumnWidth(Global.noteTableSourceUrlPosition, width);
\r
187 width = Global.getColumnWidth("noteTableAuthorPosition");
\r
188 if (width>0) setColumnWidth(Global.noteTableAuthorPosition, width);
\r
189 width = Global.getColumnWidth("noteTableSubjectDatePosition");
\r
190 if (width>0) setColumnWidth(Global.noteTableSubjectDatePosition, width);
\r
191 width = Global.getColumnWidth("noteTableSynchronizedPosition");
\r
192 if (width>0) setColumnWidth(Global.noteTableSynchronizedPosition, width);
\r
194 int from = header.visualIndex(Global.noteTableCreationPosition);
\r
195 int to = Global.getColumnPosition("noteTableCreationPosition");
\r
196 if (to>=0) header.moveSection(from, to);
\r
198 from = header.visualIndex(Global.noteTableTitlePosition);
\r
199 to = Global.getColumnPosition("noteTableTitlePosition");
\r
200 if (to>=0) header.moveSection(from, to);
\r
202 from = header.visualIndex(Global.noteTableTagPosition);
\r
203 to = Global.getColumnPosition("noteTableTagPosition");
\r
204 if (to>=0) header.moveSection(from, to);
\r
206 from = header.visualIndex(Global.noteTableNotebookPosition);
\r
207 to = Global.getColumnPosition("noteTableNotebookPosition");
\r
208 if (to>=0) header.moveSection(from, to);
\r
210 from = header.visualIndex(Global.noteTableChangedPosition);
\r
211 to = Global.getColumnPosition("noteTableChangedPosition");
\r
212 if (to>=0) header.moveSection(from, to);
\r
214 from = header.visualIndex(Global.noteTableSourceUrlPosition);
\r
215 to = Global.getColumnPosition("noteTableSourceUrlPosition");
\r
216 if (to>=0) header.moveSection(from, to);
\r
218 from = header.visualIndex(Global.noteTableAuthorPosition);
\r
219 to = Global.getColumnPosition("noteTableAuthorPosition");
\r
220 if (to>=0) header.moveSection(from, to);
\r
222 from = header.visualIndex(Global.noteTableSubjectDatePosition);
\r
223 to = Global.getColumnPosition("noteTableSubjectDatePosition");
\r
224 if (to>=0) header.moveSection(from, to);
\r
226 from = header.visualIndex(Global.noteTableSynchronizedPosition);
\r
227 to = Global.getColumnPosition("noteTableSynchronizedPosition");
\r
228 if (to>=0) header.moveSection(from, to);
\r
230 proxyModel.filter();
\r
232 QFontMetrics f = QApplication.fontMetrics();
\r
233 fontHeight = f.height();
\r
234 for (int i=0; i<runner.getNoteTableModel().rowCount(); i++)
\r
235 setRowHeight(i, fontHeight);
\r
237 setSortingEnabled(true);
\r
238 resetViewport.emit();
\r
241 public void insertRow(Note tempNote, boolean newNote, int row) {
\r
243 proxyModel.addGuid(tempNote.getGuid());
\r
245 if (row > runner.getNoteTableModel().rowCount())
\r
246 runner.getNoteTableModel().insertRow(0);
\r
249 row = runner.getNoteTableModel().rowCount();
\r
250 runner.getNoteTableModel().insertRow(row);
\r
254 QFontMetrics f = QApplication.fontMetrics();
\r
255 fontHeight = f.height();
\r
256 for (int i=0; i<runner.getNoteTableModel().rowCount(); i++)
\r
257 setRowHeight(i, fontHeight);
\r
260 protected boolean filterAcceptsRow(int sourceRow, QModelIndex sourceParent) {
\r
264 public void setAddAction(QAction a) {
\r
268 public void setMergeNotesAction(QAction a) {
\r
269 mergeNotesAction = a;
\r
272 public void setNoteHistoryAction(QAction a) {
\r
273 noteHistoryAction = a;
\r
276 public void setDeleteAction(QAction d) {
\r
280 public void setRestoreAction(QAction r) {
\r
283 public void setNoteDuplicateAction(QAction d) {
\r
284 duplicateAction = d;
\r
288 public void keyPressEvent(QKeyEvent e) {
\r
289 if (e.matches(StandardKey.MoveToStartOfDocument)) {
\r
290 if (runner.getNoteTableModel().rowCount() > 0) {
\r
295 if (e.matches(StandardKey.MoveToEndOfDocument)) {
\r
296 if (runner.getNoteTableModel().rowCount() > 0) {
\r
298 selectRow(model().rowCount()-1);
\r
301 super.keyPressEvent(e);
\r
305 public void contextMenuEvent(QContextMenuEvent event) {
\r
306 QMenu menu = new QMenu(this);
\r
307 if (Global.showDeleted) {
\r
308 menu.addAction(restoreAction);
\r
310 menu.addAction(addAction);
\r
312 menu.addAction(deleteAction);
\r
313 menu.addSeparator();
\r
314 menu.addAction(duplicateAction);
\r
315 menu.addAction(noteHistoryAction);
\r
316 menu.addAction(mergeNotesAction);
\r
318 QMenu titleColorMenu = new QMenu();
\r
319 titleColorMenu.setTitle("Title Color");
\r
320 menu.addMenu(titleColorMenu);
\r
321 noteTitleColorWhite = new QAction(titleColorMenu);
\r
322 noteTitleColorRed = new QAction(titleColorMenu);
\r
323 noteTitleColorBlue = new QAction(titleColorMenu);
\r
324 noteTitleColorGreen = new QAction(titleColorMenu);
\r
325 noteTitleColorYellow = new QAction(titleColorMenu);
\r
326 noteTitleColorBlack = new QAction(titleColorMenu);
\r
327 noteTitleColorGray = new QAction(titleColorMenu);
\r
328 noteTitleColorCyan = new QAction(titleColorMenu);
\r
329 noteTitleColorMagenta = new QAction(titleColorMenu);
\r
331 noteTitleColorWhite.setText(tr("White"));
\r
332 noteTitleColorRed.setText(tr("Red"));
\r
333 noteTitleColorBlue.setText(tr("Blue"));
\r
334 noteTitleColorGreen.setText(tr("Green"));
\r
335 noteTitleColorYellow.setText(tr("Yellow"));
\r
336 noteTitleColorBlack.setText(tr("Black"));
\r
337 noteTitleColorGray.setText(tr("Gray"));
\r
338 noteTitleColorCyan.setText(tr("Cyan"));
\r
339 noteTitleColorMagenta.setText(tr("Magenta"));
\r
341 titleColorMenu.addAction(noteTitleColorWhite);
\r
342 titleColorMenu.addAction(noteTitleColorRed);
\r
343 titleColorMenu.addAction(noteTitleColorBlue);
\r
344 titleColorMenu.addAction(noteTitleColorGreen);
\r
345 titleColorMenu.addAction(noteTitleColorYellow);
\r
346 titleColorMenu.addAction(noteTitleColorBlack);
\r
347 titleColorMenu.addAction(noteTitleColorGray);
\r
348 titleColorMenu.addAction(noteTitleColorCyan);
\r
349 titleColorMenu.addAction(noteTitleColorMagenta);
\r
351 noteTitleColorWhite.triggered.connect(this, "titleColorWhite()");
\r
353 noteTitleColorWhite.triggered.connect(this, "titleColorWhite()");
\r
354 noteTitleColorRed.triggered.connect(this, "titleColorRed()");
\r
355 noteTitleColorBlue.triggered.connect(this, "titleColorBlue()");
\r
356 noteTitleColorGreen.triggered.connect(this, "titleColorGreen()");
\r
357 noteTitleColorYellow.triggered.connect(this, "titleColorYellow()");
\r
358 noteTitleColorBlack.triggered.connect(this, "titleColorBlack()");
\r
359 noteTitleColorGray.triggered.connect(this, "titleColorGray()");
\r
360 noteTitleColorCyan.triggered.connect(this, "titleColorCyan()");
\r
361 noteTitleColorMagenta.triggered.connect(this, "titleColorMagenta()");
\r
363 menu.exec(event.globalPos());
\r
367 @SuppressWarnings("unused")
\r
368 private void titleColorWhite() {noteSignal.titleColorChanged.emit(QColor.white.rgb());}
\r
369 @SuppressWarnings("unused")
\r
370 private void titleColorRed() {noteSignal.titleColorChanged.emit(QColor.red.rgb());}
\r
371 @SuppressWarnings("unused")
\r
372 private void titleColorBlue() {noteSignal.titleColorChanged.emit(QColor.blue.rgb());}
\r
373 @SuppressWarnings("unused")
\r
374 private void titleColorGreen() {noteSignal.titleColorChanged.emit(QColor.green.rgb());}
\r
375 @SuppressWarnings("unused")
\r
376 private void titleColorYellow(){noteSignal.titleColorChanged.emit(QColor.yellow.rgb());}
\r
377 @SuppressWarnings("unused")
\r
378 private void titleColorBlack() {noteSignal.titleColorChanged.emit(QColor.black.rgb());}
\r
379 @SuppressWarnings("unused")
\r
380 private void titleColorGray() {noteSignal.titleColorChanged.emit(QColor.gray.rgb());}
\r
381 @SuppressWarnings("unused")
\r
382 private void titleColorCyan() {noteSignal.titleColorChanged.emit(QColor.cyan.rgb());}
\r
383 @SuppressWarnings("unused")
\r
384 private void titleColorMagenta() {noteSignal.titleColorChanged.emit(QColor.magenta.rgb());}
\r
389 public void dragEnterEvent(QDragEnterEvent event) {
\r
390 StringBuffer guid = new StringBuffer(1000);
\r
392 showColumn(Global.noteTableGuidPosition);
\r
393 List<QModelIndex> selections = selectionModel().selectedRows();
\r
394 hideColumn(Global.noteTableGuidPosition);
\r
396 if (selections.size() > 0) {
\r
398 for (int i=0; i<selections.size(); i++) {
\r
399 int row = selections.get(i).row();
\r
400 index = proxyModel.index(row, Global.noteTableGuidPosition);
\r
401 SortedMap<Integer, Object> ix = proxyModel.itemData(index);
\r
402 guid.append((String)ix.values().toArray()[0]);
\r
406 event.mimeData().setData("application/x-nevernote-note", new QByteArray(guid.toString()));
\r
412 public void dropEvent(QDropEvent event) {
\r
413 if (event.source() == this)
\r
417 // Return a column width
\r
418 public int getColumnWidth(int col) {
\r
419 return columnWidth(col);
\r
424 public void scrollTo(final QModelIndex index, ScrollHint hint) {
\r
425 QRect area = viewport().rect();
\r
426 QRect rect = visualRect(index);
\r
428 if (rect.top() < area.top())
\r
429 verticalScrollBar().setValue(
\r
430 verticalScrollBar().value() + rect.top() - area.top());
\r
431 else if (rect.bottom() > area.bottom())
\r
432 verticalScrollBar().setValue(
\r
433 verticalScrollBar().value() + Math.min(
\r
434 rect.bottom() - area.bottom(), rect.top() - area.top()));
\r
439 protected void updateGeometries() {
\r
440 verticalScrollBar().setPageStep(viewport().height());
\r
441 verticalScrollBar().setRange(0, Math.max(0, viewport().height()));
\r
444 protected int verticalOffset() {
\r
445 return verticalScrollBar().value();
\r