2 * This file is part of NeighborNote
3 * Copyright 2013 Yuki Takahashi
5 * This file may be licensed under the terms of of the
6 * GNU General Public License Version 2 (the ``GPL'').
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.
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.
20 package cx.fbn.nevernote.gui;
22 import java.text.SimpleDateFormat;
24 import com.evernote.edam.type.Note;
25 import com.trolltech.qt.core.QByteArray;
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.QPen;
38 import com.trolltech.qt.gui.QTextOption;
39 import com.trolltech.qt.gui.QWidget;
41 import cx.fbn.nevernote.Global;
42 import cx.fbn.nevernote.sql.DatabaseConnection;
44 public class RensoNoteListItem extends QWidget{
45 private final DatabaseConnection conn;
46 private final String noteGuid;
47 private final String noteTitle;
48 private final int relationPoints;
49 private final String noteCreated;
50 private final String tagNames;
51 private final String noteContent;
52 private final RensoNoteList parent;
53 private final boolean isStared;
54 private final int allPointSum;
55 private final QPalette palette;
57 private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
59 public RensoNoteListItem(Note note, int relationPoints, boolean isStared, int allPointSum, DatabaseConnection c, RensoNoteList parent){
63 this.isStared = isStared;
64 this.allPointSum = allPointSum;
65 this.noteGuid = new String(note.getGuid());
67 this.noteTitle = new String(note.getTitle());
68 this.relationPoints = relationPoints;
69 SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd");
70 this.noteCreated = new StringBuilder(simple.format(note.getCreated())).toString();
72 StringBuilder sb = new StringBuilder();
74 for (int i = 0; i < note.getTagNames().size(); i++) {
75 sb.append(note.getTagNames().get(i));
76 if(i + 1 < note.getTagNames().size()){
77 sb.append(Global.tagDelimeter + " ");
81 this.tagNames = new String(sb);
83 // this.noteContent = new String(note.getContent());
84 // this.noteContent = Global.extractPlainText(conn.getNoteTable().getNoteContentNoUTFConversion(note.getGuid()));
85 this.noteContent = conn.getNoteTable().getNoteContentText(note.getGuid());
86 palette = new QPalette();
87 palette.setColor(QPalette.ColorRole.Window, new QColor(255, 255, 255));
88 this.setPalette(palette);
89 this.setAutoFillBackground(true);
90 this.setBackgroundRole(QPalette.ColorRole.Window);
94 protected void paintEvent(QPaintEvent event){
95 QPainter painter = new QPainter(this);
98 painter.setPen(QColor.lightGray);
99 painter.drawLine(0, rect().height() - 1, rect().width() - 1, rect().height() - 1);
103 QFont relationFont = new QFont();
104 relationFont.setPixelSize(25);
105 relationFont.setBold(true);
106 QFont titleFont = new QFont();
107 titleFont.setPixelSize(15);
108 titleFont.setBold(true);
109 QFont normalFont = new QFont();
110 normalFont.setPixelSize(12);
113 painter.setPen(QColor.black);
114 painter.setFont(titleFont);
115 painter.drawText(85, 3, size().width() - 85, 20, Qt.AlignmentFlag.AlignLeft.value(), noteTitle);
117 painter.setFont(normalFont);
118 painter.setPen(new QColor(60, 65, 255));
119 painter.drawText(85, 23, 75, 17, Qt.AlignmentFlag.AlignLeft.value(), noteCreated);
121 painter.setPen(QColor.black);
122 painter.drawText(165, 23, size().width() - 165, 17, Qt.AlignmentFlag.AlignLeft.value(), tagNames);
124 QPen tmpPen = painter.pen();
125 painter.setPen(new QColor(100, 100, 100));
126 QTextOption option = new QTextOption();
127 option.setAlignment(Qt.AlignmentFlag.AlignLeft);
128 option.setUseDesignMetrics(true);
129 painter.drawText(new QRectF(85, 40, width() - 85, 45), noteContent, option);
130 painter.setPen(tmpPen);
133 double ratio = (double)relationPoints / allPointSum;
134 QColor relationColor;
136 relationColor = new QColor(255, 0, 0);
137 } else if (ratio >= 0.25) {
138 relationColor = new QColor(255, 100, 0);
140 relationColor = new QColor(255, 200, 0);
142 painter.setFont(relationFont);
143 tmpPen = painter.pen();
144 painter.setPen(this.palette().color(QPalette.ColorRole.Window));
145 painter.drawText(size().width() - 70, size().height() - 33, 67, 33, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");
146 painter.drawText(size().width() - 70, size().height() - 33, 73, 33, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");
147 painter.drawText(70, size().height() - 36, size().width() - 70, 36, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");
148 painter.drawText(70, size().height() - 30, size().width() - 70, 30, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");
149 painter.setPen(relationColor);
150 painter.drawText(70, size().height() - 33, size().width() - 70, 33, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");
151 painter.setPen(tmpPen);
153 // 優先順位 1.Evernoteサムネイル(ファイル), 2.Evernoteサムネイル(DB), 3.生成サムネイル(ファイル), 4.生成サムネイル(DB)
154 QImage img = new QImage();
155 String enThumbnailName = Global.getFileManager().getResDirPath("enThumbnail-" + noteGuid + ".png");
156 QFile enThumbnail = new QFile(enThumbnailName);
157 if (!enThumbnail.exists()) { // Evernoteサムネイルがファイルとして存在しない
158 QByteArray b =conn.getNoteTable().getENThumbnail(noteGuid);
159 if (b == null) { // Evernoteサムネイルがデータベースにも存在しない
160 String thumbnailName = Global.getFileManager().getResDirPath("thumbnail-" + noteGuid + ".png");
161 QFile thumbnail = new QFile(thumbnailName);
162 if (!thumbnail.exists()) { // 生成サムネイルがファイルとして存在しない
164 img.loadFromData(conn.getNoteTable().getThumbnail(noteGuid));
169 saveImage(img, noteGuid);
172 img = new QImage(enThumbnailName);
174 painter.drawImage(2, 4, img, 0, 0, 80, rect().height() - 10);
175 painter.setPen(QColor.lightGray);
176 painter.drawRect(2, 4, 80, rect().height() - 10);
180 QImage starImage = new QImage(iconPath+"star.png");
181 starImage = starImage.scaled(30, 30, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation);
182 painter.drawImage(0, 0, starImage, 0, 0, starImage.width(), starImage.height());
189 protected void enterEvent(QEvent e){
190 if (!parent.isContextMenuVisible()) {
191 palette.setColor(QPalette.ColorRole.Window, new QColor(225, 235, 255));
192 this.setPalette(palette);
197 protected void leaveEvent(QEvent e){
198 if (!parent.isContextMenuVisible()) {
199 setDefaultBackground();
204 protected void mousePressEvent(QMouseEvent e) {
206 palette.setColor(QPalette.ColorRole.Window, new QColor(165, 175, 255));
207 this.setPalette(palette);
209 super.mousePressEvent(e);
212 public void setDefaultBackground() {
213 palette.setColor(QPalette.ColorRole.Window, new QColor(255, 255, 255));
214 this.setPalette(palette);
217 // サムネイルをpng形式のファイルとしてresディレクトリに保存
218 private void saveImage(QImage thumbnail, String guid) {
219 String thumbnailName = Global.getFileManager().getResDirPath("enThumbnail-" + guid + ".png");
220 thumbnail.save(thumbnailName, "PNG");