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.
21 package cx.fbn.nevernote.gui;
23 import java.text.SimpleDateFormat;
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;
40 import cx.fbn.nevernote.Global;
41 import cx.fbn.nevernote.sql.DatabaseConnection;
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;
55 private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
57 public RensoNoteListItem(Note note, int relationPoints, boolean isStared, int allPointSum, DatabaseConnection c, RensoNoteList parent){
61 this.isStared = isStared;
62 this.allPointSum = allPointSum;
63 this.noteGuid = new String(note.getGuid());
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();
70 StringBuilder sb = new StringBuilder();
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 + " ");
79 this.tagNames = new String(sb);
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, "");
88 QPalette p = new QPalette();
89 p.setColor(QPalette.ColorRole.Window, new QColor(255, 255, 255));
91 this.setAutoFillBackground(true);
92 this.setBackgroundRole(QPalette.ColorRole.Window);
96 protected void paintEvent(QPaintEvent event){
97 QPainter painter = new QPainter(this);
100 painter.setPen(QColor.lightGray);
101 painter.drawLine(0, rect().height() - 1, rect().width() - 1, rect().height() - 1);
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);
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)) + "%");
121 painter.setPen(QColor.black);
122 painter.setFont(titleFont);
123 painter.drawText(85, 3, size().width() - 85, 20, Qt.AlignmentFlag.AlignLeft.value(), noteTitle);
125 painter.setFont(normalFont);
126 painter.setPen(new QColor(60, 65, 255));
127 painter.drawText(85, 23, 75, 17, Qt.AlignmentFlag.AlignLeft.value(), noteCreated);
129 painter.setPen(QColor.black);
130 painter.drawText(165, 23, size().width() - 165, 17, Qt.AlignmentFlag.AlignLeft.value(), tagNames);
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);
139 String thumbnailName = Global.getFileManager().getResDirPath("thumbnail-" + noteGuid + ".png");
140 QFile thumbnail = new QFile(thumbnailName);
141 if (!thumbnail.exists()) {
143 img.loadFromData(conn.getNoteTable().getThumbnail(noteGuid));
145 img = new QImage(thumbnailName);
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);
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());
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));
171 protected void leaveEvent(QEvent e){
172 if (!parent.isContextMenuVisible()) {
173 setDefaultBackground();
178 protected void mousePressEvent(QMouseEvent e) {
179 QPalette p = new QPalette();
180 p.setColor(QPalette.ColorRole.Window, new QColor(165, 175, 255));
183 super.mousePressEvent(e);
186 public void setDefaultBackground() {
187 QPalette p = new QPalette();
188 p.setColor(QPalette.ColorRole.Window, new QColor(255, 255, 255));