Refactoring. Added comments.
[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 //BookListItem is a widget which is used in lists to represent a book and show it's info.
16 class BookListItem : public QFrame{
17     Q_OBJECT
18 public:
19     //BookListItem initializes with a pointer to a Book and a pointer to the QAbstractKineticScroller
20     //of the list the BookListItem is in.
21     //The scroller is used to make it possible selecting BookListItem's.
22     BookListItem(Book *book, QAbstractKineticScroller *scrollable = 0, QWidget *parent = 0) : QFrame(parent){
23         thisBook = book;
24
25         mainLayout = new QHBoxLayout();
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         mainLayout->addLayout(subLayout);
40
41         QLabel *title = new QLabel(book->getTitle(), this);
42         title->setWordWrap(true);
43
44         subLayout->addWidget(title);
45
46         QLabel *author = new QLabel(book->getAuthor(), this);
47         author->setWordWrap(true);
48         author->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
49         subLayout->addWidget(author);
50
51         QString subject = book->getSubject();
52         if(!subject.isEmpty() && !subject.isNull()){
53             QLabel *category = new QLabel(subject, this);
54             category->setFont(QFont("Sans", 12));
55             category->setWordWrap(true);
56             category->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
57             subLayout->addWidget(category);
58         }
59         subLayout->setSpacing(0);
60         subLayout->addStretch(1);
61
62         QVBoxLayout *superLayout = new QVBoxLayout(this);
63
64
65         superLayout->addLayout(mainLayout);
66
67
68         QMargins margins(6, 4, 4, 4);
69         superLayout->setContentsMargins(margins);
70
71         setLayout(superLayout);
72
73         scroller = scrollable;
74
75         QString frameStyleSheet = "BookListItem{width: 100%;border: 3px solid #eeeeec;border-radius: 12px; background-color: #1a1a1a;}";
76
77         setStyleSheet(frameStyleSheet);
78
79     }
80 protected:
81     void mouseReleaseEvent(QMouseEvent *event){
82         if(scroller){
83             //If scroller state is Inactive the user has selected this BookListItem.
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