/* * This file is part of NeverNote * Copyright 2009 Randy Baumgarte * * This file may be licensed under the terms of of the * GNU General Public License Version 2 (the ``GPL''). * * Software distributed under the License is distributed * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either * express or implied. See the GPL for the specific language * governing rights and limitations. * * You should have received a copy of the GPL along with this * program. If not, go to http://www.gnu.org/licenses/gpl.html * or write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ package cx.fbn.nevernote.gui; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.evernote.edam.type.Notebook; import com.trolltech.qt.core.QByteArray; import com.trolltech.qt.core.QMimeData; import com.trolltech.qt.core.Qt; import com.trolltech.qt.core.Qt.SortOrder; import com.trolltech.qt.gui.QAbstractItemView; import com.trolltech.qt.gui.QAction; import com.trolltech.qt.gui.QBrush; import com.trolltech.qt.gui.QColor; import com.trolltech.qt.gui.QContextMenuEvent; import com.trolltech.qt.gui.QDragEnterEvent; import com.trolltech.qt.gui.QDragMoveEvent; import com.trolltech.qt.gui.QHeaderView; import com.trolltech.qt.gui.QIcon; import com.trolltech.qt.gui.QMenu; import com.trolltech.qt.gui.QTreeWidget; import com.trolltech.qt.gui.QTreeWidgetItem; import com.trolltech.qt.gui.QTreeWidgetItem.ChildIndicatorPolicy; import cx.fbn.nevernote.Global; import cx.fbn.nevernote.filters.NotebookCounter; import cx.fbn.nevernote.signals.NoteSignal; import cx.fbn.nevernote.sql.DatabaseConnection; public class NotebookTreeWidget extends QTreeWidget { private QAction deleteAction; private QAction addAction; private QAction editAction; private QAction iconAction; private QAction stackAction; public NoteSignal noteSignal; private HashMap icons; private final DatabaseConnection db; private List lastCounts; private final HashMap stacks; // private final QTreeWidgetItem previousMouseOver; // private boolean previousMouseOverWasSelected; public void setAddAction(QAction a) { addAction = a; } public void setDeleteAction(QAction d) { deleteAction = d; } public void setEditAction(QAction e) { editAction = e; } public void setStackAction(QAction e) { stackAction = e; } public void setIconAction(QAction e) { iconAction = e; } public NotebookTreeWidget(DatabaseConnection db) { noteSignal = new NoteSignal(); this.db = db; // setProperty("hideTree", true); List labels = new ArrayList(); labels.add("Notebooks"); labels.add(""); setAcceptDrops(true); setDragEnabled(true); setColumnCount(2); header().setResizeMode(0, QHeaderView.ResizeMode.ResizeToContents); header().setResizeMode(1, QHeaderView.ResizeMode.Stretch); header().setMovable(false); setHeaderLabels(labels); setDragDropMode(QAbstractItemView.DragDropMode.DragDrop); // If we want to mimic Evernote's notebook selection if (Global.mimicEvernoteInterface) { setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection); } else setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection); stacks = new HashMap(); // int width = Global.getColumnWidth("notebookTreeName"); // if (width>0) // setColumnWidth(0, width); // previousMouseOver = new QTreeWidgetItem(); } public void selectNotebook(QTreeWidgetItem item) { QTreeWidgetItem root = invisibleRootItem(); QTreeWidgetItem child; for (int i=0; i i) { icons = i; } public QIcon findDefaultIcon(String guid, String name, List localBooks, boolean isPublished) { String iconPath = new String("classpath:cx/fbn/nevernote/icons/"); QIcon blueIcon = new QIcon(iconPath+"notebook-blue.png"); QIcon greenIcon = new QIcon(iconPath+"notebook-green.png"); QIcon redIcon = new QIcon(iconPath+"notebook-red.png"); QIcon yellowIcon = new QIcon(iconPath+"notebook-yellow.png"); if (localBooks.contains(guid)) { return yellowIcon; } if (localBooks.contains(guid) && (name.equalsIgnoreCase("Conflicting Changes") || name.equalsIgnoreCase("Conflicting Changes (Local)"))) return redIcon; if (isPublished) return blueIcon; return greenIcon; } public void load(List books, List localBooks) { Notebook book; QTreeWidgetItem child; clear(); stacks.clear(); if (books == null) return; Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight); for (int i=0; i books, List counts) { lastCounts = counts; QTreeWidgetItem root = invisibleRootItem(); QTreeWidgetItem child; QBrush blue = new QBrush(); QBrush black = new QBrush(); black.setColor(QColor.black); if (Global.tagBehavior().equalsIgnoreCase("ColorActive") && !Global.mimicEvernoteInterface) blue.setColor(QColor.blue); else blue.setColor(QColor.black); int total=0; int size = books.size(); if (Global.mimicEvernoteInterface) size++; for (int i=0; i 0) { int count = child.childCount(); QTreeWidgetItem parent = child; int localTotal = 0; for (int j=0; j books, List counts, QBrush blue, QBrush black) { int total=0; if (child != null) { String guid = child.text(2); child.setText(1,"0"); child.setForeground(0, black); child.setForeground(1, black); for (int j=0; j 0) { child.setForeground(0, blue); child.setForeground(1, blue); } } } } return total; } // Return a list of the notebook guids, ordered by the current display order. public List getNotebookGuids() { List names = new ArrayList(); QTreeWidgetItem root = invisibleRootItem(); QTreeWidgetItem child; for (int i=0; i selected = selectedItems(); for (int i=0; i currentItems = selectedItems(); if (!parent.text(2).equalsIgnoreCase("STACK")) { // If a parent stack exists, then use it. if (parent.parent() != null) { stackName = parent.parent().text(0); stackItem = parent.parent(); } else { currentItems.add(parent); // If a stack doesn't exist, then we need to create one stackName = "New Stack"; // Find a new stack name that isn't in use for (int i=1; i<101; i++) { if (stacks.containsKey(stackName)) stackName = "New Stack(" +new Integer(i).toString() + ")"; else break; } db.getNotebookTable().setStack(parent.text(2), stackName); Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight); stackItem = createStackIcon(stackName, ra); addTopLevelItem(stackItem); } } else { stackName = parent.text(0); stackItem = parent; } List newItems = new ArrayList(); for (int i=0; i