2 package cx.fbn.nevernote.gui;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
10 import com.evernote.edam.type.Note;
11 import com.trolltech.qt.core.QSize;
12 import com.trolltech.qt.core.Qt.MouseButton;
13 import com.trolltech.qt.gui.QAction;
14 import com.trolltech.qt.gui.QApplication;
15 import com.trolltech.qt.gui.QContextMenuEvent;
16 import com.trolltech.qt.gui.QListWidget;
17 import com.trolltech.qt.gui.QListWidgetItem;
18 import com.trolltech.qt.gui.QMenu;
20 import cx.fbn.nevernote.Global;
21 import cx.fbn.nevernote.NeverNote;
22 import cx.fbn.nevernote.sql.DatabaseConnection;
23 import cx.fbn.nevernote.utilities.ApplicationLogger;
25 public class RensoNoteList extends QListWidget {
26 private final DatabaseConnection conn;
27 private final ApplicationLogger logger;
28 private final HashMap<QListWidgetItem, String> rensoNoteListItems;
29 private final List<RensoNoteListItem> rensoNoteListTrueItems;
30 private String rensoNotePressedItemGuid;
32 private final QAction openNewTabAction;
33 private final QAction starAction;
34 private final QAction unstarAction;
35 private final QAction excludeNoteAction;
36 private final NeverNote parent;
37 private final QMenu menu;
38 private int allPointSum;
40 public RensoNoteList(DatabaseConnection c, NeverNote p) {
41 logger = new ApplicationLogger("rensoNoteList.log");
42 logger.log(logger.HIGH, "Setting up rensoNoteList");
47 rensoNoteListItems = new HashMap<QListWidgetItem, String>();
48 rensoNoteListTrueItems = new ArrayList<RensoNoteListItem>();
50 this.itemPressed.connect(this, "rensoNoteItemPressed(QListWidgetItem)");
53 menu = new QMenu(this);
55 openNewTabAction = new QAction(tr("Open in New Tab"), this);
56 openNewTabAction.setToolTip(tr("Open this note in new tab"));
57 openNewTabAction.triggered.connect(parent, "openNewTabFromRNL()");
59 starAction = new QAction(tr("STAR"), this);
60 starAction.setToolTip(tr("Star this item"));
61 starAction.triggered.connect(parent, "starNote()");
63 unstarAction = new QAction(tr("UNSTAR"), this);
64 unstarAction.setToolTip(tr("Unstar this item"));
65 unstarAction.triggered.connect(parent, "unstarNote()");
67 excludeNoteAction = new QAction(tr("Exclude"), this);
68 excludeNoteAction.setToolTip(tr("Exclude this note from RensoNoteList"));
69 excludeNoteAction.triggered.connect(parent, "excludeNote()");
71 menu.addAction(openNewTabAction);
72 menu.addAction(excludeNoteAction);
73 menu.aboutToHide.connect(this, "contextMenuHidden()");
75 logger.log(logger.HIGH, "rensoNoteList setup complete");
78 public void refreshRensoNoteList(String guid) {
79 logger.log(logger.HIGH, "Entering RensoNoteList.refreshRensoNoteList");
82 rensoNoteListItems.clear();
83 rensoNoteListTrueItems.clear();
85 if (!this.isEnabled()) {
88 if (guid == null || guid.equals("")) {
92 HashMap<String, Integer> mergedHistory = new HashMap<String, Integer>();
94 // browseHistory<guid, 回数(ポイント)>
95 HashMap<String, Integer> browseHistory = conn.getHistoryTable().getBehaviorHistory("browse", guid);
96 addWeight(browseHistory, Global.getBrowseWeight());
97 mergedHistory = mergeHistory(browseHistory, new HashMap<String, Integer>());
99 // copy&pasteHistory<guid, 回数(ポイント)>
100 HashMap<String, Integer> copyAndPasteHistory = conn.getHistoryTable().getBehaviorHistory("copy & paste", guid);
101 addWeight(copyAndPasteHistory, Global.getCopyPasteWeight());
102 mergedHistory = mergeHistory(copyAndPasteHistory, mergedHistory);
104 // addNewNoteHistory<guid, 回数(ポイント)>
105 HashMap<String, Integer> addNewNoteHistory = conn.getHistoryTable().getBehaviorHistory("addNewNote", guid);
106 addWeight(addNewNoteHistory, Global.getAddNewNoteWeight());
107 mergedHistory = mergeHistory(addNewNoteHistory, mergedHistory);
109 // rensoItemClickHistory<guid, 回数(ポイント)>
110 HashMap<String, Integer> rensoItemClickHistory = conn.getHistoryTable().getBehaviorHistory("rensoItemClick", guid);
111 addWeight(rensoItemClickHistory, Global.getRensoItemClickWeight());
112 mergedHistory = mergeHistory(rensoItemClickHistory, mergedHistory);
114 // sameTagHistory<guid, 回数(ポイント)>
115 HashMap<String, Integer> sameTagHistory = conn.getHistoryTable().getBehaviorHistory("sameTag", guid);
116 addWeight(sameTagHistory, Global.getSameTagWeight());
117 mergedHistory = mergeHistory(sameTagHistory, mergedHistory);
119 // sameNotebookNoteHistory<guid, 回数(ポイント)>
120 HashMap<String, Integer> sameNotebookHistory = conn.getHistoryTable().getBehaviorHistory("sameNotebook", guid);
121 addWeight(sameNotebookHistory, Global.getSameNotebookWeight());
122 mergedHistory = mergeHistory(sameNotebookHistory, mergedHistory);
124 // すべての関連ポイントの合計を取得(関連度のパーセント算出に利用)
126 for (int p : mergedHistory.values()) {
130 addRensoNoteList(mergedHistory);
132 logger.log(logger.HIGH, "Leaving RensoNoteList.refreshRensoNoteList");
136 private void addWeight(HashMap<String, Integer> history, int weight){
137 Set<String> keySet = history.keySet();
138 Iterator<String> hist_iterator = keySet.iterator();
139 while(hist_iterator.hasNext()){
140 String key = hist_iterator.next();
141 history.put(key, history.get(key) * weight);
145 // 引数1と引数2をマージしたハッシュマップを返す
146 private HashMap<String, Integer> mergeHistory(HashMap<String, Integer> History1, HashMap<String, Integer> History2){
147 HashMap<String, Integer> mergedHistory = new HashMap<String, Integer>();
149 mergedHistory.putAll(History1);
151 Set<String> keySet = History2.keySet();
152 Iterator<String> hist2_iterator = keySet.iterator();
153 while(hist2_iterator.hasNext()){
154 String key = hist2_iterator.next();
155 if(mergedHistory.containsKey(key)){
156 mergedHistory.put(key, mergedHistory.get(key) + History2.get(key));
158 mergedHistory.put(key, History2.get(key));
162 return mergedHistory;
165 private void addRensoNoteList(HashMap<String, Integer> History){
166 String currentNoteGuid = new String(parent.getCurrentNoteGuid());
168 // スター付きノートとスター無しノートを分ける
169 HashMap<String, Integer> staredNotes = new HashMap<String, Integer>(); // スター付きノートのマップ
170 HashMap<String, Integer> normalNotes = new HashMap<String, Integer>(); // スター無しノートのマップ
171 for (String nextGuid: History.keySet()) {
172 int relationPoint = History.get(nextGuid);
173 boolean isStared = conn.getStaredTable().existNote(currentNoteGuid, nextGuid);
175 staredNotes.put(nextGuid, relationPoint);
177 normalNotes.put(nextGuid, relationPoint);
181 // 連想ノートリストアイテムの最大表示数まで繰り返す
182 for (int i = 0; i < Global.getRensoListItemMaximum(); i++) {
183 // スター付きノートがあれば先に処理する
184 HashMap<String, Integer> tmpMap = new HashMap<String, Integer>();
185 if (!staredNotes.isEmpty()) {
186 tmpMap = staredNotes;
187 }else if (!normalNotes.isEmpty()) {
188 tmpMap = normalNotes;
191 // 操作回数が多い順に取り出して連想ノートリストに追加する
192 if (!tmpMap.isEmpty()) {
194 String maxGuid = new String();
196 for (String nextGuid: tmpMap.keySet()) {
197 int relationPoint = tmpMap.get(nextGuid);
200 if (relationPoint > maxNum) {
201 maxNum = relationPoint;
206 // 次の最大値探索で邪魔なので最大値をHashMapから削除
207 tmpMap.remove(maxGuid);
209 // 関連度最大のノートがアクティブか確認
210 Note maxNote = conn.getNoteTable().getNote(maxGuid, true, false, false, false, true);
211 boolean isNoteActive = false;
212 if(maxNote != null) {
213 isNoteActive = maxNote.isActive();
216 // 存在していて、かつ関連度0でなければノート情報を取得して連想ノートリストに追加
217 if (isNoteActive && maxNum > 0) {
220 isStared = conn.getStaredTable().existNote(currentNoteGuid, maxGuid);
222 QListWidgetItem item = new QListWidgetItem();
223 RensoNoteListItem myItem = new RensoNoteListItem(maxNote, maxNum, isStared, allPointSum, conn, this);
224 item.setSizeHint(new QSize(0, 90));
226 this.setItemWidget(item, myItem);
227 rensoNoteListItems.put(item, maxGuid);
228 rensoNoteListTrueItems.add(myItem);
236 // リストのアイテムから対象ノートのguidを取得
237 public String getNoteGuid(QListWidgetItem item) {
238 return rensoNoteListItems.get(item);
241 // 関連ノートリストの右クリックメニュー
243 public void contextMenuEvent(QContextMenuEvent event){
244 // STAR, UNSTARがあれば、一度消す
245 List<QAction> menuActions = new ArrayList<QAction>(menu.actions());
246 if (menuActions.contains(starAction)) {
247 menu.removeAction(starAction);
249 if (menuActions.contains(unstarAction)) {
250 menu.removeAction(unstarAction);
253 // 対象アイテムがスター付きなら「UNSTAR」、スター無しなら「STAR」を追加
254 String currentNoteGuid = parent.getCurrentNoteGuid();
255 boolean isExist = conn.getStaredTable().existNote(currentNoteGuid, rensoNotePressedItemGuid);
257 menu.insertAction(excludeNoteAction, unstarAction);
259 menu.insertAction(excludeNoteAction, starAction);
263 menu.exec(event.globalPos());
266 // コンテキストメニューが表示されているかどうか
267 public boolean isContextMenuVisible() {
268 return menu.isVisible();
272 @SuppressWarnings("unused")
273 private void contextMenuHidden() {
274 for (int i = 0; i < rensoNoteListTrueItems.size(); i++) {
275 RensoNoteListItem item = rensoNoteListTrueItems.get(i);
276 item.setDefaultBackground();
280 // ユーザが連想ノートリストのアイテムを選択した時の処理
281 @SuppressWarnings("unused")
282 private void rensoNoteItemPressed(QListWidgetItem current) {
283 rensoNotePressedItemGuid = null;
285 if (QApplication.mouseButtons().isSet(MouseButton.RightButton)) {
286 rensoNotePressedItemGuid = getNoteGuid(current);