OSDN Git Service

a54feb58b059552aab7865208cd3f3bb8a49b54d
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / RensoNoteList.java
1 // ICHANGED
2 package cx.fbn.nevernote.gui;
3
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Set;
9
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;
19
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;
24
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;
31         
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;
39
40         public RensoNoteList(DatabaseConnection c, NeverNote p) {
41                 logger = new ApplicationLogger("rensoNoteList.log");
42                 logger.log(logger.HIGH, "Setting up rensoNoteList");
43                 allPointSum = 0;
44
45                 conn = c;
46                 this.parent = p;
47                 rensoNoteListItems = new HashMap<QListWidgetItem, String>();
48                 rensoNoteListTrueItems = new ArrayList<RensoNoteListItem>();
49                 
50                 this.itemPressed.connect(this, "rensoNoteItemPressed(QListWidgetItem)");
51                 
52                 // コンテキストメニュー作成
53                 menu = new QMenu(this);
54                 // 新しいタブで開くアクション生成
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()");
58                 // スターをつけるアクション生成
59                 starAction = new QAction(tr("STAR"), this);
60                 starAction.setToolTip(tr("Star this item"));
61                 starAction.triggered.connect(parent, "starNote()");
62                 // スターを外すアクション生成
63                 unstarAction = new QAction(tr("UNSTAR"), this);
64                 unstarAction.setToolTip(tr("Unstar this item"));
65                 unstarAction.triggered.connect(parent, "unstarNote()");
66                 // このノートを除外するアクション生成
67                 excludeNoteAction = new QAction(tr("Exclude"), this);
68                 excludeNoteAction.setToolTip(tr("Exclude this note from RensoNoteList"));
69                 excludeNoteAction.triggered.connect(parent, "excludeNote()");
70                 // コンテキストメニューに登録
71                 menu.addAction(openNewTabAction);
72                 menu.addAction(excludeNoteAction);
73                 menu.aboutToHide.connect(this, "contextMenuHidden()");
74                 
75                 logger.log(logger.HIGH, "rensoNoteList setup complete");
76         }
77
78         public void refreshRensoNoteList(String guid) {
79                 logger.log(logger.HIGH, "Entering RensoNoteList.refreshRensoNoteList");
80
81                 this.clear();
82                 rensoNoteListItems.clear();
83                 rensoNoteListTrueItems.clear();
84
85                 if (!this.isEnabled()) {
86                         return;
87                 }
88                 if (guid == null || guid.equals("")) {
89                         return;
90                 }
91
92                 HashMap<String, Integer> mergedHistory = new HashMap<String, Integer>();
93                 
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>());
98                 
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);
103                 
104                 // addNewNoteHistory<guid, 回数(ポイント)>
105                 HashMap<String, Integer> addNewNoteHistory = conn.getHistoryTable().getBehaviorHistory("addNewNote", guid);
106                 addWeight(addNewNoteHistory, Global.getAddNewNoteWeight());
107                 mergedHistory = mergeHistory(addNewNoteHistory, mergedHistory);
108                 
109                 // rensoItemClickHistory<guid, 回数(ポイント)>
110                 HashMap<String, Integer> rensoItemClickHistory = conn.getHistoryTable().getBehaviorHistory("rensoItemClick", guid);
111                 addWeight(rensoItemClickHistory, Global.getRensoItemClickWeight());
112                 mergedHistory = mergeHistory(rensoItemClickHistory, mergedHistory);
113                 
114                 // sameTagHistory<guid, 回数(ポイント)>
115                 HashMap<String, Integer> sameTagHistory = conn.getHistoryTable().getBehaviorHistory("sameTag", guid);
116                 addWeight(sameTagHistory, Global.getSameTagWeight());
117                 mergedHistory = mergeHistory(sameTagHistory, mergedHistory);
118                 
119                 // sameNotebookNoteHistory<guid, 回数(ポイント)>
120                 HashMap<String, Integer> sameNotebookHistory = conn.getHistoryTable().getBehaviorHistory("sameNotebook", guid);
121                 addWeight(sameNotebookHistory, Global.getSameNotebookWeight());
122                 mergedHistory = mergeHistory(sameNotebookHistory, mergedHistory);
123                 
124                 // すべての関連ポイントの合計を取得(関連度のパーセント算出に利用)
125                 allPointSum = 0;
126                 for (int p : mergedHistory.values()) {
127                         allPointSum += p;
128                 }
129                 
130                 addRensoNoteList(mergedHistory);
131
132                 logger.log(logger.HIGH, "Leaving RensoNoteList.refreshRensoNoteList");
133         }
134         
135         // 操作回数に重み付けする
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);
142                 }
143         }
144         
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>();
148                 
149                 mergedHistory.putAll(History1);
150                 
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));
157                         }else {
158                                 mergedHistory.put(key, History2.get(key));
159                         }
160                 }
161
162                 return mergedHistory;
163         }
164         
165         private void addRensoNoteList(HashMap<String, Integer> History){
166                 String currentNoteGuid = new String(parent.getCurrentNoteGuid());
167                 
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);
174                         if (isStared) {
175                                 staredNotes.put(nextGuid, relationPoint);
176                         } else {
177                                 normalNotes.put(nextGuid, relationPoint);
178                         }
179                 }
180                 
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;
189                         }
190                         
191                         // 操作回数が多い順に取り出して連想ノートリストに追加する
192                         if (!tmpMap.isEmpty()) {
193                                 int maxNum = -1;
194                                 String maxGuid = new String();
195                                 
196                                 for (String nextGuid: tmpMap.keySet()) {
197                                         int relationPoint = tmpMap.get(nextGuid);
198                                         
199                                         // 最大ノート探索する
200                                         if (relationPoint > maxNum) {
201                                                 maxNum = relationPoint;
202                                                 maxGuid = nextGuid;
203                                         }
204                                 }
205                                 
206                                 // 次の最大値探索で邪魔なので最大値をHashMapから削除
207                                 tmpMap.remove(maxGuid);
208         
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();
214                                 }
215                                 
216                                 // 存在していて、かつ関連度0でなければノート情報を取得して連想ノートリストに追加
217                                 if (isNoteActive && maxNum > 0) {
218                                         // スター付きか確認
219                                         boolean isStared;
220                                         isStared = conn.getStaredTable().existNote(currentNoteGuid, maxGuid);
221                                         
222                                         QListWidgetItem item = new QListWidgetItem();
223                                         RensoNoteListItem myItem = new RensoNoteListItem(maxNote, maxNum, isStared, allPointSum, conn, this);
224                                         item.setSizeHint(new QSize(0, 90));
225                                         this.addItem(item);
226                                         this.setItemWidget(item, myItem);
227                                         rensoNoteListItems.put(item, maxGuid);
228                                         rensoNoteListTrueItems.add(myItem);
229                                 } else {
230                                         break;
231                                 }
232                         }
233                 }
234         }
235
236         // リストのアイテムから対象ノートのguidを取得
237         public String getNoteGuid(QListWidgetItem item) {
238                 return rensoNoteListItems.get(item);
239         }
240         
241         // 関連ノートリストの右クリックメニュー
242         @Override
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);
248                 }
249                 if (menuActions.contains(unstarAction)) {
250                         menu.removeAction(unstarAction);
251                 }
252                 
253                 // 対象アイテムがスター付きなら「UNSTAR」、スター無しなら「STAR」を追加
254                 String currentNoteGuid = parent.getCurrentNoteGuid();
255                 boolean isExist = conn.getStaredTable().existNote(currentNoteGuid, rensoNotePressedItemGuid);
256                 if (isExist) {
257                         menu.insertAction(excludeNoteAction, unstarAction);
258                 } else {
259                         menu.insertAction(excludeNoteAction, starAction);
260                 }
261                 
262                 // コンテキストメニューを表示
263                 menu.exec(event.globalPos());
264         }
265         
266         // コンテキストメニューが表示されているかどうか
267         public boolean isContextMenuVisible() {
268                 return menu.isVisible();
269         }
270         
271         // コンテキストメニューが閉じられた時
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();
277                 }
278         }
279         
280         // ユーザが連想ノートリストのアイテムを選択した時の処理
281         @SuppressWarnings("unused")
282         private void rensoNoteItemPressed(QListWidgetItem current) {
283                 rensoNotePressedItemGuid = null;
284                 // 右クリックだったときの処理
285                 if (QApplication.mouseButtons().isSet(MouseButton.RightButton)) {
286                         rensoNotePressedItemGuid = getNoteGuid(current);
287                 }
288         }
289 }