OSDN Git Service

表示される関連度のフォントを少し変更。
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / RensoNoteListItem.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 // ICHANGED
21 package cx.fbn.nevernote.gui;
22
23 import java.text.SimpleDateFormat;
24
25 import com.evernote.edam.type.Note;
26 import com.trolltech.qt.core.QEvent;
27 import com.trolltech.qt.core.QFile;
28 import com.trolltech.qt.core.QRectF;
29 import com.trolltech.qt.core.Qt;
30 import com.trolltech.qt.gui.QColor;
31 import com.trolltech.qt.gui.QFont;
32 import com.trolltech.qt.gui.QImage;
33 import com.trolltech.qt.gui.QMouseEvent;
34 import com.trolltech.qt.gui.QPaintEvent;
35 import com.trolltech.qt.gui.QPainter;
36 import com.trolltech.qt.gui.QPalette;
37 import com.trolltech.qt.gui.QTextOption;
38 import com.trolltech.qt.gui.QWidget;
39
40 import cx.fbn.nevernote.Global;
41 import cx.fbn.nevernote.sql.DatabaseConnection;
42
43 public class RensoNoteListItem extends QWidget{
44         private final DatabaseConnection conn;
45         private final String noteGuid;
46         private final String noteTitle;
47         private final int relationPoints;
48         private final String noteCreated;
49         private final String tagNames;
50         private String noteContent;
51         private final RensoNoteList parent;
52         private final boolean isStared;
53         private final int allPointSum;
54         
55         private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
56         
57         public RensoNoteListItem(Note note, int relationPoints, boolean isStared, int allPointSum, DatabaseConnection c, RensoNoteList parent){
58                 
59                 this.conn = c;
60                 this.parent = parent;
61                 this.isStared = isStared;
62                 this.allPointSum = allPointSum;
63                 this.noteGuid = new String(note.getGuid());
64                 
65                 this.noteTitle = new String(note.getTitle());
66                 this.relationPoints = relationPoints;
67                 SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd");
68                 this.noteCreated = new StringBuilder(simple.format(note.getCreated())).toString();
69                 
70                 StringBuilder sb = new StringBuilder();
71
72                 for (int i = 0; i < note.getTagNames().size(); i++) {
73                         sb.append(note.getTagNames().get(i));
74                         if(i + 1 < note.getTagNames().size()){
75                                 sb.append(Global.tagDelimeter + " ");
76                         }
77                 }
78
79                 this.tagNames = new String(sb);
80                 
81                 // this.noteContent = new String(note.getContent());
82                 this.noteContent = conn.getNoteTable().getNoteContentNoUTFConversion(note.getGuid());
83                 this.noteContent = this.noteContent.replaceAll("<.+?>", "");
84                 this.noteContent = this.noteContent.replaceAll("\\s{2,}", " ");
85                 String kaigyo = System.getProperty("line.separator");
86                 this.noteContent = this.noteContent.replaceAll(kaigyo, "");
87                 
88                 QPalette p = new QPalette();
89                 p.setColor(QPalette.ColorRole.Window, new QColor(255, 255, 255));
90                 this.setPalette(p);
91                 this.setAutoFillBackground(true);
92                 this.setBackgroundRole(QPalette.ColorRole.Window);
93         }
94         
95         @Override
96         protected void paintEvent(QPaintEvent event){
97                 QPainter painter = new QPainter(this);
98
99                 // 枠線
100                 painter.setPen(QColor.lightGray);
101                 painter.drawLine(0, rect().height() - 1, rect().width() - 1, rect().height() - 1);
102                 
103                 // 項目の中身
104                 // フォント設定
105                 QFont relationFont = new QFont();
106                 relationFont.setPixelSize(25);
107                 relationFont.setBold(true);
108                 QFont titleFont = new QFont();
109                 titleFont.setPixelSize(15);
110                 titleFont.setBold(true);
111                 QFont normalFont = new QFont();
112                 normalFont.setPixelSize(12);
113                 
114                 // 関連度
115                 double ratio = (double)relationPoints / allPointSum;
116                 int green = (int) (255 * (1.0 - ratio));
117                 painter.setPen(new QColor(255, green, 0));
118                 painter.setFont(relationFont);
119                 painter.drawText(70, size().height() - 30, size().width() - 70, 30, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");
120                 // タイトル
121                 painter.setPen(QColor.black);
122                 painter.setFont(titleFont);
123                 painter.drawText(85, 3, size().width() - 85, 20, Qt.AlignmentFlag.AlignLeft.value(), noteTitle);
124                 // ノート作成日時
125                 painter.setFont(normalFont);
126                 painter.setPen(new QColor(60, 65, 255));
127                 painter.drawText(85, 23, 75, 17, Qt.AlignmentFlag.AlignLeft.value(), noteCreated);
128                 // タグ
129                 painter.setPen(QColor.black);
130                 painter.drawText(165, 23, size().width() - 165, 17, Qt.AlignmentFlag.AlignLeft.value(), tagNames);
131                 // ノート内容
132                 QTextOption option = new QTextOption();
133                 option.setAlignment(Qt.AlignmentFlag.AlignLeft);
134                 option.setUseDesignMetrics(true);
135                 painter.drawText(new QRectF(85, 40, width() - 85, 40), noteContent, option);
136                 
137                 // サムネイル
138                 QImage img;
139                 String thumbnailName = Global.getFileManager().getResDirPath("thumbnail-" + noteGuid + ".png");
140                 QFile thumbnail = new QFile(thumbnailName);
141                 if (!thumbnail.exists()) {
142                         img = new QImage();
143                         img.loadFromData(conn.getNoteTable().getThumbnail(noteGuid));
144                 } else {
145                         img = new QImage(thumbnailName);
146                 }
147                 painter.drawImage(2, 4, img, 0, 0, 80, rect().height() - 10);
148                 painter.setPen(QColor.lightGray);
149                 painter.drawRect(2, 4, 80, rect().height() - 10);
150                 
151                 // スター
152                 if (isStared) {
153                         QImage starImage = new QImage(iconPath+"star.png");
154                         starImage = starImage.scaled(30, 30, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation);
155                         painter.drawImage(0, 0, starImage, 0, 0, starImage.width(), starImage.height());
156                 }
157                 
158                 painter.end();
159         }
160
161         @Override
162         protected void enterEvent(QEvent e){
163                 if (!parent.isContextMenuVisible()) {
164                         QPalette p = new QPalette();
165                         p.setColor(QPalette.ColorRole.Window, new QColor(225, 235, 255));
166                         this.setPalette(p);
167                 }
168         }
169         
170         @Override
171         protected void leaveEvent(QEvent e){
172                 if (!parent.isContextMenuVisible()) {
173                         setDefaultBackground();
174                 }
175         }
176         
177         @Override
178         protected void mousePressEvent(QMouseEvent e) {
179                 QPalette p = new QPalette();
180                 p.setColor(QPalette.ColorRole.Window, new QColor(165, 175, 255));
181                 this.setPalette(p);
182                 
183                 super.mousePressEvent(e);
184         }
185         
186         public void setDefaultBackground() {
187                 QPalette p = new QPalette();
188                 p.setColor(QPalette.ColorRole.Window, new QColor(255, 255, 255));
189                 this.setPalette(p);
190         }
191 }