Changes to the startupview.
[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 <QAbstractKineticScroller>
12 #include "book.h"
13
14 //TODO: Some refactoring needed. Wordwrap on QLabels and QScrollArea don't mix well..
15
16 class BookListItem : public QWidget{
17     Q_OBJECT
18 public:
19     BookListItem(Book *book, QWidget *parent = 0, QAbstractKineticScroller *scrollable = 0) : QWidget(parent){
20         thisBook = book;
21
22         QHBoxLayout *mainLayout = new QHBoxLayout();
23         QMargins margins(0, 0, 0, 0);
24         mainLayout->setContentsMargins(margins);
25         QPixmap temp = book->getCoverImage();
26         if(!temp.isNull()){
27             QLabel *image = new QLabel(this);
28             temp = temp.scaledToWidth(100);
29             image->setPixmap(temp);
30             image->setFixedWidth(temp.width());
31             mainLayout->addWidget(image);
32             mainLayout->setAlignment(image, Qt::AlignLeft | Qt::AlignTop);
33         }
34
35
36
37         QVBoxLayout *subLayout = new QVBoxLayout();
38         subLayout->setContentsMargins(margins);
39         mainLayout->addLayout(subLayout);
40
41         QLabel *title = new QLabel(book->getTitle(), this);
42         title->setWordWrap(true);
43         title->setMinimumWidth(250);
44         title->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
45         subLayout->addWidget(title);
46         subLayout->setAlignment(title, Qt::AlignLeft);
47
48         QLabel *author = new QLabel(book->getAuthor(), this);
49         author->setWordWrap(true);
50         author->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
51         subLayout->addWidget(author);
52
53         QString subject = book->getSubject();
54         if(!subject.isEmpty() && !subject.isNull()){
55             QLabel *category = new QLabel(subject, this);
56             category->setFont(QFont("Sans", 12));
57             category->setWordWrap(true);
58             category->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
59             subLayout->addWidget(category);
60         }
61
62         QVBoxLayout *superLayout = new QVBoxLayout(this);
63         superLayout->addLayout(mainLayout);
64
65         QFrame *separator = new QFrame(this);
66         separator->setFrameStyle(QFrame::HLine | QFrame::Plain);
67         separator->setLineWidth(2);
68         QPalette separatorPalette = QApplication::palette();
69         separatorPalette.setColor(QPalette::WindowText, QColor(Qt::darkGray));
70         separator->setPalette(separatorPalette);
71         superLayout->addWidget(separator);
72
73
74         superLayout->setContentsMargins(margins);
75
76         setLayout(superLayout);
77         adjustSize(); 
78         setAutoFillBackground(true);
79
80         scroller = scrollable;
81
82         
83     }
84     ~BookListItem(){
85
86     }
87 protected:
88     void mouseReleaseEvent(QMouseEvent *event){
89         if(scroller){
90             if(scroller->state() == QAbstractKineticScroller::Inactive)
91                 emit selected(thisBook);
92         }
93         QPalette palette = QApplication::palette();
94         palette.setColor(QPalette::Window, palette.color(QPalette::Button));
95         setPalette(palette);
96     }
97     void mousePressEvent(QMouseEvent *event){
98         QPalette palette;
99         palette.setCurrentColorGroup(QPalette::Active);
100         QBrush brush = palette.highlight();
101         palette.setColor(QPalette::Window, brush.color());
102         setPalette(palette);
103     }
104
105 private:
106     Book *thisBook;
107     QAbstractKineticScroller *scroller;
108
109 signals:
110     void selected(Book*);
111
112 };
113
114 #endif