Downloading cover images. Bug fixes. UI improvements.
[mebook:mebook.git] / ui / booklistitem.h
1 #ifndef BOOKLISTITEM_H
2 #define BOOKLISTITEM_H
3
4 #include <QWidget>
5 #include <QApplication>
6 #include <QHBoxLayout>
7 #include <QVBoxLayout>
8 #include <QLabel>
9 #include <QFrame>
10 #include <QPainter>
11 #include <QLinearGradient>
12 #include <QAbstractKineticScroller>
13 #include "book.h"
14
15 //TODO:Remove dependancy on Book, class should be independent from others 
16 //Replace with a delegate?
17
18 class BookListItem : public QFrame{
19     Q_OBJECT
20 public:
21     BookListItem(Book *book, QAbstractKineticScroller *scrollable = 0, QWidget *parent = 0) : QFrame(parent){
22         thisBook = book;
23
24         mainLayout = new QHBoxLayout();
25 //        mainLayout->setContentsMargins(margins);
26         QPixmap temp = book->getCoverImage();
27         if(!temp.isNull()){
28             QLabel *image = new QLabel(this);
29             temp = temp.scaledToWidth(100);
30             image->setPixmap(temp);
31             image->setFixedWidth(temp.width());
32             mainLayout->addWidget(image);
33             mainLayout->setAlignment(image, Qt::AlignLeft | Qt::AlignTop);
34         }
35
36         connect(thisBook, SIGNAL(coverImageAdded()), this, SLOT(coverImageAdded()));
37
38         QVBoxLayout *subLayout = new QVBoxLayout();
39 //        subLayout->setContentsMargins(margins);
40         mainLayout->addLayout(subLayout);
41
42         QLabel *title = new QLabel(book->getTitle(), this);
43         title->setWordWrap(true);
44
45         subLayout->addWidget(title);
46
47         QLabel *author = new QLabel(book->getAuthor(), this);
48         author->setWordWrap(true);
49         author->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
50         subLayout->addWidget(author);
51
52         QString subject = book->getSubject();
53         if(!subject.isEmpty() && !subject.isNull()){
54             QLabel *category = new QLabel(subject, this);
55             category->setFont(QFont("Sans", 12));
56             category->setWordWrap(true);
57             category->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
58             subLayout->addWidget(category);
59         }
60         subLayout->setSpacing(0);
61         subLayout->addStretch(1);
62
63         QVBoxLayout *superLayout = new QVBoxLayout(this);
64
65
66         superLayout->addLayout(mainLayout);
67
68
69         QMargins margins(6, 4, 4, 4);
70         superLayout->setContentsMargins(margins);
71
72         setLayout(superLayout);
73
74         scroller = scrollable;
75
76         QString frameStyleSheet = "BookListItem{width: 100%;border: 3px solid #eeeeec;border-radius: 12px; background-color: #1a1a1a;}";
77
78         setStyleSheet(frameStyleSheet);
79
80     }
81 protected:
82     void mouseReleaseEvent(QMouseEvent *event){
83         if(scroller){
84             if(scroller->state() == QAbstractKineticScroller::Inactive)
85                 emit selected(thisBook);
86         }
87         QString frameStyleSheet = "BookListItem{width: 100%;border: 3px solid #eeeeec;border-radius: 12px; background-color: #1a1a1a;}";
88         setStyleSheet(frameStyleSheet);
89     }
90
91     void mousePressEvent(QMouseEvent *event){
92         QPalette palette;
93         palette.setCurrentColorGroup(QPalette::Active);
94         QBrush brush = palette.highlight();
95         QString frameStyleSheet = "BookListItem{width: 100%;border: 3px solid #eeeeec;border-radius: 12px; background-color: " + brush.color().name() + ";}";
96         setStyleSheet(frameStyleSheet);
97     }
98
99 private:
100     Book *thisBook;
101     QAbstractKineticScroller *scroller;
102     QHBoxLayout *mainLayout;
103     QFrame *separator;
104     QFrame *topSeparator;
105     QLinearGradient gradient;
106
107 signals:
108     void selected(Book*);
109
110 public slots:
111     void coverImageAdded(){
112         QPixmap temp = thisBook->getCoverImage();
113         if(!temp.isNull()){
114             QLabel *image = new QLabel(this);
115             temp = temp.scaledToWidth(100);
116             image->setPixmap(temp);
117             image->setFixedWidth(temp.width());
118             mainLayout->insertWidget(0, image);
119             mainLayout->setAlignment(image, Qt::AlignLeft | Qt::AlignTop);
120             adjustSize();
121         }
122     }
123 };
124
125 #endif