Fixes. About dialog. Books properly removed from library when file is deleted.
[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 <QContextMenuEvent>
14 #include "book.h"
15
16 //BookListItem is a widget which is used in lists to represent a book and show it's info.
17 class BookListItem : public QFrame{
18     Q_OBJECT
19 public:
20     //BookListItem initializes with a pointer to a Book and a pointer to the QAbstractKineticScroller
21     //of the list the BookListItem is in.
22     //The scroller is used to make it possible selecting BookListItem's.
23     BookListItem(Book *book = 0, QAbstractKineticScroller *scrollable = 0, QWidget *parent = 0) : QFrame(parent){
24         thisBook = book;
25
26         mainLayout = new QHBoxLayout();
27         QPixmap temp = book->getCoverImage();
28         if(!temp.isNull()){
29             QLabel *image = new QLabel(this);
30             temp = temp.scaledToWidth(100);
31             image->setPixmap(temp);
32             image->setFixedWidth(temp.width());
33             mainLayout->addWidget(image);
34             mainLayout->setAlignment(image, Qt::AlignLeft | Qt::AlignTop);
35         }
36
37         connect(thisBook, SIGNAL(coverImageAdded()), this, SLOT(coverImageAdded()));
38
39         QVBoxLayout *subLayout = new QVBoxLayout();
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
82     Book* book(){
83         return thisBook;
84     }
85 protected:
86     void mouseReleaseEvent(QMouseEvent *event){
87         if(scroller){
88             //If scroller state is Inactive the user has selected this BookListItem.
89             if(scroller->state() == QAbstractKineticScroller::Inactive)
90                 emit selected(thisBook);
91         }
92         QString frameStyleSheet = "BookListItem{width: 100%;border: 3px solid #eeeeec;border-radius: 12px; background-color: #1a1a1a;}";
93         setStyleSheet(frameStyleSheet);
94
95         QWidget::mouseReleaseEvent(event);
96     }
97
98     void mousePressEvent(QMouseEvent *event){
99         QPalette palette;
100         palette.setCurrentColorGroup(QPalette::Active);
101         QBrush brush = palette.highlight();
102         QString frameStyleSheet = "BookListItem{width: 100%;border: 3px solid #eeeeec;border-radius: 12px; background-color: " + brush.color().name() + ";}";
103         setStyleSheet(frameStyleSheet);
104
105         QWidget::mousePressEvent(event);
106     }
107
108
109 private:
110     Book *thisBook;
111     QAbstractKineticScroller *scroller;
112     QHBoxLayout *mainLayout;
113     QFrame *separator;
114     QFrame *topSeparator;
115     QLinearGradient gradient;
116
117 signals:
118     void selected(Book*);
119
120 public slots:
121     void coverImageAdded(){
122         QPixmap temp = thisBook->getCoverImage();
123         if(!temp.isNull()){
124             QLabel *image = new QLabel(this);
125             temp = temp.scaledToWidth(100);
126             image->setPixmap(temp);
127             image->setFixedWidth(temp.width());
128             mainLayout->insertWidget(0, image);
129             mainLayout->setAlignment(image, Qt::AlignLeft | Qt::AlignTop);
130             adjustSize();
131         }
132     }
133
134 };
135
136 #endif