OSDN Git Service

小さな修正
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / RensoNoteListDock.java
1 /*
2  * This file is part of NeighborNote
3  * Copyright 2013 Yuki Takahashi
4  * 
5  * This file may be licensed under the terms of of the
6  * GNU General Public License Version 2 (the ``GPL'').
7  *
8  * Software distributed under the License is distributed
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
10  * express or implied. See the GPL for the specific language
11  * governing rights and limitations.
12  *
13  * You should have received a copy of the GPL along with this
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html
15  * or write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18 */
19
20 package cx.fbn.nevernote.gui;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.EnumMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import com.trolltech.qt.core.QSize;
29 import com.trolltech.qt.gui.QComboBox;
30 import com.trolltech.qt.gui.QDockWidget;
31 import com.trolltech.qt.gui.QIcon;
32 import com.trolltech.qt.gui.QLabel;
33 import com.trolltech.qt.gui.QMessageBox;
34 import com.trolltech.qt.gui.QPushButton;
35 import com.trolltech.qt.gui.QSizePolicy;
36 import com.trolltech.qt.gui.QToolBar;
37 import com.trolltech.qt.gui.QVBoxLayout;
38 import com.trolltech.qt.gui.QWidget;
39
40 import cx.fbn.nevernote.Global;
41 import cx.fbn.nevernote.NeverNote;
42 import cx.fbn.nevernote.sql.DatabaseConnection;
43 import cx.fbn.nevernote.threads.SyncRunner;
44 import cx.fbn.nevernote.utilities.ApplicationLogger;
45
46 public class RensoNoteListDock extends QDockWidget {
47         private final DatabaseConnection        conn;
48         private final NeverNote                         parent;
49         private final SyncRunner                        syncRunner;
50         private final ApplicationLogger         logger;
51         private final QPushButton                       haltLogButton;                  // 操作ログの取得を停止ボタン
52         private final QComboBox                         weightingModeSelect;    // 連想ノートリストの重み付けプリセットモード
53         private final RensoNoteList                     rensoNoteList;                  // 連想ノートリスト
54         private final QToolBar                          rensoToolBar;                   // 連想ノートリストドックのツールバー
55         private enum PresetMode {Standard, ConcurrentBrowse, OperationToOrganize, ContentSimilarity, Custom};   // プリセットモード一覧
56         
57         private final String                            iconPath;
58         
59         public RensoNoteListDock(DatabaseConnection conn, NeverNote parent, SyncRunner syncRunner, String iconPath, String title) {
60                 super(title);
61                 
62                 this.logger = new ApplicationLogger("rensoNoteList.log");
63                 this.logger.log(this.logger.HIGH, "Setting up rensoNoteListDock");
64                 
65                 this.conn = conn;
66                 this.parent = parent;
67                 this.iconPath = iconPath;
68                 this.syncRunner = syncRunner;
69                 
70                 QVBoxLayout vLayout = new QVBoxLayout();
71                 rensoToolBar = new QToolBar();
72                 vLayout.addWidget(rensoToolBar);
73                 
74                 QLabel modeLabel = new QLabel(tr("Mode: "));
75                 modeLabel.setToolTip(tr("Preset Weighting Mode"));
76                 rensoToolBar.addWidget(modeLabel);
77                 
78                 weightingModeSelect = new QComboBox();
79                 weightingModeSelect.setMaximumWidth(150);
80                 weightingModeSelect.setToolTip(tr("Preset Weighting Mode"));
81                 weightingModeSelect.insertItem(PresetMode.Standard.ordinal(), tr("Standard"));
82                 weightingModeSelect.insertItem(PresetMode.ConcurrentBrowse.ordinal(), tr("Concurrent Browse"));
83                 weightingModeSelect.insertItem(PresetMode.OperationToOrganize.ordinal(), tr("Operation to Organize"));
84                 weightingModeSelect.insertItem(PresetMode.ContentSimilarity.ordinal(), tr("Content Similarity"));
85                 weightingModeSelect.insertItem(PresetMode.Custom.ordinal(), tr("Custom"));
86                 int savedModeIndex = 0;
87                 try {
88                         savedModeIndex = PresetMode.valueOf(Global.rensoWeightingSelect()).ordinal();
89                 } catch (Exception e) {
90                         this.logger.log(this.logger.EXTREME, "Exception in weightingModeSelect = " + e);
91                 }
92                 if (savedModeIndex >= 0) {
93                         weightingModeSelect.setCurrentIndex(savedModeIndex);
94                 } else {
95                         weightingModeSelect.setCurrentIndex(0);
96                 }
97                 weightingModeSelect.currentIndexChanged.connect(this, "weightingModeChanged(int)");
98                 rensoToolBar.addWidget(weightingModeSelect);
99                 
100                 haltLogButton = new QPushButton();
101                 QIcon haltLogIcon = new QIcon(this.iconPath + "haltLog.png");
102                 haltLogButton.setIcon(haltLogIcon);
103                 haltLogButton.setIconSize(new QSize(24, 24));
104                 haltLogButton.setToolTip(tr("Halt Collectiong Operation Log"));
105                 haltLogButton.setCheckable(true);
106                 haltLogButton.setChecked(Global.isHaltLogButton());
107                 haltLogButton.toggled.connect(this, "haltLogToggled(boolean)");
108                 
109                 // ログ取得停止ボタンを右寄せするためのスペーサ
110                 QWidget spacer = new QWidget();
111                 spacer.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding);
112                 rensoToolBar.addWidget(spacer);
113                 rensoToolBar.addWidget(haltLogButton);
114                 
115                 rensoNoteList = new RensoNoteList(this.conn, this.parent, this.syncRunner, this.logger);
116                 getRensoNoteList().itemPressed.connect(this.parent, "rensoNoteItemPressed(QListWidgetItem)");
117                 vLayout.addWidget(getRensoNoteList());
118
119                 QWidget w = new QWidget();
120                 w.setLayout(vLayout);
121                 this.setWidget(w);
122         }
123         
124         @SuppressWarnings("unused")
125         private void weightingModeChanged(int modeIndex) {
126                 logger.log(logger.EXTREME, "RensoNoteListDock.weightingModeChanged modeIndex = " + modeIndex);
127                 
128                 PresetMode mode = Global.fromOrdinal(PresetMode.class, modeIndex);
129                 
130                 // プリセットの重み付けマップを用意
131                 // 値は同時閲覧、コピペ、新規ノート追加、連想ノートクリック、同じタグ、同じノートブック、Evernote関連ノートの順
132                 Map<PresetMode, List<Integer>> presetMap = new EnumMap<PresetMode, List<Integer>>(PresetMode.class);
133                 presetMap.put(PresetMode.Standard, Arrays.asList(1, 3, 3, 10, 2, 2, 5));
134                 presetMap.put(PresetMode.ConcurrentBrowse, Arrays.asList(5, 2, 3, 10, 1, 1, 2));
135                 presetMap.put(PresetMode.OperationToOrganize, Arrays.asList(1, 2, 3, 10, 5, 5, 2));
136                 presetMap.put(PresetMode.ContentSimilarity, Arrays.asList(1, 2, 2, 10, 1, 1, 10));
137                 presetMap.put(PresetMode.Custom, Arrays.asList(Global.customBrowseWeight(), Global.customCopyPasteWeight(), 
138                                 Global.customAddNewNoteWeight(), Global.customRensoItemClickWeight(), Global.customSameTagWeight(), 
139                                 Global.customSameNotebookWeight(), Global.customENRelatedNotesWeight()));
140                 
141                 List<Integer> weightList = new ArrayList<Integer>(presetMap.get(mode));
142                 Global.setBrowseWeight(weightList.get(0));
143                 Global.setCopyPasteWeight(weightList.get(1));
144                 Global.setAddNewNoteWeight(weightList.get(2));
145                 Global.setRensoItemClickWeight(weightList.get(3));
146                 Global.setSameTagWeight(weightList.get(4));
147                 Global.setSameNotebookWeight(weightList.get(5));
148                 Global.setENRelatedNotesWeight(weightList.get(6));
149                 
150                 Global.saveRensoWeightingSelect(mode.name());
151                 
152                 // 連想ノートリストをリフレッシュ
153                 if (rensoNoteList != null && rensoNoteList.getGuid() != null ) {
154                         rensoNoteList.refreshRensoNoteList();
155                         
156                         // カスタムモードかつアプリ起動時以外なら、メッセージ表示
157                         if (mode == PresetMode.Custom) {
158                                 QMessageBox.information(this, tr("Information"), tr("Custom mode was selected.\nYou can customize weighting manually in the Edit/Preferences/Renso Note List."));
159                         }
160                 }
161         }
162         
163         @SuppressWarnings("unused")
164         private void haltLogToggled(boolean checked) {
165                 logger.log(logger.EXTREME, "RensoNoteListDock.haltLogToggled");
166                 
167                 if (checked) {
168                         QMessageBox.information(this, tr("Information"), tr("Collecting operation log halted.\nYou can resume, if you press this button again."));
169                 }
170                 Global.saveHaltLogButton(checked);
171         }
172
173         public RensoNoteList getRensoNoteList() {
174                 return rensoNoteList;
175         }
176         
177 }