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
20 package cx.fbn.nevernote.gui;
\r
22 import java.util.ArrayList;
\r
23 import java.util.List;
\r
25 import com.evernote.edam.type.Notebook;
\r
26 import com.trolltech.qt.core.QByteArray;
\r
27 import com.trolltech.qt.core.QMimeData;
\r
28 import com.trolltech.qt.core.Qt;
\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.QBrush;
\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.QDragMoveEvent;
\r
37 import com.trolltech.qt.gui.QHeaderView;
\r
38 import com.trolltech.qt.gui.QIcon;
\r
39 import com.trolltech.qt.gui.QMenu;
\r
40 import com.trolltech.qt.gui.QTreeWidget;
\r
41 import com.trolltech.qt.gui.QTreeWidgetItem;
\r
43 import cx.fbn.nevernote.Global;
\r
44 import cx.fbn.nevernote.filters.NotebookCounter;
\r
45 import cx.fbn.nevernote.signals.NoteSignal;
\r
47 public class NotebookTreeWidget extends QTreeWidget {
\r
48 private QAction deleteAction;
\r
49 private QAction addAction;
\r
50 private QAction editAction;
\r
51 public NoteSignal noteSignal;
\r
52 // private final QTreeWidgetItem previousMouseOver;
\r
53 // private boolean previousMouseOverWasSelected;
\r
55 public void setAddAction(QAction a) {
\r
59 public void setDeleteAction(QAction d) {
\r
63 public void setEditAction(QAction e) {
\r
67 public NotebookTreeWidget() {
\r
68 noteSignal = new NoteSignal();
\r
69 setProperty("hideTree", true);
\r
70 List<String> labels = new ArrayList<String>();
\r
71 labels.add("Notebooks");
\r
73 setAcceptDrops(true);
\r
74 setDragEnabled(true);
\r
76 header().setResizeMode(0, QHeaderView.ResizeMode.ResizeToContents);
\r
77 header().setResizeMode(1, QHeaderView.ResizeMode.Stretch);
\r
78 header().setMovable(false);
\r
79 setHeaderLabels(labels);
\r
80 setDragDropMode(QAbstractItemView.DragDropMode.DragDrop);
\r
81 // If we want to mimic Evernote's notebook selection
\r
82 if (Global.mimicEvernoteInterface) {
\r
83 setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection);
\r
85 setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection);
\r
87 // int width = Global.getColumnWidth("notebookTreeName");
\r
89 // setColumnWidth(0, width);
\r
90 // previousMouseOver = new QTreeWidgetItem();
\r
93 public void selectNotebook(QTreeWidgetItem item) {
\r
94 QTreeWidgetItem root = invisibleRootItem();
\r
95 QTreeWidgetItem child;
\r
97 for (int i=0; i<root.childCount(); i++) {
\r
98 child = root.child(i);
\r
99 if (child.text(2).equals(item.text(2))) {
\r
100 child.setSelected(true);
\r
107 public boolean selectGuid(String guid) {
\r
108 QTreeWidgetItem root = invisibleRootItem();
\r
109 QTreeWidgetItem child;
\r
111 for (int i=0; i<root.childCount(); i++) {
\r
112 child = root.child(i);
\r
113 if (child.text(2).equals(guid)) {
\r
114 child.setSelected(true);
\r
122 public void load(List<Notebook> books, List<String> localBooks) {
\r
124 QTreeWidgetItem child;
\r
126 String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
\r
127 QIcon blueIcon = new QIcon(iconPath+"notebook-blue.png");
\r
128 QIcon greenIcon = new QIcon(iconPath+"notebook-green.png");
\r
129 QIcon redIcon = new QIcon(iconPath+"notebook-red.png");
\r
130 QIcon yellowIcon = new QIcon(iconPath+"notebook-yellow.png");
\r
134 Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);
\r
135 for (int i=0; i<books.size(); i++) {
\r
136 book = books.get(i);
\r
137 child = new QTreeWidgetItem();
\r
138 child.setText(0, book.getName());
\r
139 child.setIcon(0, greenIcon);
\r
140 if (localBooks.contains(book.getGuid()))
\r
141 child.setIcon(0, yellowIcon);
\r
142 if (localBooks.contains(book.getGuid()) &&
\r
143 (book.getName().equalsIgnoreCase("Conflicting Changes") ||
\r
144 book.getName().equalsIgnoreCase("Conflicting Changes (Local)")))
\r
145 child.setIcon(0, redIcon);
\r
146 if (book.isPublished())
\r
147 child.setIcon(0, blueIcon);
\r
148 child.setTextAlignment(1, ra.value());
\r
149 child.setText(2, book.getGuid());
\r
150 addTopLevelItem(child);
\r
153 sortItems(0, SortOrder.AscendingOrder);
\r
154 if (Global.mimicEvernoteInterface) {
\r
155 child = new QTreeWidgetItem();
\r
156 child.setIcon(0, greenIcon);
\r
157 child.setText(0, "All Notebooks");
\r
158 // child.setText(1, "0");
\r
159 child.setText(2, "");
\r
160 child.setTextAlignment(1, ra.value());
\r
161 insertTopLevelItem(0,child);
\r
163 resizeColumnToContents(0);
\r
164 resizeColumnToContents(1);
\r
166 // update the display with the current number of notes
\r
167 public void updateCounts(List<Notebook> books, List<NotebookCounter> counts) {
\r
168 QTreeWidgetItem root = invisibleRootItem();
\r
169 QTreeWidgetItem child;
\r
171 QBrush blue = new QBrush();
\r
172 QBrush black = new QBrush();
\r
173 black.setColor(QColor.black);
\r
174 if (Global.tagBehavior().equalsIgnoreCase("ColorActive") && !Global.mimicEvernoteInterface)
\r
175 blue.setColor(QColor.blue);
\r
177 blue.setColor(QColor.black);
\r
180 // for (int i=0; i<counts.size(); i++) {
\r
181 // total=total+counts.get(i).getCount();
\r
184 int size = books.size();
\r
185 if (Global.mimicEvernoteInterface)
\r
188 for (int i=0; i<size; i++) {
\r
189 child = root.child(i);
\r
190 if (child != null) {
\r
191 String guid = child.text(2);
\r
192 child.setText(1,"0");
\r
193 child.setForeground(0, black);
\r
194 child.setForeground(1, black);
\r
195 for (int j=0; j<counts.size(); j++) {
\r
196 if (counts.get(j).getGuid().equals(guid)) {
\r
197 child.setText(1, new Integer(counts.get(j).getCount()).toString());
\r
198 total = total+counts.get(j).getCount();
\r
199 if (counts.get(j).getCount() > 0) {
\r
200 child.setForeground(0, blue);
\r
201 child.setForeground(1, blue);
\r
204 // if (guid.equals("") && Global.mimicEvernoteInterface) {
\r
205 // child.setText(1, new Integer(total).toString());
\r
211 for (int i=0; i<size; i++) {
\r
212 child = root.child(i);
\r
213 if (child != null) {
\r
214 String guid = child.text(2);
\r
215 if (guid.equals("") && Global.mimicEvernoteInterface)
\r
216 child.setText(1, new Integer(total).toString());
\r
220 // Return a list of the notebook guids, ordered by the current display order.
\r
221 public List<String> getNotebookGuids() {
\r
222 List<String> names = new ArrayList<String>();
\r
223 QTreeWidgetItem root = invisibleRootItem();
\r
224 QTreeWidgetItem child;
\r
225 for (int i=0; i<root.childCount(); i++) {
\r
226 child = root.child(i);
\r
227 String text = child.text(2);
\r
234 public void contextMenuEvent(QContextMenuEvent event) {
\r
235 QMenu menu = new QMenu(this);
\r
236 menu.addAction(addAction);
\r
237 menu.addAction(editAction);
\r
238 menu.addAction(deleteAction);
\r
239 menu.exec(event.globalPos());
\r
244 public void dragEnterEvent(QDragEnterEvent event) {
\r
245 if (event.mimeData().hasFormat("application/x-nevernote-note")) {
\r
253 protected void dragMoveEvent(QDragMoveEvent event) {
\r
254 // if (event.mimeData().hasFormat("text/plain") &&
\r
255 //event.answerRect().intersects(dropFrame.geometry()))
\r
256 QTreeWidgetItem treeItem = itemAt(event.pos().x(), event.pos().y());
\r
257 if (treeItem != null) {
\r
258 /* if (!previousMouseOver.text(0).equalsIgnoreCase(treeItem.text(0))) {
\r
259 previousMouseOver.setSelected(previousMouseOverWasSelected);
\r
260 previousMouseOverWasSelected = treeItem.isSelected();
\r
261 previousMouseOver = treeItem;
\r
262 blockSignals(true);
\r
263 treeItem.setSelected(true);
\r
264 blockSignals(false);
\r
268 if (event.mimeData().hasFormat("application/x-nevernote-note")) {
\r
269 if (event.answerRect().intersects(childrenRect()))
\r
270 event.acceptProposedAction();
\r
277 public boolean dropMimeData(QTreeWidgetItem parent, int index, QMimeData data, Qt.DropAction action) {
\r
278 if (data.hasFormat("application/x-nevernote-note")) {
\r
279 QByteArray d = data.data("application/x-nevernote-note");
\r
280 String s = d.toString();
\r
281 String noteGuidArray[] = s.split(" ");
\r
282 for (String element : noteGuidArray) {
\r
283 if (!parent.text(0).equalsIgnoreCase("All Notebooks"))
\r
284 noteSignal.notebookChanged.emit(element.trim(), parent.text(2));
\r