OSDN Git Service

294495b3cfaff100f51c12e67d35c6bd24c31288
[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.QPen;
38 import com.trolltech.qt.gui.QTextOption;
39 import com.trolltech.qt.gui.QWidget;
40
41 import cx.fbn.nevernote.Global;
42 import cx.fbn.nevernote.sql.DatabaseConnection;
43
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 String noteContent;
52         private final RensoNoteList parent;
53         private final boolean isStared;
54         private final int allPointSum;
55         private final QPalette palette;
56         
57         private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
58         
59         public RensoNoteListItem(Note note, int relationPoints, boolean isStared, int allPointSum, DatabaseConnection c, RensoNoteList parent){
60                 
61                 this.conn = c;
62                 this.parent = parent;
63                 this.isStared = isStared;
64                 this.allPointSum = allPointSum;
65                 this.noteGuid = new String(note.getGuid());
66                 
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();
71                 
72                 StringBuilder sb = new StringBuilder();
73
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 + " ");
78                         }
79                 }
80
81                 this.tagNames = new String(sb);
82                 
83                 // this.noteContent = new String(note.getContent());
84                 this.noteContent = conn.getNoteTable().getNoteContentNoUTFConversion(note.getGuid());
85                 this.noteContent = this.noteContent.replaceAll("<.+?>", "");
86                 this.noteContent = this.noteContent.replaceAll("\\s{2,}", " ");
87                 String kaigyo = System.getProperty("line.separator");
88                 this.noteContent = this.noteContent.replaceAll(kaigyo, "");
89                 
90                 palette = new QPalette();
91                 palette.setColor(QPalette.ColorRole.Window, new QColor(255, 255, 255));
92                 this.setPalette(palette);
93                 this.setAutoFillBackground(true);
94                 this.setBackgroundRole(QPalette.ColorRole.Window);
95         }
96         
97         @Override
98         protected void paintEvent(QPaintEvent event){
99                 QPainter painter = new QPainter(this);
100
101                 // 枠線
102                 painter.setPen(QColor.lightGray);
103                 painter.drawLine(0, rect().height() - 1, rect().width() - 1, rect().height() - 1);
104                 
105                 // 項目の中身
106                 // フォント設定
107                 QFont relationFont = new QFont();
108                 relationFont.setPixelSize(25);
109                 relationFont.setBold(true);
110                 QFont titleFont = new QFont();
111                 titleFont.setPixelSize(15);
112                 titleFont.setBold(true);
113                 QFont normalFont = new QFont();
114                 normalFont.setPixelSize(12);
115                 
116                 // タイトル
117                 painter.setPen(QColor.black);
118                 painter.setFont(titleFont);
119                 painter.drawText(85, 3, size().width() - 85, 20, Qt.AlignmentFlag.AlignLeft.value(), noteTitle);
120                 // ノート作成日時
121                 painter.setFont(normalFont);
122                 painter.setPen(new QColor(60, 65, 255));
123                 painter.drawText(85, 23, 75, 17, Qt.AlignmentFlag.AlignLeft.value(), noteCreated);
124                 // タグ
125                 painter.setPen(QColor.black);
126                 painter.drawText(165, 23, size().width() - 165, 17, Qt.AlignmentFlag.AlignLeft.value(), tagNames);
127                 // ノート内容
128                 QPen tmpPen = painter.pen();
129                 painter.setPen(new QColor(100, 100, 100));
130                 QTextOption option = new QTextOption();
131                 option.setAlignment(Qt.AlignmentFlag.AlignLeft);
132                 option.setUseDesignMetrics(true);
133                 painter.drawText(new QRectF(85, 40, width() - 85, 45), noteContent, option);
134                 painter.setPen(tmpPen);
135                 
136                 // 関連度
137                 double ratio = (double)relationPoints / allPointSum;
138                 QColor relationColor;
139                 if (ratio >= 0.8) {
140                         relationColor = new QColor(255, 0, 0);
141                 } else if (ratio >= 0.5) {
142                         relationColor = new QColor(255, 100, 0);
143                 } else {
144                         relationColor = new QColor(255, 200, 0);
145                 }
146                 painter.setFont(relationFont);
147                 tmpPen = painter.pen();
148                 painter.setPen(this.palette().color(QPalette.ColorRole.Window));
149                 painter.drawText(size().width() - 70, size().height() - 33, 67, 33, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");
150                 painter.drawText(size().width() - 70, size().height() - 33, 73, 33, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");
151                 painter.drawText(70, size().height() - 36, size().width() - 70, 36, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");
152                 painter.drawText(70, size().height() - 30, size().width() - 70, 30, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");
153                 painter.setPen(relationColor);
154                 painter.drawText(70, size().height() - 33, size().width() - 70, 33, Qt.AlignmentFlag.AlignRight.value(), String.valueOf((int)(ratio * 100)) + "%");             
155                 painter.setPen(tmpPen);
156                 // サムネイル
157                 QImage img;
158                 String thumbnailName = Global.getFileManager().getResDirPath("thumbnail-" + noteGuid + ".png");
159                 QFile thumbnail = new QFile(thumbnailName);
160                 if (!thumbnail.exists()) {
161                         img = new QImage();
162                         img.loadFromData(conn.getNoteTable().getThumbnail(noteGuid));
163                 } else {
164                         img = new QImage(thumbnailName);
165                 }
166                 painter.drawImage(2, 4, img, 0, 0, 80, rect().height() - 10);
167                 painter.setPen(QColor.lightGray);
168                 painter.drawRect(2, 4, 80, rect().height() - 10);
169                 
170                 // スター
171                 if (isStared) {
172                         QImage starImage = new QImage(iconPath+"star.png");
173                         starImage = starImage.scaled(30, 30, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation);
174                         painter.drawImage(0, 0, starImage, 0, 0, starImage.width(), starImage.height());
175                 }
176                 
177                 painter.end();
178         }
179
180         @Override
181         protected void enterEvent(QEvent e){
182                 if (!parent.isContextMenuVisible()) {
183                         palette.setColor(QPalette.ColorRole.Window, new QColor(225, 235, 255));
184                         this.setPalette(palette);
185                 }
186         }
187         
188         @Override
189         protected void leaveEvent(QEvent e){
190                 if (!parent.isContextMenuVisible()) {
191                         setDefaultBackground();
192                 }
193         }
194         
195         @Override
196         protected void mousePressEvent(QMouseEvent e) {
197                 
198                 palette.setColor(QPalette.ColorRole.Window, new QColor(165, 175, 255));
199                 this.setPalette(palette);
200                 
201                 super.mousePressEvent(e);
202         }
203         
204         public void setDefaultBackground() {
205                 palette.setColor(QPalette.ColorRole.Window, new QColor(255, 255, 255));
206                 this.setPalette(palette);
207         }
208 }